blob: 3179a93833c0ad20beafe800d8e24d3a21d127b5 [file] [log] [blame]
evan@chromium.org629d58e2011-04-19 07:06:22 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
evan@chromium.orgda642ae2009-04-21 09:56:07 +09002// 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.org59eef1f2013-02-24 14:40:52 +09009#include "base/files/file_path.h"
evan@chromium.orgda642ae2009-04-21 09:56:07 +090010#include "base/logging.h"
avi@chromium.org17f60622013-06-08 03:37:07 +090011#include "base/strings/utf_string_conversions.h"
brettw@chromium.org5b5f5e02011-01-01 10:01:06 +090012#include "base/threading/thread_restrictions.h"
evan@chromium.orgda642ae2009-04-21 09:56:07 +090013
14namespace base {
15
xhwang@chromium.orgeb11be62014-03-25 10:59:09 +090016std::string NativeLibraryLoadError::ToString() const {
17 return message;
18}
19
evan@chromium.orgda642ae2009-04-21 09:56:07 +090020// static
evan@chromium.org629d58e2011-04-19 07:06:22 +090021NativeLibrary LoadNativeLibrary(const FilePath& library_path,
xhwang@chromium.orgeb11be62014-03-25 10:59:09 +090022 NativeLibraryLoadError* error) {
evan@chromium.org9adf1192010-11-13 06:53:16 +090023 // dlopen() opens the file off disk.
24 base::ThreadRestrictions::AssertIOAllowed();
25
willchan@chromium.org9643dcc2010-04-23 03:07:50 +090026 // 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.org231743a2009-07-29 09:06:21 +090030 void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY);
evan@chromium.org629d58e2011-04-19 07:06:22 +090031 if (!dl && error)
xhwang@chromium.orgeb11be62014-03-25 10:59:09 +090032 error->message = dlerror();
evan@chromium.orgda642ae2009-04-21 09:56:07 +090033
34 return dl;
35}
36
37// static
38void UnloadNativeLibrary(NativeLibrary library) {
39 int ret = dlclose(library);
evan@chromium.orgf6feee02009-07-29 09:15:24 +090040 if (ret < 0) {
brettw@chromium.org5faed3c2011-10-27 06:48:00 +090041 DLOG(ERROR) << "dlclose failed: " << dlerror();
evan@chromium.orgf6feee02009-07-29 09:15:24 +090042 NOTREACHED();
43 }
evan@chromium.orgda642ae2009-04-21 09:56:07 +090044}
45
46// static
47void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090048 const char* name) {
evan@chromium.orgda642ae2009-04-21 09:56:07 +090049 return dlsym(library, name);
50}
51
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090052// static
53string16 GetNativeLibraryName(const string16& name) {
54 return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so");
55}
56
evan@chromium.orgda642ae2009-04-21 09:56:07 +090057} // namespace base