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