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