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/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", false);
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     return FileSpec(module_path.c_str(), true);
44 
45   SmallVector<StringRef, 4> ld_paths;
46 
47   if (const char *ld_library_path = ::getenv("LD_LIBRARY_PATH"))
48     StringRef(ld_library_path)
49         .split(ld_paths, StringRef(ld_library_path_separator), -1, false);
50 
51   const char *const *default_lib_path = nullptr;
52   switch (arch.GetAddressByteSize()) {
53   case 4:
54     default_lib_path = default_lib32_path;
55     break;
56   case 8:
57     default_lib_path = default_lib64_path;
58     break;
59   default:
60     assert(false && "Unknown address byte size");
61     return FileSpec();
62   }
63 
64   for (const char *const *it = default_lib_path; *it; ++it)
65     ld_paths.push_back(StringRef(*it));
66 
67   for (const StringRef &path : ld_paths) {
68     FileSpec file_candidate(path.str().c_str(), true);
69     file_candidate.AppendPathComponent(module_path.c_str());
70 
71     if (file_candidate.Exists())
72       return file_candidate;
73   }
74 
75   return FileSpec();
76 }
77 
78 bool HostInfoAndroid::ComputeTempFileBaseDirectory(FileSpec &file_spec) {
79   bool success = HostInfoLinux::ComputeTempFileBaseDirectory(file_spec);
80 
81   // On Android, there is no path which is guaranteed to be writable. If the
82   // user has not
83   // provided a path via an environment variable, the generic algorithm will
84   // deduce /tmp, which
85   // is plain wrong. In that case we have an invalid directory, we substitute
86   // the path with
87   // /data/local/tmp, which is correct at least in some cases (i.e., when
88   // running as shell user).
89   if (!success || !file_spec.Exists())
90     file_spec = FileSpec("/data/local/tmp", false);
91 
92   return file_spec.Exists();
93 }
94