2014-12-13 01:31:24 +08:00
|
|
|
//===- tools/dsymutil/MachODebugMapParser.cpp - Parse STABS debug maps ----===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-12-13 01:31:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-06 05:29:28 +08:00
|
|
|
#include "BinaryHolder.h"
|
2014-12-13 01:31:24 +08:00
|
|
|
#include "DebugMap.h"
|
2018-03-14 17:34:54 +08:00
|
|
|
#include "MachOUtils.h"
|
2015-12-12 01:50:37 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2014-12-13 01:31:24 +08:00
|
|
|
#include "llvm/Object/MachO.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2018-04-15 05:36:42 +08:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2014-12-13 01:31:24 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2019-10-10 00:19:13 +08:00
|
|
|
#include <vector>
|
2014-12-13 01:31:24 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::dsymutil;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
class MachODebugMapParser {
|
|
|
|
public:
|
2020-05-05 11:19:15 +08:00
|
|
|
MachODebugMapParser(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
|
|
|
|
StringRef BinaryPath, ArrayRef<std::string> Archs,
|
2018-04-02 18:40:43 +08:00
|
|
|
StringRef PathPrefix = "",
|
|
|
|
bool PaperTrailWarnings = false, bool Verbose = false)
|
2020-01-29 03:23:46 +08:00
|
|
|
: BinaryPath(std::string(BinaryPath)), Archs(Archs.begin(), Archs.end()),
|
|
|
|
PathPrefix(std::string(PathPrefix)),
|
2020-05-05 11:19:15 +08:00
|
|
|
PaperTrailWarnings(PaperTrailWarnings), BinHolder(VFS, Verbose),
|
2020-01-29 03:23:46 +08:00
|
|
|
CurrentDebugMapObject(nullptr) {}
|
2014-12-13 01:31:24 +08:00
|
|
|
|
2018-02-22 19:43:43 +08:00
|
|
|
/// Parses and returns the DebugMaps of the input binary. The binary contains
|
|
|
|
/// multiple maps in case it is a universal binary.
|
2014-12-13 01:31:24 +08:00
|
|
|
/// \returns an error in case the provided BinaryPath doesn't exist
|
|
|
|
/// or isn't of a supported type.
|
2015-08-06 02:27:44 +08:00
|
|
|
ErrorOr<std::vector<std::unique_ptr<DebugMap>>> parse();
|
2014-12-13 01:31:24 +08:00
|
|
|
|
2015-08-31 08:29:09 +08:00
|
|
|
/// Walk the symbol table and dump it.
|
|
|
|
bool dumpStab();
|
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
private:
|
|
|
|
std::string BinaryPath;
|
2015-08-06 06:33:28 +08:00
|
|
|
SmallVector<StringRef, 1> Archs;
|
2014-12-13 01:31:24 +08:00
|
|
|
std::string PathPrefix;
|
2018-04-02 18:40:43 +08:00
|
|
|
bool PaperTrailWarnings;
|
2014-12-13 01:31:24 +08:00
|
|
|
|
2015-01-06 05:29:28 +08:00
|
|
|
/// Owns the MemoryBuffer for the main binary.
|
2018-06-30 00:51:52 +08:00
|
|
|
BinaryHolder BinHolder;
|
2014-12-13 01:31:24 +08:00
|
|
|
/// Map of the binary symbol addresses.
|
|
|
|
StringMap<uint64_t> MainBinarySymbolAddresses;
|
2014-12-17 04:21:34 +08:00
|
|
|
StringRef MainBinaryStrings;
|
2014-12-13 01:31:24 +08:00
|
|
|
/// The constructed DebugMap.
|
|
|
|
std::unique_ptr<DebugMap> Result;
|
2019-10-10 00:19:13 +08:00
|
|
|
/// List of common symbols that need to be added to the debug map.
|
|
|
|
std::vector<std::string> CommonSymbols;
|
2014-12-13 01:31:24 +08:00
|
|
|
|
|
|
|
/// Map of the currently processed object file symbol addresses.
|
2015-12-12 01:50:37 +08:00
|
|
|
StringMap<Optional<uint64_t>> CurrentObjectAddresses;
|
2018-02-22 19:32:51 +08:00
|
|
|
/// Element of the debug map corresponding to the current object file.
|
2014-12-13 01:31:24 +08:00
|
|
|
DebugMapObject *CurrentDebugMapObject;
|
|
|
|
|
2015-03-15 09:29:30 +08:00
|
|
|
/// Holds function info while function scope processing.
|
|
|
|
const char *CurrentFunctionName;
|
|
|
|
uint64_t CurrentFunctionAddress;
|
|
|
|
|
2015-08-06 02:27:44 +08:00
|
|
|
std::unique_ptr<DebugMap> parseOneBinary(const MachOObjectFile &MainBinary,
|
|
|
|
StringRef BinaryPath);
|
|
|
|
|
2016-11-09 19:43:52 +08:00
|
|
|
void
|
|
|
|
switchToNewDebugMapObject(StringRef Filename,
|
|
|
|
sys::TimePoint<std::chrono::seconds> Timestamp);
|
2014-12-13 01:31:24 +08:00
|
|
|
void resetParserState();
|
|
|
|
uint64_t getMainBinarySymbolAddress(StringRef Name);
|
2017-09-26 16:17:28 +08:00
|
|
|
std::vector<StringRef> getMainBinarySymbolNames(uint64_t Value);
|
2015-07-24 14:41:11 +08:00
|
|
|
void loadMainBinarySymbols(const MachOObjectFile &MainBinary);
|
|
|
|
void loadCurrentObjectFileSymbols(const object::MachOObjectFile &Obj);
|
2014-12-13 01:31:24 +08:00
|
|
|
void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type,
|
|
|
|
uint8_t SectionIndex, uint16_t Flags,
|
|
|
|
uint64_t Value);
|
|
|
|
|
|
|
|
template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) {
|
|
|
|
handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
|
|
|
|
STE.n_value);
|
|
|
|
}
|
2015-08-31 08:29:09 +08:00
|
|
|
|
2019-10-10 00:19:13 +08:00
|
|
|
void addCommonSymbols();
|
|
|
|
|
2015-08-31 08:29:09 +08:00
|
|
|
/// Dump the symbol table output header.
|
|
|
|
void dumpSymTabHeader(raw_ostream &OS, StringRef Arch);
|
|
|
|
|
|
|
|
/// Dump the contents of nlist entries.
|
|
|
|
void dumpSymTabEntry(raw_ostream &OS, uint64_t Index, uint32_t StringIndex,
|
|
|
|
uint8_t Type, uint8_t SectionIndex, uint16_t Flags,
|
|
|
|
uint64_t Value);
|
|
|
|
|
|
|
|
template <typename STEType>
|
|
|
|
void dumpSymTabEntry(raw_ostream &OS, uint64_t Index, const STEType &STE) {
|
|
|
|
dumpSymTabEntry(OS, Index, STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc,
|
|
|
|
STE.n_value);
|
|
|
|
}
|
|
|
|
void dumpOneBinaryStab(const MachOObjectFile &MainBinary,
|
|
|
|
StringRef BinaryPath);
|
2018-03-14 17:34:54 +08:00
|
|
|
|
|
|
|
void Warning(const Twine &Msg, StringRef File = StringRef()) {
|
2018-04-15 05:36:42 +08:00
|
|
|
WithColor::warning() << "("
|
|
|
|
<< MachOUtils::getArchName(
|
|
|
|
Result->getTriple().getArchName())
|
|
|
|
<< ") " << File << " " << Msg << "\n";
|
2018-04-02 18:40:43 +08:00
|
|
|
|
|
|
|
if (PaperTrailWarnings) {
|
|
|
|
if (!File.empty())
|
|
|
|
Result->addDebugMapObject(File, sys::TimePoint<std::chrono::seconds>());
|
|
|
|
if (Result->end() != Result->begin())
|
|
|
|
(*--Result->end())->addWarning(Msg.str());
|
|
|
|
}
|
2018-03-14 17:34:54 +08:00
|
|
|
}
|
2014-12-13 01:31:24 +08:00
|
|
|
};
|
|
|
|
|
2015-10-07 07:24:35 +08:00
|
|
|
} // anonymous namespace
|
2014-12-13 01:31:24 +08:00
|
|
|
|
2018-02-22 19:32:51 +08:00
|
|
|
/// Reset the parser state corresponding to the current object
|
2014-12-13 01:31:24 +08:00
|
|
|
/// file. This is to be called after an object file is finished
|
|
|
|
/// processing.
|
|
|
|
void MachODebugMapParser::resetParserState() {
|
2019-10-11 00:37:38 +08:00
|
|
|
CommonSymbols.clear();
|
2014-12-13 01:31:24 +08:00
|
|
|
CurrentObjectAddresses.clear();
|
|
|
|
CurrentDebugMapObject = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:19:13 +08:00
|
|
|
/// Commons symbols won't show up in the symbol map but might need to be
|
|
|
|
/// relocated. We can add them to the symbol table ourselves by combining the
|
|
|
|
/// information in the object file (the symbol name) and the main binary (the
|
|
|
|
/// address).
|
|
|
|
void MachODebugMapParser::addCommonSymbols() {
|
|
|
|
for (auto &CommonSymbol : CommonSymbols) {
|
|
|
|
uint64_t CommonAddr = getMainBinarySymbolAddress(CommonSymbol);
|
|
|
|
if (CommonAddr == 0) {
|
|
|
|
// The main binary doesn't have an address for the given symbol.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!CurrentDebugMapObject->addSymbol(CommonSymbol, None /*ObjectAddress*/,
|
|
|
|
CommonAddr, 0 /*size*/)) {
|
|
|
|
// The symbol is already present.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
/// Create a new DebugMapObject. This function resets the state of the
|
|
|
|
/// parser that was referring to the last object file and sets
|
|
|
|
/// everything up to add symbols to the new one.
|
2016-11-09 19:43:52 +08:00
|
|
|
void MachODebugMapParser::switchToNewDebugMapObject(
|
|
|
|
StringRef Filename, sys::TimePoint<std::chrono::seconds> Timestamp) {
|
2019-10-10 00:19:13 +08:00
|
|
|
addCommonSymbols();
|
2014-12-13 01:31:24 +08:00
|
|
|
resetParserState();
|
|
|
|
|
|
|
|
SmallString<80> Path(PathPrefix);
|
|
|
|
sys::path::append(Path, Filename);
|
|
|
|
|
2018-06-30 00:51:52 +08:00
|
|
|
auto ObjectEntry = BinHolder.getObjectEntry(Path, Timestamp);
|
|
|
|
if (!ObjectEntry) {
|
|
|
|
auto Err = ObjectEntry.takeError();
|
|
|
|
Warning("unable to open object file: " + toString(std::move(Err)),
|
|
|
|
Path.str());
|
2014-12-13 01:31:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-30 00:51:52 +08:00
|
|
|
auto Object = ObjectEntry->getObjectAs<MachOObjectFile>(Result->getTriple());
|
|
|
|
if (!Object) {
|
|
|
|
auto Err = Object.takeError();
|
|
|
|
Warning("unable to open object file: " + toString(std::move(Err)),
|
|
|
|
Path.str());
|
2018-03-13 23:47:38 +08:00
|
|
|
return;
|
2015-07-24 14:41:11 +08:00
|
|
|
}
|
|
|
|
|
2017-10-06 22:49:20 +08:00
|
|
|
CurrentDebugMapObject =
|
|
|
|
&Result->addDebugMapObject(Path, Timestamp, MachO::N_OSO);
|
2018-06-30 00:51:52 +08:00
|
|
|
loadCurrentObjectFileSymbols(*Object);
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 08:29:09 +08:00
|
|
|
static std::string getArchName(const object::MachOObjectFile &Obj) {
|
2016-04-23 07:21:13 +08:00
|
|
|
Triple T = Obj.getArchTriple();
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(T.getArchName());
|
2015-08-31 08:29:09 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 02:27:44 +08:00
|
|
|
std::unique_ptr<DebugMap>
|
|
|
|
MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary,
|
|
|
|
StringRef BinaryPath) {
|
2015-07-24 14:41:11 +08:00
|
|
|
loadMainBinarySymbols(MainBinary);
|
2019-01-08 07:27:25 +08:00
|
|
|
ArrayRef<uint8_t> UUID = MainBinary.getUuid();
|
2020-05-05 11:19:15 +08:00
|
|
|
Result =
|
|
|
|
std::make_unique<DebugMap>(MainBinary.getArchTriple(), BinaryPath, UUID);
|
2014-12-17 04:21:34 +08:00
|
|
|
MainBinaryStrings = MainBinary.getStringTableData();
|
2014-12-13 01:31:24 +08:00
|
|
|
for (const SymbolRef &Symbol : MainBinary.symbols()) {
|
|
|
|
const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
|
|
|
|
if (MainBinary.is64Bit())
|
|
|
|
handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI));
|
|
|
|
else
|
|
|
|
handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI));
|
|
|
|
}
|
|
|
|
|
|
|
|
resetParserState();
|
|
|
|
return std::move(Result);
|
|
|
|
}
|
|
|
|
|
2015-08-31 08:29:09 +08:00
|
|
|
// Table that maps Darwin's Mach-O stab constants to strings to allow printing.
|
|
|
|
// llvm-nm has very similar code, the strings used here are however slightly
|
|
|
|
// different and part of the interface of dsymutil (some project's build-systems
|
|
|
|
// parse the ouptut of dsymutil -s), thus they shouldn't be changed.
|
|
|
|
struct DarwinStabName {
|
|
|
|
uint8_t NType;
|
|
|
|
const char *Name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct DarwinStabName DarwinStabNames[] = {
|
|
|
|
{MachO::N_GSYM, "N_GSYM"}, {MachO::N_FNAME, "N_FNAME"},
|
|
|
|
{MachO::N_FUN, "N_FUN"}, {MachO::N_STSYM, "N_STSYM"},
|
|
|
|
{MachO::N_LCSYM, "N_LCSYM"}, {MachO::N_BNSYM, "N_BNSYM"},
|
|
|
|
{MachO::N_PC, "N_PC"}, {MachO::N_AST, "N_AST"},
|
|
|
|
{MachO::N_OPT, "N_OPT"}, {MachO::N_RSYM, "N_RSYM"},
|
|
|
|
{MachO::N_SLINE, "N_SLINE"}, {MachO::N_ENSYM, "N_ENSYM"},
|
|
|
|
{MachO::N_SSYM, "N_SSYM"}, {MachO::N_SO, "N_SO"},
|
|
|
|
{MachO::N_OSO, "N_OSO"}, {MachO::N_LSYM, "N_LSYM"},
|
|
|
|
{MachO::N_BINCL, "N_BINCL"}, {MachO::N_SOL, "N_SOL"},
|
|
|
|
{MachO::N_PARAMS, "N_PARAM"}, {MachO::N_VERSION, "N_VERS"},
|
|
|
|
{MachO::N_OLEVEL, "N_OLEV"}, {MachO::N_PSYM, "N_PSYM"},
|
|
|
|
{MachO::N_EINCL, "N_EINCL"}, {MachO::N_ENTRY, "N_ENTRY"},
|
|
|
|
{MachO::N_LBRAC, "N_LBRAC"}, {MachO::N_EXCL, "N_EXCL"},
|
|
|
|
{MachO::N_RBRAC, "N_RBRAC"}, {MachO::N_BCOMM, "N_BCOMM"},
|
|
|
|
{MachO::N_ECOMM, "N_ECOMM"}, {MachO::N_ECOML, "N_ECOML"},
|
2015-10-07 07:24:35 +08:00
|
|
|
{MachO::N_LENG, "N_LENG"}, {0, nullptr}};
|
2015-08-31 08:29:09 +08:00
|
|
|
|
|
|
|
static const char *getDarwinStabString(uint8_t NType) {
|
|
|
|
for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
|
|
|
|
if (DarwinStabNames[i].NType == NType)
|
|
|
|
return DarwinStabNames[i].Name;
|
|
|
|
}
|
2015-10-07 07:24:35 +08:00
|
|
|
return nullptr;
|
2015-08-31 08:29:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachODebugMapParser::dumpSymTabHeader(raw_ostream &OS, StringRef Arch) {
|
|
|
|
OS << "-----------------------------------"
|
|
|
|
"-----------------------------------\n";
|
|
|
|
OS << "Symbol table for: '" << BinaryPath << "' (" << Arch.data() << ")\n";
|
|
|
|
OS << "-----------------------------------"
|
|
|
|
"-----------------------------------\n";
|
|
|
|
OS << "Index n_strx n_type n_sect n_desc n_value\n";
|
|
|
|
OS << "======== -------- ------------------ ------ ------ ----------------\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachODebugMapParser::dumpSymTabEntry(raw_ostream &OS, uint64_t Index,
|
|
|
|
uint32_t StringIndex, uint8_t Type,
|
|
|
|
uint8_t SectionIndex, uint16_t Flags,
|
|
|
|
uint64_t Value) {
|
|
|
|
// Index
|
2018-02-22 19:32:51 +08:00
|
|
|
OS << '[' << format_decimal(Index, 6)
|
|
|
|
<< "] "
|
2015-08-31 08:29:09 +08:00
|
|
|
// n_strx
|
2018-02-22 19:32:51 +08:00
|
|
|
<< format_hex_no_prefix(StringIndex, 8)
|
|
|
|
<< ' '
|
2015-08-31 08:29:09 +08:00
|
|
|
// n_type...
|
|
|
|
<< format_hex_no_prefix(Type, 2) << " (";
|
|
|
|
|
|
|
|
if (Type & MachO::N_STAB)
|
|
|
|
OS << left_justify(getDarwinStabString(Type), 13);
|
|
|
|
else {
|
|
|
|
if (Type & MachO::N_PEXT)
|
|
|
|
OS << "PEXT ";
|
|
|
|
else
|
|
|
|
OS << " ";
|
|
|
|
switch (Type & MachO::N_TYPE) {
|
|
|
|
case MachO::N_UNDF: // 0x0 undefined, n_sect == NO_SECT
|
|
|
|
OS << "UNDF";
|
|
|
|
break;
|
|
|
|
case MachO::N_ABS: // 0x2 absolute, n_sect == NO_SECT
|
|
|
|
OS << "ABS ";
|
|
|
|
break;
|
|
|
|
case MachO::N_SECT: // 0xe defined in section number n_sect
|
|
|
|
OS << "SECT";
|
|
|
|
break;
|
|
|
|
case MachO::N_PBUD: // 0xc prebound undefined (defined in a dylib)
|
|
|
|
OS << "PBUD";
|
|
|
|
break;
|
|
|
|
case MachO::N_INDR: // 0xa indirect
|
|
|
|
OS << "INDR";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OS << format_hex_no_prefix(Type, 2) << " ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Type & MachO::N_EXT)
|
|
|
|
OS << " EXT";
|
|
|
|
else
|
|
|
|
OS << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << ") "
|
|
|
|
// n_sect
|
2018-02-22 19:32:51 +08:00
|
|
|
<< format_hex_no_prefix(SectionIndex, 2)
|
|
|
|
<< " "
|
2015-08-31 08:29:09 +08:00
|
|
|
// n_desc
|
2018-02-22 19:32:51 +08:00
|
|
|
<< format_hex_no_prefix(Flags, 4)
|
|
|
|
<< " "
|
2015-08-31 08:29:09 +08:00
|
|
|
// n_value
|
|
|
|
<< format_hex_no_prefix(Value, 16);
|
|
|
|
|
|
|
|
const char *Name = &MainBinaryStrings.data()[StringIndex];
|
|
|
|
if (Name && Name[0])
|
|
|
|
OS << " '" << Name << "'";
|
|
|
|
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachODebugMapParser::dumpOneBinaryStab(const MachOObjectFile &MainBinary,
|
|
|
|
StringRef BinaryPath) {
|
|
|
|
loadMainBinarySymbols(MainBinary);
|
|
|
|
MainBinaryStrings = MainBinary.getStringTableData();
|
|
|
|
raw_ostream &OS(llvm::outs());
|
|
|
|
|
2015-08-31 08:49:34 +08:00
|
|
|
dumpSymTabHeader(OS, getArchName(MainBinary));
|
2015-08-31 08:29:09 +08:00
|
|
|
uint64_t Idx = 0;
|
|
|
|
for (const SymbolRef &Symbol : MainBinary.symbols()) {
|
|
|
|
const DataRefImpl &DRI = Symbol.getRawDataRefImpl();
|
|
|
|
if (MainBinary.is64Bit())
|
|
|
|
dumpSymTabEntry(OS, Idx, MainBinary.getSymbol64TableEntry(DRI));
|
|
|
|
else
|
|
|
|
dumpSymTabEntry(OS, Idx, MainBinary.getSymbolTableEntry(DRI));
|
|
|
|
Idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "\n\n";
|
|
|
|
resetParserState();
|
|
|
|
}
|
|
|
|
|
2015-08-06 06:33:28 +08:00
|
|
|
static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) {
|
2016-08-12 06:21:41 +08:00
|
|
|
if (Archs.empty() || is_contained(Archs, "all") || is_contained(Archs, "*"))
|
2015-08-06 06:33:28 +08:00
|
|
|
return true;
|
|
|
|
|
2016-08-12 06:21:41 +08:00
|
|
|
if (Arch.startswith("arm") && Arch != "arm64" && is_contained(Archs, "arm"))
|
2015-08-06 06:33:28 +08:00
|
|
|
return true;
|
|
|
|
|
2016-05-09 14:01:12 +08:00
|
|
|
SmallString<16> ArchName = Arch;
|
|
|
|
if (Arch.startswith("thumb"))
|
|
|
|
ArchName = ("arm" + Arch.substr(5)).str();
|
|
|
|
|
2016-08-12 06:21:41 +08:00
|
|
|
return is_contained(Archs, ArchName);
|
2015-08-06 06:33:28 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 08:29:09 +08:00
|
|
|
bool MachODebugMapParser::dumpStab() {
|
2018-06-30 00:51:52 +08:00
|
|
|
auto ObjectEntry = BinHolder.getObjectEntry(BinaryPath);
|
|
|
|
if (!ObjectEntry) {
|
|
|
|
auto Err = ObjectEntry.takeError();
|
|
|
|
WithColor::error() << "cannot load '" << BinaryPath
|
|
|
|
<< "': " << toString(std::move(Err)) << '\n';
|
2015-08-31 08:29:09 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-30 00:51:52 +08:00
|
|
|
auto Objects = ObjectEntry->getObjectsAs<MachOObjectFile>();
|
|
|
|
if (!Objects) {
|
|
|
|
auto Err = Objects.takeError();
|
|
|
|
WithColor::error() << "cannot get '" << BinaryPath
|
|
|
|
<< "' as MachO file: " << toString(std::move(Err))
|
|
|
|
<< "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto *Object : *Objects)
|
|
|
|
if (shouldLinkArch(Archs, Object->getArchTriple().getArchName()))
|
|
|
|
dumpOneBinaryStab(*Object, BinaryPath);
|
2015-08-31 08:29:09 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-06 02:27:44 +08:00
|
|
|
/// This main parsing routine tries to open the main binary and if
|
|
|
|
/// successful iterates over the STAB entries. The real parsing is
|
|
|
|
/// done in handleStabSymbolTableEntry.
|
|
|
|
ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() {
|
2018-06-30 00:51:52 +08:00
|
|
|
auto ObjectEntry = BinHolder.getObjectEntry(BinaryPath);
|
|
|
|
if (!ObjectEntry) {
|
|
|
|
return errorToErrorCode(ObjectEntry.takeError());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Objects = ObjectEntry->getObjectsAs<MachOObjectFile>();
|
|
|
|
if (!Objects) {
|
2020-03-07 06:59:41 +08:00
|
|
|
return errorToErrorCode(Objects.takeError());
|
2018-06-30 00:51:52 +08:00
|
|
|
}
|
2015-08-06 02:27:44 +08:00
|
|
|
|
|
|
|
std::vector<std::unique_ptr<DebugMap>> Results;
|
2018-06-30 00:51:52 +08:00
|
|
|
for (const auto *Object : *Objects)
|
|
|
|
if (shouldLinkArch(Archs, Object->getArchTriple().getArchName()))
|
|
|
|
Results.push_back(parseOneBinary(*Object, BinaryPath));
|
2015-08-06 02:27:44 +08:00
|
|
|
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Results);
|
2015-08-06 02:27:44 +08:00
|
|
|
}
|
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
/// Interpret the STAB entries to fill the DebugMap.
|
|
|
|
void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
|
|
|
|
uint8_t Type,
|
|
|
|
uint8_t SectionIndex,
|
|
|
|
uint16_t Flags,
|
|
|
|
uint64_t Value) {
|
|
|
|
if (!(Type & MachO::N_STAB))
|
|
|
|
return;
|
|
|
|
|
2014-12-17 04:21:34 +08:00
|
|
|
const char *Name = &MainBinaryStrings.data()[StringIndex];
|
2014-12-13 01:31:24 +08:00
|
|
|
|
|
|
|
// An N_OSO entry represents the start of a new object file description.
|
2016-11-09 19:43:52 +08:00
|
|
|
if (Type == MachO::N_OSO)
|
|
|
|
return switchToNewDebugMapObject(Name, sys::toTimePoint(Value));
|
2014-12-13 01:31:24 +08:00
|
|
|
|
2017-10-06 22:49:20 +08:00
|
|
|
if (Type == MachO::N_AST) {
|
|
|
|
SmallString<80> Path(PathPrefix);
|
|
|
|
sys::path::append(Path, Name);
|
|
|
|
Result->addDebugMapObject(Path, sys::toTimePoint(Value), Type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 19:32:51 +08:00
|
|
|
// If the last N_OSO object file wasn't found, CurrentDebugMapObject will be
|
|
|
|
// null. Do not update anything until we find the next valid N_OSO entry.
|
2014-12-13 01:31:24 +08:00
|
|
|
if (!CurrentDebugMapObject)
|
|
|
|
return;
|
|
|
|
|
2015-03-15 09:29:30 +08:00
|
|
|
uint32_t Size = 0;
|
2014-12-13 01:31:24 +08:00
|
|
|
switch (Type) {
|
|
|
|
case MachO::N_GSYM:
|
|
|
|
// This is a global variable. We need to query the main binary
|
|
|
|
// symbol table to find its address as it might not be in the
|
|
|
|
// debug map (for common symbols).
|
|
|
|
Value = getMainBinarySymbolAddress(Name);
|
|
|
|
break;
|
|
|
|
case MachO::N_FUN:
|
2015-03-15 09:29:30 +08:00
|
|
|
// Functions are scopes in STABS. They have an end marker that
|
|
|
|
// contains the function size.
|
|
|
|
if (Name[0] == '\0') {
|
|
|
|
Size = Value;
|
|
|
|
Value = CurrentFunctionAddress;
|
|
|
|
Name = CurrentFunctionName;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
CurrentFunctionName = Name;
|
|
|
|
CurrentFunctionAddress = Value;
|
2014-12-13 01:31:24 +08:00
|
|
|
return;
|
2015-03-15 09:29:30 +08:00
|
|
|
}
|
2014-12-13 01:31:24 +08:00
|
|
|
case MachO::N_STSYM:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ObjectSymIt = CurrentObjectAddresses.find(Name);
|
2017-09-26 16:17:28 +08:00
|
|
|
|
|
|
|
// If the name of a (non-static) symbol is not in the current object, we
|
|
|
|
// check all its aliases from the main binary.
|
|
|
|
if (ObjectSymIt == CurrentObjectAddresses.end() && Type != MachO::N_STSYM) {
|
|
|
|
for (const auto &Alias : getMainBinarySymbolNames(Value)) {
|
|
|
|
ObjectSymIt = CurrentObjectAddresses.find(Alias);
|
|
|
|
if (ObjectSymIt != CurrentObjectAddresses.end())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 07:33:01 +08:00
|
|
|
// ThinLTO adds a unique suffix to exported private symbols.
|
|
|
|
for (auto Iter = CurrentObjectAddresses.begin();
|
|
|
|
Iter != CurrentObjectAddresses.end(); ++Iter) {
|
|
|
|
llvm::StringRef SymbolName = Iter->getKey();
|
|
|
|
auto Pos = SymbolName.rfind(".llvm.");
|
|
|
|
if (Pos != llvm::StringRef::npos && SymbolName.substr(0, Pos) == Name) {
|
|
|
|
ObjectSymIt = Iter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 17:34:54 +08:00
|
|
|
if (ObjectSymIt == CurrentObjectAddresses.end()) {
|
|
|
|
Warning("could not find object file symbol for symbol " + Twine(Name));
|
|
|
|
return;
|
|
|
|
}
|
2017-09-26 16:17:28 +08:00
|
|
|
|
2016-01-31 12:29:22 +08:00
|
|
|
if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value,
|
2018-03-14 17:34:54 +08:00
|
|
|
Size)) {
|
|
|
|
Warning(Twine("failed to insert symbol '") + Name + "' in the debug map.");
|
|
|
|
return;
|
|
|
|
}
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Load the current object file symbols into CurrentObjectAddresses.
|
2015-07-24 14:41:11 +08:00
|
|
|
void MachODebugMapParser::loadCurrentObjectFileSymbols(
|
|
|
|
const object::MachOObjectFile &Obj) {
|
2014-12-13 01:31:24 +08:00
|
|
|
CurrentObjectAddresses.clear();
|
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
for (auto Sym : Obj.symbols()) {
|
2020-05-02 14:04:04 +08:00
|
|
|
uint64_t Addr = cantFail(Sym.getValue());
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> Name = Sym.getName();
|
|
|
|
if (!Name) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(Name.takeError());
|
2014-12-13 01:31:24 +08:00
|
|
|
continue;
|
2016-04-21 05:24:34 +08:00
|
|
|
}
|
2016-01-31 12:29:34 +08:00
|
|
|
// The value of some categories of symbols isn't meaningful. For
|
|
|
|
// example common symbols store their size in the value field, not
|
|
|
|
// their address. Absolute symbols have a fixed address that can
|
|
|
|
// conflict with standard symbols. These symbols (especially the
|
|
|
|
// common ones), might still be referenced by relocations. These
|
|
|
|
// relocations will use the symbol itself, and won't need an
|
|
|
|
// object file address. The object file address field is optional
|
|
|
|
// in the DebugMap, leave it unassigned for these symbols.
|
2020-04-10 20:24:21 +08:00
|
|
|
uint32_t Flags = cantFail(Sym.getFlags());
|
2019-10-10 00:19:13 +08:00
|
|
|
if (Flags & SymbolRef::SF_Absolute) {
|
2019-10-09 12:16:18 +08:00
|
|
|
CurrentObjectAddresses[*Name] = None;
|
2019-10-10 00:19:13 +08:00
|
|
|
} else if (Flags & SymbolRef::SF_Common) {
|
|
|
|
CurrentObjectAddresses[*Name] = None;
|
2020-01-29 03:23:46 +08:00
|
|
|
CommonSymbols.push_back(std::string(*Name));
|
2019-10-10 00:19:13 +08:00
|
|
|
} else {
|
2015-12-12 01:50:37 +08:00
|
|
|
CurrentObjectAddresses[*Name] = Addr;
|
2019-10-10 00:19:13 +08:00
|
|
|
}
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Lookup a symbol address in the main binary symbol table. The
|
|
|
|
/// parser only needs to query common symbols, thus not every symbol's
|
|
|
|
/// address is available through this function.
|
|
|
|
uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
|
|
|
|
auto Sym = MainBinarySymbolAddresses.find(Name);
|
|
|
|
if (Sym == MainBinarySymbolAddresses.end())
|
2015-07-08 01:12:59 +08:00
|
|
|
return 0;
|
2014-12-13 01:31:24 +08:00
|
|
|
return Sym->second;
|
|
|
|
}
|
|
|
|
|
2017-09-26 16:17:28 +08:00
|
|
|
/// Get all symbol names in the main binary for the given value.
|
|
|
|
std::vector<StringRef>
|
|
|
|
MachODebugMapParser::getMainBinarySymbolNames(uint64_t Value) {
|
|
|
|
std::vector<StringRef> Names;
|
|
|
|
for (const auto &Entry : MainBinarySymbolAddresses) {
|
|
|
|
if (Entry.second == Value)
|
|
|
|
Names.push_back(Entry.first());
|
|
|
|
}
|
|
|
|
return Names;
|
|
|
|
}
|
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
/// Load the interesting main binary symbols' addresses into
|
|
|
|
/// MainBinarySymbolAddresses.
|
2015-07-24 14:41:11 +08:00
|
|
|
void MachODebugMapParser::loadMainBinarySymbols(
|
|
|
|
const MachOObjectFile &MainBinary) {
|
2015-01-06 05:29:28 +08:00
|
|
|
section_iterator Section = MainBinary.section_end();
|
2015-07-24 14:41:11 +08:00
|
|
|
MainBinarySymbolAddresses.clear();
|
2015-01-06 05:29:28 +08:00
|
|
|
for (const auto &Sym : MainBinary.symbols()) {
|
2016-05-03 04:28:12 +08:00
|
|
|
Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
|
|
|
|
if (!TypeOrErr) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(TypeOrErr.takeError());
|
2016-03-24 04:27:00 +08:00
|
|
|
continue;
|
2016-05-03 04:28:12 +08:00
|
|
|
}
|
2016-03-24 04:27:00 +08:00
|
|
|
SymbolRef::Type Type = *TypeOrErr;
|
2014-12-13 01:31:24 +08:00
|
|
|
// Skip undefined and STAB entries.
|
2016-11-01 01:11:23 +08:00
|
|
|
if ((Type == SymbolRef::ST_Debug) || (Type == SymbolRef::ST_Unknown))
|
2014-12-13 01:31:24 +08:00
|
|
|
continue;
|
2018-12-01 02:56:10 +08:00
|
|
|
// In theory, the only symbols of interest are the global variables. These
|
|
|
|
// are the only ones that need to be queried because the address of common
|
|
|
|
// data won't be described in the debug map. All other addresses should be
|
|
|
|
// fetched for the debug map. In reality, by playing with 'ld -r' and
|
|
|
|
// export lists, you can get symbols described as N_GSYM in the debug map,
|
|
|
|
// but associated with a local symbol. Gather all the symbols, but prefer
|
|
|
|
// the global ones.
|
2017-10-10 01:27:47 +08:00
|
|
|
uint8_t SymType =
|
|
|
|
MainBinary.getSymbolTableEntry(Sym.getRawDataRefImpl()).n_type;
|
2018-12-01 02:56:10 +08:00
|
|
|
bool Extern = SymType & (MachO::N_EXT | MachO::N_PEXT);
|
2016-05-03 04:28:12 +08:00
|
|
|
Expected<section_iterator> SectionOrErr = Sym.getSection();
|
|
|
|
if (!SectionOrErr) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(SectionOrErr.takeError());
|
2015-08-08 07:27:14 +08:00
|
|
|
continue;
|
2016-05-03 04:28:12 +08:00
|
|
|
}
|
2015-08-08 07:27:14 +08:00
|
|
|
Section = *SectionOrErr;
|
2020-11-17 03:02:40 +08:00
|
|
|
if ((Section == MainBinary.section_end() || Section->isText()) && !Extern)
|
2015-07-03 04:55:21 +08:00
|
|
|
continue;
|
2020-05-02 14:04:04 +08:00
|
|
|
uint64_t Addr = cantFail(Sym.getValue());
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> NameOrErr = Sym.getName();
|
|
|
|
if (!NameOrErr) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
consumeError(NameOrErr.takeError());
|
2015-07-03 04:55:21 +08:00
|
|
|
continue;
|
2016-04-21 05:24:34 +08:00
|
|
|
}
|
2015-07-03 04:55:21 +08:00
|
|
|
StringRef Name = *NameOrErr;
|
|
|
|
if (Name.size() == 0 || Name[0] == '\0')
|
2014-12-13 01:31:24 +08:00
|
|
|
continue;
|
2018-12-01 02:56:10 +08:00
|
|
|
// Override only if the new key is global.
|
|
|
|
if (Extern)
|
|
|
|
MainBinarySymbolAddresses[Name] = Addr;
|
|
|
|
else
|
|
|
|
MainBinarySymbolAddresses.try_emplace(Name, Addr);
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace dsymutil {
|
2015-08-06 02:27:44 +08:00
|
|
|
llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
|
2020-05-05 11:19:15 +08:00
|
|
|
parseDebugMap(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
|
|
|
|
StringRef InputFile, ArrayRef<std::string> Archs,
|
2018-04-02 18:40:43 +08:00
|
|
|
StringRef PrependPath, bool PaperTrailWarnings, bool Verbose,
|
|
|
|
bool InputIsYAML) {
|
|
|
|
if (InputIsYAML)
|
2015-06-06 04:27:04 +08:00
|
|
|
return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
|
2018-04-02 18:40:43 +08:00
|
|
|
|
2020-05-05 11:19:15 +08:00
|
|
|
MachODebugMapParser Parser(VFS, InputFile, Archs, PrependPath,
|
|
|
|
PaperTrailWarnings, Verbose);
|
2018-04-02 18:40:43 +08:00
|
|
|
return Parser.parse();
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|
2015-08-31 08:29:09 +08:00
|
|
|
|
2020-05-05 11:19:15 +08:00
|
|
|
bool dumpStab(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
|
|
|
|
StringRef InputFile, ArrayRef<std::string> Archs,
|
2015-08-31 08:29:09 +08:00
|
|
|
StringRef PrependPath) {
|
2020-05-05 11:19:15 +08:00
|
|
|
MachODebugMapParser Parser(VFS, InputFile, Archs, PrependPath, false);
|
2015-08-31 08:29:09 +08:00
|
|
|
return Parser.dumpStab();
|
|
|
|
}
|
2015-10-07 07:24:35 +08:00
|
|
|
} // namespace dsymutil
|
|
|
|
} // namespace llvm
|