Debian Codegraph @

ext/GOTCHA/src/gotcha_dl.c

raw symbols SCIP
1 #define _GNU_SOURCE
2 #include "gotcha_dl.h"
3
4 #include <dlfcn.h>
5
6 #include "elf_ops.h"
7 #include "libc_wrappers.h"
8 #include "tool.h"
9
10 /**
11 * structure used to pass lookup addr and return library address.
12 */
13 struct Addrs {
14 ElfW(Addr) lookup_addr; // input
15 struct link_map *lmap; // output
16 int found;
17 };
18 /**
19 * This is a callback to get headers for each library.
20 * We check if the caller's virtual address is between base address and the
21 * virtual addr + memory size. Then we select the lmap based on base_addr and
22 * name
23 *
24 * @param info, information about headers
25 * @param size, size of headers
26 * @param data, data from caller
27 * @return
28 */
29 int lib_header_callback(struct dl_phdr_info *info, size_t size, void *data) {
30 struct Addrs *addrs = data;
31 const char *name = NULL;
32 ElfW(Addr) load_address;
33 for (int i = 0; i < info->dlpi_phnum; ++i) {
34 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
35 ElfW(Addr) base_addr = info->dlpi_addr;
36 ElfW(Addr) start_addr = base_addr + info->dlpi_phdr[i].p_vaddr;
37 ElfW(Addr) end_addr = start_addr + info->dlpi_phdr[i].p_memsz;
38 if (addrs->lookup_addr >= start_addr && addrs->lookup_addr < end_addr) {
39 name = info->dlpi_name;
40 load_address = info->dlpi_addr;
41 break;
42 }
43 }
44 }
45 if (name) {
46 struct link_map *current = addrs->lmap;
47 while (current) {
48 if (strcmp(current->l_name, name) == 0 &&
49 load_address == current->l_addr) {
50 addrs->lmap = current;
51 addrs->found = 1;
52 return 1;
53 }
54 current = current->l_next; // GCOVR_EXCL_LINE
55 }
56 }
57 return 0; // GCOVR_EXCL_LINE
58 }
59
60 /**
61 * Implement the logic of _dl_sym for supporting RTLD_NEXT
62 * 1. find the caller library using the program headers.
63 * 2. find the second library which has the symbol for RTLD_NEXT
64 * @param handle, handle for dl operation
65 * @param name, name of the symbol
66 * @param who, the virtual address of the caller
67 * @return link_map pointer
68 */
69
70 static struct link_map *gotchas_dlsym_rtld_next_lookup(const char *name,
71 void *who) {
72 ElfW(Addr) caller = (ElfW(Addr))who;
73 /* Iterative over the library headers and find the caller
74 * the address of the caller is set in addrs->library_laddr
75 **/
76 struct Addrs addrs;
77 addrs.lookup_addr = caller;
78 addrs.lmap = _r_debug.r_map;
79 addrs.found = 0;
80 void *symbol;
81 dl_iterate_phdr(lib_header_callback, &addrs);
82 if (!addrs.found) { // GCOVR_EXCL_START
83 error_printf("RTLD_NEXT used in code not dynamically loaded");
84 exit(127);
85 } // GCOVR_EXCL_STOP
86 struct link_map *handle = addrs.lmap->l_next;
87 while (handle) {
88 /* lookup symbol on the next-to-next lib which has symbol
89 * for RTLD_NEXT
90 **/
91 long result = lookup_exported_symbol(name, handle, &symbol);
92 if (result != -1) {
93 return handle;
94 } else {
95 debug_printf(3, "Symbol %s not found in the library %s\n", name,
96 LIB_NAME(handle));
97 }
98 handle = handle->l_next;
99 }
100 debug_printf(3, "Symbol %s not found in the libraries after caller\n", name);
101 return NULL;
102 }
103
104 gotcha_wrappee_handle_t orig_dlopen_handle;
105 gotcha_wrappee_handle_t orig_dlsym_handle;
106
107 static int per_binding(hash_key_t key, hash_data_t data,
108 void *opaque KNOWN_UNUSED) {
109 int result;
110 struct internal_binding_t *binding = (struct internal_binding_t *)data;
111
112 debug_printf(3, "Trying to re-bind %s from tool %s after dlopen\n",
113 binding->user_binding->name,
114 binding->associated_binding_table->tool->tool_name);
115
116 if (!binding->user_binding->name) return 0;
117 while (binding->next_binding) {
118 binding = binding->next_binding; // GCOVR_EXCL_START
119 debug_printf(3,
120 "Selecting new innermost version of binding %s from tool "
121 "%s.\n", // GCOVR_EXCL_LINE
122 binding->user_binding->name,
123 binding->associated_binding_table->tool->tool_name);
124 } // GCOVR_EXCL_STOP
125
126 result = prepare_symbol(binding);
127 if (result == -1) { // GCOVR_EXCL_START
128 debug_printf(3, "Still could not prepare binding %s after dlopen\n",
129 binding->user_binding->name);
130 return 0;
131 } // GCOVR_EXCL_STOP
132
133 removefrom_hashtable(&notfound_binding_table, key);
134 return 0;
135 }
136
137 static void *dlopen_wrapper(const char *filename, int flags) {
138 typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle);
139 void *handle;
140 debug_printf(1, "User called dlopen(%s, 0x%x)\n", filename,
141 (unsigned int)flags);
142 handle = orig_dlopen(filename, flags);
143
144 debug_printf(
145 2, "Searching new dlopened libraries for previously-not-found exports\n");
146 foreach_hash_entry(&notfound_binding_table, NULL, per_binding);
147
148 debug_printf(2, "Updating GOT entries for new dlopened libraries\n");
149 update_all_library_gots(&function_hash_table);
150
151 return handle;
152 }
153
154 static void *dlsym_wrapper(void *handle, const char *symbol_name) {
155 typeof(&dlopen_wrapper) orig_dlopen = gotcha_get_wrappee(orig_dlopen_handle);
156 typeof(&dlsym_wrapper) orig_dlsym = gotcha_get_wrappee(orig_dlsym_handle);
157 struct internal_binding_t *binding;
158 debug_printf(1, "User called dlsym(%p, %s)\n", handle, symbol_name);
159 int result = lookup_hashtable(&function_hash_table, (hash_key_t)symbol_name,
160 (hash_data_t *)&binding);
161 void *val = orig_dlsym(handle, symbol_name);
162 if (result != -1) {
163 void **wrappee_ptr = getInternalBindingAddressPointer(
164 (struct internal_binding_t **)binding->user_binding->function_handle);
165 if (val == NULL || *wrappee_ptr == val) {
166 // if the wrapper is found and the wrappee is the function requested.
167 // This is needed in cases where we wrap a function F1 from library A and
168 // we dynamically load function F1 from library B. As name is same, we
169 // need to make sure the wrappee are the same as well
170 return binding->user_binding->wrapper_pointer;
171 }
172 }
173 if (handle == RTLD_NEXT) {
174 struct link_map *lib = gotchas_dlsym_rtld_next_lookup(
175 symbol_name, __builtin_return_address(0));
176 if (lib) {
177 void *handle = orig_dlopen(lib->l_name, RTLD_NOW);
178 void *symbol = orig_dlsym(handle, symbol_name);
179 return symbol;
180 }
181 return NULL;
182 } else {
183 return val;
184 }
185 }
186
187 struct gotcha_binding_t dl_binds[] = {
188 {"dlopen", dlopen_wrapper, &orig_dlopen_handle},
189 {"dlsym", dlsym_wrapper, &orig_dlsym_handle}};
190 void handle_libdl() { gotcha_wrap(dl_binds, 2, "gotcha"); }