| 1 | //===-- HostInfoAndroid.cpp -----------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Host/android/HostInfoAndroid.h" |
| 10 | #include "lldb/Host/FileSystem.h" |
| 11 | #include "lldb/Host/linux/HostInfoLinux.h" |
| 12 | #include "llvm/ADT/SmallVector.h" |
| 13 | #include "llvm/ADT/StringRef.h" |
| 14 | |
| 15 | using namespace lldb_private; |
| 16 | using namespace llvm; |
| 17 | |
| 18 | void HostInfoAndroid::ComputeHostArchitectureSupport(ArchSpec &arch_32, |
| 19 | ArchSpec &arch_64) { |
| 20 | HostInfoLinux::ComputeHostArchitectureSupport(arch_32, arch_64); |
| 21 | |
| 22 | if (arch_32.IsValid()) { |
| 23 | arch_32.GetTriple().setEnvironment(llvm::Triple::Android); |
| 24 | } |
| 25 | if (arch_64.IsValid()) { |
| 26 | arch_64.GetTriple().setEnvironment(llvm::Triple::Android); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | FileSpec HostInfoAndroid::GetDefaultShell() { |
| 31 | return FileSpec("/system/bin/sh" ); |
| 32 | } |
| 33 | |
| 34 | FileSpec HostInfoAndroid::ResolveLibraryPath(const std::string &module_path, |
| 35 | const ArchSpec &arch) { |
| 36 | static const char *const ld_library_path_separator = ":" ; |
| 37 | static const char *const default_lib32_path[] = {"/vendor/lib" , "/system/lib" , |
| 38 | nullptr}; |
| 39 | static const char *const default_lib64_path[] = {"/vendor/lib64" , |
| 40 | "/system/lib64" , nullptr}; |
| 41 | |
| 42 | if (module_path.empty() || module_path[0] == '/') { |
| 43 | FileSpec file_spec(module_path.c_str()); |
| 44 | FileSystem::Instance().Resolve(file_spec); |
| 45 | return file_spec; |
| 46 | } |
| 47 | |
| 48 | SmallVector<StringRef, 4> ld_paths; |
| 49 | |
| 50 | if (const char *ld_library_path = ::getenv(name: "LD_LIBRARY_PATH" )) |
| 51 | StringRef(ld_library_path) |
| 52 | .split(A&: ld_paths, Separator: StringRef(ld_library_path_separator), MaxSplit: -1, KeepEmpty: false); |
| 53 | |
| 54 | const char *const *default_lib_path = nullptr; |
| 55 | switch (arch.GetAddressByteSize()) { |
| 56 | case 4: |
| 57 | default_lib_path = default_lib32_path; |
| 58 | break; |
| 59 | case 8: |
| 60 | default_lib_path = default_lib64_path; |
| 61 | break; |
| 62 | default: |
| 63 | assert(false && "Unknown address byte size" ); |
| 64 | return FileSpec(); |
| 65 | } |
| 66 | |
| 67 | for (const char *const *it = default_lib_path; *it; ++it) |
| 68 | ld_paths.push_back(Elt: StringRef(*it)); |
| 69 | |
| 70 | for (const StringRef &path : ld_paths) { |
| 71 | FileSpec file_candidate(path.str().c_str()); |
| 72 | FileSystem::Instance().Resolve(file_spec&: file_candidate); |
| 73 | file_candidate.AppendPathComponent(component: module_path.c_str()); |
| 74 | |
| 75 | if (FileSystem::Instance().Exists(file_spec: file_candidate)) |
| 76 | return file_candidate; |
| 77 | } |
| 78 | |
| 79 | return FileSpec(); |
| 80 | } |
| 81 | |
| 82 | bool HostInfoAndroid::ComputeTempFileBaseDirectory(FileSpec &file_spec) { |
| 83 | bool success = HostInfoLinux::ComputeTempFileBaseDirectory(file_spec); |
| 84 | |
| 85 | // On Android, there is no path which is guaranteed to be writable. If the |
| 86 | // user has not provided a path via an environment variable, the generic |
| 87 | // algorithm will deduce /tmp, which is plain wrong. In that case we have an |
| 88 | // invalid directory, we substitute the path with /data/local/tmp, which is |
| 89 | // correct at least in some cases (i.e., when running as shell user). |
| 90 | if (!success || !FileSystem::Instance().Exists(file_spec)) |
| 91 | file_spec = FileSpec("/data/local/tmp" ); |
| 92 | |
| 93 | return FileSystem::Instance().Exists(file_spec); |
| 94 | } |
| 95 | |