blob: 02c3ab07f6ac960b60a4c406577b926619598c90 [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) {
140 Triple ThumbTriple;
141 Triple T = Obj.getArch(nullptr, &ThumbTriple);
142 return T.getArchName();
143}
144
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000145std::unique_ptr<DebugMap>
146MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary,
147 StringRef BinaryPath) {
Frederic Risseb85c8f2015-07-24 06:41:11 +0000148 loadMainBinarySymbols(MainBinary);
Frederic Riss2c69d362015-08-26 05:09:59 +0000149 Result =
150 make_unique<DebugMap>(BinaryHolder::getTriple(MainBinary), BinaryPath);
Frederic Riss896b2c52014-12-16 20:21:34 +0000151 MainBinaryStrings = MainBinary.getStringTableData();
Frederic Riss231f7142014-12-12 17:31:24 +0000152 for (const SymbolRef &Symbol : MainBinary.symbols()) {
153 const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
154 if (MainBinary.is64Bit())
155 handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI));
156 else
157 handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI));
158 }
159
160 resetParserState();
161 return std::move(Result);
162}
163
Frederic Riss5ba01d62015-08-31 00:29:09 +0000164// Table that maps Darwin's Mach-O stab constants to strings to allow printing.
165// llvm-nm has very similar code, the strings used here are however slightly
166// different and part of the interface of dsymutil (some project's build-systems
167// parse the ouptut of dsymutil -s), thus they shouldn't be changed.
168struct DarwinStabName {
169 uint8_t NType;
170 const char *Name;
171};
172
173static const struct DarwinStabName DarwinStabNames[] = {
174 {MachO::N_GSYM, "N_GSYM"}, {MachO::N_FNAME, "N_FNAME"},
175 {MachO::N_FUN, "N_FUN"}, {MachO::N_STSYM, "N_STSYM"},
176 {MachO::N_LCSYM, "N_LCSYM"}, {MachO::N_BNSYM, "N_BNSYM"},
177 {MachO::N_PC, "N_PC"}, {MachO::N_AST, "N_AST"},
178 {MachO::N_OPT, "N_OPT"}, {MachO::N_RSYM, "N_RSYM"},
179 {MachO::N_SLINE, "N_SLINE"}, {MachO::N_ENSYM, "N_ENSYM"},
180 {MachO::N_SSYM, "N_SSYM"}, {MachO::N_SO, "N_SO"},
181 {MachO::N_OSO, "N_OSO"}, {MachO::N_LSYM, "N_LSYM"},
182 {MachO::N_BINCL, "N_BINCL"}, {MachO::N_SOL, "N_SOL"},
183 {MachO::N_PARAMS, "N_PARAM"}, {MachO::N_VERSION, "N_VERS"},
184 {MachO::N_OLEVEL, "N_OLEV"}, {MachO::N_PSYM, "N_PSYM"},
185 {MachO::N_EINCL, "N_EINCL"}, {MachO::N_ENTRY, "N_ENTRY"},
186 {MachO::N_LBRAC, "N_LBRAC"}, {MachO::N_EXCL, "N_EXCL"},
187 {MachO::N_RBRAC, "N_RBRAC"}, {MachO::N_BCOMM, "N_BCOMM"},
188 {MachO::N_ECOMM, "N_ECOMM"}, {MachO::N_ECOML, "N_ECOML"},
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000189 {MachO::N_LENG, "N_LENG"}, {0, nullptr}};
Frederic Riss5ba01d62015-08-31 00:29:09 +0000190
191static const char *getDarwinStabString(uint8_t NType) {
192 for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
193 if (DarwinStabNames[i].NType == NType)
194 return DarwinStabNames[i].Name;
195 }
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000196 return nullptr;
Frederic Riss5ba01d62015-08-31 00:29:09 +0000197}
198
199void MachODebugMapParser::dumpSymTabHeader(raw_ostream &OS, StringRef Arch) {
200 OS << "-----------------------------------"
201 "-----------------------------------\n";
202 OS << "Symbol table for: '" << BinaryPath << "' (" << Arch.data() << ")\n";
203 OS << "-----------------------------------"
204 "-----------------------------------\n";
205 OS << "Index n_strx n_type n_sect n_desc n_value\n";
206 OS << "======== -------- ------------------ ------ ------ ----------------\n";
207}
208
209void MachODebugMapParser::dumpSymTabEntry(raw_ostream &OS, uint64_t Index,
210 uint32_t StringIndex, uint8_t Type,
211 uint8_t SectionIndex, uint16_t Flags,
212 uint64_t Value) {
Frederic Riss5ba01d62015-08-31 00:29:09 +0000213 // Index
214 OS << '[' << format_decimal(Index, 6) << "] "
215 // n_strx
216 << format_hex_no_prefix(StringIndex, 8) << ' '
217 // n_type...
218 << format_hex_no_prefix(Type, 2) << " (";
219
220 if (Type & MachO::N_STAB)
221 OS << left_justify(getDarwinStabString(Type), 13);
222 else {
223 if (Type & MachO::N_PEXT)
224 OS << "PEXT ";
225 else
226 OS << " ";
227 switch (Type & MachO::N_TYPE) {
228 case MachO::N_UNDF: // 0x0 undefined, n_sect == NO_SECT
229 OS << "UNDF";
230 break;
231 case MachO::N_ABS: // 0x2 absolute, n_sect == NO_SECT
232 OS << "ABS ";
233 break;
234 case MachO::N_SECT: // 0xe defined in section number n_sect
235 OS << "SECT";
236 break;
237 case MachO::N_PBUD: // 0xc prebound undefined (defined in a dylib)
238 OS << "PBUD";
239 break;
240 case MachO::N_INDR: // 0xa indirect
241 OS << "INDR";
242 break;
243 default:
244 OS << format_hex_no_prefix(Type, 2) << " ";
245 break;
246 }
247 if (Type & MachO::N_EXT)
248 OS << " EXT";
249 else
250 OS << " ";
251 }
252
253 OS << ") "
254 // n_sect
255 << format_hex_no_prefix(SectionIndex, 2) << " "
256 // n_desc
257 << format_hex_no_prefix(Flags, 4) << " "
258 // n_value
259 << format_hex_no_prefix(Value, 16);
260
261 const char *Name = &MainBinaryStrings.data()[StringIndex];
262 if (Name && Name[0])
263 OS << " '" << Name << "'";
264
265 OS << "\n";
266}
267
268void MachODebugMapParser::dumpOneBinaryStab(const MachOObjectFile &MainBinary,
269 StringRef BinaryPath) {
270 loadMainBinarySymbols(MainBinary);
271 MainBinaryStrings = MainBinary.getStringTableData();
272 raw_ostream &OS(llvm::outs());
273
Frederic Risse20f2882015-08-31 00:49:34 +0000274 dumpSymTabHeader(OS, getArchName(MainBinary));
Frederic Riss5ba01d62015-08-31 00:29:09 +0000275 uint64_t Idx = 0;
276 for (const SymbolRef &Symbol : MainBinary.symbols()) {
277 const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
278 if (MainBinary.is64Bit())
279 dumpSymTabEntry(OS, Idx, MainBinary.getSymbol64TableEntry(DRI));
280 else
281 dumpSymTabEntry(OS, Idx, MainBinary.getSymbolTableEntry(DRI));
282 Idx++;
283 }
284
285 OS << "\n\n";
286 resetParserState();
287}
288
Frederic Rissae0d4362015-08-05 22:33:28 +0000289static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) {
290 if (Archs.empty() ||
291 std::find(Archs.begin(), Archs.end(), "all") != Archs.end() ||
292 std::find(Archs.begin(), Archs.end(), "*") != Archs.end())
293 return true;
294
295 if (Arch.startswith("arm") && Arch != "arm64" &&
296 std::find(Archs.begin(), Archs.end(), "arm") != Archs.end())
297 return true;
298
299 return std::find(Archs.begin(), Archs.end(), Arch) != Archs.end();
300}
301
Frederic Riss5ba01d62015-08-31 00:29:09 +0000302bool MachODebugMapParser::dumpStab() {
303 auto MainBinOrError =
304 MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath);
305 if (auto Error = MainBinOrError.getError()) {
306 llvm::errs() << "Cannot get '" << BinaryPath
307 << "' as MachO file: " << Error.message() << "\n";
308 return false;
309 }
310
311 Triple T;
312 for (const auto *Binary : *MainBinOrError)
313 if (shouldLinkArch(Archs, Binary->getArch(nullptr, &T).getArchName()))
314 dumpOneBinaryStab(*Binary, BinaryPath);
315
316 return true;
317}
318
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000319/// This main parsing routine tries to open the main binary and if
320/// successful iterates over the STAB entries. The real parsing is
321/// done in handleStabSymbolTableEntry.
322ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() {
323 auto MainBinOrError =
324 MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath);
325 if (auto Error = MainBinOrError.getError())
326 return Error;
327
328 std::vector<std::unique_ptr<DebugMap>> Results;
Frederic Rissae0d4362015-08-05 22:33:28 +0000329 Triple T;
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000330 for (const auto *Binary : *MainBinOrError)
Frederic Rissae0d4362015-08-05 22:33:28 +0000331 if (shouldLinkArch(Archs, Binary->getArch(nullptr, &T).getArchName()))
332 Results.push_back(parseOneBinary(*Binary, BinaryPath));
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000333
334 return std::move(Results);
335}
336
Frederic Riss231f7142014-12-12 17:31:24 +0000337/// Interpret the STAB entries to fill the DebugMap.
338void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
339 uint8_t Type,
340 uint8_t SectionIndex,
341 uint16_t Flags,
342 uint64_t Value) {
343 if (!(Type & MachO::N_STAB))
344 return;
345
Frederic Riss896b2c52014-12-16 20:21:34 +0000346 const char *Name = &MainBinaryStrings.data()[StringIndex];
Frederic Riss231f7142014-12-12 17:31:24 +0000347
348 // An N_OSO entry represents the start of a new object file description.
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000349 if (Type == MachO::N_OSO) {
350 sys::TimeValue Timestamp;
351 Timestamp.fromEpochTime(Value);
352 return switchToNewDebugMapObject(Name, Timestamp);
353 }
Frederic Riss231f7142014-12-12 17:31:24 +0000354
355 // If the last N_OSO object file wasn't found,
356 // CurrentDebugMapObject will be null. Do not update anything
357 // until we find the next valid N_OSO entry.
358 if (!CurrentDebugMapObject)
359 return;
360
Frederic Riss912d0f12015-03-15 01:29:30 +0000361 uint32_t Size = 0;
Frederic Riss231f7142014-12-12 17:31:24 +0000362 switch (Type) {
363 case MachO::N_GSYM:
364 // This is a global variable. We need to query the main binary
365 // symbol table to find its address as it might not be in the
366 // debug map (for common symbols).
367 Value = getMainBinarySymbolAddress(Name);
Frederic Riss231f7142014-12-12 17:31:24 +0000368 break;
369 case MachO::N_FUN:
Frederic Riss912d0f12015-03-15 01:29:30 +0000370 // Functions are scopes in STABS. They have an end marker that
371 // contains the function size.
372 if (Name[0] == '\0') {
373 Size = Value;
374 Value = CurrentFunctionAddress;
375 Name = CurrentFunctionName;
376 break;
377 } else {
378 CurrentFunctionName = Name;
379 CurrentFunctionAddress = Value;
Frederic Riss231f7142014-12-12 17:31:24 +0000380 return;
Frederic Riss912d0f12015-03-15 01:29:30 +0000381 }
Frederic Riss231f7142014-12-12 17:31:24 +0000382 case MachO::N_STSYM:
383 break;
384 default:
385 return;
386 }
387
388 auto ObjectSymIt = CurrentObjectAddresses.find(Name);
389 if (ObjectSymIt == CurrentObjectAddresses.end())
390 return Warning("could not find object file symbol for symbol " +
391 Twine(Name));
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000392 if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value,
Frederic Riss912d0f12015-03-15 01:29:30 +0000393 Size))
Frederic Riss231f7142014-12-12 17:31:24 +0000394 return Warning(Twine("failed to insert symbol '") + Name +
395 "' in the debug map.");
396}
397
398/// Load the current object file symbols into CurrentObjectAddresses.
Frederic Risseb85c8f2015-07-24 06:41:11 +0000399void MachODebugMapParser::loadCurrentObjectFileSymbols(
400 const object::MachOObjectFile &Obj) {
Frederic Riss231f7142014-12-12 17:31:24 +0000401 CurrentObjectAddresses.clear();
Frederic Riss231f7142014-12-12 17:31:24 +0000402
Frederic Risseb85c8f2015-07-24 06:41:11 +0000403 for (auto Sym : Obj.symbols()) {
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000404 uint64_t Addr = Sym.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000405 ErrorOr<StringRef> Name = Sym.getName();
406 if (!Name)
407 continue;
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()) {
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000440 SymbolRef::Type Type = Sym.getType();
Frederic Riss231f7142014-12-12 17:31:24 +0000441 // Skip undefined and STAB entries.
Rafael Espindola2fa80cc2015-06-26 12:18:49 +0000442 if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown))
Frederic Riss231f7142014-12-12 17:31:24 +0000443 continue;
Frederic Riss231f7142014-12-12 17:31:24 +0000444 // The only symbols of interest are the global variables. These
445 // are the only ones that need to be queried because the address
446 // of common data won't be described in the debug map. All other
447 // addresses should be fetched for the debug map.
Rafael Espindola8bab8892015-08-07 23:27:14 +0000448 if (!(Sym.getFlags() & SymbolRef::SF_Global))
449 continue;
450 ErrorOr<section_iterator> SectionOrErr = Sym.getSection();
451 if (!SectionOrErr)
452 continue;
453 Section = *SectionOrErr;
454 if (Section == MainBinary.section_end() || Section->isText())
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000455 continue;
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000456 uint64_t Addr = Sym.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000457 ErrorOr<StringRef> NameOrErr = Sym.getName();
458 if (!NameOrErr)
459 continue;
460 StringRef Name = *NameOrErr;
461 if (Name.size() == 0 || Name[0] == '\0')
Frederic Riss231f7142014-12-12 17:31:24 +0000462 continue;
463 MainBinarySymbolAddresses[Name] = Addr;
464 }
465}
466
467namespace llvm {
468namespace dsymutil {
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000469llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Rissae0d4362015-08-05 22:33:28 +0000470parseDebugMap(StringRef InputFile, ArrayRef<std::string> Archs,
471 StringRef PrependPath, bool Verbose, bool InputIsYAML) {
Frederic Riss90e0bd92015-06-03 20:29:24 +0000472 if (!InputIsYAML) {
Frederic Rissae0d4362015-08-05 22:33:28 +0000473 MachODebugMapParser Parser(InputFile, Archs, PrependPath, Verbose);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000474 return Parser.parse();
475 } else {
Frederic Riss4d0ba662015-06-05 20:27:04 +0000476 return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
Frederic Riss90e0bd92015-06-03 20:29:24 +0000477 }
Frederic Riss231f7142014-12-12 17:31:24 +0000478}
Frederic Riss5ba01d62015-08-31 00:29:09 +0000479
480bool dumpStab(StringRef InputFile, ArrayRef<std::string> Archs,
481 StringRef PrependPath) {
482 MachODebugMapParser Parser(InputFile, Archs, PrependPath, false);
483 return Parser.dumpStab();
484}
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000485} // namespace dsymutil
486} // namespace llvm