blob: fd23ced7f72232332d7fd470424e8786dccc597d [file] [log] [blame]
Ian Rogers7655f292013-07-29 11:07:13 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Ian Rogers7655f292013-07-29 11:07:13 -070018#include "base/logging.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "entrypoints/entrypoint_utils.h"
Ian Rogers848871b2013-08-05 10:56:33 -070020#include "mirror/object-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070021#include "scoped_thread_state_change-inl.h"
Ian Rogers7655f292013-07-29 11:07:13 -070022#include "thread.h"
23
24namespace art {
25
26// Used by the JNI dlsym stub to find the native method to invoke if none is registered.
Ian Rogers04c31d22014-07-07 21:44:06 -070027#if defined(__arm__) || defined(__aarch64__)
Ian Rogers848871b2013-08-05 10:56:33 -070028extern "C" void* artFindNativeMethod() {
29 Thread* self = Thread::Current();
Ian Rogers04c31d22014-07-07 21:44:06 -070030#else
31extern "C" void* artFindNativeMethod(Thread* self) {
32 DCHECK_EQ(self, Thread::Current());
33#endif
Ian Rogers7655f292013-07-29 11:07:13 -070034 Locks::mutator_lock_->AssertNotHeld(self); // We come here as Native.
Ian Rogers7655f292013-07-29 11:07:13 -070035 ScopedObjectAccess soa(self);
36
Mathieu Chartiere401d142015-04-22 13:56:20 -070037 ArtMethod* method = self->GetCurrentMethod(nullptr);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070038 DCHECK(method != nullptr);
Ian Rogers7655f292013-07-29 11:07:13 -070039
Mathieu Chartier2cebb242015-04-21 16:50:40 -070040 // Lookup symbol address for method, on failure we'll return null with an exception set,
Ian Rogers848871b2013-08-05 10:56:33 -070041 // otherwise we return the address of the method we found.
Ian Rogers7655f292013-07-29 11:07:13 -070042 void* native_code = soa.Vm()->FindCodeForNativeMethod(method);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070043 if (native_code == nullptr) {
Ian Rogers7655f292013-07-29 11:07:13 -070044 DCHECK(self->IsExceptionPending());
Mathieu Chartier2cebb242015-04-21 16:50:40 -070045 return nullptr;
Ian Rogers7655f292013-07-29 11:07:13 -070046 } else {
47 // Register so that future calls don't come here
Ian Rogers6f3dbba2014-10-14 17:41:57 -070048 method->RegisterNative(native_code, false);
Ian Rogers7655f292013-07-29 11:07:13 -070049 return native_code;
50 }
51}
52
Ian Rogers7655f292013-07-29 11:07:13 -070053} // namespace art