diff a/src/hotspot/share/cds/archiveHeapWriter.cpp b/src/hotspot/share/cds/archiveHeapWriter.cpp
--- a/src/hotspot/share/cds/archiveHeapWriter.cpp
+++ b/src/hotspot/share/cds/archiveHeapWriter.cpp
@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.
@@ -25,10 +25,12 @@
 #include "precompiled.hpp"
 #include "cds/archiveHeapWriter.hpp"
 #include "cds/cdsConfig.hpp"
 #include "cds/filemap.hpp"
 #include "cds/heapShared.hpp"
+#include "cds/regeneratedClasses.hpp"
+#include "classfile/javaClasses.hpp"
 #include "classfile/systemDictionary.hpp"
 #include "gc/shared/collectedHeap.hpp"
 #include "memory/iterator.inline.hpp"
 #include "memory/oopFactory.hpp"
 #include "memory/universe.hpp"
@@ -57,14 +59,23 @@
 size_t ArchiveHeapWriter::_heap_roots_word_size;
 
 address ArchiveHeapWriter::_requested_bottom;
 address ArchiveHeapWriter::_requested_top;
 
+static size_t _num_strings = 0;
+static size_t _string_bytes = 0; 
+static size_t _num_packages = 0;
+
 GrowableArrayCHeap<ArchiveHeapWriter::NativePointerInfo, mtClassShared>* ArchiveHeapWriter::_native_pointers;
 GrowableArrayCHeap<oop, mtClassShared>* ArchiveHeapWriter::_source_objs;
+GrowableArrayCHeap<oop, mtClassShared>* ArchiveHeapWriter::_perm_objs = nullptr;
 GrowableArrayCHeap<ArchiveHeapWriter::HeapObjOrder, mtClassShared>* ArchiveHeapWriter::_source_objs_order;
 
+static GrowableArrayCHeap<size_t, mtClassShared> *_permobj_seg_buffered_addrs = nullptr;
+static GrowableArrayCHeap<size_t, mtClassShared> *_permobj_seg_bytesizes = nullptr;
+static GrowableArrayCHeap<int, mtClassShared> *_permobj_seg_lengths = nullptr;
+
 ArchiveHeapWriter::BufferOffsetToSourceObjectTable*
   ArchiveHeapWriter::_buffer_offset_to_source_obj_table = nullptr;
 
 
 typedef ResourceHashtable<
@@ -86,10 +97,14 @@
     _requested_top = nullptr;
 
     _native_pointers = new GrowableArrayCHeap<NativePointerInfo, mtClassShared>(2048);
     _source_objs = new GrowableArrayCHeap<oop, mtClassShared>(10000);
 
+    _permobj_seg_buffered_addrs = new GrowableArrayCHeap<size_t, mtClassShared>(5);
+    _permobj_seg_bytesizes = new GrowableArrayCHeap<size_t, mtClassShared>(5);
+    _permobj_seg_lengths = new GrowableArrayCHeap<int, mtClassShared>(5);
+
     guarantee(UseG1GC, "implementation limitation");
     guarantee(MIN_GC_REGION_ALIGNMENT <= /*G1*/HeapRegion::min_region_size_in_words() * HeapWordSize, "must be");
   }
 }
 
@@ -97,15 +112,22 @@
   _source_objs->append(src_obj);
 }
 
 void ArchiveHeapWriter::write(GrowableArrayCHeap<oop, mtClassShared>* roots,
                               ArchiveHeapInfo* heap_info) {
+  ResourceMark rm;
+  GrowableArray<size_t> permobj_seg_offsets;
   assert(HeapShared::can_write(), "sanity");
   allocate_buffer();
-  copy_source_objs_to_buffer(roots);
+  int num_permobj = copy_source_objs_to_buffer(roots, &permobj_seg_offsets);
   set_requested_address(heap_info);
-  relocate_embedded_oops(roots, heap_info);
+  relocate_embedded_oops(roots, heap_info, &permobj_seg_offsets, num_permobj);
+  if (UseCompressedOops) {
+    add_permobj_segments_to_roots<narrowOop>(roots, heap_info, &permobj_seg_offsets);
+  } else {
+    add_permobj_segments_to_roots<oop>(roots, heap_info, &permobj_seg_offsets);
+  }
 }
 
 bool ArchiveHeapWriter::is_too_large_to_archive(oop o) {
   return is_too_large_to_archive(o->size());
 }
