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