evan@chromium.org | 629d58e | 2011-04-19 07:06:22 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/native_library.h" |
| 6 | |
| 7 | #include <dlfcn.h> |
| 8 | |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 10 | #include "base/logging.h" |
avi@chromium.org | 17f6062 | 2013-06-08 03:37:07 +0900 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
brettw@chromium.org | 5b5f5e0 | 2011-01-01 10:01:06 +0900 | [diff] [blame] | 12 | #include "base/threading/thread_restrictions.h" |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 13 | |
| 14 | namespace base { |
| 15 | |
xhwang@chromium.org | eb11be6 | 2014-03-25 10:59:09 +0900 | [diff] [blame] | 16 | std::string NativeLibraryLoadError::ToString() const { |
| 17 | return message; |
| 18 | } |
| 19 | |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 20 | // static |
evan@chromium.org | 629d58e | 2011-04-19 07:06:22 +0900 | [diff] [blame] | 21 | NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
xhwang@chromium.org | eb11be6 | 2014-03-25 10:59:09 +0900 | [diff] [blame] | 22 | NativeLibraryLoadError* error) { |
evan@chromium.org | 9adf119 | 2010-11-13 06:53:16 +0900 | [diff] [blame] | 23 | // dlopen() opens the file off disk. |
| 24 | base::ThreadRestrictions::AssertIOAllowed(); |
| 25 | |
willchan@chromium.org | 9643dcc | 2010-04-23 03:07:50 +0900 | [diff] [blame] | 26 | // We deliberately do not use RTLD_DEEPBIND. For the history why, please |
| 27 | // refer to the bug tracker. Some useful bug reports to read include: |
| 28 | // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892, |
| 29 | // and http://crbug.com/40794. |
evan@chromium.org | 231743a | 2009-07-29 09:06:21 +0900 | [diff] [blame] | 30 | void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY); |
evan@chromium.org | 629d58e | 2011-04-19 07:06:22 +0900 | [diff] [blame] | 31 | if (!dl && error) |
xhwang@chromium.org | eb11be6 | 2014-03-25 10:59:09 +0900 | [diff] [blame] | 32 | error->message = dlerror(); |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 33 | |
| 34 | return dl; |
| 35 | } |
| 36 | |
| 37 | // static |
| 38 | void UnloadNativeLibrary(NativeLibrary library) { |
| 39 | int ret = dlclose(library); |
evan@chromium.org | f6feee0 | 2009-07-29 09:15:24 +0900 | [diff] [blame] | 40 | if (ret < 0) { |
brettw@chromium.org | 5faed3c | 2011-10-27 06:48:00 +0900 | [diff] [blame] | 41 | DLOG(ERROR) << "dlclose failed: " << dlerror(); |
evan@chromium.org | f6feee0 | 2009-07-29 09:15:24 +0900 | [diff] [blame] | 42 | NOTREACHED(); |
| 43 | } |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // static |
| 47 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 48 | const char* name) { |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 49 | return dlsym(library, name); |
| 50 | } |
| 51 | |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 52 | // static |
| 53 | string16 GetNativeLibraryName(const string16& name) { |
| 54 | return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so"); |
| 55 | } |
| 56 | |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 57 | } // namespace base |