#include "BinaryHolder.h"
#include "DebugMap.h"
#include "MachOUtils.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/Object/MachO.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <vector>
namespace {
using namespace llvm;
using namespace llvm::dsymutil;
using namespace llvm::object;
class MachODebugMapParser {
public:
MachODebugMapParser(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
StringRef BinaryPath, ArrayRef<std::string> Archs,
StringRef PathPrefix = "",
bool PaperTrailWarnings = false, bool Verbose = false)
: BinaryPath(std::string(BinaryPath)), Archs(Archs.begin(), Archs.end()),
PathPrefix(std::string(PathPrefix)),
PaperTrailWarnings(PaperTrailWarnings), BinHolder(VFS, Verbose),
CurrentDebugMapObject(nullptr) {}
ErrorOr<std::vector<std::unique_ptr<DebugMap>>> parse();
bool dumpStab();
private:
std::string BinaryPath;
SmallVector<StringRef, 1> Archs;
std::string PathPrefix;
bool PaperTrailWarnings;
BinaryHolder BinHolder;
StringMap<uint64_t> MainBinarySymbolAddresses;
StringRef MainBinaryStrings;
std::unique_ptr<DebugMap> Result;
std::vector<std::string> CommonSymbols;
StringMap<Optional<uint64_t>> CurrentObjectAddresses;
StringMap<Optional<uint64_t>> CurrentObjectAliasMap;
SmallSet<uint64_t, 4> SeenAliasValues;
DebugMapObject *CurrentDebugMapObject;
const char *CurrentFunctionName;
uint64_t CurrentFunctionAddress;
std::unique_ptr<DebugMap> parseOneBinary(const MachOObjectFile &MainBinary,
StringRef BinaryPath);
void
switchToNewDebugMapObject(StringRef Filename,
sys::TimePoint<std::chrono::seconds> Timestamp);
void resetParserState();
uint64_t getMainBinarySymbolAddress(StringRef Name);
std::vector<StringRef> getMainBinarySymbolNames(uint64_t Value);
void loadMainBinarySymbols(const MachOObjectFile &MainBinary);
void loadCurrentObjectFileSymbols(const object::MachOObjectFile &Obj);
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);
}
void addCommonSymbols();
void dumpSymTabHeader(raw_ostream &OS, StringRef Arch);
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);
void Warning(const Twine &Msg, StringRef File = StringRef()) {
WithColor::warning() << "("
<< MachOUtils::getArchName(
Result->getTriple().getArchName())
<< ") " << File << " " << Msg << "\n";
if (PaperTrailWarnings) {
if (!File.empty())
Result->addDebugMapObject(File, sys::TimePoint<std::chrono::seconds>());
if (Result->end() != Result->begin())
(*--Result->end())->addWarning(Msg.str());
}
}
};
}
void MachODebugMapParser::resetParserState() {
CommonSymbols.clear();
CurrentObjectAddresses.clear();
CurrentObjectAliasMap.clear();
SeenAliasValues.clear();
CurrentDebugMapObject = nullptr;
}
void MachODebugMapParser::addCommonSymbols() {
for (auto &CommonSymbol : CommonSymbols) {
uint64_t CommonAddr = getMainBinarySymbolAddress(CommonSymbol);
if (CommonAddr == 0) {
continue;
}
if (!CurrentDebugMapObject->addSymbol(CommonSymbol, None ,
CommonAddr, 0 )) {
continue;
}
}
}
void MachODebugMapParser::switchToNewDebugMapObject(
StringRef Filename, sys::TimePoint<std::chrono::seconds> Timestamp) {
addCommonSymbols();
resetParserState();
SmallString<80> Path(PathPrefix);
sys::path::append(Path, Filename);
auto ObjectEntry = BinHolder.getObjectEntry(Path, Timestamp);
if (!ObjectEntry) {
auto Err = ObjectEntry.takeError();
Warning("unable to open object file: " + toString(std::move(Err)),
Path.str());
return;
}
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());
return;
}
CurrentDebugMapObject =
&Result->addDebugMapObject(Path, Timestamp, MachO::N_OSO);
loadCurrentObjectFileSymbols(*Object);
}
static std::string getArchName(const object::MachOObjectFile &Obj) {
Triple T = Obj.getArchTriple();
return std::string(T.getArchName());
}
std::unique_ptr<DebugMap>
MachODebugMapParser::parseOneBinary(const MachOObjectFile &MainBinary,
StringRef BinaryPath) {
loadMainBinarySymbols(MainBinary);
ArrayRef<uint8_t> UUID = MainBinary.getUuid();
Result =
std::make_unique<DebugMap>(MainBinary.getArchTriple(), BinaryPath, UUID);
MainBinaryStrings = MainBinary.getStringTableData();
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);
}
struct DarwinStabName {
uint8_t NType;
const char *Name;
};
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"},
{MachO::N_LENG, "N_LENG"}, {0, nullptr}};
static const char *getDarwinStabString(uint8_t NType) {
for (unsigned i = 0; DarwinStabNames[i].Name; i++) {
if (DarwinStabNames[i].NType == NType)
return DarwinStabNames[i].Name;
}
return nullptr;
}
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) {
OS << '[' << format_decimal(Index, 6)
<< "] "
<< format_hex_no_prefix(StringIndex, 8)
<< ' '
<< 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: OS << "UNDF";
break;
case MachO::N_ABS: OS << "ABS ";
break;
case MachO::N_SECT: OS << "SECT";
break;
case MachO::N_PBUD: OS << "PBUD";
break;
case MachO::N_INDR: OS << "INDR";
break;
default:
OS << format_hex_no_prefix(Type, 2) << " ";
break;
}
if (Type & MachO::N_EXT)
OS << " EXT";
else
OS << " ";
}
OS << ") "
<< format_hex_no_prefix(SectionIndex, 2)
<< " "
<< format_hex_no_prefix(Flags, 4)
<< " "
<< 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());
dumpSymTabHeader(OS, getArchName(MainBinary));
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();
}
static bool shouldLinkArch(SmallVectorImpl<StringRef> &Archs, StringRef Arch) {
if (Archs.empty() || is_contained(Archs, "all") || is_contained(Archs, "*"))
return true;
if (Arch.startswith("arm") && Arch != "arm64" && is_contained(Archs, "arm"))
return true;
SmallString<16> ArchName = Arch;
if (Arch.startswith("thumb"))
ArchName = ("arm" + Arch.substr(5)).str();
return is_contained(Archs, ArchName);
}
bool MachODebugMapParser::dumpStab() {
auto ObjectEntry = BinHolder.getObjectEntry(BinaryPath);
if (!ObjectEntry) {
auto Err = ObjectEntry.takeError();
WithColor::error() << "cannot load '" << BinaryPath
<< "': " << toString(std::move(Err)) << '\n';
return false;
}
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);
return true;
}
ErrorOr<std::vector<std::unique_ptr<DebugMap>>> MachODebugMapParser::parse() {
auto ObjectEntry = BinHolder.getObjectEntry(BinaryPath);
if (!ObjectEntry) {
return errorToErrorCode(ObjectEntry.takeError());
}
auto Objects = ObjectEntry->getObjectsAs<MachOObjectFile>();
if (!Objects) {
return errorToErrorCode(Objects.takeError());
}
std::vector<std::unique_ptr<DebugMap>> Results;
for (const auto *Object : *Objects)
if (shouldLinkArch(Archs, Object->getArchTriple().getArchName()))
Results.push_back(parseOneBinary(*Object, BinaryPath));
return std::move(Results);
}
void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex,
uint8_t Type,
uint8_t SectionIndex,
uint16_t Flags,
uint64_t Value) {
if (!(Type & MachO::N_STAB))
return;
const char *Name = &MainBinaryStrings.data()[StringIndex];
if (Type == MachO::N_OSO)
return switchToNewDebugMapObject(Name, sys::toTimePoint(Value));
if (Type == MachO::N_AST) {
SmallString<80> Path(PathPrefix);
sys::path::append(Path, Name);
Result->addDebugMapObject(Path, sys::toTimePoint(Value), Type);
return;
}
if (!CurrentDebugMapObject)
return;
uint32_t Size = 0;
switch (Type) {
case MachO::N_GSYM:
Value = getMainBinarySymbolAddress(Name);
break;
case MachO::N_FUN:
if (Name[0] == '\0') {
Size = Value;
Value = CurrentFunctionAddress;
Name = CurrentFunctionName;
break;
} else {
CurrentFunctionName = Name;
CurrentFunctionAddress = Value;
return;
}
case MachO::N_STSYM:
break;
default:
return;
}
auto ObjectSymIt = CurrentObjectAddresses.find(Name);
if (ObjectSymIt == CurrentObjectAddresses.end() && Type != MachO::N_STSYM) {
if (SeenAliasValues.count(Value) == 0) {
auto Aliases = getMainBinarySymbolNames(Value);
for (const auto &Alias : Aliases) {
auto It = CurrentObjectAddresses.find(Alias);
if (It != CurrentObjectAddresses.end()) {
auto AliasValue = It->getValue();
for (const auto &Alias : Aliases)
CurrentObjectAliasMap[Alias] = AliasValue;
break;
}
}
SeenAliasValues.insert(Value);
}
auto AliasIt = CurrentObjectAliasMap.find(Name);
if (AliasIt != CurrentObjectAliasMap.end())
ObjectSymIt = AliasIt;
}
if (ObjectSymIt == CurrentObjectAddresses.end()) {
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;
}
}
}
if (ObjectSymIt == CurrentObjectAddresses.end()) {
Warning("could not find object file symbol for symbol " + Twine(Name));
return;
}
if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value,
Size)) {
Warning(Twine("failed to insert symbol '") + Name + "' in the debug map.");
return;
}
}
void MachODebugMapParser::loadCurrentObjectFileSymbols(
const object::MachOObjectFile &Obj) {
CurrentObjectAddresses.clear();
for (auto Sym : Obj.symbols()) {
uint64_t Addr = cantFail(Sym.getValue());
Expected<StringRef> Name = Sym.getName();
if (!Name) {
auto Err = Name.takeError();
Warning("failed to get symbol name: " + toString(std::move(Err)),
Obj.getFileName());
continue;
}
uint32_t Flags = cantFail(Sym.getFlags());
if (Flags & SymbolRef::SF_Absolute) {
CurrentObjectAddresses[*Name] = None;
} else if (Flags & SymbolRef::SF_Common) {
CurrentObjectAddresses[*Name] = None;
CommonSymbols.push_back(std::string(*Name));
} else {
CurrentObjectAddresses[*Name] = Addr;
}
}
}
uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) {
auto Sym = MainBinarySymbolAddresses.find(Name);
if (Sym == MainBinarySymbolAddresses.end())
return 0;
return Sym->second;
}
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;
}
void MachODebugMapParser::loadMainBinarySymbols(
const MachOObjectFile &MainBinary) {
section_iterator Section = MainBinary.section_end();
MainBinarySymbolAddresses.clear();
for (const auto &Sym : MainBinary.symbols()) {
Expected<SymbolRef::Type> TypeOrErr = Sym.getType();
if (!TypeOrErr) {
auto Err = TypeOrErr.takeError();
Warning("failed to get symbol type: " + toString(std::move(Err)),
MainBinary.getFileName());
continue;
}
SymbolRef::Type Type = *TypeOrErr;
if ((Type == SymbolRef::ST_Debug) || (Type == SymbolRef::ST_Unknown))
continue;
uint8_t SymType =
MainBinary.getSymbolTableEntry(Sym.getRawDataRefImpl()).n_type;
bool Extern = SymType & (MachO::N_EXT | MachO::N_PEXT);
Expected<section_iterator> SectionOrErr = Sym.getSection();
if (!SectionOrErr) {
auto Err = TypeOrErr.takeError();
Warning("failed to get symbol section: " + toString(std::move(Err)),
MainBinary.getFileName());
continue;
}
Section = *SectionOrErr;
if ((Section == MainBinary.section_end() || Section->isText()) && !Extern)
continue;
uint64_t Addr = cantFail(Sym.getValue());
Expected<StringRef> NameOrErr = Sym.getName();
if (!NameOrErr) {
auto Err = NameOrErr.takeError();
Warning("failed to get symbol name: " + toString(std::move(Err)),
MainBinary.getFileName());
continue;
}
StringRef Name = *NameOrErr;
if (Name.size() == 0 || Name[0] == '\0')
continue;
if (Extern)
MainBinarySymbolAddresses[Name] = Addr;
else
MainBinarySymbolAddresses.try_emplace(Name, Addr);
}
}
namespace llvm {
namespace dsymutil {
llvm::ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
parseDebugMap(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
StringRef InputFile, ArrayRef<std::string> Archs,
StringRef PrependPath, bool PaperTrailWarnings, bool Verbose,
bool InputIsYAML) {
if (InputIsYAML)
return DebugMap::parseYAMLDebugMap(InputFile, PrependPath, Verbose);
MachODebugMapParser Parser(VFS, InputFile, Archs, PrependPath,
PaperTrailWarnings, Verbose);
return Parser.parse();
}
bool dumpStab(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
StringRef InputFile, ArrayRef<std::string> Archs,
StringRef PrependPath) {
MachODebugMapParser Parser(VFS, InputFile, Archs, PrependPath, false);
return Parser.dumpStab();
}
} }