blob: e986af2327e2ee73d23f263181d42aa89e1de2ad [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();
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000121 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();
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000137 std::string helper = GetTestLibRoot() +
Ryan Prichard8f639a42018-10-01 23:10:05 -0700138 "/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";
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000164 std::string helper = GetTestLibRoot() + "/preinit_syscall_test_helper";
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700165 ExecTestHelper eth;
166 eth.SetArgs({ helper.c_str(), nullptr });
167 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
168#endif
169}
170
Ryan Prichard5a664902018-11-22 02:14:14 -0800171TEST(dl, preinit_getauxval) {
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700172#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800173 SKIP_WITH_HWASAN << "hwasan not initialized in preinit_array, b/124007027";
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000174 std::string helper = GetTestLibRoot() + "/preinit_getauxval_test_helper";
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700175 ExecTestHelper eth;
176 eth.SetArgs({ helper.c_str(), nullptr });
177 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Christopher Ferris01db9bd2018-11-07 14:39:43 -0800178#else
179 // Force a failure when not compiled for bionic so the test is considered a pass.
180 ASSERT_TRUE(false);
Elliott Hugheseb04ed52017-03-29 13:48:02 -0700181#endif
182}
183
Jiyong Park02586a22017-05-20 01:01:24 +0900184
185TEST(dl, exec_without_ld_preload) {
186#if defined(__BIONIC__)
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000187 std::string helper = GetTestLibRoot() + "/ld_preload_test_helper";
Jiyong Park02586a22017-05-20 01:01:24 +0900188 ExecTestHelper eth;
189 eth.SetArgs({ helper.c_str(), nullptr });
190 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "12345");
191#endif
192}
193
194TEST(dl, exec_with_ld_preload) {
195#if defined(__BIONIC__)
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000196 std::string helper = GetTestLibRoot() + "/ld_preload_test_helper";
197 std::string env = std::string("LD_PRELOAD=") + GetTestLibRoot() + "/ld_preload_test_helper_lib2.so";
Jiyong Park02586a22017-05-20 01:01:24 +0900198 ExecTestHelper eth;
199 eth.SetArgs({ helper.c_str(), nullptr });
200 eth.SetEnv({ env.c_str(), nullptr });
201 // ld_preload_test_helper calls get_value_from_lib() and returns the value.
202 // The symbol is defined by two libs: ld_preload_test_helper_lib.so and
203 // ld_preloaded_lib.so. The former is DT_NEEDED and the latter is LD_PRELOADED
204 // via this execution. The main executable is linked to the LD_PRELOADED lib
205 // and the value given from the lib is returned.
206 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0, "54321");
207#endif
208}
209
210
211// ld_config_test_helper must fail because it is depending on a lib which is not
212// in the search path
213//
214// Call sequence is...
215// _helper -- (get_value_from_lib()) -->
216// _lib1.so -- (get_value_from_another_lib()) -->
217// _lib2.so (returns 12345)
218// The two libs are in ns2/ subdir.
219TEST(dl, exec_without_ld_config_file) {
220#if defined(__BIONIC__)
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000221 std::string error_message = "CANNOT LINK EXECUTABLE \"" + GetTestLibRoot() +
Colin Crossbadcb382021-09-24 17:49:58 -0700222 "/ld_config_test_helper\": library \"ld_config_test_helper_lib1.so\" "
223 "not found: needed by main executable\n";
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000224 std::string helper = GetTestLibRoot() + "/ld_config_test_helper";
Jiyong Park02586a22017-05-20 01:01:24 +0900225 ExecTestHelper eth;
226 eth.SetArgs({ helper.c_str(), nullptr });
dimitry04f7a792017-09-29 11:52:17 +0200227 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900228#endif
229}
230
231#if defined(__BIONIC__)
dimitry1280cf52018-05-09 14:37:47 +0200232extern "C" void android_get_LD_LIBRARY_PATH(char*, size_t);
Ryan Prichard0044cd12018-04-03 20:03:12 -0700233static void create_ld_config_file(const char* config_file) {
dimitry1280cf52018-05-09 14:37:47 +0200234 char default_search_paths[PATH_MAX];
235 android_get_LD_LIBRARY_PATH(default_search_paths, sizeof(default_search_paths));
236
Ryan Prichard0044cd12018-04-03 20:03:12 -0700237 std::ofstream fout(config_file, std::ios::out);
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000238 fout << "dir.test = " << GetTestLibRoot() << "/" << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900239 << "[test]" << std::endl
240 << "additional.namespaces = ns2" << std::endl
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000241 << "namespace.default.search.paths = " << GetTestLibRoot() << std::endl
Jiyong Park02586a22017-05-20 01:01:24 +0900242 << "namespace.default.links = ns2" << std::endl
Colin Crossbadcb382021-09-24 17:49:58 -0700243 << "namespace.default.link.ns2.shared_libs = "
244 "libc.so:libm.so:libdl.so:ld_config_test_helper_lib1.so"
245 << std::endl
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000246 << "namespace.ns2.search.paths = " << default_search_paths << ":" << GetTestLibRoot()
Colin Crossbadcb382021-09-24 17:49:58 -0700247 << "/ns2" << std::endl;
Jiyong Park02586a22017-05-20 01:01:24 +0900248 fout.close();
249}
250#endif
251
Jiyong Park41704cd2017-09-19 09:48:07 +0900252#if defined(__BIONIC__)
Ryan Prichard546723b2021-06-04 17:27:39 -0700253// This test can't rely on ro.debuggable, because it might have been forced on
254// in a user build ("Force Debuggable"). In that configuration, ro.debuggable is
255// true, but Bionic's LD_CONFIG_FILE testing support is still disabled.
256static bool is_user_build() {
257 return android::base::GetProperty("ro.build.type", "user") == std::string("user");
Jiyong Park41704cd2017-09-19 09:48:07 +0900258}
259#endif
Jiyong Park02586a22017-05-20 01:01:24 +0900260
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800261// lib1.so and lib2.so are now searchable by having another namespace 'ns2'
Jiyong Park02586a22017-05-20 01:01:24 +0900262// whose search paths include the 'ns2/' subdir.
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800263//
264// lib1.so is linked with DF_1_GLOBAL, so both it and the executable are added
265// to every namespace.
266//
267// namespace configuration ('*' indicates primary ns)
268// - default: exe[*], lib1.so
269// - ns2: exe, lib1.so[*], lib2.so[*]
270//
Jiyong Park02586a22017-05-20 01:01:24 +0900271TEST(dl, exec_with_ld_config_file) {
272#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800273 SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
Ryan Prichard546723b2021-06-04 17:27:39 -0700274 if (is_user_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800275 GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
Jiyong Park41704cd2017-09-19 09:48:07 +0900276 }
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000277 std::string helper = GetTestLibRoot() + "/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700278 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800279 create_ld_config_file(config_file.path);
280 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900281 ExecTestHelper eth;
282 eth.SetArgs({ helper.c_str(), nullptr });
283 eth.SetEnv({ env.c_str(), nullptr });
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800284 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
285 "foo lib1\n"
286 "lib1_call_funcs\n"
287 "foo lib1\n"
288 "bar lib2\n");
Jiyong Park02586a22017-05-20 01:01:24 +0900289#endif
290}
291
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800292// lib3.so has same foo and bar symbols as lib2.so. lib3.so is LD_PRELOADed.
293// This test ensures that LD_PRELOADed libs are available to all namespaces.
294//
295// namespace configuration ('*' indicates primary ns)
296// - default: exe[*], lib3.so[*], lib1.so
297// - ns2: exe, lib3.so, lib1.so[*], lib2.so[*]
298//
299// Ensure that, in both namespaces, a call to foo calls the lib3.so symbol,
300// which then calls the lib1.so symbol using RTLD_NEXT. Ensure that RTLD_NEXT
301// finds nothing when called from lib1.so.
302//
303// For the bar symbol, lib3.so's primary namespace is the default namespace, but
304// lib2.so is not in the default namespace, so using RTLD_NEXT from lib3.so
305// doesn't find the symbol in lib2.so.
Jiyong Park02586a22017-05-20 01:01:24 +0900306TEST(dl, exec_with_ld_config_file_with_ld_preload) {
307#if defined(__BIONIC__)
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800308 SKIP_WITH_HWASAN << "libclang_rt.hwasan is not found with custom ld config";
Ryan Prichard546723b2021-06-04 17:27:39 -0700309 if (is_user_build()) {
Ryan Prichard55f9a242019-12-10 14:45:20 -0800310 GTEST_SKIP() << "LD_CONFIG_FILE is not supported on user build";
Jiyong Park41704cd2017-09-19 09:48:07 +0900311 }
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000312 std::string helper = GetTestLibRoot() + "/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700313 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800314 create_ld_config_file(config_file.path);
315 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000316 std::string env2 = std::string("LD_PRELOAD=") + GetTestLibRoot() + "/ld_config_test_helper_lib3.so";
Jiyong Park02586a22017-05-20 01:01:24 +0900317 ExecTestHelper eth;
318 eth.SetArgs({ helper.c_str(), nullptr });
319 eth.SetEnv({ env.c_str(), env2.c_str(), nullptr });
Ryan Prichard058eb8f2020-12-17 22:59:04 -0800320 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, 0,
321 "foo lib3\n"
322 "foo lib1\n"
323 "lib1_call_funcs\n"
324 "foo lib3\n"
325 "foo lib1\n"
326 "bar lib3\n"
327 "lib3_call_funcs\n"
328 "foo lib3\n"
329 "foo lib1\n"
330 "bar lib3\n");
Jiyong Park02586a22017-05-20 01:01:24 +0900331#endif
332}
333
Jiyong Park02586a22017-05-20 01:01:24 +0900334// ensures that LD_CONFIG_FILE env var does not work for production builds.
335// The test input is the same as exec_with_ld_config_file, but it must fail in
336// this case.
337TEST(dl, disable_ld_config_file) {
338#if defined(__BIONIC__)
339 if (getuid() == 0) {
340 // when executed from the shell (e.g. not as part of CTS), skip the test.
341 // This test is only for CTS.
Ryan Prichard55f9a242019-12-10 14:45:20 -0800342 GTEST_SKIP() << "test is not supported with root uid";
Jiyong Park02586a22017-05-20 01:01:24 +0900343 }
Ryan Prichard546723b2021-06-04 17:27:39 -0700344 if (!is_user_build()) {
345 GTEST_SKIP() << "test requires user build";
Jiyong Park4945d8f2017-08-30 11:30:53 +0900346 }
347
Colin Crossbadcb382021-09-24 17:49:58 -0700348 std::string error_message =
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000349 std::string("CANNOT LINK EXECUTABLE ") + "\"" + GetTestLibRoot() +
Colin Crossbadcb382021-09-24 17:49:58 -0700350 "/ld_config_test_helper\": " +
Ryan Prichard0f672142019-12-10 14:50:11 -0800351 "library \"ld_config_test_helper_lib1.so\" not found: needed by main executable\n";
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000352 std::string helper = GetTestLibRoot() + "/ld_config_test_helper";
Ryan Prichard0044cd12018-04-03 20:03:12 -0700353 TemporaryFile config_file;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800354 create_ld_config_file(config_file.path);
355 std::string env = std::string("LD_CONFIG_FILE=") + config_file.path;
Jiyong Park02586a22017-05-20 01:01:24 +0900356 ExecTestHelper eth;
357 eth.SetArgs({ helper.c_str(), nullptr });
358 eth.SetEnv({ env.c_str(), nullptr });
Jiyong Park98283862017-11-28 13:37:03 +0900359 eth.Run([&]() { execve(helper.c_str(), eth.GetArgs(), eth.GetEnv()); }, EXIT_FAILURE, error_message.c_str());
Jiyong Park02586a22017-05-20 01:01:24 +0900360#endif
361}
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800362
363static void RelocationsTest(const char* lib, const char* expectation) {
364#if defined(__BIONIC__)
365 // Does readelf think the .so file looks right?
Elliott Hughes8d8138a2024-03-12 22:37:13 +0000366 const std::string path = GetTestLibRoot() + "/" + lib;
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800367 ExecTestHelper eth;
368 eth.SetArgs({ "readelf", "-SW", path.c_str(), nullptr });
369 eth.Run([&]() { execvpe("readelf", eth.GetArgs(), eth.GetEnv()); }, 0, nullptr);
Elliott Hughesec580d32021-04-12 15:55:29 -0700370
371 ASSERT_TRUE(std::regex_search(eth.GetOutput(), std::regex(expectation))) << eth.GetOutput();
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800372
373 // Can we load it?
374 void* handle = dlopen(lib, RTLD_NOW);
375 ASSERT_TRUE(handle != nullptr) << dlerror();
376#else
377 UNUSED(lib);
378 UNUSED(expectation);
379 GTEST_SKIP() << "test is not supported on glibc";
380#endif
381}
382
383TEST(dl, relocations_RELR) {
Elliott Hughesec580d32021-04-12 15:55:29 -0700384 RelocationsTest("librelocations-RELR.so", "\\.relr\\.dyn * RELR");
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800385}
386
387TEST(dl, relocations_ANDROID_RELR) {
Elliott Hughesec580d32021-04-12 15:55:29 -0700388 RelocationsTest("librelocations-ANDROID_RELR.so", "\\.relr\\.dyn * ANDROID_RELR");
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800389}
390
391TEST(dl, relocations_ANDROID_REL) {
392 RelocationsTest("librelocations-ANDROID_REL.so",
393#if __LP64__
Elliott Hughesec580d32021-04-12 15:55:29 -0700394 "\\.rela\\.dyn * ANDROID_RELA"
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800395#else
Elliott Hughesec580d32021-04-12 15:55:29 -0700396 "\\.rel\\.dyn * ANDROID_REL"
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800397#endif
Elliott Hughesec580d32021-04-12 15:55:29 -0700398 );
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800399}
400
401TEST(dl, relocations_fat) {
402 RelocationsTest("librelocations-fat.so",
403#if __LP64__
Elliott Hughesec580d32021-04-12 15:55:29 -0700404 "\\.rela\\.dyn * RELA"
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800405#else
Elliott Hughesec580d32021-04-12 15:55:29 -0700406 "\\.rel\\.dyn * REL"
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800407#endif
Elliott Hughesec580d32021-04-12 15:55:29 -0700408 );
Elliott Hughes6dd1f582020-01-28 12:18:35 -0800409}