blob: a55b36420587288581576c366148037095e987cc [file] [log] [blame]
jeffhaoacf5aa72012-09-12 17:25:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <dlfcn.h>
Elliott Hughes8e15b082012-09-26 11:44:01 -070020#include <libgen.h>
21#include <limits.h>
22#include <stdio.h>
23#include <stdint.h>
24
Dmitriy Ivanov8de1dde2014-09-04 18:23:00 -070025#include "private/ScopeGuard.h"
26
Elliott Hughes8e15b082012-09-26 11:44:01 -070027#include <string>
jeffhaoacf5aa72012-09-12 17:25:30 -070028
Elliott Hughes3b297c42012-10-11 16:08:51 -070029#define ASSERT_SUBSTR(needle, haystack) \
30 ASSERT_PRED_FORMAT2(::testing::IsSubstring, needle, haystack)
31
Elliott Hughes1728b232014-05-14 10:02:03 -070032static bool g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070033extern "C" void DlSymTestFunction() {
Elliott Hughes1728b232014-05-14 10:02:03 -070034 g_called = true;
jeffhaoacf5aa72012-09-12 17:25:30 -070035}
36
Dmitriy Ivanovf8846a42014-07-08 21:21:34 -070037static int g_ctor_function_called = 0;
38
39extern "C" void ctor_function() __attribute__ ((constructor));
40
41extern "C" void ctor_function() {
42 g_ctor_function_called = 17;
43}
44
45TEST(dlfcn, ctor_function_call) {
46 ASSERT_EQ(17, g_ctor_function_called);
47}
48
Elliott Hughese66190d2012-12-18 15:57:55 -080049TEST(dlfcn, dlsym_in_self) {
Elliott Hughes3b297c42012-10-11 16:08:51 -070050 dlerror(); // Clear any pending errors.
jeffhaoacf5aa72012-09-12 17:25:30 -070051 void* self = dlopen(NULL, RTLD_NOW);
52 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -070053 ASSERT_TRUE(dlerror() == NULL);
jeffhaoacf5aa72012-09-12 17:25:30 -070054
55 void* sym = dlsym(self, "DlSymTestFunction");
56 ASSERT_TRUE(sym != NULL);
57
58 void (*function)() = reinterpret_cast<void(*)()>(sym);
59
Elliott Hughes1728b232014-05-14 10:02:03 -070060 g_called = false;
jeffhaoacf5aa72012-09-12 17:25:30 -070061 function();
Elliott Hughes1728b232014-05-14 10:02:03 -070062 ASSERT_TRUE(g_called);
Elliott Hughes1a696162012-11-01 13:49:32 -070063
64 ASSERT_EQ(0, dlclose(self));
jeffhaoacf5aa72012-09-12 17:25:30 -070065}
Elliott Hughes8e15b082012-09-26 11:44:01 -070066
Dmitriy Ivanovdb7a17d2014-08-04 23:39:22 +000067TEST(dlfcn, dlsym_with_dependencies) {
68 void* handle = dlopen("libtest_with_dependency.so", RTLD_NOW);
69 ASSERT_TRUE(handle != NULL);
70 dlerror();
71 // This symbol is in DT_NEEDED library.
72 void* sym = dlsym(handle, "getRandomNumber");
73 ASSERT_TRUE(sym != NULL);
74 int (*fn)(void);
75 fn = reinterpret_cast<int (*)(void)>(sym);
76 EXPECT_EQ(4, fn());
77 dlclose(handle);
78}
79
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -070080TEST(dlfcn, dlopen_noload) {
81 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
82 ASSERT_TRUE(handle == NULL);
83 handle = dlopen("libtest_simple.so", RTLD_NOW);
84 void* handle2 = dlopen("libtest_simple.so", RTLD_NOW | RTLD_NOLOAD);
85 ASSERT_TRUE(handle != NULL);
86 ASSERT_TRUE(handle2 != NULL);
87 ASSERT_TRUE(handle == handle2);
88 ASSERT_EQ(0, dlclose(handle));
89 ASSERT_EQ(0, dlclose(handle2));
90}
91
Dmitriy Ivanovf4cb6312014-09-11 15:16:03 -070092// ifuncs are only supported on intel and arm64 for now
Dmitriy Ivanov1cd0c672014-10-01 16:26:49 -070093#if defined(__i386__) || defined(__x86_64__)
Brigid Smith31b88da2014-07-23 11:22:25 -070094TEST(dlfcn, ifunc) {
Dmitriy Ivanovbd321c12014-08-21 13:54:03 -070095 typedef const char* (*fn_ptr)();
Brigid Smith31b88da2014-07-23 11:22:25 -070096
97 // ifunc's choice depends on whether IFUNC_CHOICE has a value
98 // first check the set case
99 setenv("IFUNC_CHOICE", "set", 1);
100 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
101 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanovbd321c12014-08-21 13:54:03 -0700102 fn_ptr foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
103 fn_ptr foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smith31b88da2014-07-23 11:22:25 -0700104 ASSERT_TRUE(foo_ptr != NULL);
105 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanovbd321c12014-08-21 13:54:03 -0700106 ASSERT_EQ(strncmp("set", foo_ptr(), 3), 0);
107 ASSERT_EQ(strncmp("set", foo_library_ptr(), 3), 0);
Brigid Smith31b88da2014-07-23 11:22:25 -0700108 dlclose(handle);
109
110 // then check the unset case
111 unsetenv("IFUNC_CHOICE");
112 handle = dlopen("libtest_ifunc.so", RTLD_NOW);
113 ASSERT_TRUE(handle != NULL);
Dmitriy Ivanovbd321c12014-08-21 13:54:03 -0700114 foo_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo"));
115 foo_library_ptr = reinterpret_cast<fn_ptr>(dlsym(handle, "foo_library"));
Brigid Smith31b88da2014-07-23 11:22:25 -0700116 ASSERT_TRUE(foo_ptr != NULL);
117 ASSERT_TRUE(foo_library_ptr != NULL);
Dmitriy Ivanovbd321c12014-08-21 13:54:03 -0700118 ASSERT_EQ(strncmp("unset", foo_ptr(), 5), 0);
119 ASSERT_EQ(strncmp("unset", foo_library_ptr(), 3), 0);
120 dlclose(handle);
121}
122
123TEST(dlfcn, ifunc_ctor_call) {
124 typedef const char* (*fn_ptr)();
125
126 void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
Dmitriy Ivanovf4cb6312014-09-11 15:16:03 -0700127 ASSERT_TRUE(handle != nullptr) << dlerror();
128 fn_ptr is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
129 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
130 ASSERT_STREQ("false", is_ctor_called());
131
132 is_ctor_called = reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
133 ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
Dmitriy Ivanovbd321c12014-08-21 13:54:03 -0700134 ASSERT_STREQ("true", is_ctor_called());
Brigid Smith31b88da2014-07-23 11:22:25 -0700135 dlclose(handle);
136}
137#endif
138
Dmitriy Ivanov8de1dde2014-09-04 18:23:00 -0700139TEST(dlfcn, dlopen_check_relocation_dt_needed_order) {
140 // This is the structure of the test library and
141 // its dt_needed libraries
142 // libtest_relo_check_dt_needed_order.so
143 // |
144 // +-> libtest_relo_check_dt_needed_order_1.so
145 // |
146 // +-> libtest_relo_check_dt_needed_order_2.so
147 //
148 // The root library references relo_test_get_answer_lib - which is defined
149 // in both dt_needed libraries, the correct relocation should
150 // use the function defined in libtest_relo_check_dt_needed_order_1.so
151 void* handle = nullptr;
Dmitriy Ivanovef1306d2014-09-08 16:22:22 -0700152 auto guard = make_scope_guard([&]() {
Dmitriy Ivanov8de1dde2014-09-04 18:23:00 -0700153 dlclose(handle);
154 });
155
156 handle = dlopen("libtest_relo_check_dt_needed_order.so", RTLD_NOW);
157 ASSERT_TRUE(handle != nullptr) << dlerror();
158
159 typedef int (*fn_t) (void);
160 fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "relo_test_get_answer"));
161 ASSERT_TRUE(fn != nullptr) << dlerror();
162 ASSERT_EQ(1, fn());
163}
164
Dmitriy Ivanovae69a952014-09-05 16:42:53 -0700165TEST(dlfcn, dlopen_check_order) {
166 // Here is how the test library and its dt_needed
167 // libraries are arranged
168 //
169 // libtest_check_order.so
170 // |
171 // +-> libtest_check_order_1_left.so
172 // | |
173 // | +-> libtest_check_order_a.so
174 // | |
175 // | +-> libtest_check_order_b.so
176 // |
177 // +-> libtest_check_order_2_right.so
178 // | |
179 // | +-> libtest_check_order_d.so
180 // | |
181 // | +-> libtest_check_order_b.so
182 // |
183 // +-> libtest_check_order_3_c.so
184 //
185 // load order should be (1, 2, 3, a, b, d)
186 //
187 // get_answer() is defined in (2, 3, a, b, c)
188 // get_answer2() is defined in (b, d)
189 void* sym = dlsym(RTLD_DEFAULT, "dlopen_test_get_answer");
190 ASSERT_TRUE(sym == nullptr);
191 void* handle = dlopen("libtest_check_order.so", RTLD_NOW);
192 ASSERT_TRUE(handle != nullptr);
193 typedef int (*fn_t) (void);
194 fn_t fn, fn2;
195 fn = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer"));
196 ASSERT_TRUE(fn != NULL);
197 fn2 = reinterpret_cast<fn_t>(dlsym(RTLD_DEFAULT, "dlopen_test_get_answer2"));
198 ASSERT_TRUE(fn2 != NULL);
199
200 ASSERT_EQ(42, fn());
201 ASSERT_EQ(43, fn2());
202 dlclose(handle);
203}
204
Dmitriy Ivanovc85e82d2014-09-15 17:00:10 -0700205TEST(dlfcn, dlopen_check_rtld_local) {
206 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
207 ASSERT_TRUE(sym == nullptr);
208
209 // implicit RTLD_LOCAL
210 void* handle = dlopen("libtest_simple.so", RTLD_NOW);
211 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
212 ASSERT_TRUE(sym == nullptr);
213 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
214 sym = dlsym(handle, "dlopen_testlib_simple_func");
215 ASSERT_TRUE(sym != nullptr);
216 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
217 dlclose(handle);
218
219 // explicit RTLD_LOCAL
220 handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_LOCAL);
221 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
222 ASSERT_TRUE(sym == nullptr);
223 ASSERT_SUBSTR("undefined symbol: dlopen_testlib_simple_func", dlerror());
224 sym = dlsym(handle, "dlopen_testlib_simple_func");
225 ASSERT_TRUE(sym != nullptr);
226 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
227 dlclose(handle);
228}
229
230TEST(dlfcn, dlopen_check_rtld_global) {
231 void* sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
232 ASSERT_TRUE(sym == nullptr);
233
234 void* handle = dlopen("libtest_simple.so", RTLD_NOW | RTLD_GLOBAL);
235 sym = dlsym(RTLD_DEFAULT, "dlopen_testlib_simple_func");
236 ASSERT_TRUE(sym != nullptr) << dlerror();
237 ASSERT_TRUE(reinterpret_cast<bool (*)(void)>(sym)());
238 dlclose(handle);
239}
240
Dmitriy Ivanovae69a952014-09-05 16:42:53 -0700241// libtest_with_dependency_loop.so -> libtest_with_dependency_loop_a.so ->
242// libtest_with_dependency_loop_b.so -> libtest_with_dependency_loop_c.so ->
243// libtest_with_dependency_loop_a.so
244TEST(dlfcn, dlopen_check_loop) {
245 void* handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW);
Dmitriy Ivanov7d971ec2014-09-09 10:21:42 -0700246 ASSERT_TRUE(handle == nullptr);
Dmitriy Ivanovae69a952014-09-05 16:42:53 -0700247 ASSERT_STREQ("dlopen failed: recursive link to \"libtest_with_dependency_loop_a.so\"", dlerror());
Dmitriy Ivanov7d971ec2014-09-09 10:21:42 -0700248 // This symbol should never be exposed
249 void* f = dlsym(RTLD_DEFAULT, "dlopen_test_invalid_function");
250 ASSERT_TRUE(f == nullptr);
251 ASSERT_SUBSTR("undefined symbol: dlopen_test_invalid_function", dlerror());
252
253 // dlopen second time to make sure that the library wasn't loaded even though dlopen returned null.
254 // This may happen if during cleanup the root library or one of the depended libs were not removed
255 // from soinfo list.
256 handle = dlopen("libtest_with_dependency_loop.so", RTLD_NOW | RTLD_NOLOAD);
257 ASSERT_TRUE(handle == nullptr);
258 ASSERT_STREQ("dlopen failed: library \"libtest_with_dependency_loop.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
Dmitriy Ivanovae69a952014-09-05 16:42:53 -0700259}
260
Elliott Hughese66190d2012-12-18 15:57:55 -0800261TEST(dlfcn, dlopen_failure) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700262 void* self = dlopen("/does/not/exist", RTLD_NOW);
263 ASSERT_TRUE(self == NULL);
Elliott Hughes063525c2014-05-13 11:19:57 -0700264#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700265 ASSERT_STREQ("dlopen failed: library \"/does/not/exist\" not found", dlerror());
266#else
267 ASSERT_STREQ("/does/not/exist: cannot open shared object file: No such file or directory", dlerror());
268#endif
269}
270
Elliott Hughesad88a082012-10-24 18:37:21 -0700271static void* ConcurrentDlErrorFn(void*) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700272 dlopen("/child/thread", RTLD_NOW);
273 return reinterpret_cast<void*>(strdup(dlerror()));
274}
275
Elliott Hughese66190d2012-12-18 15:57:55 -0800276TEST(dlfcn, dlerror_concurrent) {
Elliott Hughes5419b942012-10-16 15:54:46 -0700277 dlopen("/main/thread", RTLD_NOW);
278 const char* main_thread_error = dlerror();
279 ASSERT_SUBSTR("/main/thread", main_thread_error);
280
281 pthread_t t;
282 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentDlErrorFn, NULL));
283 void* result;
284 ASSERT_EQ(0, pthread_join(t, &result));
285 char* child_thread_error = static_cast<char*>(result);
286 ASSERT_SUBSTR("/child/thread", child_thread_error);
287 free(child_thread_error);
288
289 ASSERT_SUBSTR("/main/thread", main_thread_error);
290}
291
Elliott Hughese66190d2012-12-18 15:57:55 -0800292TEST(dlfcn, dlsym_failures) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700293 dlerror(); // Clear any pending errors.
Elliott Hughes8e15b082012-09-26 11:44:01 -0700294 void* self = dlopen(NULL, RTLD_NOW);
295 ASSERT_TRUE(self != NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700296 ASSERT_TRUE(dlerror() == NULL);
297
298 void* sym;
299
Dmitriy Ivanov44adf932014-05-22 09:49:24 -0700300#if defined(__BIONIC__) && !defined(__LP64__)
301 // RTLD_DEFAULT in lp32 bionic is not (void*)0
302 // so it can be distinguished from the NULL handle.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700303 sym = dlsym(NULL, "test");
304 ASSERT_TRUE(sym == NULL);
Elliott Hughes3b297c42012-10-11 16:08:51 -0700305 ASSERT_SUBSTR("dlsym library handle is null", dlerror());
Elliott Hughes3b297c42012-10-11 16:08:51 -0700306#endif
307
308 // NULL symbol name.
Elliott Hughes063525c2014-05-13 11:19:57 -0700309#if defined(__BIONIC__)
Elliott Hughes3b297c42012-10-11 16:08:51 -0700310 // glibc marks this parameter non-null and SEGVs if you cheat.
311 sym = dlsym(self, NULL);
312 ASSERT_TRUE(sym == NULL);
313 ASSERT_SUBSTR("", dlerror());
314#endif
315
316 // Symbol that doesn't exist.
317 sym = dlsym(self, "ThisSymbolDoesNotExist");
318 ASSERT_TRUE(sym == NULL);
319 ASSERT_SUBSTR("undefined symbol: ThisSymbolDoesNotExist", dlerror());
Elliott Hughes1a696162012-11-01 13:49:32 -0700320
321 ASSERT_EQ(0, dlclose(self));
Elliott Hughes3b297c42012-10-11 16:08:51 -0700322}
323
Elliott Hughese66190d2012-12-18 15:57:55 -0800324TEST(dlfcn, dladdr) {
Elliott Hughes3b297c42012-10-11 16:08:51 -0700325 dlerror(); // Clear any pending errors.
326 void* self = dlopen(NULL, RTLD_NOW);
327 ASSERT_TRUE(self != NULL);
328 ASSERT_TRUE(dlerror() == NULL);
Elliott Hughes8e15b082012-09-26 11:44:01 -0700329
330 void* sym = dlsym(self, "DlSymTestFunction");
331 ASSERT_TRUE(sym != NULL);
332
333 // Deliberately ask dladdr for an address inside a symbol, rather than the symbol base address.
334 void* addr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(sym) + 2);
335
336 Dl_info info;
337 int rc = dladdr(addr, &info);
338 ASSERT_NE(rc, 0); // Zero on error, non-zero on success.
339
340 // Get the name of this executable.
341 char executable_path[PATH_MAX];
342 rc = readlink("/proc/self/exe", executable_path, sizeof(executable_path));
343 ASSERT_NE(rc, -1);
344 executable_path[rc] = '\0';
345 std::string executable_name(basename(executable_path));
346
347 // The filename should be that of this executable.
348 // Note that we don't know whether or not we have the full path, so we want an "ends_with" test.
349 std::string dli_fname(info.dli_fname);
350 dli_fname = basename(&dli_fname[0]);
351 ASSERT_EQ(dli_fname, executable_name);
352
353 // The symbol name should be the symbol we looked up.
354 ASSERT_STREQ(info.dli_sname, "DlSymTestFunction");
355
356 // The address should be the exact address of the symbol.
357 ASSERT_EQ(info.dli_saddr, sym);
358
359 // Look in /proc/pid/maps to find out what address we were loaded at.
360 // TODO: factor /proc/pid/maps parsing out into a class and reuse all over bionic.
361 void* base_address = NULL;
Elliott Hughes8e15b082012-09-26 11:44:01 -0700362 char line[BUFSIZ];
Elliott Hughes64218232014-08-25 17:26:50 -0700363 FILE* fp = fopen("/proc/self/maps", "r");
Elliott Hughes8e15b082012-09-26 11:44:01 -0700364 ASSERT_TRUE(fp != NULL);
365 while (fgets(line, sizeof(line), fp) != NULL) {
366 uintptr_t start = strtoul(line, 0, 16);
367 line[strlen(line) - 1] = '\0'; // Chomp the '\n'.
368 char* path = strchr(line, '/');
Elliott Hughes156da962012-10-09 17:14:56 -0700369 if (path != NULL && strcmp(executable_path, path) == 0) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700370 base_address = reinterpret_cast<void*>(start);
371 break;
372 }
373 }
374 fclose(fp);
375
376 // The base address should be the address we were loaded at.
377 ASSERT_EQ(info.dli_fbase, base_address);
Elliott Hughes1a696162012-11-01 13:49:32 -0700378
379 ASSERT_EQ(0, dlclose(self));
Elliott Hughes8e15b082012-09-26 11:44:01 -0700380}
381
Elliott Hughese66190d2012-12-18 15:57:55 -0800382TEST(dlfcn, dladdr_invalid) {
Elliott Hughes8e15b082012-09-26 11:44:01 -0700383 Dl_info info;
384
Elliott Hughes3b297c42012-10-11 16:08:51 -0700385 dlerror(); // Clear any pending errors.
386
Elliott Hughes8e15b082012-09-26 11:44:01 -0700387 // No symbol corresponding to NULL.
388 ASSERT_EQ(dladdr(NULL, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700389 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700390
391 // No symbol corresponding to a stack address.
392 ASSERT_EQ(dladdr(&info, &info), 0); // Zero on error, non-zero on success.
Elliott Hughes3b297c42012-10-11 16:08:51 -0700393 ASSERT_TRUE(dlerror() == NULL); // dladdr(3) doesn't set dlerror(3).
Elliott Hughes8e15b082012-09-26 11:44:01 -0700394}
Elliott Hughes124fae92012-10-31 14:20:03 -0700395
Elliott Hughes124fae92012-10-31 14:20:03 -0700396// Our dynamic linker doesn't support GNU hash tables.
Elliott Hughesa43e9062013-01-07 14:18:22 -0800397#if defined(__BIONIC__)
398// GNU-style ELF hash tables are incompatible with the MIPS ABI.
399// MIPS requires .dynsym to be sorted to match the GOT but GNU-style requires sorting by hash code.
400#if !defined(__mips__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800401TEST(dlfcn, dlopen_library_with_only_gnu_hash) {
Elliott Hughes124fae92012-10-31 14:20:03 -0700402 dlerror(); // Clear any pending errors.
Elliott Hughes6e33b022012-11-07 18:16:02 -0800403 void* handle = dlopen("no-elf-hash-table-library.so", RTLD_NOW);
Elliott Hughes124fae92012-10-31 14:20:03 -0700404 ASSERT_TRUE(handle == NULL);
Elliott Hughes6e33b022012-11-07 18:16:02 -0800405 ASSERT_STREQ("dlopen failed: empty/missing DT_HASH in \"no-elf-hash-table-library.so\" (built with --hash-style=gnu?)", dlerror());
Elliott Hughes124fae92012-10-31 14:20:03 -0700406}
407#endif
Elliott Hughesa43e9062013-01-07 14:18:22 -0800408#endif
Elliott Hughese66190d2012-12-18 15:57:55 -0800409
410TEST(dlfcn, dlopen_bad_flags) {
411 dlerror(); // Clear any pending errors.
412 void* handle;
413
Elliott Hughes063525c2014-05-13 11:19:57 -0700414#if defined(__GLIBC__)
Elliott Hughese66190d2012-12-18 15:57:55 -0800415 // glibc was smart enough not to define RTLD_NOW as 0, so it can detect missing flags.
416 handle = dlopen(NULL, 0);
417 ASSERT_TRUE(handle == NULL);
418 ASSERT_SUBSTR("invalid", dlerror());
419#endif
420
421 handle = dlopen(NULL, 0xffffffff);
422 ASSERT_TRUE(handle == NULL);
423 ASSERT_SUBSTR("invalid", dlerror());
424
425 // glibc actually allows you to choose both RTLD_NOW and RTLD_LAZY at the same time, and so do we.
426 handle = dlopen(NULL, RTLD_NOW|RTLD_LAZY);
427 ASSERT_TRUE(handle != NULL);
428 ASSERT_SUBSTR(NULL, dlerror());
429}
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400430
431TEST(dlfcn, rtld_default_unknown_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800432 void* addr = dlsym(RTLD_DEFAULT, "ANY_UNKNOWN_SYMBOL_NAME");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400433 ASSERT_TRUE(addr == NULL);
434}
435
436TEST(dlfcn, rtld_default_known_symbol) {
Elliott Hughes2ed71092013-11-11 15:48:06 -0800437 void* addr = dlsym(RTLD_DEFAULT, "fopen");
438 ASSERT_TRUE(addr != NULL);
439}
440
441TEST(dlfcn, rtld_next_unknown_symbol) {
442 void* addr = dlsym(RTLD_NEXT, "ANY_UNKNOWN_SYMBOL_NAME");
443 ASSERT_TRUE(addr == NULL);
444}
445
446TEST(dlfcn, rtld_next_known_symbol) {
447 void* addr = dlsym(RTLD_NEXT, "fopen");
Sergey Melnikovebd506c2013-10-31 18:02:12 +0400448 ASSERT_TRUE(addr != NULL);
449}
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700450
Dmitriy Ivanovce441662014-06-17 15:56:38 -0700451TEST(dlfcn, dlsym_weak_func) {
452 dlerror();
453 void* handle = dlopen("libtest_dlsym_weak_func.so",RTLD_NOW);
454 ASSERT_TRUE(handle != NULL);
455
456 int (*weak_func)();
457 weak_func = reinterpret_cast<int (*)()>(dlsym(handle, "weak_func"));
458 ASSERT_TRUE(weak_func != NULL) << "dlerror: " << dlerror();
459 EXPECT_EQ(42, weak_func());
460 dlclose(handle);
461}
462
Dmitriy Ivanov7db18092014-05-08 12:27:25 -0700463TEST(dlfcn, dlopen_symlink) {
464 void* handle1 = dlopen("libdlext_test.so", RTLD_NOW);
465 void* handle2 = dlopen("libdlext_test_v2.so", RTLD_NOW);
466 ASSERT_TRUE(handle1 != NULL);
467 ASSERT_TRUE(handle2 != NULL);
468 ASSERT_EQ(handle1, handle2);
469}