18113a8bbSFred Riss //===-- ObjectFileMachOTest.cpp -------------------------------------------===//
28113a8bbSFred Riss //
38113a8bbSFred Riss // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48113a8bbSFred Riss // See https://llvm.org/LICENSE.txt for license information.
58113a8bbSFred Riss // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68113a8bbSFred Riss //
78113a8bbSFred Riss //===----------------------------------------------------------------------===//
88113a8bbSFred Riss
98113a8bbSFred Riss #include "lldb/Host/HostInfo.h"
108113a8bbSFred Riss #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
118113a8bbSFred Riss #include "TestingSupport/SubsystemRAII.h"
128113a8bbSFred Riss #include "TestingSupport/TestUtilities.h"
138113a8bbSFred Riss #include "lldb/Core/Module.h"
148113a8bbSFred Riss #include "lldb/Host/FileSystem.h"
158113a8bbSFred Riss #include "lldb/lldb-defines.h"
168113a8bbSFred Riss #include "gtest/gtest.h"
178113a8bbSFred Riss
188113a8bbSFred Riss #ifdef __APPLE__
198113a8bbSFred Riss #include <dlfcn.h>
208113a8bbSFred Riss #endif
218113a8bbSFred Riss
228113a8bbSFred Riss using namespace lldb_private;
238113a8bbSFred Riss using namespace llvm;
248113a8bbSFred Riss
258113a8bbSFred Riss namespace {
268113a8bbSFred Riss class ObjectFileMachOTest : public ::testing::Test {
278113a8bbSFred Riss SubsystemRAII<FileSystem, HostInfo, ObjectFileMachO> subsystems;
288113a8bbSFred Riss };
298113a8bbSFred Riss } // namespace
308113a8bbSFred Riss
318113a8bbSFred Riss #if defined(__APPLE__)
TEST_F(ObjectFileMachOTest,ModuleFromSharedCacheInfo)328113a8bbSFred Riss TEST_F(ObjectFileMachOTest, ModuleFromSharedCacheInfo) {
338113a8bbSFred Riss SharedCacheImageInfo image_info =
348113a8bbSFred Riss HostInfo::GetSharedCacheImageInfo("/usr/lib/libobjc.A.dylib");
358113a8bbSFred Riss EXPECT_TRUE(image_info.uuid);
368113a8bbSFred Riss EXPECT_TRUE(image_info.data_sp);
378113a8bbSFred Riss
388113a8bbSFred Riss ModuleSpec spec(FileSpec(), UUID(), image_info.data_sp);
398113a8bbSFred Riss lldb::ModuleSP module = std::make_shared<Module>(spec);
408113a8bbSFred Riss ObjectFile *OF = module->GetObjectFile();
418113a8bbSFred Riss ASSERT_TRUE(llvm::isa<ObjectFileMachO>(OF));
428113a8bbSFred Riss EXPECT_TRUE(
438113a8bbSFred Riss OF->GetArchitecture().IsCompatibleMatch(HostInfo::GetArchitecture()));
448113a8bbSFred Riss Symtab *symtab = OF->GetSymtab();
458113a8bbSFred Riss ASSERT_NE(symtab, nullptr);
468113a8bbSFred Riss void *libobjc = dlopen("/usr/lib/libobjc.A.dylib", RTLD_LAZY);
478113a8bbSFred Riss ASSERT_NE(libobjc, nullptr);
488113a8bbSFred Riss
498113a8bbSFred Riss // This function checks that if we read something from the
508113a8bbSFred Riss // ObjectFile we get through the shared cache in-mmeory
518113a8bbSFred Riss // buffer, it matches what we get by reading directly the
528113a8bbSFred Riss // memory of the symbol.
538113a8bbSFred Riss auto check_symbol = [&](const char *sym_name) {
548113a8bbSFred Riss std::vector<uint32_t> symbol_indices;
558113a8bbSFred Riss symtab->FindAllSymbolsWithNameAndType(ConstString(sym_name),
568113a8bbSFred Riss lldb::eSymbolTypeAny, symbol_indices);
578113a8bbSFred Riss EXPECT_EQ(symbol_indices.size(), 1u);
588113a8bbSFred Riss
598113a8bbSFred Riss Symbol *sym = symtab->SymbolAtIndex(symbol_indices[0]);
608113a8bbSFred Riss ASSERT_NE(sym, nullptr);
618113a8bbSFred Riss Address base = sym->GetAddress();
628113a8bbSFred Riss size_t size = sym->GetByteSize();
638113a8bbSFred Riss ASSERT_NE(size, 0u);
648113a8bbSFred Riss uint8_t buffer[size];
658113a8bbSFred Riss EXPECT_EQ(OF->ReadSectionData(base.GetSection().get(), base.GetOffset(),
668113a8bbSFred Riss buffer, size),
678113a8bbSFred Riss size);
688113a8bbSFred Riss
698113a8bbSFred Riss void *sym_addr = dlsym(libobjc, sym_name);
708113a8bbSFred Riss ASSERT_NE(sym_addr, nullptr);
718113a8bbSFred Riss EXPECT_EQ(memcmp(buffer, sym_addr, size), 0);
728113a8bbSFred Riss };
738113a8bbSFred Riss
748113a8bbSFred Riss // Read a symbol from the __TEXT segment...
758113a8bbSFred Riss check_symbol("objc_msgSend");
768113a8bbSFred Riss // ... and one from the __DATA segment
778113a8bbSFred Riss check_symbol("OBJC_CLASS_$_NSObject");
788113a8bbSFred Riss }
79*4ad19b80SJonas Devlieghere
TEST_F(ObjectFileMachOTest,IndirectSymbolsInTheSharedCache)80*4ad19b80SJonas Devlieghere TEST_F(ObjectFileMachOTest, IndirectSymbolsInTheSharedCache) {
81*4ad19b80SJonas Devlieghere SharedCacheImageInfo image_info = HostInfo::GetSharedCacheImageInfo(
82*4ad19b80SJonas Devlieghere "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit");
83*4ad19b80SJonas Devlieghere ModuleSpec spec(FileSpec(), UUID(), image_info.data_sp);
84*4ad19b80SJonas Devlieghere lldb::ModuleSP module = std::make_shared<Module>(spec);
85*4ad19b80SJonas Devlieghere
86*4ad19b80SJonas Devlieghere ObjectFile *OF = module->GetObjectFile();
87*4ad19b80SJonas Devlieghere ASSERT_TRUE(llvm::isa<ObjectFileMachO>(OF));
88*4ad19b80SJonas Devlieghere EXPECT_TRUE(
89*4ad19b80SJonas Devlieghere OF->GetArchitecture().IsCompatibleMatch(HostInfo::GetArchitecture()));
90*4ad19b80SJonas Devlieghere
91*4ad19b80SJonas Devlieghere // Check that we can parse the symbol table several times over without
92*4ad19b80SJonas Devlieghere // crashing.
93*4ad19b80SJonas Devlieghere Symtab symtab(OF);
94*4ad19b80SJonas Devlieghere for (size_t i = 0; i < 10; i++)
95*4ad19b80SJonas Devlieghere OF->ParseSymtab(symtab);
96*4ad19b80SJonas Devlieghere }
978113a8bbSFred Riss #endif
98