[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [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 | |||||
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame^] | 11 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 12 | #include "base/threading/thread_restrictions.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 13 | |
14 | namespace base { | ||||
15 | |||||
16 | // static | ||||
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 17 | NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
18 | std::string* error) { | ||||
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 19 | // dlopen() opens the file off disk. |
20 | base::ThreadRestrictions::AssertIOAllowed(); | ||||
21 | |||||
[email protected] | 0ba5b7b | 2010-04-22 18:07:50 | [diff] [blame] | 22 | // We deliberately do not use RTLD_DEEPBIND. For the history why, please |
23 | // refer to the bug tracker. Some useful bug reports to read include: | ||||
24 | // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892, | ||||
25 | // and http://crbug.com/40794. | ||||
[email protected] | de8dbce | 2009-07-29 00:06:21 | [diff] [blame] | 26 | void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY); |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 27 | if (!dl && error) |
28 | *error = dlerror(); | ||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 29 | |
30 | return dl; | ||||
31 | } | ||||
32 | |||||
33 | // static | ||||
34 | void UnloadNativeLibrary(NativeLibrary library) { | ||||
35 | int ret = dlclose(library); | ||||
[email protected] | 803ef4ef | 2009-07-29 00:15:24 | [diff] [blame] | 36 | if (ret < 0) { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 37 | DLOG(ERROR) << "dlclose failed: " << dlerror(); |
[email protected] | 803ef4ef | 2009-07-29 00:15:24 | [diff] [blame] | 38 | NOTREACHED(); |
39 | } | ||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 40 | } |
41 | |||||
42 | // static | ||||
43 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | ||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 44 | const char* name) { |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 45 | return dlsym(library, name); |
46 | } | ||||
47 | |||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 48 | // static |
49 | string16 GetNativeLibraryName(const string16& name) { | ||||
50 | return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so"); | ||||
51 | } | ||||
52 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 53 | } // namespace base |