@@ -184,17 +206,21 @@
   // We usually have very small heaps. If we get a huge one it's probably caused by a bug.
   guarantee(min_bytes <= max_jint, "we dont support archiving more than 2G of objects");
   _buffer->at_grow(to_array_index(min_bytes));
 }
 
-void ArchiveHeapWriter::copy_roots_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) {
+size_t ArchiveHeapWriter::create_objarray_in_buffer(GrowableArrayCHeap<oop, mtClassShared>* input,
+                                                    int from,         // copy from this index in input
+                                                    int num_elms,     // copy this number of elements from input
+                                                    int extra_length, // add extra elements at the end of the copy
+                                                    size_t& objarray_word_size) {
   Klass* k = Universe::objectArrayKlass(); // already relocated to point to archived klass
-  int length = roots->length();
-  _heap_roots_word_size = objArrayOopDesc::object_size(length);
-  size_t byte_size = _heap_roots_word_size * HeapWordSize;
+  int length = num_elms + extra_length;
+  objarray_word_size = objArrayOopDesc::object_size(length);
+  size_t byte_size = objarray_word_size * HeapWordSize;
   if (byte_size >= MIN_GC_REGION_ALIGNMENT) {
-    log_error(cds, heap)("roots array is too large. Please reduce the number of classes");
+    log_error(cds, heap)("input array is too large. Please reduce the number of classes");
     vm_exit(1);
   }
 
   maybe_fill_gc_region_gap(byte_size);
 
@@ -212,23 +238,25 @@
     // This is copied from ObjArrayAllocator::initialize
     arrayOopDesc::set_length(mem, length);
   }
 
   objArrayOop arrayOop = objArrayOop(cast_to_oop(mem));
-  for (int i = 0; i < length; i++) {
+  for (int i = 0; i < num_elms; i++) {
     // Do not use arrayOop->obj_at_put(i, o) as arrayOop is outside of the real heap!
-    oop o = roots->at(i);
+    oop o = input->at(i + from);
     if (UseCompressedOops) {
       * arrayOop->obj_at_addr<narrowOop>(i) = CompressedOops::encode(o);
     } else {
       * arrayOop->obj_at_addr<oop>(i) = o;
     }
   }
   log_info(cds, heap)("archived obj roots[%d] = " SIZE_FORMAT " bytes, klass = %p, obj = %p", length, byte_size, k, mem);
 
-  _heap_roots_offset = _buffer_used;
+  size_t roots_bottom_offset = _buffer_used;
   _buffer_used = new_used;
+
+  return roots_bottom_offset;
 }
 
 static int oop_sorting_rank(oop o) {
   bool has_oop_ptr, has_native_ptr;
   HeapShared::get_pointer_info(o, has_oop_ptr, has_native_ptr);
@@ -279,28 +307,64 @@
   log_info(cds)("computed ranks");
   _source_objs_order->sort(compare_objs_by_oop_fields);
   log_info(cds)("sorting heap objects done");
 }
 
-void ArchiveHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) {
+int ArchiveHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots, GrowableArray<size_t>* permobj_seg_offsets) {
   sort_source_objs();
+  _perm_objs = new GrowableArrayCHeap<oop, mtClassShared>();
   for (int i = 0; i < _source_objs_order->length(); i++) {
     int src_obj_index = _source_objs_order->at(i)._index;
     oop src_obj = _source_objs->at(src_obj_index);
     HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(src_obj);
     assert(info != nullptr, "must be");
     size_t buffer_offset = copy_one_source_obj_to_buffer(src_obj);
     info->set_buffer_offset(buffer_offset);
 
     _buffer_offset_to_source_obj_table->put_when_absent(buffer_offset, src_obj);
     _buffer_offset_to_source_obj_table->maybe_grow();
+    if (UsePermanentHeapObjects) {
+      // TODO: add only the objects that are needed by AOT. (How??)
+      int perm_index = _perm_objs->length();
+      HeapShared::add_to_permanent_index_table(src_obj, perm_index);
+      _perm_objs->append(src_obj);
+    }
+  }
+
+  // Create HeapShared::roots() in the output buffer. Reserve some extra slots at the end of it
+  // for the permobj_segments
+  int permobj_segments = (_perm_objs->length() + PERMOBJ_SEGMENT_MAX_LENGTH - 1) / PERMOBJ_SEGMENT_MAX_LENGTH;
+  _heap_roots_offset = create_objarray_in_buffer(roots, 0, roots->length(), permobj_segments, _heap_roots_word_size);
+
+  // Create the permobj_segments in the output buffer.
+  for (int from = 0; from < _perm_objs->length(); from += PERMOBJ_SEGMENT_MAX_LENGTH) {
+    int num_elems = MIN2(PERMOBJ_SEGMENT_MAX_LENGTH, _perm_objs->length() - from);
+    size_t word_size;
+    size_t permobj_seg_bottom_offset = create_objarray_in_buffer(_perm_objs, from, num_elems, 0, word_size);
+    permobj_seg_offsets->append(permobj_seg_bottom_offset);
+    _permobj_seg_buffered_addrs->append(permobj_seg_bottom_offset);
+    _permobj_seg_bytesizes->append(word_size * HeapWordSize);
+    _permobj_seg_lengths->append(num_elems);
   }
 
-  copy_roots_to_buffer(roots);
+  log_info(cds)("Size of heap region = " SIZE_FORMAT " bytes, %d objects, %d roots, %d native ptrs, %d permobjs in %d segments",
+                _buffer_used, _source_objs->length() + 2, roots->length(), _num_native_ptrs, _perm_objs->length(), permobj_segments);
+  log_info(cds)("   strings  = " SIZE_FORMAT_W(8) " (" SIZE_FORMAT " bytes)", _num_strings, _string_bytes);
+  log_info(cds)("   packages = " SIZE_FORMAT_W(8), _num_packages);
+
+  assert(permobj_seg_offsets->length() == permobj_segments, "sanity");
+  HeapShared::set_permobj_segments(permobj_segments);
+  int n = _perm_objs->length();
+  return n;
+}
 
