1*8113a8bbSFred Riss //===-- ObjectFileMachOTest.cpp -------------------------------------------===//
2*8113a8bbSFred Riss //
3*8113a8bbSFred Riss // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*8113a8bbSFred Riss // See https://llvm.org/LICENSE.txt for license information.
5*8113a8bbSFred Riss // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*8113a8bbSFred Riss //
7*8113a8bbSFred Riss //===----------------------------------------------------------------------===//
8*8113a8bbSFred Riss 
9*8113a8bbSFred Riss #include "lldb/Host/HostInfo.h"
10*8113a8bbSFred Riss #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
11*8113a8bbSFred Riss #include "TestingSupport/SubsystemRAII.h"
12*8113a8bbSFred Riss #include "TestingSupport/TestUtilities.h"
13*8113a8bbSFred Riss #include "lldb/Core/Module.h"
14*8113a8bbSFred Riss #include "lldb/Host/FileSystem.h"
15*8113a8bbSFred Riss #include "lldb/lldb-defines.h"
16*8113a8bbSFred Riss #include "gtest/gtest.h"
17*8113a8bbSFred Riss 
18*8113a8bbSFred Riss #ifdef __APPLE__
19*8113a8bbSFred Riss #include <dlfcn.h>
20*8113a8bbSFred Riss #endif
21*8113a8bbSFred Riss 
22*8113a8bbSFred Riss using namespace lldb_private;
23*8113a8bbSFred Riss using namespace llvm;
24*8113a8bbSFred Riss 
25*8113a8bbSFred Riss namespace {
26*8113a8bbSFred Riss class ObjectFileMachOTest : public ::testing::Test {
27*8113a8bbSFred Riss   SubsystemRAII<FileSystem, HostInfo, ObjectFileMachO> subsystems;
28*8113a8bbSFred Riss };
29*8113a8bbSFred Riss } // namespace
30*8113a8bbSFred Riss 
31*8113a8bbSFred Riss #if defined(__APPLE__)
32*8113a8bbSFred Riss TEST_F(ObjectFileMachOTest, ModuleFromSharedCacheInfo) {
33*8113a8bbSFred Riss   SharedCacheImageInfo image_info =
34*8113a8bbSFred Riss       HostInfo::GetSharedCacheImageInfo("/usr/lib/libobjc.A.dylib");
35*8113a8bbSFred Riss   EXPECT_TRUE(image_info.uuid);
36*8113a8bbSFred Riss   EXPECT_TRUE(image_info.data_sp);
37*8113a8bbSFred Riss 
38*8113a8bbSFred Riss   ModuleSpec spec(FileSpec(), UUID(), image_info.data_sp);
39*8113a8bbSFred Riss   lldb::ModuleSP module = std::make_shared<Module>(spec);
40*8113a8bbSFred Riss   ObjectFile *OF = module->GetObjectFile();
41*8113a8bbSFred Riss   ASSERT_TRUE(llvm::isa<ObjectFileMachO>(OF));
42*8113a8bbSFred Riss   EXPECT_TRUE(
43*8113a8bbSFred Riss       OF->GetArchitecture().IsCompatibleMatch(HostInfo::GetArchitecture()));
44*8113a8bbSFred Riss   Symtab *symtab = OF->GetSymtab();
45*8113a8bbSFred Riss   ASSERT_NE(symtab, nullptr);
46*8113a8bbSFred Riss   void *libobjc = dlopen("/usr/lib/libobjc.A.dylib", RTLD_LAZY);
47*8113a8bbSFred Riss   ASSERT_NE(libobjc, nullptr);
48*8113a8bbSFred Riss 
49*8113a8bbSFred Riss   // This function checks that if we read something from the
50*8113a8bbSFred Riss   // ObjectFile we get through the shared cache in-mmeory
51*8113a8bbSFred Riss   // buffer, it matches what we get by reading directly the
52*8113a8bbSFred Riss   // memory of the symbol.
53*8113a8bbSFred Riss   auto check_symbol = [&](const char *sym_name) {
54*8113a8bbSFred Riss     std::vector<uint32_t> symbol_indices;
55*8113a8bbSFred Riss     symtab->FindAllSymbolsWithNameAndType(ConstString(sym_name),
56*8113a8bbSFred Riss                                           lldb::eSymbolTypeAny, symbol_indices);
57*8113a8bbSFred Riss     EXPECT_EQ(symbol_indices.size(), 1u);
58*8113a8bbSFred Riss 
59*8113a8bbSFred Riss     Symbol *sym = symtab->SymbolAtIndex(symbol_indices[0]);
60*8113a8bbSFred Riss     ASSERT_NE(sym, nullptr);
61*8113a8bbSFred Riss     Address base = sym->GetAddress();
62*8113a8bbSFred Riss     size_t size = sym->GetByteSize();
63*8113a8bbSFred Riss     ASSERT_NE(size, 0u);
64*8113a8bbSFred Riss     uint8_t buffer[size];
65*8113a8bbSFred Riss     EXPECT_EQ(OF->ReadSectionData(base.GetSection().get(), base.GetOffset(),
66*8113a8bbSFred Riss                                   buffer, size),
67*8113a8bbSFred Riss               size);
68*8113a8bbSFred Riss 
69*8113a8bbSFred Riss     void *sym_addr = dlsym(libobjc, sym_name);
70*8113a8bbSFred Riss     ASSERT_NE(sym_addr, nullptr);
71*8113a8bbSFred Riss     EXPECT_EQ(memcmp(buffer, sym_addr, size), 0);
72*8113a8bbSFred Riss   };
73*8113a8bbSFred Riss 
74*8113a8bbSFred Riss   // Read a symbol from the __TEXT segment...
75*8113a8bbSFred Riss   check_symbol("objc_msgSend");
76*8113a8bbSFred Riss   // ... and one from the __DATA segment
77*8113a8bbSFred Riss   check_symbol("OBJC_CLASS_$_NSObject");
78*8113a8bbSFred Riss }
79*8113a8bbSFred Riss #endif
80