Jiyong Park | 4069961 | 2021-05-24 16:55:06 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 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 <dlfcn.h> |
| 18 | |
| 19 | #include <cstdlib> |
| 20 | #include <iostream> |
| 21 | |
| 22 | int main(int argc, char* argv[]) { |
| 23 | if (argc < 2) { |
| 24 | std::cout << "Usage:\n"; |
| 25 | std::cout << " " << argv[0] << " LIBNAME [ARGS...]\n"; |
| 26 | return EXIT_FAILURE; |
| 27 | } |
| 28 | |
| 29 | const char* libname = argv[1]; |
| 30 | void* handle = dlopen(libname, RTLD_NOW); |
| 31 | if (handle == nullptr) { |
| 32 | std::cerr << "Failed to load " << libname << ": " << dlerror() << "\n"; |
| 33 | return EXIT_FAILURE; |
| 34 | } |
| 35 | |
| 36 | int (*entry)(int argc, char* argv[]) = nullptr; |
| 37 | entry = reinterpret_cast<decltype(entry)>(dlsym(handle, "android_native_main")); |
| 38 | if (entry == nullptr) { |
| 39 | std::cerr << "Failed to find entrypoint `android_native_main`: " << dlerror() << "\n"; |
| 40 | return EXIT_FAILURE; |
| 41 | } |
| 42 | |
| 43 | return entry(argc - 1, argv + 1); |
| 44 | } |