xref: /llvm-project/llvm/tools/dsymutil/MachODebugMapParser.cpp (revision 9e8eb418e583974c7d24ca3d81f567accd5adbfa)
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 "dsymutil.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/Object/MachO.h"
15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/raw_ostream.h"
17 
18 namespace {
19 using namespace llvm;
20 using namespace llvm::dsymutil;
21 using namespace llvm::object;
22 
23 class MachODebugMapParser {
24 public:
25   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) {}
30 
31   /// \brief Parses and returns the DebugMaps of the input binary.
32   /// The binary contains multiple maps in case it is a universal
33   /// binary.
34   /// \returns an error in case the provided BinaryPath doesn't exist
35   /// or isn't of a supported type.
36   ErrorOr<std::vector<std::unique_ptr<DebugMap>>> parse();
37 
38   /// Walk the symbol table and dump it.
39   bool dumpStab();
40 
41 private:
42   std::string BinaryPath;
43   SmallVector<StringRef, 1> Archs;
44   std::string PathPrefix;
45 
46   /// Owns the MemoryBuffer for the main binary.
47   BinaryHolder MainBinaryHolder;
48   /// Map of the binary symbol addresses.
49   StringMap<uint64_t> MainBinarySymbolAddresses;
50   StringRef MainBinaryStrings;
51   /// The constructed DebugMap.
52   std::unique_ptr<DebugMap> Result;
53 
54   /// Owns the MemoryBuffer for the currently handled object file.
55   BinaryHolder CurrentObjectHolder;
56   /// Map of the currently processed object file symbol addresses.
57   StringMap<Optional<uint64_t>> CurrentObjectAddresses;
58   /// Element of the debug map corresponfing 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 switchToNewDebugMapObject(StringRef Filename, sys::TimeValue Timestamp);
69   void resetParserState();
70   uint64_t getMainBinarySymbolAddress(StringRef Name);
71   void loadMainBinarySymbols(const MachOObjectFile &MainBinary);
72   void loadCurrentObjectFileSymbols(const object::MachOObjectFile &Obj);
73   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   }
81 
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);
97 };
98 
99 static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; }
100 } // anonymous namespace
101 
102 /// Reset the parser state coresponding to the current object
103 /// file. This is to be called after an object file is finished
104 /// processing.
105 void MachODebugMapParser::resetParserState() {
106   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.
113 void MachODebugMapParser::switchToNewDebugMapObject(StringRef Filename,
114                                                     sys::TimeValue Timestamp) {
115   resetParserState();
116 
117   SmallString<80> Path(PathPrefix);
118   sys::path::append(Path, Filename);
119 
120   auto MachOOrError =
121       CurrentObjectHolder.GetFilesAs<MachOObjectFile>(Path, Timestamp);
122   if (auto Error = MachOOrError.getError()) {
123     Warning(Twine("cannot open debug object \"") + Path.str() + "\": " +
124             Error.message() + "\n");
125     return;
126   }
127 
128   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 
135   CurrentDebugMapObject = &Result->addDebugMapObject(Path, Timestamp);
136   loadCurrentObjectFileSymbols(*ErrOrAchObj);
137 }
138 
139 static std::string getArchName(const object::MachOObjectFile &Obj) {
140   Triple T = Obj.getArchTriple();
141   return T.getArchName();
142 }
143 
144 std::unique_ptr<DebugMap>
145 MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary,
146                                     StringRef BinaryPath) {
147   loadMainBinarySymbols(MainBinary);
148   Result = make_unique<DebugMap>(MainBinary.getArchTriple(), BinaryPath);
149   MainBinaryStrings = MainBinary.getStringTableData();
150   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 
162 // 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.
166 struct DarwinStabName {
167   uint8_t NType;
168   const char *Name;
169 };
170 
171 static 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"},
187     {MachO::N_LENG, "N_LENG"},    {0, nullptr}};
188 
189 static 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   }
194   return nullptr;
195 }
196 
197 void 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 
207 void 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) {
211   // 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 
266 void MachODebugMapParser::dumpOneBinaryStab(const MachOObjectFile &MainBinary,
267                                             StringRef BinaryPath) {
268   loadMainBinarySymbols(MainBinary);
269   MainBinaryStrings = MainBinary.getStringTableData();
270   raw_ostream &OS(llvm::outs());
271 
272   dumpSymTabHeader(OS, getArchName(MainBinary));
273   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 
287 static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) {
288   if (Archs.empty() ||
289       std::find(Archs.begin(), Archs.end(), "all") != Archs.end() ||
290       std::find(Archs.begin(), Archs.end(), "*") != Archs.end())
291     return true;
292 
293   if (Arch.startswith("arm") && Arch != "arm64" &&
294       std::find(Archs.begin(), Archs.end(), "arm") != Archs.end())
295     return true;
296 
297   return std::find(Archs.begin(), Archs.end(), Arch) != Archs.end();
298 }
299 
300 bool MachODebugMapParser::dumpStab() {
301   auto MainBinOrError =
302       MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath);
303   if (auto Error = MainBinOrError.getError()) {
304     llvm::errs() << "Cannot get '" << BinaryPath
305                  << "' as MachO file: " << Error.message() << "\n";
306     return false;
307   }
308 
309   for (const auto *Binary : *MainBinOrError)
310     if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName()))
311       dumpOneBinaryStab(*Binary, BinaryPath);
312 
313   return true;
314 }
315 
316 /// This main parsing routine tries to open the main binary and if
317 /// successful iterates over the STAB entries. The real parsing is
318 /// done in handleStabSymbolTableEntry.
319 ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() {
320   auto MainBinOrError =
321       MainBinaryHolder.GetFilesAs<MachOObjectFile>(BinaryPath);
322   if (auto Error = MainBinOrError.getError())
323     return Error;
324 
325   std::vector<std::unique_ptr<DebugMap>> Results;
326   for (const auto *Binary : *MainBinOrError)
327     if (shouldLinkArch(Archs, Binary->getArchTriple().getArchName()))
328       Results.push_back(parseOneBinary(*Binary, BinaryPath));
329 
330   return std::move(Results);
331 }
332 
333 /// Interpret the STAB entries to fill the DebugMap.
334 void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
335                                                      uint8_t Type,
336                                                      uint8_t SectionIndex,
337                                                      uint16_t Flags,
338                                                      uint64_t Value) {
339   if (!(Type & MachO::N_STAB))
340     return;
341 
342   const char *Name = &MainBinaryStrings.data()[StringIndex];
343 
344   // An N_OSO entry represents the start of a new object file description.
345   if (Type == MachO::N_OSO) {
346     sys::TimeValue Timestamp;
347     Timestamp.fromEpochTime(Value);
348     return switchToNewDebugMapObject(Name, Timestamp);
349   }
350 
351   // If the last N_OSO object file wasn't found,
352   // CurrentDebugMapObject will be null. Do not update anything
353   // until we find the next valid N_OSO entry.
354   if (!CurrentDebugMapObject)
355     return;
356 
357   uint32_t Size = 0;
358   switch (Type) {
359   case MachO::N_GSYM:
360     // This is a global variable. We need to query the main binary
361     // symbol table to find its address as it might not be in the
362     // debug map (for common symbols).
363     Value = getMainBinarySymbolAddress(Name);
364     break;
365   case MachO::N_FUN:
366     // Functions are scopes in STABS. They have an end marker that
367     // contains the function size.
368     if (Name[0] == '\0') {
369       Size = Value;
370       Value = CurrentFunctionAddress;
371       Name = CurrentFunctionName;
372       break;
373     } else {
374       CurrentFunctionName = Name;
375       CurrentFunctionAddress = Value;
376       return;
377     }
378   case MachO::N_STSYM:
379     break;
380   default:
381     return;
382   }
383 
384   auto ObjectSymIt = CurrentObjectAddresses.find(Name);
385   if (ObjectSymIt == CurrentObjectAddresses.end())
386     return Warning("could not find object file symbol for symbol " +
387                    Twine(Name));
388   if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value,
389                                         Size))
390     return Warning(Twine("failed to insert symbol '") + Name +
391                    "' in the debug map.");
392 }
393 
394 /// Load the current object file symbols into CurrentObjectAddresses.
395 void MachODebugMapParser::loadCurrentObjectFileSymbols(
396     const object::MachOObjectFile &Obj) {
397   CurrentObjectAddresses.clear();
398 
399   for (auto Sym : Obj.symbols()) {
400     uint64_t Addr = Sym.getValue();
401     Expected<StringRef> Name = Sym.getName();
402     if (!Name) {
403       // TODO: Actually report errors helpfully.
404       consumeError(Name.takeError());
405       continue;
406     }
407     // The value of some categories of symbols isn't meaningful. For
408     // example common symbols store their size in the value field, not
409     // their address. Absolute symbols have a fixed address that can
410     // conflict with standard symbols. These symbols (especially the
411     // common ones), might still be referenced by relocations. These
412     // relocations will use the symbol itself, and won't need an
413     // object file address. The object file address field is optional
414     // in the DebugMap, leave it unassigned for these symbols.
415     if (Sym.getFlags() & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))
416       CurrentObjectAddresses[*Name] = None;
417     else
418       CurrentObjectAddresses[*Name] = Addr;
419   }
420 }
421 
422 /// Lookup a symbol address in the main binary symbol table. The
423 /// parser only needs to query common symbols, thus not every symbol's
424 /// address is available through this function.
425 uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
426   auto Sym = MainBinarySymbolAddresses.find(Name);
427   if (Sym == MainBinarySymbolAddresses.end())
428     return 0;
429   return Sym->second;
430 }
431 
432 /// Load the interesting main binary symbols' addresses into
433 /// MainBinarySymbolAddresses.
434 void MachODebugMapParser::loadMainBinarySymbols(
435     const MachOObjectFile &MainBinary) {
436   section_iterator Section = MainBinary.section_end();
437   MainBinarySymbolAddresses.clear();
438   for (const auto &Sym : MainBinary.symbols()) {
439     ErrorOr<SymbolRef::Type> TypeOrErr = Sym.getType();
440     if (!TypeOrErr)
441       continue;
442     SymbolRef::Type Type = *TypeOrErr;
443     // Skip undefined and STAB entries.
444     if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown))
445       continue;
446     // The only symbols of interest are the global variables. These
447     // are the only ones that need to be queried because the address
448     // of common data won't be described in the debug map. All other
449     // addresses should be fetched for the debug map.
450     if (!(Sym.getFlags() & SymbolRef::SF_Global))
451       continue;
452     ErrorOr<section_iterator> SectionOrErr = Sym.getSection();
453     if (!SectionOrErr)
454       continue;
455     Section = *SectionOrErr;
456     if (Section == MainBinary.section_end() || Section->isText())
457       continue;
458     uint64_t Addr = Sym.getValue();
459     Expected<StringRef> NameOrErr = Sym.getName();
460     if (!NameOrErr) {
461       // TODO: Actually report errors helpfully.
462       consumeError(NameOrErr.takeError());
463       continue;
464     }
465     StringRef Name = *NameOrErr;
466     if (Name.size() == 0 || Name[0] == '\0')
467       continue;
468     MainBinarySymbolAddresses[Name] = Addr;
469   }
470 }
471 
472 namespace llvm {
473 namespace dsymutil {
474 llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
475 parseDebugMap(StringRef InputFile, ArrayRef<std::string> Archs,
476               StringRef PrependPath, bool Verbose, bool InputIsYAML) {
477   if (!InputIsYAML) {
478     MachODebugMapParser Parser(InputFile, Archs, PrependPath, Verbose);
479     return Parser.parse();
480   } else {
481     return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
482   }
483 }
484 
485 bool dumpStab(StringRef InputFile, ArrayRef<std::string> Archs,
486               StringRef PrependPath) {
487   MachODebugMapParser Parser(InputFile, Archs, PrependPath, false);
488   return Parser.dumpStab();
489 }
490 } // namespace dsymutil
491 } // namespace llvm
492