-  log_info(cds)("Size of heap region = " SIZE_FORMAT " bytes, %d objects, %d roots, %d native ptrs",
-                _buffer_used, _source_objs->length() + 1, roots->length(), _num_native_ptrs);
+oop ArchiveHeapWriter::get_perm_object_by_index(int permanent_index) {
+  if (_perm_objs != nullptr && 0 <= permanent_index && permanent_index < _perm_objs->length()) {
+    return _perm_objs->at(permanent_index);
+  } else {
+    return nullptr;
+  }
 }
 
 size_t ArchiveHeapWriter::filler_array_byte_size(int length) {
   size_t byte_size = objArrayOopDesc::object_size(length) * HeapWordSize;
   return byte_size;
@@ -381,11 +445,27 @@
 void update_buffered_object_field(address buffered_obj, int field_offset, T value) {
   T* field_addr = cast_to_oop(buffered_obj)->field_addr<T>(field_offset);
   *field_addr = value;
 }
 
+void ArchiveHeapWriter::update_stats(oop src_obj) {
+  if (java_lang_String::is_instance(src_obj)) {
+    _num_strings ++;
+    _string_bytes += src_obj->size() * HeapWordSize;
+    _string_bytes += java_lang_String::value(src_obj)->size() * HeapWordSize;
+  } else {
+    Klass* k = src_obj->klass();
+    Symbol* name = k->name();
+    if (name->equals("java/lang/NamedPackage") || name->equals("java/lang/Package")) {
+      _num_packages ++;
+    }
+  }
+}
+
 size_t ArchiveHeapWriter::copy_one_source_obj_to_buffer(oop src_obj) {
+  update_stats(src_obj);
+
   assert(!is_too_large_to_archive(src_obj), "already checked");
   size_t byte_size = src_obj->size() * HeapWordSize;
   assert(byte_size > 0, "no zero-size objects");
 
   // For region-based collectors such as G1, the archive heap may be mapped into
@@ -498,11 +578,15 @@
   return CompressedOops::decode(*buffered_addr);
 }
 
 template <typename T> void ArchiveHeapWriter::relocate_field_in_buffer(T* field_addr_in_buffer, CHeapBitMap* oopmap) {
   oop source_referent = load_source_oop_from_buffer<T>(field_addr_in_buffer);
-  if (!CompressedOops::is_null(source_referent)) {
+  if (source_referent != nullptr) {
+    if (java_lang_Class::is_instance(source_referent)) {
+      source_referent = HeapShared::scratch_java_mirror(source_referent);
+      assert(source_referent != nullptr, "must be");
+    }
     oop request_referent = source_obj_to_requested_obj(source_referent);
     store_requested_oop_in_buffer<T>(field_addr_in_buffer, request_referent);
     mark_oop_pointer<T>(field_addr_in_buffer, oopmap);
   }
 }
@@ -542,13 +626,13 @@
     assert(src_hash == archived_hash, "Different hash codes: original " INTPTR_FORMAT ", archived " INTPTR_FORMAT, src_hash, archived_hash);
   }
 }
 
 // Relocate an element in the buffered copy of HeapShared::roots()
