1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include "dlfcn_symlink_support.h"
18*8d67ca89SAndroid Build Coastguard Worker
19*8d67ca89SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*8d67ca89SAndroid Build Coastguard Worker
21*8d67ca89SAndroid Build Coastguard Worker #include <dlfcn.h>
22*8d67ca89SAndroid Build Coastguard Worker #include <libgen.h>
23*8d67ca89SAndroid Build Coastguard Worker #include <link.h>
24*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
25*8d67ca89SAndroid Build Coastguard Worker
26*8d67ca89SAndroid Build Coastguard Worker #include <android-base/strings.h>
27*8d67ca89SAndroid Build Coastguard Worker
28*8d67ca89SAndroid Build Coastguard Worker #include <algorithm>
29*8d67ca89SAndroid Build Coastguard Worker #include <string>
30*8d67ca89SAndroid Build Coastguard Worker #include <vector>
31*8d67ca89SAndroid Build Coastguard Worker
32*8d67ca89SAndroid Build Coastguard Worker static const constexpr char* source_file_name = "libdlext_test.so";
33*8d67ca89SAndroid Build Coastguard Worker static const constexpr char* symlink_name_prefix = "libdlext_test_";
34*8d67ca89SAndroid Build Coastguard Worker
dl_callback(struct dl_phdr_info * info,size_t,void * data)35*8d67ca89SAndroid Build Coastguard Worker static int dl_callback(struct dl_phdr_info *info, size_t /* size */, void *data) {
36*8d67ca89SAndroid Build Coastguard Worker // The case when path is not absolute and is equal to source_file_name
37*8d67ca89SAndroid Build Coastguard Worker // is disregarded intentionally since in bionic dlpi_name should always
38*8d67ca89SAndroid Build Coastguard Worker // be realpath to a shared object.
39*8d67ca89SAndroid Build Coastguard Worker const std::string suffix = std::string("/") + source_file_name;
40*8d67ca89SAndroid Build Coastguard Worker
41*8d67ca89SAndroid Build Coastguard Worker // TODO (dimitry): remove this check once fake libdl.so is gone
42*8d67ca89SAndroid Build Coastguard Worker if (info->dlpi_name == nullptr) {
43*8d67ca89SAndroid Build Coastguard Worker // This is linker imposing as libdl.so - skip it
44*8d67ca89SAndroid Build Coastguard Worker return 0;
45*8d67ca89SAndroid Build Coastguard Worker }
46*8d67ca89SAndroid Build Coastguard Worker
47*8d67ca89SAndroid Build Coastguard Worker if (android::base::EndsWith(info->dlpi_name, suffix)) {
48*8d67ca89SAndroid Build Coastguard Worker std::string* path = reinterpret_cast<std::string*>(data);
49*8d67ca89SAndroid Build Coastguard Worker *path = info->dlpi_name;
50*8d67ca89SAndroid Build Coastguard Worker return 1; // found
51*8d67ca89SAndroid Build Coastguard Worker }
52*8d67ca89SAndroid Build Coastguard Worker
53*8d67ca89SAndroid Build Coastguard Worker return 0;
54*8d67ca89SAndroid Build Coastguard Worker }
55*8d67ca89SAndroid Build Coastguard Worker
create_dlfcn_test_symlink(const char * suffix,std::string * result)56*8d67ca89SAndroid Build Coastguard Worker void create_dlfcn_test_symlink(const char* suffix, std::string* result) {
57*8d67ca89SAndroid Build Coastguard Worker void* handle = dlopen(source_file_name, RTLD_NOW);
58*8d67ca89SAndroid Build Coastguard Worker std::string source_file_path;
59*8d67ca89SAndroid Build Coastguard Worker
60*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(handle != nullptr) << dlerror();
61*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(dl_iterate_phdr(dl_callback, &source_file_path) == 1)
62*8d67ca89SAndroid Build Coastguard Worker << "dl_phdr_info for \"" << source_file_name << "\" was not found.";
63*8d67ca89SAndroid Build Coastguard Worker
64*8d67ca89SAndroid Build Coastguard Worker dlclose(handle);
65*8d67ca89SAndroid Build Coastguard Worker std::vector<char> buf;
66*8d67ca89SAndroid Build Coastguard Worker std::copy(source_file_path.begin(), source_file_path.end(), std::back_inserter(buf));
67*8d67ca89SAndroid Build Coastguard Worker buf.push_back('\0');
68*8d67ca89SAndroid Build Coastguard Worker
69*8d67ca89SAndroid Build Coastguard Worker std::string path_dir = dirname(&buf[0]);
70*8d67ca89SAndroid Build Coastguard Worker std::string link_path = path_dir + "/" + symlink_name_prefix + suffix + ".so";
71*8d67ca89SAndroid Build Coastguard Worker
72*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(symlink(source_file_path.c_str(), link_path.c_str()) == 0) << strerror(errno);
73*8d67ca89SAndroid Build Coastguard Worker *result = link_path;
74*8d67ca89SAndroid Build Coastguard Worker }
75*8d67ca89SAndroid Build Coastguard Worker
remove_dlfcn_test_symlink(const std::string & path)76*8d67ca89SAndroid Build Coastguard Worker void remove_dlfcn_test_symlink(const std::string& path) {
77*8d67ca89SAndroid Build Coastguard Worker ASSERT_TRUE(unlink(path.c_str()) == 0) << strerror(errno);
78*8d67ca89SAndroid Build Coastguard Worker }
79