1 // Copyright (c) 2011 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"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/strings/strcat.h"
12 #include "base/strings/string_piece.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/threading/scoped_blocking_call.h"
19 std::string NativeLibraryLoadError::ToString() const {
23 NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
24 const NativeLibraryOptions& options,
25 NativeLibraryLoadError* error) {
26 // dlopen() opens the file off disk.
27 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
29 // We deliberately do not use RTLD_DEEPBIND by default. For the history why,
30 // please refer to the bug tracker. Some useful bug reports to read include:
31 // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892,
32 // and http://crbug.com/40794.
33 int flags = RTLD_LAZY;
34 #if defined(OS_ANDROID) || !defined(RTLD_DEEPBIND)
35 // Certain platforms don't define RTLD_DEEPBIND. Android dlopen() requires
36 // further investigation, as it might vary across versions. Crash here to
37 // warn developers that they're trying to rely on uncertain behavior.
38 CHECK(!options.prefer_own_symbols);
40 if (options.prefer_own_symbols)
41 flags |= RTLD_DEEPBIND;
43 void* dl = dlopen(library_path.value().c_str(), flags);
45 error->message = dlerror();
50 void UnloadNativeLibrary(NativeLibrary library) {
51 int ret = dlclose(library);
53 DLOG(ERROR) << "dlclose failed: " << dlerror();
58 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
60 return dlsym(library, name.data());
63 std::string GetNativeLibraryName(StringPiece name) {
64 DCHECK(IsStringASCII(name));
65 return StrCat({"lib", name, ".so"});
68 std::string GetLoadableModuleName(StringPiece name) {
69 return GetNativeLibraryName(name);