1*4bd2bfb6SVedant Kumar"""
2*4bd2bfb6SVedant KumarTest that SBProcess.LoadImageUsingPaths uses RTLD_LAZY
3*4bd2bfb6SVedant Kumar"""
4*4bd2bfb6SVedant Kumar
5*4bd2bfb6SVedant Kumar
6*4bd2bfb6SVedant Kumar
7*4bd2bfb6SVedant Kumarimport os
8*4bd2bfb6SVedant Kumarimport shutil
9*4bd2bfb6SVedant Kumarimport lldb
10*4bd2bfb6SVedant Kumarfrom lldbsuite.test.decorators import *
11*4bd2bfb6SVedant Kumarfrom lldbsuite.test.lldbtest import *
12*4bd2bfb6SVedant Kumarfrom lldbsuite.test import lldbutil
13*4bd2bfb6SVedant Kumar
14*4bd2bfb6SVedant Kumar
15*4bd2bfb6SVedant Kumarclass LoadUsingLazyBind(TestBase):
16*4bd2bfb6SVedant Kumar
17*4bd2bfb6SVedant Kumar    mydir = TestBase.compute_mydir(__file__)
18*4bd2bfb6SVedant Kumar    NO_DEBUG_INFO_TESTCASE = True
19*4bd2bfb6SVedant Kumar
20*4bd2bfb6SVedant Kumar    @skipIfRemote
21*4bd2bfb6SVedant Kumar    @skipIfWindows # The Windows platform doesn't implement DoLoadImage.
22*4bd2bfb6SVedant Kumar    # Failing for unknown reasons on Linux, see
23*4bd2bfb6SVedant Kumar    # https://bugs.llvm.org/show_bug.cgi?id=49656.
24*4bd2bfb6SVedant Kumar    @skipUnlessDarwin
25*4bd2bfb6SVedant Kumar    def test_load_using_lazy_bind(self):
26*4bd2bfb6SVedant Kumar        """Test that we load using RTLD_LAZY"""
27*4bd2bfb6SVedant Kumar
28*4bd2bfb6SVedant Kumar        self.build()
29*4bd2bfb6SVedant Kumar        wd = os.path.realpath(self.getBuildDir())
30*4bd2bfb6SVedant Kumar
31*4bd2bfb6SVedant Kumar        ext = '.so'
32*4bd2bfb6SVedant Kumar        if self.platformIsDarwin():
33*4bd2bfb6SVedant Kumar            ext = '.dylib'
34*4bd2bfb6SVedant Kumar
35*4bd2bfb6SVedant Kumar        def make_lib_path(name):
36*4bd2bfb6SVedant Kumar            libpath = os.path.join(wd, name + ext)
37*4bd2bfb6SVedant Kumar            self.assertTrue(os.path.exists(libpath))
38*4bd2bfb6SVedant Kumar            return libpath
39*4bd2bfb6SVedant Kumar
40*4bd2bfb6SVedant Kumar        libt1 = make_lib_path('libt1')
41*4bd2bfb6SVedant Kumar        libt2_0 = make_lib_path('libt2_0')
42*4bd2bfb6SVedant Kumar        libt2_1 = make_lib_path('libt2_1')
43*4bd2bfb6SVedant Kumar
44*4bd2bfb6SVedant Kumar        # Overwrite t2_0 with t2_1 to delete the definition of `use`.
45*4bd2bfb6SVedant Kumar        shutil.copy(libt2_1, libt2_0)
46*4bd2bfb6SVedant Kumar
47*4bd2bfb6SVedant Kumar        # Launch a process and break
48*4bd2bfb6SVedant Kumar        (target, process, thread, _) = lldbutil.run_to_source_breakpoint(self,
49*4bd2bfb6SVedant Kumar                                                "break here",
50*4bd2bfb6SVedant Kumar                                                lldb.SBFileSpec("main.cpp"))
51*4bd2bfb6SVedant Kumar
52*4bd2bfb6SVedant Kumar        # Load libt1; should fail unless we use RTLD_LAZY
53*4bd2bfb6SVedant Kumar        error = lldb.SBError()
54*4bd2bfb6SVedant Kumar        lib_spec = lldb.SBFileSpec('libt1' + ext)
55*4bd2bfb6SVedant Kumar        paths = lldb.SBStringList()
56*4bd2bfb6SVedant Kumar        paths.AppendString(wd)
57*4bd2bfb6SVedant Kumar        out_spec = lldb.SBFileSpec()
58*4bd2bfb6SVedant Kumar        token = process.LoadImageUsingPaths(lib_spec, paths, out_spec, error)
59*4bd2bfb6SVedant Kumar        self.assertNotEqual(token, lldb.LLDB_INVALID_IMAGE_TOKEN, "Got a valid token")
60*4bd2bfb6SVedant Kumar
61*4bd2bfb6SVedant Kumar        # Calling `f1()` should return 5.
62*4bd2bfb6SVedant Kumar        frame = thread.GetFrameAtIndex(0)
63*4bd2bfb6SVedant Kumar        val = frame.EvaluateExpression("f1()")
64*4bd2bfb6SVedant Kumar        self.assertTrue(val.IsValid())
65*4bd2bfb6SVedant Kumar        self.assertEquals(val.GetValueAsSigned(-1), 5)
66