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