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/strcat.h"
25 #include "base/strings/string_piece.h"
26 #include "base/strings/stringprintf.h"
27 #include "base/strings/utf_string_conversions.h"
28 #include "base/threading/thread_restrictions.h"
32 std::string NativeLibraryLoadError::ToString() const {
36 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
37 const NativeLibraryOptions& options,
38 NativeLibraryLoadError* error) {
39 std::vector<base::FilePath::StringType> components;
40 library_path.GetComponents(&components);
41 if (components.size() != 1u) {
42 NOTREACHED() << "library_path is a path, should be a filename: "
43 << library_path.MaybeAsASCII();
47 FilePath computed_path;
48 base::PathService::Get(DIR_SOURCE_ROOT, &computed_path);
49 computed_path = computed_path.AppendASCII("lib").Append(components[0]);
51 // Use fdio_open_fd (a Fuchsia-specific API) here so we can pass the
52 // appropriate FS rights flags to request executability.
53 // TODO(1018538): Teach base::File about FLAG_EXECUTE on Fuchsia, and then
54 // use it here instead of using fdio_open_fd() directly.
56 zx_status_t status = fdio_open_fd(
57 computed_path.value().c_str(),
58 fuchsia::io::OPEN_RIGHT_READABLE | fuchsia::io::OPEN_RIGHT_EXECUTABLE,
59 base::ScopedFD::Receiver(fd).get());
60 if (status != ZX_OK) {
63 base::StringPrintf("fdio_open_fd: %s", zx_status_get_string(status));
69 status = fdio_get_vmo_exec(fd.get(), vmo.reset_and_get_address());
70 if (status != ZX_OK) {
72 error->message = base::StringPrintf("fdio_get_vmo_exec: %s",
73 zx_status_get_string(status));
78 NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
82 void UnloadNativeLibrary(NativeLibrary library) {
83 // dlclose() is a no-op on Fuchsia, so do nothing here.
86 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
88 return dlsym(library, name.data());
91 std::string GetNativeLibraryName(StringPiece name) {
92 return StrCat({"lib", name, ".so"});
95 std::string GetLoadableModuleName(StringPiece name) {
96 return GetNativeLibraryName(name);