blob: 2f3e9056fdbacf6d70071f62775b79fdf7107a55 [file] [log] [blame]
Dmitriy Ivanov0416d882014-11-04 09:38:18 -08001/*
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
Jiyong Park4945d8f2017-08-30 11:30:53 +090019#if defined(__BIONIC__)
20#include <android-base/properties.h>
21#endif
22
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080023#include <dlfcn.h>
24#include <libgen.h>
25#include <limits.h>
26#include <stdio.h>
27#include <stdint.h>
Dmytro Chystiakov595c3812019-10-01 11:28:49 -070028#include <sys/stat.h>
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080029
Jiyong Park02586a22017-05-20 01:01:24 +090030#include <fstream>
Elliott Hughesec580d32021-04-12 15:55:29 -070031#include <iostream>
32#include <regex>
33#include <string>
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080034
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080035#include <android-base/file.h>
Elliott Hughes3f73ea62022-10-19 16:16:54 +000036#include <android-base/macros.h>
Florian Mayer750dcd32022-04-15 15:54:47 -070037#include <android-base/test_utils.h>
Elliott Hughes3f73ea62022-10-19 16:16:54 +000038#include "gtest_globals.h"
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080039#include "utils.h"
40
Dmitriy Ivanov0416d882014-11-04 09:38:18 -080041extern "C" int main_global_default_serial() {
42 return 3370318;
43}
44
45extern "C" int main_global_protected_serial() {
46 return 2716057;
47}
48
49// The following functions are defined in DT_NEEDED
50// libdl_preempt_test.so library.
51
52// This one calls main_global_default_serial
53extern "C" int main_global_default_get_serial();
54
55// This one calls main_global_protected_serial
56extern "C" int main_global_protected_get_serial();
57
58// This one calls lib_global_default_serial
59extern "C" int lib_global_default_get_serial();
60
61// This one calls lib_global_protected_serial
62extern "C" int lib_global_protected_get_serial();
63
64// This test verifies that the global default function
65// main_global_default_serial() is preempted by
66// the function defined above.
67TEST(dl, main_preempts_global_default) {
68 ASSERT_EQ(3370318, main_global_default_get_serial());
69}
70
71// This one makes sure that the global protected
72// symbols do not get preempted
73TEST(dl, main_does_not_preempt_global_protected) {
74 ASSERT_EQ(3370318, main_global_protected_get_serial());
75}
76
77// check same things for lib
78TEST(dl, lib_preempts_global_default) {
79 ASSERT_EQ(3370318, lib_global_default_get_serial());
80}
81
82TEST(dl, lib_does_not_preempt_global_protected) {
83 ASSERT_EQ(3370318, lib_global_protected_get_serial());
84}
85
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080086#if defined(__BIONIC__)
87#if defined(__LP64__)
Elliott Hughes3f73ea62022-10-19 16:16:54 +000088#define LINKER_NAME "linker64"
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080089#else
Elliott Hughes3f73ea62022-10-19 16:16:54 +000090#define LINKER_NAME "linker"
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -080091#endif
Elliott Hughes3f73ea62022-10-19 16:16:54 +000092static constexpr const char* kPathToLinker = "/system/bin/" LINKER_NAME;
93static constexpr const char* kAlternatePathToLinker = "/system/bin/" ABI_STRING "/" LINKER_NAME;
94#undef LINKER_NAME
Dmytro Chystiakov595c3812019-10-01 11:28:49 -070095
96const char* PathToLinker() {
97 // On the systems with emulated architecture linker would be of different
98 // architecture. Try to use alternate paths first.
99 struct stat buffer;
100 if (stat(kAlternatePathToLinker, &buffer) == 0) {
101 return kAlternatePathToLinker;
102 }
103 return kPathToLinker;
104}
105#endif // defined(__BIONIC__)
Ryan Prichard8f639a42018-10-01 23:10:05 -0700106
107TEST(dl, exec_linker) {
108#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700109 const char* path_to_linker = PathToLinker();
110 std::string usage_prefix = std::string("Usage: ") + path_to_linker;
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -0800111 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700112 eth.SetArgs({ path_to_linker, nullptr });
113 eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Ryan Prichard8f639a42018-10-01 23:10:05 -0700114 ASSERT_EQ(0u, eth.GetOutput().find(usage_prefix)) << "Test output:\n" << eth.GetOutput();
115#endif
116}
117
118TEST(dl, exec_linker_load_file) {
119#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700120 const char* path_to_linker = PathToLinker();
Colin Crossbadcb382021-09-24 17:49:58 -0700121 std::string helper = GetTestlibRoot() + "/exec_linker_helper";
Ryan Prichard8f639a42018-10-01 23:10:05 -0700122 std::string expected_output =
123 "ctor: argc=1 argv[0]=" + helper + "\n" +
124 "main: argc=1 argv[0]=" + helper + "\n" +
Elliott Hughes75064c12020-01-22 20:46:12 -0800125 "__progname=exec_linker_helper\n" +
Ryan Prichard8f639a42018-10-01 23:10:05 -0700126 "helper_func called\n";
127 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700128 eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
Elliott Hughes419554e2021-10-01 10:12:15 -0700129 eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
130 ASSERT_EQ(expected_output, eth.GetOutput());
Dimitry Ivanov2a6955e2017-02-23 11:53:43 -0800131#endif
132}
133
Ryan Prichard8f639a42018-10-01 23:10:05 -0700134TEST(dl, exec_linker_load_from_zip) {
135#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700136 const char* path_to_linker = PathToLinker();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700137 std::string helper = GetTestlibRoot() +
138 "/libdlext_test_zip/libdlext_test_zip_zipaligned.zip!/libdir/exec_linker_helper";
139 std::string expected_output =
140 "ctor: argc=1 argv[0]=" + helper + "\n" +
141 "main: argc=1 argv[0]=" + helper + "\n" +
Elliott Hughes75064c12020-01-22 20:46:12 -0800142 "__progname=exec_linker_helper\n" +
Ryan Prichard8f639a42018-10-01 23:10:05 -0700143 "helper_func called\n";
144 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700145 eth.SetArgs({ path_to_linker, helper.c_str(), nullptr });
Elliott Hughes419554e2021-10-01 10:12:15 -0700146 eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
147 ASSERT_EQ(expected_output, eth.GetOutput());
Ryan Prichard8f639a42018-10-01 23:10:05 -0700148#endif
149}
150
151TEST(dl, exec_linker_load_self) {
152#if defined(__BIONIC__)
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700153 const char* path_to_linker = PathToLinker();
Ryan Prichard8f639a42018-10-01 23:10:05 -0700154 std::string error_message = "error: linker cannot load itself\n";
155 ExecTestHelper eth;
Dmytro Chystiakov595c3812019-10-01 11:28:49 -0700156 eth.SetArgs({ path_to_linker, path_to_linker, nullptr });
157 eth.Run([&]() { execve(path_to_linker, eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Ryan Prichard8f639a42018-10-01 23:10:05 -0700158#endif
159}
160
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700161TEST(dl, preinit_system_calls) {
162#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800163 SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
Colin Crossbadcb382021-09-24 17:49:58 -0700164 std::string helper = GetTestlibRoot() + "/preinit_syscall_test_helper";
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700165 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
166 ExecTestHelper eth;
167 eth.SetArgs({ helper.c_str(), nullptr });
168 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
169#endif
170}
171
Ryan Prichard5a664902018-11-22 02:14:14 -0800172TEST(dl, preinit_getauxval) {
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700173#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800174 SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
Colin Crossbadcb382021-09-24 17:49:58 -0700175 std::string helper = GetTestlibRoot() + "/preinit_getauxval_test_helper";
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700176 chmod(helper.c_str(), 0755); // TODO: "x" lost in CTS, b/34945607
177 ExecTestHelper eth;
178 eth.SetArgs({ helper.c_str(), nullptr });
179 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Christopher Ferris01db9bd2018-11-07 14:39:43 -0800180#else
181 // Force a failure when not compiled for bionic so the test is considered a pass.
182 ASSERT_TRUE(false);
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700183#endif
184}
185
Jiyong Park02586a22017-05-20 01:01:24 +0900186
187TEST(dl, exec_without_ld_preload) {
188#if defined(__BIONIC__)
Colin Crossbadcb382021-09-24 17:49:58 -0700189 std::string helper = GetTestlibRoot() + "/ld_preload_test_helper";
Jiyong Park02586a22017-05-20 01:01:24 +0900190 chmod(helper.c_str(), 0755);
191 ExecTestHelper eth;
192 eth.SetArgs({ helper.c_str(), nullptr });
193 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
194#endif
195}
196
197TEST(dl, exec_with_ld_preload) {
198#if defined(__BIONIC__)
Colin Crossbadcb382021-09-24 17:49:58 -0700199 std::string helper = GetTestlibRoot() + "/ld_preload_test_helper";
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700200 std::string env = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_preload_test_helper_lib2.so";
Jiyong Park02586a22017-05-20 01:01:24 +0900201 chmod(helper.c_str(), 0755);
202 ExecTestHelper eth;
203 eth.SetArgs({ helper.c_str(), nullptr });
204 eth.SetEnv({ env.c_str(), nullptr });
205 // ld_preload_test_helper calls get_value_from_lib() and returns the value.
206 // The symbol is defined by two libs: ld_preload_test_helper_lib.so and
207 // ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED
208 // via this execution. The main executable is linked to the LD_PRELOADED lib
209 // and the value given from the lib is returned.
210 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
211#endif
212}
213
214
215// ld_config_test_helper must fail because it is depending on a lib which is not
216// in the search path
217//
218// Call sequence is...
219// _helper -- (get_value_from_lib()) -->
220// _lib1.so -- (get_value_from_another_lib()) -->
221// _lib2.so (returns 12345)
222// The two libs are in ns2/ subdir.
223TEST(dl, exec_without_ld_config_file) {
224#if defined(__BIONIC__)
Colin Crossbadcb382021-09-24 17:49:58 -0700225 std::string error_message = "CANNOT LINK EXECUTABLE \"" + GetTestlibRoot() +
226 "/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" "
227 "not found: needed by main executable\n";
228 std::string helper = GetTestlibRoot() + "/ld_config_test_helper";
Jiyong Park02586a22017-05-20 01:01:24 +0900229 chmod(helper.c_str(), 0755);
230 ExecTestHelper eth;
231 eth.SetArgs({ helper.c_str(), nullptr });
dimitry04f7a792017-09-29 11:52:17 +0200232 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900233#endif
234}
235
236#if defined(__BIONIC__)
dimitry1280cf52018-05-09 14:37:47 +0200237extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t);
Ryan Prichard0044cd12018-04-03 20:03:12 -0700238static void create_ld_config_file(const char* config_file) {
dimitry1280cf52018-05-09 14:37:47 +0200239 char default_search_paths[PATH_MAX];
240 android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths));
241
Ryan Prichard0044cd12018-04-03 20:03:12 -0700242 std::ofstream fout(config_file, std::ios::out);
Colin Crossbadcb382021-09-24 17:49:58 -0700243 fout << "dir.test = " << GetTestlibRoot() << "/" << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900244 << "[test]" << std::endl
245 << "additional.namespaces = ns2" << std::endl
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700246 << "namespace.default.search.paths = " << GetTestlibRoot() << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900247 << "namespace.default.links = ns2" << std::endl
Colin Crossbadcb382021-09-24 17:49:58 -0700248 << "namespace.default.link.ns2.shared_libs = "
249 "libc.so:libm.so:libdl.so:ld_config_test_helper_lib1.so"
250 << std::endl
251 << "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestlibRoot()
252 << "/ns2" << std::endl;
Jiyong Park02586a22017-05-20 01:01:24 +0900253 fout.close();
254}
255#endif
256
Jiyong Park41704cd2017-09-19 09:48:07 +0900257#if defined(__BIONIC__)
Ryan Prichard546723b2021-06-04 17:27:39 -0700258// This test can't rely on ro.debuggable, because it might have been forced on
259// in a user build ("Force Debuggable"). In that configuration, ro.debuggable is
260// true, but Bionic's LD_CONFIG_FILE testing support is still disabled.
261static bool is_user_build() {
262 return android::base::GetProperty("ro.build.type", "user") == std::string("user");
Jiyong Park41704cd2017-09-19 09:48:07 +0900263}
264#endif
Jiyong Park02586a22017-05-20 01:01:24 +0900265
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800266// lib1.so and lib2.so are now searchable by having another namespace 'ns2'
Jiyong Park02586a22017-05-20 01:01:24 +0900267// whose search paths include the 'ns2/' subdir.
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800268//
269// lib1.so is linked with DF_1_GLOBAL, so both it and the executable are added
270// to every namespace.
271//
272// namespace configuration ('*' indicates primary ns)
273// - default: exe[*], lib1.so
274// - ns2: exe, lib1.so[*], lib2.so[*]
275//
Jiyong Park02586a22017-05-20 01:01:24 +0900276TEST(dl, exec_with_ld_config_file) {
277#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800278 SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
Ryan Prichard546723b2021-06-04 17:27:39 -0700279 if (is_user_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800280 GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
Jiyong Park41704cd2017-09-19 09:48:07 +0900281 }
Colin Crossbadcb382021-09-24 17:49:58 -0700282 std::string helper = GetTestlibRoot() + "/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700283 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800284 create_ld_config_file(config_file.path);
285 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900286 chmod(helper.c_str(), 0755);
287 ExecTestHelper eth;
288 eth.SetArgs({ helper.c_str(), nullptr });
289 eth.SetEnv({ env.c_str(), nullptr });
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800290 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
291 "foo lib1\n"
292 "lib1_call_funcs\n"
293 "foo lib1\n"
294 "bar lib2\n");
Jiyong Park02586a22017-05-20 01:01:24 +0900295#endif
296}
297
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800298// lib3.so has same foo and bar symbols as lib2.so. lib3.so is LD_PRELOADed.
299// This test ensures that LD_PRELOADed libs are available to all namespaces.
300//
301// namespace configuration ('*' indicates primary ns)
302// - default: exe[*], lib3.so[*], lib1.so
303// - ns2: exe, lib3.so, lib1.so[*], lib2.so[*]
304//
305// Ensure that, in both namespaces, a call to foo calls the lib3.so symbol,
306// which then calls the lib1.so symbol using RTLD_NEXT. Ensure that RTLD_NEXT
307// finds nothing when called from lib1.so.
308//
309// For the bar symbol, lib3.so's primary namespace is the default namespace, but
310// lib2.so is not in the default namespace, so using RTLD_NEXT from lib3.so
311// doesn't find the symbol in lib2.so.
Jiyong Park02586a22017-05-20 01:01:24 +0900312TEST(dl, exec_with_ld_config_file_with_ld_preload) {
313#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800314 SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
Ryan Prichard546723b2021-06-04 17:27:39 -0700315 if (is_user_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800316 GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
Jiyong Park41704cd2017-09-19 09:48:07 +0900317 }
Colin Crossbadcb382021-09-24 17:49:58 -0700318 std::string helper = GetTestlibRoot() + "/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700319 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800320 create_ld_config_file(config_file.path);
321 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Christopher Ferris6d2c0bd2018-08-21 18:13:10 -0700322 std::string env2 = std::string("LD_PRELOAD=") + GetTestlibRoot() + "/ld_config_test_helper_lib3.so";
Jiyong Park02586a22017-05-20 01:01:24 +0900323 chmod(helper.c_str(), 0755);
324 ExecTestHelper eth;
325 eth.SetArgs({ helper.c_str(), nullptr });
326 eth.SetEnv({ env.c_str(), env2.c_str(), nullptr });
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800327 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
328 "foo lib3\n"
329 "foo lib1\n"
330 "lib1_call_funcs\n"
331 "foo lib3\n"
332 "foo lib1\n"
333 "bar lib3\n"
334 "lib3_call_funcs\n"
335 "foo lib3\n"
336 "foo lib1\n"
337 "bar lib3\n");
Jiyong Park02586a22017-05-20 01:01:24 +0900338#endif
339}
340
Jiyong Park02586a22017-05-20 01:01:24 +0900341// ensures that LD_CONFIG_FILE env var does not work for production builds.
342// The test input is the same as exec_with_ld_config_file, but it must fail in
343// this case.
344TEST(dl, disable_ld_config_file) {
345#if defined(__BIONIC__)
346 if (getuid() == 0) {
347 // when executed from the shell (e.g. not as part of CTS), skip the test.
348 // This test is only for CTS.
Ryan Prichard55f9a242019-12-10 14:45:20 -0800349 GTEST_SKIP() << "test is not supported with root uid";
Jiyong Park02586a22017-05-20 01:01:24 +0900350 }
Ryan Prichard546723b2021-06-04 17:27:39 -0700351 if (!is_user_build()) {
352 GTEST_SKIP() << "test requires user build";
Jiyong Park4945d8f2017-08-30 11:30:53 +0900353 }
354
Colin Crossbadcb382021-09-24 17:49:58 -0700355 std::string error_message =
356 std::string("CANNOT LINK EXECUTABLE ") + "\"" + GetTestlibRoot() +
357 "/ld_config_test_helper\": " +
Ryan Prichard0f672142019-12-10 14:50:11 -0800358 "library \"ld_config_test_helper_lib1.so\" not found: needed by main executable\n";
Colin Crossbadcb382021-09-24 17:49:58 -0700359 std::string helper = GetTestlibRoot() + "/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700360 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800361 create_ld_config_file(config_file.path);
362 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900363 chmod(helper.c_str(), 0755);
364 ExecTestHelper eth;
365 eth.SetArgs({ helper.c_str(), nullptr });
366 eth.SetEnv({ env.c_str(), nullptr });
Jiyong Park98283862017-11-28 13:37:03 +0900367 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900368#endif
369}
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800370
371static void RelocationsTest(const char* lib, const char* expectation) {
372#if defined(__BIONIC__)
373 // Does readelf think the .so file looks right?
374 const std::string path = GetTestlibRoot() + "/" + lib;
375 ExecTestHelper eth;
376 eth.SetArgs({ "readelf", "-SW", path.c_str(), nullptr });
377 eth.Run([&]() { execvpe("readelf", eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Elliott Hughesec580d32021-04-12 15:55:29 -0700378
379 ASSERT_TRUE(std::regex_search(eth.GetOutput(), std::regex(expectation))) << eth.GetOutput();
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800380
381 // Can we load it?
382 void* handle = dlopen(lib, RTLD_NOW);
383 ASSERT_TRUE(handle != nullptr) << dlerror();
384#else
385 UNUSED(lib);
386 UNUSED(expectation);
387 GTEST_SKIP() << "test is not supported on glibc";
388#endif
389}
390
391TEST(dl, relocations_RELR) {
Elliott Hughesec580d32021-04-12 15:55:29 -0700392 RelocationsTest("librelocations-RELR.so", "\\.relr\\.dyn * RELR");
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800393}
394
395TEST(dl, relocations_ANDROID_RELR) {
Elliott Hughesec580d32021-04-12 15:55:29 -0700396 RelocationsTest("librelocations-ANDROID_RELR.so", "\\.relr\\.dyn * ANDROID_RELR");
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800397}
398
399TEST(dl, relocations_ANDROID_REL) {
400 RelocationsTest("librelocations-ANDROID_REL.so",
401#if __LP64__
Elliott Hughesec580d32021-04-12 15:55:29 -0700402 "\\.rela\\.dyn * ANDROID_RELA"
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800403#else
Elliott Hughesec580d32021-04-12 15:55:29 -0700404 "\\.rel\\.dyn * ANDROID_REL"
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800405#endif
Elliott Hughesec580d32021-04-12 15:55:29 -0700406 );
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800407}
408
409TEST(dl, relocations_fat) {
410 RelocationsTest("librelocations-fat.so",
411#if __LP64__
Elliott Hughesec580d32021-04-12 15:55:29 -0700412 "\\.rela\\.dyn * RELA"
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800413#else
Elliott Hughesec580d32021-04-12 15:55:29 -0700414 "\\.rel\\.dyn * REL"
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800415#endif
Elliott Hughesec580d32021-04-12 15:55:29 -0700416 );
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800417}