14bd2bfb6SVedant Kumar"""
24bd2bfb6SVedant KumarTest that SBProcess.LoadImageUsingPaths uses RTLD_LAZY
34bd2bfb6SVedant Kumar"""
44bd2bfb6SVedant Kumar
54bd2bfb6SVedant Kumar
64bd2bfb6SVedant Kumar
74bd2bfb6SVedant Kumarimport os
84bd2bfb6SVedant Kumarimport shutil
94bd2bfb6SVedant Kumarimport lldb
104bd2bfb6SVedant Kumarfrom lldbsuite.test.decorators import *
114bd2bfb6SVedant Kumarfrom lldbsuite.test.lldbtest import *
124bd2bfb6SVedant Kumarfrom lldbsuite.test import lldbutil
134bd2bfb6SVedant Kumar
144bd2bfb6SVedant Kumar
154bd2bfb6SVedant Kumarclass LoadUsingLazyBind(TestBase):
164bd2bfb6SVedant Kumar    NO_DEBUG_INFO_TESTCASE = True
174bd2bfb6SVedant Kumar
184bd2bfb6SVedant Kumar    @skipIfRemote
194bd2bfb6SVedant Kumar    @skipIfWindows # The Windows platform doesn't implement DoLoadImage.
20*45597316SMuhammad Omair Javaid    @skipIf(oslist=["linux"], archs=["arm"]) # Fails on arm/linux
214bd2bfb6SVedant Kumar    # Failing for unknown reasons on Linux, see
224bd2bfb6SVedant Kumar    # https://bugs.llvm.org/show_bug.cgi?id=49656.
234bd2bfb6SVedant Kumar    def test_load_using_lazy_bind(self):
244bd2bfb6SVedant Kumar        """Test that we load using RTLD_LAZY"""
254bd2bfb6SVedant Kumar
264bd2bfb6SVedant Kumar        self.build()
274bd2bfb6SVedant Kumar        wd = os.path.realpath(self.getBuildDir())
284bd2bfb6SVedant Kumar
29bad5ee15SPavel Labath        def make_lib_name(name):
30bad5ee15SPavel Labath            return (self.platformContext.shlib_prefix + name + "." +
31bad5ee15SPavel Labath                    self.platformContext.shlib_extension)
324bd2bfb6SVedant Kumar
334bd2bfb6SVedant Kumar        def make_lib_path(name):
34bad5ee15SPavel Labath            libpath = os.path.join(wd, make_lib_name(name))
354bd2bfb6SVedant Kumar            self.assertTrue(os.path.exists(libpath))
364bd2bfb6SVedant Kumar            return libpath
374bd2bfb6SVedant Kumar
38bad5ee15SPavel Labath        libt2_0 = make_lib_path('t2_0')
39bad5ee15SPavel Labath        libt2_1 = make_lib_path('t2_1')
404bd2bfb6SVedant Kumar
414bd2bfb6SVedant Kumar        # Overwrite t2_0 with t2_1 to delete the definition of `use`.
424bd2bfb6SVedant Kumar        shutil.copy(libt2_1, libt2_0)
434bd2bfb6SVedant Kumar
444bd2bfb6SVedant Kumar        # Launch a process and break
454bd2bfb6SVedant Kumar        (target, process, thread, _) = lldbutil.run_to_source_breakpoint(self,
464bd2bfb6SVedant Kumar                                                "break here",
47bad5ee15SPavel Labath                                                lldb.SBFileSpec("main.cpp"),
48bad5ee15SPavel Labath                                                extra_images=["t1"])
494bd2bfb6SVedant Kumar
504bd2bfb6SVedant Kumar        # Load libt1; should fail unless we use RTLD_LAZY
514bd2bfb6SVedant Kumar        error = lldb.SBError()
52bad5ee15SPavel Labath        lib_spec = lldb.SBFileSpec(make_lib_name('t1'))
534bd2bfb6SVedant Kumar        paths = lldb.SBStringList()
544bd2bfb6SVedant Kumar        paths.AppendString(wd)
554bd2bfb6SVedant Kumar        out_spec = lldb.SBFileSpec()
564bd2bfb6SVedant Kumar        token = process.LoadImageUsingPaths(lib_spec, paths, out_spec, error)
574bd2bfb6SVedant Kumar        self.assertNotEqual(token, lldb.LLDB_INVALID_IMAGE_TOKEN, "Got a valid token")
584bd2bfb6SVedant Kumar
594bd2bfb6SVedant Kumar        # Calling `f1()` should return 5.
604bd2bfb6SVedant Kumar        frame = thread.GetFrameAtIndex(0)
614bd2bfb6SVedant Kumar        val = frame.EvaluateExpression("f1()")
624bd2bfb6SVedant Kumar        self.assertTrue(val.IsValid())
634bd2bfb6SVedant Kumar        self.assertEquals(val.GetValueAsSigned(-1), 5)
64