1 /*
   2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 
  26 #include "aot/aotCodeHeap.hpp"
  27 #include "aot/aotLoader.hpp"
  28 #include "classfile/javaAssertions.hpp"
  29 #include "gc/g1/heapRegion.hpp"
  30 #include "gc/shared/gcLocker.hpp"
  31 #include "interpreter/abstractInterpreter.hpp"
  32 #include "jvmci/compilerRuntime.hpp"
  33 #include "jvmci/jvmciRuntime.hpp"
  34 #include "oops/method.hpp"
  35 #include "runtime/os.hpp"
  36 #include "runtime/sharedRuntime.hpp"
  37 #include "runtime/vm_operations.hpp"
  38 
  39 bool AOTLib::_narrow_oop_shift_initialized = false;
  40 int  AOTLib::_narrow_oop_shift = 0;
  41 int  AOTLib::_narrow_klass_shift = 0;
  42 
  43 address AOTLib::load_symbol(const char *name) {
  44   address symbol = (address) os::dll_lookup(_dl_handle, name);
  45   if (symbol == NULL) {
  46     tty->print_cr("Shared file %s error: missing %s", _name, name);
  47     vm_exit(1);
  48   }
  49   return symbol;
  50 }
  51 
  52 Klass* AOTCodeHeap::get_klass_from_got(const char* klass_name, int klass_len, const Method* method) {
  53   AOTKlassData* klass_data = (AOTKlassData*)_lib->load_symbol(klass_name);
  54   Klass* k = (Klass*)_klasses_got[klass_data->_got_index];
  55   if (k == NULL) {
  56     Thread* thread = Thread::current();
  57     k = lookup_klass(klass_name, klass_len, method, thread);
  58     // Note, exceptions are cleared.
  59     if (k == NULL) {
  60       fatal("Shared file %s error: klass %s should be resolved already", _lib->name(), klass_name);
  61       vm_exit(1);
  62     }
  63     // Patch now to avoid extra runtime lookup
  64     _klasses_got[klass_data->_got_index] = k;
  65     if (k->is_instance_klass()) {
  66       InstanceKlass* ik = InstanceKlass::cast(k);
  67       if (ik->is_initialized()) {
  68         _klasses_got[klass_data->_got_index - 1] = ik;
  69       }
  70     }
  71   }
  72   return k;
  73 }
  74 
  75 Klass* AOTCodeHeap::lookup_klass(const char* name, int len, const Method* method, Thread* thread) {
  76   ResourceMark rm(thread);
  77   assert(method != NULL, "incorrect call parameter");
  78   methodHandle caller(thread, (Method*)method);
  79 
  80   // Use class loader of aot method.
  81   Handle loader(thread, caller->method_holder()->class_loader());
  82   Handle protection_domain(thread, caller->method_holder()->protection_domain());
  83 
  84   // Ignore wrapping L and ;
  85   if (name[0] == 'L') {
  86     assert(len > 2, "small name %s", name);
  87     name++;
  88     len -= 2;
  89   }
  90   TempNewSymbol sym = SymbolTable::probe(name, len);
  91   if (sym == NULL) {
  92     log_debug(aot, class, resolve)("Probe failed for AOT class %s", name);
  93     return NULL;
  94   }
  95   Klass* k = SystemDictionary::find_instance_or_array_klass(sym, loader, protection_domain, thread);
  96   assert(!thread->has_pending_exception(), "should not throw");
  97 
  98   if (k != NULL) {
  99     log_info(aot, class, resolve)("%s %s (lookup)", caller->method_holder()->external_name(), k->external_name());
 100   }
 101   return k;
 102 }
 103 
 104 void AOTLib::handle_config_error(const char* format, ...) {
 105   if (PrintAOT) {
 106     va_list ap;
 107     va_start(ap, format);
 108     tty->vprint_cr(format, ap);
 109     va_end(ap);
 110   }
 111   if (UseAOTStrictLoading) {
 112     vm_exit(1);
 113   }
 114   _valid = false;
 115 }
 116 
 117 void AOTLib::verify_flag(bool aot_flag, bool flag, const char* name) {
 118   if (_valid && aot_flag != flag) {
 119     handle_config_error("Shared file %s error: %s has different value '%s' from current '%s'", _name, name , (aot_flag ? "true" : "false"), (flag ? "true" : "false"));
 120   }
 121 }
 122 
 123 void AOTLib::verify_flag(int aot_flag, int flag, const char* name) {
 124   if (_valid && aot_flag != flag) {
 125     handle_config_error("Shared file %s error: %s has different value '%d' from current '%d'", _name, name , aot_flag, flag);
 126   }
 127 }
 128 
 129 void AOTLib::verify_config() {
 130   GrowableArray<AOTLib*>* libraries = AOTLoader::libraries();
 131   for (GrowableArrayIterator<AOTLib*> lib = libraries->begin(); lib != libraries->end(); ++lib) {
 132     if ((*lib)->_config == _config) {
 133       handle_config_error("AOT library %s already loaded.", (*lib)->_name);
 134       return;
 135     }
 136   }
 137 
 138   if (_header->_version != AOTHeader::AOT_SHARED_VERSION) {
 139     handle_config_error("Invalid version of the shared file %s. Expected %d but was %d", _name, _header->_version, AOTHeader::AOT_SHARED_VERSION);
 140     return;
 141   }
 142 
 143   const char* aot_jvm_version = (const char*)_header + _header->_jvm_version_offset + 2;
 144   if (strcmp(aot_jvm_version, VM_Version::jre_release_version()) != 0) {
 145     handle_config_error("JVM version '%s' recorded in the shared file %s does not match current version '%s'", aot_jvm_version, _name, VM_Version::jre_release_version());
 146     return;
 147   }
 148 
 149   // Debug VM has different layout of runtime and metadata structures
 150 #ifdef ASSERT
 151   verify_flag(_config->_debug_VM, true, "Debug VM version");
 152 #else
 153   verify_flag(!(_config->_debug_VM), true, "Product VM version");
 154 #endif
 155   // Check configuration size
 156   verify_flag(_config->_config_size, AOTConfiguration::CONFIG_SIZE, "AOT configuration size");
 157 
 158   // Check flags
 159   verify_flag(_config->_useCompressedOops, UseCompressedOops, "UseCompressedOops");
 160   verify_flag(_config->_useCompressedClassPointers, UseCompressedClassPointers, "UseCompressedClassPointers");
 161   verify_flag(_config->_useG1GC, UseG1GC, "UseG1GC");
 162   verify_flag(_config->_useTLAB, UseTLAB, "UseTLAB");
 163   verify_flag(_config->_useBiasedLocking, UseBiasedLocking, "UseBiasedLocking");
 164   verify_flag(_config->_objectAlignment, ObjectAlignmentInBytes, "ObjectAlignmentInBytes");
 165   verify_flag(_config->_contendedPaddingWidth, ContendedPaddingWidth, "ContendedPaddingWidth");
 166   verify_flag(_config->_fieldsAllocationStyle, FieldsAllocationStyle, "FieldsAllocationStyle");
 167   verify_flag(_config->_compactFields, CompactFields, "CompactFields");
 168   verify_flag(_config->_enableContended, EnableContended, "EnableContended");
 169   verify_flag(_config->_restrictContended, RestrictContended, "RestrictContended");
 170 
 171   if (!TieredCompilation && _config->_tieredAOT) {
 172     handle_config_error("Shared file %s error: Expected to run with tiered compilation on", _name);
 173   }
 174 
 175   // Shifts are static values which initialized by 0 until java heap initialization.
 176   // AOT libs are loaded before heap initialized so shift values are not set.
 177   // It is okay since ObjectAlignmentInBytes flag which defines shifts value is set before AOT libs are loaded.
 178   // Set shifts value based on first AOT library config.
 179   if (UseCompressedOops && _valid) {
 180     if (!_narrow_oop_shift_initialized) {
 181       _narrow_oop_shift = _config->_narrowOopShift;
 182       if (UseCompressedClassPointers) { // It is set only if UseCompressedOops is set
 183         _narrow_klass_shift = _config->_narrowKlassShift;
 184       }
 185       _narrow_oop_shift_initialized = true;
 186     } else {
 187       verify_flag(_config->_narrowOopShift, _narrow_oop_shift, "aot_config->_narrowOopShift");
 188       if (UseCompressedClassPointers) { // It is set only if UseCompressedOops is set
 189         verify_flag(_config->_narrowKlassShift, _narrow_klass_shift, "aot_config->_narrowKlassShift");
 190       }
 191     }
 192   }
 193 }
 194 
 195 AOTLib::~AOTLib() {
 196   free((void*) _name);
 197 }
 198 
 199 AOTCodeHeap::~AOTCodeHeap() {
 200   if (_classes != NULL) {
 201     FREE_C_HEAP_ARRAY(AOTClass, _classes);
 202   }
 203   if (_code_to_aot != NULL) {
 204     FREE_C_HEAP_ARRAY(CodeToAMethod, _code_to_aot);
 205   }
 206 }
 207 
 208 AOTLib::AOTLib(void* handle, const char* name, int dso_id) : _valid(true), _dl_handle(handle), _dso_id(dso_id) {
 209   _name = (const char*) strdup(name);
 210 
 211   // Verify that VM runs with the same parameters as AOT tool.
 212   _config = (AOTConfiguration*) load_symbol("A.config");
 213   _header = (AOTHeader*) load_symbol("A.header");
 214 
 215   verify_config();
 216 
 217   if (!_valid && PrintAOT) {
 218       tty->print("%7d ", (int) tty->time_stamp().milliseconds());
 219       tty->print_cr("%4d     skipped %s  aot library", _dso_id, _name);
 220   }
 221 }
 222 
 223 AOTCodeHeap::AOTCodeHeap(AOTLib* lib) :
 224     CodeHeap("CodeHeap 'AOT'", CodeBlobType::AOT), _lib(lib), _classes(NULL), _code_to_aot(NULL) {
 225   assert(_lib->is_valid(), "invalid library");
 226 
 227   _lib_symbols_initialized = false;
 228   _aot_id = 0;
 229 
 230   _class_count = _lib->header()->_class_count;
 231   _method_count = _lib->header()->_method_count;
 232 
 233   // Collect metaspace info: names -> address in .got section
 234   _metaspace_names = (const char*) _lib->load_symbol("A.meta.names");
 235   _method_metadata =     (address) _lib->load_symbol("A.meth.metadata");
 236   _methods_offsets =     (address) _lib->load_symbol("A.meth.offsets");
 237   _klasses_offsets =     (address) _lib->load_symbol("A.kls.offsets");
 238   _dependencies    =     (address) _lib->load_symbol("A.kls.dependencies");
 239   _code_space      =     (address) _lib->load_symbol("A.text");
 240 
 241   // First cell is number of elements.
 242   _klasses_got      = (Metadata**) _lib->load_symbol("A.kls.got");
 243   _klasses_got_size = _lib->header()->_klasses_got_size;
 244 
 245   _metadata_got      = (Metadata**) _lib->load_symbol("A.meta.got");
 246   _metadata_got_size = _lib->header()->_metadata_got_size;
 247 
 248   _oop_got      = (oop*) _lib->load_symbol("A.oop.got");
 249   _oop_got_size = _lib->header()->_oop_got_size;
 250 
 251   // Collect stubs info
 252   _stubs_offsets = (int*) _lib->load_symbol("A.stubs.offsets");
 253 
 254   // code segments table
 255   _code_segments = (address) _lib->load_symbol("A.code.segments");
 256 
 257   // method state
 258   _method_state = (jlong*) _lib->load_symbol("A.meth.state");
 259 
 260   // Create a table for mapping classes
 261   _classes = NEW_C_HEAP_ARRAY(AOTClass, _class_count, mtCode);
 262   memset(_classes, 0, _class_count * sizeof(AOTClass));
 263 
 264   // Create table for searching AOTCompiledMethod based on pc.
 265   _code_to_aot = NEW_C_HEAP_ARRAY(CodeToAMethod, _method_count, mtCode);
 266   memset(_code_to_aot, 0, _method_count * sizeof(CodeToAMethod));
 267 
 268   _memory.set_low_boundary((char *)_code_space);
 269   _memory.set_high_boundary((char *)_code_space);
 270   _memory.set_low((char *)_code_space);
 271   _memory.set_high((char *)_code_space);
 272 
 273   _segmap.set_low_boundary((char *)_code_segments);
 274   _segmap.set_low((char *)_code_segments);
 275 
 276   _log2_segment_size = exact_log2(_lib->config()->_codeSegmentSize);
 277 
 278   // Register aot stubs
 279   register_stubs();
 280 
 281   if (PrintAOT || (PrintCompilation && PrintAOT)) {
 282     tty->print("%7d ", (int) tty->time_stamp().milliseconds());
 283     tty->print_cr("%4d     loaded    %s  aot library", _lib->id(), _lib->name());
 284   }
 285 }
 286 
 287 void AOTCodeHeap::publish_aot(const methodHandle& mh, AOTMethodData* method_data, int code_id) {
 288   // The method may be explicitly excluded by the user.
 289   // Or Interpreter uses an intrinsic for this method.
 290   if (CompilerOracle::should_exclude(mh) || !AbstractInterpreter::can_be_compiled(mh)) {
 291     return;
 292   }
 293 
 294   address code = method_data->_code;
 295   const char* name = method_data->_name;
 296   aot_metadata* meta = method_data->_meta;
 297 
 298   if (meta->scopes_pcs_begin() == meta->scopes_pcs_end()) {
 299     // When the AOT compiler compiles something big we fail to generate metadata
 300     // in CodeInstaller::gather_metadata. In that case the scopes_pcs_begin == scopes_pcs_end.
 301     // In all successful cases we always have 2 entries of scope pcs.
 302     log_info(aot, class, resolve)("Failed to load %s (no metadata available)", mh->name_and_sig_as_C_string());
 303     _code_to_aot[code_id]._state = invalid;
 304     return;
 305   }
 306 
 307   jlong* state_adr = &_method_state[code_id];
 308   address metadata_table = method_data->_metadata_table;
 309   int metadata_size = method_data->_metadata_size;
 310   assert(code_id < _method_count, "sanity");
 311   _aot_id++;
 312 
 313 #ifdef ASSERT
 314   if (_aot_id > CIStop || _aot_id < CIStart) {
 315     // Skip compilation
 316     return;
 317   }
 318 #endif
 319   // Check one more time.
 320   if (_code_to_aot[code_id]._state == invalid) {
 321     return;
 322   }
 323   AOTCompiledMethod *aot = new AOTCompiledMethod(code, mh(), meta, metadata_table, metadata_size, state_adr, this, name, code_id, _aot_id);
 324   assert(_code_to_aot[code_id]._aot == NULL, "should be not initialized");
 325   _code_to_aot[code_id]._aot = aot; // Should set this first
 326   if (Atomic::cmpxchg(in_use, &_code_to_aot[code_id]._state, not_set) != not_set) {
 327     _code_to_aot[code_id]._aot = NULL; // Clean
 328   } else { // success
 329     // Publish method
 330 #ifdef TIERED
 331     mh->set_aot_code(aot);
 332 #endif
 333     Method::set_code(mh, aot);
 334     if (PrintAOT || (PrintCompilation && PrintAOT)) {
 335       aot->print_on(tty, NULL);
 336     }
 337     // Publish oop only after we are visible to CompiledMethodIterator
 338     aot->set_oop(mh()->method_holder()->klass_holder());
 339   }
 340 }
 341 
 342 void AOTCodeHeap::link_primitive_array_klasses() {
 343   ResourceMark rm;
 344   for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) {
 345     BasicType t = (BasicType)i;
 346     if (is_java_primitive(t)) {
 347       const Klass* arr_klass = Universe::typeArrayKlassObj(t);
 348       AOTKlassData* klass_data = (AOTKlassData*) os::dll_lookup(_lib->dl_handle(), arr_klass->signature_name());
 349       if (klass_data != NULL) {
 350         // Set both GOT cells, resolved and initialized klass pointers.
 351         // _got_index points to second cell - resolved klass pointer.
 352         _klasses_got[klass_data->_got_index-1] = (Metadata*)arr_klass; // Initialized
 353         _klasses_got[klass_data->_got_index  ] = (Metadata*)arr_klass; // Resolved
 354         if (PrintAOT) {
 355           tty->print_cr("[Found  %s  in  %s]", arr_klass->internal_name(), _lib->name());
 356         }
 357       }
 358     }
 359   }
 360 }
 361 
 362 void AOTCodeHeap::register_stubs() {
 363   int stubs_count = _stubs_offsets[0]; // contains number
 364   _stubs_offsets++;
 365   AOTMethodOffsets* stub_offsets = (AOTMethodOffsets*)_stubs_offsets;
 366   for (int i = 0; i < stubs_count; ++i) {
 367     const char* stub_name = _metaspace_names + stub_offsets[i]._name_offset;
 368     address entry = _code_space  + stub_offsets[i]._code_offset;
 369     aot_metadata* meta = (aot_metadata *) (_method_metadata + stub_offsets[i]._meta_offset);
 370     address metadata_table = (address)_metadata_got + stub_offsets[i]._metadata_got_offset;
 371     int metadata_size = stub_offsets[i]._metadata_got_size;
 372     int code_id = stub_offsets[i]._code_id;
 373     assert(code_id < _method_count, "sanity");
 374     jlong* state_adr = &_method_state[code_id];
 375     int len = build_u2_from((address)stub_name);
 376     stub_name += 2;
 377     char* full_name = NEW_C_HEAP_ARRAY(char, len+5, mtCode);
 378     if (full_name == NULL) { // No memory?
 379       break;
 380     }
 381     memcpy(full_name, "AOT ", 4);
 382     memcpy(full_name+4, stub_name, len);
 383     full_name[len+4] = 0;
 384     guarantee(_code_to_aot[code_id]._state != invalid, "stub %s can't be invalidated", full_name);
 385     AOTCompiledMethod* aot = new AOTCompiledMethod(entry, NULL, meta, metadata_table, metadata_size, state_adr, this, full_name, code_id, i);
 386     assert(_code_to_aot[code_id]._aot  == NULL, "should be not initialized");
 387     _code_to_aot[code_id]._aot  = aot;
 388     if (Atomic::cmpxchg(in_use, &_code_to_aot[code_id]._state, not_set) != not_set) {
 389       fatal("stab '%s' code state is %d", full_name, _code_to_aot[code_id]._state);
 390     }
 391     // Adjust code buffer boundaries only for stubs because they are last in the buffer.
 392     adjust_boundaries(aot);
 393     if (PrintAOT && Verbose) {
 394       aot->print_on(tty, NULL);
 395     }
 396   }
 397 }
 398 
 399 #define SET_AOT_GLOBAL_SYMBOL_VALUE(AOTSYMNAME, AOTSYMTYPE, VMSYMVAL) \
 400   {                                                                   \
 401     AOTSYMTYPE * adr = (AOTSYMTYPE *) os::dll_lookup(_lib->dl_handle(), AOTSYMNAME);  \
 402     /* Check for a lookup error */                                    \
 403     guarantee(adr != NULL, "AOT Symbol not found %s", AOTSYMNAME);    \
 404     *adr = (AOTSYMTYPE) VMSYMVAL;                                     \
 405   }
 406 
 407 void AOTCodeHeap::link_graal_runtime_symbols()  {
 408     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_monitorenter", address, JVMCIRuntime::monitorenter);
 409     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_monitorexit", address, JVMCIRuntime::monitorexit);
 410     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_log_object", address, JVMCIRuntime::log_object);
 411     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_log_printf", address, JVMCIRuntime::log_printf);
 412     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_log_primitive", address, JVMCIRuntime::log_primitive);
 413     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_new_instance", address, JVMCIRuntime::new_instance);
 414     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_new_array", address, JVMCIRuntime::new_array);
 415     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_new_multi_array", address, JVMCIRuntime::new_multi_array);
 416     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_dynamic_new_array", address, JVMCIRuntime::dynamic_new_array);
 417     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_validate_object", address, JVMCIRuntime::validate_object);
 418     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_write_barrier_pre", address, JVMCIRuntime::write_barrier_pre);
 419     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_identity_hash_code", address, JVMCIRuntime::identity_hash_code);
 420     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_write_barrier_post", address, JVMCIRuntime::write_barrier_post);
 421     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_dynamic_new_instance", address, JVMCIRuntime::dynamic_new_instance);
 422     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_thread_is_interrupted", address, JVMCIRuntime::thread_is_interrupted);
 423     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_exception_handler_for_pc", address, JVMCIRuntime::exception_handler_for_pc);
 424     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_test_deoptimize_call_int", address, JVMCIRuntime::test_deoptimize_call_int);
 425     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_throw_and_post_jvmti_exception", address, JVMCIRuntime::throw_and_post_jvmti_exception);
 426     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_throw_klass_external_name_exception", address, JVMCIRuntime::throw_klass_external_name_exception);
 427     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_throw_class_cast_exception", address, JVMCIRuntime::throw_class_cast_exception);
 428     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_vm_message", address, JVMCIRuntime::vm_message);
 429     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_jvmci_runtime_vm_error", address, JVMCIRuntime::vm_error);
 430 }
 431 
 432 void AOTCodeHeap::link_shared_runtime_symbols() {
 433     SET_AOT_GLOBAL_SYMBOL_VALUE("_resolve_static_entry", address, SharedRuntime::get_resolve_static_call_stub());
 434     SET_AOT_GLOBAL_SYMBOL_VALUE("_resolve_virtual_entry", address, SharedRuntime::get_resolve_virtual_call_stub());
 435     SET_AOT_GLOBAL_SYMBOL_VALUE("_resolve_opt_virtual_entry", address, SharedRuntime::get_resolve_opt_virtual_call_stub());
 436     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_deopt_blob_unpack", address, SharedRuntime::deopt_blob()->unpack());
 437     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_deopt_blob_uncommon_trap", address, SharedRuntime::deopt_blob()->uncommon_trap());
 438     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_ic_miss_stub", address, SharedRuntime::get_ic_miss_stub());
 439     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_handle_wrong_method_stub", address, SharedRuntime::get_handle_wrong_method_stub());
 440     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_exception_handler_for_return_address", address, SharedRuntime::exception_handler_for_return_address);
 441     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_register_finalizer", address, SharedRuntime::register_finalizer);
 442     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_OSR_migration_end", address, SharedRuntime::OSR_migration_end);
 443     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_dynamic_invoke", address, CompilerRuntime::resolve_dynamic_invoke);
 444     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_string_by_symbol", address, CompilerRuntime::resolve_string_by_symbol);
 445     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_klass_by_symbol", address, CompilerRuntime::resolve_klass_by_symbol);
 446     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_resolve_method_by_symbol_and_load_counters", address, CompilerRuntime::resolve_method_by_symbol_and_load_counters);
 447     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_initialize_klass_by_symbol", address, CompilerRuntime::initialize_klass_by_symbol);
 448     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_invocation_event", address, CompilerRuntime::invocation_event);
 449     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_backedge_event", address, CompilerRuntime::backedge_event);
 450 
 451     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dpow", address, SharedRuntime::dpow);
 452     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dexp", address, SharedRuntime::dexp);
 453     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dcos", address, SharedRuntime::dcos);
 454     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dsin", address, SharedRuntime::dsin);
 455     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dtan", address, SharedRuntime::dtan);
 456     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dlog", address, SharedRuntime::dlog);
 457     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_shared_runtime_dlog10", address, SharedRuntime::dlog10);
 458 }
 459 
 460 void AOTCodeHeap::link_stub_routines_symbols() {
 461     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jbyte_arraycopy", address, StubRoutines::_jbyte_arraycopy);
 462     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jshort_arraycopy", address, StubRoutines::_jshort_arraycopy);
 463     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jint_arraycopy", address, StubRoutines::_jint_arraycopy);
 464     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jlong_arraycopy", address, StubRoutines::_jlong_arraycopy);
 465     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_arraycopy", address, StubRoutines::_oop_arraycopy);
 466     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_arraycopy_uninit", address, StubRoutines::_oop_arraycopy_uninit);
 467 
 468     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jbyte_disjoint_arraycopy", address, StubRoutines::_jbyte_disjoint_arraycopy);
 469     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jshort_disjoint_arraycopy", address, StubRoutines::_jshort_disjoint_arraycopy);
 470     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jint_disjoint_arraycopy", address, StubRoutines::_jint_disjoint_arraycopy);
 471     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_jlong_disjoint_arraycopy", address, StubRoutines::_jlong_disjoint_arraycopy);
 472     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_disjoint_arraycopy", address, StubRoutines::_oop_disjoint_arraycopy);
 473     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_oop_disjoint_arraycopy_uninit", address, StubRoutines::_oop_disjoint_arraycopy_uninit);
 474 
 475     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jbyte_arraycopy", address, StubRoutines::_arrayof_jbyte_arraycopy);
 476     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jshort_arraycopy", address, StubRoutines::_arrayof_jshort_arraycopy);
 477     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jint_arraycopy", address, StubRoutines::_arrayof_jint_arraycopy);
 478     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jlong_arraycopy", address, StubRoutines::_arrayof_jlong_arraycopy);
 479     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_arraycopy", address, StubRoutines::_arrayof_oop_arraycopy);
 480     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_arraycopy_uninit", address, StubRoutines::_arrayof_oop_arraycopy_uninit);
 481 
 482     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jbyte_disjoint_arraycopy", address, StubRoutines::_arrayof_jbyte_disjoint_arraycopy);
 483     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jshort_disjoint_arraycopy", address, StubRoutines::_arrayof_jshort_disjoint_arraycopy);
 484     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jint_disjoint_arraycopy", address, StubRoutines::_arrayof_jint_disjoint_arraycopy);
 485     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_jlong_disjoint_arraycopy", address, StubRoutines::_arrayof_jlong_disjoint_arraycopy);
 486     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_disjoint_arraycopy", address, StubRoutines::_arrayof_oop_disjoint_arraycopy);
 487     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_arrayof_oop_disjoint_arraycopy_uninit", address, StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit);
 488 
 489     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_unsafe_arraycopy", address, StubRoutines::_unsafe_arraycopy);
 490 
 491     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_checkcast_arraycopy", address, StubRoutines::_checkcast_arraycopy);
 492 
 493     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_generic_arraycopy", address, StubRoutines::_generic_arraycopy);
 494 
 495     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_aescrypt_encryptBlock", address, StubRoutines::_aescrypt_encryptBlock);
 496     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_aescrypt_decryptBlock", address, StubRoutines::_aescrypt_decryptBlock);
 497     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_cipherBlockChaining_encryptAESCrypt", address, StubRoutines::_cipherBlockChaining_encryptAESCrypt);
 498     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_cipherBlockChaining_decryptAESCrypt", address, StubRoutines::_cipherBlockChaining_decryptAESCrypt);
 499     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_update_bytes_crc32", address, StubRoutines::_updateBytesCRC32);
 500     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_crc_table_adr", address, StubRoutines::_crc_table_adr);
 501 
 502 
 503     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha1_implCompress", address, StubRoutines::_sha1_implCompress);
 504     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha1_implCompressMB", address, StubRoutines::_sha1_implCompressMB);
 505     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha256_implCompress", address, StubRoutines::_sha256_implCompress);
 506     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha256_implCompressMB", address, StubRoutines::_sha256_implCompressMB);
 507     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha512_implCompress", address, StubRoutines::_sha512_implCompress);
 508     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_sha512_implCompressMB", address, StubRoutines::_sha512_implCompressMB);
 509     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_multiplyToLen", address, StubRoutines::_multiplyToLen);
 510 
 511     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_counterMode_AESCrypt", address, StubRoutines::_counterMode_AESCrypt);
 512     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_ghash_processBlocks", address, StubRoutines::_ghash_processBlocks);
 513     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_crc32c_table_addr", address, StubRoutines::_crc32c_table_addr);
 514     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_updateBytesCRC32C", address, StubRoutines::_updateBytesCRC32C);
 515     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_updateBytesAdler32", address, StubRoutines::_updateBytesAdler32);
 516     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_squareToLen", address, StubRoutines::_squareToLen);
 517     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_mulAdd", address, StubRoutines::_mulAdd);
 518     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_montgomeryMultiply",  address, StubRoutines::_montgomeryMultiply);
 519     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_montgomerySquare", address, StubRoutines::_montgomerySquare);
 520     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_vectorizedMismatch", address, StubRoutines::_vectorizedMismatch);
 521 
 522     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_stub_routines_throw_delayed_StackOverflowError_entry", address, StubRoutines::_throw_delayed_StackOverflowError_entry);
 523 
 524 }
 525 
 526 void AOTCodeHeap::link_os_symbols() {
 527     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_os_javaTimeMillis", address, os::javaTimeMillis);
 528     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_os_javaTimeNanos", address, os::javaTimeNanos);
 529 }
 530 
 531 /*
 532  * Link any global symbols in precompiled DSO with dlopen() _dl_handle
 533  * dso_handle.
 534  */
 535 
 536 void AOTCodeHeap::link_global_lib_symbols() {
 537   if (!_lib_symbols_initialized) {
 538     _lib_symbols_initialized = true;
 539 
 540     CollectedHeap* heap = Universe::heap();
 541     CardTableModRefBS* ct = (CardTableModRefBS*)(heap->barrier_set());
 542     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_card_table_address", address, ct->byte_map_base);
 543     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_heap_top_address", address, (heap->supports_inline_contig_alloc() ? heap->top_addr() : NULL));
 544     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_heap_end_address", address, (heap->supports_inline_contig_alloc() ? heap->end_addr() : NULL));
 545     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_polling_page", address, os::get_polling_page());
 546     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_narrow_klass_base_address", address, Universe::narrow_klass_base());
 547     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_narrow_oop_base_address", address, Universe::narrow_oop_base());
 548     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_log_of_heap_region_grain_bytes", int, HeapRegion::LogOfHRGrainBytes);
 549     SET_AOT_GLOBAL_SYMBOL_VALUE("_aot_inline_contiguous_allocation_supported", bool, heap->supports_inline_contig_alloc());
 550     link_shared_runtime_symbols();
 551     link_stub_routines_symbols();
 552     link_os_symbols();
 553     link_graal_runtime_symbols();
 554 
 555     // Link primitive array klasses.
 556     link_primitive_array_klasses();
 557   }
 558 }
 559 
 560 #ifndef PRODUCT
 561 int AOTCodeHeap::klasses_seen = 0;
 562 int AOTCodeHeap::aot_klasses_found = 0;
 563 int AOTCodeHeap::aot_klasses_fp_miss = 0;
 564 int AOTCodeHeap::aot_klasses_cl_miss = 0;
 565 int AOTCodeHeap::aot_methods_found = 0;
 566 
 567 void AOTCodeHeap::print_statistics() {
 568   tty->print_cr("Classes seen: %d  AOT classes found: %d  AOT methods found: %d", klasses_seen, aot_klasses_found, aot_methods_found);
 569   tty->print_cr("AOT fingerprint mismatches: %d  AOT class loader mismatches: %d", aot_klasses_fp_miss, aot_klasses_cl_miss);
 570 }
 571 #endif
 572 
 573 Method* AOTCodeHeap::find_method(Klass* klass, Thread* thread, const char* method_name) {
 574   int method_name_len = build_u2_from((address)method_name);
 575   method_name += 2;
 576   const char* signature_name = method_name + method_name_len;
 577   int signature_name_len = build_u2_from((address)signature_name);
 578   signature_name += 2;
 579   // The class should have been loaded so the method and signature should already be
 580   // in the symbol table.  If they're not there, the method doesn't exist.
 581   TempNewSymbol name = SymbolTable::probe(method_name, method_name_len);
 582   TempNewSymbol signature = SymbolTable::probe(signature_name, signature_name_len);
 583 
 584   Method* m;
 585   if (name == NULL || signature == NULL) {
 586     m = NULL;
 587   } else if (name == vmSymbols::object_initializer_name() ||
 588              name == vmSymbols::class_initializer_name()) {
 589     // Never search superclasses for constructors
 590     if (klass->is_instance_klass()) {
 591       m = InstanceKlass::cast(klass)->find_method(name, signature);
 592     } else {
 593       m = NULL;
 594     }
 595   } else {
 596     m = klass->lookup_method(name, signature);
 597     if (m == NULL && klass->is_instance_klass()) {
 598       m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name, signature);
 599     }
 600   }
 601   if (m == NULL) {
 602     // Fatal error because we assume classes and methods should not be changed since aot compilation.
 603     const char* klass_name = klass->external_name();
 604     int klass_len = (int)strlen(klass_name);
 605     char* meta_name = NEW_RESOURCE_ARRAY(char, klass_len + 1 + method_name_len + signature_name_len + 1);
 606     memcpy(meta_name, klass_name, klass_len);
 607     meta_name[klass_len] = '.';
 608     memcpy(&meta_name[klass_len + 1], method_name, method_name_len);
 609     memcpy(&meta_name[klass_len + 1 + method_name_len], signature_name, signature_name_len);
 610     meta_name[klass_len + 1 + method_name_len + signature_name_len] = '\0';
 611     Handle exception = Exceptions::new_exception(thread, vmSymbols::java_lang_NoSuchMethodError(), meta_name);
 612     java_lang_Throwable::print(exception(), tty);
 613     tty->cr();
 614     java_lang_Throwable::print_stack_trace(exception, tty);
 615     tty->cr();
 616     fatal("Failed to find method '%s'", meta_name);
 617   }
 618   NOT_PRODUCT( aot_methods_found++; )
 619   return m;
 620 }
 621 
 622 AOTKlassData* AOTCodeHeap::find_klass(const char *name) {
 623   return (AOTKlassData*) os::dll_lookup(_lib->dl_handle(), name);
 624 }
 625 
 626 AOTKlassData* AOTCodeHeap::find_klass(InstanceKlass* ik) {
 627   ResourceMark rm;
 628   AOTKlassData* klass_data = find_klass(ik->signature_name());
 629   return klass_data;
 630 }
 631 
 632 bool AOTCodeHeap::is_dependent_method(Klass* dependee, AOTCompiledMethod* aot) {
 633   InstanceKlass *dependee_ik = InstanceKlass::cast(dependee);
 634   AOTKlassData* klass_data = find_klass(dependee_ik);
 635   if (klass_data == NULL) {
 636     return false; // no AOT records for this class - no dependencies
 637   }
 638   if (!dependee_ik->has_passed_fingerprint_check()) {
 639     return false; // different class
 640   }
 641 
 642   int methods_offset = klass_data->_dependent_methods_offset;
 643   if (methods_offset >= 0) {
 644     address methods_cnt_adr = _dependencies + methods_offset;
 645     int methods_cnt = *(int*)methods_cnt_adr;
 646     int* indexes = (int*)(methods_cnt_adr + 4);
 647     for (int i = 0; i < methods_cnt; ++i) {
 648       int code_id = indexes[i];
 649       if (_code_to_aot[code_id]._aot == aot) {
 650         return true; // found dependent method
 651       }
 652     }
 653   }
 654   return false;
 655 }
 656 
 657 void AOTCodeHeap::sweep_dependent_methods(int* indexes, int methods_cnt) {
 658   int marked = 0;
 659   for (int i = 0; i < methods_cnt; ++i) {
 660     int code_id = indexes[i];
 661     // Invalidate aot code.
 662     if (Atomic::cmpxchg(invalid, &_code_to_aot[code_id]._state, not_set) != not_set) {
 663       if (_code_to_aot[code_id]._state == in_use) {
 664         AOTCompiledMethod* aot = _code_to_aot[code_id]._aot;
 665         assert(aot != NULL, "aot should be set");
 666         if (!aot->is_runtime_stub()) { // Something is wrong - should not invalidate stubs.
 667           aot->mark_for_deoptimization(false);
 668           marked++;
 669         }
 670       }
 671     }
 672   }
 673   if (marked > 0) {
 674     VM_Deoptimize op;
 675     VMThread::execute(&op);
 676   }
 677 }
 678 
 679 void AOTCodeHeap::sweep_dependent_methods(AOTKlassData* klass_data) {
 680   // Make dependent methods non_entrant forever.
 681   int methods_offset = klass_data->_dependent_methods_offset;
 682   if (methods_offset >= 0) {
 683     address methods_cnt_adr = _dependencies + methods_offset;
 684     int methods_cnt = *(int*)methods_cnt_adr;
 685     int* indexes = (int*)(methods_cnt_adr + 4);
 686     sweep_dependent_methods(indexes, methods_cnt);
 687   }
 688 }
 689 
 690 void AOTCodeHeap::sweep_dependent_methods(InstanceKlass* ik) {
 691   AOTKlassData* klass_data = find_klass(ik);
 692   vmassert(klass_data != NULL, "dependency data missing");
 693   sweep_dependent_methods(klass_data);
 694 }
 695 
 696 void AOTCodeHeap::sweep_method(AOTCompiledMethod *aot) {
 697   int indexes[] = {aot->method_index()};
 698   sweep_dependent_methods(indexes, 1);
 699   vmassert(aot->method()->code() != aot && aot->method()->aot_code() == NULL, "method still active");
 700 }
 701 
 702 
 703 bool AOTCodeHeap::load_klass_data(InstanceKlass* ik, Thread* thread) {
 704   ResourceMark rm;
 705 
 706   NOT_PRODUCT( klasses_seen++; )
 707 
 708   AOTKlassData* klass_data = find_klass(ik);
 709   if (klass_data == NULL) {
 710     return false;
 711   }
 712 
 713   if (!ik->has_passed_fingerprint_check()) {
 714     NOT_PRODUCT( aot_klasses_fp_miss++; )
 715     log_trace(aot, class, fingerprint)("class  %s%s  has bad fingerprint in  %s tid=" INTPTR_FORMAT,
 716                                        ik->internal_name(), ik->is_shared() ? " (shared)" : "",
 717                                        _lib->name(), p2i(thread));
 718     sweep_dependent_methods(klass_data);
 719     return false;
 720   }
 721 
 722   if (ik->has_been_redefined()) {
 723     log_trace(aot, class, load)("class  %s%s in %s  has been redefined tid=" INTPTR_FORMAT,
 724                                 ik->internal_name(), ik->is_shared() ? " (shared)" : "",
 725                                 _lib->name(), p2i(thread));
 726     sweep_dependent_methods(klass_data);
 727     return false;
 728   }
 729 
 730   assert(klass_data->_class_id < _class_count, "invalid class id");
 731   AOTClass* aot_class = &_classes[klass_data->_class_id];
 732   if (aot_class->_classloader != NULL && aot_class->_classloader != ik->class_loader_data()) {
 733     log_trace(aot, class, load)("class  %s  in  %s already loaded for classloader %p vs %p tid=" INTPTR_FORMAT,
 734                                 ik->internal_name(), _lib->name(), aot_class->_classloader, ik->class_loader_data(), p2i(thread));
 735     NOT_PRODUCT( aot_klasses_cl_miss++; )
 736     return false;
 737   }
 738 
 739   if (_lib->config()->_omitAssertions && JavaAssertions::enabled(ik->name()->as_C_string(), ik->class_loader() == NULL)) {
 740     log_trace(aot, class, load)("class  %s  in  %s does not have java assertions in compiled code, but assertions are enabled for this execution.", ik->internal_name(), _lib->name());
 741     sweep_dependent_methods(klass_data);
 742     return false;
 743   }
 744 
 745   NOT_PRODUCT( aot_klasses_found++; )
 746 
 747   log_trace(aot, class, load)("found  %s  in  %s for classloader %p tid=" INTPTR_FORMAT, ik->internal_name(), _lib->name(), ik->class_loader_data(), p2i(thread));
 748 
 749   aot_class->_classloader = ik->class_loader_data();
 750   // Set klass's Resolve (second) got cell.
 751   _klasses_got[klass_data->_got_index] = ik;
 752   if (ik->is_initialized()) {
 753     _klasses_got[klass_data->_got_index - 1] = ik;
 754   }
 755 
 756   // Initialize global symbols of the DSO to the corresponding VM symbol values.
 757   link_global_lib_symbols();
 758 
 759   int methods_offset = klass_data->_compiled_methods_offset;
 760   if (methods_offset >= 0) {
 761     address methods_cnt_adr = _methods_offsets + methods_offset;
 762     int methods_cnt = *(int*)methods_cnt_adr;
 763     // Collect data about compiled methods
 764     AOTMethodData* methods_data = NEW_RESOURCE_ARRAY(AOTMethodData, methods_cnt);
 765     AOTMethodOffsets* methods_offsets = (AOTMethodOffsets*)(methods_cnt_adr + 4);
 766     for (int i = 0; i < methods_cnt; ++i) {
 767       AOTMethodOffsets* method_offsets = &methods_offsets[i];
 768       int code_id = method_offsets->_code_id;
 769       if (_code_to_aot[code_id]._state == invalid) {
 770         continue; // skip AOT methods slots which have been invalidated
 771       }
 772       AOTMethodData* method_data = &methods_data[i];
 773       const char* aot_name = _metaspace_names + method_offsets->_name_offset;
 774       method_data->_name = aot_name;
 775       method_data->_code = _code_space  + method_offsets->_code_offset;
 776       method_data->_meta = (aot_metadata*)(_method_metadata + method_offsets->_meta_offset);
 777       method_data->_metadata_table = (address)_metadata_got + method_offsets->_metadata_got_offset;
 778       method_data->_metadata_size  = method_offsets->_metadata_got_size;
 779       // aot_name format: "<u2_size>Ljava/lang/ThreadGroup;<u2_size>addUnstarted<u2_size>()V"
 780       int klass_len = build_u2_from((address)aot_name);
 781       const char* method_name = aot_name + 2 + klass_len;
 782       Method* m = AOTCodeHeap::find_method(ik, thread, method_name);
 783       methodHandle mh(thread, m);
 784       if (mh->code() != NULL) { // Does it have already compiled code?
 785         continue; // Don't overwrite
 786       }
 787       publish_aot(mh, method_data, code_id);
 788     }
 789   }
 790   return true;
 791 }
 792 
 793 AOTCompiledMethod* AOTCodeHeap::next_in_use_at(int start) const {
 794   for (int index = start; index < _method_count; index++) {
 795     if (_code_to_aot[index]._state != in_use) {
 796       continue; // Skip uninitialized entries.
 797     }
 798     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 799     return aot;
 800   }
 801   return NULL;
 802 }
 803 
 804 void* AOTCodeHeap::first() const {
 805   return next_in_use_at(0);
 806 }
 807 
 808 void* AOTCodeHeap::next(void* p) const {
 809   AOTCompiledMethod *aot = (AOTCompiledMethod *)p;
 810   int next_index = aot->method_index() + 1;
 811   assert(next_index <= _method_count, "");
 812   if (next_index == _method_count) {
 813     return NULL;
 814   }
 815   return next_in_use_at(next_index);
 816 }
 817 
 818 void* AOTCodeHeap::find_start(void* p) const {
 819   if (!contains(p)) {
 820     return NULL;
 821   }
 822   size_t offset = pointer_delta(p, low_boundary(), 1);
 823   // Use segments table
 824   size_t seg_idx = offset / _lib->config()->_codeSegmentSize;
 825   if ((int)(_code_segments[seg_idx]) == 0xff) {
 826     return NULL;
 827   }
 828   while (_code_segments[seg_idx] > 0) {
 829     seg_idx -= (int)_code_segments[seg_idx];
 830   }
 831   int code_offset = (int)seg_idx * _lib->config()->_codeSegmentSize;
 832   int aot_index = *(int*)(_code_space + code_offset);
 833   AOTCompiledMethod* aot = _code_to_aot[aot_index]._aot;
 834   assert(aot != NULL, "should find registered aot method");
 835   return aot;
 836 }
 837 
 838 AOTCompiledMethod* AOTCodeHeap::find_aot(address p) const {
 839   assert(contains(p), "should be here");
 840   return (AOTCompiledMethod *)find_start(p);
 841 }
 842 
 843 CodeBlob* AOTCodeHeap::find_blob_unsafe(void* start) const {
 844   return (CodeBlob*)AOTCodeHeap::find_start(start);
 845 }
 846 
 847 void AOTCodeHeap::oops_do(OopClosure* f) {
 848   for (int i = 0; i < _oop_got_size; i++) {
 849     oop* p = &_oop_got[i];
 850     if (*p == NULL)  continue;  // skip non-oops
 851     f->do_oop(p);
 852   }
 853   for (int index = 0; index < _method_count; index++) {
 854     if (_code_to_aot[index]._state != in_use) {
 855       continue; // Skip uninitialized entries.
 856     }
 857     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 858     aot->do_oops(f);
 859   }
 860 }
 861 
 862 // Scan only klasses_got cells which should have only Klass*,
 863 // metadata_got cells are scanned only for alive AOT methods
 864 // by AOTCompiledMethod::metadata_do().
 865 void AOTCodeHeap::got_metadata_do(void f(Metadata*)) {
 866   for (int i = 1; i < _klasses_got_size; i++) {
 867     Metadata** p = &_klasses_got[i];
 868     Metadata* md = *p;
 869     if (md == NULL)  continue;  // skip non-oops
 870     if (Metaspace::contains(md)) {
 871       f(md);
 872     } else {
 873       intptr_t meta = (intptr_t)md;
 874       fatal("Invalid value in _klasses_got[%d] = " INTPTR_FORMAT, i, meta);
 875     }
 876   }
 877 }
 878 
 879 void AOTCodeHeap::cleanup_inline_caches() {
 880   for (int index = 0; index < _method_count; index++) {
 881     if (_code_to_aot[index]._state != in_use) {
 882       continue; // Skip uninitialized entries.
 883     }
 884     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 885     aot->cleanup_inline_caches();
 886   }
 887 }
 888 
 889 #ifdef ASSERT
 890 int AOTCodeHeap::verify_icholder_relocations() {
 891   int count = 0;
 892   for (int index = 0; index < _method_count; index++) {
 893     if (_code_to_aot[index]._state != in_use) {
 894       continue; // Skip uninitialized entries.
 895     }
 896     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 897     count += aot->verify_icholder_relocations();
 898   }
 899   return count;
 900 }
 901 #endif
 902 
 903 void AOTCodeHeap::flush_evol_dependents_on(InstanceKlass* dependee) {
 904   for (int index = 0; index < _method_count; index++) {
 905     if (_code_to_aot[index]._state != in_use) {
 906       continue; // Skip uninitialized entries.
 907     }
 908     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 909     aot->flush_evol_dependents_on(dependee);
 910   }
 911 }
 912 
 913 void AOTCodeHeap::metadata_do(void f(Metadata*)) {
 914   for (int index = 0; index < _method_count; index++) {
 915     if (_code_to_aot[index]._state != in_use) {
 916       continue; // Skip uninitialized entries.
 917     }
 918     AOTCompiledMethod* aot = _code_to_aot[index]._aot;
 919     if (aot->_is_alive()) {
 920       aot->metadata_do(f);
 921     }
 922   }
 923   // Scan klasses_got cells.
 924   got_metadata_do(f);
 925 }
 926 
 927 bool AOTCodeHeap::reconcile_dynamic_klass(AOTCompiledMethod *caller, InstanceKlass* holder, int index, Klass *dyno_klass, const char *descriptor1, const char *descriptor2) {
 928   const char * const descriptors[2] = {descriptor1, descriptor2};
 929   JavaThread *thread = JavaThread::current();
 930   ResourceMark rm(thread);
 931 
 932   AOTKlassData* holder_data = find_klass(holder);
 933   vmassert(holder_data != NULL, "klass %s not found", holder->signature_name());
 934   vmassert(is_dependent_method(holder, caller), "sanity");
 935 
 936   AOTKlassData* dyno_data = NULL;
 937   bool adapter_failed = false;
 938   char buf[64];
 939   int descriptor_index = 0;
 940   // descriptors[0] specific name ("adapter:<method_id>") for matching
 941   // descriptors[1] fall-back name ("adapter") for depdencies
 942   while (descriptor_index < 2) {
 943     const char *descriptor = descriptors[descriptor_index];
 944     if (descriptor == NULL) {
 945       break;
 946     }
 947     jio_snprintf(buf, sizeof buf, "%s<%d:%d>", descriptor, holder_data->_class_id, index);
 948     dyno_data = find_klass(buf);
 949     if (dyno_data != NULL) {
 950       break;
 951     }
 952     // If match failed then try fall-back for dependencies
 953     ++descriptor_index;
 954     adapter_failed = true;
 955   }
 956 
 957   if (dyno_data == NULL && dyno_klass == NULL) {
 958     // all is well, no (appendix) at compile-time, and still none
 959     return true;
 960   }
 961 
 962   if (dyno_data == NULL) {
 963     // no (appendix) at build-time, but now there is
 964     sweep_dependent_methods(holder_data);
 965     return false;
 966   }
 967 
 968   if (adapter_failed) {
 969     // adapter method mismatch
 970     sweep_dependent_methods(holder_data);
 971     sweep_dependent_methods(dyno_data);
 972     return false;
 973   }
 974 
 975   if (dyno_klass == NULL) {
 976     // (appendix) at build-time, none now
 977     sweep_dependent_methods(holder_data);
 978     sweep_dependent_methods(dyno_data);
 979     return false;
 980   }
 981 
 982   // TODO: support array appendix object
 983   if (!dyno_klass->is_instance_klass()) {
 984     sweep_dependent_methods(holder_data);
 985     sweep_dependent_methods(dyno_data);
 986     return false;
 987   }
 988 
 989   InstanceKlass* dyno = InstanceKlass::cast(dyno_klass);
 990 
 991   if (!dyno->is_anonymous()) {
 992     if (_klasses_got[dyno_data->_got_index] != dyno) {
 993       // compile-time class different from runtime class, fail and deoptimize
 994       sweep_dependent_methods(holder_data);
 995       sweep_dependent_methods(dyno_data);
 996       return false;
 997     }
 998 
 999     if (dyno->is_initialized()) {
1000       _klasses_got[dyno_data->_got_index - 1] = dyno;
1001     }
1002     return true;
1003   }
1004 
1005   // TODO: support anonymous supers
1006   if (!dyno->supers_have_passed_fingerprint_checks() || dyno->get_stored_fingerprint() != dyno_data->_fingerprint) {
1007       NOT_PRODUCT( aot_klasses_fp_miss++; )
1008       log_trace(aot, class, fingerprint)("class  %s%s  has bad fingerprint in  %s tid=" INTPTR_FORMAT,
1009           dyno->internal_name(), dyno->is_shared() ? " (shared)" : "",
1010           _lib->name(), p2i(thread));
1011     sweep_dependent_methods(holder_data);
1012     sweep_dependent_methods(dyno_data);
1013     return false;
1014   }
1015 
1016   _klasses_got[dyno_data->_got_index] = dyno;
1017   if (dyno->is_initialized()) {
1018     _klasses_got[dyno_data->_got_index - 1] = dyno;
1019   }
1020 
1021   // TODO: hook up any AOT code
1022   // load_klass_data(dyno_data, thread);
1023   return true;
1024 }
1025 
1026 bool AOTCodeHeap::reconcile_dynamic_method(AOTCompiledMethod *caller, InstanceKlass* holder, int index, Method *adapter_method) {
1027     InstanceKlass *adapter_klass = adapter_method->method_holder();
1028     char buf[64];
1029     jio_snprintf(buf, sizeof buf, "adapter:%d", adapter_method->method_idnum());
1030     if (!reconcile_dynamic_klass(caller, holder, index, adapter_klass, buf, "adapter")) {
1031       return false;
1032     }
1033     return true;
1034 }
1035 
1036 bool AOTCodeHeap::reconcile_dynamic_invoke(AOTCompiledMethod* caller, InstanceKlass* holder, int index, Method* adapter_method, Klass *appendix_klass) {
1037     if (!reconcile_dynamic_klass(caller, holder, index, appendix_klass, "appendix")) {
1038       return false;
1039     }
1040 
1041     if (!reconcile_dynamic_method(caller, holder, index, adapter_method)) {
1042       return false;
1043     }
1044 
1045     return true;
1046 }