1/*
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
17#include "base/logging.h"
18#include "entrypoints/entrypoint_utils.h"
19#include "mirror/art_method-inl.h"
20#include "mirror/object-inl.h"
21#include "scoped_thread_state_change.h"
22#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.
27#if defined(__arm__) || defined(__aarch64__)
28extern "C" void* artFindNativeMethod() {
29  Thread* self = Thread::Current();
30#else
31extern "C" void* artFindNativeMethod(Thread* self) {
32  DCHECK_EQ(self, Thread::Current());
33#endif
34  Locks::mutator_lock_->AssertNotHeld(self);  // We come here as Native.
35  ScopedObjectAccess soa(self);
36
37  mirror::ArtMethod* method = self->GetCurrentMethod(NULL);
38  DCHECK(method != NULL);
39
40  // Lookup symbol address for method, on failure we'll return NULL with an exception set,
41  // otherwise we return the address of the method we found.
42  void* native_code = soa.Vm()->FindCodeForNativeMethod(method);
43  if (native_code == NULL) {
44    DCHECK(self->IsExceptionPending());
45    return NULL;
46  } else {
47    // Register so that future calls don't come here
48    method->RegisterNative(self, native_code, false);
49    return native_code;
50  }
51}
52
53}  // namespace art
54