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 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 ObjectEntry = BinHolder.getObjectEntry(BinaryPath); 325 if (!ObjectEntry) { 326 auto Err = ObjectEntry.takeError(); 327 WithColor::error() << "cannot load '" << BinaryPath 328 << "': " << toString(std::move(Err)) << '\n'; 329 return false; 330 } 331 332 auto Objects = ObjectEntry->getObjectsAs<MachOObjectFile>(); 333 if (!Objects) { 334 auto Err = Objects.takeError(); 335 WithColor::error() << "cannot get '" << BinaryPath 336 << "' as MachO file: " << toString(std::move(Err)) 337 << "\n"; 338 return false; 339 } 340 341 for (const auto *Object : *Objects) 342 if (shouldLinkArch(Archs, Object->getArchTriple().getArchName())) 343 dumpOneBinaryStab(*Object, BinaryPath); 344 345 return true; 346 } 347 348 /// This main parsing routine tries to open the main binary and if 349 /// successful iterates over the STAB entries. The real parsing is 350 /// done in handleStabSymbolTableEntry. 351 ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() { 352 auto ObjectEntry = BinHolder.getObjectEntry(BinaryPath); 353 if (!ObjectEntry) { 354 return errorToErrorCode(ObjectEntry.takeError()); 355 } 356 357 auto Objects = ObjectEntry->getObjectsAs<MachOObjectFile>(); 358 if (!Objects) { 359 return errorToErrorCode(ObjectEntry.takeError()); 360 } 361 362 std::vector<std::unique_ptr<DebugMap>> Results; 363 for (const auto *Object : *Objects) 364 if (shouldLinkArch(Archs, Object->getArchTriple().getArchName())) 365 Results.push_back(parseOneBinary(*Object, BinaryPath)); 366 367 return std::move(Results); 368 } 369 370 /// Interpret the STAB entries to fill the DebugMap. 371 void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex, 372 uint8_t Type, 373 uint8_t SectionIndex, 374 uint16_t Flags, 375 uint64_t Value) { 376 if (!(Type & MachO::N_STAB)) 377 return; 378 379 const char *Name = &MainBinaryStrings.data()[StringIndex]; 380 381 // An N_OSO entry represents the start of a new object file description. 382 if (Type == MachO::N_OSO) 383 return switchToNewDebugMapObject(Name, sys::toTimePoint(Value)); 384 385 if (Type == MachO::N_AST) { 386 SmallString<80> Path(PathPrefix); 387 sys::path::append(Path, Name); 388 Result->addDebugMapObject(Path, sys::toTimePoint(Value), Type); 389 return; 390 } 391 392 // If the last N_OSO object file wasn't found, CurrentDebugMapObject will be 393 // null. Do not update anything until we find the next valid N_OSO entry. 394 if (!CurrentDebugMapObject) 395 return; 396 397 uint32_t Size = 0; 398 switch (Type) { 399 case MachO::N_GSYM: 400 // This is a global variable. We need to query the main binary 401 // symbol table to find its address as it might not be in the 402 // debug map (for common symbols). 403 Value = getMainBinarySymbolAddress(Name); 404 break; 405 case MachO::N_FUN: 406 // Functions are scopes in STABS. They have an end marker that 407 // contains the function size. 408 if (Name[0] == '\0') { 409 Size = Value; 410 Value = CurrentFunctionAddress; 411 Name = CurrentFunctionName; 412 break; 413 } else { 414 CurrentFunctionName = Name; 415 CurrentFunctionAddress = Value; 416 return; 417 } 418 case MachO::N_STSYM: 419 break; 420 default: 421 return; 422 } 423 424 auto ObjectSymIt = CurrentObjectAddresses.find(Name); 425 426 // If the name of a (non-static) symbol is not in the current object, we 427 // check all its aliases from the main binary. 428 if (ObjectSymIt == CurrentObjectAddresses.end() && Type != MachO::N_STSYM) { 429 for (const auto &Alias : getMainBinarySymbolNames(Value)) { 430 ObjectSymIt = CurrentObjectAddresses.find(Alias); 431 if (ObjectSymIt != CurrentObjectAddresses.end()) 432 break; 433 } 434 } 435 436 if (ObjectSymIt == CurrentObjectAddresses.end()) { 437 Warning("could not find object file symbol for symbol " + Twine(Name)); 438 return; 439 } 440 441 if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value, 442 Size)) { 443 Warning(Twine("failed to insert symbol '") + Name + "' in the debug map."); 444 return; 445 } 446 } 447 448 /// Load the current object file symbols into CurrentObjectAddresses. 449 void MachODebugMapParser::loadCurrentObjectFileSymbols( 450 const object::MachOObjectFile &Obj) { 451 CurrentObjectAddresses.clear(); 452 453 for (auto Sym : Obj.symbols()) { 454 uint64_t Addr = Sym.getValue(); 455 Expected<StringRef> Name = Sym.getName(); 456 if (!Name) { 457 // TODO: Actually report errors helpfully. 458 consumeError(Name.takeError()); 459 continue; 460 } 461 // The value of some categories of symbols isn't meaningful. For 462 // example common symbols store their size in the value field, not 463 // their address. Absolute symbols have a fixed address that can 464 // conflict with standard symbols. These symbols (especially the 465 // common ones), might still be referenced by relocations. These 466 // relocations will use the symbol itself, and won't need an 467 // object file address. The object file address field is optional 468 // in the DebugMap, leave it unassigned for these symbols. 469 if (Sym.getFlags() & (SymbolRef::SF_Absolute | SymbolRef::SF_Common)) 470 CurrentObjectAddresses[*Name] = None; 471 else 472 CurrentObjectAddresses[*Name] = Addr; 473 } 474 } 475 476 /// Lookup a symbol address in the main binary symbol table. The 477 /// parser only needs to query common symbols, thus not every symbol's 478 /// address is available through this function. 479 uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) { 480 auto Sym = MainBinarySymbolAddresses.find(Name); 481 if (Sym == MainBinarySymbolAddresses.end()) 482 return 0; 483 return Sym->second; 484 } 485 486 /// Get all symbol names in the main binary for the given value. 487 std::vector<StringRef> 488 MachODebugMapParser::getMainBinarySymbolNames(uint64_t Value) { 489 std::vector<StringRef> Names; 490 for (const auto &Entry : MainBinarySymbolAddresses) { 491 if (Entry.second == Value) 492 Names.push_back(Entry.first()); 493 } 494 return Names; 495 } 496 497 /// Load the interesting main binary symbols' addresses into 498 /// MainBinarySymbolAddresses. 499 void MachODebugMapParser::loadMainBinarySymbols( 500 const MachOObjectFile &MainBinary) { 501 section_iterator Section = MainBinary.section_end(); 502 MainBinarySymbolAddresses.clear(); 503 for (const auto &Sym : MainBinary.symbols()) { 504 Expected<SymbolRef::Type> TypeOrErr = Sym.getType(); 505 if (!TypeOrErr) { 506 // TODO: Actually report errors helpfully. 507 consumeError(TypeOrErr.takeError()); 508 continue; 509 } 510 SymbolRef::Type Type = *TypeOrErr; 511 // Skip undefined and STAB entries. 512 if ((Type == SymbolRef::ST_Debug) || (Type == SymbolRef::ST_Unknown)) 513 continue; 514 // The only symbols of interest are the global variables. These 515 // are the only ones that need to be queried because the address 516 // of common data won't be described in the debug map. All other 517 // addresses should be fetched for the debug map. 518 uint8_t SymType = 519 MainBinary.getSymbolTableEntry(Sym.getRawDataRefImpl()).n_type; 520 if (!(SymType & (MachO::N_EXT | MachO::N_PEXT))) 521 continue; 522 Expected<section_iterator> SectionOrErr = Sym.getSection(); 523 if (!SectionOrErr) { 524 // TODO: Actually report errors helpfully. 525 consumeError(SectionOrErr.takeError()); 526 continue; 527 } 528 Section = *SectionOrErr; 529 if (Section == MainBinary.section_end() || Section->isText()) 530 continue; 531 uint64_t Addr = Sym.getValue(); 532 Expected<StringRef> NameOrErr = Sym.getName(); 533 if (!NameOrErr) { 534 // TODO: Actually report errors helpfully. 535 consumeError(NameOrErr.takeError()); 536 continue; 537 } 538 StringRef Name = *NameOrErr; 539 if (Name.size() == 0 || Name[0] == '\0') 540 continue; 541 MainBinarySymbolAddresses[Name] = Addr; 542 } 543 } 544 545 namespace llvm { 546 namespace dsymutil { 547 llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>> 548 parseDebugMap(StringRef InputFile, ArrayRef<std::string> Archs, 549 StringRef PrependPath, bool PaperTrailWarnings, bool Verbose, 550 bool InputIsYAML) { 551 if (InputIsYAML) 552 return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose); 553 554 MachODebugMapParser Parser(InputFile, Archs, PrependPath, PaperTrailWarnings, 555 Verbose); 556 return Parser.parse(); 557 } 558 559 bool dumpStab(StringRef InputFile, ArrayRef<std::string> Archs, 560 StringRef PrependPath) { 561 MachODebugMapParser Parser(InputFile, Archs, PrependPath, false); 562 return Parser.dumpStab(); 563 } 564 } // namespace dsymutil 565 } // namespace llvm 566