-template <typename T> void ArchiveHeapWriter::relocate_root_at(oop requested_roots, int index, CHeapBitMap* oopmap) {
+template <typename T> void ArchiveHeapWriter::relocate_root_at(oop requested_roots, address buffered_roots_addr, int index, CHeapBitMap* oopmap) {
   size_t offset = (size_t)((objArrayOop)requested_roots)->obj_at_offset<T>(index);
-  relocate_field_in_buffer<T>((T*)(buffered_heap_roots_addr() + offset), oopmap);
+  relocate_field_in_buffer<T>((T*)(buffered_roots_addr + offset), oopmap);
 }
 
 class ArchiveHeapWriter::EmbeddedOopRelocator: public BasicOopIterateClosure {
   oop _src_obj;
   address _buffered_obj;
@@ -579,11 +663,13 @@
                 (end - start) * 100 / total_bits);
 }
 
 // Update all oop fields embedded in the buffered objects
 void ArchiveHeapWriter::relocate_embedded_oops(GrowableArrayCHeap<oop, mtClassShared>* roots,
-                                               ArchiveHeapInfo* heap_info) {
+                                               ArchiveHeapInfo* heap_info,
+                                               GrowableArray<size_t>* permobj_seg_offsets,
+                                               int num_permobjs) {
   size_t oopmap_unit = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop));
   size_t heap_region_byte_size = _buffer_used;
   heap_info->oopmap()->resize(heap_region_byte_size   / oopmap_unit);
 
   for (int i = 0; i < _source_objs_order->length(); i++) {
@@ -596,37 +682,95 @@
     address buffered_obj = offset_to_buffered_address<address>(info->buffer_offset());
     EmbeddedOopRelocator relocator(src_obj, buffered_obj, heap_info->oopmap());
     src_obj->oop_iterate(&relocator);
   };
 
-  // Relocate HeapShared::roots(), which is created in copy_roots_to_buffer() and
+  // Relocate HeapShared::roots(), which is created in create_objarray_in_buffer() and
   // doesn't have a corresponding src_obj, so we can't use EmbeddedOopRelocator on it.
   oop requested_roots = requested_obj_from_buffer_offset(_heap_roots_offset);
   update_header_for_requested_obj(requested_roots, nullptr, Universe::objectArrayKlass());
   int length = roots != nullptr ? roots->length() : 0;
   for (int i = 0; i < length; i++) {
     if (UseCompressedOops) {
-      relocate_root_at<narrowOop>(requested_roots, i, heap_info->oopmap());
+      relocate_root_at<narrowOop>(requested_roots, buffered_heap_roots_addr(), i, heap_info->oopmap());
     } else {
-      relocate_root_at<oop>(requested_roots, i, heap_info->oopmap());
+      relocate_root_at<oop>(requested_roots, buffered_heap_roots_addr(), i, heap_info->oopmap());
+    }
+  }
+
+  int num_permobjs_relocated = 0;
+  for (int i = 0; i < permobj_seg_offsets->length(); i++) {
+    int length = MIN2(PERMOBJ_SEGMENT_MAX_LENGTH, num_permobjs - num_permobjs_relocated);
+    // Relocate each of the segments. They were created in create_objarray_in_buffer() and
+    // don't have a corresponding src_obj, so we can't use EmbeddedOopRelocator.
+    size_t permobj_seg_bottom_offset = permobj_seg_offsets->at(i);
+    oop requested_permobj_seg = requested_obj_from_buffer_offset(permobj_seg_bottom_offset);
+    update_header_for_requested_obj(requested_permobj_seg, nullptr, Universe::objectArrayKlass());
+    for (int i = 0; i < length; i++) {
+      address buffered_addr = offset_to_buffered_address<address>(permobj_seg_bottom_offset);
+      if (UseCompressedOops) {
+        relocate_root_at<narrowOop>(requested_permobj_seg, buffered_addr, i, heap_info->oopmap());
+      } else {
+        relocate_root_at<oop>(requested_permobj_seg, buffered_addr, i, heap_info->oopmap());
+      }
     }
+    num_permobjs_relocated += length;
   }
 
   compute_ptrmap(heap_info);
 
   size_t total_bytes = (size_t)_buffer->length();
   log_bitmap_usage("oopmap", heap_info->oopmap(), total_bytes / (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop)));
   log_bitmap_usage("ptrmap", heap_info->ptrmap(), total_bytes / sizeof(address));
 }
 
