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
19 HostInfoAndroid::ComputeHostArchitectureSupport(ArchSpec &arch_32, ArchSpec &arch_64)
20 {
21     HostInfoLinux::ComputeHostArchitectureSupport(arch_32, arch_64);
22 
23     if (arch_32.IsValid())
24     {
25         arch_32.GetTriple().setEnvironment(llvm::Triple::Android);
26     }
27     if (arch_64.IsValid())
28     {
29         arch_64.GetTriple().setEnvironment(llvm::Triple::Android);
30     }
31 }
32 
33 bool
34 HostInfoAndroid::ComputeSupportExeDirectory(FileSpec &file_spec)
35 {
36     file_spec.GetDirectory() = HostInfoLinux::GetProgramFileSpec().GetDirectory();
37     return (bool)file_spec.GetDirectory();
38 }
39 
40 FileSpec
41 HostInfoAndroid::GetDefaultShell()
42 {
43     return FileSpec("/system/bin/sh", false);
44 }
45 
46 FileSpec
47 HostInfoAndroid::ResolveLibraryPath(const std::string& module_path, const ArchSpec& arch)
48 {
49     static const char* const ld_library_path_separator = ":";
50     static const char* const default_lib32_path[] = {
51         "/vendor/lib",
52         "/system/lib",
53         nullptr
54     };
55     static const char* const default_lib64_path[] = {
56         "/vendor/lib64",
57         "/system/lib64",
58         nullptr
59     };
60 
61     if (module_path.empty() || module_path[0] == '/')
62         return FileSpec(module_path.c_str(), true);
63 
64     SmallVector<StringRef, 4> ld_paths;
65 
66     if (const char* ld_library_path = ::getenv("LD_LIBRARY_PATH"))
67         StringRef(ld_library_path).split(ld_paths, StringRef(ld_library_path_separator), -1, false);
68 
69     const char* const* default_lib_path = nullptr;
70     switch (arch.GetAddressByteSize())
71     {
72         case 4:
73             default_lib_path = default_lib32_path;
74             break;
75         case 8:
76             default_lib_path = default_lib64_path;
77             break;
78         default:
79             assert(false && "Unknown address byte size");
80             return FileSpec();
81     }
82 
83     for(const char* const* it = default_lib_path; *it; ++it)
84         ld_paths.push_back(StringRef(*it));
85 
86     for (const StringRef& path : ld_paths)
87     {
88         FileSpec file_candidate(path.str().c_str(), true);
89         file_candidate.AppendPathComponent(module_path.c_str());
90 
91         if (file_candidate.Exists())
92             return file_candidate;
93     }
94 
95     return FileSpec();
96 }
97