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