1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderData.hpp"
  27 #include "classfile/javaClasses.inline.hpp"
  28 #include "classfile/metadataOnStackMark.hpp"
  29 #include "classfile/stringTable.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "interpreter/linkResolver.hpp"
  33 #include "memory/heapInspection.hpp"
  34 #include "memory/metadataFactory.hpp"
  35 #include "memory/metaspaceClosure.hpp"
  36 #include "memory/metaspaceShared.hpp"
  37 #include "memory/oopFactory.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "oops/constantPool.hpp"
  40 #include "oops/instanceKlass.hpp"
  41 #include "oops/objArrayKlass.hpp"
  42 #include "oops/objArrayOop.inline.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "prims/jvm.h"
  45 #include "runtime/fieldType.hpp"
  46 #include "runtime/init.hpp"
  47 #include "runtime/javaCalls.hpp"
  48 #include "runtime/signature.hpp"
  49 #include "runtime/vframe.hpp"
  50 #include "utilities/copy.hpp"
  51 #if INCLUDE_ALL_GCS
  52 #include "gc/g1/g1SATBCardTableModRefBS.hpp"
  53 #endif // INCLUDE_ALL_GCS
  54 
  55 ConstantPool* ConstantPool::allocate(ClassLoaderData* loader_data, int length, TRAPS) {
  56   Array<u1>* tags = MetadataFactory::new_array<u1>(loader_data, length, 0, CHECK_NULL);
  57   int size = ConstantPool::size(length);
  58   return new (loader_data, size, MetaspaceObj::ConstantPoolType, THREAD) ConstantPool(tags);
  59 }
  60 
  61 #ifdef ASSERT
  62 
  63 // MetaspaceObj allocation invariant is calloc equivalent memory
  64 // simple verification of this here (JVM_CONSTANT_Invalid == 0 )
  65 static bool tag_array_is_zero_initialized(Array<u1>* tags) {
  66   assert(tags != NULL, "invariant");
  67   const int length = tags->length();
  68   for (int index = 0; index < length; ++index) {
  69     if (JVM_CONSTANT_Invalid != tags->at(index)) {
  70       return false;
  71     }
  72   }
  73   return true;
  74 }
  75 
  76 #endif
  77 
  78 ConstantPool::ConstantPool(Array<u1>* tags) :
  79   _tags(tags),
  80   _length(tags->length()) {
  81 
  82     assert(_tags != NULL, "invariant");
  83     assert(tags->length() == _length, "invariant");
  84     assert(tag_array_is_zero_initialized(tags), "invariant");
  85     assert(0 == flags(), "invariant");
  86     assert(0 == version(), "invariant");
  87     assert(NULL == _pool_holder, "invariant");
  88 }
  89 
  90 void ConstantPool::deallocate_contents(ClassLoaderData* loader_data) {
  91   if (cache() != NULL) {
  92     MetadataFactory::free_metadata(loader_data, cache());
  93     set_cache(NULL);
  94   }
  95 
  96   MetadataFactory::free_array<Klass*>(loader_data, resolved_klasses());
  97   set_resolved_klasses(NULL);
  98 
  99   MetadataFactory::free_array<jushort>(loader_data, operands());
 100   set_operands(NULL);
 101 
 102   release_C_heap_structures();
 103 
 104   // free tag array
 105   MetadataFactory::free_array<u1>(loader_data, tags());
 106   set_tags(NULL);
 107 }
 108 
 109 void ConstantPool::release_C_heap_structures() {
 110   // walk constant pool and decrement symbol reference counts
 111   unreference_symbols();
 112 }
 113 
 114 void ConstantPool::metaspace_pointers_do(MetaspaceClosure* it) {
 115   log_trace(cds)("Iter(ConstantPool): %p", this);
 116 
 117   it->push(&_tags, MetaspaceClosure::_writable);
 118   it->push(&_cache);
 119   it->push(&_pool_holder);
 120   it->push(&_operands);
 121   it->push(&_resolved_klasses, MetaspaceClosure::_writable);
 122 
 123   for (int i = 0; i < length(); i++) {
 124     // The only MSO's embedded in the CP entries are Symbols:
 125     //   JVM_CONSTANT_String (normal and pseudo)
 126     //   JVM_CONSTANT_Utf8
 127     constantTag ctag = tag_at(i);
 128     if (ctag.is_string() || ctag.is_utf8()) {
 129       it->push(symbol_at_addr(i));
 130     }
 131   }
 132 }
 133 
 134 objArrayOop ConstantPool::resolved_references() const {
 135   return (objArrayOop)_cache->resolved_references();
 136 }
 137 
 138 // Create resolved_references array and mapping array for original cp indexes
 139 // The ldc bytecode was rewritten to have the resolved reference array index so need a way
 140 // to map it back for resolving and some unlikely miscellaneous uses.
 141 // The objects created by invokedynamic are appended to this list.
 142 void ConstantPool::initialize_resolved_references(ClassLoaderData* loader_data,
 143                                                   const intStack& reference_map,
 144                                                   int constant_pool_map_length,
 145                                                   TRAPS) {
 146   // Initialized the resolved object cache.
 147   int map_length = reference_map.length();
 148   if (map_length > 0) {
 149     // Only need mapping back to constant pool entries.  The map isn't used for
 150     // invokedynamic resolved_reference entries.  For invokedynamic entries,
 151     // the constant pool cache index has the mapping back to both the constant
 152     // pool and to the resolved reference index.
 153     if (constant_pool_map_length > 0) {
 154       Array<u2>* om = MetadataFactory::new_array<u2>(loader_data, constant_pool_map_length, CHECK);
 155 
 156       for (int i = 0; i < constant_pool_map_length; i++) {
 157         int x = reference_map.at(i);
 158         assert(x == (int)(jushort) x, "klass index is too big");
 159         om->at_put(i, (jushort)x);
 160       }
 161       set_reference_map(om);
 162     }
 163 
 164     // Create Java array for holding resolved strings, methodHandles,
 165     // methodTypes, invokedynamic and invokehandle appendix objects, etc.
 166     objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 167     Handle refs_handle (THREAD, (oop)stom);  // must handleize.
 168     set_resolved_references(loader_data->add_handle(refs_handle));
 169   }
 170 }
 171 
 172 void ConstantPool::allocate_resolved_klasses(ClassLoaderData* loader_data, int num_klasses, TRAPS) {
 173   // A ConstantPool can't possibly have 0xffff valid class entries,
 174   // because entry #0 must be CONSTANT_Invalid, and each class entry must refer to a UTF8
 175   // entry for the class's name. So at most we will have 0xfffe class entries.
 176   // This allows us to use 0xffff (ConstantPool::_temp_resolved_klass_index) to indicate
 177   // UnresolvedKlass entries that are temporarily created during class redefinition.
 178   assert(num_klasses < CPKlassSlot::_temp_resolved_klass_index, "sanity");
 179   assert(resolved_klasses() == NULL, "sanity");
 180   Array<Klass*>* rk = MetadataFactory::new_array<Klass*>(loader_data, num_klasses, CHECK);
 181   set_resolved_klasses(rk);
 182 }
 183 
 184 void ConstantPool::initialize_unresolved_klasses(ClassLoaderData* loader_data, TRAPS) {
 185   int len = length();
 186   int num_klasses = 0;
 187   for (int i = 1; i <len; i++) {
 188     switch (tag_at(i).value()) {
 189     case JVM_CONSTANT_ClassIndex:
 190       {
 191         const int class_index = klass_index_at(i);
 192         unresolved_klass_at_put(i, class_index, num_klasses++);
 193       }
 194       break;
 195 #ifndef PRODUCT
 196     case JVM_CONSTANT_Class:
 197     case JVM_CONSTANT_UnresolvedClass:
 198     case JVM_CONSTANT_UnresolvedClassInError:
 199       // All of these should have been reverted back to ClassIndex before calling
 200       // this function.
 201       ShouldNotReachHere();
 202 #endif
 203     }
 204   }
 205   allocate_resolved_klasses(loader_data, num_klasses, THREAD);
 206 }
 207 
 208 // Anonymous class support:
 209 void ConstantPool::klass_at_put(int class_index, int name_index, int resolved_klass_index, Klass* k, Symbol* name) {
 210   assert(is_within_bounds(class_index), "index out of bounds");
 211   assert(is_within_bounds(name_index), "index out of bounds");
 212   assert((resolved_klass_index & 0xffff0000) == 0, "must be");
 213   *int_at_addr(class_index) =
 214     build_int_from_shorts((jushort)resolved_klass_index, (jushort)name_index);
 215 
 216   symbol_at_put(name_index, name);
 217   name->increment_refcount();
 218   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 219   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 220 
 221   // The interpreter assumes when the tag is stored, the klass is resolved
 222   // and the Klass* non-NULL, so we need hardware store ordering here.
 223   if (k != NULL) {
 224     release_tag_at_put(class_index, JVM_CONSTANT_Class);
 225   } else {
 226     release_tag_at_put(class_index, JVM_CONSTANT_UnresolvedClass);
 227   }
 228 }
 229 
 230 // Anonymous class support:
 231 void ConstantPool::klass_at_put(int class_index, Klass* k) {
 232   assert(k != NULL, "must be valid klass");
 233   CPKlassSlot kslot = klass_slot_at(class_index);
 234   int resolved_klass_index = kslot.resolved_klass_index();
 235   Klass** adr = resolved_klasses()->adr_at(resolved_klass_index);
 236   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 237 
 238   // The interpreter assumes when the tag is stored, the klass is resolved
 239   // and the Klass* non-NULL, so we need hardware store ordering here.
 240   release_tag_at_put(class_index, JVM_CONSTANT_Class);
 241 }
 242 
 243 #if INCLUDE_CDS_JAVA_HEAP
 244 // Archive the resolved references
 245 void ConstantPool::archive_resolved_references(Thread* THREAD) {
 246   if (_cache == NULL) {
 247     return; // nothing to do
 248   }
 249 
 250   InstanceKlass *ik = pool_holder();
 251   if (!(ik->is_shared_boot_class() || ik->is_shared_platform_class() ||
 252         ik->is_shared_app_class())) {
 253     // Archiving resolved references for classes from non-builtin loaders
 254     // is not yet supported.
 255     set_resolved_references(NULL);
 256     return;
 257   }
 258 
 259   objArrayOop rr = resolved_references();
 260   Array<u2>* ref_map = reference_map();
 261   if (rr != NULL) {
 262     int ref_map_len = ref_map == NULL ? 0 : ref_map->length();
 263     int rr_len = rr->length();
 264     for (int i = 0; i < rr_len; i++) {
 265       oop p = rr->obj_at(i);
 266       rr->obj_at_put(i, NULL);
 267       if (p != NULL && i < ref_map_len) {
 268         int index = object_to_cp_index(i);
 269         // Skip the entry if the string hash code is 0 since the string
 270         // is not included in the shared string_table, see StringTable::copy_shared_string.
 271         if (tag_at(index).is_string() && java_lang_String::hash_code(p) != 0) {
 272           oop op = StringTable::create_archived_string(p, THREAD);
 273           // If the String object is not archived (possibly too large),
 274           // NULL is returned. Also set it in the array, so we won't
 275           // have a 'bad' reference in the archived resolved_reference
 276           // array.
 277           rr->obj_at_put(i, op);
 278         }
 279       }
 280     }
 281 
 282     oop archived = MetaspaceShared::archive_heap_object(rr, THREAD);
 283     _cache->set_archived_references(archived);
 284     set_resolved_references(NULL);
 285   }
 286 }
 287 
 288 bool ConstantPool::resolve_class_constants(TRAPS) {
 289   assert(DumpSharedSpaces, "used during dump time only");
 290   // The _cache may be NULL if the _pool_holder klass fails verification
 291   // at dump time due to missing dependencies.
 292   if (cache() == NULL || reference_map() == NULL) {
 293     return true; // nothing to do
 294   }
 295 
 296   constantPoolHandle cp(THREAD, this);
 297   for (int index = 1; index < length(); index++) { // Index 0 is unused
 298     if (tag_at(index).is_string()) {
 299       Symbol* sym = cp->unresolved_string_at(index);
 300       // Look up only. Only resolve references to already interned strings.
 301       oop str = StringTable::lookup(sym);
 302       if (str != NULL) {
 303         int cache_index = cp->cp_to_object_index(index);
 304         cp->string_at_put(index, cache_index, str);
 305       }
 306     }
 307   }
 308   return true;
 309 }
 310 #endif
 311 
 312 // CDS support. Create a new resolved_references array.
 313 void ConstantPool::restore_unshareable_info(TRAPS) {
 314   assert(is_constantPool(), "ensure C++ vtable is restored");
 315   assert(on_stack(), "should always be set for shared constant pools");
 316   assert(is_shared(), "should always be set for shared constant pools");
 317   assert(_cache != NULL, "constant pool _cache should not be NULL");
 318 
 319   // Only create the new resolved references array if it hasn't been attempted before
 320   if (resolved_references() != NULL) return;
 321 
 322   // restore the C++ vtable from the shared archive
 323   restore_vtable();
 324 
 325   if (SystemDictionary::Object_klass_loaded()) {
 326     ClassLoaderData* loader_data = pool_holder()->class_loader_data();
 327 #if INCLUDE_CDS_JAVA_HEAP
 328     if (MetaspaceShared::open_archive_heap_region_mapped() &&
 329         _cache->archived_references() != NULL) {
 330       oop archived = _cache->archived_references();
 331       // Make sure GC knows the cached object is now live. This is necessary after
 332       // initial GC marking and during concurrent marking as strong roots are only
 333       // scanned during initial marking (at the start of the GC marking).
 334       assert(UseG1GC, "Requires G1 GC");
 335       G1SATBCardTableModRefBS::enqueue(archived);
 336       // Create handle for the archived resolved reference array object
 337       Handle refs_handle(THREAD, (oop)archived);
 338       set_resolved_references(loader_data->add_handle(refs_handle));
 339     } else
 340 #endif
 341     {
 342       // No mapped archived resolved reference array
 343       // Recreate the object array and add to ClassLoaderData.
 344       int map_length = resolved_reference_length();
 345       if (map_length > 0) {
 346         objArrayOop stom = oopFactory::new_objArray(SystemDictionary::Object_klass(), map_length, CHECK);
 347         Handle refs_handle(THREAD, (oop)stom);  // must handleize.
 348         set_resolved_references(loader_data->add_handle(refs_handle));
 349       }
 350     }
 351   }
 352 }
 353 
 354 void ConstantPool::remove_unshareable_info() {
 355   // Resolved references are not in the shared archive.
 356   // Save the length for restoration.  It is not necessarily the same length
 357   // as reference_map.length() if invokedynamic is saved. It is needed when
 358   // re-creating the resolved reference array if archived heap data cannot be map
 359   // at runtime.
 360   set_resolved_reference_length(
 361     resolved_references() != NULL ? resolved_references()->length() : 0);
 362 
 363   // If archiving heap objects is not allowed, clear the resolved references.
 364   // Otherwise, it is cleared after the resolved references array is cached
 365   // (see archive_resolved_references()).
 366   if (!MetaspaceShared::is_heap_object_archiving_allowed()) {
 367     set_resolved_references(NULL);
 368   }
 369 
 370   // Shared ConstantPools are in the RO region, so the _flags cannot be modified.
 371   // The _on_stack flag is used to prevent ConstantPools from deallocation during
 372   // class redefinition. Since shared ConstantPools cannot be deallocated anyway,
 373   // we always set _on_stack to true to avoid having to change _flags during runtime.
 374   _flags |= (_on_stack | _is_shared);
 375   int num_klasses = 0;
 376   for (int index = 1; index < length(); index++) { // Index 0 is unused
 377     assert(!tag_at(index).is_unresolved_klass_in_error(), "This must not happen during dump time");
 378     if (tag_at(index).is_klass()) {
 379       // This class was resolved as a side effect of executing Java code
 380       // during dump time. We need to restore it back to an UnresolvedClass,
 381       // so that the proper class loading and initialization can happen
 382       // at runtime.
 383       CPKlassSlot kslot = klass_slot_at(index);
 384       int resolved_klass_index = kslot.resolved_klass_index();
 385       int name_index = kslot.name_index();
 386       assert(tag_at(name_index).is_symbol(), "sanity");
 387       resolved_klasses()->at_put(resolved_klass_index, NULL);
 388       tag_at_put(index, JVM_CONSTANT_UnresolvedClass);
 389       assert(klass_name_at(index) == symbol_at(name_index), "sanity");
 390     }
 391   }
 392   if (cache() != NULL) {
 393     cache()->remove_unshareable_info();
 394   }
 395 }
 396 
 397 int ConstantPool::cp_to_object_index(int cp_index) {
 398   // this is harder don't do this so much.
 399   int i = reference_map()->find(cp_index);
 400   // We might not find the index for jsr292 call.
 401   return (i < 0) ? _no_index_sentinel : i;
 402 }
 403 
 404 void ConstantPool::string_at_put(int which, int obj_index, oop str) {
 405   resolved_references()->obj_at_put(obj_index, str);
 406 }
 407 
 408 void ConstantPool::trace_class_resolution(const constantPoolHandle& this_cp, Klass* k) {
 409   ResourceMark rm;
 410   int line_number = -1;
 411   const char * source_file = NULL;
 412   if (JavaThread::current()->has_last_Java_frame()) {
 413     // try to identify the method which called this function.
 414     vframeStream vfst(JavaThread::current());
 415     if (!vfst.at_end()) {
 416       line_number = vfst.method()->line_number_from_bci(vfst.bci());
 417       Symbol* s = vfst.method()->method_holder()->source_file_name();
 418       if (s != NULL) {
 419         source_file = s->as_C_string();
 420       }
 421     }
 422   }
 423   if (k != this_cp->pool_holder()) {
 424     // only print something if the classes are different
 425     if (source_file != NULL) {
 426       log_debug(class, resolve)("%s %s %s:%d",
 427                  this_cp->pool_holder()->external_name(),
 428                  k->external_name(), source_file, line_number);
 429     } else {
 430       log_debug(class, resolve)("%s %s",
 431                  this_cp->pool_holder()->external_name(),
 432                  k->external_name());
 433     }
 434   }
 435 }
 436 
 437 Klass* ConstantPool::klass_at_impl(const constantPoolHandle& this_cp, int which,
 438                                    bool save_resolution_error, TRAPS) {
 439   assert(THREAD->is_Java_thread(), "must be a Java thread");
 440 
 441   // A resolved constantPool entry will contain a Klass*, otherwise a Symbol*.
 442   // It is not safe to rely on the tag bit's here, since we don't have a lock, and
 443   // the entry and tag is not updated atomicly.
 444   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 445   int resolved_klass_index = kslot.resolved_klass_index();
 446   int name_index = kslot.name_index();
 447   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 448 
 449   Klass* klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 450   if (klass != NULL) {
 451     return klass;
 452   }
 453 
 454   // This tag doesn't change back to unresolved class unless at a safepoint.
 455   if (this_cp->tag_at(which).is_unresolved_klass_in_error()) {
 456     // The original attempt to resolve this constant pool entry failed so find the
 457     // class of the original error and throw another error of the same class
 458     // (JVMS 5.4.3).
 459     // If there is a detail message, pass that detail message to the error.
 460     // The JVMS does not strictly require us to duplicate the same detail message,
 461     // or any internal exception fields such as cause or stacktrace.  But since the
 462     // detail message is often a class name or other literal string, we will repeat it
 463     // if we can find it in the symbol table.
 464     throw_resolution_error(this_cp, which, CHECK_0);
 465     ShouldNotReachHere();
 466   }
 467 
 468   Handle mirror_handle;
 469   Symbol* name = this_cp->symbol_at(name_index);
 470   Handle loader (THREAD, this_cp->pool_holder()->class_loader());
 471   Handle protection_domain (THREAD, this_cp->pool_holder()->protection_domain());
 472   Klass* k = SystemDictionary::resolve_or_fail(name, loader, protection_domain, true, THREAD);
 473   if (!HAS_PENDING_EXCEPTION) {
 474     // preserve the resolved klass from unloading
 475     mirror_handle = Handle(THREAD, k->java_mirror());
 476     // Do access check for klasses
 477     verify_constant_pool_resolve(this_cp, k, THREAD);
 478   }
 479 
 480   // Failed to resolve class. We must record the errors so that subsequent attempts
 481   // to resolve this constant pool entry fail with the same error (JVMS 5.4.3).
 482   if (HAS_PENDING_EXCEPTION) {
 483     if (save_resolution_error) {
 484       save_and_throw_exception(this_cp, which, constantTag(JVM_CONSTANT_UnresolvedClass), CHECK_NULL);
 485       // If CHECK_NULL above doesn't return the exception, that means that
 486       // some other thread has beaten us and has resolved the class.
 487       // To preserve old behavior, we return the resolved class.
 488       klass = this_cp->resolved_klasses()->at(resolved_klass_index);
 489       assert(klass != NULL, "must be resolved if exception was cleared");
 490       return klass;
 491     } else {
 492       return NULL;  // return the pending exception
 493     }
 494   }
 495 
 496   // Make this class loader depend upon the class loader owning the class reference
 497   ClassLoaderData* this_key = this_cp->pool_holder()->class_loader_data();
 498   this_key->record_dependency(k, CHECK_NULL); // Can throw OOM
 499 
 500   // logging for class+resolve.
 501   if (log_is_enabled(Debug, class, resolve)){
 502     trace_class_resolution(this_cp, k);
 503   }
 504   Klass** adr = this_cp->resolved_klasses()->adr_at(resolved_klass_index);
 505   OrderAccess::release_store_ptr((Klass* volatile *)adr, k);
 506   // The interpreter assumes when the tag is stored, the klass is resolved
 507   // and the Klass* stored in _resolved_klasses is non-NULL, so we need
 508   // hardware store ordering here.
 509   this_cp->release_tag_at_put(which, JVM_CONSTANT_Class);
 510   return k;
 511 }
 512 
 513 
 514 // Does not update ConstantPool* - to avoid any exception throwing. Used
 515 // by compiler and exception handling.  Also used to avoid classloads for
 516 // instanceof operations. Returns NULL if the class has not been loaded or
 517 // if the verification of constant pool failed
 518 Klass* ConstantPool::klass_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 519   CPKlassSlot kslot = this_cp->klass_slot_at(which);
 520   int resolved_klass_index = kslot.resolved_klass_index();
 521   int name_index = kslot.name_index();
 522   assert(this_cp->tag_at(name_index).is_symbol(), "sanity");
 523 
 524   Klass* k = this_cp->resolved_klasses()->at(resolved_klass_index);
 525   if (k != NULL) {
 526     return k;
 527   } else {
 528     Thread *thread = Thread::current();
 529     Symbol* name = this_cp->symbol_at(name_index);
 530     oop loader = this_cp->pool_holder()->class_loader();
 531     oop protection_domain = this_cp->pool_holder()->protection_domain();
 532     Handle h_prot (thread, protection_domain);
 533     Handle h_loader (thread, loader);
 534     Klass* k = SystemDictionary::find(name, h_loader, h_prot, thread);
 535 
 536     if (k != NULL) {
 537       // Make sure that resolving is legal
 538       EXCEPTION_MARK;
 539       // return NULL if verification fails
 540       verify_constant_pool_resolve(this_cp, k, THREAD);
 541       if (HAS_PENDING_EXCEPTION) {
 542         CLEAR_PENDING_EXCEPTION;
 543         return NULL;
 544       }
 545       return k;
 546     } else {
 547       return k;
 548     }
 549   }
 550 }
 551 
 552 
 553 Klass* ConstantPool::klass_ref_at_if_loaded(const constantPoolHandle& this_cp, int which) {
 554   return klass_at_if_loaded(this_cp, this_cp->klass_ref_index_at(which));
 555 }
 556 
 557 
 558 Method* ConstantPool::method_at_if_loaded(const constantPoolHandle& cpool,
 559                                                    int which) {
 560   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
 561   int cache_index = decode_cpcache_index(which, true);
 562   if (!(cache_index >= 0 && cache_index < cpool->cache()->length())) {
 563     // FIXME: should be an assert
 564     log_debug(class, resolve)("bad operand %d in:", which); cpool->print();
 565     return NULL;
 566   }
 567   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 568   return e->method_if_resolved(cpool);
 569 }
 570 
 571 
 572 bool ConstantPool::has_appendix_at_if_loaded(const constantPoolHandle& cpool, int which) {
 573   if (cpool->cache() == NULL)  return false;  // nothing to load yet
 574   int cache_index = decode_cpcache_index(which, true);
 575   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 576   return e->has_appendix();
 577 }
 578 
 579 oop ConstantPool::appendix_at_if_loaded(const constantPoolHandle& cpool, int which) {
 580   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
 581   int cache_index = decode_cpcache_index(which, true);
 582   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 583   return e->appendix_if_resolved(cpool);
 584 }
 585 
 586 
 587 bool ConstantPool::has_method_type_at_if_loaded(const constantPoolHandle& cpool, int which) {
 588   if (cpool->cache() == NULL)  return false;  // nothing to load yet
 589   int cache_index = decode_cpcache_index(which, true);
 590   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 591   return e->has_method_type();
 592 }
 593 
 594 oop ConstantPool::method_type_at_if_loaded(const constantPoolHandle& cpool, int which) {
 595   if (cpool->cache() == NULL)  return NULL;  // nothing to load yet
 596   int cache_index = decode_cpcache_index(which, true);
 597   ConstantPoolCacheEntry* e = cpool->cache()->entry_at(cache_index);
 598   return e->method_type_if_resolved(cpool);
 599 }
 600 
 601 
 602 Symbol* ConstantPool::impl_name_ref_at(int which, bool uncached) {
 603   int name_index = name_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
 604   return symbol_at(name_index);
 605 }
 606 
 607 
 608 Symbol* ConstantPool::impl_signature_ref_at(int which, bool uncached) {
 609   int signature_index = signature_ref_index_at(impl_name_and_type_ref_index_at(which, uncached));
 610   return symbol_at(signature_index);
 611 }
 612 
 613 
 614 int ConstantPool::impl_name_and_type_ref_index_at(int which, bool uncached) {
 615   int i = which;
 616   if (!uncached && cache() != NULL) {
 617     if (ConstantPool::is_invokedynamic_index(which)) {
 618       // Invokedynamic index is index into the constant pool cache
 619       int pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
 620       pool_index = invoke_dynamic_name_and_type_ref_index_at(pool_index);
 621       assert(tag_at(pool_index).is_name_and_type(), "");
 622       return pool_index;
 623     }
 624     // change byte-ordering and go via cache
 625     i = remap_instruction_operand_from_cache(which);
 626   } else {
 627     if (tag_at(which).is_invoke_dynamic()) {
 628       int pool_index = invoke_dynamic_name_and_type_ref_index_at(which);
 629       assert(tag_at(pool_index).is_name_and_type(), "");
 630       return pool_index;
 631     }
 632   }
 633   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
 634   assert(!tag_at(i).is_invoke_dynamic(), "Must be handled above");
 635   jint ref_index = *int_at_addr(i);
 636   return extract_high_short_from_int(ref_index);
 637 }
 638 
 639 constantTag ConstantPool::impl_tag_ref_at(int which, bool uncached) {
 640   int pool_index = which;
 641   if (!uncached && cache() != NULL) {
 642     if (ConstantPool::is_invokedynamic_index(which)) {
 643       // Invokedynamic index is index into resolved_references
 644       pool_index = invokedynamic_cp_cache_entry_at(which)->constant_pool_index();
 645     } else {
 646       // change byte-ordering and go via cache
 647       pool_index = remap_instruction_operand_from_cache(which);
 648     }
 649   }
 650   return tag_at(pool_index);
 651 }
 652 
 653 int ConstantPool::impl_klass_ref_index_at(int which, bool uncached) {
 654   guarantee(!ConstantPool::is_invokedynamic_index(which),
 655             "an invokedynamic instruction does not have a klass");
 656   int i = which;
 657   if (!uncached && cache() != NULL) {
 658     // change byte-ordering and go via cache
 659     i = remap_instruction_operand_from_cache(which);
 660   }
 661   assert(tag_at(i).is_field_or_method(), "Corrupted constant pool");
 662   jint ref_index = *int_at_addr(i);
 663   return extract_low_short_from_int(ref_index);
 664 }
 665 
 666 
 667 
 668 int ConstantPool::remap_instruction_operand_from_cache(int operand) {
 669   int cpc_index = operand;
 670   DEBUG_ONLY(cpc_index -= CPCACHE_INDEX_TAG);
 671   assert((int)(u2)cpc_index == cpc_index, "clean u2");
 672   int member_index = cache()->entry_at(cpc_index)->constant_pool_index();
 673   return member_index;
 674 }
 675 
 676 
 677 void ConstantPool::verify_constant_pool_resolve(const constantPoolHandle& this_cp, Klass* k, TRAPS) {
 678  if (k->is_instance_klass() || k->is_objArray_klass()) {
 679     InstanceKlass* holder = this_cp->pool_holder();
 680     Klass* elem = k->is_instance_klass() ? k : ObjArrayKlass::cast(k)->bottom_klass();
 681 
 682     // The element type could be a typeArray - we only need the access check if it is
 683     // an reference to another class
 684     if (elem->is_instance_klass()) {
 685       LinkResolver::check_klass_accessability(holder, elem, CHECK);
 686     }
 687   }
 688 }
 689 
 690 
 691 int ConstantPool::name_ref_index_at(int which_nt) {
 692   jint ref_index = name_and_type_at(which_nt);
 693   return extract_low_short_from_int(ref_index);
 694 }
 695 
 696 
 697 int ConstantPool::signature_ref_index_at(int which_nt) {
 698   jint ref_index = name_and_type_at(which_nt);
 699   return extract_high_short_from_int(ref_index);
 700 }
 701 
 702 
 703 Klass* ConstantPool::klass_ref_at(int which, TRAPS) {
 704   return klass_at(klass_ref_index_at(which), THREAD);
 705 }
 706 
 707 Symbol* ConstantPool::klass_name_at(int which) const {
 708   return symbol_at(klass_slot_at(which).name_index());
 709 }
 710 
 711 Symbol* ConstantPool::klass_ref_at_noresolve(int which) {
 712   jint ref_index = klass_ref_index_at(which);
 713   return klass_at_noresolve(ref_index);
 714 }
 715 
 716 Symbol* ConstantPool::uncached_klass_ref_at_noresolve(int which) {
 717   jint ref_index = uncached_klass_ref_index_at(which);
 718   return klass_at_noresolve(ref_index);
 719 }
 720 
 721 char* ConstantPool::string_at_noresolve(int which) {
 722   return unresolved_string_at(which)->as_C_string();
 723 }
 724 
 725 BasicType ConstantPool::basic_type_for_signature_at(int which) const {
 726   return FieldType::basic_type(symbol_at(which));
 727 }
 728 
 729 
 730 void ConstantPool::resolve_string_constants_impl(const constantPoolHandle& this_cp, TRAPS) {
 731   for (int index = 1; index < this_cp->length(); index++) { // Index 0 is unused
 732     if (this_cp->tag_at(index).is_string()) {
 733       this_cp->string_at(index, CHECK);
 734     }
 735   }
 736 }
 737 
 738 Symbol* ConstantPool::exception_message(const constantPoolHandle& this_cp, int which, constantTag tag, oop pending_exception) {
 739   // Dig out the detailed message to reuse if possible
 740   Symbol* message = java_lang_Throwable::detail_message(pending_exception);
 741   if (message != NULL) {
 742     return message;
 743   }
 744 
 745   // Return specific message for the tag
 746   switch (tag.value()) {
 747   case JVM_CONSTANT_UnresolvedClass:
 748     // return the class name in the error message
 749     message = this_cp->klass_name_at(which);
 750     break;
 751   case JVM_CONSTANT_MethodHandle:
 752     // return the method handle name in the error message
 753     message = this_cp->method_handle_name_ref_at(which);
 754     break;
 755   case JVM_CONSTANT_MethodType:
 756     // return the method type signature in the error message
 757     message = this_cp->method_type_signature_at(which);
 758     break;
 759   default:
 760     ShouldNotReachHere();
 761   }
 762 
 763   return message;
 764 }
 765 
 766 void ConstantPool::throw_resolution_error(const constantPoolHandle& this_cp, int which, TRAPS) {
 767   Symbol* message = NULL;
 768   Symbol* error = SystemDictionary::find_resolution_error(this_cp, which, &message);
 769   assert(error != NULL && message != NULL, "checking");
 770   CLEAR_PENDING_EXCEPTION;
 771   ResourceMark rm;
 772   THROW_MSG(error, message->as_C_string());
 773 }
 774 
 775 // If resolution for Class, MethodHandle or MethodType fails, save the exception
 776 // in the resolution error table, so that the same exception is thrown again.
 777 void ConstantPool::save_and_throw_exception(const constantPoolHandle& this_cp, int which,
 778                                             constantTag tag, TRAPS) {
 779   Symbol* error = PENDING_EXCEPTION->klass()->name();
 780 
 781   int error_tag = tag.error_value();
 782 
 783   if (!PENDING_EXCEPTION->
 784     is_a(SystemDictionary::LinkageError_klass())) {
 785     // Just throw the exception and don't prevent these classes from
 786     // being loaded due to virtual machine errors like StackOverflow
 787     // and OutOfMemoryError, etc, or if the thread was hit by stop()
 788     // Needs clarification to section 5.4.3 of the VM spec (see 6308271)
 789   } else if (this_cp->tag_at(which).value() != error_tag) {
 790     Symbol* message = exception_message(this_cp, which, tag, PENDING_EXCEPTION);
 791     SystemDictionary::add_resolution_error(this_cp, which, error, message);
 792     // CAS in the tag.  If a thread beat us to registering this error that's fine.
 793     // If another thread resolved the reference, this is a race condition. This
 794     // thread may have had a security manager or something temporary.
 795     // This doesn't deterministically get an error.   So why do we save this?
 796     // We save this because jvmti can add classes to the bootclass path after
 797     // this error, so it needs to get the same error if the error is first.
 798     jbyte old_tag = Atomic::cmpxchg((jbyte)error_tag,
 799                             (jbyte*)this_cp->tag_addr_at(which), (jbyte)tag.value());
 800     if (old_tag != error_tag && old_tag != tag.value()) {
 801       // MethodHandles and MethodType doesn't change to resolved version.
 802       assert(this_cp->tag_at(which).is_klass(), "Wrong tag value");
 803       // Forget the exception and use the resolved class.
 804       CLEAR_PENDING_EXCEPTION;
 805     }
 806   } else {
 807     // some other thread put this in error state
 808     throw_resolution_error(this_cp, which, CHECK);
 809   }
 810 }
 811 
 812 // Called to resolve constants in the constant pool and return an oop.
 813 // Some constant pool entries cache their resolved oop. This is also
 814 // called to create oops from constants to use in arguments for invokedynamic
 815 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp, int index, int cache_index, TRAPS) {
 816   oop result_oop = NULL;
 817   Handle throw_exception;
 818 
 819   if (cache_index == _possible_index_sentinel) {
 820     // It is possible that this constant is one which is cached in the objects.
 821     // We'll do a linear search.  This should be OK because this usage is rare.
 822     assert(index > 0, "valid index");
 823     cache_index = this_cp->cp_to_object_index(index);
 824   }
 825   assert(cache_index == _no_index_sentinel || cache_index >= 0, "");
 826   assert(index == _no_index_sentinel || index >= 0, "");
 827 
 828   if (cache_index >= 0) {
 829     result_oop = this_cp->resolved_references()->obj_at(cache_index);
 830     if (result_oop != NULL) {
 831       return result_oop;
 832       // That was easy...
 833     }
 834     index = this_cp->object_to_cp_index(cache_index);
 835   }
 836 
 837   jvalue prim_value;  // temp used only in a few cases below
 838 
 839   constantTag tag = this_cp->tag_at(index);
 840 
 841   switch (tag.value()) {
 842 
 843   case JVM_CONSTANT_UnresolvedClass:
 844   case JVM_CONSTANT_UnresolvedClassInError:
 845   case JVM_CONSTANT_Class:
 846     {
 847       assert(cache_index == _no_index_sentinel, "should not have been set");
 848       Klass* resolved = klass_at_impl(this_cp, index, true, CHECK_NULL);
 849       // ldc wants the java mirror.
 850       result_oop = resolved->java_mirror();
 851       break;
 852     }
 853 
 854   case JVM_CONSTANT_String:
 855     assert(cache_index != _no_index_sentinel, "should have been set");
 856     if (this_cp->is_pseudo_string_at(index)) {
 857       result_oop = this_cp->pseudo_string_at(index, cache_index);
 858       break;
 859     }
 860     result_oop = string_at_impl(this_cp, index, cache_index, CHECK_NULL);
 861     break;
 862 
 863   case JVM_CONSTANT_MethodHandleInError:
 864   case JVM_CONSTANT_MethodTypeInError:
 865     {
 866       throw_resolution_error(this_cp, index, CHECK_NULL);
 867       break;
 868     }
 869 
 870   case JVM_CONSTANT_MethodHandle:
 871     {
 872       int ref_kind                 = this_cp->method_handle_ref_kind_at(index);
 873       int callee_index             = this_cp->method_handle_klass_index_at(index);
 874       Symbol*  name =      this_cp->method_handle_name_ref_at(index);
 875       Symbol*  signature = this_cp->method_handle_signature_ref_at(index);
 876       constantTag m_tag  = this_cp->tag_at(this_cp->method_handle_index_at(index));
 877       { ResourceMark rm(THREAD);
 878         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodHandle:%d [%d/%d/%d] %s.%s",
 879                               ref_kind, index, this_cp->method_handle_index_at(index),
 880                               callee_index, name->as_C_string(), signature->as_C_string());
 881       }
 882 
 883       Klass* callee = klass_at_impl(this_cp, callee_index, true, CHECK_NULL);
 884 
 885       // Check constant pool method consistency
 886       if ((callee->is_interface() && m_tag.is_method()) ||
 887           ((!callee->is_interface() && m_tag.is_interface_method()))) {
 888         ResourceMark rm(THREAD);
 889         char buf[400];
 890         jio_snprintf(buf, sizeof(buf),
 891           "Inconsistent constant pool data in classfile for class %s. "
 892           "Method %s%s at index %d is %s and should be %s",
 893           callee->name()->as_C_string(), name->as_C_string(), signature->as_C_string(), index,
 894           callee->is_interface() ? "CONSTANT_MethodRef" : "CONSTANT_InterfaceMethodRef",
 895           callee->is_interface() ? "CONSTANT_InterfaceMethodRef" : "CONSTANT_MethodRef");
 896         THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 897       }
 898 
 899       Klass* klass = this_cp->pool_holder();
 900       Handle value = SystemDictionary::link_method_handle_constant(klass, ref_kind,
 901                                                                    callee, name, signature,
 902                                                                    THREAD);
 903       result_oop = value();
 904       if (HAS_PENDING_EXCEPTION) {
 905         save_and_throw_exception(this_cp, index, tag, CHECK_NULL);
 906       }
 907       break;
 908     }
 909 
 910   case JVM_CONSTANT_MethodType:
 911     {
 912       Symbol*  signature = this_cp->method_type_signature_at(index);
 913       { ResourceMark rm(THREAD);
 914         log_debug(class, resolve)("resolve JVM_CONSTANT_MethodType [%d/%d] %s",
 915                               index, this_cp->method_type_index_at(index),
 916                               signature->as_C_string());
 917       }
 918       Klass* klass = this_cp->pool_holder();
 919       Handle value = SystemDictionary::find_method_handle_type(signature, klass, THREAD);
 920       result_oop = value();
 921       if (HAS_PENDING_EXCEPTION) {
 922         save_and_throw_exception(this_cp, index, tag, CHECK_NULL);
 923       }
 924       break;
 925     }
 926 
 927   case JVM_CONSTANT_Integer:
 928     assert(cache_index == _no_index_sentinel, "should not have been set");
 929     prim_value.i = this_cp->int_at(index);
 930     result_oop = java_lang_boxing_object::create(T_INT, &prim_value, CHECK_NULL);
 931     break;
 932 
 933   case JVM_CONSTANT_Float:
 934     assert(cache_index == _no_index_sentinel, "should not have been set");
 935     prim_value.f = this_cp->float_at(index);
 936     result_oop = java_lang_boxing_object::create(T_FLOAT, &prim_value, CHECK_NULL);
 937     break;
 938 
 939   case JVM_CONSTANT_Long:
 940     assert(cache_index == _no_index_sentinel, "should not have been set");
 941     prim_value.j = this_cp->long_at(index);
 942     result_oop = java_lang_boxing_object::create(T_LONG, &prim_value, CHECK_NULL);
 943     break;
 944 
 945   case JVM_CONSTANT_Double:
 946     assert(cache_index == _no_index_sentinel, "should not have been set");
 947     prim_value.d = this_cp->double_at(index);
 948     result_oop = java_lang_boxing_object::create(T_DOUBLE, &prim_value, CHECK_NULL);
 949     break;
 950 
 951   default:
 952     DEBUG_ONLY( tty->print_cr("*** %p: tag at CP[%d/%d] = %d",
 953                               this_cp(), index, cache_index, tag.value()));
 954     assert(false, "unexpected constant tag");
 955     break;
 956   }
 957 
 958   if (cache_index >= 0) {
 959     // Benign race condition:  resolved_references may already be filled in.
 960     // The important thing here is that all threads pick up the same result.
 961     // It doesn't matter which racing thread wins, as long as only one
 962     // result is used by all threads, and all future queries.
 963     oop old_result = this_cp->resolved_references()->atomic_compare_exchange_oop(cache_index, result_oop, NULL);
 964     if (old_result == NULL) {
 965       return result_oop;  // was installed
 966     } else {
 967       // Return the winning thread's result.  This can be different than
 968       // the result here for MethodHandles.
 969       return old_result;
 970     }
 971   } else {
 972     return result_oop;
 973   }
 974 }
 975 
 976 oop ConstantPool::uncached_string_at(int which, TRAPS) {
 977   Symbol* sym = unresolved_string_at(which);
 978   oop str = StringTable::intern(sym, CHECK_(NULL));
 979   assert(java_lang_String::is_instance(str), "must be string");
 980   return str;
 981 }
 982 
 983 
 984 oop ConstantPool::resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS) {
 985   assert(this_cp->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool");
 986 
 987   Handle bsm;
 988   int argc;
 989   {
 990     // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments
 991     // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry.
 992     // It is accompanied by the optional arguments.
 993     int bsm_index = this_cp->invoke_dynamic_bootstrap_method_ref_index_at(index);
 994     oop bsm_oop = this_cp->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL);
 995     if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) {
 996       THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle");
 997     }
 998 
 999     // Extract the optional static arguments.
1000     argc = this_cp->invoke_dynamic_argument_count_at(index);
1001     if (argc == 0)  return bsm_oop;
1002 
1003     bsm = Handle(THREAD, bsm_oop);
1004   }
1005 
1006   objArrayHandle info;
1007   {
1008     objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL);
1009     info = objArrayHandle(THREAD, info_oop);
1010   }
1011 
1012   info->obj_at_put(0, bsm());
1013   for (int i = 0; i < argc; i++) {
1014     int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i);
1015     oop arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL);
1016     info->obj_at_put(1+i, arg_oop);
1017   }
1018 
1019   return info();
1020 }
1021 
1022 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS) {
1023   // If the string has already been interned, this entry will be non-null
1024   oop str = this_cp->resolved_references()->obj_at(obj_index);
1025   if (str != NULL) return str;
1026   Symbol* sym = this_cp->unresolved_string_at(which);
1027   str = StringTable::intern(sym, CHECK_(NULL));
1028   this_cp->string_at_put(which, obj_index, str);
1029   assert(java_lang_String::is_instance(str), "must be string");
1030   return str;
1031 }
1032 
1033 
1034 bool ConstantPool::klass_name_at_matches(const InstanceKlass* k, int which) {
1035   // Names are interned, so we can compare Symbol*s directly
1036   Symbol* cp_name = klass_name_at(which);
1037   return (cp_name == k->name());
1038 }
1039 
1040 
1041 // Iterate over symbols and decrement ones which are Symbol*s
1042 // This is done during GC.
1043 // Only decrement the UTF8 symbols. Strings point to
1044 // these symbols but didn't increment the reference count.
1045 void ConstantPool::unreference_symbols() {
1046   for (int index = 1; index < length(); index++) { // Index 0 is unused
1047     constantTag tag = tag_at(index);
1048     if (tag.is_symbol()) {
1049       symbol_at(index)->decrement_refcount();
1050     }
1051   }
1052 }
1053 
1054 
1055 // Compare this constant pool's entry at index1 to the constant pool
1056 // cp2's entry at index2.
1057 bool ConstantPool::compare_entry_to(int index1, const constantPoolHandle& cp2,
1058        int index2, TRAPS) {
1059 
1060   // The error tags are equivalent to non-error tags when comparing
1061   jbyte t1 = tag_at(index1).non_error_value();
1062   jbyte t2 = cp2->tag_at(index2).non_error_value();
1063 
1064   if (t1 != t2) {
1065     // Not the same entry type so there is nothing else to check. Note
1066     // that this style of checking will consider resolved/unresolved
1067     // class pairs as different.
1068     // From the ConstantPool* API point of view, this is correct
1069     // behavior. See VM_RedefineClasses::merge_constant_pools() to see how this
1070     // plays out in the context of ConstantPool* merging.
1071     return false;
1072   }
1073 
1074   switch (t1) {
1075   case JVM_CONSTANT_Class:
1076   {
1077     Klass* k1 = klass_at(index1, CHECK_false);
1078     Klass* k2 = cp2->klass_at(index2, CHECK_false);
1079     if (k1 == k2) {
1080       return true;
1081     }
1082   } break;
1083 
1084   case JVM_CONSTANT_ClassIndex:
1085   {
1086     int recur1 = klass_index_at(index1);
1087     int recur2 = cp2->klass_index_at(index2);
1088     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1089     if (match) {
1090       return true;
1091     }
1092   } break;
1093 
1094   case JVM_CONSTANT_Double:
1095   {
1096     jdouble d1 = double_at(index1);
1097     jdouble d2 = cp2->double_at(index2);
1098     if (d1 == d2) {
1099       return true;
1100     }
1101   } break;
1102 
1103   case JVM_CONSTANT_Fieldref:
1104   case JVM_CONSTANT_InterfaceMethodref:
1105   case JVM_CONSTANT_Methodref:
1106   {
1107     int recur1 = uncached_klass_ref_index_at(index1);
1108     int recur2 = cp2->uncached_klass_ref_index_at(index2);
1109     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1110     if (match) {
1111       recur1 = uncached_name_and_type_ref_index_at(index1);
1112       recur2 = cp2->uncached_name_and_type_ref_index_at(index2);
1113       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1114       if (match) {
1115         return true;
1116       }
1117     }
1118   } break;
1119 
1120   case JVM_CONSTANT_Float:
1121   {
1122     jfloat f1 = float_at(index1);
1123     jfloat f2 = cp2->float_at(index2);
1124     if (f1 == f2) {
1125       return true;
1126     }
1127   } break;
1128 
1129   case JVM_CONSTANT_Integer:
1130   {
1131     jint i1 = int_at(index1);
1132     jint i2 = cp2->int_at(index2);
1133     if (i1 == i2) {
1134       return true;
1135     }
1136   } break;
1137 
1138   case JVM_CONSTANT_Long:
1139   {
1140     jlong l1 = long_at(index1);
1141     jlong l2 = cp2->long_at(index2);
1142     if (l1 == l2) {
1143       return true;
1144     }
1145   } break;
1146 
1147   case JVM_CONSTANT_NameAndType:
1148   {
1149     int recur1 = name_ref_index_at(index1);
1150     int recur2 = cp2->name_ref_index_at(index2);
1151     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1152     if (match) {
1153       recur1 = signature_ref_index_at(index1);
1154       recur2 = cp2->signature_ref_index_at(index2);
1155       match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1156       if (match) {
1157         return true;
1158       }
1159     }
1160   } break;
1161 
1162   case JVM_CONSTANT_StringIndex:
1163   {
1164     int recur1 = string_index_at(index1);
1165     int recur2 = cp2->string_index_at(index2);
1166     bool match = compare_entry_to(recur1, cp2, recur2, CHECK_false);
1167     if (match) {
1168       return true;
1169     }
1170   } break;
1171 
1172   case JVM_CONSTANT_UnresolvedClass:
1173   {
1174     Symbol* k1 = klass_name_at(index1);
1175     Symbol* k2 = cp2->klass_name_at(index2);
1176     if (k1 == k2) {
1177       return true;
1178     }
1179   } break;
1180 
1181   case JVM_CONSTANT_MethodType:
1182   {
1183     int k1 = method_type_index_at(index1);
1184     int k2 = cp2->method_type_index_at(index2);
1185     bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1186     if (match) {
1187       return true;
1188     }
1189   } break;
1190 
1191   case JVM_CONSTANT_MethodHandle:
1192   {
1193     int k1 = method_handle_ref_kind_at(index1);
1194     int k2 = cp2->method_handle_ref_kind_at(index2);
1195     if (k1 == k2) {
1196       int i1 = method_handle_index_at(index1);
1197       int i2 = cp2->method_handle_index_at(index2);
1198       bool match = compare_entry_to(i1, cp2, i2, CHECK_false);
1199       if (match) {
1200         return true;
1201       }
1202     }
1203   } break;
1204 
1205   case JVM_CONSTANT_InvokeDynamic:
1206   {
1207     int k1 = invoke_dynamic_name_and_type_ref_index_at(index1);
1208     int k2 = cp2->invoke_dynamic_name_and_type_ref_index_at(index2);
1209     int i1 = invoke_dynamic_bootstrap_specifier_index(index1);
1210     int i2 = cp2->invoke_dynamic_bootstrap_specifier_index(index2);
1211     // separate statements and variables because CHECK_false is used
1212     bool match_entry = compare_entry_to(k1, cp2, k2, CHECK_false);
1213     bool match_operand = compare_operand_to(i1, cp2, i2, CHECK_false);
1214     return (match_entry && match_operand);
1215   } break;
1216 
1217   case JVM_CONSTANT_String:
1218   {
1219     Symbol* s1 = unresolved_string_at(index1);
1220     Symbol* s2 = cp2->unresolved_string_at(index2);
1221     if (s1 == s2) {
1222       return true;
1223     }
1224   } break;
1225 
1226   case JVM_CONSTANT_Utf8:
1227   {
1228     Symbol* s1 = symbol_at(index1);
1229     Symbol* s2 = cp2->symbol_at(index2);
1230     if (s1 == s2) {
1231       return true;
1232     }
1233   } break;
1234 
1235   // Invalid is used as the tag for the second constant pool entry
1236   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1237   // not be seen by itself.
1238   case JVM_CONSTANT_Invalid: // fall through
1239 
1240   default:
1241     ShouldNotReachHere();
1242     break;
1243   }
1244 
1245   return false;
1246 } // end compare_entry_to()
1247 
1248 
1249 // Resize the operands array with delta_len and delta_size.
1250 // Used in RedefineClasses for CP merge.
1251 void ConstantPool::resize_operands(int delta_len, int delta_size, TRAPS) {
1252   int old_len  = operand_array_length(operands());
1253   int new_len  = old_len + delta_len;
1254   int min_len  = (delta_len > 0) ? old_len : new_len;
1255 
1256   int old_size = operands()->length();
1257   int new_size = old_size + delta_size;
1258   int min_size = (delta_size > 0) ? old_size : new_size;
1259 
1260   ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1261   Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, new_size, CHECK);
1262 
1263   // Set index in the resized array for existing elements only
1264   for (int idx = 0; idx < min_len; idx++) {
1265     int offset = operand_offset_at(idx);                       // offset in original array
1266     operand_offset_at_put(new_ops, idx, offset + 2*delta_len); // offset in resized array
1267   }
1268   // Copy the bootstrap specifiers only
1269   Copy::conjoint_memory_atomic(operands()->adr_at(2*old_len),
1270                                new_ops->adr_at(2*new_len),
1271                                (min_size - 2*min_len) * sizeof(u2));
1272   // Explicitly deallocate old operands array.
1273   // Note, it is not needed for 7u backport.
1274   if ( operands() != NULL) { // the safety check
1275     MetadataFactory::free_array<u2>(loader_data, operands());
1276   }
1277   set_operands(new_ops);
1278 } // end resize_operands()
1279 
1280 
1281 // Extend the operands array with the length and size of the ext_cp operands.
1282 // Used in RedefineClasses for CP merge.
1283 void ConstantPool::extend_operands(const constantPoolHandle& ext_cp, TRAPS) {
1284   int delta_len = operand_array_length(ext_cp->operands());
1285   if (delta_len == 0) {
1286     return; // nothing to do
1287   }
1288   int delta_size = ext_cp->operands()->length();
1289 
1290   assert(delta_len  > 0 && delta_size > 0, "extended operands array must be bigger");
1291 
1292   if (operand_array_length(operands()) == 0) {
1293     ClassLoaderData* loader_data = pool_holder()->class_loader_data();
1294     Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, delta_size, CHECK);
1295     // The first element index defines the offset of second part
1296     operand_offset_at_put(new_ops, 0, 2*delta_len); // offset in new array
1297     set_operands(new_ops);
1298   } else {
1299     resize_operands(delta_len, delta_size, CHECK);
1300   }
1301 
1302 } // end extend_operands()
1303 
1304 
1305 // Shrink the operands array to a smaller array with new_len length.
1306 // Used in RedefineClasses for CP merge.
1307 void ConstantPool::shrink_operands(int new_len, TRAPS) {
1308   int old_len = operand_array_length(operands());
1309   if (new_len == old_len) {
1310     return; // nothing to do
1311   }
1312   assert(new_len < old_len, "shrunken operands array must be smaller");
1313 
1314   int free_base  = operand_next_offset_at(new_len - 1);
1315   int delta_len  = new_len - old_len;
1316   int delta_size = 2*delta_len + free_base - operands()->length();
1317 
1318   resize_operands(delta_len, delta_size, CHECK);
1319 
1320 } // end shrink_operands()
1321 
1322 
1323 void ConstantPool::copy_operands(const constantPoolHandle& from_cp,
1324                                  const constantPoolHandle& to_cp,
1325                                  TRAPS) {
1326 
1327   int from_oplen = operand_array_length(from_cp->operands());
1328   int old_oplen  = operand_array_length(to_cp->operands());
1329   if (from_oplen != 0) {
1330     ClassLoaderData* loader_data = to_cp->pool_holder()->class_loader_data();
1331     // append my operands to the target's operands array
1332     if (old_oplen == 0) {
1333       // Can't just reuse from_cp's operand list because of deallocation issues
1334       int len = from_cp->operands()->length();
1335       Array<u2>* new_ops = MetadataFactory::new_array<u2>(loader_data, len, CHECK);
1336       Copy::conjoint_memory_atomic(
1337           from_cp->operands()->adr_at(0), new_ops->adr_at(0), len * sizeof(u2));
1338       to_cp->set_operands(new_ops);
1339     } else {
1340       int old_len  = to_cp->operands()->length();
1341       int from_len = from_cp->operands()->length();
1342       int old_off  = old_oplen * sizeof(u2);
1343       int from_off = from_oplen * sizeof(u2);
1344       // Use the metaspace for the destination constant pool
1345       Array<u2>* new_operands = MetadataFactory::new_array<u2>(loader_data, old_len + from_len, CHECK);
1346       int fillp = 0, len = 0;
1347       // first part of dest
1348       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(0),
1349                                    new_operands->adr_at(fillp),
1350                                    (len = old_off) * sizeof(u2));
1351       fillp += len;
1352       // first part of src
1353       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(0),
1354                                    new_operands->adr_at(fillp),
1355                                    (len = from_off) * sizeof(u2));
1356       fillp += len;
1357       // second part of dest
1358       Copy::conjoint_memory_atomic(to_cp->operands()->adr_at(old_off),
1359                                    new_operands->adr_at(fillp),
1360                                    (len = old_len - old_off) * sizeof(u2));
1361       fillp += len;
1362       // second part of src
1363       Copy::conjoint_memory_atomic(from_cp->operands()->adr_at(from_off),
1364                                    new_operands->adr_at(fillp),
1365                                    (len = from_len - from_off) * sizeof(u2));
1366       fillp += len;
1367       assert(fillp == new_operands->length(), "");
1368 
1369       // Adjust indexes in the first part of the copied operands array.
1370       for (int j = 0; j < from_oplen; j++) {
1371         int offset = operand_offset_at(new_operands, old_oplen + j);
1372         assert(offset == operand_offset_at(from_cp->operands(), j), "correct copy");
1373         offset += old_len;  // every new tuple is preceded by old_len extra u2's
1374         operand_offset_at_put(new_operands, old_oplen + j, offset);
1375       }
1376 
1377       // replace target operands array with combined array
1378       to_cp->set_operands(new_operands);
1379     }
1380   }
1381 } // end copy_operands()
1382 
1383 
1384 // Copy this constant pool's entries at start_i to end_i (inclusive)
1385 // to the constant pool to_cp's entries starting at to_i. A total of
1386 // (end_i - start_i) + 1 entries are copied.
1387 void ConstantPool::copy_cp_to_impl(const constantPoolHandle& from_cp, int start_i, int end_i,
1388        const constantPoolHandle& to_cp, int to_i, TRAPS) {
1389 
1390 
1391   int dest_i = to_i;  // leave original alone for debug purposes
1392 
1393   for (int src_i = start_i; src_i <= end_i; /* see loop bottom */ ) {
1394     copy_entry_to(from_cp, src_i, to_cp, dest_i, CHECK);
1395 
1396     switch (from_cp->tag_at(src_i).value()) {
1397     case JVM_CONSTANT_Double:
1398     case JVM_CONSTANT_Long:
1399       // double and long take two constant pool entries
1400       src_i += 2;
1401       dest_i += 2;
1402       break;
1403 
1404     default:
1405       // all others take one constant pool entry
1406       src_i++;
1407       dest_i++;
1408       break;
1409     }
1410   }
1411   copy_operands(from_cp, to_cp, CHECK);
1412 
1413 } // end copy_cp_to_impl()
1414 
1415 
1416 // Copy this constant pool's entry at from_i to the constant pool
1417 // to_cp's entry at to_i.
1418 void ConstantPool::copy_entry_to(const constantPoolHandle& from_cp, int from_i,
1419                                         const constantPoolHandle& to_cp, int to_i,
1420                                         TRAPS) {
1421 
1422   int tag = from_cp->tag_at(from_i).value();
1423   switch (tag) {
1424   case JVM_CONSTANT_ClassIndex:
1425   {
1426     jint ki = from_cp->klass_index_at(from_i);
1427     to_cp->klass_index_at_put(to_i, ki);
1428   } break;
1429 
1430   case JVM_CONSTANT_Double:
1431   {
1432     jdouble d = from_cp->double_at(from_i);
1433     to_cp->double_at_put(to_i, d);
1434     // double takes two constant pool entries so init second entry's tag
1435     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1436   } break;
1437 
1438   case JVM_CONSTANT_Fieldref:
1439   {
1440     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1441     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1442     to_cp->field_at_put(to_i, class_index, name_and_type_index);
1443   } break;
1444 
1445   case JVM_CONSTANT_Float:
1446   {
1447     jfloat f = from_cp->float_at(from_i);
1448     to_cp->float_at_put(to_i, f);
1449   } break;
1450 
1451   case JVM_CONSTANT_Integer:
1452   {
1453     jint i = from_cp->int_at(from_i);
1454     to_cp->int_at_put(to_i, i);
1455   } break;
1456 
1457   case JVM_CONSTANT_InterfaceMethodref:
1458   {
1459     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1460     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1461     to_cp->interface_method_at_put(to_i, class_index, name_and_type_index);
1462   } break;
1463 
1464   case JVM_CONSTANT_Long:
1465   {
1466     jlong l = from_cp->long_at(from_i);
1467     to_cp->long_at_put(to_i, l);
1468     // long takes two constant pool entries so init second entry's tag
1469     to_cp->tag_at_put(to_i + 1, JVM_CONSTANT_Invalid);
1470   } break;
1471 
1472   case JVM_CONSTANT_Methodref:
1473   {
1474     int class_index = from_cp->uncached_klass_ref_index_at(from_i);
1475     int name_and_type_index = from_cp->uncached_name_and_type_ref_index_at(from_i);
1476     to_cp->method_at_put(to_i, class_index, name_and_type_index);
1477   } break;
1478 
1479   case JVM_CONSTANT_NameAndType:
1480   {
1481     int name_ref_index = from_cp->name_ref_index_at(from_i);
1482     int signature_ref_index = from_cp->signature_ref_index_at(from_i);
1483     to_cp->name_and_type_at_put(to_i, name_ref_index, signature_ref_index);
1484   } break;
1485 
1486   case JVM_CONSTANT_StringIndex:
1487   {
1488     jint si = from_cp->string_index_at(from_i);
1489     to_cp->string_index_at_put(to_i, si);
1490   } break;
1491 
1492   case JVM_CONSTANT_Class:
1493   case JVM_CONSTANT_UnresolvedClass:
1494   case JVM_CONSTANT_UnresolvedClassInError:
1495   {
1496     // Revert to JVM_CONSTANT_ClassIndex
1497     int name_index = from_cp->klass_slot_at(from_i).name_index();
1498     assert(from_cp->tag_at(name_index).is_symbol(), "sanity");
1499     to_cp->klass_index_at_put(to_i, name_index);
1500   } break;
1501 
1502   case JVM_CONSTANT_String:
1503   {
1504     Symbol* s = from_cp->unresolved_string_at(from_i);
1505     to_cp->unresolved_string_at_put(to_i, s);
1506   } break;
1507 
1508   case JVM_CONSTANT_Utf8:
1509   {
1510     Symbol* s = from_cp->symbol_at(from_i);
1511     // Need to increase refcount, the old one will be thrown away and deferenced
1512     s->increment_refcount();
1513     to_cp->symbol_at_put(to_i, s);
1514   } break;
1515 
1516   case JVM_CONSTANT_MethodType:
1517   case JVM_CONSTANT_MethodTypeInError:
1518   {
1519     jint k = from_cp->method_type_index_at(from_i);
1520     to_cp->method_type_index_at_put(to_i, k);
1521   } break;
1522 
1523   case JVM_CONSTANT_MethodHandle:
1524   case JVM_CONSTANT_MethodHandleInError:
1525   {
1526     int k1 = from_cp->method_handle_ref_kind_at(from_i);
1527     int k2 = from_cp->method_handle_index_at(from_i);
1528     to_cp->method_handle_index_at_put(to_i, k1, k2);
1529   } break;
1530 
1531   case JVM_CONSTANT_InvokeDynamic:
1532   {
1533     int k1 = from_cp->invoke_dynamic_bootstrap_specifier_index(from_i);
1534     int k2 = from_cp->invoke_dynamic_name_and_type_ref_index_at(from_i);
1535     k1 += operand_array_length(to_cp->operands());  // to_cp might already have operands
1536     to_cp->invoke_dynamic_at_put(to_i, k1, k2);
1537   } break;
1538 
1539   // Invalid is used as the tag for the second constant pool entry
1540   // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
1541   // not be seen by itself.
1542   case JVM_CONSTANT_Invalid: // fall through
1543 
1544   default:
1545   {
1546     ShouldNotReachHere();
1547   } break;
1548   }
1549 } // end copy_entry_to()
1550 
1551 // Search constant pool search_cp for an entry that matches this
1552 // constant pool's entry at pattern_i. Returns the index of a
1553 // matching entry or zero (0) if there is no matching entry.
1554 int ConstantPool::find_matching_entry(int pattern_i,
1555       const constantPoolHandle& search_cp, TRAPS) {
1556 
1557   // index zero (0) is not used
1558   for (int i = 1; i < search_cp->length(); i++) {
1559     bool found = compare_entry_to(pattern_i, search_cp, i, CHECK_0);
1560     if (found) {
1561       return i;
1562     }
1563   }
1564 
1565   return 0;  // entry not found; return unused index zero (0)
1566 } // end find_matching_entry()
1567 
1568 
1569 // Compare this constant pool's bootstrap specifier at idx1 to the constant pool
1570 // cp2's bootstrap specifier at idx2.
1571 bool ConstantPool::compare_operand_to(int idx1, const constantPoolHandle& cp2, int idx2, TRAPS) {
1572   int k1 = operand_bootstrap_method_ref_index_at(idx1);
1573   int k2 = cp2->operand_bootstrap_method_ref_index_at(idx2);
1574   bool match = compare_entry_to(k1, cp2, k2, CHECK_false);
1575 
1576   if (!match) {
1577     return false;
1578   }
1579   int argc = operand_argument_count_at(idx1);
1580   if (argc == cp2->operand_argument_count_at(idx2)) {
1581     for (int j = 0; j < argc; j++) {
1582       k1 = operand_argument_index_at(idx1, j);
1583       k2 = cp2->operand_argument_index_at(idx2, j);
1584       match = compare_entry_to(k1, cp2, k2, CHECK_false);
1585       if (!match) {
1586         return false;
1587       }
1588     }
1589     return true;           // got through loop; all elements equal
1590   }
1591   return false;
1592 } // end compare_operand_to()
1593 
1594 // Search constant pool search_cp for a bootstrap specifier that matches
1595 // this constant pool's bootstrap specifier at pattern_i index.
1596 // Return the index of a matching bootstrap specifier or (-1) if there is no match.
1597 int ConstantPool::find_matching_operand(int pattern_i,
1598                     const constantPoolHandle& search_cp, int search_len, TRAPS) {
1599   for (int i = 0; i < search_len; i++) {
1600     bool found = compare_operand_to(pattern_i, search_cp, i, CHECK_(-1));
1601     if (found) {
1602       return i;
1603     }
1604   }
1605   return -1;  // bootstrap specifier not found; return unused index (-1)
1606 } // end find_matching_operand()
1607 
1608 
1609 #ifndef PRODUCT
1610 
1611 const char* ConstantPool::printable_name_at(int which) {
1612 
1613   constantTag tag = tag_at(which);
1614 
1615   if (tag.is_string()) {
1616     return string_at_noresolve(which);
1617   } else if (tag.is_klass() || tag.is_unresolved_klass()) {
1618     return klass_name_at(which)->as_C_string();
1619   } else if (tag.is_symbol()) {
1620     return symbol_at(which)->as_C_string();
1621   }
1622   return "";
1623 }
1624 
1625 #endif // PRODUCT
1626 
1627 
1628 // JVMTI GetConstantPool support
1629 
1630 // For debugging of constant pool
1631 const bool debug_cpool = false;
1632 
1633 #define DBG(code) do { if (debug_cpool) { (code); } } while(0)
1634 
1635 static void print_cpool_bytes(jint cnt, u1 *bytes) {
1636   const char* WARN_MSG = "Must not be such entry!";
1637   jint size = 0;
1638   u2   idx1, idx2;
1639 
1640   for (jint idx = 1; idx < cnt; idx++) {
1641     jint ent_size = 0;
1642     u1   tag  = *bytes++;
1643     size++;                       // count tag
1644 
1645     printf("const #%03d, tag: %02d ", idx, tag);
1646     switch(tag) {
1647       case JVM_CONSTANT_Invalid: {
1648         printf("Invalid");
1649         break;
1650       }
1651       case JVM_CONSTANT_Unicode: {
1652         printf("Unicode      %s", WARN_MSG);
1653         break;
1654       }
1655       case JVM_CONSTANT_Utf8: {
1656         u2 len = Bytes::get_Java_u2(bytes);
1657         char str[128];
1658         if (len > 127) {
1659            len = 127;
1660         }
1661         strncpy(str, (char *) (bytes+2), len);
1662         str[len] = '\0';
1663         printf("Utf8          \"%s\"", str);
1664         ent_size = 2 + len;
1665         break;
1666       }
1667       case JVM_CONSTANT_Integer: {
1668         u4 val = Bytes::get_Java_u4(bytes);
1669         printf("int          %d", *(int *) &val);
1670         ent_size = 4;
1671         break;
1672       }
1673       case JVM_CONSTANT_Float: {
1674         u4 val = Bytes::get_Java_u4(bytes);
1675         printf("float        %5.3ff", *(float *) &val);
1676         ent_size = 4;
1677         break;
1678       }
1679       case JVM_CONSTANT_Long: {
1680         u8 val = Bytes::get_Java_u8(bytes);
1681         printf("long         " INT64_FORMAT, (int64_t) *(jlong *) &val);
1682         ent_size = 8;
1683         idx++; // Long takes two cpool slots
1684         break;
1685       }
1686       case JVM_CONSTANT_Double: {
1687         u8 val = Bytes::get_Java_u8(bytes);
1688         printf("double       %5.3fd", *(jdouble *)&val);
1689         ent_size = 8;
1690         idx++; // Double takes two cpool slots
1691         break;
1692       }
1693       case JVM_CONSTANT_Class: {
1694         idx1 = Bytes::get_Java_u2(bytes);
1695         printf("class        #%03d", idx1);
1696         ent_size = 2;
1697         break;
1698       }
1699       case JVM_CONSTANT_String: {
1700         idx1 = Bytes::get_Java_u2(bytes);
1701         printf("String       #%03d", idx1);
1702         ent_size = 2;
1703         break;
1704       }
1705       case JVM_CONSTANT_Fieldref: {
1706         idx1 = Bytes::get_Java_u2(bytes);
1707         idx2 = Bytes::get_Java_u2(bytes+2);
1708         printf("Field        #%03d, #%03d", (int) idx1, (int) idx2);
1709         ent_size = 4;
1710         break;
1711       }
1712       case JVM_CONSTANT_Methodref: {
1713         idx1 = Bytes::get_Java_u2(bytes);
1714         idx2 = Bytes::get_Java_u2(bytes+2);
1715         printf("Method       #%03d, #%03d", idx1, idx2);
1716         ent_size = 4;
1717         break;
1718       }
1719       case JVM_CONSTANT_InterfaceMethodref: {
1720         idx1 = Bytes::get_Java_u2(bytes);
1721         idx2 = Bytes::get_Java_u2(bytes+2);
1722         printf("InterfMethod #%03d, #%03d", idx1, idx2);
1723         ent_size = 4;
1724         break;
1725       }
1726       case JVM_CONSTANT_NameAndType: {
1727         idx1 = Bytes::get_Java_u2(bytes);
1728         idx2 = Bytes::get_Java_u2(bytes+2);
1729         printf("NameAndType  #%03d, #%03d", idx1, idx2);
1730         ent_size = 4;
1731         break;
1732       }
1733       case JVM_CONSTANT_ClassIndex: {
1734         printf("ClassIndex  %s", WARN_MSG);
1735         break;
1736       }
1737       case JVM_CONSTANT_UnresolvedClass: {
1738         printf("UnresolvedClass: %s", WARN_MSG);
1739         break;
1740       }
1741       case JVM_CONSTANT_UnresolvedClassInError: {
1742         printf("UnresolvedClassInErr: %s", WARN_MSG);
1743         break;
1744       }
1745       case JVM_CONSTANT_StringIndex: {
1746         printf("StringIndex: %s", WARN_MSG);
1747         break;
1748       }
1749     }
1750     printf(";\n");
1751     bytes += ent_size;
1752     size  += ent_size;
1753   }
1754   printf("Cpool size: %d\n", size);
1755   fflush(0);
1756   return;
1757 } /* end print_cpool_bytes */
1758 
1759 
1760 // Returns size of constant pool entry.
1761 jint ConstantPool::cpool_entry_size(jint idx) {
1762   switch(tag_at(idx).value()) {
1763     case JVM_CONSTANT_Invalid:
1764     case JVM_CONSTANT_Unicode:
1765       return 1;
1766 
1767     case JVM_CONSTANT_Utf8:
1768       return 3 + symbol_at(idx)->utf8_length();
1769 
1770     case JVM_CONSTANT_Class:
1771     case JVM_CONSTANT_String:
1772     case JVM_CONSTANT_ClassIndex:
1773     case JVM_CONSTANT_UnresolvedClass:
1774     case JVM_CONSTANT_UnresolvedClassInError:
1775     case JVM_CONSTANT_StringIndex:
1776     case JVM_CONSTANT_MethodType:
1777     case JVM_CONSTANT_MethodTypeInError:
1778       return 3;
1779 
1780     case JVM_CONSTANT_MethodHandle:
1781     case JVM_CONSTANT_MethodHandleInError:
1782       return 4; //tag, ref_kind, ref_index
1783 
1784     case JVM_CONSTANT_Integer:
1785     case JVM_CONSTANT_Float:
1786     case JVM_CONSTANT_Fieldref:
1787     case JVM_CONSTANT_Methodref:
1788     case JVM_CONSTANT_InterfaceMethodref:
1789     case JVM_CONSTANT_NameAndType:
1790       return 5;
1791 
1792     case JVM_CONSTANT_InvokeDynamic:
1793       // u1 tag, u2 bsm, u2 nt
1794       return 5;
1795 
1796     case JVM_CONSTANT_Long:
1797     case JVM_CONSTANT_Double:
1798       return 9;
1799   }
1800   assert(false, "cpool_entry_size: Invalid constant pool entry tag");
1801   return 1;
1802 } /* end cpool_entry_size */
1803 
1804 
1805 // SymbolHashMap is used to find a constant pool index from a string.
1806 // This function fills in SymbolHashMaps, one for utf8s and one for
1807 // class names, returns size of the cpool raw bytes.
1808 jint ConstantPool::hash_entries_to(SymbolHashMap *symmap,
1809                                           SymbolHashMap *classmap) {
1810   jint size = 0;
1811 
1812   for (u2 idx = 1; idx < length(); idx++) {
1813     u2 tag = tag_at(idx).value();
1814     size += cpool_entry_size(idx);
1815 
1816     switch(tag) {
1817       case JVM_CONSTANT_Utf8: {
1818         Symbol* sym = symbol_at(idx);
1819         symmap->add_entry(sym, idx);
1820         DBG(printf("adding symbol entry %s = %d\n", sym->as_utf8(), idx));
1821         break;
1822       }
1823       case JVM_CONSTANT_Class:
1824       case JVM_CONSTANT_UnresolvedClass:
1825       case JVM_CONSTANT_UnresolvedClassInError: {
1826         Symbol* sym = klass_name_at(idx);
1827         classmap->add_entry(sym, idx);
1828         DBG(printf("adding class entry %s = %d\n", sym->as_utf8(), idx));
1829         break;
1830       }
1831       case JVM_CONSTANT_Long:
1832       case JVM_CONSTANT_Double: {
1833         idx++; // Both Long and Double take two cpool slots
1834         break;
1835       }
1836     }
1837   }
1838   return size;
1839 } /* end hash_utf8_entries_to */
1840 
1841 
1842 // Copy cpool bytes.
1843 // Returns:
1844 //    0, in case of OutOfMemoryError
1845 //   -1, in case of internal error
1846 //  > 0, count of the raw cpool bytes that have been copied
1847 int ConstantPool::copy_cpool_bytes(int cpool_size,
1848                                           SymbolHashMap* tbl,
1849                                           unsigned char *bytes) {
1850   u2   idx1, idx2;
1851   jint size  = 0;
1852   jint cnt   = length();
1853   unsigned char *start_bytes = bytes;
1854 
1855   for (jint idx = 1; idx < cnt; idx++) {
1856     u1   tag      = tag_at(idx).value();
1857     jint ent_size = cpool_entry_size(idx);
1858 
1859     assert(size + ent_size <= cpool_size, "Size mismatch");
1860 
1861     *bytes = tag;
1862     DBG(printf("#%03hd tag=%03hd, ", (short)idx, (short)tag));
1863     switch(tag) {
1864       case JVM_CONSTANT_Invalid: {
1865         DBG(printf("JVM_CONSTANT_Invalid"));
1866         break;
1867       }
1868       case JVM_CONSTANT_Unicode: {
1869         assert(false, "Wrong constant pool tag: JVM_CONSTANT_Unicode");
1870         DBG(printf("JVM_CONSTANT_Unicode"));
1871         break;
1872       }
1873       case JVM_CONSTANT_Utf8: {
1874         Symbol* sym = symbol_at(idx);
1875         char*     str = sym->as_utf8();
1876         // Warning! It's crashing on x86 with len = sym->utf8_length()
1877         int       len = (int) strlen(str);
1878         Bytes::put_Java_u2((address) (bytes+1), (u2) len);
1879         for (int i = 0; i < len; i++) {
1880             bytes[3+i] = (u1) str[i];
1881         }
1882         DBG(printf("JVM_CONSTANT_Utf8: %s ", str));
1883         break;
1884       }
1885       case JVM_CONSTANT_Integer: {
1886         jint val = int_at(idx);
1887         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1888         break;
1889       }
1890       case JVM_CONSTANT_Float: {
1891         jfloat val = float_at(idx);
1892         Bytes::put_Java_u4((address) (bytes+1), *(u4*)&val);
1893         break;
1894       }
1895       case JVM_CONSTANT_Long: {
1896         jlong val = long_at(idx);
1897         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1898         idx++;             // Long takes two cpool slots
1899         break;
1900       }
1901       case JVM_CONSTANT_Double: {
1902         jdouble val = double_at(idx);
1903         Bytes::put_Java_u8((address) (bytes+1), *(u8*)&val);
1904         idx++;             // Double takes two cpool slots
1905         break;
1906       }
1907       case JVM_CONSTANT_Class:
1908       case JVM_CONSTANT_UnresolvedClass:
1909       case JVM_CONSTANT_UnresolvedClassInError: {
1910         *bytes = JVM_CONSTANT_Class;
1911         Symbol* sym = klass_name_at(idx);
1912         idx1 = tbl->symbol_to_value(sym);
1913         assert(idx1 != 0, "Have not found a hashtable entry");
1914         Bytes::put_Java_u2((address) (bytes+1), idx1);
1915         DBG(printf("JVM_CONSTANT_Class: idx=#%03hd, %s", idx1, sym->as_utf8()));
1916         break;
1917       }
1918       case JVM_CONSTANT_String: {
1919         *bytes = JVM_CONSTANT_String;
1920         Symbol* sym = unresolved_string_at(idx);
1921         idx1 = tbl->symbol_to_value(sym);
1922         assert(idx1 != 0, "Have not found a hashtable entry");
1923         Bytes::put_Java_u2((address) (bytes+1), idx1);
1924         DBG(printf("JVM_CONSTANT_String: idx=#%03hd, %s", idx1, sym->as_utf8()));
1925         break;
1926       }
1927       case JVM_CONSTANT_Fieldref:
1928       case JVM_CONSTANT_Methodref:
1929       case JVM_CONSTANT_InterfaceMethodref: {
1930         idx1 = uncached_klass_ref_index_at(idx);
1931         idx2 = uncached_name_and_type_ref_index_at(idx);
1932         Bytes::put_Java_u2((address) (bytes+1), idx1);
1933         Bytes::put_Java_u2((address) (bytes+3), idx2);
1934         DBG(printf("JVM_CONSTANT_Methodref: %hd %hd", idx1, idx2));
1935         break;
1936       }
1937       case JVM_CONSTANT_NameAndType: {
1938         idx1 = name_ref_index_at(idx);
1939         idx2 = signature_ref_index_at(idx);
1940         Bytes::put_Java_u2((address) (bytes+1), idx1);
1941         Bytes::put_Java_u2((address) (bytes+3), idx2);
1942         DBG(printf("JVM_CONSTANT_NameAndType: %hd %hd", idx1, idx2));
1943         break;
1944       }
1945       case JVM_CONSTANT_ClassIndex: {
1946         *bytes = JVM_CONSTANT_Class;
1947         idx1 = klass_index_at(idx);
1948         Bytes::put_Java_u2((address) (bytes+1), idx1);
1949         DBG(printf("JVM_CONSTANT_ClassIndex: %hd", idx1));
1950         break;
1951       }
1952       case JVM_CONSTANT_StringIndex: {
1953         *bytes = JVM_CONSTANT_String;
1954         idx1 = string_index_at(idx);
1955         Bytes::put_Java_u2((address) (bytes+1), idx1);
1956         DBG(printf("JVM_CONSTANT_StringIndex: %hd", idx1));
1957         break;
1958       }
1959       case JVM_CONSTANT_MethodHandle:
1960       case JVM_CONSTANT_MethodHandleInError: {
1961         *bytes = JVM_CONSTANT_MethodHandle;
1962         int kind = method_handle_ref_kind_at(idx);
1963         idx1 = method_handle_index_at(idx);
1964         *(bytes+1) = (unsigned char) kind;
1965         Bytes::put_Java_u2((address) (bytes+2), idx1);
1966         DBG(printf("JVM_CONSTANT_MethodHandle: %d %hd", kind, idx1));
1967         break;
1968       }
1969       case JVM_CONSTANT_MethodType:
1970       case JVM_CONSTANT_MethodTypeInError: {
1971         *bytes = JVM_CONSTANT_MethodType;
1972         idx1 = method_type_index_at(idx);
1973         Bytes::put_Java_u2((address) (bytes+1), idx1);
1974         DBG(printf("JVM_CONSTANT_MethodType: %hd", idx1));
1975         break;
1976       }
1977       case JVM_CONSTANT_InvokeDynamic: {
1978         *bytes = tag;
1979         idx1 = extract_low_short_from_int(*int_at_addr(idx));
1980         idx2 = extract_high_short_from_int(*int_at_addr(idx));
1981         assert(idx2 == invoke_dynamic_name_and_type_ref_index_at(idx), "correct half of u4");
1982         Bytes::put_Java_u2((address) (bytes+1), idx1);
1983         Bytes::put_Java_u2((address) (bytes+3), idx2);
1984         DBG(printf("JVM_CONSTANT_InvokeDynamic: %hd %hd", idx1, idx2));
1985         break;
1986       }
1987     }
1988     DBG(printf("\n"));
1989     bytes += ent_size;
1990     size  += ent_size;
1991   }
1992   assert(size == cpool_size, "Size mismatch");
1993 
1994   // Keep temorarily for debugging until it's stable.
1995   DBG(print_cpool_bytes(cnt, start_bytes));
1996   return (int)(bytes - start_bytes);
1997 } /* end copy_cpool_bytes */
1998 
1999 #undef DBG
2000 
2001 
2002 void ConstantPool::set_on_stack(const bool value) {
2003   if (value) {
2004     // Only record if it's not already set.
2005     if (!on_stack()) {
2006       assert(!is_shared(), "should always be set for shared constant pools");
2007       _flags |= _on_stack;
2008       MetadataOnStackMark::record(this);
2009     }
2010   } else {
2011     // Clearing is done single-threadedly.
2012     if (!is_shared()) {
2013       _flags &= ~_on_stack;
2014     }
2015   }
2016 }
2017 
2018 // JSR 292 support for patching constant pool oops after the class is linked and
2019 // the oop array for resolved references are created.
2020 // We can't do this during classfile parsing, which is how the other indexes are
2021 // patched.  The other patches are applied early for some error checking
2022 // so only defer the pseudo_strings.
2023 void ConstantPool::patch_resolved_references(GrowableArray<Handle>* cp_patches) {
2024   for (int index = 1; index < cp_patches->length(); index++) { // Index 0 is unused
2025     Handle patch = cp_patches->at(index);
2026     if (patch.not_null()) {
2027       assert (tag_at(index).is_string(), "should only be string left");
2028       // Patching a string means pre-resolving it.
2029       // The spelling in the constant pool is ignored.
2030       // The constant reference may be any object whatever.
2031       // If it is not a real interned string, the constant is referred
2032       // to as a "pseudo-string", and must be presented to the CP
2033       // explicitly, because it may require scavenging.
2034       int obj_index = cp_to_object_index(index);
2035       pseudo_string_at_put(index, obj_index, patch());
2036      DEBUG_ONLY(cp_patches->at_put(index, Handle());)
2037     }
2038   }
2039 #ifdef ASSERT
2040   // Ensure that all the patches have been used.
2041   for (int index = 0; index < cp_patches->length(); index++) {
2042     assert(cp_patches->at(index).is_null(),
2043            "Unused constant pool patch at %d in class file %s",
2044            index,
2045            pool_holder()->external_name());
2046   }
2047 #endif // ASSERT
2048 }
2049 
2050 #ifndef PRODUCT
2051 
2052 // CompileTheWorld support. Preload all classes loaded references in the passed in constantpool
2053 void ConstantPool::preload_and_initialize_all_classes(ConstantPool* obj, TRAPS) {
2054   guarantee(obj->is_constantPool(), "object must be constant pool");
2055   constantPoolHandle cp(THREAD, (ConstantPool*)obj);
2056   guarantee(cp->pool_holder() != NULL, "must be fully loaded");
2057 
2058   for (int i = 0; i< cp->length();  i++) {
2059     if (cp->tag_at(i).is_unresolved_klass()) {
2060       // This will force loading of the class
2061       Klass* klass = cp->klass_at(i, CHECK);
2062       if (klass->is_instance_klass()) {
2063         // Force initialization of class
2064         InstanceKlass::cast(klass)->initialize(CHECK);
2065       }
2066     }
2067   }
2068 }
2069 
2070 #endif
2071 
2072 
2073 // Printing
2074 
2075 void ConstantPool::print_on(outputStream* st) const {
2076   assert(is_constantPool(), "must be constantPool");
2077   st->print_cr("%s", internal_name());
2078   if (flags() != 0) {
2079     st->print(" - flags: 0x%x", flags());
2080     if (has_preresolution()) st->print(" has_preresolution");
2081     if (on_stack()) st->print(" on_stack");
2082     st->cr();
2083   }
2084   if (pool_holder() != NULL) {
2085     st->print_cr(" - holder: " INTPTR_FORMAT, p2i(pool_holder()));
2086   }
2087   st->print_cr(" - cache: " INTPTR_FORMAT, p2i(cache()));
2088   st->print_cr(" - resolved_references: " INTPTR_FORMAT, p2i(resolved_references()));
2089   st->print_cr(" - reference_map: " INTPTR_FORMAT, p2i(reference_map()));
2090   st->print_cr(" - resolved_klasses: " INTPTR_FORMAT, p2i(resolved_klasses()));
2091 
2092   for (int index = 1; index < length(); index++) {      // Index 0 is unused
2093     ((ConstantPool*)this)->print_entry_on(index, st);
2094     switch (tag_at(index).value()) {
2095       case JVM_CONSTANT_Long :
2096       case JVM_CONSTANT_Double :
2097         index++;   // Skip entry following eigth-byte constant
2098     }
2099 
2100   }
2101   st->cr();
2102 }
2103 
2104 // Print one constant pool entry
2105 void ConstantPool::print_entry_on(const int index, outputStream* st) {
2106   EXCEPTION_MARK;
2107   st->print(" - %3d : ", index);
2108   tag_at(index).print_on(st);
2109   st->print(" : ");
2110   switch (tag_at(index).value()) {
2111     case JVM_CONSTANT_Class :
2112       { Klass* k = klass_at(index, CATCH);
2113         guarantee(k != NULL, "need klass");
2114         k->print_value_on(st);
2115         st->print(" {" PTR_FORMAT "}", p2i(k));
2116       }
2117       break;
2118     case JVM_CONSTANT_Fieldref :
2119     case JVM_CONSTANT_Methodref :
2120     case JVM_CONSTANT_InterfaceMethodref :
2121       st->print("klass_index=%d", uncached_klass_ref_index_at(index));
2122       st->print(" name_and_type_index=%d", uncached_name_and_type_ref_index_at(index));
2123       break;
2124     case JVM_CONSTANT_String :
2125       if (is_pseudo_string_at(index)) {
2126         oop anObj = pseudo_string_at(index);
2127         anObj->print_value_on(st);
2128         st->print(" {" PTR_FORMAT "}", p2i(anObj));
2129       } else {
2130         unresolved_string_at(index)->print_value_on(st);
2131       }
2132       break;
2133     case JVM_CONSTANT_Integer :
2134       st->print("%d", int_at(index));
2135       break;
2136     case JVM_CONSTANT_Float :
2137       st->print("%f", float_at(index));
2138       break;
2139     case JVM_CONSTANT_Long :
2140       st->print_jlong(long_at(index));
2141       break;
2142     case JVM_CONSTANT_Double :
2143       st->print("%lf", double_at(index));
2144       break;
2145     case JVM_CONSTANT_NameAndType :
2146       st->print("name_index=%d", name_ref_index_at(index));
2147       st->print(" signature_index=%d", signature_ref_index_at(index));
2148       break;
2149     case JVM_CONSTANT_Utf8 :
2150       symbol_at(index)->print_value_on(st);
2151       break;
2152     case JVM_CONSTANT_ClassIndex: {
2153         int name_index = *int_at_addr(index);
2154         st->print("klass_index=%d ", name_index);
2155         symbol_at(name_index)->print_value_on(st);
2156       }
2157       break;
2158     case JVM_CONSTANT_UnresolvedClass :               // fall-through
2159     case JVM_CONSTANT_UnresolvedClassInError: {
2160         CPKlassSlot kslot = klass_slot_at(index);
2161         int resolved_klass_index = kslot.resolved_klass_index();
2162         int name_index = kslot.name_index();
2163         assert(tag_at(name_index).is_symbol(), "sanity");
2164 
2165         Klass* klass = resolved_klasses()->at(resolved_klass_index);
2166         if (klass != NULL) {
2167           klass->print_value_on(st);
2168         } else {
2169           symbol_at(name_index)->print_value_on(st);
2170         }
2171       }
2172       break;
2173     case JVM_CONSTANT_MethodHandle :
2174     case JVM_CONSTANT_MethodHandleInError :
2175       st->print("ref_kind=%d", method_handle_ref_kind_at(index));
2176       st->print(" ref_index=%d", method_handle_index_at(index));
2177       break;
2178     case JVM_CONSTANT_MethodType :
2179     case JVM_CONSTANT_MethodTypeInError :
2180       st->print("signature_index=%d", method_type_index_at(index));
2181       break;
2182     case JVM_CONSTANT_InvokeDynamic :
2183       {
2184         st->print("bootstrap_method_index=%d", invoke_dynamic_bootstrap_method_ref_index_at(index));
2185         st->print(" name_and_type_index=%d", invoke_dynamic_name_and_type_ref_index_at(index));
2186         int argc = invoke_dynamic_argument_count_at(index);
2187         if (argc > 0) {
2188           for (int arg_i = 0; arg_i < argc; arg_i++) {
2189             int arg = invoke_dynamic_argument_index_at(index, arg_i);
2190             st->print((arg_i == 0 ? " arguments={%d" : ", %d"), arg);
2191           }
2192           st->print("}");
2193         }
2194       }
2195       break;
2196     default:
2197       ShouldNotReachHere();
2198       break;
2199   }
2200   st->cr();
2201 }
2202 
2203 void ConstantPool::print_value_on(outputStream* st) const {
2204   assert(is_constantPool(), "must be constantPool");
2205   st->print("constant pool [%d]", length());
2206   if (has_preresolution()) st->print("/preresolution");
2207   if (operands() != NULL)  st->print("/operands[%d]", operands()->length());
2208   print_address_on(st);
2209   st->print(" for ");
2210   pool_holder()->print_value_on(st);
2211   if (pool_holder() != NULL) {
2212     bool extra = (pool_holder()->constants() != this);
2213     if (extra)  st->print(" (extra)");
2214   }
2215   if (cache() != NULL) {
2216     st->print(" cache=" PTR_FORMAT, p2i(cache()));
2217   }
2218 }
2219 
2220 #if INCLUDE_SERVICES
2221 // Size Statistics
2222 void ConstantPool::collect_statistics(KlassSizeStats *sz) const {
2223   sz->_cp_all_bytes += (sz->_cp_bytes          = sz->count(this));
2224   sz->_cp_all_bytes += (sz->_cp_tags_bytes     = sz->count_array(tags()));
2225   sz->_cp_all_bytes += (sz->_cp_cache_bytes    = sz->count(cache()));
2226   sz->_cp_all_bytes += (sz->_cp_operands_bytes = sz->count_array(operands()));
2227   sz->_cp_all_bytes += (sz->_cp_refmap_bytes   = sz->count_array(reference_map()));
2228 
2229   sz->_ro_bytes += sz->_cp_operands_bytes + sz->_cp_tags_bytes +
2230                    sz->_cp_refmap_bytes;
2231   sz->_rw_bytes += sz->_cp_bytes + sz->_cp_cache_bytes;
2232 }
2233 #endif // INCLUDE_SERVICES
2234 
2235 // Verification
2236 
2237 void ConstantPool::verify_on(outputStream* st) {
2238   guarantee(is_constantPool(), "object must be constant pool");
2239   for (int i = 0; i< length();  i++) {
2240     constantTag tag = tag_at(i);
2241     if (tag.is_klass() || tag.is_unresolved_klass()) {
2242       guarantee(klass_name_at(i)->refcount() != 0, "should have nonzero reference count");
2243     } else if (tag.is_symbol()) {
2244       CPSlot entry = slot_at(i);
2245       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2246     } else if (tag.is_string()) {
2247       CPSlot entry = slot_at(i);
2248       guarantee(entry.get_symbol()->refcount() != 0, "should have nonzero reference count");
2249     }
2250   }
2251   if (cache() != NULL) {
2252     // Note: cache() can be NULL before a class is completely setup or
2253     // in temporary constant pools used during constant pool merging
2254     guarantee(cache()->is_constantPoolCache(), "should be constant pool cache");
2255   }
2256   if (pool_holder() != NULL) {
2257     // Note: pool_holder() can be NULL in temporary constant pools
2258     // used during constant pool merging
2259     guarantee(pool_holder()->is_klass(),    "should be klass");
2260   }
2261 }
2262 
2263 
2264 void SymbolHashMap::add_entry(Symbol* sym, u2 value) {
2265   char *str = sym->as_utf8();
2266   unsigned int hash = compute_hash(str, sym->utf8_length());
2267   unsigned int index = hash % table_size();
2268 
2269   // check if already in map
2270   // we prefer the first entry since it is more likely to be what was used in
2271   // the class file
2272   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2273     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2274     if (en->hash() == hash && en->symbol() == sym) {
2275         return;  // already there
2276     }
2277   }
2278 
2279   SymbolHashMapEntry* entry = new SymbolHashMapEntry(hash, sym, value);
2280   entry->set_next(bucket(index));
2281   _buckets[index].set_entry(entry);
2282   assert(entry->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2283 }
2284 
2285 SymbolHashMapEntry* SymbolHashMap::find_entry(Symbol* sym) {
2286   assert(sym != NULL, "SymbolHashMap::find_entry - symbol is NULL");
2287   char *str = sym->as_utf8();
2288   int   len = sym->utf8_length();
2289   unsigned int hash = SymbolHashMap::compute_hash(str, len);
2290   unsigned int index = hash % table_size();
2291   for (SymbolHashMapEntry *en = bucket(index); en != NULL; en = en->next()) {
2292     assert(en->symbol() != NULL, "SymbolHashMapEntry symbol is NULL");
2293     if (en->hash() == hash && en->symbol() == sym) {
2294       return en;
2295     }
2296   }
2297   return NULL;
2298 }