1 //===- 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 
10 #include "BinaryHolder.h"
11 #include "DebugMap.h"
12 #include "ErrorReporting.h"
13 #include "MachOUtils.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/Object/MachO.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 namespace {
20 using namespace llvm;
21 using namespace llvm::dsymutil;
22 using namespace llvm::object;
23 
24 class MachODebugMapParser {
25 public:
26   MachODebugMapParser(StringRef BinaryPath, ArrayRef<std::string> Archs,
27                       StringRef PathPrefix = "",
28                       bool PaperTrailWarnings = false, bool Verbose = false)
29       : BinaryPath(BinaryPath), Archs(Archs.begin(), Archs.end()),
30         PathPrefix(PathPrefix), PaperTrailWarnings(PaperTrailWarnings),
31         MainBinaryHolder(Verbose), CurrentObjectHolder(Verbose),
32         CurrentDebugMapObject(nullptr) {}
33 
34   /// Parses and returns the DebugMaps of the input binary. The binary contains
35   /// multiple maps in case it is a universal binary.
36   /// \returns an error in case the provided BinaryPath doesn't exist
37   /// or isn't of a supported type.
38   ErrorOr<std::vector<std::unique_ptr<DebugMap>>> parse();
39 
40   /// Walk the symbol table and dump it.
41   bool dumpStab();
42 
43 private:
44   std::string BinaryPath;
45   SmallVector<StringRef, 1> Archs;
46   std::string PathPrefix;
47   bool PaperTrailWarnings;
48 
49   /// Owns the MemoryBuffer for the main binary.
50   BinaryHolder MainBinaryHolder;
51   /// Map of the binary symbol addresses.
52   StringMap<uint64_t> MainBinarySymbolAddresses;
53   StringRef MainBinaryStrings;
54   /// The constructed DebugMap.
55   std::unique_ptr<DebugMap> Result;
56 
57   /// Owns the MemoryBuffer for the currently handled object file.
58   BinaryHolder CurrentObjectHolder;
59   /// Map of the currently processed object file symbol addresses.
60   StringMap<Optional<uint64_t>> CurrentObjectAddresses;
61   /// Element of the debug map corresponding to the current object file.
62   DebugMapObject *CurrentDebugMapObject;
63 
64   /// Holds function info while function scope processing.
65   const char *CurrentFunctionName;
66   uint64_t CurrentFunctionAddress;
67 
68   std::unique_ptr<DebugMap> parseOneBinary(const MachOObjectFile &MainBinary,
69                                            StringRef BinaryPath);
70 
71   void
72   switchToNewDebugMapObject(StringRef Filename,
73                             sys::TimePoint<std::chrono::seconds> Timestamp);
74   void resetParserState();
75   uint64_t getMainBinarySymbolAddress(StringRef Name);
76   std::vector<StringRef> getMainBinarySymbolNames(uint64_t Value);
77   void loadMainBinarySymbols(const MachOObjectFile &MainBinary);
78   void loadCurrentObjectFileSymbols(const object::MachOObjectFile &Obj);
79   void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type,
80                                   uint8_t SectionIndex, uint16_t Flags,
81                                   uint64_t Value);
82 
83   template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) {
84     handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
85                                STE.n_value);
86   }
87 
88   /// Dump the symbol table output header.
89   void dumpSymTabHeader(raw_ostream &OS, StringRef Arch);
90 
91   /// Dump the contents of nlist entries.
92   void dumpSymTabEntry(raw_ostream &OS, uint64_t Index, uint32_t StringIndex,
93                        uint8_t Type, uint8_t SectionIndex, uint16_t Flags,
94                        uint64_t Value);
95 
96   template <typename STEType>
97   void dumpSymTabEntry(raw_ostream &OS, uint64_t Index, const STEType &STE) {
98     dumpSymTabEntry(OS, Index, STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
99                     STE.n_value);
100   }
101   void dumpOneBinaryStab(const MachOObjectFile &MainBinary,
102                          StringRef BinaryPath);
103 
104   void Warning(const Twine &Msg, StringRef File = StringRef()) {
105     warn_ostream() << "("
106                    << MachOUtils::getArchName(Result->getTriple().getArchName())
107                    << ") " << File << " " << Msg << "\n";
108 
109     if (PaperTrailWarnings) {
110       if (!File.empty())
111         Result->addDebugMapObject(File, sys::TimePoint<std::chrono::seconds>());
112       if (Result->end() != Result->begin())
113         (*--Result->end())->addWarning(Msg.str());
114     }
115   }
116 };
117 
118 } // anonymous namespace
119 
120 /// Reset the parser state corresponding to the current object
121 /// file. This is to be called after an object file is finished
122 /// processing.
123 void MachODebugMapParser::resetParserState() {
124   CurrentObjectAddresses.clear();
125   CurrentDebugMapObject = nullptr;
126 }
127 
128 /// Create a new DebugMapObject. This function resets the state of the
129 /// parser that was referring to the last object file and sets
130 /// everything up to add symbols to the new one.
131 void MachODebugMapParser::switchToNewDebugMapObject(
132     StringRef Filename, sys::TimePoint<std::chrono::seconds> Timestamp) {
133   resetParserState();
134 
135   SmallString<80> Path(PathPrefix);
136   sys::path::append(Path, Filename);
137 
138   auto MachOOrError =
139       CurrentObjectHolder.GetFilesAs<MachOObjectFile>(Path, Timestamp);
140   if (auto Error = MachOOrError.getError()) {
141     Warning("unable to open object file: " + Error.message(), Path.str());
142     return;
143   }
144 
145   auto ErrOrAchObj =
146       CurrentObjectHolder.GetAs<MachOObjectFile>(Result->getTriple());
147   if (auto Error = ErrOrAchObj.getError()) {
148     Warning("unable to open object file: " + Error.message(), Path.str());
149     return;
150   }
151 
152   CurrentDebugMapObject =
153       &Result->addDebugMapObject(Path, Timestamp, MachO::N_OSO);
154   loadCurrentObjectFileSymbols(*ErrOrAchObj);
155 }
156 
157 static std::string getArchName(const object::MachOObjectFile &Obj) {
158   Triple T = Obj.getArchTriple();
159   return T.getArchName();
160 }
161 
162 std::unique_ptr<DebugMap>
163 MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary,
164                                     StringRef BinaryPath) {
165   loadMainBinarySymbols(MainBinary);
166   Result = make_unique<DebugMap>(MainBinary.getArchTriple(), BinaryPath);
167   MainBinaryStrings = MainBinary.getStringTableData();
168   for (const SymbolRef &Symbol : MainBinary.symbols()) {
169     const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
170     if (MainBinary.is64Bit())
171       handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI));
172     else
173       handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI));
174   }
175 
176   resetParserState();
177   return std::move(Result);
178 }
179 
180 // Table that maps Darwin's Mach-O stab constants to strings to allow printing.
181 // llvm-nm has very similar code, the strings used here are however slightly
182 // different and part of the interface of dsymutil (some project's build-systems
183 // parse the ouptut of dsymutil -s), thus they shouldn't be changed.
184 struct DarwinStabName {
185   uint8_t NType;
186   const char *Name;
187 };
188 
189 static const struct DarwinStabName DarwinStabNames[] = {
190     {MachO::N_GSYM, "N_GSYM"},    {MachO::N_FNAME, "N_FNAME"},
191     {MachO::N_FUN, "N_FUN"},      {MachO::N_STSYM, "N_STSYM"},
192     {MachO::N_LCSYM, "N_LCSYM"},  {MachO::N_BNSYM, "N_BNSYM"},
193     {MachO::N_PC, "N_PC"},        {MachO::N_AST, "N_AST"},
194     {MachO::N_OPT, "N_OPT"},      {MachO::N_RSYM, "N_RSYM"},
195     {MachO::N_SLINE, "N_SLINE"},  {MachO::N_ENSYM, "N_ENSYM"},
196     {MachO::N_SSYM, "N_SSYM"},    {MachO::N_SO, "N_SO"},
197     {MachO::N_OSO, "N_OSO"},      {MachO::N_LSYM, "N_LSYM"},
198     {MachO::N_BINCL, "N_BINCL"},  {MachO::N_SOL, "N_SOL"},
199     {MachO::N_PARAMS, "N_PARAM"}, {MachO::N_VERSION, "N_VERS"},
200     {MachO::N_OLEVEL, "N_OLEV"},  {MachO::N_PSYM, "N_PSYM"},
201     {MachO::N_EINCL, "N_EINCL"},  {MachO::N_ENTRY, "N_ENTRY"},
202     {MachO::N_LBRAC, "N_LBRAC"},  {MachO::N_EXCL, "N_EXCL"},
203     {MachO::N_RBRAC, "N_RBRAC"},  {MachO::N_BCOMM, "N_BCOMM"},
204     {MachO::N_ECOMM, "N_ECOMM"},  {MachO::N_ECOML, "N_ECOML"},
205     {MachO::N_LENG, "N_LENG"},    {0, nullptr}};
206 
207 static const char *getDarwinStabString(uint8_t NType) {
208   for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
209     if (DarwinStabNames[i].NType == NType)
210       return DarwinStabNames[i].Name;
211   }
212   return nullptr;
213 }
214 
215 void MachODebugMapParser::dumpSymTabHeader(raw_ostream &OS, StringRef Arch) {
216   OS << "-----------------------------------"
217         "-----------------------------------\n";
218   OS << "Symbol table for: '" << BinaryPath << "' (" << Arch.data() << ")\n";
219   OS << "-----------------------------------"
220         "-----------------------------------\n";
221   OS << "Index    n_strx   n_type             n_sect n_desc n_value\n";
222   OS << "======== -------- ------------------ ------ ------ ----------------\n";
223 }
224 
225 void MachODebugMapParser::dumpSymTabEntry(raw_ostream &OS, uint64_t Index,
226                                           uint32_t StringIndex, uint8_t Type,
227                                           uint8_t SectionIndex, uint16_t Flags,
228                                           uint64_t Value) {
229   // Index
230   OS << '[' << format_decimal(Index, 6)
231      << "] "
232      // n_strx
233      << format_hex_no_prefix(StringIndex, 8)
234      << ' '
235      // n_type...
236      << format_hex_no_prefix(Type, 2) << " (";
237 
238   if (Type & MachO::N_STAB)
239     OS << left_justify(getDarwinStabString(Type), 13);
240   else {
241     if (Type & MachO::N_PEXT)
242       OS << "PEXT ";
243     else
244       OS << "     ";
245     switch (Type & MachO::N_TYPE) {
246     case MachO::N_UNDF: // 0x0 undefined, n_sect == NO_SECT
247       OS << "UNDF";
248       break;
249     case MachO::N_ABS: // 0x2 absolute, n_sect == NO_SECT
250       OS << "ABS ";
251       break;
252     case MachO::N_SECT: // 0xe defined in section number n_sect
253       OS << "SECT";
254       break;
255     case MachO::N_PBUD: // 0xc prebound undefined (defined in a dylib)
256       OS << "PBUD";
257       break;
258     case MachO::N_INDR: // 0xa indirect
259       OS << "INDR";
260       break;
261     default:
262       OS << format_hex_no_prefix(Type, 2) << "    ";
263       break;
264     }
265     if (Type & MachO::N_EXT)
266       OS << " EXT";
267     else
268       OS << "    ";
269   }
270 
271   OS << ") "
272      // n_sect
273      << format_hex_no_prefix(SectionIndex, 2)
274      << "     "
275      // n_desc
276      << format_hex_no_prefix(Flags, 4)
277      << "   "
278      // n_value
279      << format_hex_no_prefix(Value, 16);
280 
281   const char *Name = &MainBinaryStrings.data()[StringIndex];
282   if (Name && Name[0])
283     OS << " '" << Name << "'";
284 
285   OS << "\n";
286 }
287 
288 void MachODebugMapParser::dumpOneBinaryStab(const MachOObjectFile &MainBinary,
289                                             StringRef BinaryPath) {
290   loadMainBinarySymbols(MainBinary);
291   MainBinaryStrings = MainBinary.getStringTableData();
292   raw_ostream &OS(llvm::outs());
293 
294   dumpSymTabHeader(OS, getArchName(MainBinary));
295   uint64_t Idx = 0;
296   for (const SymbolRef &Symbol : MainBinary.symbols()) {
297     const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
298     if (MainBinary.is64Bit())
299       dumpSymTabEntry(OS, Idx, MainBinary.getSymbol64TableEntry(DRI));
300     else
301       dumpSymTabEntry(OS, Idx, MainBinary.getSymbolTableEntry(DRI));
302     Idx++;
303   }
304 
305   OS << "\n\n";
306   resetParserState();
307 }
308 
309 static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) {
310   if (Archs.empty() || is_contained(Archs, "all") || is_contained(Archs, "*"))
311     return true;
312 
313   if (Arch.startswith("arm") && Arch != "arm64" && is_contained(Archs, "arm"))
314     return true;
315 
316   SmallString<16> ArchName = Arch;
317   if (Arch.startswith("thumb"))
318     ArchName = ("arm" + Arch.substr(5)).str();
319 
320   return is_contained(Archs, ArchName);
321 }
322 
323 bool MachODebugMapParser::dumpStab() {
324   auto MainBinOrError =
325       MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath);
326   if (auto Error = MainBinOrError.getError()) {
327     llvm::errs() << "Cannot get '" << BinaryPath
328                  << "' as MachO file: " << Error.message() << "\n";
329     return false;
330   }
331 
332   for (const auto *Binary : *MainBinOrError)
333     if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName()))
334       dumpOneBinaryStab(*Binary, BinaryPath);
335 
336   return true;
337 }
338 
339 /// This main parsing routine tries to open the main binary and if
340 /// successful iterates over the STAB entries. The real parsing is
341 /// done in handleStabSymbolTableEntry.
342 ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() {
343   auto MainBinOrError =
344       MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath);
345   if (auto Error = MainBinOrError.getError())
346     return Error;
347 
348   std::vector<std::unique_ptr<DebugMap>> Results;
349   for (const auto *Binary : *MainBinOrError)
350     if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName()))
351       Results.push_back(parseOneBinary(*Binary, BinaryPath));
352 
353   return std::move(Results);
354 }
355 
356 /// Interpret the STAB entries to fill the DebugMap.
357 void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
358                                                      uint8_t Type,
359                                                      uint8_t SectionIndex,
360                                                      uint16_t Flags,
361                                                      uint64_t Value) {
362   if (!(Type & MachO::N_STAB))
363     return;
364 
365   const char *Name = &MainBinaryStrings.data()[StringIndex];
366 
367   // An N_OSO entry represents the start of a new object file description.
368   if (Type == MachO::N_OSO)
369     return switchToNewDebugMapObject(Name, sys::toTimePoint(Value));
370 
371   if (Type == MachO::N_AST) {
372     SmallString<80> Path(PathPrefix);
373     sys::path::append(Path, Name);
374     Result->addDebugMapObject(Path, sys::toTimePoint(Value), Type);
375     return;
376   }
377 
378   // If the last N_OSO object file wasn't found, CurrentDebugMapObject will be
379   // null. Do not update anything until we find the next valid N_OSO entry.
380   if (!CurrentDebugMapObject)
381     return;
382 
383   uint32_t Size = 0;
384   switch (Type) {
385   case MachO::N_GSYM:
386     // This is a global variable. We need to query the main binary
387     // symbol table to find its address as it might not be in the
388     // debug map (for common symbols).
389     Value = getMainBinarySymbolAddress(Name);
390     break;
391   case MachO::N_FUN:
392     // Functions are scopes in STABS. They have an end marker that
393     // contains the function size.
394     if (Name[0] == '\0') {
395       Size = Value;
396       Value = CurrentFunctionAddress;
397       Name = CurrentFunctionName;
398       break;
399     } else {
400       CurrentFunctionName = Name;
401       CurrentFunctionAddress = Value;
402       return;
403     }
404   case MachO::N_STSYM:
405     break;
406   default:
407     return;
408   }
409 
410   auto ObjectSymIt = CurrentObjectAddresses.find(Name);
411 
412   // If the name of a (non-static) symbol is not in the current object, we
413   // check all its aliases from the main binary.
414   if (ObjectSymIt == CurrentObjectAddresses.end() && Type != MachO::N_STSYM) {
415     for (const auto &Alias : getMainBinarySymbolNames(Value)) {
416       ObjectSymIt = CurrentObjectAddresses.find(Alias);
417       if (ObjectSymIt != CurrentObjectAddresses.end())
418         break;
419     }
420   }
421 
422   if (ObjectSymIt == CurrentObjectAddresses.end()) {
423     Warning("could not find object file symbol for symbol " + Twine(Name));
424     return;
425   }
426 
427   if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value,
428                                         Size)) {
429     Warning(Twine("failed to insert symbol '") + Name + "' in the debug map.");
430     return;
431   }
432 }
433 
434 /// Load the current object file symbols into CurrentObjectAddresses.
435 void MachODebugMapParser::loadCurrentObjectFileSymbols(
436     const object::MachOObjectFile &Obj) {
437   CurrentObjectAddresses.clear();
438 
439   for (auto Sym : Obj.symbols()) {
440     uint64_t Addr = Sym.getValue();
441     Expected<StringRef> Name = Sym.getName();
442     if (!Name) {
443       // TODO: Actually report errors helpfully.
444       consumeError(Name.takeError());
445       continue;
446     }
447     // The value of some categories of symbols isn't meaningful. For
448     // example common symbols store their size in the value field, not
449     // their address. Absolute symbols have a fixed address that can
450     // conflict with standard symbols. These symbols (especially the
451     // common ones), might still be referenced by relocations. These
452     // relocations will use the symbol itself, and won't need an
453     // object file address. The object file address field is optional
454     // in the DebugMap, leave it unassigned for these symbols.
455     if (Sym.getFlags() & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))
456       CurrentObjectAddresses[*Name] = None;
457     else
458       CurrentObjectAddresses[*Name] = Addr;
459   }
460 }
461 
462 /// Lookup a symbol address in the main binary symbol table. The
463 /// parser only needs to query common symbols, thus not every symbol's
464 /// address is available through this function.
465 uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
466   auto Sym = MainBinarySymbolAddresses.find(Name);
467   if (Sym == MainBinarySymbolAddresses.end())
468     return 0;
469   return Sym->second;
470 }
471 
472 /// Get all symbol names in the main binary for the given value.
473 std::vector<StringRef>
474 MachODebugMapParser::getMainBinarySymbolNames(uint64_t Value) {
475   std::vector<StringRef> Names;
476   for (const auto &Entry : MainBinarySymbolAddresses) {
477     if (Entry.second == Value)
478       Names.push_back(Entry.first());
479   }
480   return Names;
481 }
482 
483 /// Load the interesting main binary symbols' addresses into
484 /// MainBinarySymbolAddresses.
485 void MachODebugMapParser::loadMainBinarySymbols(
486     const MachOObjectFile &MainBinary) {
487   section_iterator Section = MainBinary.section_end();
488   MainBinarySymbolAddresses.clear();
489   for (const auto &Sym : MainBinary.symbols()) {
490     Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
491     if (!TypeOrErr) {
492       // TODO: Actually report errors helpfully.
493       consumeError(TypeOrErr.takeError());
494       continue;
495     }
496     SymbolRef::Type Type = *TypeOrErr;
497     // Skip undefined and STAB entries.
498     if ((Type == SymbolRef::ST_Debug) || (Type == SymbolRef::ST_Unknown))
499       continue;
500     // The only symbols of interest are the global variables. These
501     // are the only ones that need to be queried because the address
502     // of common data won't be described in the debug map. All other
503     // addresses should be fetched for the debug map.
504     uint8_t SymType =
505         MainBinary.getSymbolTableEntry(Sym.getRawDataRefImpl()).n_type;
506     if (!(SymType & (MachO::N_EXT | MachO::N_PEXT)))
507       continue;
508     Expected<section_iterator> SectionOrErr = Sym.getSection();
509     if (!SectionOrErr) {
510       // TODO: Actually report errors helpfully.
511       consumeError(SectionOrErr.takeError());
512       continue;
513     }
514     Section = *SectionOrErr;
515     if (Section == MainBinary.section_end() || Section->isText())
516       continue;
517     uint64_t Addr = Sym.getValue();
518     Expected<StringRef> NameOrErr = Sym.getName();
519     if (!NameOrErr) {
520       // TODO: Actually report errors helpfully.
521       consumeError(NameOrErr.takeError());
522       continue;
523     }
524     StringRef Name = *NameOrErr;
525     if (Name.size() == 0 || Name[0] == '\0')
526       continue;
527     MainBinarySymbolAddresses[Name] = Addr;
528   }
529 }
530 
531 namespace llvm {
532 namespace dsymutil {
533 llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
534 parseDebugMap(StringRef InputFile, ArrayRef<std::string> Archs,
535               StringRef PrependPath, bool PaperTrailWarnings, bool Verbose,
536               bool InputIsYAML) {
537   if (InputIsYAML)
538     return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
539 
540   MachODebugMapParser Parser(InputFile, Archs, PrependPath, PaperTrailWarnings,
541                              Verbose);
542   return Parser.parse();
543 }
544 
545 bool dumpStab(StringRef InputFile, ArrayRef<std::string> Archs,
546               StringRef PrependPath) {
547   MachODebugMapParser Parser(InputFile, Archs, PrependPath, false);
548   return Parser.dumpStab();
549 }
550 } // namespace dsymutil
551 } // namespace llvm
552