+// Put the permobj_segments in the extra space that we have reserved at the end of the HeapShared::roots() array.
+template <typename T> void ArchiveHeapWriter::add_permobj_segments_to_roots(GrowableArrayCHeap<oop, mtClassShared>* roots,
+                                                                            ArchiveHeapInfo* heap_info,
+                                                                            GrowableArray<size_t>* permobj_seg_offsets) {
+  for (int i = 0; i <  permobj_seg_offsets->length(); i++) {
+    size_t permobj_seg_bottom_offset = permobj_seg_offsets->at(i);
+    oop requested_roots = requested_obj_from_buffer_offset(_heap_roots_offset);
+    oop requested_permobj_seg = requested_obj_from_buffer_offset(permobj_seg_bottom_offset);
+    int permobj_index = roots->length() + i;
+
+    size_t offset = (size_t)((objArrayOop)requested_roots)->obj_at_offset<T>(permobj_index);
+    T* addr = (T*)(buffered_heap_roots_addr() + offset);
+    store_requested_oop_in_buffer<T>(addr, requested_permobj_seg);
+    mark_oop_pointer<T>(addr, heap_info->oopmap());
+  }
+}
+
+// If the buffered_addr is one of the permobj segments, returns the size information about this segment.
+int ArchiveHeapWriter::get_permobj_segment_at(address buffered_addr, size_t* byte_size, int* permobj_segment_length) {
+  size_t offset = buffered_addr - buffer_bottom();
+  for (int i = 0; i < _permobj_seg_buffered_addrs->length(); i++) {
+    if (offset == _permobj_seg_buffered_addrs->at(i)) {
+      *byte_size = _permobj_seg_bytesizes->at(i);
+      *permobj_segment_length = _permobj_seg_lengths->at(i);
+      return i;
+    }
+  }
+  return -1;
+}
+
+oop ArchiveHeapWriter::get_permobj_source_addr(int permobj_segment, int index) {
+  for (int i = 0; i < permobj_segment; i++) {
+    index += _permobj_seg_lengths->at(i);
+  }
+
+  return _source_objs->at(index);
+}
+
 void ArchiveHeapWriter::mark_native_pointer(oop src_obj, int field_offset) {
   Metadata* ptr = src_obj->metadata_field_acquire(field_offset);
   if (ptr != nullptr) {
     NativePointerInfo info;
     info._src_obj = src_obj;
     info._field_offset = field_offset;
     _native_pointers->append(info);
+    assert(ArchiveBuilder::current()->has_been_archived((address)ptr), "must be archived %p", ptr);
     HeapShared::set_has_native_pointers(src_obj);
     _num_native_ptrs ++;
   }
 }
 
@@ -679,10 +823,14 @@
 
     Metadata** buffered_field_addr = requested_addr_to_buffered_addr(requested_field_addr);
     Metadata* native_ptr = *buffered_field_addr;
     assert(native_ptr != nullptr, "sanity");
 
+    if (RegeneratedClasses::has_been_regenerated((address)native_ptr)) {
+      native_ptr = (Metadata*)RegeneratedClasses::get_regenerated_object((address)native_ptr);
+    }
+
     address buffered_native_ptr = ArchiveBuilder::current()->get_buffered_addr((address)native_ptr);
     address requested_native_ptr = ArchiveBuilder::current()->to_requested(buffered_native_ptr);
     *buffered_field_addr = (Metadata*)requested_native_ptr;
   }
 
