1 //===-- main.c --------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include <stdio.h>
9 #include <dlfcn.h>
10 #include <limits.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <libgen.h>
14 #include <stdlib.h>
15 
16 int
17 main (int argc, char const *argv[])
18 {
19 #if defined (__APPLE__)
20     const char *a_name = "@executable_path/libloadunload_a.dylib";
21     const char *c_name = "@executable_path/libloadunload_c.dylib";
22 #else
23     const char *a_name = "libloadunload_a.so";
24     const char *c_name = "libloadunload_c.so";
25 #endif
26     void *a_dylib_handle = NULL;
27     void *c_dylib_handle = NULL;
28     int (*a_function) (void);
29 
30     a_dylib_handle = dlopen (a_name, RTLD_NOW); // Set break point at this line for test_lldb_process_load_and_unload_commands().
31     if (a_dylib_handle == NULL)
32     {
33         fprintf (stderr, "%s\n", dlerror());
34         exit (1);
35     }
36 
37     a_function = (int (*) ()) dlsym (a_dylib_handle, "a_function");
38     if (a_function == NULL)
39     {
40         fprintf (stderr, "%s\n", dlerror());
41         exit (2);
42     }
43     printf ("First time around, got: %d\n", a_function ());
44     dlclose (a_dylib_handle);
45 
46     c_dylib_handle = dlopen (c_name, RTLD_NOW);
47     if (c_dylib_handle == NULL)
48     {
49         fprintf (stderr, "%s\n", dlerror());
50         exit (3);
51     }
52     a_function = (int (*) ()) dlsym (c_dylib_handle, "c_function");
53     if (a_function == NULL)
54     {
55         fprintf (stderr, "%s\n", dlerror());
56         exit (4);
57     }
58 
59     a_dylib_handle = dlopen (a_name, RTLD_NOW);
60     if (a_dylib_handle == NULL)
61     {
62         fprintf (stderr, "%s\n", dlerror());
63         exit (5);
64     }
65 
66     a_function = (int (*) ()) dlsym (a_dylib_handle, "a_function");
67     if (a_function == NULL)
68     {
69         fprintf (stderr, "%s\n", dlerror());
70         exit (6);
71     }
72     printf ("Second time around, got: %d\n", a_function ());
73     dlclose (a_dylib_handle);
74 
75     int d_function(void);
76     printf ("d_function returns: %d\n", d_function());
77 
78     return 0;
79 }
80