1 /*
   2  * Copyright (c) 2023, 2024, 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 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.hpp"
  27 #include "cds/cdsAccess.hpp"
  28 #include "cds/cdsConfig.hpp"
  29 #include "cds/metaspaceShared.hpp"
  30 #include "ci/ciConstant.hpp"
  31 #include "ci/ciEnv.hpp"
  32 #include "ci/ciField.hpp"
  33 #include "ci/ciMethod.hpp"
  34 #include "ci/ciMethodData.hpp"
  35 #include "ci/ciObject.hpp"
  36 #include "ci/ciUtilities.inline.hpp"
  37 #include "classfile/javaAssertions.hpp"
  38 #include "classfile/stringTable.hpp"
  39 #include "classfile/symbolTable.hpp"
  40 #include "classfile/systemDictionary.hpp"
  41 #include "classfile/vmClasses.hpp"
  42 #include "classfile/vmIntrinsics.hpp"
  43 #include "code/codeBlob.hpp"
  44 #include "code/codeCache.hpp"
  45 #include "code/oopRecorder.inline.hpp"
  46 #include "code/SCCache.hpp"
  47 #include "compiler/abstractCompiler.hpp"
  48 #include "compiler/compilationPolicy.hpp"
  49 #include "compiler/compileBroker.hpp"
  50 #include "compiler/compileTask.hpp"
  51 #include "gc/g1/g1BarrierSetRuntime.hpp"
  52 #include "gc/shared/gcConfig.hpp"
  53 #include "logging/log.hpp"
  54 #include "memory/universe.hpp"
  55 #include "oops/klass.inline.hpp"
  56 #include "oops/method.inline.hpp"
  57 #include "oops/trainingData.hpp"
  58 #include "prims/jvmtiThreadState.hpp"
  59 #include "runtime/atomic.hpp"
  60 #include "runtime/flags/flagSetting.hpp"
  61 #include "runtime/globals_extension.hpp"
  62 #include "runtime/handles.inline.hpp"
  63 #include "runtime/java.hpp"
  64 #include "runtime/jniHandles.inline.hpp"
  65 #include "runtime/os.inline.hpp"
  66 #include "runtime/sharedRuntime.hpp"
  67 #include "runtime/stubCodeGenerator.hpp"
  68 #include "runtime/stubRoutines.hpp"
  69 #include "runtime/timerTrace.hpp"
  70 #include "runtime/threadIdentifier.hpp"
  71 #include "utilities/ostream.hpp"
  72 #ifdef COMPILER1
  73 #include "c1/c1_Runtime1.hpp"
  74 #include "c1/c1_LIRAssembler.hpp"
  75 #include "gc/shared/c1/barrierSetC1.hpp"
  76 #include "gc/g1/c1/g1BarrierSetC1.hpp"
  77 #include "gc/z/c1/zBarrierSetC1.hpp"
  78 #endif
  79 #ifdef COMPILER2
  80 #include "opto/runtime.hpp"
  81 #endif
  82 #if INCLUDE_JVMCI
  83 #include "jvmci/jvmci.hpp"
  84 #endif
  85 
  86 #include <sys/stat.h>
  87 #include <errno.h>
  88 
  89 #ifndef O_BINARY       // if defined (Win32) use binary files.
  90 #define O_BINARY 0     // otherwise do nothing.
  91 #endif
  92 
  93 static elapsedTimer _t_totalLoad;
  94 static elapsedTimer _t_totalRegister;
  95 static elapsedTimer _t_totalFind;
  96 static elapsedTimer _t_totalStore;
  97 
  98 SCCache* SCCache::_cache = nullptr;
  99 
 100 static bool enable_timers() {
 101   return CITime || log_is_enabled(Info, init);
 102 }
 103 
 104 static void exit_vm_on_load_failure() {
 105   // Treat SCC warnings as error when RequireSharedSpaces is on.
 106   if (RequireSharedSpaces) {
 107     vm_exit_during_initialization("Unable to used startup cached code.", nullptr);
 108   }
 109 }
 110 
 111 static void exit_vm_on_store_failure() {
 112   // Treat SCC warnings as error when RequireSharedSpaces is on.
 113   if (RequireSharedSpaces) {
 114     tty->print_cr("Unable to create startup cached code.");
 115     // Failure during AOT code caching, we don't want to dump core
 116     vm_abort(false);
 117   }
 118 }
 119 void SCCache::initialize() {
 120   if (LoadCachedCode && !UseSharedSpaces) {
 121     return;
 122   }
 123   if (StoreCachedCode || LoadCachedCode) {
 124     if (FLAG_IS_DEFAULT(ClassInitBarrierMode)) {
 125       FLAG_SET_DEFAULT(ClassInitBarrierMode, 1);
 126     }
 127   } else if (ClassInitBarrierMode > 0) {
 128     log_info(scc, init)("Set ClassInitBarrierMode to 0 because StoreCachedCode and LoadCachedCode are false.");
 129     FLAG_SET_DEFAULT(ClassInitBarrierMode, 0);
 130   }
 131   if ((LoadCachedCode || StoreCachedCode) && CachedCodeFile != nullptr) {
 132     const int len = (int)strlen(CachedCodeFile);
 133     // cache file path
 134     char* path  = NEW_C_HEAP_ARRAY(char, len+1, mtCode);
 135     memcpy(path, CachedCodeFile, len);
 136     path[len] = '\0';
 137     if (!open_cache(path)) {
 138       exit_vm_on_load_failure();
 139       return;
 140     }
 141     if (StoreCachedCode) {
 142       FLAG_SET_DEFAULT(FoldStableValues, false);
 143       FLAG_SET_DEFAULT(ForceUnreachable, true);
 144     }
 145     FLAG_SET_DEFAULT(DelayCompilerStubsGeneration, false);
 146   }
 147 }
 148 
 149 void SCCache::init2() {
 150   if (!is_on()) {
 151     return;
 152   }
 153   // After Universe initialized
 154   BarrierSet* bs = BarrierSet::barrier_set();
 155   if (bs->is_a(BarrierSet::CardTableBarrierSet)) {
 156     address byte_map_base = ci_card_table_address_as<address>();
 157     if (is_on_for_write() && !external_word_Relocation::can_be_relocated(byte_map_base)) {
 158       // Bail out since we can't encode card table base address with relocation
 159       log_warning(scc, init)("Can't create Startup Code Cache because card table base address is not relocatable: " INTPTR_FORMAT, p2i(byte_map_base));
 160       close();
 161       exit_vm_on_load_failure();
 162     }
 163   }
 164   if (!verify_vm_config()) {
 165     close();
 166     exit_vm_on_load_failure();
 167   }
 168 }
 169 
 170 void SCCache::print_timers_on(outputStream* st) {
 171   if (LoadCachedCode) {
 172     st->print_cr ("    SC Load Time:         %7.3f s", _t_totalLoad.seconds());
 173     st->print_cr ("      nmethod register:     %7.3f s", _t_totalRegister.seconds());
 174     st->print_cr ("      find cached code:     %7.3f s", _t_totalFind.seconds());
 175   }
 176   if (StoreCachedCode) {
 177     st->print_cr ("    SC Store Time:        %7.3f s", _t_totalStore.seconds());
 178   }
 179 }
 180 
 181 bool SCCache::is_C3_on() {
 182 #if INCLUDE_JVMCI
 183   if (UseJVMCICompiler) {
 184     return (StoreCachedCode || LoadCachedCode) && UseC2asC3;
 185   }
 186 #endif
 187   return false;
 188 }
 189 
 190 bool SCCache::is_code_load_thread_on() {
 191   return UseCodeLoadThread && LoadCachedCode;
 192 }
 193 
 194 bool SCCache::gen_preload_code(ciMethod* m, int entry_bci) {
 195   VM_ENTRY_MARK;
 196   return (entry_bci == InvocationEntryBci) && is_on() && _cache->gen_preload_code() &&
 197          CDSAccess::can_generate_cached_code(m->get_Method());
 198 }
 199 
 200 static void print_helper(nmethod* nm, outputStream* st) {
 201   SCCache::iterate([&](SCCEntry* e) {
 202     if (e->method() == nm->method()) {
 203       ResourceMark rm;
 204       stringStream ss;
 205       ss.print("A%s%d", (e->for_preload() ? "P" : ""), e->comp_level());
 206       if (e->decompile() > 0) {
 207         ss.print("+D%d", e->decompile());
 208       }
 209       ss.print("[%s%s%s]",
 210                (e->is_loaded()   ? "L" : ""),
 211                (e->load_fail()   ? "F" : ""),
 212                (e->not_entrant() ? "I" : ""));
 213       ss.print("#%d", e->comp_id());
 214 
 215       st->print(" %s", ss.freeze());
 216     }
 217   });
 218 }
 219 
 220 void SCCache::close() {
 221   if (is_on()) {
 222     if (SCCache::is_on_for_read()) {
 223       LogStreamHandle(Info, init) log;
 224       if (log.is_enabled()) {
 225         log.print_cr("Startup Code Cache statistics (when closed): ");
 226         SCCache::print_statistics_on(&log);
 227         log.cr();
 228         SCCache::print_timers_on(&log);
 229 
 230         LogStreamHandle(Info, scc, init) log1;
 231         if (log1.is_enabled()) {
 232           SCCache::print_unused_entries_on(&log1);
 233         }
 234 
 235         LogStreamHandle(Info, scc, codecache) info_scc;
 236         if (info_scc.is_enabled()) {
 237           NMethodIterator iter(NMethodIterator::all);
 238           while (iter.next()) {
 239             nmethod* nm = iter.method();
 240             if (nm->is_in_use() && !nm->is_native_method() && !nm->is_osr_method()) {
 241               info_scc.print("%5d:%c%c%c%d:", nm->compile_id(),
 242                              (nm->method()->is_shared() ? 'S' : ' '),
 243                              (nm->is_scc() ? 'A' : ' '),
 244                              (nm->preloaded() ? 'P' : ' '),
 245                              nm->comp_level());
 246               print_helper(nm, &info_scc);
 247               info_scc.print(": ");
 248               CompileTask::print(&info_scc, nm, nullptr, true /*short_form*/);
 249 
 250               LogStreamHandle(Debug, scc, codecache) debug_scc;
 251               if (debug_scc.is_enabled()) {
 252                 MethodTrainingData* mtd = MethodTrainingData::lookup_for(nm->method());
 253                 if (mtd != nullptr) {
 254                   mtd->iterate_all_compiles([&](CompileTrainingData* ctd) {
 255                     debug_scc.print("     CTD: "); ctd->print_on(&debug_scc); debug_scc.cr();
 256                   });
 257                 }
 258               }
 259             }
 260           }
 261         }
 262       }
 263     }
 264 
 265     delete _cache; // Free memory
 266     _cache = nullptr;
 267   }
 268 }
 269 
 270 void SCCache::invalidate(SCCEntry* entry) {
 271   // This could be concurent execution
 272   if (entry != nullptr && is_on()) { // Request could come after cache is closed.
 273     _cache->invalidate_entry(entry);
 274   }
 275 }
 276 
 277 bool SCCache::is_loaded(SCCEntry* entry) {
 278   if (is_on() && _cache->cache_buffer() != nullptr) {
 279     return (uint)((char*)entry - _cache->cache_buffer()) < _cache->load_size();
 280   }
 281   return false;
 282 }
 283 
 284 void SCCache::preload_code(JavaThread* thread) {
 285   if ((ClassInitBarrierMode == 0) || !is_on_for_read()) {
 286     return;
 287   }
 288   if ((DisableCachedCode & (1 << 3)) != 0) {
 289     return; // no preloaded code (level 5);
 290   }
 291   _cache->preload_startup_code(thread);
 292 }
 293 
 294 SCCEntry* SCCache::find_code_entry(const methodHandle& method, uint comp_level) {
 295   switch (comp_level) {
 296     case CompLevel_simple:
 297       if ((DisableCachedCode & (1 << 0)) != 0) {
 298         return nullptr;
 299       }
 300       break;
 301     case CompLevel_limited_profile:
 302       if ((DisableCachedCode & (1 << 1)) != 0) {
 303         return nullptr;
 304       }
 305       break;
 306     case CompLevel_full_optimization:
 307       if ((DisableCachedCode & (1 << 2)) != 0) {
 308         return nullptr;
 309       }
 310       break;
 311 
 312     default: return nullptr; // Level 1, 2, and 4 only
 313   }
 314   TraceTime t1("SC total find code time", &_t_totalFind, enable_timers(), false);
 315   if (is_on() && _cache->cache_buffer() != nullptr) {
 316     MethodData* md = method->method_data();
 317     uint decomp = (md == nullptr) ? 0 : md->decompile_count();
 318 
 319     ResourceMark rm;
 320     const char* target_name = method->name_and_sig_as_C_string();
 321     uint hash = java_lang_String::hash_code((const jbyte*)target_name, (int)strlen(target_name));
 322     SCCEntry* entry = _cache->find_entry(SCCEntry::Code, hash, comp_level, decomp);
 323     if (entry == nullptr) {
 324       log_info(scc, nmethod)("Missing entry for '%s' (comp_level %d, decomp: %d, hash: " UINT32_FORMAT_X_0 ")", target_name, (uint)comp_level, decomp, hash);
 325 #ifdef ASSERT
 326     } else {
 327       uint name_offset = entry->offset() + entry->name_offset();
 328       uint name_size   = entry->name_size(); // Includes '/0'
 329       const char* name = _cache->cache_buffer() + name_offset;
 330       if (strncmp(target_name, name, name_size) != 0) {
 331         assert(false, "SCA: saved nmethod's name '%s' is different from '%s', hash: " UINT32_FORMAT_X_0, name, target_name, hash);
 332       }
 333 #endif
 334     }
 335 
 336     DirectiveSet* directives = DirectivesStack::getMatchingDirective(method, nullptr);
 337     if (directives->IgnorePrecompiledOption) {
 338       LogStreamHandle(Info, scc, compilation) log;
 339       if (log.is_enabled()) {
 340         log.print("Ignore cached code entry on level %d for ", comp_level);
 341         method->print_value_on(&log);
 342       }
 343       return nullptr;
 344     }
 345 
 346     return entry;
 347   }
 348   return nullptr;
 349 }
 350 
 351 void SCCache::add_C_string(const char* str) {
 352   if (is_on_for_write()) {
 353     _cache->add_new_C_string(str);
 354   }
 355 }
 356 
 357 bool SCCache::allow_const_field(ciConstant& value) {
 358   return !is_on() || !StoreCachedCode // Restrict only when we generate cache
 359         // Can not trust primitive too   || !is_reference_type(value.basic_type())
 360         // May disable this too for now  || is_reference_type(value.basic_type()) && value.as_object()->should_be_constant()
 361         ;
 362 }
 363 
 364 bool SCCache::open_cache(const char* cache_path) {
 365   if (LoadCachedCode) {
 366     log_info(scc)("Trying to load Startup Code Cache '%s'", cache_path);
 367     struct stat st;
 368     if (os::stat(cache_path, &st) != 0) {
 369       log_warning(scc, init)("Specified Startup Code Cache file not found '%s'", cache_path);
 370       return false;
 371     } else if ((st.st_mode & S_IFMT) != S_IFREG) {
 372       log_warning(scc, init)("Specified Startup Code Cache is not file '%s'", cache_path);
 373       return false;
 374     }
 375     int fd = os::open(cache_path, O_RDONLY | O_BINARY, 0);
 376     if (fd < 0) {
 377       if (errno == ENOENT) {
 378         log_warning(scc, init)("Specified Startup Code Cache file not found '%s'", cache_path);
 379       } else {
 380         log_warning(scc, init)("Failed to open Startup Code Cache file '%s': (%s)", cache_path, os::strerror(errno));
 381       }
 382       return false;
 383     } else {
 384       log_info(scc, init)("Opened for read Startup Code Cache '%s'", cache_path);
 385     }
 386     SCCache* cache = new SCCache(cache_path, fd, (uint)st.st_size);
 387     bool failed = cache->failed();
 388     if (::close(fd) < 0) {
 389       log_warning(scc)("Failed to close for read Startup Code Cache file '%s'", cache_path);
 390       failed = true;
 391     }
 392     if (failed) {
 393       delete cache;
 394       _cache = nullptr;
 395       return false;
 396     }
 397     _cache = cache;
 398   }
 399   if (_cache == nullptr && StoreCachedCode) {
 400     SCCache* cache = new SCCache(cache_path, -1 /* fd */, 0 /* size */);
 401     if (cache->failed()) {
 402       delete cache;
 403       _cache = nullptr;
 404       return false;
 405     }
 406     _cache = cache;
 407   }
 408   return true;
 409 }
 410 
 411 class CachedCodeDirectory {
 412 public:
 413   int _some_number;
 414   InstanceKlass* _some_klass;
 415   size_t _my_data_length;
 416   void* _my_data;
 417 };
 418 
 419 // Skeleton code for including cached code in CDS:
 420 //
 421 // [1] Use CachedCodeDirectory to keep track of all of data related to cached code.
 422 //     E.g., you can build a hashtable to record what methods have been archived.
 423 //
 424 // [2] Memory for all data for cached code, including CachedCodeDirectory, should be
 425 //     allocated using CDSAccess::allocate_from_code_cache().
 426 //
 427 // [3] CachedCodeDirectory must be the very first allocation.
 428 //
 429 // [4] Two kinds of pointer can be stored:
 430 //     - A pointer p that points to metadata. CDSAccess::can_generate_cached_code(p) must return true.
 431 //     - A pointer to a buffer returned by CDSAccess::allocate_from_code_cache().
 432 //       (It's OK to point to an interior location within this buffer).
 433 //     Such pointers must be stored using CDSAccess::set_pointer()
 434 //
 435 // The buffers allocated by CDSAccess::allocate_from_code_cache() are in a contiguous region. At runtime, this
 436 // region is mapped to the beginning of the CodeCache (see _cds_code_space in codeCache.cpp). All the pointers
 437 // in this buffer are relocated as necessary (e.g., to account for the runtime location of the CodeCache).
 438 //
 439 // Example:
 440 //
 441 // # make sure hw.cds doesn't exist, so that it's regenerated (1.5 step training)
 442 // $ rm -f hw.cds; java -Xlog:cds,scc::uptime,tags,pid -XX:CacheDataStore=hw.cds -cp ~/tmp/HelloWorld.jar HelloWorld
 443 //
 444 // # After training is finish, hw.cds should contain a CachedCodeDirectory. You can see the effect of relocation
 445 // # from the [scc] log.
 446 // $ java -Xlog:cds,scc -XX:CacheDataStore=hw.cds -cp ~/tmp/HelloWorld.jar HelloWorld
 447 // [0.016s][info][scc] new workflow: cached code mapped at 0x7fef97ebc000
 448 // [0.016s][info][scc] _cached_code_directory->_some_klass     = 0x800009ca8 (java.lang.String)
 449 // [0.016s][info][scc] _cached_code_directory->_some_number    = 0
 450 // [0.016s][info][scc] _cached_code_directory->_my_data_length = 0
 451 // [0.016s][info][scc] _cached_code_directory->_my_data        = 0x7fef97ebc020 (32 bytes offset from base)
 452 //
 453 // The 1.5 step training may be hard to debug. If you want to run in a debugger, run the above training step
 454 // with an additional "-XX:+CDSManualFinalImage" command-line argument.
 455 
 456 // This is always at the very beginning of the mmaped CDS "cc" (cached code) region
 457 static CachedCodeDirectory* _cached_code_directory = nullptr;
 458 
 459 #if INCLUDE_CDS_JAVA_HEAP
 460 void SCCache::new_workflow_start_writing_cache() {
 461   CachedCodeDirectory* dir = (CachedCodeDirectory*)CDSAccess::allocate_from_code_cache(sizeof(CachedCodeDirectory));
 462   _cached_code_directory = dir;
 463 
 464   CDSAccess::set_pointer(&dir->_some_klass, vmClasses::String_klass());
 465 
 466   size_t n = 120;
 467   void* d = (void*)CDSAccess::allocate_from_code_cache(n);
 468   CDSAccess::set_pointer(&dir->_my_data, d);
 469 }
 470 
 471 void SCCache::new_workflow_end_writing_cache() {
 472 
 473 }
 474 
 475 void SCCache::new_workflow_load_cache() {
 476   void* ptr = CodeCache::map_cached_code();
 477   if (ptr != nullptr) {
 478     // At this point:
 479     // - CodeCache::initialize_heaps() has finished.
 480     // - CDS archive is fully mapped ("metadata", "heap" and "cached_code" regions are mapped)
 481     // - All pointers in the mapped CDS regions are relocated.
 482     // - CDSAccess::get_archived_object() works.
 483     ResourceMark rm;
 484     _cached_code_directory = (CachedCodeDirectory*)ptr;
 485     InstanceKlass* k = _cached_code_directory->_some_klass;
 486     log_info(scc)("new workflow: cached code mapped at %p", ptr);
 487     log_info(scc)("_cached_code_directory->_some_klass     = %p (%s)", k, k->external_name());
 488     log_info(scc)("_cached_code_directory->_some_number    = %d", _cached_code_directory->_some_number);
 489     log_info(scc)("_cached_code_directory->_my_data_length = %zu", _cached_code_directory->_my_data_length);
 490     log_info(scc)("_cached_code_directory->_my_data        = %p (%zu bytes offset from base)", _cached_code_directory->_my_data,
 491                   pointer_delta((address)_cached_code_directory->_my_data, (address)_cached_code_directory, 1));
 492   }
 493 }
 494 #endif // INCLUDE_CDS_JAVA_HEAP
 495 
 496 #define DATA_ALIGNMENT HeapWordSize
 497 
 498 SCCache::SCCache(const char* cache_path, int fd, uint load_size) {
 499   _load_header = nullptr;
 500   _cache_path = cache_path;
 501   _for_read  = LoadCachedCode;
 502   _for_write = StoreCachedCode;
 503   _load_size = load_size;
 504   _store_size = 0;
 505   _write_position = 0;
 506   _closing  = false;
 507   _failed = false;
 508   _lookup_failed = false;
 509   _table = nullptr;
 510   _load_entries = nullptr;
 511   _store_entries  = nullptr;
 512   _C_strings_buf  = nullptr;
 513   _load_buffer = nullptr;
 514   _store_buffer = nullptr;
 515   _C_load_buffer = nullptr;
 516   _C_store_buffer = nullptr;
 517   _store_entries_cnt = 0;
 518   _gen_preload_code = false;
 519   _for_preload = false;       // changed while storing entry data
 520   _has_clinit_barriers = false;
 521 
 522   _compile_id = 0;
 523   _comp_level = 0;
 524 
 525   _use_meta_ptrs = UseSharedSpaces ? UseMetadataPointers : false;
 526 
 527   // Read header at the begining of cache
 528   uint header_size = sizeof(SCCHeader);
 529   if (_for_read) {
 530     // Read cache
 531     _C_load_buffer = NEW_C_HEAP_ARRAY(char, load_size + DATA_ALIGNMENT, mtCode);
 532     _load_buffer = align_up(_C_load_buffer, DATA_ALIGNMENT);
 533     uint n = (uint)::read(fd, _load_buffer, load_size);
 534     if (n != load_size) {
 535       log_warning(scc, init)("Failed to read %d bytes at address " INTPTR_FORMAT " from Startup Code Cache file '%s'", load_size, p2i(_load_buffer), _cache_path);
 536       set_failed();
 537       return;
 538     }
 539     log_info(scc, init)("Read %d bytes at address " INTPTR_FORMAT " from Startup Code Cache '%s'", load_size, p2i(_load_buffer), _cache_path);
 540 
 541     _load_header = (SCCHeader*)addr(0);
 542     const char* scc_jvm_version = addr(_load_header->jvm_version_offset());
 543     if (strncmp(scc_jvm_version, VM_Version::internal_vm_info_string(), strlen(scc_jvm_version)) != 0) {
 544       log_warning(scc, init)("Disable Startup Code Cache: JVM version '%s' recorded in '%s' does not match current version '%s'", scc_jvm_version, _cache_path, VM_Version::internal_vm_info_string());
 545       set_failed();
 546       return;
 547     }
 548     if (!_load_header->verify_config(_cache_path, load_size)) {
 549       set_failed();
 550       return;
 551     }
 552     log_info(scc, init)("Read header from Startup Code Cache '%s'", cache_path);
 553     if (_load_header->has_meta_ptrs()) {
 554       assert(UseSharedSpaces, "should be verified already");
 555       _use_meta_ptrs = true; // Regardless UseMetadataPointers
 556       UseMetadataPointers = true;
 557     }
 558     // Read strings
 559     load_strings();
 560   }
 561   if (_for_write) {
 562     _gen_preload_code = _use_meta_ptrs && (ClassInitBarrierMode > 0);
 563 
 564     _C_store_buffer = NEW_C_HEAP_ARRAY(char, CachedCodeMaxSize + DATA_ALIGNMENT, mtCode);
 565     _store_buffer = align_up(_C_store_buffer, DATA_ALIGNMENT);
 566     // Entries allocated at the end of buffer in reverse (as on stack).
 567     _store_entries = (SCCEntry*)align_up(_C_store_buffer + CachedCodeMaxSize, DATA_ALIGNMENT);
 568     log_info(scc, init)("Allocated store buffer at address " INTPTR_FORMAT " of size %d", p2i(_store_buffer), CachedCodeMaxSize);
 569   }
 570   _table = new SCAddressTable();
 571 }
 572 
 573 void SCCache::init_table() {
 574   SCCache* cache = SCCache::cache();
 575   if (cache != nullptr && cache->_table != nullptr) {
 576     cache->_table->init();
 577   }
 578 }
 579 
 580 void SCCache::init_opto_table() {
 581   SCCache* cache = SCCache::cache();
 582   if (cache != nullptr && cache->_table != nullptr) {
 583     cache->_table->init_opto();
 584   }
 585 }
 586 
 587 void SCCache::init_c1_table() {
 588   SCCache* cache = SCCache::cache();
 589   if (cache != nullptr && cache->_table != nullptr) {
 590     cache->_table->init_c1();
 591   }
 592 }
 593 
 594 void SCConfig::record(bool use_meta_ptrs) {
 595   _flags = 0;
 596   if (use_meta_ptrs) {
 597     _flags |= metadataPointers;
 598   }
 599 #ifdef ASSERT
 600   _flags |= debugVM;
 601 #endif
 602   if (UseCompressedOops) {
 603     _flags |= compressedOops;
 604   }
 605   if (UseCompressedClassPointers) {
 606     _flags |= compressedClassPointers;
 607   }
 608   if (UseTLAB) {
 609     _flags |= useTLAB;
 610   }
 611   if (JavaAssertions::systemClassDefault()) {
 612     _flags |= systemClassAssertions;
 613   }
 614   if (JavaAssertions::userClassDefault()) {
 615     _flags |= userClassAssertions;
 616   }
 617   if (EnableContended) {
 618     _flags |= enableContendedPadding;
 619   }
 620   if (RestrictContended) {
 621     _flags |= restrictContendedPadding;
 622   }
 623   _compressedOopShift    = CompressedOops::shift();
 624   _compressedKlassShift  = CompressedKlassPointers::shift();
 625   _contendedPaddingWidth = ContendedPaddingWidth;
 626   _objectAlignment       = ObjectAlignmentInBytes;
 627   _gc                    = (uint)Universe::heap()->kind();
 628 }
 629 
 630 bool SCConfig::verify(const char* cache_path) const {
 631 #ifdef ASSERT
 632   if ((_flags & debugVM) == 0) {
 633     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created by product VM, it can't be used by debug VM", cache_path);
 634     return false;
 635   }
 636 #else
 637   if ((_flags & debugVM) != 0) {
 638     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created by debug VM, it can't be used by product VM", cache_path);
 639     return false;
 640   }
 641 #endif
 642 
 643   CollectedHeap::Name scc_gc = (CollectedHeap::Name)_gc;
 644   if (scc_gc != Universe::heap()->kind()) {
 645     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with different GC: %s vs current %s", cache_path, GCConfig::hs_err_name(scc_gc), GCConfig::hs_err_name());
 646     return false;
 647   }
 648 
 649   if (((_flags & compressedOops) != 0) != UseCompressedOops) {
 650     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with UseCompressedOops = %s", cache_path, UseCompressedOops ? "false" : "true");
 651     return false;
 652   }
 653   if (((_flags & compressedClassPointers) != 0) != UseCompressedClassPointers) {
 654     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with UseCompressedClassPointers = %s", cache_path, UseCompressedClassPointers ? "false" : "true");
 655     return false;
 656   }
 657 
 658   if (((_flags & systemClassAssertions) != 0) != JavaAssertions::systemClassDefault()) {
 659     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with JavaAssertions::systemClassDefault() = %s", cache_path, JavaAssertions::systemClassDefault() ? "disabled" : "enabled");
 660     return false;
 661   }
 662   if (((_flags & userClassAssertions) != 0) != JavaAssertions::userClassDefault()) {
 663     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with JavaAssertions::userClassDefault() = %s", cache_path, JavaAssertions::userClassDefault() ? "disabled" : "enabled");
 664     return false;
 665   }
 666 
 667   if (((_flags & enableContendedPadding) != 0) != EnableContended) {
 668     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with EnableContended = %s", cache_path, EnableContended ? "false" : "true");
 669     return false;
 670   }
 671   if (((_flags & restrictContendedPadding) != 0) != RestrictContended) {
 672     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with RestrictContended = %s", cache_path, RestrictContended ? "false" : "true");
 673     return false;
 674   }
 675   if (_compressedOopShift != (uint)CompressedOops::shift()) {
 676     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with CompressedOops::shift() = %d vs current %d", cache_path, _compressedOopShift, CompressedOops::shift());
 677     return false;
 678   }
 679   if (_compressedKlassShift != (uint)CompressedKlassPointers::shift()) {
 680     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with CompressedKlassPointers::shift() = %d vs current %d", cache_path, _compressedKlassShift, CompressedKlassPointers::shift());
 681     return false;
 682   }
 683   if (_contendedPaddingWidth != (uint)ContendedPaddingWidth) {
 684     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with ContendedPaddingWidth = %d vs current %d", cache_path, _contendedPaddingWidth, ContendedPaddingWidth);
 685     return false;
 686   }
 687   if (_objectAlignment != (uint)ObjectAlignmentInBytes) {
 688     log_warning(scc, init)("Disable Startup Code Cache: '%s' was created with ObjectAlignmentInBytes = %d vs current %d", cache_path, _objectAlignment, ObjectAlignmentInBytes);
 689     return false;
 690   }
 691   return true;
 692 }
 693 
 694 bool SCCHeader::verify_config(const char* cache_path, uint load_size) const {
 695   if (_version != SCC_VERSION) {
 696     log_warning(scc, init)("Disable Startup Code Cache: different SCC version %d vs %d recorded in '%s'", SCC_VERSION, _version, cache_path);
 697     return false;
 698   }
 699   if (_cache_size != load_size) {
 700     log_warning(scc, init)("Disable Startup Code Cache: different cached code size %d vs %d recorded in '%s'", load_size, _cache_size, cache_path);
 701     return false;
 702   }
 703   if (has_meta_ptrs() && !UseSharedSpaces) {
 704     log_warning(scc, init)("Disable Startup Cached Code: '%s' contains metadata pointers but CDS is off", cache_path);
 705     return false;
 706   }
 707   return true;
 708 }
 709 
 710 volatile int SCCache::_reading_nmethod = 0;
 711 
 712 SCCache::~SCCache() {
 713   if (_closing) {
 714     return; // Already closed
 715   }
 716   // Stop any further access to cache.
 717   // Checked on entry to load_nmethod() and store_nmethod().
 718   _closing = true;
 719   if (_for_read && _reading_nmethod > 0) {
 720     // Wait for all load_nmethod() finish.
 721     // TODO: may be have new separate locker for SCA.
 722     MonitorLocker locker(Compilation_lock, Mutex::_no_safepoint_check_flag);
 723     while (_reading_nmethod > 0) {
 724       locker.wait(10); // Wait 10 ms
 725     }
 726   }
 727   // Prevent writing code into cache while we are closing it.
 728   // This lock held by ciEnv::register_method() which calls store_nmethod().
 729   MutexLocker ml(Compile_lock);
 730   if (for_write()) { // Finalize cache
 731     finish_write();
 732   }
 733   FREE_C_HEAP_ARRAY(char, _cache_path);
 734   if (_C_load_buffer != nullptr) {
 735     FREE_C_HEAP_ARRAY(char, _C_load_buffer);
 736     _C_load_buffer = nullptr;
 737     _load_buffer = nullptr;
 738   }
 739   if (_C_store_buffer != nullptr) {
 740     FREE_C_HEAP_ARRAY(char, _C_store_buffer);
 741     _C_store_buffer = nullptr;
 742     _store_buffer = nullptr;
 743   }
 744   if (_table != nullptr) {
 745     delete _table;
 746     _table = nullptr;
 747   }
 748 }
 749 
 750 SCCache* SCCache::open_for_read() {
 751   if (SCCache::is_on_for_read()) {
 752     return SCCache::cache();
 753   }
 754   return nullptr;
 755 }
 756 
 757 SCCache* SCCache::open_for_write() {
 758   if (SCCache::is_on_for_write()) {
 759     SCCache* cache = SCCache::cache();
 760     cache->clear_lookup_failed(); // Reset bit
 761     return cache;
 762   }
 763   return nullptr;
 764 }
 765 
 766 void copy_bytes(const char* from, address to, uint size) {
 767   assert(size > 0, "sanity");
 768   bool by_words = true;
 769   if ((size > 2 * HeapWordSize) && (((intptr_t)from | (intptr_t)to) & (HeapWordSize - 1)) == 0) {
 770     // Use wordwise copies if possible:
 771     Copy::disjoint_words((HeapWord*)from,
 772                          (HeapWord*)to,
 773                          ((size_t)size + HeapWordSize-1) / HeapWordSize);
 774   } else {
 775     by_words = false;
 776     Copy::conjoint_jbytes(from, to, (size_t)size);
 777   }
 778   log_trace(scc)("Copied %d bytes as %s from " INTPTR_FORMAT " to " INTPTR_FORMAT, size, (by_words ? "HeapWord" : "bytes"), p2i(from), p2i(to));
 779 }
 780 
 781 void SCCReader::set_read_position(uint pos) {
 782   if (pos == _read_position) {
 783     return;
 784   }
 785   assert(pos < _cache->load_size(), "offset:%d >= file size:%d", pos, _cache->load_size());
 786   _read_position = pos;
 787 }
 788 
 789 bool SCCache::set_write_position(uint pos) {
 790   if (pos == _write_position) {
 791     return true;
 792   }
 793   if (_store_size < _write_position) {
 794     _store_size = _write_position; // Adjust during write
 795   }
 796   assert(pos < _store_size, "offset:%d >= file size:%d", pos, _store_size);
 797   _write_position = pos;
 798   return true;
 799 }
 800 
 801 static char align_buffer[256] = { 0 };
 802 
 803 bool SCCache::align_write() {
 804   // We are not executing code from cache - we copy it by bytes first.
 805   // No need for big alignment (or at all).
 806   uint padding = DATA_ALIGNMENT - (_write_position & (DATA_ALIGNMENT - 1));
 807   if (padding == DATA_ALIGNMENT) {
 808     return true;
 809   }
 810   uint n = write_bytes((const void*)&align_buffer, padding);
 811   if (n != padding) {
 812     return false;
 813   }
 814   log_trace(scc)("Adjust write alignment in Startup Code Cache '%s'", _cache_path);
 815   return true;
 816 }
 817 
 818 uint SCCache::write_bytes(const void* buffer, uint nbytes) {
 819   assert(for_write(), "Code Cache file is not created");
 820   if (nbytes == 0) {
 821     return 0;
 822   }
 823   uint new_position = _write_position + nbytes;
 824   if (new_position >= (uint)((char*)_store_entries - _store_buffer)) {
 825     log_warning(scc)("Failed to write %d bytes at offset %d to Startup Code Cache file '%s'. Increase CachedCodeMaxSize.",
 826                      nbytes, _write_position, _cache_path);
 827     set_failed();
 828     exit_vm_on_store_failure();
 829     return 0;
 830   }
 831   copy_bytes((const char* )buffer, (address)(_store_buffer + _write_position), nbytes);
 832   log_trace(scc)("Wrote %d bytes at offset %d to Startup Code Cache '%s'", nbytes, _write_position, _cache_path);
 833   _write_position += nbytes;
 834   if (_store_size < _write_position) {
 835     _store_size = _write_position;
 836   }
 837   return nbytes;
 838 }
 839 
 840 void SCCEntry::update_method_for_writing() {
 841   if (_method != nullptr) {
 842     _method = CDSAccess::method_in_cached_code(_method);
 843   }
 844 }
 845 
 846 void SCCEntry::print(outputStream* st) const {
 847   st->print_cr(" SCA entry " INTPTR_FORMAT " [kind: %d, id: " UINT32_FORMAT_X_0 ", offset: %d, size: %d, comp_level: %d, comp_id: %d, decompiled: %d, %s%s%s%s]",
 848                p2i(this), (int)_kind, _id, _offset, _size, _comp_level, _comp_id, _decompile,
 849                (_not_entrant? "not_entrant" : "entrant"),
 850                (_loaded ? ", loaded" : ""),
 851                (_has_clinit_barriers ? ", has_clinit_barriers" : ""),
 852                (_for_preload ? ", for_preload" : ""));
 853 }
 854 
 855 void* SCCEntry::operator new(size_t x, SCCache* cache) {
 856   return (void*)(cache->add_entry());
 857 }
 858 
 859 bool skip_preload(methodHandle mh) {
 860   if (!mh->method_holder()->is_loaded()) {
 861     return true;
 862   }
 863   DirectiveSet* directives = DirectivesStack::getMatchingDirective(mh, nullptr);
 864   if (directives->DontPreloadOption) {
 865     LogStreamHandle(Info, scc, init) log;
 866     if (log.is_enabled()) {
 867       log.print("Exclude preloading code for ");
 868       mh->print_value_on(&log);
 869     }
 870     return true;
 871   }
 872   return false;
 873 }
 874 
 875 void SCCache::preload_startup_code(TRAPS) {
 876   if (CompilationPolicy::compiler_count(CompLevel_full_optimization) == 0) {
 877     // Since we reuse the CompilerBroker API to install cached code, we're required to have a JIT compiler for the
 878     // level we want (that is CompLevel_full_optimization).
 879     return;
 880   }
 881   assert(_for_read, "sanity");
 882   uint count = _load_header->entries_count();
 883   if (_load_entries == nullptr) {
 884     // Read it
 885     _search_entries = (uint*)addr(_load_header->entries_offset()); // [id, index]
 886     _load_entries = (SCCEntry*)(_search_entries + 2 * count);
 887     log_info(scc, init)("Read %d entries table at offset %d from Startup Code Cache '%s'", count, _load_header->entries_offset(), _cache_path);
 888   }
 889   uint preload_entries_count = _load_header->preload_entries_count();
 890   if (preload_entries_count > 0) {
 891     uint* entries_index = (uint*)addr(_load_header->preload_entries_offset());
 892     log_info(scc, init)("Load %d preload entries from Startup Code Cache '%s'", preload_entries_count, _cache_path);
 893     uint count = MIN2(preload_entries_count, SCLoadStop);
 894     for (uint i = SCLoadStart; i < count; i++) {
 895       uint index = entries_index[i];
 896       SCCEntry* entry = &(_load_entries[index]);
 897       if (entry->not_entrant()) {
 898         continue;
 899       }
 900       methodHandle mh(THREAD, entry->method());
 901       assert((mh.not_null() && MetaspaceShared::is_in_shared_metaspace((address)mh())), "sanity");
 902       if (skip_preload(mh)) {
 903         continue; // Exclude preloading for this method
 904       }
 905       assert(mh->method_holder()->is_loaded(), "");
 906       if (!mh->method_holder()->is_linked()) {
 907         assert(!HAS_PENDING_EXCEPTION, "");
 908         mh->method_holder()->link_class(THREAD);
 909         if (HAS_PENDING_EXCEPTION) {
 910           LogStreamHandle(Info, scc) log;
 911           if (log.is_enabled()) {
 912             ResourceMark rm;
 913             log.print("Linkage failed for %s: ", mh->method_holder()->external_name());
 914             THREAD->pending_exception()->print_value_on(&log);
 915             if (log_is_enabled(Debug, scc)) {
 916               THREAD->pending_exception()->print_on(&log);
 917             }
 918           }
 919           CLEAR_PENDING_EXCEPTION;
 920         }
 921       }
 922       if (mh->scc_entry() != nullptr) {
 923         // Second C2 compilation of the same method could happen for
 924         // different reasons without marking first entry as not entrant.
 925         continue; // Keep old entry to avoid issues
 926       }
 927       mh->set_scc_entry(entry);
 928       CompileBroker::compile_method(mh, InvocationEntryBci, CompLevel_full_optimization, methodHandle(), 0, false, CompileTask::Reason_Preload, CHECK);
 929     }
 930   }
 931 }
 932 
 933 static bool check_entry(SCCEntry::Kind kind, uint id, uint comp_level, uint decomp, SCCEntry* entry) {
 934   if (entry->kind() == kind) {
 935     assert(entry->id() == id, "sanity");
 936     if (kind != SCCEntry::Code || (!entry->not_entrant() && !entry->has_clinit_barriers() &&
 937                                   entry->comp_level() == comp_level &&
 938                                   (comp_level == CompLevel_limited_profile || entry->decompile() == decomp))) {
 939       return true; // Found
 940     }
 941   }
 942   return false;
 943 }
 944 
 945 SCCEntry* SCCache::find_entry(SCCEntry::Kind kind, uint id, uint comp_level, uint decomp) {
 946   assert(_for_read, "sanity");
 947   uint count = _load_header->entries_count();
 948   if (_load_entries == nullptr) {
 949     // Read it
 950     _search_entries = (uint*)addr(_load_header->entries_offset()); // [id, index]
 951     _load_entries = (SCCEntry*)(_search_entries + 2 * count);
 952     log_info(scc, init)("Read %d entries table at offset %d from Startup Code Cache '%s'", count, _load_header->entries_offset(), _cache_path);
 953   }
 954   // Binary search
 955   int l = 0;
 956   int h = count - 1;
 957   while (l <= h) {
 958     int mid = (l + h) >> 1;
 959     int ix = mid * 2;
 960     uint is = _search_entries[ix];
 961     if (is == id) {
 962       int index = _search_entries[ix + 1];
 963       SCCEntry* entry = &(_load_entries[index]);
 964       if (check_entry(kind, id, comp_level, decomp, entry)) {
 965         return entry; // Found
 966       }
 967       // Leaner search around (could be the same nmethod with different decompile count)
 968       for (int i = mid - 1; i >= l; i--) { // search back
 969         ix = i * 2;
 970         is = _search_entries[ix];
 971         if (is != id) {
 972           break;
 973         }
 974         index = _search_entries[ix + 1];
 975         SCCEntry* entry = &(_load_entries[index]);
 976         if (check_entry(kind, id, comp_level, decomp, entry)) {
 977           return entry; // Found
 978         }
 979       }
 980       for (int i = mid + 1; i <= h; i++) { // search forward
 981         ix = i * 2;
 982         is = _search_entries[ix];
 983         if (is != id) {
 984           break;
 985         }
 986         index = _search_entries[ix + 1];
 987         SCCEntry* entry = &(_load_entries[index]);
 988         if (check_entry(kind, id, comp_level, decomp, entry)) {
 989           return entry; // Found
 990         }
 991       }
 992       break; // Not found match (different decompile count or not_entrant state).
 993     } else if (is < id) {
 994       l = mid + 1;
 995     } else {
 996       h = mid - 1;
 997     }
 998   }
 999   return nullptr;
1000 }
1001 
1002 void SCCache::invalidate_entry(SCCEntry* entry) {
1003   assert(entry!= nullptr, "all entries should be read already");
1004   if (entry->not_entrant()) {
1005     return; // Someone invalidated it already
1006   }
1007 #ifdef ASSERT
1008   bool found = false;
1009   if (_for_read) {
1010     uint count = _load_header->entries_count();
1011     uint i = 0;
1012     for(; i < count; i++) {
1013       if (entry == &(_load_entries[i])) {
1014         break;
1015       }
1016     }
1017     found = (i < count);
1018   }
1019   if (!found && _for_write) {
1020     uint count = _store_entries_cnt;
1021     uint i = 0;
1022     for(; i < count; i++) {
1023       if (entry == &(_store_entries[i])) {
1024         break;
1025       }
1026     }
1027     found = (i < count);
1028   }
1029   assert(found, "entry should exist");
1030 #endif
1031   entry->set_not_entrant();
1032   {
1033     uint name_offset = entry->offset() + entry->name_offset();
1034     const char* name;
1035     if (SCCache::is_loaded(entry)) {
1036       name = _load_buffer + name_offset;
1037     } else {
1038       name = _store_buffer + name_offset;
1039     }
1040     uint level   = entry->comp_level();
1041     uint comp_id = entry->comp_id();
1042     uint decomp  = entry->decompile();
1043     bool clinit_brs = entry->has_clinit_barriers();
1044     log_info(scc, nmethod)("Invalidated entry for '%s' (comp_id %d, comp_level %d, decomp: %d, hash: " UINT32_FORMAT_X_0 "%s)",
1045                            name, comp_id, level, decomp, entry->id(), (clinit_brs ? ", has clinit barriers" : ""));
1046   }
1047   if (entry->next() != nullptr) {
1048     entry = entry->next();
1049     assert(entry->has_clinit_barriers(), "expecting only such entries here");
1050     invalidate_entry(entry);
1051   }
1052 }
1053 
1054 extern "C" {
1055   static int uint_cmp(const void *i, const void *j) {
1056     uint a = *(uint *)i;
1057     uint b = *(uint *)j;
1058     return a > b ? 1 : a < b ? -1 : 0;
1059   }
1060 }
1061 
1062 bool SCCache::finish_write() {
1063   if (!align_write()) {
1064     return false;
1065   }
1066   uint strings_offset = _write_position;
1067   int strings_count = store_strings();
1068   if (strings_count < 0) {
1069     return false;
1070   }
1071   if (!align_write()) {
1072     return false;
1073   }
1074   uint strings_size = _write_position - strings_offset;
1075 
1076   uint entries_count = 0; // Number of entrant (useful) code entries
1077   uint entries_offset = _write_position;
1078 
1079   uint store_count = _store_entries_cnt;
1080   if (store_count > 0) {
1081     uint header_size = (uint)align_up(sizeof(SCCHeader),  DATA_ALIGNMENT);
1082     const char* vm_version = VM_Version::internal_vm_info_string();
1083     uint vm_version_size = (uint)align_up(strlen(vm_version) + 1, DATA_ALIGNMENT);
1084     uint load_count = (_load_header != nullptr) ? _load_header->entries_count() : 0;
1085     uint code_count = store_count + load_count;
1086     uint search_count = code_count * 2;
1087     uint search_size = search_count * sizeof(uint);
1088     uint entries_size = (uint)align_up(code_count * sizeof(SCCEntry), DATA_ALIGNMENT); // In bytes
1089     uint preload_entries_cnt = 0;
1090     uint* preload_entries = NEW_C_HEAP_ARRAY(uint, code_count, mtCode);
1091     uint preload_entries_size = code_count * sizeof(uint);
1092     // _write_position should include code and strings
1093     uint code_alignment = code_count * DATA_ALIGNMENT; // We align_up code size when storing it.
1094     uint total_size = _write_position + _load_size + header_size + vm_version_size +
1095                      code_alignment + search_size + preload_entries_size + entries_size;
1096 
1097     // Create ordered search table for entries [id, index];
1098     uint* search = NEW_C_HEAP_ARRAY(uint, search_count, mtCode);
1099     char* buffer = NEW_C_HEAP_ARRAY(char, total_size + DATA_ALIGNMENT, mtCode);
1100     char* start = align_up(buffer, DATA_ALIGNMENT);
1101     char* current = start + header_size; // Skip header
1102     uint jvm_version_offset = current - start;
1103     copy_bytes(vm_version, (address)current, (uint)strlen(vm_version) + 1);
1104     current += vm_version_size;
1105 
1106     SCCEntry* entries_address = _store_entries; // Pointer to latest entry
1107     uint not_entrant_nb = 0;
1108     uint max_size = 0;
1109     // Add old entries first
1110     if (_for_read && (_load_header != nullptr)) {
1111       for(uint i = 0; i < load_count; i++) {
1112         if (_load_entries[i].load_fail()) {
1113           continue;
1114         }
1115         if (_load_entries[i].not_entrant()) {
1116           log_info(scc, exit)("Not entrant load entry id: %d, decomp: %d, hash: " UINT32_FORMAT_X_0, i, _load_entries[i].decompile(), _load_entries[i].id());
1117           not_entrant_nb++;
1118           if (_load_entries[i].for_preload()) {
1119             // Skip not entrant preload code:
1120             // we can't pre-load code which may have failing dependencies.
1121             continue;
1122           }
1123           _load_entries[i].set_entrant(); // Reset
1124         } else if (_load_entries[i].for_preload() && _load_entries[i].method() != nullptr) {
1125           // record entrant first version code for pre-loading
1126           preload_entries[preload_entries_cnt++] = entries_count;
1127         }
1128         {
1129           uint size = align_up(_load_entries[i].size(), DATA_ALIGNMENT);
1130           if (size > max_size) {
1131             max_size = size;
1132           }
1133           copy_bytes((_load_buffer + _load_entries[i].offset()), (address)current, size);
1134           _load_entries[i].set_offset(current - start); // New offset
1135           current += size;
1136           uint n = write_bytes(&(_load_entries[i]), sizeof(SCCEntry));
1137           if (n != sizeof(SCCEntry)) {
1138             FREE_C_HEAP_ARRAY(char, buffer);
1139             FREE_C_HEAP_ARRAY(uint, search);
1140             return false;
1141           }
1142           search[entries_count*2 + 0] = _load_entries[i].id();
1143           search[entries_count*2 + 1] = entries_count;
1144           entries_count++;
1145         }
1146       }
1147     }
1148     // SCCEntry entries were allocated in reverse in store buffer.
1149     // Process them in reverse order to cache first code first.
1150     for (int i = store_count - 1; i >= 0; i--) {
1151       if (entries_address[i].load_fail()) {
1152         continue;
1153       }
1154       if (entries_address[i].not_entrant()) {
1155         log_info(scc, exit)("Not entrant new entry comp_id: %d, comp_level: %d, decomp: %d, hash: " UINT32_FORMAT_X_0 "%s", entries_address[i].comp_id(), entries_address[i].comp_level(), entries_address[i].decompile(), entries_address[i].id(), (entries_address[i].has_clinit_barriers() ? ", has clinit barriers" : ""));
1156         not_entrant_nb++;
1157         if (entries_address[i].for_preload()) {
1158           // Skip not entrant preload code:
1159           // we can't pre-load code which may have failing dependencies.
1160           continue;
1161         }
1162         entries_address[i].set_entrant(); // Reset
1163       } else if (entries_address[i].for_preload() && entries_address[i].method() != nullptr) {
1164         // record entrant first version code for pre-loading
1165         preload_entries[preload_entries_cnt++] = entries_count;
1166       }
1167       {
1168         entries_address[i].set_next(nullptr); // clear pointers before storing data
1169         uint size = align_up(entries_address[i].size(), DATA_ALIGNMENT);
1170         if (size > max_size) {
1171           max_size = size;
1172         }
1173         copy_bytes((_store_buffer + entries_address[i].offset()), (address)current, size);
1174         entries_address[i].set_offset(current - start); // New offset
1175         entries_address[i].update_method_for_writing();
1176         current += size;
1177         uint n = write_bytes(&(entries_address[i]), sizeof(SCCEntry));
1178         if (n != sizeof(SCCEntry)) {
1179           FREE_C_HEAP_ARRAY(char, buffer);
1180           FREE_C_HEAP_ARRAY(uint, search);
1181           return false;
1182         }
1183         search[entries_count*2 + 0] = entries_address[i].id();
1184         search[entries_count*2 + 1] = entries_count;
1185         entries_count++;
1186       }
1187     }
1188     if (entries_count == 0) {
1189       log_info(scc, exit)("No new entires, cache files %s was not %s", _cache_path, (_for_read ? "updated" : "created"));
1190       FREE_C_HEAP_ARRAY(char, buffer);
1191       FREE_C_HEAP_ARRAY(uint, search);
1192       return true; // Nothing to write
1193     }
1194     assert(entries_count <= (store_count + load_count), "%d > (%d + %d)", entries_count, store_count, load_count);
1195     // Write strings
1196     if (strings_count > 0) {
1197       copy_bytes((_store_buffer + strings_offset), (address)current, strings_size);
1198       strings_offset = (current - start); // New offset
1199       current += strings_size;
1200     }
1201     uint preload_entries_offset = (current - start);
1202     preload_entries_size = preload_entries_cnt * sizeof(uint);
1203     if (preload_entries_size > 0) {
1204       copy_bytes((const char*)preload_entries, (address)current, preload_entries_size);
1205       current += preload_entries_size;
1206       log_info(scc, exit)("Wrote %d preload entries to Startup Code Cache '%s'", preload_entries_cnt, _cache_path);
1207     }
1208     if (preload_entries != nullptr) {
1209       FREE_C_HEAP_ARRAY(uint, preload_entries);
1210     }
1211 
1212     uint new_entries_offset = (current - start); // New offset
1213     // Sort and store search table
1214     qsort(search, entries_count, 2*sizeof(uint), uint_cmp);
1215     search_size = 2 * entries_count * sizeof(uint);
1216     copy_bytes((const char*)search, (address)current, search_size);
1217     FREE_C_HEAP_ARRAY(uint, search);
1218     current += search_size;
1219 
1220     // Write entries
1221     entries_size = entries_count * sizeof(SCCEntry); // New size
1222     copy_bytes((_store_buffer + entries_offset), (address)current, entries_size);
1223     current += entries_size;
1224     log_info(scc, exit)("Wrote %d SCCEntry entries (%d were not entrant, %d max size) to Startup Code Cache '%s'", entries_count, not_entrant_nb, max_size, _cache_path);
1225 
1226     uint size = (current - start);
1227     assert(size <= total_size, "%d > %d", size , total_size);
1228 
1229     // Finalize header
1230     SCCHeader* header = (SCCHeader*)start;
1231     header->init(jvm_version_offset, size,
1232                  (uint)strings_count, strings_offset,
1233                  entries_count, new_entries_offset,
1234                  preload_entries_cnt, preload_entries_offset,
1235                  _use_meta_ptrs);
1236     log_info(scc, init)("Wrote header to Startup Code Cache '%s'", _cache_path);
1237 
1238     // Now store to file
1239 #ifdef _WINDOWS  // On Windows, need WRITE permission to remove the file.
1240     chmod(_cache_path, _S_IREAD | _S_IWRITE);
1241 #endif
1242     // Use remove() to delete the existing file because, on Unix, this will
1243     // allow processes that have it open continued access to the file.
1244     remove(_cache_path);
1245     int fd = os::open(_cache_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
1246     if (fd < 0) {
1247       log_warning(scc, exit)("Unable to create Startup Code Cache file '%s': (%s)", _cache_path, os::strerror(errno));
1248       FREE_C_HEAP_ARRAY(char, buffer);
1249       exit_vm_on_store_failure();
1250       return false;
1251     } else {
1252       log_info(scc, exit)("Opened for write Startup Code Cache '%s'", _cache_path);
1253     }
1254     bool success = os::write(fd, start, (size_t)size);
1255     if (!success) {
1256       log_warning(scc, exit)("Failed to write %d bytes to Startup Code Cache file '%s': (%s)", size, _cache_path, os::strerror(errno));
1257       FREE_C_HEAP_ARRAY(char, buffer);
1258       exit_vm_on_store_failure();
1259       return false;
1260     }
1261     log_info(scc, exit)("Wrote %d bytes to Startup Code Cache '%s'", size, _cache_path);
1262     if (::close(fd) < 0) {
1263       log_warning(scc, exit)("Failed to close for write Startup Code Cache file '%s'", _cache_path);
1264       exit_vm_on_store_failure();
1265     } else {
1266       log_info(scc, exit)("Closed for write Startup Code Cache '%s'", _cache_path);
1267     }
1268     FREE_C_HEAP_ARRAY(char, buffer);
1269   }
1270   return true;
1271 }
1272 
1273 bool SCCache::load_stub(StubCodeGenerator* cgen, vmIntrinsicID id, const char* name, address start) {
1274   assert(start == cgen->assembler()->pc(), "wrong buffer");
1275   SCCache* cache = open_for_read();
1276   if (cache == nullptr) {
1277     return false;
1278   }
1279   SCCEntry* entry = cache->find_entry(SCCEntry::Stub, (uint)id);
1280   if (entry == nullptr) {
1281     return false;
1282   }
1283   uint entry_position = entry->offset();
1284   // Read name
1285   uint name_offset = entry->name_offset() + entry_position;
1286   uint name_size   = entry->name_size(); // Includes '/0'
1287   const char* saved_name = cache->addr(name_offset);
1288   if (strncmp(name, saved_name, (name_size - 1)) != 0) {
1289     log_warning(scc)("Saved stub's name '%s' is different from '%s' for id:%d", saved_name, name, (int)id);
1290     cache->set_failed();
1291     exit_vm_on_load_failure();
1292     return false;
1293   }
1294   log_info(scc,stubs)("Reading stub '%s' id:%d from Startup Code Cache '%s'", name, (int)id, cache->_cache_path);
1295   // Read code
1296   uint code_offset = entry->code_offset() + entry_position;
1297   uint code_size   = entry->code_size();
1298   copy_bytes(cache->addr(code_offset), start, code_size);
1299   cgen->assembler()->code_section()->set_end(start + code_size);
1300   log_info(scc,stubs)("Read stub '%s' id:%d from Startup Code Cache '%s'", name, (int)id, cache->_cache_path);
1301   return true;
1302 }
1303 
1304 bool SCCache::store_stub(StubCodeGenerator* cgen, vmIntrinsicID id, const char* name, address start) {
1305   SCCache* cache = open_for_write();
1306   if (cache == nullptr) {
1307     return false;
1308   }
1309   log_info(scc, stubs)("Writing stub '%s' id:%d to Startup Code Cache '%s'", name, (int)id, cache->_cache_path);
1310   if (!cache->align_write()) {
1311     return false;
1312   }
1313 #ifdef ASSERT
1314   CodeSection* cs = cgen->assembler()->code_section();
1315   if (cs->has_locs()) {
1316     uint reloc_count = cs->locs_count();
1317     tty->print_cr("======== write stubs code section relocations [%d]:", reloc_count);
1318     // Collect additional data
1319     RelocIterator iter(cs);
1320     while (iter.next()) {
1321       switch (iter.type()) {
1322         case relocInfo::none:
1323           break;
1324         default: {
1325           iter.print_current_on(tty);
1326           fatal("stub's relocation %d unimplemented", (int)iter.type());
1327           break;
1328         }
1329       }
1330     }
1331   }
1332 #endif
1333   uint entry_position = cache->_write_position;
1334 
1335   // Write code
1336   uint code_offset = 0;
1337   uint code_size = cgen->assembler()->pc() - start;
1338   uint n = cache->write_bytes(start, code_size);
1339   if (n != code_size) {
1340     return false;
1341   }
1342   // Write name
1343   uint name_offset = cache->_write_position - entry_position;
1344   uint name_size = (uint)strlen(name) + 1; // Includes '/0'
1345   n = cache->write_bytes(name, name_size);
1346   if (n != name_size) {
1347     return false;
1348   }
1349   uint entry_size = cache->_write_position - entry_position;
1350   SCCEntry* entry = new(cache) SCCEntry(entry_position, entry_size, name_offset, name_size,
1351                                           code_offset, code_size, 0, 0,
1352                                           SCCEntry::Stub, (uint32_t)id);
1353   log_info(scc, stubs)("Wrote stub '%s' id:%d to Startup Code Cache '%s'", name, (int)id, cache->_cache_path);
1354   return true;
1355 }
1356 
1357 Klass* SCCReader::read_klass(const methodHandle& comp_method, bool shared) {
1358   uint code_offset = read_position();
1359   uint state = *(uint*)addr(code_offset);
1360   uint init_state = (state  & 1);
1361   uint array_dim  = (state >> 1);
1362   code_offset += sizeof(int);
1363   if (_cache->use_meta_ptrs() && shared) {
1364     uint klass_offset = *(uint*)addr(code_offset);
1365     code_offset += sizeof(uint);
1366     set_read_position(code_offset);
1367     Klass* k = (Klass*)((address)SharedBaseAddress + klass_offset);
1368     if (!MetaspaceShared::is_in_shared_metaspace((address)k)) {
1369       // Something changed in CDS
1370       set_lookup_failed();
1371       log_info(scc)("Lookup failed for shared klass: " INTPTR_FORMAT " is not in CDS ", p2i((address)k));
1372       return nullptr;
1373     }
1374     assert(k->is_klass(), "sanity");
1375     ResourceMark rm;
1376     const char* comp_name = comp_method->name_and_sig_as_C_string();
1377     if (k->is_instance_klass() && !InstanceKlass::cast(k)->is_loaded()) {
1378       set_lookup_failed();
1379       log_info(scc)("%d '%s' (L%d): Lookup failed for klass %s: not loaded",
1380                        compile_id(), comp_name, comp_level(), k->external_name());
1381       return nullptr;
1382     } else
1383     // Allow not initialized klass which was uninitialized during code caching or for preload
1384     if (k->is_instance_klass() && !InstanceKlass::cast(k)->is_initialized() && (init_state == 1) && !_preload) {
1385       set_lookup_failed();
1386       log_info(scc)("%d '%s' (L%d): Lookup failed for klass %s: not initialized",
1387                        compile_id(), comp_name, comp_level(), k->external_name());
1388       return nullptr;
1389     }
1390     if (array_dim > 0) {
1391       assert(k->is_instance_klass() || k->is_typeArray_klass(), "sanity check");
1392       Klass* ak = k->array_klass_or_null(array_dim);
1393       // FIXME: what would it take to create an array class on the fly?
1394 //      Klass* ak = k->array_klass(dim, JavaThread::current());
1395 //      guarantee(JavaThread::current()->pending_exception() == nullptr, "");
1396       if (ak == nullptr) {
1397         set_lookup_failed();
1398         log_info(scc)("%d (L%d): %d-dimension array klass lookup failed: %s",
1399                          compile_id(), comp_level(), array_dim, k->external_name());
1400       }
1401       log_info(scc)("%d (L%d): Klass lookup: %s (object array)", compile_id(), comp_level(), k->external_name());
1402       return ak;
1403     } else {
1404       log_info(scc)("%d (L%d): Shared klass lookup: %s",
1405                     compile_id(), comp_level(), k->external_name());
1406       return k;
1407     }
1408   }
1409   int name_length = *(int*)addr(code_offset);
1410   code_offset += sizeof(int);
1411   const char* dest = addr(code_offset);
1412   code_offset += name_length + 1;
1413   set_read_position(code_offset);
1414   TempNewSymbol klass_sym = SymbolTable::probe(&(dest[0]), name_length);
1415   if (klass_sym == nullptr) {
1416     set_lookup_failed();
1417     log_info(scc)("%d (L%d): Probe failed for class %s",
1418                      compile_id(), comp_level(), &(dest[0]));
1419     return nullptr;
1420   }
1421   // Use class loader of compiled method.
1422   Thread* thread = Thread::current();
1423   Handle loader(thread, comp_method->method_holder()->class_loader());
1424   Handle protection_domain(thread, comp_method->method_holder()->protection_domain());
1425   Klass* k = SystemDictionary::find_instance_or_array_klass(thread, klass_sym, loader, protection_domain);
1426   assert(!thread->has_pending_exception(), "should not throw");
1427   if (k == nullptr && !loader.is_null()) {
1428     // Try default loader and domain
1429     k = SystemDictionary::find_instance_or_array_klass(thread, klass_sym, Handle(), Handle());
1430     assert(!thread->has_pending_exception(), "should not throw");
1431   }
1432   if (k != nullptr) {
1433     // Allow not initialized klass which was uninitialized during code caching
1434     if (k->is_instance_klass() && !InstanceKlass::cast(k)->is_initialized() && (init_state == 1)) {
1435       set_lookup_failed();
1436       log_info(scc)("%d (L%d): Lookup failed for klass %s: not initialized", compile_id(), comp_level(), &(dest[0]));
1437       return nullptr;
1438     }
1439     log_info(scc)("%d (L%d): Klass lookup %s", compile_id(), comp_level(), k->external_name());
1440   } else {
1441     set_lookup_failed();
1442     log_info(scc)("%d (L%d): Lookup failed for class %s", compile_id(), comp_level(), &(dest[0]));
1443     return nullptr;
1444   }
1445   return k;
1446 }
1447 
1448 Method* SCCReader::read_method(const methodHandle& comp_method, bool shared) {
1449   uint code_offset = read_position();
1450   if (_cache->use_meta_ptrs() && shared) {
1451     uint method_offset = *(uint*)addr(code_offset);
1452     code_offset += sizeof(uint);
1453     set_read_position(code_offset);
1454     Method* m = (Method*)((address)SharedBaseAddress + method_offset);
1455     if (!MetaspaceShared::is_in_shared_metaspace((address)m)) {
1456       // Something changed in CDS
1457       set_lookup_failed();
1458       log_info(scc)("Lookup failed for shared method: " INTPTR_FORMAT " is not in CDS ", p2i((address)m));
1459       return nullptr;
1460     }
1461     assert(m->is_method(), "sanity");
1462     ResourceMark rm;
1463     const char* comp_name = comp_method->name_and_sig_as_C_string();
1464     Klass* k = m->method_holder();
1465     if (!k->is_instance_klass()) {
1466       set_lookup_failed();
1467       log_info(scc)("%d '%s' (L%d): Lookup failed for holder %s: not instance klass", compile_id(), comp_name, comp_level(), k->external_name());
1468       return nullptr;
1469     } else if (!MetaspaceShared::is_in_shared_metaspace((address)k)) {
1470       set_lookup_failed();
1471       log_info(scc)("%d '%s' (L%d): Lookup failed for holder %s: not in CDS", compile_id(), comp_name, comp_level(), k->external_name());
1472       return nullptr;
1473     } else if (!InstanceKlass::cast(k)->is_loaded()) {
1474       set_lookup_failed();
1475       log_info(scc)("%d '%s' (L%d): Lookup failed for holder %s: not loaded", compile_id(), comp_name, comp_level(), k->external_name());
1476       return nullptr;
1477     } else if (!InstanceKlass::cast(k)->is_linked()) {
1478       set_lookup_failed();
1479       log_info(scc)("%d '%s' (L%d): Lookup failed for holder %s: not linked%s", compile_id(), comp_name, comp_level(), k->external_name(), (_preload ? " for code preload" : ""));
1480       return nullptr;
1481     }
1482     log_info(scc)("%d (L%d): Shared method lookup: %s", compile_id(), comp_level(), m->name_and_sig_as_C_string());
1483     return m;
1484   }
1485   int holder_length = *(int*)addr(code_offset);
1486   code_offset += sizeof(int);
1487   int name_length = *(int*)addr(code_offset);
1488   code_offset += sizeof(int);
1489   int signat_length = *(int*)addr(code_offset);
1490   code_offset += sizeof(int);
1491 
1492   const char* dest = addr(code_offset);
1493   code_offset += holder_length + 1 + name_length + 1 + signat_length + 1;
1494   set_read_position(code_offset);
1495   TempNewSymbol klass_sym = SymbolTable::probe(&(dest[0]), holder_length);
1496   if (klass_sym == nullptr) {
1497     set_lookup_failed();
1498     log_info(scc)("%d (L%d): Probe failed for class %s", compile_id(), comp_level(), &(dest[0]));
1499     return nullptr;
1500   }
1501   // Use class loader of compiled method.
1502   Thread* thread = Thread::current();
1503   Handle loader(thread, comp_method->method_holder()->class_loader());
1504   Handle protection_domain(thread, comp_method->method_holder()->protection_domain());
1505   Klass* k = SystemDictionary::find_instance_or_array_klass(thread, klass_sym, loader, protection_domain);
1506   assert(!thread->has_pending_exception(), "should not throw");
1507   if (k == nullptr && !loader.is_null()) {
1508     // Try default loader and domain
1509     k = SystemDictionary::find_instance_or_array_klass(thread, klass_sym, Handle(), Handle());
1510     assert(!thread->has_pending_exception(), "should not throw");
1511   }
1512   if (k != nullptr) {
1513     if (!k->is_instance_klass()) {
1514       set_lookup_failed();
1515       log_info(scc)("%d (L%d): Lookup failed for holder %s: not instance klass",
1516                        compile_id(), comp_level(), &(dest[0]));
1517       return nullptr;
1518     } else if (!InstanceKlass::cast(k)->is_linked()) {
1519       set_lookup_failed();
1520       log_info(scc)("%d (L%d): Lookup failed for holder %s: not linked",
1521                        compile_id(), comp_level(), &(dest[0]));
1522       return nullptr;
1523     }
1524     log_info(scc)("%d (L%d): Holder lookup: %s", compile_id(), comp_level(), k->external_name());
1525   } else {
1526     set_lookup_failed();
1527     log_info(scc)("%d (L%d): Lookup failed for holder %s",
1528                   compile_id(), comp_level(), &(dest[0]));
1529     return nullptr;
1530   }
1531   TempNewSymbol name_sym = SymbolTable::probe(&(dest[holder_length + 1]), name_length);
1532   int pos = holder_length + 1 + name_length + 1;
1533   TempNewSymbol sign_sym = SymbolTable::probe(&(dest[pos]), signat_length);
1534   if (name_sym == nullptr) {
1535     set_lookup_failed();
1536     log_info(scc)("%d (L%d): Probe failed for method name %s",
1537                      compile_id(), comp_level(), &(dest[holder_length + 1]));
1538     return nullptr;
1539   }
1540   if (sign_sym == nullptr) {
1541     set_lookup_failed();
1542     log_info(scc)("%d (L%d): Probe failed for method signature %s",
1543                      compile_id(), comp_level(), &(dest[pos]));
1544     return nullptr;
1545   }
1546   Method* m = InstanceKlass::cast(k)->find_method(name_sym, sign_sym);
1547   if (m != nullptr) {
1548     ResourceMark rm;
1549     log_info(scc)("%d (L%d): Method lookup: %s", compile_id(), comp_level(), m->name_and_sig_as_C_string());
1550   } else {
1551     set_lookup_failed();
1552     log_info(scc)("%d (L%d): Lookup failed for method %s::%s%s",
1553                      compile_id(), comp_level(), &(dest[0]), &(dest[holder_length + 1]), &(dest[pos]));
1554     return nullptr;
1555   }
1556   return m;
1557 }
1558 
1559 bool SCCache::write_klass(Klass* klass) {
1560   if (klass->is_hidden()) { // Skip such nmethod
1561     set_lookup_failed();
1562     return false;
1563   }
1564   bool can_use_meta_ptrs = _use_meta_ptrs;
1565   uint array_dim = 0;
1566   if (klass->is_objArray_klass()) {
1567     array_dim = ObjArrayKlass::cast(klass)->dimension();
1568     klass     = ObjArrayKlass::cast(klass)->bottom_klass(); // overwrites klass
1569   }
1570   uint init_state = 0;
1571   if (klass->is_instance_klass()) {
1572     InstanceKlass* ik = InstanceKlass::cast(klass);
1573     ClassLoaderData* cld = ik->class_loader_data();
1574     if (!cld->is_builtin_class_loader_data()) {
1575       set_lookup_failed();
1576       return false;
1577     }
1578     if (_for_preload && !CDSAccess::can_generate_cached_code(ik)) {
1579       _for_preload = false;
1580       // Bailout if code has clinit barriers:
1581       // method will be recompiled without them in any case
1582       if (_has_clinit_barriers) {
1583         set_lookup_failed();
1584         return false;
1585       }
1586       can_use_meta_ptrs = false;
1587     }
1588     init_state = (ik->is_initialized() ? 1 : 0);
1589   }
1590   ResourceMark rm;
1591   uint state = (array_dim << 1) | (init_state & 1);
1592   if (can_use_meta_ptrs && CDSAccess::can_generate_cached_code(klass)) {
1593     DataKind kind = DataKind::Klass_Shared;
1594     uint n = write_bytes(&kind, sizeof(int));
1595     if (n != sizeof(int)) {
1596       return false;
1597     }
1598     // Record state of instance klass initialization.
1599     n = write_bytes(&state, sizeof(int));
1600     if (n != sizeof(int)) {
1601       return false;
1602     }
1603     uint klass_offset = CDSAccess::delta_from_shared_address_base((address)klass);
1604     n = write_bytes(&klass_offset, sizeof(uint));
1605     if (n != sizeof(uint)) {
1606       return false;
1607     }
1608     log_info(scc)("%d (L%d): Wrote shared klass: %s%s%s @ 0x%08x", compile_id(), comp_level(), klass->external_name(),
1609                   (!klass->is_instance_klass() ? "" : (init_state == 1 ? " (initialized)" : " (not-initialized)")),
1610                   (array_dim > 0 ? " (object array)" : ""),
1611                   klass_offset);
1612     return true;
1613   }
1614   // Bailout if code has clinit barriers:
1615   // method will be recompiled without them in any case
1616   if (_for_preload && _has_clinit_barriers) {
1617     set_lookup_failed();
1618     return false;
1619   }
1620   _for_preload = false;
1621   log_info(scc,cds)("%d (L%d): Not shared klass: %s", compile_id(), comp_level(), klass->external_name());
1622   DataKind kind = DataKind::Klass;
1623   uint n = write_bytes(&kind, sizeof(int));
1624   if (n != sizeof(int)) {
1625     return false;
1626   }
1627   // Record state of instance klass initialization.
1628   n = write_bytes(&state, sizeof(int));
1629   if (n != sizeof(int)) {
1630     return false;
1631   }
1632   Symbol* name = klass->name();
1633   int name_length = name->utf8_length();
1634   int total_length = name_length + 1;
1635   char* dest = NEW_RESOURCE_ARRAY(char, total_length);
1636   name->as_C_string(dest, total_length);
1637   dest[total_length - 1] = '\0';
1638   LogTarget(Info, scc, loader) log;
1639   if (log.is_enabled()) {
1640     LogStream ls(log);
1641     oop loader = klass->class_loader();
1642     oop domain = klass->protection_domain();
1643     ls.print("Class %s loader: ", dest);
1644     if (loader == nullptr) {
1645       ls.print("nullptr");
1646     } else {
1647       loader->print_value_on(&ls);
1648     }
1649     ls.print(" domain: ");
1650     if (domain == nullptr) {
1651       ls.print("nullptr");
1652     } else {
1653       domain->print_value_on(&ls);
1654     }
1655     ls.cr();
1656   }
1657   n = write_bytes(&name_length, sizeof(int));
1658   if (n != sizeof(int)) {
1659     return false;
1660   }
1661   n = write_bytes(dest, total_length);
1662   if (n != (uint)total_length) {
1663     return false;
1664   }
1665   log_info(scc)("%d (L%d): Wrote klass: %s%s%s",
1666                 compile_id(), comp_level(),
1667                 dest, (!klass->is_instance_klass() ? "" : (init_state == 1 ? " (initialized)" : " (not-initialized)")),
1668                 (array_dim > 0 ? " (object array)" : ""));
1669   return true;
1670 }
1671 
1672 bool SCCache::write_method(Method* method) {
1673   bool can_use_meta_ptrs = _use_meta_ptrs;
1674   Klass* klass = method->method_holder();
1675   if (klass->is_instance_klass()) {
1676     InstanceKlass* ik = InstanceKlass::cast(klass);
1677     ClassLoaderData* cld = ik->class_loader_data();
1678     if (!cld->is_builtin_class_loader_data()) {
1679       set_lookup_failed();
1680       return false;
1681     }
1682     if (_for_preload && !CDSAccess::can_generate_cached_code(ik)) {
1683       _for_preload = false;
1684       // Bailout if code has clinit barriers:
1685       // method will be recompiled without them in any case
1686       if (_has_clinit_barriers) {
1687         set_lookup_failed();
1688         return false;
1689       }
1690       can_use_meta_ptrs = false;
1691     }
1692   }
1693   ResourceMark rm;
1694   if (can_use_meta_ptrs && CDSAccess::can_generate_cached_code(method)) {
1695     DataKind kind = DataKind::Method_Shared;
1696     uint n = write_bytes(&kind, sizeof(int));
1697     if (n != sizeof(int)) {
1698       return false;
1699     }
1700     uint method_offset = CDSAccess::delta_from_shared_address_base((address)method);
1701     n = write_bytes(&method_offset, sizeof(uint));
1702     if (n != sizeof(uint)) {
1703       return false;
1704     }
1705     log_info(scc)("%d (L%d): Wrote shared method: %s @ 0x%08x", compile_id(), comp_level(), method->name_and_sig_as_C_string(), method_offset);
1706     return true;
1707   }
1708   // Bailout if code has clinit barriers:
1709   // method will be recompiled without them in any case
1710   if (_for_preload && _has_clinit_barriers) {
1711     set_lookup_failed();
1712     return false;
1713   }
1714   _for_preload = false;
1715   log_info(scc,cds)("%d (L%d): Not shared method: %s", compile_id(), comp_level(), method->name_and_sig_as_C_string());
1716   if (method->is_hidden()) { // Skip such nmethod
1717     set_lookup_failed();
1718     return false;
1719   }
1720   DataKind kind = DataKind::Method;
1721   uint n = write_bytes(&kind, sizeof(int));
1722   if (n != sizeof(int)) {
1723     return false;
1724   }
1725   Symbol* name   = method->name();
1726   Symbol* holder = method->klass_name();
1727   Symbol* signat = method->signature();
1728   int name_length   = name->utf8_length();
1729   int holder_length = holder->utf8_length();
1730   int signat_length = signat->utf8_length();
1731 
1732   // Write sizes and strings
1733   int total_length = holder_length + 1 + name_length + 1 + signat_length + 1;
1734   char* dest = NEW_RESOURCE_ARRAY(char, total_length);
1735   holder->as_C_string(dest, total_length);
1736   dest[holder_length] = '\0';
1737   int pos = holder_length + 1;
1738   name->as_C_string(&(dest[pos]), (total_length - pos));
1739   pos += name_length;
1740   dest[pos++] = '\0';
1741   signat->as_C_string(&(dest[pos]), (total_length - pos));
1742   dest[total_length - 1] = '\0';
1743 
1744   LogTarget(Info, scc, loader) log;
1745   if (log.is_enabled()) {
1746     LogStream ls(log);
1747     oop loader = klass->class_loader();
1748     oop domain = klass->protection_domain();
1749     ls.print("Holder %s loader: ", dest);
1750     if (loader == nullptr) {
1751       ls.print("nullptr");
1752     } else {
1753       loader->print_value_on(&ls);
1754     }
1755     ls.print(" domain: ");
1756     if (domain == nullptr) {
1757       ls.print("nullptr");
1758     } else {
1759       domain->print_value_on(&ls);
1760     }
1761     ls.cr();
1762   }
1763 
1764   n = write_bytes(&holder_length, sizeof(int));
1765   if (n != sizeof(int)) {
1766     return false;
1767   }
1768   n = write_bytes(&name_length, sizeof(int));
1769   if (n != sizeof(int)) {
1770     return false;
1771   }
1772   n = write_bytes(&signat_length, sizeof(int));
1773   if (n != sizeof(int)) {
1774     return false;
1775   }
1776   n = write_bytes(dest, total_length);
1777   if (n != (uint)total_length) {
1778     return false;
1779   }
1780   dest[holder_length] = ' ';
1781   dest[holder_length + 1 + name_length] = ' ';
1782   log_info(scc)("%d (L%d): Wrote method: %s", compile_id(), comp_level(), dest);
1783   return true;
1784 }
1785 
1786 // Repair the pc relative information in the code after load
1787 bool SCCReader::read_relocations(CodeBuffer* buffer, CodeBuffer* orig_buffer,
1788                                  OopRecorder* oop_recorder, ciMethod* target) {
1789   bool success = true;
1790   for (int i = 0; i < (int)CodeBuffer::SECT_LIMIT; i++) {
1791     uint code_offset = read_position();
1792     int reloc_count = *(int*)addr(code_offset);
1793     code_offset += sizeof(int);
1794     if (reloc_count == 0) {
1795       set_read_position(code_offset);
1796       continue;
1797     }
1798     // Read _locs_point (as offset from start)
1799     int locs_point_off = *(int*)addr(code_offset);
1800     code_offset += sizeof(int);
1801     uint reloc_size = reloc_count * sizeof(relocInfo);
1802     CodeSection* cs  = buffer->code_section(i);
1803     if (cs->locs_capacity() < reloc_count) {
1804       cs->expand_locs(reloc_count);
1805     }
1806     relocInfo* reloc_start = cs->locs_start();
1807     copy_bytes(addr(code_offset), (address)reloc_start, reloc_size);
1808     code_offset += reloc_size;
1809     cs->set_locs_end(reloc_start + reloc_count);
1810     cs->set_locs_point(cs->start() + locs_point_off);
1811 
1812     // Read additional relocation data: uint per relocation
1813     uint  data_size  = reloc_count * sizeof(uint);
1814     uint* reloc_data = (uint*)addr(code_offset);
1815     code_offset += data_size;
1816     set_read_position(code_offset);
1817     LogStreamHandle(Info, scc, reloc) log;
1818     if (log.is_enabled()) {
1819       log.print_cr("======== read code section %d relocations [%d]:", i, reloc_count);
1820     }
1821     RelocIterator iter(cs);
1822     int j = 0;
1823     while (iter.next()) {
1824       switch (iter.type()) {
1825         case relocInfo::none:
1826           break;
1827         case relocInfo::oop_type: {
1828           VM_ENTRY_MARK;
1829           oop_Relocation* r = (oop_Relocation*)iter.reloc();
1830           if (r->oop_is_immediate()) {
1831             assert(reloc_data[j] == (uint)j, "should be");
1832             methodHandle comp_method(THREAD, target->get_Method());
1833             jobject jo = read_oop(THREAD, comp_method);
1834             if (lookup_failed()) {
1835               success = false;
1836               break;
1837             }
1838             r->set_value((address)jo);
1839           } else if (false) {
1840             // Get already updated value from OopRecorder.
1841             assert(oop_recorder != nullptr, "sanity");
1842             int index = r->oop_index();
1843             jobject jo = oop_recorder->oop_at(index);
1844             oop obj = JNIHandles::resolve(jo);
1845             r->set_value(*reinterpret_cast<address*>(&obj));
1846           }
1847           break;
1848         }
1849         case relocInfo::metadata_type: {
1850           VM_ENTRY_MARK;
1851           metadata_Relocation* r = (metadata_Relocation*)iter.reloc();
1852           Metadata* m;
1853           if (r->metadata_is_immediate()) {
1854             assert(reloc_data[j] == (uint)j, "should be");
1855             methodHandle comp_method(THREAD, target->get_Method());
1856             m = read_metadata(comp_method);
1857             if (lookup_failed()) {
1858               success = false;
1859               break;
1860             }
1861           } else {
1862             // Get already updated value from OopRecorder.
1863             assert(oop_recorder != nullptr, "sanity");
1864             int index = r->metadata_index();
1865             m = oop_recorder->metadata_at(index);
1866           }
1867           r->set_value((address)m);
1868           break;
1869         }
1870         case relocInfo::virtual_call_type:   // Fall through. They all call resolve_*_call blobs.
1871         case relocInfo::opt_virtual_call_type:
1872         case relocInfo::static_call_type: {
1873           address dest = _cache->address_for_id(reloc_data[j]);
1874           if (dest != (address)-1) {
1875             ((CallRelocation*)iter.reloc())->set_destination(dest);
1876           }
1877           break;
1878         }
1879         case relocInfo::trampoline_stub_type: {
1880           address dest = _cache->address_for_id(reloc_data[j]);
1881           if (dest != (address)-1) {
1882             ((trampoline_stub_Relocation*)iter.reloc())->set_destination(dest);
1883           }
1884           break;
1885         }
1886         case relocInfo::static_stub_type:
1887           break;
1888         case relocInfo::runtime_call_type: {
1889           address dest = _cache->address_for_id(reloc_data[j]);
1890           if (dest != (address)-1) {
1891             ((CallRelocation*)iter.reloc())->set_destination(dest);
1892           }
1893           break;
1894         }
1895         case relocInfo::runtime_call_w_cp_type:
1896           fatal("runtime_call_w_cp_type unimplemented");
1897           //address destination = iter.reloc()->value();
1898           break;
1899         case relocInfo::external_word_type: {
1900           address target = _cache->address_for_id(reloc_data[j]);
1901           // Add external address to global table
1902           int index = ExternalsRecorder::find_index(target);
1903           // Update index in relocation
1904           Relocation::add_jint(iter.data(), index);
1905           external_word_Relocation* reloc = (external_word_Relocation*)iter.reloc();
1906           assert(reloc->target() == target, "sanity");
1907           reloc->set_value(target); // Patch address in the code
1908           iter.reloc()->fix_relocation_after_move(orig_buffer, buffer);
1909           break;
1910         }
1911         case relocInfo::internal_word_type:
1912           iter.reloc()->fix_relocation_after_move(orig_buffer, buffer);
1913           break;
1914         case relocInfo::section_word_type:
1915           iter.reloc()->fix_relocation_after_move(orig_buffer, buffer);
1916           break;
1917         case relocInfo::poll_type:
1918           break;
1919         case relocInfo::poll_return_type:
1920           break;
1921         case relocInfo::post_call_nop_type:
1922           break;
1923         case relocInfo::entry_guard_type:
1924           break;
1925         default:
1926           fatal("relocation %d unimplemented", (int)iter.type());
1927           break;
1928       }
1929       if (success && log.is_enabled()) {
1930         iter.print_current_on(&log);
1931       }
1932       j++;
1933     }
1934     assert(j <= (int)reloc_count, "sanity");
1935   }
1936   return success;
1937 }
1938 
1939 bool SCCReader::read_code(CodeBuffer* buffer, CodeBuffer* orig_buffer, uint code_offset) {
1940   assert(code_offset == align_up(code_offset, DATA_ALIGNMENT), "%d not aligned to %d", code_offset, DATA_ALIGNMENT);
1941   assert(buffer->blob() != nullptr, "sanity");
1942   SCCodeSection* scc_cs = (SCCodeSection*)addr(code_offset);
1943   for (int i = 0; i < (int)CodeBuffer::SECT_LIMIT; i++) {
1944     CodeSection* cs = buffer->code_section(i);
1945     // Read original section size and address.
1946     uint orig_size = scc_cs[i]._size;
1947     log_debug(scc)("======== read code section %d [%d]:", i, orig_size);
1948     uint orig_size_align = align_up(orig_size, DATA_ALIGNMENT);
1949     if (i != (int)CodeBuffer::SECT_INSTS) {
1950       buffer->initialize_section_size(cs, orig_size_align);
1951     }
1952     if (orig_size_align > (uint)cs->capacity()) { // Will not fit
1953       log_info(scc)("%d (L%d): original code section %d size %d > current capacity %d",
1954                        compile_id(), comp_level(), i, orig_size, cs->capacity());
1955       return false;
1956     }
1957     if (orig_size == 0) {
1958       assert(cs->size() == 0, "should match");
1959       continue;  // skip trivial section
1960     }
1961     address orig_start = scc_cs[i]._origin_address;
1962 
1963     // Populate fake original buffer (no code allocation in CodeCache).
1964     // It is used for relocations to calculate sections addesses delta.
1965     CodeSection* orig_cs = orig_buffer->code_section(i);
1966     assert(!orig_cs->is_allocated(), "This %d section should not be set", i);
1967     orig_cs->initialize(orig_start, orig_size);
1968 
1969     // Load code to new buffer.
1970     address code_start = cs->start();
1971     copy_bytes(addr(scc_cs[i]._offset + code_offset), code_start, orig_size_align);
1972     cs->set_end(code_start + orig_size);
1973   }
1974 
1975   return true;
1976 }
1977 
1978 bool SCCache::load_exception_blob(CodeBuffer* buffer, int* pc_offset) {
1979 #ifdef ASSERT
1980   LogStreamHandle(Debug, scc, nmethod) log;
1981   if (log.is_enabled()) {
1982     FlagSetting fs(PrintRelocations, true);
1983     buffer->print_on(&log);
1984   }
1985 #endif
1986   SCCache* cache = open_for_read();
1987   if (cache == nullptr) {
1988     return false;
1989   }
1990   SCCEntry* entry = cache->find_entry(SCCEntry::Blob, 999);
1991   if (entry == nullptr) {
1992     return false;
1993   }
1994   SCCReader reader(cache, entry, nullptr);
1995   return reader.compile_blob(buffer, pc_offset);
1996 }
1997 
1998 bool SCCReader::compile_blob(CodeBuffer* buffer, int* pc_offset) {
1999   uint entry_position = _entry->offset();
2000 
2001   // Read pc_offset
2002   *pc_offset = *(int*)addr(entry_position);
2003 
2004   // Read name
2005   uint name_offset = entry_position + _entry->name_offset();
2006   uint name_size = _entry->name_size(); // Includes '/0'
2007   const char* name = addr(name_offset);
2008 
2009   log_info(scc, stubs)("%d (L%d): Reading blob '%s' with pc_offset %d from Startup Code Cache '%s'",
2010                        compile_id(), comp_level(), name, *pc_offset, _cache->cache_path());
2011 
2012   if (strncmp(buffer->name(), name, (name_size - 1)) != 0) {
2013     log_warning(scc)("%d (L%d): Saved blob's name '%s' is different from '%s'",
2014                      compile_id(), comp_level(), name, buffer->name());
2015     ((SCCache*)_cache)->set_failed();
2016     exit_vm_on_load_failure();
2017     return false;
2018   }
2019 
2020   // Create fake original CodeBuffer
2021   CodeBuffer orig_buffer(name);
2022 
2023   // Read code
2024   uint code_offset = entry_position + _entry->code_offset();
2025   if (!read_code(buffer, &orig_buffer, code_offset)) {
2026     return false;
2027   }
2028 
2029   // Read relocations
2030   uint reloc_offset = entry_position + _entry->reloc_offset();
2031   set_read_position(reloc_offset);
2032   if (!read_relocations(buffer, &orig_buffer, nullptr, nullptr)) {
2033     return false;
2034   }
2035 
2036   log_info(scc, stubs)("%d (L%d): Read blob '%s' from Startup Code Cache '%s'",
2037                        compile_id(), comp_level(), name, _cache->cache_path());
2038 #ifdef ASSERT
2039   LogStreamHandle(Debug, scc, nmethod) log;
2040   if (log.is_enabled()) {
2041     FlagSetting fs(PrintRelocations, true);
2042     buffer->print_on(&log);
2043     buffer->decode();
2044   }
2045 #endif
2046   return true;
2047 }
2048 
2049 bool SCCache::write_relocations(CodeBuffer* buffer, uint& all_reloc_size) {
2050   uint all_reloc_count = 0;
2051   for (int i = 0; i < (int)CodeBuffer::SECT_LIMIT; i++) {
2052     CodeSection* cs = buffer->code_section(i);
2053     uint reloc_count = cs->has_locs() ? cs->locs_count() : 0;
2054     all_reloc_count += reloc_count;
2055   }
2056   all_reloc_size = all_reloc_count * sizeof(relocInfo);
2057   bool success = true;
2058   uint* reloc_data = NEW_C_HEAP_ARRAY(uint, all_reloc_count, mtCode);
2059   for (int i = 0; i < (int)CodeBuffer::SECT_LIMIT; i++) {
2060     CodeSection* cs = buffer->code_section(i);
2061     int reloc_count = cs->has_locs() ? cs->locs_count() : 0;
2062     uint n = write_bytes(&reloc_count, sizeof(int));
2063     if (n != sizeof(int)) {
2064       success = false;
2065       break;
2066     }
2067     if (reloc_count == 0) {
2068       continue;
2069     }
2070     // Write _locs_point (as offset from start)
2071     int locs_point_off = cs->locs_point_off();
2072     n = write_bytes(&locs_point_off, sizeof(int));
2073     if (n != sizeof(int)) {
2074       success = false;
2075       break;
2076     }
2077     relocInfo* reloc_start = cs->locs_start();
2078     uint reloc_size      = reloc_count * sizeof(relocInfo);
2079     n = write_bytes(reloc_start, reloc_size);
2080     if (n != reloc_size) {
2081       success = false;
2082       break;
2083     }
2084     LogStreamHandle(Info, scc, reloc) log;
2085     if (log.is_enabled()) {
2086       log.print_cr("======== write code section %d relocations [%d]:", i, reloc_count);
2087     }
2088     // Collect additional data
2089     RelocIterator iter(cs);
2090     bool has_immediate = false;
2091     int j = 0;
2092     while (iter.next()) {
2093       reloc_data[j] = 0; // initialize
2094       switch (iter.type()) {
2095         case relocInfo::none:
2096           break;
2097         case relocInfo::oop_type: {
2098           oop_Relocation* r = (oop_Relocation*)iter.reloc();
2099           if (r->oop_is_immediate()) {
2100             reloc_data[j] = (uint)j; // Indication that we need to restore immediate
2101             has_immediate = true;
2102           }
2103           break;
2104         }
2105         case relocInfo::metadata_type: {
2106           metadata_Relocation* r = (metadata_Relocation*)iter.reloc();
2107           if (r->metadata_is_immediate()) {
2108             reloc_data[j] = (uint)j; // Indication that we need to restore immediate
2109             has_immediate = true;
2110           }
2111           break;
2112         }
2113         case relocInfo::virtual_call_type:  // Fall through. They all call resolve_*_call blobs.
2114         case relocInfo::opt_virtual_call_type:
2115         case relocInfo::static_call_type: {
2116           CallRelocation* r = (CallRelocation*)iter.reloc();
2117           address dest = r->destination();
2118           if (dest == r->addr()) { // possible call via trampoline on Aarch64
2119             dest = (address)-1;    // do nothing in this case when loading this relocation
2120           }
2121           reloc_data[j] = _table->id_for_address(dest, iter, buffer);
2122           break;
2123         }
2124         case relocInfo::trampoline_stub_type: {
2125           address dest = ((trampoline_stub_Relocation*)iter.reloc())->destination();
2126           reloc_data[j] = _table->id_for_address(dest, iter, buffer);
2127           break;
2128         }
2129         case relocInfo::static_stub_type:
2130           break;
2131         case relocInfo::runtime_call_type: {
2132           // Record offset of runtime destination
2133           CallRelocation* r = (CallRelocation*)iter.reloc();
2134           address dest = r->destination();
2135           if (dest == r->addr()) { // possible call via trampoline on Aarch64
2136             dest = (address)-1;    // do nothing in this case when loading this relocation
2137           }
2138           reloc_data[j] = _table->id_for_address(dest, iter, buffer);
2139           break;
2140         }
2141         case relocInfo::runtime_call_w_cp_type:
2142           fatal("runtime_call_w_cp_type unimplemented");
2143           break;
2144         case relocInfo::external_word_type: {
2145           // Record offset of runtime target
2146           address target = ((external_word_Relocation*)iter.reloc())->target();
2147           reloc_data[j] = _table->id_for_address(target, iter, buffer);
2148           break;
2149         }
2150         case relocInfo::internal_word_type:
2151           break;
2152         case relocInfo::section_word_type:
2153           break;
2154         case relocInfo::poll_type:
2155           break;
2156         case relocInfo::poll_return_type:
2157           break;
2158         case relocInfo::post_call_nop_type:
2159           break;
2160         case relocInfo::entry_guard_type:
2161           break;
2162         default:
2163           fatal("relocation %d unimplemented", (int)iter.type());
2164           break;
2165       }
2166       if (log.is_enabled()) {
2167         iter.print_current_on(&log);
2168       }
2169       j++;
2170     }
2171     assert(j <= (int)reloc_count, "sanity");
2172     // Write additional relocation data: uint per relocation
2173     uint data_size = reloc_count * sizeof(uint);
2174     n = write_bytes(reloc_data, data_size);
2175     if (n != data_size) {
2176       success = false;
2177       break;
2178     }
2179     if (has_immediate) {
2180       // Save information about immediates in this Code Section
2181       RelocIterator iter_imm(cs);
2182       int j = 0;
2183       while (iter_imm.next()) {
2184         switch (iter_imm.type()) {
2185           case relocInfo::oop_type: {
2186             oop_Relocation* r = (oop_Relocation*)iter_imm.reloc();
2187             if (r->oop_is_immediate()) {
2188               assert(reloc_data[j] == (uint)j, "should be");
2189               jobject jo = *(jobject*)(r->oop_addr()); // Handle currently
2190               if (!write_oop(jo)) {
2191                 success = false;
2192               }
2193             }
2194             break;
2195           }
2196           case relocInfo::metadata_type: {
2197             metadata_Relocation* r = (metadata_Relocation*)iter_imm.reloc();
2198             if (r->metadata_is_immediate()) {
2199               assert(reloc_data[j] == (uint)j, "should be");
2200               Metadata* m = r->metadata_value();
2201               if (!write_metadata(m)) {
2202                 success = false;
2203               }
2204             }
2205             break;
2206           }
2207           default:
2208             break;
2209         }
2210         if (!success) {
2211           break;
2212         }
2213         j++;
2214       } // while (iter_imm.next())
2215     } // if (has_immediate)
2216   } // for(i < SECT_LIMIT)
2217   FREE_C_HEAP_ARRAY(uint, reloc_data);
2218   return success;
2219 }
2220 
2221 bool SCCache::write_code(CodeBuffer* buffer, uint& code_size) {
2222   assert(_write_position == align_up(_write_position, DATA_ALIGNMENT), "%d not aligned to %d", _write_position, DATA_ALIGNMENT);
2223   //assert(buffer->blob() != nullptr, "sanity");
2224   uint code_offset = _write_position;
2225   uint cb_total_size = (uint)buffer->total_content_size();
2226   // Write information about Code sections first.
2227   SCCodeSection scc_cs[CodeBuffer::SECT_LIMIT];
2228   uint scc_cs_size = (uint)(sizeof(SCCodeSection) * CodeBuffer::SECT_LIMIT);
2229   uint offset = align_up(scc_cs_size, DATA_ALIGNMENT);
2230   uint total_size = 0;
2231   for (int i = 0; i < (int)CodeBuffer::SECT_LIMIT; i++) {
2232     const CodeSection* cs = buffer->code_section(i);
2233     assert(cs->mark() == nullptr, "CodeSection::_mark is not implemented");
2234     uint cs_size = (uint)cs->size();
2235     scc_cs[i]._size = cs_size;
2236     scc_cs[i]._origin_address = (cs_size == 0) ? nullptr : cs->start();
2237     scc_cs[i]._offset = (cs_size == 0) ? 0 : (offset + total_size);
2238     assert(cs->mark() == nullptr, "CodeSection::_mark is not implemented");
2239     total_size += align_up(cs_size, DATA_ALIGNMENT);
2240   }
2241   uint n = write_bytes(scc_cs, scc_cs_size);
2242   if (n != scc_cs_size) {
2243     return false;
2244   }
2245   if (!align_write()) {
2246     return false;
2247   }
2248   assert(_write_position == (code_offset + offset), "%d  != (%d + %d)", _write_position, code_offset, offset);
2249   for (int i = 0; i < (int)CodeBuffer::SECT_LIMIT; i++) {
2250     const CodeSection* cs = buffer->code_section(i);
2251     uint cs_size = (uint)cs->size();
2252     if (cs_size == 0) {
2253       continue;  // skip trivial section
2254     }
2255     assert((_write_position - code_offset) == scc_cs[i]._offset, "%d != %d", _write_position, scc_cs[i]._offset);
2256     // Write code
2257     n = write_bytes(cs->start(), cs_size);
2258     if (n != cs_size) {
2259       return false;
2260     }
2261     if (!align_write()) {
2262       return false;
2263     }
2264   }
2265   assert((_write_position - code_offset) == (offset + total_size), "(%d - %d) != (%d + %d)", _write_position, code_offset, offset, total_size);
2266   code_size = total_size;
2267   return true;
2268 }
2269 
2270 bool SCCache::store_exception_blob(CodeBuffer* buffer, int pc_offset) {
2271   SCCache* cache = open_for_write();
2272   if (cache == nullptr) {
2273     return false;
2274   }
2275   log_info(scc, stubs)("Writing blob '%s' to Startup Code Cache '%s'", buffer->name(), cache->_cache_path);
2276 
2277 #ifdef ASSERT
2278   LogStreamHandle(Debug, scc, nmethod) log;
2279   if (log.is_enabled()) {
2280     FlagSetting fs(PrintRelocations, true);
2281     buffer->print_on(&log);
2282     buffer->decode();
2283   }
2284 #endif
2285   if (!cache->align_write()) {
2286     return false;
2287   }
2288   uint entry_position = cache->_write_position;
2289 
2290   // Write pc_offset
2291   uint n = cache->write_bytes(&pc_offset, sizeof(int));
2292   if (n != sizeof(int)) {
2293     return false;
2294   }
2295 
2296   // Write name
2297   const char* name = buffer->name();
2298   uint name_offset = cache->_write_position - entry_position;
2299   uint name_size = (uint)strlen(name) + 1; // Includes '/0'
2300   n = cache->write_bytes(name, name_size);
2301   if (n != name_size) {
2302     return false;
2303   }
2304 
2305   // Write code section
2306   if (!cache->align_write()) {
2307     return false;
2308   }
2309   uint code_offset = cache->_write_position - entry_position;
2310   uint code_size = 0;
2311   if (!cache->write_code(buffer, code_size)) {
2312     return false;
2313   }
2314   // Write relocInfo array
2315   uint reloc_offset = cache->_write_position - entry_position;
2316   uint reloc_size = 0;
2317   if (!cache->write_relocations(buffer, reloc_size)) {
2318     return false;
2319   }
2320 
2321   uint entry_size = cache->_write_position - entry_position;
2322   SCCEntry* entry = new(cache) SCCEntry(entry_position, entry_size, name_offset, name_size,
2323                                           code_offset, code_size, reloc_offset, reloc_size,
2324                                           SCCEntry::Blob, (uint32_t)999);
2325   log_info(scc, stubs)("Wrote stub '%s' to Startup Code Cache '%s'", name, cache->_cache_path);
2326   return true;
2327 }
2328 
2329 DebugInformationRecorder* SCCReader::read_debug_info(OopRecorder* oop_recorder) {
2330   uint code_offset = align_up(read_position(), DATA_ALIGNMENT);
2331   int data_size  = *(int*)addr(code_offset);
2332   code_offset   += sizeof(int);
2333   int pcs_length = *(int*)addr(code_offset);
2334   code_offset   += sizeof(int);
2335 
2336   log_debug(scc)("======== read DebugInfo [%d, %d]:", data_size, pcs_length);
2337 
2338   // Aligned initial sizes
2339   int data_size_align  = align_up(data_size, DATA_ALIGNMENT);
2340   int pcs_length_align = pcs_length + 1;
2341   assert(sizeof(PcDesc) > DATA_ALIGNMENT, "sanity");
2342   DebugInformationRecorder* recorder = new DebugInformationRecorder(oop_recorder, data_size_align, pcs_length);
2343 
2344   copy_bytes(addr(code_offset), recorder->stream()->buffer(), data_size_align);
2345   recorder->stream()->set_position(data_size);
2346   code_offset += data_size;
2347 
2348   uint pcs_size = pcs_length * sizeof(PcDesc);
2349   copy_bytes(addr(code_offset), (address)recorder->pcs(), pcs_size);
2350   code_offset += pcs_size;
2351   set_read_position(code_offset);
2352   return recorder;
2353 }
2354 
2355 bool SCCache::write_debug_info(DebugInformationRecorder* recorder) {
2356   if (!align_write()) {
2357     return false;
2358   }
2359   // Don't call data_size() and pcs_size(). They will freeze OopRecorder.
2360   int data_size = recorder->stream()->position(); // In bytes
2361   uint n = write_bytes(&data_size, sizeof(int));
2362   if (n != sizeof(int)) {
2363     return false;
2364   }
2365   int pcs_length = recorder->pcs_length(); // In bytes
2366   n = write_bytes(&pcs_length, sizeof(int));
2367   if (n != sizeof(int)) {
2368     return false;
2369   }
2370   n = write_bytes(recorder->stream()->buffer(), data_size);
2371   if (n != (uint)data_size) {
2372     return false;
2373   }
2374   uint pcs_size = pcs_length * sizeof(PcDesc);
2375   n = write_bytes(recorder->pcs(), pcs_size);
2376   if (n != pcs_size) {
2377     return false;
2378   }
2379   return true;
2380 }
2381 
2382 OopMapSet* SCCReader::read_oop_maps() {
2383   uint code_offset = read_position();
2384   int om_count = *(int*)addr(code_offset);
2385   code_offset += sizeof(int);
2386 
2387   log_debug(scc)("======== read oop maps [%d]:", om_count);
2388 
2389   OopMapSet* oop_maps = new OopMapSet(om_count);
2390   for (int i = 0; i < (int)om_count; i++) {
2391     int data_size = *(int*)addr(code_offset);
2392     code_offset += sizeof(int);
2393 
2394     OopMap* oop_map = new OopMap(data_size);
2395     // Preserve allocated stream
2396     CompressedWriteStream* stream = oop_map->write_stream();
2397 
2398     // Read data which overwrites default data
2399     copy_bytes(addr(code_offset), (address)oop_map, sizeof(OopMap));
2400     code_offset += sizeof(OopMap);
2401     stream->set_position(data_size);
2402     oop_map->set_write_stream(stream);
2403     if (data_size > 0) {
2404       copy_bytes(addr(code_offset), (address)(oop_map->data()), (uint)data_size);
2405       code_offset += data_size;
2406     }
2407 #ifdef ASSERT
2408     oop_map->_locs_length = 0;
2409     oop_map->_locs_used   = nullptr;
2410 #endif
2411     oop_maps->add(oop_map);
2412   }
2413   set_read_position(code_offset);
2414   return oop_maps;
2415 }
2416 
2417 bool SCCache::write_oop_maps(OopMapSet* oop_maps) {
2418   uint om_count = oop_maps->size();
2419   uint n = write_bytes(&om_count, sizeof(int));
2420   if (n != sizeof(int)) {
2421     return false;
2422   }
2423   for (int i = 0; i < (int)om_count; i++) {
2424     OopMap* om = oop_maps->at(i);
2425     int data_size = om->data_size();
2426     n = write_bytes(&data_size, sizeof(int));
2427     if (n != sizeof(int)) {
2428       return false;
2429     }
2430     n = write_bytes(om, sizeof(OopMap));
2431     if (n != sizeof(OopMap)) {
2432       return false;
2433     }
2434     n = write_bytes(om->data(), (uint)data_size);
2435     if (n != (uint)data_size) {
2436       return false;
2437     }
2438   }
2439   return true;
2440 }
2441 
2442 jobject SCCReader::read_oop(JavaThread* thread, const methodHandle& comp_method) {
2443   uint code_offset = read_position();
2444   oop obj = nullptr;
2445   DataKind kind = *(DataKind*)addr(code_offset);
2446   code_offset += sizeof(DataKind);
2447   set_read_position(code_offset);
2448   if (kind == DataKind::Null) {
2449     return nullptr;
2450   } else if (kind == DataKind::No_Data) {
2451     return (jobject)Universe::non_oop_word();
2452   } else if (kind == DataKind::Klass || kind == DataKind::Klass_Shared) {
2453     Klass* k = read_klass(comp_method, (kind == DataKind::Klass_Shared));
2454     if (k == nullptr) {
2455       return nullptr;
2456     }
2457     obj = k->java_mirror();
2458     if (obj == nullptr) {
2459       set_lookup_failed();
2460       log_info(scc)("Lookup failed for java_mirror of klass %s", k->external_name());
2461       return nullptr;
2462     }
2463   } else if (kind == DataKind::Primitive) {
2464     code_offset = read_position();
2465     int t = *(int*)addr(code_offset);
2466     code_offset += sizeof(int);
2467     set_read_position(code_offset);
2468     BasicType bt = (BasicType)t;
2469     obj = java_lang_Class::primitive_mirror(bt);
2470     log_info(scc)("%d (L%d): Read primitive type klass: %s", compile_id(), comp_level(), type2name(bt));
2471   } else if (kind == DataKind::String_Shared) {
2472     code_offset = read_position();
2473     int k = *(int*)addr(code_offset);
2474     code_offset += sizeof(int);
2475     set_read_position(code_offset);
2476     obj = CDSAccess::get_archived_object(k);
2477     assert(k == CDSAccess::get_archived_object_permanent_index(obj), "sanity");
2478   } else if (kind == DataKind::String) {
2479     code_offset = read_position();
2480     int length = *(int*)addr(code_offset);
2481     code_offset += sizeof(int);
2482     set_read_position(code_offset);
2483     const char* dest = addr(code_offset);
2484     set_read_position(code_offset + length);
2485     obj = StringTable::intern(&(dest[0]), thread);
2486     if (obj == nullptr) {
2487       set_lookup_failed();
2488       log_info(scc)("%d (L%d): Lookup failed for String %s",
2489                        compile_id(), comp_level(), &(dest[0]));
2490       return nullptr;
2491     }
2492     assert(java_lang_String::is_instance(obj), "must be string");
2493     log_info(scc)("%d (L%d): Read String: %s", compile_id(), comp_level(), dest);
2494   } else if (kind == DataKind::SysLoader) {
2495     obj = SystemDictionary::java_system_loader();
2496     log_info(scc)("%d (L%d): Read java_system_loader", compile_id(), comp_level());
2497   } else if (kind == DataKind::PlaLoader) {
2498     obj = SystemDictionary::java_platform_loader();
2499     log_info(scc)("%d (L%d): Read java_platform_loader", compile_id(), comp_level());
2500   } else if (kind == DataKind::MH_Oop_Shared) {
2501     code_offset = read_position();
2502     int k = *(int*)addr(code_offset);
2503     code_offset += sizeof(int);
2504     set_read_position(code_offset);
2505     obj = CDSAccess::get_archived_object(k);
2506     assert(k == CDSAccess::get_archived_object_permanent_index(obj), "sanity");
2507   } else {
2508     set_lookup_failed();
2509     log_info(scc)("%d (L%d): Unknown oop's kind: %d",
2510                      compile_id(), comp_level(), (int)kind);
2511     return nullptr;
2512   }
2513   return JNIHandles::make_local(thread, obj);
2514 }
2515 
2516 bool SCCReader::read_oops(OopRecorder* oop_recorder, ciMethod* target) {
2517   uint code_offset = read_position();
2518   int oop_count = *(int*)addr(code_offset);
2519   code_offset += sizeof(int);
2520   set_read_position(code_offset);
2521   log_debug(scc)("======== read oops [%d]:", oop_count);
2522   if (oop_count == 0) {
2523     return true;
2524   }
2525   {
2526     VM_ENTRY_MARK;
2527     methodHandle comp_method(THREAD, target->get_Method());
2528     for (int i = 1; i < oop_count; i++) {
2529       jobject jo = read_oop(THREAD, comp_method);
2530       if (lookup_failed()) {
2531         return false;
2532       }
2533       if (oop_recorder->is_real(jo)) {
2534         oop_recorder->find_index(jo);
2535       } else {
2536         oop_recorder->allocate_oop_index(jo);
2537       }
2538       LogStreamHandle(Debug, scc, oops) log;
2539       if (log.is_enabled()) {
2540         log.print("%d: " INTPTR_FORMAT " ", i, p2i(jo));
2541         if (jo == (jobject)Universe::non_oop_word()) {
2542           log.print("non-oop word");
2543         } else if (jo == nullptr) {
2544           log.print("nullptr-oop");
2545         } else {
2546           JNIHandles::resolve(jo)->print_value_on(&log);
2547         }
2548         log.cr();
2549       }
2550     }
2551   }
2552   return true;
2553 }
2554 
2555 Metadata* SCCReader::read_metadata(const methodHandle& comp_method) {
2556   uint code_offset = read_position();
2557   Metadata* m = nullptr;
2558   DataKind kind = *(DataKind*)addr(code_offset);
2559   code_offset += sizeof(DataKind);
2560   set_read_position(code_offset);
2561   if (kind == DataKind::Null) {
2562     m = (Metadata*)nullptr;
2563   } else if (kind == DataKind::No_Data) {
2564     m = (Metadata*)Universe::non_oop_word();
2565   } else if (kind == DataKind::Klass || kind == DataKind::Klass_Shared) {
2566     m = (Metadata*)read_klass(comp_method, (kind == DataKind::Klass_Shared));
2567   } else if (kind == DataKind::Method || kind == DataKind::Method_Shared) {
2568     m = (Metadata*)read_method(comp_method, (kind == DataKind::Method_Shared));
2569   } else if (kind == DataKind::MethodCnts) {
2570     kind = *(DataKind*)addr(code_offset);
2571     bool shared = (kind == DataKind::Method_Shared);
2572     assert(kind == DataKind::Method || shared, "Sanity");
2573     code_offset += sizeof(DataKind);
2574     set_read_position(code_offset);
2575     m = (Metadata*)read_method(comp_method, shared);
2576     if (m != nullptr) {
2577       Method* method = (Method*)m;
2578       m = method->get_method_counters(Thread::current());
2579       if (m == nullptr) {
2580         set_lookup_failed();
2581         log_info(scc)("%d (L%d): Failed to get MethodCounters", compile_id(), comp_level());
2582       } else {
2583         log_info(scc)("%d (L%d): Read MethodCounters : " INTPTR_FORMAT, compile_id(), comp_level(), p2i(m));
2584       }
2585     }
2586   } else {
2587     set_lookup_failed();
2588     log_info(scc)("%d (L%d): Unknown metadata's kind: %d", compile_id(), comp_level(), (int)kind);
2589   }
2590   return m;
2591 }
2592 
2593 bool SCCReader::read_metadata(OopRecorder* oop_recorder, ciMethod* target) {
2594   uint code_offset = read_position();
2595   int metadata_count = *(int*)addr(code_offset);
2596   code_offset += sizeof(int);
2597   set_read_position(code_offset);
2598 
2599   log_debug(scc)("======== read metadata [%d]:", metadata_count);
2600 
2601   if (metadata_count == 0) {
2602     return true;
2603   }
2604   {
2605     VM_ENTRY_MARK;
2606     methodHandle comp_method(THREAD, target->get_Method());
2607 
2608     for (int i = 1; i < metadata_count; i++) {
2609       Metadata* m = read_metadata(comp_method);
2610       if (lookup_failed()) {
2611         return false;
2612       }
2613       if (oop_recorder->is_real(m)) {
2614         oop_recorder->find_index(m);
2615       } else {
2616         oop_recorder->allocate_metadata_index(m);
2617       }
2618       LogTarget(Debug, scc, metadata) log;
2619       if (log.is_enabled()) {
2620         LogStream ls(log);
2621         ls.print("%d: " INTPTR_FORMAT " ", i, p2i(m));
2622         if (m == (Metadata*)Universe::non_oop_word()) {
2623           ls.print("non-metadata word");
2624         } else if (m == nullptr) {
2625           ls.print("nullptr-oop");
2626         } else {
2627           Metadata::print_value_on_maybe_null(&ls, m);
2628         }
2629         ls.cr();
2630       }
2631     }
2632   }
2633   return true;
2634 }
2635 
2636 bool SCCache::write_oop(jobject& jo) {
2637   DataKind kind;
2638   uint n = 0;
2639   oop obj = JNIHandles::resolve(jo);
2640   if (jo == nullptr) {
2641     kind = DataKind::Null;
2642     n = write_bytes(&kind, sizeof(int));
2643     if (n != sizeof(int)) {
2644       return false;
2645     }
2646   } else if (jo == (jobject)Universe::non_oop_word()) {
2647     kind = DataKind::No_Data;
2648     n = write_bytes(&kind, sizeof(int));
2649     if (n != sizeof(int)) {
2650       return false;
2651     }
2652   } else if (java_lang_Class::is_instance(obj)) {
2653     if (java_lang_Class::is_primitive(obj)) {
2654       int bt = (int)java_lang_Class::primitive_type(obj);
2655       kind = DataKind::Primitive;
2656       n = write_bytes(&kind, sizeof(int));
2657       if (n != sizeof(int)) {
2658         return false;
2659       }
2660       n = write_bytes(&bt, sizeof(int));
2661       if (n != sizeof(int)) {
2662         return false;
2663       }
2664       log_info(scc)("%d (L%d): Write primitive type klass: %s", compile_id(), comp_level(), type2name((BasicType)bt));
2665     } else {
2666       Klass* klass = java_lang_Class::as_Klass(obj);
2667       if (!write_klass(klass)) {
2668         return false;
2669       }
2670     }
2671   } else if (java_lang_String::is_instance(obj)) { // herere
2672     int k = CDSAccess::get_archived_object_permanent_index(obj);  // k >= 1 means obj is a "permanent heap object"
2673     if (k > 0) {
2674       kind = DataKind::String_Shared;
2675       n = write_bytes(&kind, sizeof(int));
2676       if (n != sizeof(int)) {
2677         return false;
2678       }
2679       n = write_bytes(&k, sizeof(int));
2680       if (n != sizeof(int)) {
2681         return false;
2682       }
2683       return true;
2684     }
2685     kind = DataKind::String;
2686     n = write_bytes(&kind, sizeof(int));
2687     if (n != sizeof(int)) {
2688       return false;
2689     }
2690     ResourceMark rm;
2691     int length = 0;
2692     const char* string = java_lang_String::as_utf8_string(obj, length);
2693     length++; // write tailing '/0'
2694     n = write_bytes(&length, sizeof(int));
2695     if (n != sizeof(int)) {
2696       return false;
2697     }
2698     n = write_bytes(string, (uint)length);
2699     if (n != (uint)length) {
2700       return false;
2701     }
2702     log_info(scc)("%d (L%d): Write String: %s", compile_id(), comp_level(), string);
2703   } else if (java_lang_Module::is_instance(obj)) {
2704     fatal("Module object unimplemented");
2705   } else if (java_lang_ClassLoader::is_instance(obj)) {
2706     if (obj == SystemDictionary::java_system_loader()) {
2707       kind = DataKind::SysLoader;
2708       log_info(scc)("%d (L%d): Write ClassLoader: java_system_loader", compile_id(), comp_level());
2709     } else if (obj == SystemDictionary::java_platform_loader()) {
2710       kind = DataKind::PlaLoader;
2711       log_info(scc)("%d (L%d): Write ClassLoader: java_platform_loader", compile_id(), comp_level());
2712     } else {
2713       fatal("ClassLoader object unimplemented");
2714       return false;
2715     }
2716     n = write_bytes(&kind, sizeof(int));
2717     if (n != sizeof(int)) {
2718       return false;
2719     }
2720   } else { // herere
2721     int k = CDSAccess::get_archived_object_permanent_index(obj);  // k >= 1 means obj is a "permanent heap object"
2722     if (k > 0) {
2723       kind = DataKind::MH_Oop_Shared;
2724       n = write_bytes(&kind, sizeof(int));
2725       if (n != sizeof(int)) {
2726         return false;
2727       }
2728       n = write_bytes(&k, sizeof(int));
2729       if (n != sizeof(int)) {
2730         return false;
2731       }
2732       return true;
2733     }
2734     // Unhandled oop - bailout
2735     set_lookup_failed();
2736     log_info(scc, nmethod)("%d (L%d): Unhandled obj: " PTR_FORMAT " : %s",
2737                               compile_id(), comp_level(), p2i(obj), obj->klass()->external_name());
2738     return false;
2739   }
2740   return true;
2741 }
2742 
2743 bool SCCache::write_oops(OopRecorder* oop_recorder) {
2744   int oop_count = oop_recorder->oop_count();
2745   uint n = write_bytes(&oop_count, sizeof(int));
2746   if (n != sizeof(int)) {
2747     return false;
2748   }
2749   log_debug(scc)("======== write oops [%d]:", oop_count);
2750 
2751   for (int i = 1; i < oop_count; i++) { // skip first virtual nullptr
2752     jobject jo = oop_recorder->oop_at(i);
2753     LogStreamHandle(Info, scc, oops) log;
2754     if (log.is_enabled()) {
2755       log.print("%d: " INTPTR_FORMAT " ", i, p2i(jo));
2756       if (jo == (jobject)Universe::non_oop_word()) {
2757         log.print("non-oop word");
2758       } else if (jo == nullptr) {
2759         log.print("nullptr-oop");
2760       } else {
2761         JNIHandles::resolve(jo)->print_value_on(&log);
2762       }
2763       log.cr();
2764     }
2765     if (!write_oop(jo)) {
2766       return false;
2767     }
2768   }
2769   return true;
2770 }
2771 
2772 bool SCCache::write_metadata(Metadata* m) {
2773   uint n = 0;
2774   if (m == nullptr) {
2775     DataKind kind = DataKind::Null;
2776     n = write_bytes(&kind, sizeof(int));
2777     if (n != sizeof(int)) {
2778       return false;
2779     }
2780   } else if (m == (Metadata*)Universe::non_oop_word()) {
2781     DataKind kind = DataKind::No_Data;
2782     n = write_bytes(&kind, sizeof(int));
2783     if (n != sizeof(int)) {
2784       return false;
2785     }
2786   } else if (m->is_klass()) {
2787     if (!write_klass((Klass*)m)) {
2788       return false;
2789     }
2790   } else if (m->is_method()) {
2791     if (!write_method((Method*)m)) {
2792       return false;
2793     }
2794   } else if (m->is_methodCounters()) {
2795     DataKind kind = DataKind::MethodCnts;
2796     n = write_bytes(&kind, sizeof(int));
2797     if (n != sizeof(int)) {
2798       return false;
2799     }
2800     if (!write_method(((MethodCounters*)m)->method())) {
2801       return false;
2802     }
2803     log_info(scc)("%d (L%d): Write MethodCounters : " INTPTR_FORMAT, compile_id(), comp_level(), p2i(m));
2804   } else { // Not supported
2805     fatal("metadata : " INTPTR_FORMAT " unimplemented", p2i(m));
2806     return false;
2807   }
2808   return true;
2809 }
2810 
2811 bool SCCache::write_metadata(OopRecorder* oop_recorder) {
2812   int metadata_count = oop_recorder->metadata_count();
2813   uint n = write_bytes(&metadata_count, sizeof(int));
2814   if (n != sizeof(int)) {
2815     return false;
2816   }
2817 
2818   log_debug(scc)("======== write metadata [%d]:", metadata_count);
2819 
2820   for (int i = 1; i < metadata_count; i++) { // skip first virtual nullptr
2821     Metadata* m = oop_recorder->metadata_at(i);
2822     LogStreamHandle(Debug, scc, metadata) log;
2823     if (log.is_enabled()) {
2824       log.print("%d: " INTPTR_FORMAT " ", i, p2i(m));
2825       if (m == (Metadata*)Universe::non_oop_word()) {
2826         log.print("non-metadata word");
2827       } else if (m == nullptr) {
2828         log.print("nullptr-oop");
2829       } else {
2830         Metadata::print_value_on_maybe_null(&log, m);
2831       }
2832       log.cr();
2833     }
2834     if (!write_metadata(m)) {
2835       return false;
2836     }
2837   }
2838   return true;
2839 }
2840 
2841 bool SCCReader::read_dependencies(Dependencies* dependencies) {
2842   uint code_offset = read_position();
2843   int dependencies_size = *(int*)addr(code_offset);
2844 
2845   log_debug(scc)("======== read dependencies [%d]:", dependencies_size);
2846 
2847   code_offset += sizeof(int);
2848   code_offset = align_up(code_offset, DATA_ALIGNMENT);
2849   if (dependencies_size > 0) {
2850     dependencies->set_content((u_char*)addr(code_offset), dependencies_size);
2851   }
2852   code_offset += dependencies_size;
2853   set_read_position(code_offset);
2854   return true;
2855 }
2856 
2857 bool SCCache::load_nmethod(ciEnv* env, ciMethod* target, int entry_bci, AbstractCompiler* compiler, CompLevel comp_level) {
2858   TraceTime t1("SC total load time", &_t_totalLoad, enable_timers(), false);
2859   CompileTask* task = env->task();
2860   SCCEntry* entry = task->scc_entry();
2861   bool preload = task->preload();
2862   assert(entry != nullptr, "sanity");
2863   SCCache* cache = open_for_read();
2864   if (cache == nullptr) {
2865     return false;
2866   }
2867   if (log_is_enabled(Info, scc, nmethod)) {
2868     uint decomp = (target->method_data() == nullptr) ? 0 : target->method_data()->decompile_count();
2869     VM_ENTRY_MARK;
2870     ResourceMark rm;
2871     methodHandle method(THREAD, target->get_Method());
2872     const char* target_name = method->name_and_sig_as_C_string();
2873     uint hash = java_lang_String::hash_code((const jbyte*)target_name, (int)strlen(target_name));
2874     bool clinit_brs = entry->has_clinit_barriers();
2875     log_info(scc, nmethod)("%d (L%d): %s nmethod '%s' (decomp: %d, hash: " UINT32_FORMAT_X_0 "%s)",
2876                            task->compile_id(), task->comp_level(), (preload ? "Preloading" : "Reading"),
2877                            target_name, decomp, hash, (clinit_brs ? ", has clinit barriers" : ""));
2878   }
2879   ReadingMark rdmk;
2880   SCCReader reader(cache, entry, task);
2881   bool success = reader.compile(env, target, entry_bci, compiler);
2882   if (success) {
2883     task->set_num_inlined_bytecodes(entry->num_inlined_bytecodes());
2884   } else {
2885     entry->set_load_fail();
2886   }
2887   return success;
2888 }
2889 
2890 SCCReader::SCCReader(SCCache* cache, SCCEntry* entry, CompileTask* task) {
2891   _cache = cache;
2892   _entry   = entry;
2893   _load_buffer = cache->cache_buffer();
2894   _read_position = 0;
2895   if (task != nullptr) {
2896     _compile_id = task->compile_id();
2897     _comp_level = task->comp_level();
2898     _preload    = task->preload();
2899   } else {
2900     _compile_id = 0;
2901     _comp_level = 0;
2902     _preload    = false;
2903   }
2904   _lookup_failed = false;
2905 }
2906 
2907 bool SCCReader::compile(ciEnv* env, ciMethod* target, int entry_bci, AbstractCompiler* compiler) {
2908   uint entry_position = _entry->offset();
2909   uint code_offset = entry_position + _entry->code_offset();
2910   set_read_position(code_offset);
2911 
2912   // Read flags
2913   int flags = *(int*)addr(code_offset);
2914   code_offset += sizeof(int);
2915   bool has_monitors      = (flags & 0x1) != 0;
2916   bool has_wide_vectors  = (flags & 0x2) != 0;
2917   bool has_unsafe_access = (flags & 0x4) != 0;
2918   bool has_scoped_access = (flags & 0x8) != 0;
2919 
2920   int orig_pc_offset = *(int*)addr(code_offset);
2921   code_offset += sizeof(int);
2922   int frame_size = *(int*)addr(code_offset);
2923   code_offset += sizeof(int);
2924 
2925   // Read offsets
2926   CodeOffsets* offsets = (CodeOffsets*)addr(code_offset);
2927   code_offset += sizeof(CodeOffsets);
2928 
2929   // Create Debug Information Recorder to record scopes, oopmaps, etc.
2930   OopRecorder* oop_recorder = new OopRecorder(env->arena());
2931   env->set_oop_recorder(oop_recorder);
2932 
2933   set_read_position(code_offset);
2934 
2935   // Write OopRecorder data
2936   if (!read_oops(oop_recorder, target)) {
2937     return false;
2938   }
2939   if (!read_metadata(oop_recorder, target)) {
2940     return false;
2941   }
2942 
2943   // Read Debug info
2944   DebugInformationRecorder* recorder = read_debug_info(oop_recorder);
2945   if (recorder == nullptr) {
2946     return false;
2947   }
2948   env->set_debug_info(recorder);
2949 
2950   // Read Dependencies (compressed already)
2951   Dependencies* dependencies = new Dependencies(env);
2952   if (!read_dependencies(dependencies)) {
2953     return false;
2954   }
2955   env->set_dependencies(dependencies);
2956 
2957   // Read oop maps
2958   OopMapSet* oop_maps = read_oop_maps();
2959   if (oop_maps == nullptr) {
2960     return false;
2961   }
2962 
2963   // Read exception handles
2964   code_offset = read_position();
2965   int exc_table_length = *(int*)addr(code_offset);
2966   code_offset += sizeof(int);
2967   ExceptionHandlerTable handler_table(MAX2(exc_table_length, 4));
2968   if (exc_table_length > 0) {
2969     handler_table.set_length(exc_table_length);
2970     uint exc_table_size = handler_table.size_in_bytes();
2971     copy_bytes(addr(code_offset), (address)handler_table.table(), exc_table_size);
2972     code_offset += exc_table_size;
2973   }
2974 
2975   // Read null check table
2976   int nul_chk_length = *(int*)addr(code_offset);
2977   code_offset += sizeof(int);
2978   ImplicitExceptionTable nul_chk_table;
2979   if (nul_chk_length > 0) {
2980     nul_chk_table.set_size(nul_chk_length);
2981     nul_chk_table.set_len(nul_chk_length);
2982     uint nul_chk_size = nul_chk_table.size_in_bytes();
2983     copy_bytes(addr(code_offset), (address)nul_chk_table.data(), nul_chk_size - sizeof(implicit_null_entry));
2984     code_offset += nul_chk_size;
2985   }
2986 
2987   uint reloc_size = _entry->reloc_size();
2988   CodeBuffer buffer("Compile::Fill_buffer", _entry->code_size(), reloc_size);
2989   buffer.initialize_oop_recorder(oop_recorder);
2990 
2991   const char* name = addr(entry_position + _entry->name_offset());
2992 
2993   // Create fake original CodeBuffer
2994   CodeBuffer orig_buffer(name);
2995 
2996   // Read code
2997   if (!read_code(&buffer, &orig_buffer, align_up(code_offset, DATA_ALIGNMENT))) {
2998     return false;
2999   }
3000 
3001   // Read relocations
3002   uint reloc_offset = entry_position + _entry->reloc_offset();
3003   set_read_position(reloc_offset);
3004   if (!read_relocations(&buffer, &orig_buffer, oop_recorder, target)) {
3005     return false;
3006   }
3007 
3008   log_info(scc, nmethod)("%d (L%d): Read nmethod '%s' from Startup Code Cache '%s'", compile_id(), comp_level(), name, _cache->cache_path());
3009 #ifdef ASSERT
3010   LogStreamHandle(Debug, scc, nmethod) log;
3011   if (log.is_enabled()) {
3012     FlagSetting fs(PrintRelocations, true);
3013     buffer.print_on(&log);
3014     buffer.decode();
3015   }
3016 #endif
3017 
3018   if (VerifyCachedCode) {
3019     return false;
3020   }
3021 
3022   // Register nmethod
3023   TraceTime t1("SC total nmethod register time", &_t_totalRegister, enable_timers(), false);
3024   env->register_method(target, entry_bci,
3025                        offsets, orig_pc_offset,
3026                        &buffer, frame_size,
3027                        oop_maps, &handler_table,
3028                        &nul_chk_table, compiler,
3029                        _entry->has_clinit_barriers(),
3030                        false,
3031                        has_unsafe_access,
3032                        has_wide_vectors,
3033                        has_monitors,
3034                        has_scoped_access,
3035                        0, true /* install_code */,
3036                        (SCCEntry *)_entry);
3037   CompileTask* task = env->task();
3038   bool success = task->is_success();
3039   if (success) {
3040     ((SCCEntry *)_entry)->set_loaded();
3041   }
3042   return success;
3043 }
3044 
3045 // No concurency for writing to cache file because this method is called from
3046 // ciEnv::register_method() under MethodCompileQueue_lock and Compile_lock locks.
3047 SCCEntry* SCCache::store_nmethod(const methodHandle& method,
3048                      int comp_id,
3049                      int entry_bci,
3050                      CodeOffsets* offsets,
3051                      int orig_pc_offset,
3052                      DebugInformationRecorder* recorder,
3053                      Dependencies* dependencies,
3054                      CodeBuffer* buffer,
3055                      int frame_size,
3056                      OopMapSet* oop_maps,
3057                      ExceptionHandlerTable* handler_table,
3058                      ImplicitExceptionTable* nul_chk_table,
3059                      AbstractCompiler* compiler,
3060                      CompLevel comp_level,
3061                      bool has_clinit_barriers,
3062                      bool for_preload,
3063                      bool has_unsafe_access,
3064                      bool has_wide_vectors,
3065                      bool has_monitors,
3066                      bool has_scoped_access) {
3067   CompileTask* task = ciEnv::current()->task();
3068 
3069   if (!CDSConfig::is_dumping_cached_code()) {
3070     return nullptr; // The metadata and heap in the CDS image haven't been finalized yet.
3071   }
3072   if (entry_bci != InvocationEntryBci) {
3073     return nullptr; // No OSR
3074   }
3075   if (compiler->is_c1() && (comp_level == CompLevel_simple || comp_level == CompLevel_limited_profile)) {
3076     // Cache tier1 compilations
3077   } else if (!compiler->is_c2()) {
3078     return nullptr; // Only C2 now
3079   }
3080   TraceTime t1("SC total store time", &_t_totalStore, enable_timers(), false);
3081   SCCache* cache = open_for_write();
3082   if (cache == nullptr) {
3083     return nullptr; // Cache file is closed
3084   }
3085   SCCEntry* entry = cache->write_nmethod(method, comp_id, entry_bci, offsets, orig_pc_offset, recorder, dependencies, buffer,
3086                                   frame_size, oop_maps, handler_table, nul_chk_table, compiler, comp_level,
3087                                   has_clinit_barriers, for_preload, has_unsafe_access, has_wide_vectors, has_monitors, has_scoped_access);
3088   if (entry == nullptr) {
3089     log_info(scc, nmethod)("%d (L%d): nmethod store attempt failed", task->compile_id(), task->comp_level());
3090   }
3091   return entry;
3092 }
3093 
3094 SCCEntry* SCCache::write_nmethod(const methodHandle& method,
3095                                  int comp_id,
3096                                  int entry_bci,
3097                                  CodeOffsets* offsets,
3098                                  int orig_pc_offset,
3099                                  DebugInformationRecorder* recorder,
3100                                  Dependencies* dependencies,
3101                                  CodeBuffer* buffer,
3102                                  int frame_size,
3103                                  OopMapSet* oop_maps,
3104                                  ExceptionHandlerTable* handler_table,
3105                                  ImplicitExceptionTable* nul_chk_table,
3106                                  AbstractCompiler* compiler,
3107                                  CompLevel comp_level,
3108                                  bool has_clinit_barriers,
3109                                  bool for_preload,
3110                                  bool has_unsafe_access,
3111                                  bool has_wide_vectors,
3112                                  bool has_monitors,
3113                                  bool has_scoped_access) {
3114   CompileTask* task = ciEnv::current()->task();
3115 
3116 //  if (method->is_hidden()) {
3117 //    ResourceMark rm;
3118 //    log_info(scc, nmethod)("%d (L%d): Skip hidden method '%s'", task->compile_id(), task->comp_level(), method->name_and_sig_as_C_string());
3119 //    return nullptr;
3120 //  }
3121   if (buffer->before_expand() != nullptr) {
3122     ResourceMark rm;
3123     log_info(scc, nmethod)("%d (L%d): Skip nmethod with expanded buffer '%s'", task->compile_id(), task->comp_level(), method->name_and_sig_as_C_string());
3124     return nullptr;
3125   }
3126 #ifdef ASSERT
3127   LogStreamHandle(Debug, scc, nmethod) log;
3128   if (log.is_enabled()) {
3129     tty->print_cr(" == store_nmethod");
3130     FlagSetting fs(PrintRelocations, true);
3131     buffer->print_on(&log);
3132     buffer->decode();
3133   }
3134 #endif
3135   assert(!has_clinit_barriers || _gen_preload_code, "sanity");
3136   Method* m = method();
3137   bool method_in_cds = MetaspaceShared::is_in_shared_metaspace((address)m); // herere
3138   InstanceKlass* holder = m->method_holder();
3139   bool klass_in_cds = holder->is_shared() && !holder->is_shared_unregistered_class();
3140   bool builtin_loader = holder->class_loader_data()->is_builtin_class_loader_data();
3141   if (!builtin_loader) {
3142     ResourceMark rm;
3143     log_info(scc, nmethod)("%d (L%d): Skip method '%s' loaded by custom class loader %s", task->compile_id(), task->comp_level(), method->name_and_sig_as_C_string(), holder->class_loader_data()->loader_name());
3144     return nullptr;
3145   }
3146   if (for_preload && !(method_in_cds && klass_in_cds)) {
3147     ResourceMark rm;
3148     log_info(scc, nmethod)("%d (L%d): Skip method '%s' for preload: not in CDS", task->compile_id(), task->comp_level(), method->name_and_sig_as_C_string());
3149     return nullptr;
3150   }
3151   assert(!for_preload || method_in_cds, "sanity");
3152   _for_preload = for_preload;
3153   _has_clinit_barriers = has_clinit_barriers;
3154 
3155   if (!align_write()) {
3156     return nullptr;
3157   }
3158   _compile_id = task->compile_id();
3159   _comp_level = task->comp_level();
3160 
3161   uint entry_position = _write_position;
3162 
3163   uint decomp = (method->method_data() == nullptr) ? 0 : method->method_data()->decompile_count();
3164   // Write name
3165   uint name_offset = 0;
3166   uint name_size   = 0;
3167   uint hash = 0;
3168   uint n;
3169   {
3170     ResourceMark rm;
3171     const char* name   = method->name_and_sig_as_C_string();
3172     log_info(scc, nmethod)("%d (L%d): Writing nmethod '%s' (comp level: %d, decomp: %d%s) to Startup Code Cache '%s'",
3173                            task->compile_id(), task->comp_level(), name, comp_level, decomp,
3174                            (has_clinit_barriers ? ", has clinit barriers" : ""), _cache_path);
3175 
3176     LogStreamHandle(Info, scc, loader) log;
3177     if (log.is_enabled()) {
3178       oop loader = holder->class_loader();
3179       oop domain = holder->protection_domain();
3180       log.print("Holder: ");
3181       holder->print_value_on(&log);
3182       log.print(" loader: ");
3183       if (loader == nullptr) {
3184         log.print("nullptr");
3185       } else {
3186         loader->print_value_on(&log);
3187       }
3188       log.print(" domain: ");
3189       if (domain == nullptr) {
3190         log.print("nullptr");
3191       } else {
3192         domain->print_value_on(&log);
3193       }
3194       log.cr();
3195     }
3196     name_offset = _write_position  - entry_position;
3197     name_size   = (uint)strlen(name) + 1; // Includes '/0'
3198     n = write_bytes(name, name_size);
3199     if (n != name_size) {
3200       return nullptr;
3201     }
3202     hash = java_lang_String::hash_code((const jbyte*)name, (int)strlen(name));
3203   }
3204 
3205   if (!align_write()) {
3206     return nullptr;
3207   }
3208 
3209   uint code_offset = _write_position - entry_position;
3210 
3211   int flags = (has_scoped_access ? 0x8 : 0) |
3212               (has_unsafe_access ? 0x4 : 0) |
3213               (has_wide_vectors  ? 0x2 : 0) |
3214               (has_monitors      ? 0x1 : 0);
3215   n = write_bytes(&flags, sizeof(int));
3216   if (n != sizeof(int)) {
3217     return nullptr;
3218   }
3219 
3220   n = write_bytes(&orig_pc_offset, sizeof(int));
3221   if (n != sizeof(int)) {
3222     return nullptr;
3223   }
3224 
3225   n = write_bytes(&frame_size, sizeof(int));
3226   if (n != sizeof(int)) {
3227     return nullptr;
3228   }
3229 
3230   // Write offsets
3231   n = write_bytes(offsets, sizeof(CodeOffsets));
3232   if (n != sizeof(CodeOffsets)) {
3233     return nullptr;
3234   }
3235 
3236   // Write OopRecorder data
3237   if (!write_oops(buffer->oop_recorder())) {
3238     if (lookup_failed() && !failed()) {
3239       // Skip this method and reposition file
3240       set_write_position(entry_position);
3241     }
3242     return nullptr;
3243   }
3244   if (!write_metadata(buffer->oop_recorder())) {
3245     if (lookup_failed() && !failed()) {
3246       // Skip this method and reposition file
3247       set_write_position(entry_position);
3248     }
3249     return nullptr;
3250   }
3251 
3252   // Write Debug info
3253   if (!write_debug_info(recorder)) {
3254     return nullptr;
3255   }
3256   // Write Dependencies
3257   int dependencies_size = (int)dependencies->size_in_bytes();
3258   n = write_bytes(&dependencies_size, sizeof(int));
3259   if (n != sizeof(int)) {
3260     return nullptr;
3261   }
3262   if (!align_write()) {
3263     return nullptr;
3264   }
3265   n = write_bytes(dependencies->content_bytes(), dependencies_size);
3266   if (n != (uint)dependencies_size) {
3267     return nullptr;
3268   }
3269 
3270   // Write oop maps
3271   if (!write_oop_maps(oop_maps)) {
3272     return nullptr;
3273   }
3274 
3275   // Write exception handles
3276   int exc_table_length = handler_table->length();
3277   n = write_bytes(&exc_table_length, sizeof(int));
3278   if (n != sizeof(int)) {
3279     return nullptr;
3280   }
3281   uint exc_table_size = handler_table->size_in_bytes();
3282   n = write_bytes(handler_table->table(), exc_table_size);
3283   if (n != exc_table_size) {
3284     return nullptr;
3285   }
3286 
3287   // Write null check table
3288   int nul_chk_length = nul_chk_table->len();
3289   n = write_bytes(&nul_chk_length, sizeof(int));
3290   if (n != sizeof(int)) {
3291     return nullptr;
3292   }
3293   uint nul_chk_size = nul_chk_table->size_in_bytes();
3294   n = write_bytes(nul_chk_table->data(), nul_chk_size);
3295   if (n != nul_chk_size) {
3296     return nullptr;
3297   }
3298 
3299   // Write code section
3300   if (!align_write()) {
3301     return nullptr;
3302   }
3303   uint code_size = 0;
3304   if (!write_code(buffer, code_size)) {
3305     return nullptr;
3306   }
3307   // Write relocInfo array
3308   uint reloc_offset = _write_position - entry_position;
3309   uint reloc_size = 0;
3310   if (!write_relocations(buffer, reloc_size)) {
3311     if (lookup_failed() && !failed()) {
3312       // Skip this method and reposition file
3313       set_write_position(entry_position);
3314     }
3315     return nullptr;
3316   }
3317   uint entry_size = _write_position - entry_position;
3318 
3319   SCCEntry* entry = new (this) SCCEntry(entry_position, entry_size, name_offset, name_size,
3320                                         code_offset, code_size, reloc_offset, reloc_size,
3321                                         SCCEntry::Code, hash, (uint)comp_level, (uint)comp_id, decomp,
3322                                         has_clinit_barriers, _for_preload);
3323   if (method_in_cds) {
3324     entry->set_method(m);
3325   }
3326 #ifdef ASSERT
3327   if (has_clinit_barriers || _for_preload) {
3328     assert(for_preload, "sanity");
3329     assert(entry->method() != nullptr, "sanity");
3330   }
3331 #endif
3332   {
3333     ResourceMark rm;
3334     const char* name   = method->name_and_sig_as_C_string();
3335     log_info(scc, nmethod)("%d (L%d): Wrote nmethod '%s'%s to Startup Code Cache '%s'",
3336                            task->compile_id(), task->comp_level(), name, (_for_preload ? " (for preload)" : ""), _cache_path);
3337   }
3338   if (VerifyCachedCode) {
3339     return nullptr;
3340   }
3341   return entry;
3342 }
3343 
3344 static void print_helper1(outputStream* st, const char* name, int count) {
3345   if (count > 0) {
3346     st->print(" %s=%d", name, count);
3347   }
3348 }
3349 static void print_helper(outputStream* st, const char* name, int stats[6+3][6], int idx) {
3350   int total = stats[idx][0];
3351   if (total > 0) {
3352     st->print("  %s:", name);
3353     print_helper1(st, "total",               stats[idx][0]);
3354     //print_helper1(st, "for_preload",         stats[idx][2]); // implied by Tier5
3355     print_helper1(st, "loaded",              stats[idx][3]);
3356     print_helper1(st, "invalidated",         stats[idx][4]);
3357     print_helper1(st, "failed",              stats[idx][5]);
3358     print_helper1(st, "has_clinit_barriers", stats[idx][1]);
3359     st->cr();
3360   }
3361 }
3362 
3363 void SCCache::print_statistics_on(outputStream* st) {
3364   SCCache* cache = open_for_read();
3365   if (cache != nullptr) {
3366     ReadingMark rdmk;
3367 
3368     uint count = cache->_load_header->entries_count();
3369     uint* search_entries = (uint*)cache->addr(cache->_load_header->entries_offset()); // [id, index]
3370     SCCEntry* load_entries = (SCCEntry*)(search_entries + 2 * count);
3371 
3372     int stats[6 + 3][6] = {0};
3373     for (uint i = 0; i < count; i++) {
3374       int index = search_entries[2*i + 1];
3375       SCCEntry* entry = &(load_entries[index]);
3376 
3377       int lvl = entry->kind();
3378       if (entry->kind() == SCCEntry::Code) {
3379         lvl += entry->comp_level() + (entry->for_preload() ? 1 : 0);
3380       }
3381       ++stats[lvl][0]; // total
3382       if (entry->has_clinit_barriers()) {
3383         ++stats[lvl][1];
3384       }
3385       if (entry->for_preload()) {
3386         ++stats[lvl][2];
3387       }
3388       if (entry->is_loaded()) {
3389         ++stats[lvl][3];
3390       }
3391       if (entry->not_entrant()) {
3392         ++stats[lvl][4];
3393       }
3394       if (entry->load_fail()) {
3395         ++stats[lvl][5];
3396       }
3397     }
3398 
3399     print_helper(st, "None", stats, SCCEntry::None);
3400     print_helper(st, "Stub", stats, SCCEntry::Stub);
3401     print_helper(st, "Blob", stats, SCCEntry::Blob);
3402     for (int lvl = 0; lvl <= CompLevel_full_optimization + 1; lvl++) {
3403       ResourceMark rm;
3404       stringStream ss;
3405       ss.print("SC T%d", lvl);
3406       print_helper(st, ss.freeze(), stats, SCCEntry::Code + lvl);
3407     }
3408 
3409   } else {
3410     st->print_cr("failed to open SCA at %s", CachedCodeFile);
3411   }
3412 }
3413 
3414 void SCCache::print_on(outputStream* st) {
3415   SCCache* cache = open_for_read();
3416   if (cache != nullptr) {
3417     ReadingMark rdmk;
3418 
3419     uint count = cache->_load_header->entries_count();
3420     uint* search_entries = (uint*)cache->addr(cache->_load_header->entries_offset()); // [id, index]
3421     SCCEntry* load_entries = (SCCEntry*)(search_entries + 2 * count);
3422 
3423     for (uint i = 0; i < count; i++) {
3424       int index = search_entries[2*i + 1];
3425       SCCEntry* entry = &(load_entries[index]);
3426 
3427       st->print_cr("%4u: %4u: K%u L%u offset=%u decompile=%u size=%u code_size=%u%s%s%s%s",
3428                 i, index, entry->kind(), entry->comp_level(), entry->offset(),
3429                 entry->decompile(), entry->size(), entry->code_size(),
3430                 entry->has_clinit_barriers() ? " has_clinit_barriers" : "",
3431                 entry->for_preload()         ? " for_preload"         : "",
3432                 entry->is_loaded()           ? " loaded"              : "",
3433                 entry->not_entrant()         ? " not_entrant"         : "");
3434       st->print_raw("         ");
3435       SCCReader reader(cache, entry, nullptr);
3436       reader.print_on(st);
3437     }
3438   } else {
3439     st->print_cr("failed to open SCA at %s", CachedCodeFile);
3440   }
3441 }
3442 
3443 void SCCache::print_unused_entries_on(outputStream* st) {
3444   LogStreamHandle(Info, scc, init) info;
3445   if (info.is_enabled()) {
3446     SCCache::iterate([&](SCCEntry* entry) {
3447       if (!entry->is_loaded()) {
3448         MethodTrainingData* mtd = MethodTrainingData::lookup_for(entry->method());
3449         if (mtd != nullptr) {
3450           if (mtd->has_holder()) {
3451             if (mtd->holder()->method_holder()->is_initialized()) {
3452               ResourceMark rm;
3453               mtd->iterate_all_compiles([&](CompileTrainingData* ctd) {
3454                 if ((uint)ctd->level() == entry->comp_level()) {
3455                   if (ctd->init_deps_left() == 0) {
3456                     nmethod* nm = mtd->holder()->code();
3457                     if (nm == nullptr) {
3458                       if (mtd->holder()->queued_for_compilation()) {
3459                         return; // scheduled for compilation
3460                       }
3461                     } else if ((uint)nm->comp_level() >= entry->comp_level()) {
3462                       return; // already online compiled and superseded by a more optimal method
3463                     }
3464                     info.print("SCC entry not loaded: ");
3465                     ctd->print_on(&info);
3466                     info.cr();
3467                   }
3468                 }
3469               });
3470             } else {
3471               // not yet initialized
3472             }
3473           } else {
3474             info.print("SCC entry doesn't have a holder: ");
3475             mtd->print_on(&info);
3476             info.cr();
3477           }
3478         }
3479       }
3480     });
3481   }
3482 }
3483 
3484 void SCCReader::print_on(outputStream* st) {
3485   uint entry_position = _entry->offset();
3486   set_read_position(entry_position);
3487 
3488   // Read name
3489   uint name_offset = entry_position + _entry->name_offset();
3490   uint name_size = _entry->name_size(); // Includes '/0'
3491   const char* name = addr(name_offset);
3492 
3493   st->print_cr("  name: %s", name);
3494 }
3495 
3496 #define _extrs_max 80
3497 #define _stubs_max 120
3498 #define _blobs_max 80
3499 #define _shared_blobs_max 16
3500 #define _C2_blobs_max 21
3501 #define _C1_blobs_max (_blobs_max - _shared_blobs_max - _C2_blobs_max)
3502 #define _all_max 280
3503 
3504 #define SET_ADDRESS(type, addr)                           \
3505   {                                                       \
3506     type##_addr[type##_length++] = (address) (addr);      \
3507     assert(type##_length <= type##_max, "increase size"); \
3508   }
3509 
3510 static bool initializing = false;
3511 void SCAddressTable::init() {
3512   if (_complete || initializing) return; // Done already
3513   initializing = true;
3514   _extrs_addr = NEW_C_HEAP_ARRAY(address, _extrs_max, mtCode);
3515   _stubs_addr = NEW_C_HEAP_ARRAY(address, _stubs_max, mtCode);
3516   _blobs_addr = NEW_C_HEAP_ARRAY(address, _blobs_max, mtCode);
3517 
3518   // Divide _blobs_addr array to chunks because they could be initialized in parrallel
3519   _C2_blobs_addr = _blobs_addr + _shared_blobs_max;// C2 blobs addresses stored after shared blobs
3520   _C1_blobs_addr = _C2_blobs_addr + _C2_blobs_max; // C1 blobs addresses stored after C2 blobs
3521 
3522   _extrs_length = 0;
3523   _stubs_length = 0;
3524   _blobs_length = 0;       // for shared blobs
3525   _C1_blobs_length = 0;
3526   _C2_blobs_length = 0;
3527   _final_blobs_length = 0; // Depends on numnber of C1 blobs
3528 
3529   // Runtime methods
3530 #ifdef COMPILER2
3531   SET_ADDRESS(_extrs, OptoRuntime::handle_exception_C);
3532 #endif
3533 #ifdef COMPILER1
3534   SET_ADDRESS(_extrs, Runtime1::is_instance_of);
3535   SET_ADDRESS(_extrs, Runtime1::trace_block_entry);
3536 #endif
3537 
3538   SET_ADDRESS(_extrs, CompressedOops::ptrs_base_addr());
3539   SET_ADDRESS(_extrs, G1BarrierSetRuntime::write_ref_field_post_entry);
3540   SET_ADDRESS(_extrs, G1BarrierSetRuntime::write_ref_field_pre_entry);
3541 
3542   SET_ADDRESS(_extrs, SharedRuntime::complete_monitor_unlocking_C);
3543   SET_ADDRESS(_extrs, SharedRuntime::enable_stack_reserved_zone);
3544 #ifdef AMD64
3545   SET_ADDRESS(_extrs, SharedRuntime::montgomery_multiply);
3546   SET_ADDRESS(_extrs, SharedRuntime::montgomery_square);
3547 #endif // AMD64
3548   SET_ADDRESS(_extrs, SharedRuntime::d2f);
3549   SET_ADDRESS(_extrs, SharedRuntime::d2i);
3550   SET_ADDRESS(_extrs, SharedRuntime::d2l);
3551   SET_ADDRESS(_extrs, SharedRuntime::dcos);
3552   SET_ADDRESS(_extrs, SharedRuntime::dexp);
3553   SET_ADDRESS(_extrs, SharedRuntime::dlog);
3554   SET_ADDRESS(_extrs, SharedRuntime::dlog10);
3555   SET_ADDRESS(_extrs, SharedRuntime::dpow);
3556   SET_ADDRESS(_extrs, SharedRuntime::drem);
3557   SET_ADDRESS(_extrs, SharedRuntime::dsin);
3558   SET_ADDRESS(_extrs, SharedRuntime::dtan);
3559   SET_ADDRESS(_extrs, SharedRuntime::f2i);
3560   SET_ADDRESS(_extrs, SharedRuntime::f2l);
3561   SET_ADDRESS(_extrs, SharedRuntime::frem);
3562   SET_ADDRESS(_extrs, SharedRuntime::l2d);
3563   SET_ADDRESS(_extrs, SharedRuntime::l2f);
3564   SET_ADDRESS(_extrs, SharedRuntime::ldiv);
3565   SET_ADDRESS(_extrs, SharedRuntime::lmul);
3566   SET_ADDRESS(_extrs, SharedRuntime::lrem);
3567 #if INCLUDE_JVMTI
3568   SET_ADDRESS(_extrs, &JvmtiExport::_should_notify_object_alloc);
3569 #endif /* INCLUDE_JVMTI */
3570   BarrierSet* bs = BarrierSet::barrier_set();
3571   if (bs->is_a(BarrierSet::CardTableBarrierSet)) {
3572     SET_ADDRESS(_extrs, ci_card_table_address_as<address>());
3573   }
3574   SET_ADDRESS(_extrs, ThreadIdentifier::unsafe_offset());
3575   SET_ADDRESS(_extrs, Thread::current);
3576 
3577   SET_ADDRESS(_extrs, os::javaTimeMillis);
3578   SET_ADDRESS(_extrs, os::javaTimeNanos);
3579 
3580 #if INCLUDE_JVMTI
3581   SET_ADDRESS(_extrs, &JvmtiVTMSTransitionDisabler::_VTMS_notify_jvmti_events);
3582 #endif /* INCLUDE_JVMTI */
3583   SET_ADDRESS(_extrs, StubRoutines::crc_table_addr());
3584 #ifndef PRODUCT
3585   SET_ADDRESS(_extrs, &SharedRuntime::_partial_subtype_ctr);
3586   SET_ADDRESS(_extrs, JavaThread::verify_cross_modify_fence_failure);
3587 #endif
3588 
3589 #if defined(AMD64) || defined(AARCH64) || defined(RISCV64)
3590   SET_ADDRESS(_extrs, MacroAssembler::debug64);
3591 #endif
3592 #if defined(AMD64)
3593   SET_ADDRESS(_extrs, StubRoutines::x86::arrays_hashcode_powers_of_31());
3594 #endif
3595 
3596 #ifdef X86
3597   SET_ADDRESS(_extrs, LIR_Assembler::float_signmask_pool);
3598   SET_ADDRESS(_extrs, LIR_Assembler::double_signmask_pool);
3599   SET_ADDRESS(_extrs, LIR_Assembler::float_signflip_pool);
3600   SET_ADDRESS(_extrs, LIR_Assembler::double_signflip_pool);
3601 #endif
3602 
3603   // Stubs
3604   SET_ADDRESS(_stubs, StubRoutines::method_entry_barrier());
3605   SET_ADDRESS(_stubs, StubRoutines::forward_exception_entry());
3606 /*
3607   SET_ADDRESS(_stubs, StubRoutines::throw_AbstractMethodError_entry());
3608   SET_ADDRESS(_stubs, StubRoutines::throw_IncompatibleClassChangeError_entry());
3609   SET_ADDRESS(_stubs, StubRoutines::throw_NullPointerException_at_call_entry());
3610   SET_ADDRESS(_stubs, StubRoutines::throw_StackOverflowError_entry());
3611   SET_ADDRESS(_stubs, StubRoutines::throw_delayed_StackOverflowError_entry());
3612 */
3613   SET_ADDRESS(_stubs, StubRoutines::atomic_xchg_entry());
3614   SET_ADDRESS(_stubs, StubRoutines::atomic_cmpxchg_entry());
3615   SET_ADDRESS(_stubs, StubRoutines::atomic_cmpxchg_long_entry());
3616   SET_ADDRESS(_stubs, StubRoutines::atomic_add_entry());
3617   SET_ADDRESS(_stubs, StubRoutines::fence_entry());
3618 
3619   SET_ADDRESS(_stubs, StubRoutines::cont_thaw());
3620   SET_ADDRESS(_stubs, StubRoutines::cont_returnBarrier());
3621   SET_ADDRESS(_stubs, StubRoutines::cont_returnBarrierExc());
3622 
3623   JFR_ONLY(SET_ADDRESS(_stubs, StubRoutines::jfr_write_checkpoint());)
3624 
3625 
3626   SET_ADDRESS(_stubs, StubRoutines::jbyte_arraycopy());
3627   SET_ADDRESS(_stubs, StubRoutines::jshort_arraycopy());
3628   SET_ADDRESS(_stubs, StubRoutines::jint_arraycopy());
3629   SET_ADDRESS(_stubs, StubRoutines::jlong_arraycopy());
3630   SET_ADDRESS(_stubs, StubRoutines::_oop_arraycopy);
3631   SET_ADDRESS(_stubs, StubRoutines::_oop_arraycopy_uninit);
3632 
3633   SET_ADDRESS(_stubs, StubRoutines::jbyte_disjoint_arraycopy());
3634   SET_ADDRESS(_stubs, StubRoutines::jshort_disjoint_arraycopy());
3635   SET_ADDRESS(_stubs, StubRoutines::jint_disjoint_arraycopy());
3636   SET_ADDRESS(_stubs, StubRoutines::jlong_disjoint_arraycopy());
3637   SET_ADDRESS(_stubs, StubRoutines::_oop_disjoint_arraycopy);
3638   SET_ADDRESS(_stubs, StubRoutines::_oop_disjoint_arraycopy_uninit);
3639 
3640   SET_ADDRESS(_stubs, StubRoutines::arrayof_jbyte_arraycopy());
3641   SET_ADDRESS(_stubs, StubRoutines::arrayof_jshort_arraycopy());
3642   SET_ADDRESS(_stubs, StubRoutines::arrayof_jint_arraycopy());
3643   SET_ADDRESS(_stubs, StubRoutines::arrayof_jlong_arraycopy());
3644   SET_ADDRESS(_stubs, StubRoutines::_arrayof_oop_arraycopy);
3645   SET_ADDRESS(_stubs, StubRoutines::_arrayof_oop_arraycopy_uninit);
3646 
3647   SET_ADDRESS(_stubs, StubRoutines::arrayof_jbyte_disjoint_arraycopy());
3648   SET_ADDRESS(_stubs, StubRoutines::arrayof_jshort_disjoint_arraycopy());
3649   SET_ADDRESS(_stubs, StubRoutines::arrayof_jint_disjoint_arraycopy());
3650   SET_ADDRESS(_stubs, StubRoutines::arrayof_jlong_disjoint_arraycopy());
3651   SET_ADDRESS(_stubs, StubRoutines::_arrayof_oop_disjoint_arraycopy);
3652   SET_ADDRESS(_stubs, StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit);
3653 
3654   SET_ADDRESS(_stubs, StubRoutines::_checkcast_arraycopy);
3655   SET_ADDRESS(_stubs, StubRoutines::_checkcast_arraycopy_uninit);
3656 
3657   SET_ADDRESS(_stubs, StubRoutines::unsafe_arraycopy());
3658   SET_ADDRESS(_stubs, StubRoutines::generic_arraycopy());
3659 
3660   SET_ADDRESS(_stubs, StubRoutines::jbyte_fill());
3661   SET_ADDRESS(_stubs, StubRoutines::jshort_fill());
3662   SET_ADDRESS(_stubs, StubRoutines::jint_fill());
3663   SET_ADDRESS(_stubs, StubRoutines::arrayof_jbyte_fill());
3664   SET_ADDRESS(_stubs, StubRoutines::arrayof_jshort_fill());
3665   SET_ADDRESS(_stubs, StubRoutines::arrayof_jint_fill());
3666 
3667   SET_ADDRESS(_stubs, StubRoutines::data_cache_writeback());
3668   SET_ADDRESS(_stubs, StubRoutines::data_cache_writeback_sync());
3669 
3670   SET_ADDRESS(_stubs, StubRoutines::aescrypt_encryptBlock());
3671   SET_ADDRESS(_stubs, StubRoutines::aescrypt_decryptBlock());
3672   SET_ADDRESS(_stubs, StubRoutines::cipherBlockChaining_encryptAESCrypt());
3673   SET_ADDRESS(_stubs, StubRoutines::cipherBlockChaining_decryptAESCrypt());
3674   SET_ADDRESS(_stubs, StubRoutines::electronicCodeBook_encryptAESCrypt());
3675   SET_ADDRESS(_stubs, StubRoutines::electronicCodeBook_decryptAESCrypt());
3676   SET_ADDRESS(_stubs, StubRoutines::poly1305_processBlocks());
3677   SET_ADDRESS(_stubs, StubRoutines::counterMode_AESCrypt());
3678   SET_ADDRESS(_stubs, StubRoutines::ghash_processBlocks());
3679   SET_ADDRESS(_stubs, StubRoutines::chacha20Block());
3680   SET_ADDRESS(_stubs, StubRoutines::base64_encodeBlock());
3681   SET_ADDRESS(_stubs, StubRoutines::base64_decodeBlock());
3682   SET_ADDRESS(_stubs, StubRoutines::md5_implCompress());
3683   SET_ADDRESS(_stubs, StubRoutines::md5_implCompressMB());
3684   SET_ADDRESS(_stubs, StubRoutines::sha1_implCompress());
3685   SET_ADDRESS(_stubs, StubRoutines::sha1_implCompressMB());
3686   SET_ADDRESS(_stubs, StubRoutines::sha256_implCompress());
3687   SET_ADDRESS(_stubs, StubRoutines::sha256_implCompressMB());
3688   SET_ADDRESS(_stubs, StubRoutines::sha512_implCompress());
3689   SET_ADDRESS(_stubs, StubRoutines::sha512_implCompressMB());
3690   SET_ADDRESS(_stubs, StubRoutines::sha3_implCompress());
3691   SET_ADDRESS(_stubs, StubRoutines::sha3_implCompressMB());
3692 
3693   SET_ADDRESS(_stubs, StubRoutines::updateBytesCRC32());
3694 
3695   SET_ADDRESS(_stubs, StubRoutines::crc32c_table_addr());
3696   SET_ADDRESS(_stubs, StubRoutines::updateBytesCRC32C());
3697   SET_ADDRESS(_stubs, StubRoutines::updateBytesAdler32());
3698 
3699   SET_ADDRESS(_stubs, StubRoutines::multiplyToLen());
3700   SET_ADDRESS(_stubs, StubRoutines::squareToLen());
3701   SET_ADDRESS(_stubs, StubRoutines::mulAdd());
3702   SET_ADDRESS(_stubs, StubRoutines::montgomeryMultiply());
3703   SET_ADDRESS(_stubs, StubRoutines::montgomerySquare());
3704   SET_ADDRESS(_stubs, StubRoutines::bigIntegerRightShift());
3705   SET_ADDRESS(_stubs, StubRoutines::bigIntegerLeftShift());
3706   SET_ADDRESS(_stubs, StubRoutines::galoisCounterMode_AESCrypt());
3707 
3708   SET_ADDRESS(_stubs, StubRoutines::vectorizedMismatch());
3709 
3710   SET_ADDRESS(_stubs, StubRoutines::dexp());
3711   SET_ADDRESS(_stubs, StubRoutines::dlog());
3712   SET_ADDRESS(_stubs, StubRoutines::dlog10());
3713   SET_ADDRESS(_stubs, StubRoutines::dpow());
3714   SET_ADDRESS(_stubs, StubRoutines::dsin());
3715   SET_ADDRESS(_stubs, StubRoutines::dcos());
3716   SET_ADDRESS(_stubs, StubRoutines::dlibm_reduce_pi04l());
3717   SET_ADDRESS(_stubs, StubRoutines::dlibm_sin_cos_huge());
3718   SET_ADDRESS(_stubs, StubRoutines::dlibm_tan_cot_huge());
3719   SET_ADDRESS(_stubs, StubRoutines::dtan());
3720 
3721   SET_ADDRESS(_stubs, StubRoutines::f2hf_adr());
3722   SET_ADDRESS(_stubs, StubRoutines::hf2f_adr());
3723 
3724 #if defined(AMD64)
3725   SET_ADDRESS(_stubs, StubRoutines::x86::d2i_fixup());
3726   SET_ADDRESS(_stubs, StubRoutines::x86::f2i_fixup());
3727   SET_ADDRESS(_stubs, StubRoutines::x86::d2l_fixup());
3728   SET_ADDRESS(_stubs, StubRoutines::x86::f2l_fixup());
3729   SET_ADDRESS(_stubs, StubRoutines::x86::float_sign_mask());
3730   SET_ADDRESS(_stubs, StubRoutines::x86::float_sign_flip());
3731   SET_ADDRESS(_stubs, StubRoutines::x86::double_sign_mask());
3732   SET_ADDRESS(_stubs, StubRoutines::x86::double_sign_flip());
3733   SET_ADDRESS(_stubs, StubRoutines::x86::vector_popcount_lut());
3734   SET_ADDRESS(_stubs, StubRoutines::x86::vector_float_sign_mask());
3735   SET_ADDRESS(_stubs, StubRoutines::x86::vector_float_sign_flip());
3736   SET_ADDRESS(_stubs, StubRoutines::x86::vector_double_sign_mask());
3737   SET_ADDRESS(_stubs, StubRoutines::x86::vector_double_sign_flip());
3738   // The iota indices are ordered by type B/S/I/L/F/D, and the offset between two types is 64.
3739   // See C2_MacroAssembler::load_iota_indices().
3740   for (int i = 0; i < 6; i++) {
3741     SET_ADDRESS(_stubs, StubRoutines::x86::vector_iota_indices() + i * 64);
3742   }
3743 #endif
3744 #if defined(AARCH64)
3745   SET_ADDRESS(_stubs, StubRoutines::aarch64::d2i_fixup());
3746   SET_ADDRESS(_stubs, StubRoutines::aarch64::f2i_fixup());
3747   SET_ADDRESS(_stubs, StubRoutines::aarch64::d2l_fixup());
3748   SET_ADDRESS(_stubs, StubRoutines::aarch64::f2l_fixup());
3749   SET_ADDRESS(_stubs, StubRoutines::aarch64::float_sign_mask());
3750   SET_ADDRESS(_stubs, StubRoutines::aarch64::float_sign_flip());
3751   SET_ADDRESS(_stubs, StubRoutines::aarch64::double_sign_mask());
3752   SET_ADDRESS(_stubs, StubRoutines::aarch64::double_sign_flip());
3753   SET_ADDRESS(_stubs, StubRoutines::aarch64::zero_blocks());
3754   SET_ADDRESS(_stubs, StubRoutines::aarch64::count_positives());
3755   SET_ADDRESS(_stubs, StubRoutines::aarch64::count_positives_long());
3756   SET_ADDRESS(_stubs, StubRoutines::aarch64::large_array_equals());
3757   SET_ADDRESS(_stubs, StubRoutines::aarch64::compare_long_string_LL());
3758   SET_ADDRESS(_stubs, StubRoutines::aarch64::compare_long_string_UU());
3759   SET_ADDRESS(_stubs, StubRoutines::aarch64::compare_long_string_LU());
3760   SET_ADDRESS(_stubs, StubRoutines::aarch64::compare_long_string_UL());
3761   SET_ADDRESS(_stubs, StubRoutines::aarch64::string_indexof_linear_ul());
3762   SET_ADDRESS(_stubs, StubRoutines::aarch64::string_indexof_linear_ll());
3763   SET_ADDRESS(_stubs, StubRoutines::aarch64::string_indexof_linear_uu());
3764   SET_ADDRESS(_stubs, StubRoutines::aarch64::large_byte_array_inflate());
3765   SET_ADDRESS(_stubs, StubRoutines::aarch64::spin_wait());
3766 #endif
3767 
3768   // Blobs
3769   SET_ADDRESS(_blobs, SharedRuntime::get_handle_wrong_method_stub());
3770   SET_ADDRESS(_blobs, SharedRuntime::get_ic_miss_stub());
3771   SET_ADDRESS(_blobs, SharedRuntime::get_resolve_opt_virtual_call_stub());
3772   SET_ADDRESS(_blobs, SharedRuntime::get_resolve_virtual_call_stub());
3773   SET_ADDRESS(_blobs, SharedRuntime::get_resolve_static_call_stub());
3774   SET_ADDRESS(_blobs, SharedRuntime::deopt_blob()->entry_point());
3775   SET_ADDRESS(_blobs, SharedRuntime::polling_page_safepoint_handler_blob()->entry_point());
3776   SET_ADDRESS(_blobs, SharedRuntime::polling_page_return_handler_blob()->entry_point());
3777 #ifdef COMPILER2
3778   SET_ADDRESS(_blobs, SharedRuntime::polling_page_vectors_safepoint_handler_blob()->entry_point());
3779   SET_ADDRESS(_blobs, SharedRuntime::uncommon_trap_blob()->entry_point());
3780 #endif
3781   SET_ADDRESS(_blobs, StubRoutines::throw_AbstractMethodError_entry());
3782   SET_ADDRESS(_blobs, StubRoutines::throw_IncompatibleClassChangeError_entry());
3783   SET_ADDRESS(_blobs, StubRoutines::throw_NullPointerException_at_call_entry());
3784   SET_ADDRESS(_blobs, StubRoutines::throw_StackOverflowError_entry());
3785   SET_ADDRESS(_blobs, StubRoutines::throw_delayed_StackOverflowError_entry());
3786 
3787   assert(_blobs_length <= _shared_blobs_max, "increase _shared_blobs_max to %d", _blobs_length);
3788   _final_blobs_length = _blobs_length;
3789   _complete = true;
3790   log_info(scc,init)("External addresses and stubs recorded");
3791 }
3792 
3793 void SCAddressTable::init_opto() {
3794 #ifdef COMPILER2
3795   // OptoRuntime Blobs
3796   SET_ADDRESS(_C2_blobs, OptoRuntime::exception_blob()->entry_point());
3797   SET_ADDRESS(_C2_blobs, OptoRuntime::new_instance_Java());
3798   SET_ADDRESS(_C2_blobs, OptoRuntime::new_array_Java());
3799   SET_ADDRESS(_C2_blobs, OptoRuntime::new_array_nozero_Java());
3800   SET_ADDRESS(_C2_blobs, OptoRuntime::multianewarray2_Java());
3801   SET_ADDRESS(_C2_blobs, OptoRuntime::multianewarray3_Java());
3802   SET_ADDRESS(_C2_blobs, OptoRuntime::multianewarray4_Java());
3803   SET_ADDRESS(_C2_blobs, OptoRuntime::multianewarray5_Java());
3804   SET_ADDRESS(_C2_blobs, OptoRuntime::multianewarrayN_Java());
3805   SET_ADDRESS(_C2_blobs, OptoRuntime::vtable_must_compile_stub());
3806   SET_ADDRESS(_C2_blobs, OptoRuntime::complete_monitor_locking_Java());
3807   SET_ADDRESS(_C2_blobs, OptoRuntime::monitor_notify_Java());
3808   SET_ADDRESS(_C2_blobs, OptoRuntime::monitor_notifyAll_Java());
3809   SET_ADDRESS(_C2_blobs, OptoRuntime::rethrow_stub());
3810   SET_ADDRESS(_C2_blobs, OptoRuntime::slow_arraycopy_Java());
3811   SET_ADDRESS(_C2_blobs, OptoRuntime::register_finalizer_Java());
3812   SET_ADDRESS(_C2_blobs, OptoRuntime::class_init_barrier_Java());
3813 #if INCLUDE_JVMTI
3814   SET_ADDRESS(_C2_blobs, OptoRuntime::notify_jvmti_vthread_start());
3815   SET_ADDRESS(_C2_blobs, OptoRuntime::notify_jvmti_vthread_end());
3816   SET_ADDRESS(_C2_blobs, OptoRuntime::notify_jvmti_vthread_mount());
3817   SET_ADDRESS(_C2_blobs, OptoRuntime::notify_jvmti_vthread_unmount());
3818 #endif /* INCLUDE_JVMTI */
3819 #endif
3820 
3821   assert(_C2_blobs_length <= _C2_blobs_max, "increase _C2_blobs_max to %d", _C2_blobs_length);
3822   _final_blobs_length = MAX2(_final_blobs_length, (_shared_blobs_max + _C2_blobs_length));
3823   _opto_complete = true;
3824   log_info(scc,init)("OptoRuntime Blobs recorded");
3825 }
3826 
3827 void SCAddressTable::init_c1() {
3828 #ifdef COMPILER1
3829   // Runtime1 Blobs
3830   for (int i = 0; i < Runtime1::number_of_ids; i++) {
3831     Runtime1::StubID id = (Runtime1::StubID)i;
3832     if (Runtime1::blob_for(id) == nullptr) {
3833       log_info(scc, init)("C1 blob %s is missing", Runtime1::name_for(id));
3834       continue;
3835     }
3836     if (Runtime1::entry_for(id) == nullptr) {
3837       log_info(scc, init)("C1 blob %s is missing entry", Runtime1::name_for(id));
3838       continue;
3839     }
3840     address entry = Runtime1::entry_for(id);
3841     SET_ADDRESS(_C1_blobs, entry);
3842   }
3843 #if INCLUDE_G1GC
3844   if (UseG1GC) {
3845     G1BarrierSetC1* bs = (G1BarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
3846     address entry = bs->pre_barrier_c1_runtime_code_blob()->code_begin();
3847     SET_ADDRESS(_C1_blobs, entry);
3848     entry = bs->post_barrier_c1_runtime_code_blob()->code_begin();
3849     SET_ADDRESS(_C1_blobs, entry);
3850   }
3851 #endif // INCLUDE_G1GC
3852 #if INCLUDE_ZGC
3853   if (UseZGC) {
3854     ZBarrierSetC1* bs = (ZBarrierSetC1*)BarrierSet::barrier_set()->barrier_set_c1();
3855     SET_ADDRESS(_C1_blobs, bs->_load_barrier_on_oop_field_preloaded_runtime_stub);
3856     SET_ADDRESS(_C1_blobs, bs->_load_barrier_on_weak_oop_field_preloaded_runtime_stub);
3857     SET_ADDRESS(_C1_blobs, bs->_store_barrier_on_oop_field_with_healing);
3858     SET_ADDRESS(_C1_blobs, bs->_store_barrier_on_oop_field_without_healing);
3859   }
3860 #endif // INCLUDE_ZGC
3861 #endif // COMPILER1
3862 
3863   assert(_C1_blobs_length <= _C1_blobs_max, "increase _C1_blobs_max to %d", _C1_blobs_length);
3864   _final_blobs_length = MAX2(_final_blobs_length, (_shared_blobs_max + _C2_blobs_max + _C1_blobs_length));
3865   _c1_complete = true;
3866   log_info(scc,init)("Runtime1 Blobs recorded");
3867 }
3868 
3869 #undef SET_ADDRESS
3870 #undef _extrs_max
3871 #undef _stubs_max
3872 #undef _blobs_max
3873 #undef _shared_blobs_max
3874 #undef _C1_blobs_max
3875 #undef _C2_blobs_max
3876 
3877 SCAddressTable::~SCAddressTable() {
3878   if (_extrs_addr != nullptr) {
3879     FREE_C_HEAP_ARRAY(address, _extrs_addr);
3880   }
3881   if (_stubs_addr != nullptr) {
3882     FREE_C_HEAP_ARRAY(address, _stubs_addr);
3883   }
3884   if (_blobs_addr != nullptr) {
3885     FREE_C_HEAP_ARRAY(address, _blobs_addr);
3886   }
3887 }
3888 
3889 #define MAX_STR_COUNT 200
3890 static const char* _C_strings[MAX_STR_COUNT] = {nullptr};
3891 static int _C_strings_count = 0;
3892 static int _C_strings_s[MAX_STR_COUNT] = {0};
3893 static int _C_strings_id[MAX_STR_COUNT] = {0};
3894 static int _C_strings_len[MAX_STR_COUNT] = {0};
3895 static int _C_strings_hash[MAX_STR_COUNT] = {0};
3896 static int _C_strings_used = 0;
3897 
3898 void SCCache::load_strings() {
3899   uint strings_count  = _load_header->strings_count();
3900   if (strings_count == 0) {
3901     return;
3902   }
3903   uint strings_offset = _load_header->strings_offset();
3904   uint strings_size   = _load_header->entries_offset() - strings_offset;
3905   uint data_size = (uint)(strings_count * sizeof(uint));
3906   uint* sizes = (uint*)addr(strings_offset);
3907   uint* hashs = (uint*)addr(strings_offset + data_size);
3908   strings_size -= 2 * data_size;
3909   _C_strings_buf = addr(strings_offset + 2 * data_size);
3910   const char* p = _C_strings_buf;
3911   assert(strings_count <= MAX_STR_COUNT, "sanity");
3912   for (uint i = 0; i < strings_count; i++) {
3913     _C_strings[i] = p;
3914     uint len = sizes[i];
3915     _C_strings_s[i] = i;
3916     _C_strings_id[i] = i;
3917     _C_strings_len[i] = len;
3918     _C_strings_hash[i] = hashs[i];
3919     p += len;
3920   }
3921   assert((uint)(p - _C_strings_buf) <= strings_size, "(" INTPTR_FORMAT " - " INTPTR_FORMAT ") = %d > %d ", p2i(p), p2i(_C_strings_buf), (uint)(p - _C_strings_buf), strings_size);
3922   _C_strings_count = strings_count;
3923   _C_strings_used  = strings_count;
3924   log_info(scc, init)("Load %d C strings at offset %d from Startup Code Cache '%s'", _C_strings_count, strings_offset, _cache_path);
3925 }
3926 
3927 int SCCache::store_strings() {
3928   uint offset = _write_position;
3929   uint length = 0;
3930   if (_C_strings_used > 0) {
3931     // Write sizes first
3932     for (int i = 0; i < _C_strings_used; i++) {
3933       uint len = _C_strings_len[i] + 1; // Include 0
3934       length += len;
3935       assert(len < 1000, "big string: %s", _C_strings[i]);
3936       uint n = write_bytes(&len, sizeof(uint));
3937       if (n != sizeof(uint)) {
3938         return -1;
3939       }
3940     }
3941     // Write hashs
3942     for (int i = 0; i < _C_strings_used; i++) {
3943       uint n = write_bytes(&(_C_strings_hash[i]), sizeof(uint));
3944       if (n != sizeof(uint)) {
3945         return -1;
3946       }
3947     }
3948     for (int i = 0; i < _C_strings_used; i++) {
3949       uint len = _C_strings_len[i] + 1; // Include 0
3950       uint n = write_bytes(_C_strings[_C_strings_s[i]], len);
3951       if (n != len) {
3952         return -1;
3953       }
3954     }
3955     log_info(scc, exit)("Wrote %d C strings of total length %d at offset %d to Startup Code Cache '%s'",
3956                         _C_strings_used, length, offset, _cache_path);
3957   }
3958   return _C_strings_used;
3959 }
3960 
3961 void SCCache::add_new_C_string(const char* str) {
3962   assert(for_write(), "only when storing code");
3963   _table->add_C_string(str);
3964 }
3965 
3966 void SCAddressTable::add_C_string(const char* str) {
3967   if (str != nullptr && _complete && (_opto_complete || _c1_complete)) {
3968     // Check previous strings address
3969     for (int i = 0; i < _C_strings_count; i++) {
3970       if (_C_strings[i] == str) {
3971         return; // Found existing one
3972       }
3973     }
3974     // Add new one
3975     if (_C_strings_count < MAX_STR_COUNT) {
3976       log_trace(scc)("add_C_string: [%d] " INTPTR_FORMAT " %s", _C_strings_count, p2i(str), str);
3977       _C_strings_id[_C_strings_count] = -1; // Init
3978       _C_strings[_C_strings_count++] = str;
3979     } else {
3980       CompileTask* task = ciEnv::current()->task();
3981       log_info(scc)("%d (L%d): Number of C strings > max %d %s",
3982                        task->compile_id(), task->comp_level(), MAX_STR_COUNT, str);
3983     }
3984   }
3985 }
3986 
3987 int SCAddressTable::id_for_C_string(address str) {
3988   for (int i = 0; i < _C_strings_count; i++) {
3989     if (_C_strings[i] == (const char*)str) { // found
3990       int id = _C_strings_id[i];
3991       if (id >= 0) {
3992         assert(id < _C_strings_used, "%d >= %d", id , _C_strings_used);
3993         return id; // Found recorded
3994       }
3995       // Search for the same string content
3996       int len = (int)strlen((const char*)str);
3997       int hash = java_lang_String::hash_code((const jbyte*)str, len);
3998       for (int j = 0; j < _C_strings_used; j++) {
3999         if ((_C_strings_len[j] == len) && (_C_strings_hash[j] == hash)) {
4000           _C_strings_id[i] = j; // Found match
4001           return j;
4002         }
4003       }
4004       // Not found in recorded, add new
4005       id = _C_strings_used++;
4006       _C_strings_s[id] = i;
4007       _C_strings_id[i] = id;
4008       _C_strings_len[id] = len;
4009       _C_strings_hash[id] = hash;
4010       return id;
4011     }
4012   }
4013   return -1;
4014 }
4015 
4016 address SCAddressTable::address_for_C_string(int idx) {
4017   assert(idx < _C_strings_count, "sanity");
4018   return (address)_C_strings[idx];
4019 }
4020 
4021 int search_address(address addr, address* table, uint length) {
4022   for (int i = 0; i < (int)length; i++) {
4023     if (table[i] == addr) {
4024       return i;
4025     }
4026   }
4027   return -1;
4028 }
4029 
4030 address SCAddressTable::address_for_id(int idx) {
4031   if (!_complete) {
4032     fatal("SCA table is not complete");
4033   }
4034   if (idx == -1) {
4035     return (address)-1;
4036   }
4037   uint id = (uint)idx;
4038   if (id >= _all_max && idx < (_all_max + _C_strings_count)) {
4039     return address_for_C_string(idx - _all_max);
4040   }
4041   if (idx < 0 || id == (_extrs_length + _stubs_length + _final_blobs_length)) {
4042     fatal("Incorrect id %d for SCA table", id);
4043   }
4044   if (idx > (_all_max + _C_strings_count)) {
4045     return (address)os::init + idx;
4046   }
4047   if (id < _extrs_length) {
4048     return _extrs_addr[id];
4049   }
4050   id -= _extrs_length;
4051   if (id < _stubs_length) {
4052     return _stubs_addr[id];
4053   }
4054   id -= _stubs_length;
4055   if (id < _final_blobs_length) {
4056     return _blobs_addr[id];
4057   }
4058   return nullptr;
4059 }
4060 
4061 int SCAddressTable::id_for_address(address addr, RelocIterator reloc, CodeBuffer* buffer) {
4062   int id = -1;
4063   if (addr == (address)-1) { // Static call stub has jump to itself
4064     return id;
4065   }
4066   if (!_complete) {
4067     fatal("SCA table is not complete");
4068   }
4069   // Seach for C string
4070   id = id_for_C_string(addr);
4071   if (id >=0) {
4072     return id + _all_max;
4073   }
4074   if (StubRoutines::contains(addr)) {
4075     // Search in stubs
4076     id = search_address(addr, _stubs_addr, _stubs_length);
4077     if (id < 0) {
4078       StubCodeDesc* desc = StubCodeDesc::desc_for(addr);
4079       if (desc == nullptr) {
4080         desc = StubCodeDesc::desc_for(addr + frame::pc_return_offset);
4081       }
4082       const char* sub_name = (desc != nullptr) ? desc->name() : "<unknown>";
4083       fatal("Address " INTPTR_FORMAT " for Stub:%s is missing in SCA table", p2i(addr), sub_name);
4084     } else {
4085       id += _extrs_length;
4086     }
4087   } else {
4088     CodeBlob* cb = CodeCache::find_blob(addr);
4089     if (cb != nullptr) {
4090       // Search in code blobs
4091       id = search_address(addr, _blobs_addr, _final_blobs_length);
4092       if (id < 0) {
4093         fatal("Address " INTPTR_FORMAT " for Blob:%s is missing in SCA table", p2i(addr), cb->name());
4094       } else {
4095         id += _extrs_length + _stubs_length;
4096       }
4097     } else {
4098       // Search in runtime functions
4099       id = search_address(addr, _extrs_addr, _extrs_length);
4100       if (id < 0) {
4101         ResourceMark rm;
4102         const int buflen = 1024;
4103         char* func_name = NEW_RESOURCE_ARRAY(char, buflen);
4104         int offset = 0;
4105         if (os::dll_address_to_function_name(addr, func_name, buflen, &offset)) {
4106           if (offset > 0) {
4107             // Could be address of C string
4108             uint dist = (uint)pointer_delta(addr, (address)os::init, 1);
4109             CompileTask* task = ciEnv::current()->task();
4110             uint compile_id = 0;
4111             uint comp_level =0;
4112             if (task != nullptr) { // this could be called from compiler runtime initialization (compiler blobs)
4113               compile_id = task->compile_id();
4114               comp_level = task->comp_level();
4115             }
4116             log_info(scc)("%d (L%d): Address " INTPTR_FORMAT " (offset %d) for runtime target '%s' is missing in SCA table",
4117                           compile_id, comp_level, p2i(addr), dist, (const char*)addr);
4118             assert(dist > (uint)(_all_max + MAX_STR_COUNT), "change encoding of distance");
4119             return dist;
4120           }
4121           fatal("Address " INTPTR_FORMAT " for runtime target '%s+%d' is missing in SCA table", p2i(addr), func_name, offset);
4122         } else {
4123           os::print_location(tty, p2i(addr), true);
4124           reloc.print_current_on(tty);
4125 #ifndef PRODUCT
4126           buffer->print_on(tty);
4127           buffer->decode();
4128 #endif // !PRODUCT
4129           fatal("Address " INTPTR_FORMAT " for <unknown> is missing in SCA table", p2i(addr));
4130         }
4131       }
4132     }
4133   }
4134   return id;
4135 }