blob: 051c1e941ef00f85a8169c993025a87c82292e55 [file] [log] [blame]
Frederic Riss231f7142014-12-12 17:31:24 +00001//===- tools/dsymutil/MachODebugMapParser.cpp - Parse STABS debug maps ----===//
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
Frederic Riss43988502015-01-05 21:29:28 +000010#include "BinaryHolder.h"
Frederic Riss231f7142014-12-12 17:31:24 +000011#include "DebugMap.h"
12#include "dsymutil.h"
Frederic Riss841b1732015-12-11 17:50:37 +000013#include "llvm/ADT/Optional.h"
Frederic Riss231f7142014-12-12 17:31:24 +000014#include "llvm/Object/MachO.h"
15#include "llvm/Support/Path.h"
16#include "llvm/Support/raw_ostream.h"
17
18namespace {
19using namespace llvm;
20using namespace llvm::dsymutil;
21using namespace llvm::object;
22
23class MachODebugMapParser {
24public:
Frederic Rissae0d4362015-08-05 22:33:28 +000025 MachODebugMapParser(StringRef BinaryPath, ArrayRef<std::string> Archs,
26 StringRef PathPrefix = "", bool Verbose = false)
27 : BinaryPath(BinaryPath), Archs(Archs.begin(), Archs.end()),
28 PathPrefix(PathPrefix), MainBinaryHolder(Verbose),
29 CurrentObjectHolder(Verbose), CurrentDebugMapObject(nullptr) {}
Frederic Riss231f7142014-12-12 17:31:24 +000030
Frederic Riss4dd3e0c2015-08-05 18:27:44 +000031 /// \brief Parses and returns the DebugMaps of the input binary.
32 /// The binary contains multiple maps in case it is a universal
33 /// binary.
Frederic Riss231f7142014-12-12 17:31:24 +000034 /// \returns an error in case the provided BinaryPath doesn't exist
35 /// or isn't of a supported type.
Frederic Riss4dd3e0c2015-08-05 18:27:44 +000036 ErrorOr<std::vector<std::unique_ptr<DebugMap>>> parse();
Frederic Riss231f7142014-12-12 17:31:24 +000037
Frederic Riss5ba01d62015-08-31 00:29:09 +000038 /// Walk the symbol table and dump it.
39 bool dumpStab();
40
Frederic Riss231f7142014-12-12 17:31:24 +000041private:
42 std::string BinaryPath;
Frederic Rissae0d4362015-08-05 22:33:28 +000043 SmallVector<StringRef, 1> Archs;
Frederic Riss231f7142014-12-12 17:31:24 +000044 std::string PathPrefix;
45
Frederic Riss43988502015-01-05 21:29:28 +000046 /// Owns the MemoryBuffer for the main binary.
47 BinaryHolder MainBinaryHolder;
Frederic Riss231f7142014-12-12 17:31:24 +000048 /// Map of the binary symbol addresses.
49 StringMap<uint64_t> MainBinarySymbolAddresses;
Frederic Riss896b2c52014-12-16 20:21:34 +000050 StringRef MainBinaryStrings;
Frederic Riss231f7142014-12-12 17:31:24 +000051 /// The constructed DebugMap.
52 std::unique_ptr<DebugMap> Result;
53
Frederic Riss43988502015-01-05 21:29:28 +000054 /// Owns the MemoryBuffer for the currently handled object file.
55 BinaryHolder CurrentObjectHolder;
Frederic Riss231f7142014-12-12 17:31:24 +000056 /// Map of the currently processed object file symbol addresses.
Frederic Riss841b1732015-12-11 17:50:37 +000057 StringMap<Optional<uint64_t>> CurrentObjectAddresses;
Frederic Riss231f7142014-12-12 17:31:24 +000058 /// Element of the debug map corresponfing to the current object file.
59 DebugMapObject *CurrentDebugMapObject;
60
Frederic Riss912d0f12015-03-15 01:29:30 +000061 /// Holds function info while function scope processing.
62 const char *CurrentFunctionName;
63 uint64_t CurrentFunctionAddress;
64
Frederic Riss4dd3e0c2015-08-05 18:27:44 +000065 std::unique_ptr<DebugMap> parseOneBinary(const MachOObjectFile &MainBinary,
66 StringRef BinaryPath);
67
Frederic Riss9ccfddc2015-07-22 23:24:00 +000068 void switchToNewDebugMapObject(StringRef Filename, sys::TimeValue Timestamp);
Frederic Riss231f7142014-12-12 17:31:24 +000069 void resetParserState();
70 uint64_t getMainBinarySymbolAddress(StringRef Name);
Frederic Risseb85c8f2015-07-24 06:41:11 +000071 void loadMainBinarySymbols(const MachOObjectFile &MainBinary);
72 void loadCurrentObjectFileSymbols(const object::MachOObjectFile &Obj);
Frederic Riss231f7142014-12-12 17:31:24 +000073 void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type,
74 uint8_t SectionIndex, uint16_t Flags,
75 uint64_t Value);
76
77 template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) {
78 handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
79 STE.n_value);
80 }
Frederic Riss5ba01d62015-08-31 00:29:09 +000081
82 /// Dump the symbol table output header.
83 void dumpSymTabHeader(raw_ostream &OS, StringRef Arch);
84
85 /// Dump the contents of nlist entries.
86 void dumpSymTabEntry(raw_ostream &OS, uint64_t Index, uint32_t StringIndex,
87 uint8_t Type, uint8_t SectionIndex, uint16_t Flags,
88 uint64_t Value);
89
90 template <typename STEType>
91 void dumpSymTabEntry(raw_ostream &OS, uint64_t Index, const STEType &STE) {
92 dumpSymTabEntry(OS, Index, STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
93 STE.n_value);
94 }
95 void dumpOneBinaryStab(const MachOObjectFile &MainBinary,
96 StringRef BinaryPath);
Frederic Riss231f7142014-12-12 17:31:24 +000097};
98
99static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; }
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000100} // anonymous namespace
Frederic Riss231f7142014-12-12 17:31:24 +0000101
Frederic Riss231f7142014-12-12 17:31:24 +0000102/// Reset the parser state coresponding to the current object
103/// file. This is to be called after an object file is finished
104/// processing.
105void MachODebugMapParser::resetParserState() {
Frederic Riss231f7142014-12-12 17:31:24 +0000106 CurrentObjectAddresses.clear();
107 CurrentDebugMapObject = nullptr;
108}
109
110/// Create a new DebugMapObject. This function resets the state of the
111/// parser that was referring to the last object file and sets
112/// everything up to add symbols to the new one.
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000113void MachODebugMapParser::switchToNewDebugMapObject(StringRef Filename,
114 sys::TimeValue Timestamp) {
Frederic Riss231f7142014-12-12 17:31:24 +0000115 resetParserState();
116
117 SmallString<80> Path(PathPrefix);
118 sys::path::append(Path, Filename);
119
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000120 auto MachOOrError =
Frederic Risseb85c8f2015-07-24 06:41:11 +0000121 CurrentObjectHolder.GetFilesAs<MachOObjectFile>(Path, Timestamp);
Frederic Riss231f7142014-12-12 17:31:24 +0000122 if (auto Error = MachOOrError.getError()) {
123 Warning(Twine("cannot open debug object \"") + Path.str() + "\": " +
124 Error.message() + "\n");
125 return;
126 }
127
Frederic Risseb85c8f2015-07-24 06:41:11 +0000128 auto ErrOrAchObj =
129 CurrentObjectHolder.GetAs<MachOObjectFile>(Result->getTriple());
130 if (auto Err = ErrOrAchObj.getError()) {
131 return Warning(Twine("cannot open debug object \"") + Path.str() + "\": " +
132 Err.message() + "\n");
133 }
134
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000135 CurrentDebugMapObject = &Result->addDebugMapObject(Path, Timestamp);
Frederic Risseb85c8f2015-07-24 06:41:11 +0000136 loadCurrentObjectFileSymbols(*ErrOrAchObj);
Frederic Riss231f7142014-12-12 17:31:24 +0000137}
138
Frederic Riss5ba01d62015-08-31 00:29:09 +0000139static std::string getArchName(const object::MachOObjectFile &Obj) {
Tim Northover9e8eb412016-04-22 23:21:13 +0000140 Triple T = Obj.getArchTriple();
Frederic Riss5ba01d62015-08-31 00:29:09 +0000141 return T.getArchName();
142}
143
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000144std::unique_ptr<DebugMap>
145MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary,
146 StringRef BinaryPath) {
Frederic Risseb85c8f2015-07-24 06:41:11 +0000147 loadMainBinarySymbols(MainBinary);
Tim Northover9e8eb412016-04-22 23:21:13 +0000148 Result = make_unique<DebugMap>(MainBinary.getArchTriple(), BinaryPath);
Frederic Riss896b2c52014-12-16 20:21:34 +0000149 MainBinaryStrings = MainBinary.getStringTableData();
Frederic Riss231f7142014-12-12 17:31:24 +0000150 for (const SymbolRef &Symbol : MainBinary.symbols()) {
151 const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
152 if (MainBinary.is64Bit())
153 handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI));
154 else
155 handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI));
156 }
157
158 resetParserState();
159 return std::move(Result);
160}
161
Frederic Riss5ba01d62015-08-31 00:29:09 +0000162// Table that maps Darwin's Mach-O stab constants to strings to allow printing.
163// llvm-nm has very similar code, the strings used here are however slightly
164// different and part of the interface of dsymutil (some project's build-systems
165// parse the ouptut of dsymutil -s), thus they shouldn't be changed.
166struct DarwinStabName {
167 uint8_t NType;
168 const char *Name;
169};
170
171static const struct DarwinStabName DarwinStabNames[] = {
172 {MachO::N_GSYM, "N_GSYM"}, {MachO::N_FNAME, "N_FNAME"},
173 {MachO::N_FUN, "N_FUN"}, {MachO::N_STSYM, "N_STSYM"},
174 {MachO::N_LCSYM, "N_LCSYM"}, {MachO::N_BNSYM, "N_BNSYM"},
175 {MachO::N_PC, "N_PC"}, {MachO::N_AST, "N_AST"},
176 {MachO::N_OPT, "N_OPT"}, {MachO::N_RSYM, "N_RSYM"},
177 {MachO::N_SLINE, "N_SLINE"}, {MachO::N_ENSYM, "N_ENSYM"},
178 {MachO::N_SSYM, "N_SSYM"}, {MachO::N_SO, "N_SO"},
179 {MachO::N_OSO, "N_OSO"}, {MachO::N_LSYM, "N_LSYM"},
180 {MachO::N_BINCL, "N_BINCL"}, {MachO::N_SOL, "N_SOL"},
181 {MachO::N_PARAMS, "N_PARAM"}, {MachO::N_VERSION, "N_VERS"},
182 {MachO::N_OLEVEL, "N_OLEV"}, {MachO::N_PSYM, "N_PSYM"},
183 {MachO::N_EINCL, "N_EINCL"}, {MachO::N_ENTRY, "N_ENTRY"},
184 {MachO::N_LBRAC, "N_LBRAC"}, {MachO::N_EXCL, "N_EXCL"},
185 {MachO::N_RBRAC, "N_RBRAC"}, {MachO::N_BCOMM, "N_BCOMM"},
186 {MachO::N_ECOMM, "N_ECOMM"}, {MachO::N_ECOML, "N_ECOML"},
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000187 {MachO::N_LENG, "N_LENG"}, {0, nullptr}};
Frederic Riss5ba01d62015-08-31 00:29:09 +0000188
189static const char *getDarwinStabString(uint8_t NType) {
190 for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
191 if (DarwinStabNames[i].NType == NType)
192 return DarwinStabNames[i].Name;
193 }
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000194 return nullptr;
Frederic Riss5ba01d62015-08-31 00:29:09 +0000195}
196
197void MachODebugMapParser::dumpSymTabHeader(raw_ostream &OS, StringRef Arch) {
198 OS << "-----------------------------------"
199 "-----------------------------------\n";
200 OS << "Symbol table for: '" << BinaryPath << "' (" << Arch.data() << ")\n";
201 OS << "-----------------------------------"
202 "-----------------------------------\n";
203 OS << "Index n_strx n_type n_sect n_desc n_value\n";
204 OS << "======== -------- ------------------ ------ ------ ----------------\n";
205}
206
207void MachODebugMapParser::dumpSymTabEntry(raw_ostream &OS, uint64_t Index,
208 uint32_t StringIndex, uint8_t Type,
209 uint8_t SectionIndex, uint16_t Flags,
210 uint64_t Value) {
Frederic Riss5ba01d62015-08-31 00:29:09 +0000211 // Index
212 OS << '[' << format_decimal(Index, 6) << "] "
213 // n_strx
214 << format_hex_no_prefix(StringIndex, 8) << ' '
215 // n_type...
216 << format_hex_no_prefix(Type, 2) << " (";
217
218 if (Type & MachO::N_STAB)
219 OS << left_justify(getDarwinStabString(Type), 13);
220 else {
221 if (Type & MachO::N_PEXT)
222 OS << "PEXT ";
223 else
224 OS << " ";
225 switch (Type & MachO::N_TYPE) {
226 case MachO::N_UNDF: // 0x0 undefined, n_sect == NO_SECT
227 OS << "UNDF";
228 break;
229 case MachO::N_ABS: // 0x2 absolute, n_sect == NO_SECT
230 OS << "ABS ";
231 break;
232 case MachO::N_SECT: // 0xe defined in section number n_sect
233 OS << "SECT";
234 break;
235 case MachO::N_PBUD: // 0xc prebound undefined (defined in a dylib)
236 OS << "PBUD";
237 break;
238 case MachO::N_INDR: // 0xa indirect
239 OS << "INDR";
240 break;
241 default:
242 OS << format_hex_no_prefix(Type, 2) << " ";
243 break;
244 }
245 if (Type & MachO::N_EXT)
246 OS << " EXT";
247 else
248 OS << " ";
249 }
250
251 OS << ") "
252 // n_sect
253 << format_hex_no_prefix(SectionIndex, 2) << " "
254 // n_desc
255 << format_hex_no_prefix(Flags, 4) << " "
256 // n_value
257 << format_hex_no_prefix(Value, 16);
258
259 const char *Name = &MainBinaryStrings.data()[StringIndex];
260 if (Name && Name[0])
261 OS << " '" << Name << "'";
262
263 OS << "\n";
264}
265
266void MachODebugMapParser::dumpOneBinaryStab(const MachOObjectFile &MainBinary,
267 StringRef BinaryPath) {
268 loadMainBinarySymbols(MainBinary);
269 MainBinaryStrings = MainBinary.getStringTableData();
270 raw_ostream &OS(llvm::outs());
271
Frederic Risse20f2882015-08-31 00:49:34 +0000272 dumpSymTabHeader(OS, getArchName(MainBinary));
Frederic Riss5ba01d62015-08-31 00:29:09 +0000273 uint64_t Idx = 0;
274 for (const SymbolRef &Symbol : MainBinary.symbols()) {
275 const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
276 if (MainBinary.is64Bit())
277 dumpSymTabEntry(OS, Idx, MainBinary.getSymbol64TableEntry(DRI));
278 else
279 dumpSymTabEntry(OS, Idx, MainBinary.getSymbolTableEntry(DRI));
280 Idx++;
281 }
282
283 OS << "\n\n";
284 resetParserState();
285}
286
Frederic Rissae0d4362015-08-05 22:33:28 +0000287static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) {
David Majnemer0d955d02016-08-11 22:21:41 +0000288 if (Archs.empty() || is_contained(Archs, "all") || is_contained(Archs, "*"))
Frederic Rissae0d4362015-08-05 22:33:28 +0000289 return true;
290
David Majnemer0d955d02016-08-11 22:21:41 +0000291 if (Arch.startswith("arm") && Arch != "arm64" && is_contained(Archs, "arm"))
Frederic Rissae0d4362015-08-05 22:33:28 +0000292 return true;
293
Frederic Riss5af2c002016-05-09 06:01:12 +0000294 SmallString<16> ArchName = Arch;
295 if (Arch.startswith("thumb"))
296 ArchName = ("arm" + Arch.substr(5)).str();
297
David Majnemer0d955d02016-08-11 22:21:41 +0000298 return is_contained(Archs, ArchName);
Frederic Rissae0d4362015-08-05 22:33:28 +0000299}
300
Frederic Riss5ba01d62015-08-31 00:29:09 +0000301bool MachODebugMapParser::dumpStab() {
302 auto MainBinOrError =
303 MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath);
304 if (auto Error = MainBinOrError.getError()) {
305 llvm::errs() << "Cannot get '" << BinaryPath
306 << "' as MachO file: " << Error.message() << "\n";
307 return false;
308 }
309
Frederic Riss5ba01d62015-08-31 00:29:09 +0000310 for (const auto *Binary : *MainBinOrError)
Tim Northover9e8eb412016-04-22 23:21:13 +0000311 if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName()))
Frederic Riss5ba01d62015-08-31 00:29:09 +0000312 dumpOneBinaryStab(*Binary, BinaryPath);
313
314 return true;
315}
316
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000317/// This main parsing routine tries to open the main binary and if
318/// successful iterates over the STAB entries. The real parsing is
319/// done in handleStabSymbolTableEntry.
320ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() {
321 auto MainBinOrError =
322 MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath);
323 if (auto Error = MainBinOrError.getError())
324 return Error;
325
326 std::vector<std::unique_ptr<DebugMap>> Results;
327 for (const auto *Binary : *MainBinOrError)
Tim Northover9e8eb412016-04-22 23:21:13 +0000328 if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName()))
Frederic Rissae0d4362015-08-05 22:33:28 +0000329 Results.push_back(parseOneBinary(*Binary, BinaryPath));
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000330
331 return std::move(Results);
332}
333
Frederic Riss231f7142014-12-12 17:31:24 +0000334/// Interpret the STAB entries to fill the DebugMap.
335void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
336 uint8_t Type,
337 uint8_t SectionIndex,
338 uint16_t Flags,
339 uint64_t Value) {
340 if (!(Type & MachO::N_STAB))
341 return;
342
Frederic Riss896b2c52014-12-16 20:21:34 +0000343 const char *Name = &MainBinaryStrings.data()[StringIndex];
Frederic Riss231f7142014-12-12 17:31:24 +0000344
345 // An N_OSO entry represents the start of a new object file description.
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000346 if (Type == MachO::N_OSO) {
347 sys::TimeValue Timestamp;
348 Timestamp.fromEpochTime(Value);
349 return switchToNewDebugMapObject(Name, Timestamp);
350 }
Frederic Riss231f7142014-12-12 17:31:24 +0000351
352 // If the last N_OSO object file wasn't found,
353 // CurrentDebugMapObject will be null. Do not update anything
354 // until we find the next valid N_OSO entry.
355 if (!CurrentDebugMapObject)
356 return;
357
Frederic Riss912d0f12015-03-15 01:29:30 +0000358 uint32_t Size = 0;
Frederic Riss231f7142014-12-12 17:31:24 +0000359 switch (Type) {
360 case MachO::N_GSYM:
361 // This is a global variable. We need to query the main binary
362 // symbol table to find its address as it might not be in the
363 // debug map (for common symbols).
364 Value = getMainBinarySymbolAddress(Name);
Frederic Riss231f7142014-12-12 17:31:24 +0000365 break;
366 case MachO::N_FUN:
Frederic Riss912d0f12015-03-15 01:29:30 +0000367 // Functions are scopes in STABS. They have an end marker that
368 // contains the function size.
369 if (Name[0] == '\0') {
370 Size = Value;
371 Value = CurrentFunctionAddress;
372 Name = CurrentFunctionName;
373 break;
374 } else {
375 CurrentFunctionName = Name;
376 CurrentFunctionAddress = Value;
Frederic Riss231f7142014-12-12 17:31:24 +0000377 return;
Frederic Riss912d0f12015-03-15 01:29:30 +0000378 }
Frederic Riss231f7142014-12-12 17:31:24 +0000379 case MachO::N_STSYM:
380 break;
381 default:
382 return;
383 }
384
385 auto ObjectSymIt = CurrentObjectAddresses.find(Name);
386 if (ObjectSymIt == CurrentObjectAddresses.end())
387 return Warning("could not find object file symbol for symbol " +
388 Twine(Name));
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000389 if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value,
Frederic Riss912d0f12015-03-15 01:29:30 +0000390 Size))
Frederic Riss231f7142014-12-12 17:31:24 +0000391 return Warning(Twine("failed to insert symbol '") + Name +
392 "' in the debug map.");
393}
394
395/// Load the current object file symbols into CurrentObjectAddresses.
Frederic Risseb85c8f2015-07-24 06:41:11 +0000396void MachODebugMapParser::loadCurrentObjectFileSymbols(
397 const object::MachOObjectFile &Obj) {
Frederic Riss231f7142014-12-12 17:31:24 +0000398 CurrentObjectAddresses.clear();
Frederic Riss231f7142014-12-12 17:31:24 +0000399
Frederic Risseb85c8f2015-07-24 06:41:11 +0000400 for (auto Sym : Obj.symbols()) {
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000401 uint64_t Addr = Sym.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000402 Expected<StringRef> Name = Sym.getName();
403 if (!Name) {
404 // TODO: Actually report errors helpfully.
405 consumeError(Name.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000406 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000407 }
Frederic Riss6c8521a2016-01-31 04:29:34 +0000408 // The value of some categories of symbols isn't meaningful. For
409 // example common symbols store their size in the value field, not
410 // their address. Absolute symbols have a fixed address that can
411 // conflict with standard symbols. These symbols (especially the
412 // common ones), might still be referenced by relocations. These
413 // relocations will use the symbol itself, and won't need an
414 // object file address. The object file address field is optional
415 // in the DebugMap, leave it unassigned for these symbols.
416 if (Sym.getFlags() & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))
Frederic Riss841b1732015-12-11 17:50:37 +0000417 CurrentObjectAddresses[*Name] = None;
418 else
419 CurrentObjectAddresses[*Name] = Addr;
Frederic Riss231f7142014-12-12 17:31:24 +0000420 }
421}
422
423/// Lookup a symbol address in the main binary symbol table. The
424/// parser only needs to query common symbols, thus not every symbol's
425/// address is available through this function.
426uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
427 auto Sym = MainBinarySymbolAddresses.find(Name);
428 if (Sym == MainBinarySymbolAddresses.end())
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000429 return 0;
Frederic Riss231f7142014-12-12 17:31:24 +0000430 return Sym->second;
431}
432
433/// Load the interesting main binary symbols' addresses into
434/// MainBinarySymbolAddresses.
Frederic Risseb85c8f2015-07-24 06:41:11 +0000435void MachODebugMapParser::loadMainBinarySymbols(
436 const MachOObjectFile &MainBinary) {
Frederic Riss43988502015-01-05 21:29:28 +0000437 section_iterator Section = MainBinary.section_end();
Frederic Risseb85c8f2015-07-24 06:41:11 +0000438 MainBinarySymbolAddresses.clear();
Frederic Riss43988502015-01-05 21:29:28 +0000439 for (const auto &Sym : MainBinary.symbols()) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000440 Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
441 if (!TypeOrErr) {
442 // TODO: Actually report errors helpfully.
443 consumeError(TypeOrErr.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000444 continue;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000445 }
Kevin Enderby5afbc1c2016-03-23 20:27:00 +0000446 SymbolRef::Type Type = *TypeOrErr;
Frederic Riss231f7142014-12-12 17:31:24 +0000447 // Skip undefined and STAB entries.
David Majnemerf5c06892016-10-31 17:11:23 +0000448 if ((Type == SymbolRef::ST_Debug) || (Type == SymbolRef::ST_Unknown))
Frederic Riss231f7142014-12-12 17:31:24 +0000449 continue;
Frederic Riss231f7142014-12-12 17:31:24 +0000450 // The only symbols of interest are the global variables. These
451 // are the only ones that need to be queried because the address
452 // of common data won't be described in the debug map. All other
453 // addresses should be fetched for the debug map.
Rafael Espindola8bab8892015-08-07 23:27:14 +0000454 if (!(Sym.getFlags() & SymbolRef::SF_Global))
455 continue;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000456 Expected<section_iterator> SectionOrErr = Sym.getSection();
457 if (!SectionOrErr) {
458 // TODO: Actually report errors helpfully.
459 consumeError(SectionOrErr.takeError());
Rafael Espindola8bab8892015-08-07 23:27:14 +0000460 continue;
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000461 }
Rafael Espindola8bab8892015-08-07 23:27:14 +0000462 Section = *SectionOrErr;
463 if (Section == MainBinary.section_end() || Section->isText())
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000464 continue;
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000465 uint64_t Addr = Sym.getValue();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000466 Expected<StringRef> NameOrErr = Sym.getName();
467 if (!NameOrErr) {
468 // TODO: Actually report errors helpfully.
469 consumeError(NameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000470 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000471 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000472 StringRef Name = *NameOrErr;
473 if (Name.size() == 0 || Name[0] == '\0')
Frederic Riss231f7142014-12-12 17:31:24 +0000474 continue;
475 MainBinarySymbolAddresses[Name] = Addr;
476 }
477}
478
479namespace llvm {
480namespace dsymutil {
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000481llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Rissae0d4362015-08-05 22:33:28 +0000482parseDebugMap(StringRef InputFile, ArrayRef<std::string> Archs,
483 StringRef PrependPath, bool Verbose, bool InputIsYAML) {
Frederic Riss90e0bd92015-06-03 20:29:24 +0000484 if (!InputIsYAML) {
Frederic Rissae0d4362015-08-05 22:33:28 +0000485 MachODebugMapParser Parser(InputFile, Archs, PrependPath, Verbose);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000486 return Parser.parse();
487 } else {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000488 return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000489 }
Frederic Riss231f7142014-12-12 17:31:24 +0000490}
Frederic Riss5ba01d62015-08-31 00:29:09 +0000491
492bool dumpStab(StringRef InputFile, ArrayRef<std::string> Archs,
493 StringRef PrependPath) {
494 MachODebugMapParser Parser(InputFile, Archs, PrependPath, false);
495 return Parser.dumpStab();
496}
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000497} // namespace dsymutil
498} // namespace llvm