1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/native_library.h"
8 #include <fuchsia/io/cpp/fidl.h>
9 #include <lib/fdio/directory.h>
10 #include <lib/fdio/io.h>
11 #include <lib/zx/vmo.h>
13 #include <zircon/dlfcn.h>
14 #include <zircon/status.h>
15 #include <zircon/syscalls.h>
17 #include "base/base_paths_fuchsia.h"
18 #include "base/files/file.h"
19 #include "base/files/file_path.h"
20 #include "base/fuchsia/fuchsia_logging.h"
21 #include "base/notreached.h"
22 #include "base/path_service.h"
23 #include "base/posix/safe_strerror.h"
24 #include "base/strings/stringprintf.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/threading/thread_restrictions.h"
30 std::string NativeLibraryLoadError::ToString() const {
34 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
35 const NativeLibraryOptions& options,
36 NativeLibraryLoadError* error) {
37 std::vector<base::FilePath::StringType> components;
38 library_path.GetComponents(&components);
39 if (components.size() != 1u) {
40 NOTREACHED() << "library_path is a path, should be a filename: "
41 << library_path.MaybeAsASCII();
45 FilePath computed_path;
46 base::PathService::Get(DIR_SOURCE_ROOT, &computed_path);
47 computed_path = computed_path.AppendASCII("lib").Append(components[0]);
49 // Use fdio_open_fd (a Fuchsia-specific API) here so we can pass the
50 // appropriate FS rights flags to request executability.
51 // TODO(1018538): Teach base::File about FLAG_EXECUTE on Fuchsia, and then
52 // use it here instead of using fdio_open_fd() directly.
54 zx_status_t status = fdio_open_fd(
55 computed_path.value().c_str(),
56 fuchsia::io::OPEN_RIGHT_READABLE | fuchsia::io::OPEN_RIGHT_EXECUTABLE,
57 base::ScopedFD::Receiver(fd).get());
58 if (status != ZX_OK) {
61 base::StringPrintf("fdio_open_fd: %s", zx_status_get_string(status));
67 status = fdio_get_vmo_exec(fd.get(), vmo.reset_and_get_address());
68 if (status != ZX_OK) {
70 error->message = base::StringPrintf("fdio_get_vmo_exec: %s",
71 zx_status_get_string(status));
76 NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
80 void UnloadNativeLibrary(NativeLibrary library) {
81 // dlclose() is a no-op on Fuchsia, so do nothing here.
84 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
86 return dlsym(library, name.data());
89 std::string GetNativeLibraryName(StringPiece name) {
90 return base::StringPrintf("lib%s.so", name.as_string().c_str());
93 std::string GetLoadableModuleName(StringPiece name) {
94 return GetNativeLibraryName(name);