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 <lib/fdio/io.h>
9 #include <lib/zx/vmo.h>
11 #include <zircon/dlfcn.h>
12 #include <zircon/status.h>
13 #include <zircon/syscalls.h>
15 #include "base/base_paths_fuchsia.h"
16 #include "base/files/file.h"
17 #include "base/files/file_path.h"
18 #include "base/fuchsia/fuchsia_logging.h"
19 #include "base/logging.h"
20 #include "base/path_service.h"
21 #include "base/posix/safe_strerror.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/threading/thread_restrictions.h"
28 std::string NativeLibraryLoadError::ToString() const {
32 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
33 const NativeLibraryOptions& options,
34 NativeLibraryLoadError* error) {
35 std::vector<base::FilePath::StringType> components;
36 library_path.GetComponents(&components);
37 if (components.size() != 1u) {
38 NOTREACHED() << "library_path is a path, should be a filename: "
39 << library_path.MaybeAsASCII();
43 FilePath computed_path;
44 base::PathService::Get(DIR_SOURCE_ROOT, &computed_path);
45 computed_path = computed_path.AppendASCII("lib").Append(components[0]);
46 base::File library(computed_path,
47 base::File::FLAG_OPEN | base::File::FLAG_READ);
48 if (!library.IsValid()) {
50 error->message = base::StringPrintf(
52 base::File::ErrorToString(library.error_details()).c_str());
58 zx_status_t status = fdio_get_vmo_clone(library.GetPlatformFile(),
59 vmo.reset_and_get_address());
60 if (status != ZX_OK) {
62 error->message = base::StringPrintf("fdio_get_vmo_clone: %s",
63 zx_status_get_string(status));
68 // VMOs must be marked as exec-capable to be mapped executable in dlopen_vmo,
69 // and fdio_get_vmo_clone shouldn't be marking every VMO it returns
70 // exec-capable. So we should mark it as exec-capable here.
71 // In the fullness of time, this invalid handle should be swapped out for a
72 // ZX_RSRC_KIND_VMEX handle.
73 status = vmo.replace_as_executable(zx::handle(), &vmo);
74 if (status != ZX_OK) {
76 error->message = base::StringPrintf("zx_vmo_replace_as_executable: %s",
77 zx_status_get_string(status));
82 NativeLibrary result = dlopen_vmo(vmo.get(), RTLD_LAZY | RTLD_LOCAL);
86 void UnloadNativeLibrary(NativeLibrary library) {
87 // dlclose() is a no-op on Fuchsia, so do nothing here.
90 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
92 return dlsym(library, name.data());
95 std::string GetNativeLibraryName(StringPiece name) {
96 return base::StringPrintf("lib%s.so", name.as_string().c_str());
99 std::string GetLoadableModuleName(StringPiece name) {
100 return GetNativeLibraryName(name);