#include "lldb/Host/HostInfo.h"
#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
#include "TestingSupport/SubsystemRAII.h"
#include "TestingSupport/TestUtilities.h"
#include "lldb/Core/Module.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/lldb-defines.h"
#include "gtest/gtest.h"
#ifdef __APPLE__
#include <dlfcn.h>
#endif
usingnamespacelldb_private;
usingnamespacellvm;
namespace {
class ObjectFileMachOTest : public ::testing::Test { … };
}
#if defined(__APPLE__)
TEST_F(ObjectFileMachOTest, ModuleFromSharedCacheInfo) {
SharedCacheImageInfo image_info =
HostInfo::GetSharedCacheImageInfo("/usr/lib/libobjc.A.dylib");
EXPECT_TRUE(image_info.uuid);
EXPECT_TRUE(image_info.data_sp);
ModuleSpec spec(FileSpec(), UUID(), image_info.data_sp);
lldb::ModuleSP module = std::make_shared<Module>(spec);
ObjectFile *OF = module->GetObjectFile();
ASSERT_TRUE(llvm::isa<ObjectFileMachO>(OF));
EXPECT_TRUE(
OF->GetArchitecture().IsCompatibleMatch(HostInfo::GetArchitecture()));
Symtab *symtab = OF->GetSymtab();
ASSERT_NE(symtab, nullptr);
void *libobjc = dlopen("/usr/lib/libobjc.A.dylib", RTLD_LAZY);
ASSERT_NE(libobjc, nullptr);
auto check_symbol = [&](const char *sym_name) {
std::vector<uint32_t> symbol_indices;
symtab->FindAllSymbolsWithNameAndType(ConstString(sym_name),
lldb::eSymbolTypeAny, symbol_indices);
EXPECT_EQ(symbol_indices.size(), 1u);
Symbol *sym = symtab->SymbolAtIndex(symbol_indices[0]);
ASSERT_NE(sym, nullptr);
Address base = sym->GetAddress();
size_t size = sym->GetByteSize();
ASSERT_NE(size, 0u);
uint8_t buffer[size];
EXPECT_EQ(OF->ReadSectionData(base.GetSection().get(), base.GetOffset(),
buffer, size),
size);
void *sym_addr = dlsym(libobjc, sym_name);
ASSERT_NE(sym_addr, nullptr);
EXPECT_EQ(memcmp(buffer, sym_addr, size), 0);
};
check_symbol("objc_msgSend");
check_symbol("OBJC_CLASS_$_NSObject");
}
TEST_F(ObjectFileMachOTest, IndirectSymbolsInTheSharedCache) {
SharedCacheImageInfo image_info = HostInfo::GetSharedCacheImageInfo(
"/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit");
ModuleSpec spec(FileSpec(), UUID(), image_info.data_sp);
lldb::ModuleSP module = std::make_shared<Module>(spec);
ObjectFile *OF = module->GetObjectFile();
ASSERT_TRUE(llvm::isa<ObjectFileMachO>(OF));
EXPECT_TRUE(
OF->GetArchitecture().IsCompatibleMatch(HostInfo::GetArchitecture()));
Symtab symtab(OF);
for (size_t i = 0; i < 10; i++)
OF->ParseSymtab(symtab);
}
#endif