/* Portable data dumper for XEmacs. -*- coding: utf-8 -*-
   Copyright (C) 1999-2000,2004 Olivier Galibert
   Copyright (C) 2001 Martin Buchholz
   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2010 Ben Wing.
   Copyright (C) 2025 Free Software Foundation.

This file is part of XEmacs.

XEmacs is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

XEmacs is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with XEmacs.  If not, see <http://www.gnu.org/licenses/>. */

/* Synched up with: Not in FSF. */

/* This file has been Mule-ized, Ben Wing, 10-10-04. */

/* #### Put in much more assertions.  Whenever we store fixups in the
   process or writing out data, make sure the fixups (offsets) point to the
   beginning of an object, i.e. are registered.  Same whenever we read in
   -- verify offsets as registered, and when compute a fixup, verify the
   pointer is pointing within the pdump area.  registered and check within
   pdump area.  For specific types of pointers (e.g. to Lisp_Objects),
   check if they're pointing to the right kinds of types.  It should be
   possible to check that a putative Lisp_Object is really a Lisp_Object
   since it will follow a strict format in its header. */

#include <config.h>
#include "lisp.h"

#include "specifier.h"
#include "file-coding.h"
#include "elhash.h"
#include "lstream.h"
#include "sysfile.h"
#include "console-stream.h"

#ifdef WIN32_NATIVE
#include "syswindows.h"
#else
#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif
#ifdef DUMP_IN_EXEC
#include "dump-data.h"
#else
#include "paths.h"
#endif
#endif

typedef struct
{
  const void *blockaddr;
  Bytecount size;
  const struct memory_description *desc;
} pdump_root_block;

typedef struct
{
  Dynarr_declare (pdump_root_block);
} pdump_root_block_dynarr;

typedef struct
{
  void **ptraddress;
  const struct sized_memory_description *desc;
} pdump_root_block_ptr;

typedef struct
{
  Dynarr_declare (pdump_root_block_ptr);
} pdump_root_block_ptr_dynarr;

typedef struct
{
  const void *object;
  void *data;
  Bytecount size;
  EMACS_INT offset;
  EMACS_INT dest_offset;
  EMACS_INT save_offset;
  const struct opaque_convert_functions *fcts;
} pdump_cv_data_info;

typedef struct 
{
  Dynarr_declare (pdump_cv_data_info);
} pdump_cv_data_info_dynarr;

typedef struct
{
  EMACS_INT dest_offset;
  EMACS_INT save_offset;
  Bytecount size;
} pdump_cv_data_dump_info;

typedef struct
{
  const void *object;
  void *data;
  Bytecount size;
  EMACS_INT index;
  EMACS_INT save_offset;
  const struct opaque_convert_functions *fcts;
} pdump_cv_ptr_info;

typedef struct 
{
  Dynarr_declare (pdump_cv_ptr_info);
} pdump_cv_ptr_info_dynarr;

typedef struct
{
  EMACS_INT save_offset;
  Bytecount size;
} pdump_cv_ptr_dump_info;

typedef struct
{
  EMACS_INT save_offset;
  Bytecount size;
  void *adr;
} pdump_cv_ptr_load_info;

typedef struct
{
  Lisp_Object *address;
  Lisp_Object value;
} pdump_static_Lisp_Object;

typedef struct
{
  void **address;
  void *value;
} pdump_static_pointer;

static pdump_root_block_dynarr *pdump_root_blocks;
static pdump_root_block_ptr_dynarr *pdump_root_block_ptrs;
static Lisp_Object_ptr_dynarr *pdump_root_lisp_objects;
static Lisp_Object_ptr_dynarr *pdump_nil_lisp_objects;
static Lisp_Object_ptr_dynarr *pdump_weak_object_chains;
static pdump_cv_data_info_dynarr *pdump_cv_data;
static pdump_cv_ptr_info_dynarr *pdump_cv_ptr;

/* Mark SIZE bytes at non-heap address BLOCKADDR for dumping, described
   by DESC.  Called by outside callers during XEmacs initialization.  */

void
dump_add_root_block (const void *blockaddr, Bytecount size,
		     const struct memory_description *desc)
{
  pdump_root_block info;
  info.blockaddr = blockaddr;
  info.size = size;
  info.desc = desc;
  if (pdump_root_blocks == NULL)
    pdump_root_blocks = Dynarr_new (pdump_root_block);
  Dynarr_add (pdump_root_blocks, info);
}

/* Mark the block described by DESC and pointed to by the pointer at
   non-heap address PTRADDRESS for dumping.
   All the objects reachable from this pointer will also be dumped.
   Called by outside callers during XEmacs initialization. */
void
dump_add_root_block_ptr (void *ptraddress,
			 const struct sized_memory_description *desc)
{
  pdump_root_block_ptr info;
  info.ptraddress = (void **) ptraddress;
  info.desc = desc;

  if (pdump_root_block_ptrs == NULL)
    pdump_root_block_ptrs = Dynarr_new (pdump_root_block_ptr);
  Dynarr_add (pdump_root_block_ptrs, info);
}

/* Mark the Lisp_Object at non-heap address VARADDRESS for dumping.
   All the objects reachable from this var will also be dumped.
   Called by outside callers during XEmacs initialization.  */
void
dump_add_root_lisp_object (Lisp_Object *varaddress)
{
  if (pdump_root_lisp_objects == NULL)
    pdump_root_lisp_objects = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
  Dynarr_add (pdump_root_lisp_objects, varaddress);
}

void dump_add_nil_lisp_object_1 (Lisp_Object *);

void
dump_add_nil_lisp_object_1 (Lisp_Object *varaddress)
{
  if (pdump_nil_lisp_objects == NULL)
    pdump_nil_lisp_objects
	    = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
  gc_checking_assert (!EQ (*varaddress, Qnull_pointer));
  Dynarr_add (pdump_nil_lisp_objects, varaddress);
}

/* Like dump_add_root_lisp_object(), but tell the dumper that VAR should be
   initialized to Qnil on pdump_load(), irrespective of its value at dump
   time.  */
void
dump_add_nil_lisp_object (Lisp_Object *varaddress)
{
  dump_add_nil_lisp_object_1 (varaddress);
  dump_add_root_lisp_object (varaddress);
}

/* Mark the list pointed to by the Lisp_Object at VARADDRESS for dumping.
   Called by outside callers during XEmacs initialization.  */
void
dump_add_weak_object_chain (Lisp_Object *varaddress)
{
  if (pdump_weak_object_chains == NULL)
    pdump_weak_object_chains = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
  Dynarr_add (pdump_weak_object_chains, varaddress);
}

/* The bulk of the entries in STATICPROS are pointers in the data
   segment. However, it is legitimate to pass heap pointers to staticpro(),
   when those addresses are known to the dumper. The dumper separates these
   out and when the dump file is written staticpros contains only pointers in
   the data segment.

   We want those Lisp objects protected by staticpro() to be relocated on load
   from the dump file. Data segment objects passed to staticpro(), which are
   global variables like Qfoo or Vbar, are themselves pointers to heap objects,
   and need to be annotated as such within the dump file, otherwise ASLR
   interacts poorly with them. This is achieved with dump_add_root_block_ptr
   (&staticpros, &staticpros_description) within pdump().

   The annotation as XD_BLOCK_DATA_PTR means that we will save and restore the
   pointer within the data segment (correcting for ASLR), as well as the
   Lisp_Object pointed to, and those Lisp_Objects pointed to within it,
   appropriately for the dump file relocation. */
static const struct memory_description staticpro_description_1[] = {
  { XD_BLOCK_DATA_PTR, 0, 1, { &lisp_object_description } },
  { XD_END }
};

static const struct sized_memory_description staticpro_description = {
  sizeof (Lisp_Object *),
  staticpro_description_1
};

static const struct memory_description staticpros_description_1[] = {
  XD_DYNARR_DESC (Lisp_Object_ptr_dynarr, &staticpro_description),
  { XD_END }
};

static const struct sized_memory_description staticpros_description = {
  sizeof (Lisp_Object_ptr_dynarr),
  staticpros_description_1
};

/* While temacs is running, STATICPROS_NODUMP describes pointers to
   Lisp_Objects that we wish to have reachable by GC while running temacs, but
   that should be discarded at generation of the dump file.  For this use-case
   it should be empty immediately after pdump_load() and it does not matter
   whether a pointer is in the heap or in the data segment.

   Traditionally the related STATICPROS only contained pointers to the data
   segment (or to BSS). This led to abuse of staticpro_nodump() for heap
   segment pointers and reinit_vars_of_*() functions that solely existed to
   call staticpro_nodump() on an address in the dump file.

   staticpro() now accepts pointers to the data segment, the BSS and the heap.

   As the dump registration step encounters the addresses of heap
   Lisp_Objects, it checks if those addresses are in STATICPROS, and if so it
   moves them to STATICPROS_NODUMP, which has its elements relocated as
   appropriate for addresses in the dump file. This does not conflict with its
   occasional use by modules after pdump_load(). */
static const struct memory_description staticpro_nodump_description_1[] = {
  { XD_BLOCK_OFFSET_PTR, 0, 1, { &lisp_object_description } },
  { XD_END }
};

static const struct sized_memory_description staticpro_nodump_description = {
  sizeof (Lisp_Object *),
  staticpro_nodump_description_1
};

static const struct memory_description staticpros_nodump_description_1[] = {
  XD_DYNARR_DESC (Lisp_Object_ptr_dynarr, &staticpro_nodump_description),
  { XD_END }
};

static const struct sized_memory_description staticpros_nodump_description = {
  sizeof (Lisp_Object_ptr_dynarr),
  staticpros_nodump_description_1
};

inline static void
pdump_align_stream (FILE *stream, Bytecount alignment)
{
  OFF_T offset = FTELL (stream);
  OFF_T adjustment = ALIGN_SIZE (offset, alignment) - offset;
  if (adjustment)
    {
      if (FSEEK (stream, adjustment, SEEK_CUR) == -1)
	{
	  report_file_error ("Unable to fseek dump file",
			     build_ascstring (EMACS_DUMP_FILE_NAME));
	}
    }
}

#define PDUMP_ALIGN_OUTPUT(type) pdump_align_stream (pdump_out, ALIGNOF (type))

#define PDUMP_WRITE(type, object) \
retry_fwrite (&object, sizeof (object), 1, pdump_out);

#define PDUMP_WRITE_ALIGNED(type, object) do {	\
  PDUMP_ALIGN_OUTPUT (type);			\
  PDUMP_WRITE (type, object);			\
} while (0)

#define PDUMP_READ(ptr, type) \
(((type *) (ptr = (Rawbyte *) (((type *) ptr) + 1)))[-1])

#define PDUMP_READ_ALIGNED(ptr, type) \
((ptr = (Rawbyte *) ALIGN_PTR (ptr, type)), PDUMP_READ (ptr, type))



typedef struct
{
  const struct memory_description *desc;
  Elemcount count;
} pdump_reloc_table;

static Rawbyte *pdump_rt_list = 0;

void
pdump_objects_unmark (void)
{
  int i;
  Rawbyte *p = pdump_rt_list;
  if (p)
    for (;;)
      {
	pdump_reloc_table *rt = (pdump_reloc_table *)p;
	p += sizeof (pdump_reloc_table);
	if (rt->desc)
	  {
	    for (i=0; i<rt->count; i++)
	      {
		struct lrecord_header *lh = * (struct lrecord_header **) p;
#ifdef ALLOC_TYPE_STATS
		if (C_READONLY_RECORD_HEADER_P (lh))
		  tick_lrecord_stats (lh, ALLOC_IN_USE);

		else
		  {
		    tick_lrecord_stats (lh, MARKED_RECORD_HEADER_P (lh) ?
					ALLOC_IN_USE : ALLOC_ON_FREE_LIST);
		    UNMARK_RECORD_HEADER (lh);
		  }
#else /* not ALLOC_TYPE_STATS */
		if (! C_READONLY_RECORD_HEADER_P (lh))
		  UNMARK_RECORD_HEADER (lh);
#endif /* (not) ALLOC_TYPE_STATS */
		p += sizeof (EMACS_INT);
	      }
	  } else
	    break;
      }
}


/* The structure of the dump file looks like this:
 0		- header
		- dumped objects
 stab_offset	- nb_cv_data*struct(dest, adr) for in-object externally
		  represented data
		- nb_cv_ptr*(adr) for pointed-to externally represented data
 		- nb_root_block_ptrs*struct(void *, adr)
		  for global pointers to heap blocks
		- nb_root_blocks*struct(void *, size, info) for global
		  data-segment blocks to restore
		- relocation table
		- root lisp object address/value couples with the count
		  preceding the list
 */


#define PDUMP_SIGNATURE "XEmacsDP"
#define PDUMP_SIGNATURE_LEN (sizeof (PDUMP_SIGNATURE) - 1)

typedef struct
{
  char signature[PDUMP_SIGNATURE_LEN];
  unsigned int id;
  EMACS_UINT stab_offset;
  /* Usually 0, reflecting an offset into the dump file. */
  EMACS_UINT reloc_address;
  /* Known address in the code (text) segment, for ASLR. */
  EMACS_UINT Fcons_address;			
  /* Known address in the data segment, for ASLR. */
  EMACS_UINT lisp_object_description_address;	
  Elemcount nb_root_block_ptrs;
  Elemcount nb_root_blocks;
  Elemcount nb_cv_data;
  Elemcount nb_cv_ptr;
} pdump_header;

Rawbyte *pdump_start;
Rawbyte *pdump_end;
static Bytecount pdump_length;

static pdump_cv_data_dump_info *pdump_loaded_cv_data;
static pdump_cv_ptr_load_info  *pdump_loaded_cv_ptr;

#ifdef WIN32_NATIVE
/* Handle for the dump file */
static HANDLE pdump_hFile = INVALID_HANDLE_VALUE;
/* Handle for the file mapping object for the dump file */
static HANDLE pdump_hMap = INVALID_HANDLE_VALUE;
#endif

static unsigned char pdump_align_table[] =
{
  64, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
  16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
  32, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1,
  16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1
};

static inline int
pdump_size_to_align (Bytecount size)
{
  return pdump_align_table[size % countof (pdump_align_table)];
}

/************************************************************************/
/*                     Registering memory blocks                        */
/************************************************************************/

/* "Registering" or recording a heap memory block (which will need to be
   written out, reloaded and relocated, and to which there may be pointers
   from other heap blocks or from the data segment) happens both in a list
   and in a hash table.  There is a single hash table covering all
   registered blocks, but different lists for different kinds of blocks.
   There is one list for "opaque data" (stuff identified as
   XD_OPAQUE_DATA_PTR, XD_ASCII_STRING), one list for each
   type of Lisp object, and one list for each different memory descriptor.
   This lets similar-sized and aligned objects be grouped together when
   they are written out, to save space.

   pdump_block_list is a list keeping track of registered memory blocks.
   pdump_block_list_elt is a single entry through the list, and the list is
   threaded through the NEXT pointer.  The information in this list
   associated with a particular block of memory is

   -- address of the beginning
   -- number of elements at that address
   -- size of each element
   -- offset to this block in the dumped data

   pdump_desc_list is a list keeping track of the various descriptions
   that we've seen.  The primary purpose of this is so that memory blocks
   can be grouped depending on the particular memory description
   appropriate for them.  The format of the list is different from
   pdump_block_list -- a single array is used. (#### Dynarr should have
   been used!!!).  The information in this list associated with a
   description is

   -- pointer to the description
   -- a pdump_block_list of blocks using that description

   Functions for working with lists of memory blocks:

   -- Add a memory block to a list using pdump_add_block()

   -- Get a memory block from a pointer to its beginning using
      pdump_get_block().  This uses the hash table, which lists everything.

   -- Return the memory-block list (pdump_block_list) associated with a
      descriptor, using pdump_get_block_list().  If no entry found in the
      pdump_desc_list, add a new one.

*/ 

typedef struct pdump_block_list_elt
{
  struct pdump_block_list_elt *next;
  const void *obj;
  Bytecount size;
  Elemcount count;
  EMACS_INT save_offset;
} pdump_block_list_elt;

typedef struct
{
  pdump_block_list_elt *first;
  int align;
  Elemcount count;
} pdump_block_list;

typedef struct pdump_desc_list_elt
{
  pdump_block_list list;
  const struct memory_description *desc;
} pdump_desc_list_elt;

typedef struct
{
  pdump_desc_list_elt *list;
  Elemcount count;
  int size;
} pdump_desc_list;

static pdump_block_list *pdump_object_table;
static pdump_block_list pdump_opaque_data_list;
static pdump_desc_list pdump_desc_table;

static int *pdump_alert_undump_object;

static EMACS_UINT cur_offset;
static Bytecount max_size;
static int pdump_fd;
static void *pdump_buf;
static FILE *pdump_out;

#define PDUMP_HASHSIZE        2164111

static pdump_block_list_elt **pdump_hash;

/* Since most pointers are eight bytes aligned, the >>3 allows for a better hash */
static int
pdump_make_hash (const void *obj)
{
  return ((EMACS_UINT)(obj)>>3) % PDUMP_HASHSIZE;
}

/* Return the entry for an already-registered memory block at OBJ,
   or NULL if none. */

static pdump_block_list_elt *
pdump_get_block (const void *obj)
{
  int pos = pdump_make_hash (obj);
  pdump_block_list_elt *e;

  assert (obj != 0);

  while ((e = pdump_hash[pos]) != 0)
    {
      if (e->obj == obj)
	return e;

      pos++;
      if (pos == PDUMP_HASHSIZE)
	pos = 0;
    }
  return 0;
}

/* Register a new memory block or return the entry for an already-registered
   memory block at OBJ, or NULL if none. */

static void
pdump_add_block (pdump_block_list *list, const void *obj, Bytecount size,
		 Elemcount count)
{
  pdump_block_list_elt *e;
  int pos = pdump_make_hash (obj);

  while ((e = pdump_hash[pos]) != 0)
    {
      if (e->obj == obj)
	return;

      pos++;
      if (pos == PDUMP_HASHSIZE)
	pos = 0;
    }

  e = xnew (pdump_block_list_elt);

  e->next = list->first;
  e->obj = obj;
  e->size = size;
  e->count = count;
  list->first = e;

  list->count += count;
  pdump_hash[pos] = e;

  {
    int align = pdump_size_to_align (size);

    if (align < list->align)
      list->align = align;
  }
}


static pdump_block_list *
pdump_get_block_list (const struct memory_description *desc)
{
  int i;
  for (i=0; i<pdump_desc_table.count; i++)
    if (pdump_desc_table.list[i].desc == desc)
      return &pdump_desc_table.list[i].list;

  if (pdump_desc_table.size <= pdump_desc_table.count)
    {
      if (pdump_desc_table.size == -1)
	pdump_desc_table.size = 10;
      else
	pdump_desc_table.size = pdump_desc_table.size * 2;
      pdump_desc_table.list = (pdump_desc_list_elt *)
	xrealloc (pdump_desc_table.list,
		  pdump_desc_table.size * sizeof (pdump_desc_list_elt));
    }
  pdump_desc_table.list[pdump_desc_table.count].list.first = 0;
  pdump_desc_table.list[pdump_desc_table.count].list.align = ALIGNOF (max_align_t);
  pdump_desc_table.list[pdump_desc_table.count].list.count = 0;
  pdump_desc_table.list[pdump_desc_table.count].desc = desc;

  return &pdump_desc_table.list[pdump_desc_table.count++].list;
}

static pdump_cv_ptr_info *
pdump_find_in_cv_ptr_dynarr(const void *object)
{
  int i;
  for (i = 0; i < Dynarr_length (pdump_cv_ptr); i++)
    if (Dynarr_at (pdump_cv_ptr, i).object == object)
      return Dynarr_atp (pdump_cv_ptr, i);
  return 0;
}

#define BACKTRACE_MAX 65536

static struct
{
  struct lrecord_header *obj;
  int position;
  EMACS_INT offset;
} backtrace[BACKTRACE_MAX];

static int pdump_depth;

void
pdump_backtrace (void)
{
  int i;
  stderr_out ("pdump backtrace :\n");
  for (i = 0; i < pdump_depth; i++)
    {
      if (!backtrace[i].obj)
	stderr_out ("  - ind. (%d, %zd)\n",
		    backtrace[i].position,
		    backtrace[i].offset);
      else
	{
	  stderr_out ("  - %s (%d, %zd)\n",
                      LRECORD_IMPLEMENTATION_IBYTE_NAME
                      (LHEADER_IMPLEMENTATION (backtrace[i].obj)),
		      backtrace[i].position,
		      backtrace[i].offset);
	}
    }
}

static void
pdump_unsupported_dump_type (enum memory_description_type type,
			     int do_backtrace)
{
  stderr_out ("Unsupported dump type : %d\n", type);
#ifdef WIN32_NATIVE
  stderr_out ("Are you compiling with SUPPORT_EDIT_AND_CONTINUE?\n");
  stderr_out ("See the PROBLEMS file.\n");
#endif
  if (do_backtrace)
    pdump_backtrace ();
  ABORT ();
}

static void
pdump_bump_depth (void)
{
  int me = pdump_depth++;
  if (me >= BACKTRACE_MAX)
    {
      stderr_out ("Backtrace overflow, loop ?\n");
      ABORT ();
    }
  backtrace[me].obj = 0;
  backtrace[me].position = 0;
  backtrace[me].offset = 0;
}

/* GC-reachability for those heap Lisp_Objects within the dump file, but not
   reachable from obarray or from a call to staticpro() on a data segment
   address, is a conceptual mess.

   The Right Thing is, at the point dump_add_root_block_ptr() is called,
   descend the block described using its description, add the address of any
   Lisp_Objects encountered to a Dynarr within this file and to
   STATICPROS_NODUMP. Discard the dump-time STATICPROS_NODUMP, then construct a
   STATICPROS_NODUMP to be used post-pdump_load() from that first Dynarr. This
   would be relocated as heap data rather than data-segment or BSS data.

   I implemented something close to this and it led to an extra three thousand
   GC roots post-pdump_load(). This many is clearly not necessary as we have
   been running for decades without this level of GC protection, and adding it
   would have an ongoing noticeable impact on GC performance for normal (post
   pdump_load()) use.

   The old thing was for certain Lisp_Objects to have staticpro_nodump() done
   on them both during loadup and after pdump_load(), in reinit_vars_of_FILE()
   functions. This was done inconsistently, for some symbols and not for
   others; it matters very little for interned symbols, as they will be
   reachable through obarray throughout the dump process and they will not be
   swept post pdump_load()†; it was most important for lists, which would be
   amputated (or rather, their conses would become free) during GC at dump
   time. This was also an abuse of staticpro_nodump() since its other, and
   principal planned use, is for data that we don't wish to have around post
   pdump_load().

   Adding pointers to only those Lisp_Objects described in descriptions passed
   to dump_add_root_block_ptr() or dump_add_root_block() doesn't work, since
   some of the specifier types legitimately reference specifier types that are
   encountered later, and it's impossible to work out that the later types
   need processing from this perspective without adding an extra step to
   pdump().

   The current thing is for those same Lisp_Object addresses to now just have
   staticpro() called on them and for the dumper to figure out which should be
   dumped as heap objects and which as data segment objects. The following
   hash table contains fixnums generated by STORE_VOID_IN_LISP() reflecting
   the entries in STATICPROS before dumping, and the register step moves
   entries to STATICPROS_NODUMP as it encounters heap addresses known to it.

   † Though if they are uninterned and then a new symbol interned their values
   available to the low-level code will differ from that available to
   Lisp. The Right Thing to avoid this is to no longer expose Vobarray as a
   hash table directly to Lisp, and implement the Common Lisp specification
   for packages. This will be practical, the dependencies on the specific type
   of Vobarray were addressed with work in 2016 to move it from being a vector
   to a Lisp hash table. I note Gerd Moellman maintains a fork of GNU Emacs
   with this feature. This would also remove the need for staticpro() within
   defsymbol() and ease unloading of modules. */
static Lisp_Object Vstaticpros_hash, Vstaticpros_nodump_block_offsets;

static int pdump_min_shuffle_depth = 0;

inline static void
shuffle_staticpros_if_needed (const Lisp_Object *pobj, Bytecount offset)
{
  htentry *e;
  if (pdump_depth >= pdump_min_shuffle_depth &&
      (e = find_htentry (STORE_VOID_IN_LISP ((void *) pobj),
                         XHASH_TABLE (Vstaticpros_hash)),
       !HTENTRY_CLEAR_P (e)))
    {
      Dynarr_add (staticpros_nodump, (Lisp_Object *) pobj);
      Dynarr_delete_object (staticpros, (Lisp_Object *) pobj);
      Fremhash (STORE_VOID_IN_LISP ((void *) pobj),
                Vstaticpros_hash);
      Fputhash (STORE_VOID_IN_LISP ((void *) pobj), make_fixnum (offset),
                Vstaticpros_nodump_block_offsets);
    }
}

static void pdump_register_object (Lisp_Object obj);
static void pdump_register_block_contents (const void *data,
					   Bytecount size,
					   const struct memory_description *
					   desc,
					   Elemcount count);
static void pdump_register_block (const void *data,
				  Bytecount size,
				  const struct memory_description *desc,
				  Elemcount count);

static void
pdump_register_sub (const void *data, const struct memory_description *desc)
{
  int pos;
  int me = pdump_depth - 1;

  for (pos = 0; desc[pos].type != XD_END; pos++)
    {
      const struct memory_description *desc1 = &desc[pos];
      EMACS_INT offset = lispdesc_indirect_count (desc1->offset, desc,
						  data);
      const void *rdata = (const Rawbyte *) data + offset;

      backtrace[me].position = pos;
      backtrace[me].offset = offset;

    union_switcheroo:

      /* If the flag says don't dump, then don't dump. */
      if ((desc1->flags) & XD_FLAG_NO_PDUMP)
	continue;

      switch (desc1->type)
	{
	case XD_BYTECOUNT:
	case XD_ELEMCOUNT:
	case XD_HASHCODE:
	case XD_INT:
	case XD_LONG:
	case XD_INT_RESET:
	case XD_LO_LINK:
	case XD_FUNCTION_POINTER:
	case XD_MEMORY_DESCRIPTION:
	case XD_SIZED_MEMORY_DESCRIPTION:
        case XD_BLOCK_OFFSET_PTR:
	  break;
	case XD_OPAQUE_DATA_PTR:
	  {
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);

	    pdump_add_block (&pdump_opaque_data_list,
			     *(void **)rdata, count, 1);
	    break;
	  }
	case XD_ASCII_STRING:
	  {
	    const Ascbyte *str = * (const Ascbyte **) rdata;
	    if (str)
	      pdump_add_block (&pdump_opaque_data_list, str, strlen (str) + 1,
			       1);
	    break;
	  }
	case XD_LISP_OBJECT:
	  {
	    const Lisp_Object *pobj = (const Lisp_Object *) rdata;

	    assert (desc1->data1 == 0);

	    backtrace[me].offset =
	      (const Rawbyte *) pobj - (const Rawbyte *) data;
	    pdump_register_object (*pobj);
            shuffle_staticpros_if_needed (pobj, offset);
	    break;
	  }
	case XD_LISP_OBJECT_ARRAY:
	  {
	    int i;
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);

	    for (i = 0; i < count; i++)
	      {
		const Lisp_Object *pobj = ((const Lisp_Object *) rdata) + i;
		Lisp_Object dobj = *pobj;

		backtrace[me].offset =
		  (const Rawbyte *) pobj - (const Rawbyte *) data;
		pdump_register_object (dobj);
                shuffle_staticpros_if_needed (pobj, backtrace[me].offset);
	      }
	    break;
	  }

	case XD_BLOCK_DATA_PTR:
	  {
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);
	    const struct sized_memory_description *sdesc =
	      lispdesc_indirect_description (data, desc1->data2.descr);
	    const Rawbyte *dobj = *(const Rawbyte **)rdata;

	    if (dobj)
	      {
		pdump_root_block info;

		pdump_register_block (dobj, sdesc->size, sdesc->description,
				      count);
		info.blockaddr = dobj;
		info.size = sdesc->size;
		info.desc = sdesc->description;
		/* This is OK and appropriate; normal entries in
		   pdump_root_blocks do not specify COUNT, and
		   pdump_register_block / pdump_register_block_contents are
		   idempotent. */
		Dynarr_add (pdump_root_blocks, info);
	      }
	    break;
	  }

	case XD_BLOCK_PTR:
	  {
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);
	    const struct sized_memory_description *sdesc =
	      lispdesc_indirect_description (data, desc1->data2.descr);
	    const Rawbyte *dobj = *(const Rawbyte **)rdata;
	    if (dobj)
	      pdump_register_block (dobj, sdesc->size, sdesc->description,
				    count);
	    break;
	  }
	case XD_BLOCK_ARRAY:
	  {
	    EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc,
						       data);
	    const struct sized_memory_description *sdesc =
	      lispdesc_indirect_description (data, desc1->data2.descr);

	    pdump_register_block_contents (rdata, sdesc->size,
					   sdesc->description, count);
	    break;
	  }
	case XD_UNION:
	case XD_UNION_DYNAMIC_SIZE:
	  desc1 = lispdesc_process_xd_union (desc1, desc, data);
	  if (desc1)
	    goto union_switcheroo;
	  break;
	case XD_OPAQUE_PTR_CONVERTIBLE:
	  {
	    pdump_cv_ptr_info info;
	    info.object = *(void **)rdata;
	    info.index = 0;
	    info.save_offset = 0;
	    info.fcts = desc1->data2.funcs;
	    if (!pdump_find_in_cv_ptr_dynarr (info.object))
	      {
		info.fcts->convert(info.object, &info.data, &info.size);
		Dynarr_add (pdump_cv_ptr, info);
	      }
	    break;
	  }
	case XD_OPAQUE_DATA_CONVERTIBLE:
	  {
	    pdump_cv_data_info info;
	    info.object = data;
	    info.offset = offset;
	    info.dest_offset = 0;
	    info.save_offset = 0;
	    info.fcts = desc1->data2.funcs;

	    info.fcts->convert(rdata, &info.data, &info.size);
	    Dynarr_add (pdump_cv_data, info);
	    break;
	  }

	default:
	  pdump_unsupported_dump_type (desc1->type, 1);
	}
    }
}

static void
pdump_register_object (Lisp_Object obj)
{
  struct lrecord_header *objh;
  const struct lrecord_implementation *imp;

  if (!POINTER_TYPE_P (XTYPE (obj)))
    return;

  objh = XRECORD_LHEADER (obj);
  if (!objh)
    return;

  if (pdump_get_block (objh))
    return;

  imp = LHEADER_IMPLEMENTATION (objh);

  if (imp->description
      && RECORD_DUMPABLE (objh))
    {
      pdump_bump_depth ();
      backtrace[pdump_depth - 1].obj = objh;
      pdump_add_block (pdump_object_table + objh->type,
		       objh, detagged_lisp_object_size (objh), 1);
      pdump_register_sub (objh, imp->description);
      --pdump_depth;
    }
  else
    {
      pdump_alert_undump_object[objh->type]++;
      stderr_out ("Undumpable object type : %s\n",
                  LRECORD_IMPLEMENTATION_IBYTE_NAME (imp));
      pdump_backtrace ();
    }
}


/* Register the referenced objects in the array of COUNT blocks located at
   DATA; each block is described by SIZE and DESC.  "Block" here simply
   means any block of memory.

   This does not register the block of memory itself; it may, for
   example, be an array of structures inlined in another memory block
   and thus should not be registered.  See pdump_register_block(),
   which does register the memory block. */

static void
pdump_register_block_contents (const void *data,
			       Bytecount size,
			       const struct memory_description *desc,
			       Elemcount count)
{
  Elemcount i;
  Bytecount elsize;

  pdump_bump_depth ();
  elsize = lispdesc_block_size_1 (data, size, desc);
  for (i = 0; i < count; i++)
    {
      pdump_register_sub (((Rawbyte *) data) + elsize * i, desc);
    }
  --pdump_depth;
}

/* Register the array of COUNT blocks located at DATA; each block is described
   by SDESC.  "Block" here simply means any block of memory, which is more
   accurate and less confusing than terms like `struct' and `object'.  A
   `block' could be a single integer or Lisp_Object, for example, as long as
   the description is accurate.

   This is like pdump_register_block_contents() but also registers
   the memory block itself. */

static void
pdump_register_block (const void *data,
		      Bytecount size,
		      const struct memory_description *desc,
		      Elemcount count)
{
  if (data && !pdump_get_block (data))
    {
      pdump_add_block (pdump_get_block_list (desc), data,
		       lispdesc_block_size_1 (data, size, desc), count);
      pdump_register_block_contents (data, size, desc, count);
    }
}

/* Store the already-calculated new pointer offsets for all pointers in the
   COUNT contiguous blocks of memory, each described by DESC and of size
   SIZE, whose original is located at ORIG_DATA and the modifiable copy at
   DATA.  We examine the description to figure out where the pointers are,
   and then look up the replacement values using pdump_get_block().

   This is done just before writing the modified block of memory to the dump
   file.  The new pointer offsets have been carefully calculated so that the
   data being pointed gets written at that offset in the dump file.  That way,
   the dump file is a correct memory image except for a constant (a delta)
   that needs to be added to all pointers.

   The dump file is, these days, usually stored in the XEmacs executable
   image, and in theory for builds on platforms without ASLR this step
   (addition of the delta) could be done at dump time. It currently (2024)
   isn't, and I have no plans to do that given that the majority of platforms
   have ASLR and would prefer that we not keep data segment addresses constant
   from invocation to invocation.

   If the dump file is not stored in the XEmacs executable, it is loaded using
   mmap(2). The MAP_FIXED flag to mmap(2) is deprecated and unreliable with
   ASLR (the address may already be allocated), so, similarly, adding the
   delta needs to be done at pdump_load() time.

   Dumped data has to be writable at startup to allow that delta to be added
   to pointers within the dump file.  For that reason, it would be of limited
   value to attempt to map the dump file read only, there is no opportunity
   for it to be shared read-only between different invocations of XEmacs. Dump
   data is currently writable from Lisp, there is no special code in the Lisp
   implementation to prevent this, and adding it would make writing Lisp code
   harder.

   Dumped objects that are freed by the garbage collector are not reclaimed.
   This is acceptable, dumped data will rarely be freed (usually only when
   strings are resized, something that doesn't happen much) and the bonus of
   not having the heap infrastructure (the malloc() headers and so on) kept
   around in the dumped data outweighs this occasional loss of garbage. */
static void
pdump_store_new_pointer_offsets (Elemcount count, void *data,
				 const void *orig_data,
				 const struct memory_description *desc,
				 Bytecount size)
{
  Elemcount i;
  int pos;
  /* Process each block one by one */
  for (i = 0; i < count; i++)
    {
      /* CUR points to the beginning of each block in the new data. */
      Rawbyte *cur = ((Rawbyte *)data) + i * size;
      /* Scan each line of the description for relocatable pointers */
      for (pos = 0; desc[pos].type != XD_END; pos++)
	{
	  /* RDATA points to the beginning of each element in the new data. */
	  const struct memory_description *desc1 = &desc[pos];
	  /* #### Change ORIG_DATA to DATA.  See below. */
	  void *rdata = cur + lispdesc_indirect_count (desc1->offset, desc,
						       orig_data);
	union_switcheroo:

	  /* If the flag says don't dump, then don't dump. */
	  if ((desc1->flags) & XD_FLAG_NO_PDUMP)
	    continue;

	  switch (desc1->type)
	    {
	    case XD_BYTECOUNT:
	    case XD_ELEMCOUNT:
	    case XD_HASHCODE:
	    case XD_FUNCTION_POINTER:
	    case XD_MEMORY_DESCRIPTION:
	    case XD_SIZED_MEMORY_DESCRIPTION:
	    case XD_BLOCK_DATA_PTR:
	    case XD_INT:
	    case XD_LONG:
	      break;
	    case XD_INT_RESET:
	      {
		EMACS_INT val = lispdesc_indirect_count (desc1->data1, desc,
							 orig_data);
		* (int *) rdata = (int) val;
		break;
	      }
	    case XD_OPAQUE_DATA_PTR:
	    case XD_ASCII_STRING:
	    case XD_BLOCK_PTR:
	      {
		void *ptr = * (void **) rdata;
		if (ptr)
		  * (EMACS_INT *) rdata = pdump_get_block (ptr)->save_offset;
		break;
	      }
	    case XD_BLOCK_OFFSET_PTR:
	      {
		void *ptr = * (void **) rdata;
		if (ptr)
                  {
                    Lisp_Object offset
                      = Fgethash (STORE_VOID_IN_LISP ((void *) ptr),
                                  Vstaticpros_nodump_block_offsets,
                                  Qnil);
                    pdump_block_list_elt *block = 
                      pdump_get_block ((Rawbyte *) ptr - XFIXNUM (offset));
                    * (EMACS_INT *) rdata
                      = block->save_offset + XFIXNUM (offset);
                  }
		break;
	      }
	    case XD_LO_LINK:
	      {
		/* As described in lrecord.h, this is a weak link.
                   Thus, we need to link this object not (necessarily)
                   to the object directly pointed to, but to the next
                   referenced object in the chain.  None of the
                   intermediate objects will be written out, so we
                   traverse down the chain of objects until we find a
                   referenced one. (The Qnil or Qunbound that ends the
                   chain will always be a referenced object.) */
		Lisp_Object obj = * (Lisp_Object *) rdata;
		pdump_block_list_elt *elt1;
		/* #### Figure out how to handle indirect offsets here.
		   #### In general, when computing indirect counts, do we
		   really need to use the orig_data pointer?  Why not just
		   use the new stuff?

		   No, we don't usually need orig_data.  We only need it
		   when fetching pointers out of the data, not integers.
		   This currently occurs only with description maps.  We
		   should change the other places to DATA to emphasize
		   this. */
		assert (!XD_IS_INDIRECT (desc1->offset));
		for (;;)
		  {
		    elt1 = pdump_get_block (XRECORD_LHEADER (obj));
		    if (elt1)
		      break;
		    obj = * (Lisp_Object *) (desc1->offset +
					     (Rawbyte *)
					     (XRECORD_LHEADER (obj)));
		  }
		* (EMACS_INT *) rdata = elt1->save_offset;
		break;
	      }
	    case XD_LISP_OBJECT:
	      {
		Lisp_Object *pobj = (Lisp_Object *) rdata;

		assert (desc1->data1 == 0);

		if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj))
		  * (EMACS_INT *) pobj =
		    pdump_get_block (XRECORD_LHEADER (*pobj))->save_offset;
		break;
	      }
	    case XD_LISP_OBJECT_ARRAY:
	      {
		EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
							 orig_data);
		int j;

		for (j = 0; j < num; j++)
		  {
		    Lisp_Object *pobj = ((Lisp_Object *) rdata) + j;
		    if (POINTER_TYPE_P (XTYPE (*pobj)) &&
			XRECORD_LHEADER (*pobj))
		      * (EMACS_INT *) pobj =
			pdump_get_block (XRECORD_LHEADER (*pobj))->save_offset;
		  }
		break;
	      }
	    case XD_BLOCK_ARRAY:
	      {
		EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
							 orig_data);
		const struct sized_memory_description *sdesc =
		  lispdesc_indirect_description (orig_data, desc1->data2.descr);

		pdump_store_new_pointer_offsets
		  (num, rdata,
		   ((Rawbyte *) rdata - (Rawbyte *) data) +
		   (Rawbyte *) orig_data,
		   sdesc->description,
		   lispdesc_block_size
		   (((Rawbyte *) rdata - (Rawbyte *) data) +
		    (Rawbyte *) orig_data, sdesc));
		break;
	      }
	    case XD_UNION:
	    case XD_UNION_DYNAMIC_SIZE:
	      desc1 = lispdesc_process_xd_union (desc1, desc, orig_data);
	      if (desc1)
		goto union_switcheroo;
	      break;

	    case XD_OPAQUE_PTR_CONVERTIBLE:
	      *(EMACS_INT *)rdata = pdump_find_in_cv_ptr_dynarr (*(void **)rdata)->index;
	      break;

	    case XD_OPAQUE_DATA_CONVERTIBLE:
	      /* in-object, nothing to do */
	      break;

	    default:
	      pdump_unsupported_dump_type (desc1->type, 0);
	    }
	}
    }
}

/* Write out to global file descriptor PDUMP_OUT the element (one or
   more contiguous blocks of identical size/description) recorded in
   ELT and described by DESC.  The element is first copied to a buffer
   and then all pointers (this includes Lisp_Objects other than
   integer/character) are relocated to the (pre-computed) offset in
   the dump file. */
static void
pdump_dump_data (pdump_block_list_elt *elt,
		 const struct memory_description *desc)
{
  Bytecount size = elt->size;
  Elemcount count = elt->count;
  if (desc)
    {
      /* Copy to temporary buffer */
      memcpy (pdump_buf, elt->obj, size*count);

      /* Store new offsets into all pointers in block */
      pdump_store_new_pointer_offsets (count, pdump_buf, elt->obj, desc, size);
    }
  retry_fwrite (desc ? pdump_buf : elt->obj, size, count, pdump_out);
}

static EMACS_UINT c_code_delta;
static EMACS_UINT c_data_delta;
static EMACS_UINT dump_delta;

static inline lisp_fn_t
pdump_reloc_c_func (const lisp_fn_t ptr)
{
  if (ptr == NULL)
    {
      return NULL;
    }

  return ((lisp_fn_t) ((EMACS_UINT)((Rawbyte *) ptr) + c_code_delta));
}

static inline void *
pdump_reloc_c_data (const void *ptr)
{
  if (ptr == NULL)
    {
      return NULL;
    }

  return (void *) (((EMACS_UINT) ((Rawbyte *) ptr) + c_data_delta));
}

static inline Lisp_Object
pdump_reloc_lisp_object (const Lisp_Object obj)
{
  if (EQ (obj, Qnull_pointer) || !POINTER_TYPE_P (XTYPE (obj)))
    {
      return obj;
    }

  return wrap_pointer_1 ((const void *) ((EMACS_UINT)((Rawbyte *) XPNTR (obj))
                                         + dump_delta));
}

static inline void *
pdump_reloc_lisp_data (const void *ptr)
{
  if (ptr == NULL)
    {
      return NULL;
    }

  return (void *) ((EMACS_UINT)((Rawbyte *) ptr) + dump_delta);
}

/* Like lispdesc_indirect_description(), but adjust the returned value
   according to the delta between the dump-time C data segment and the load
   time C data segment if that is appropriate.

   I had thought it would be necessary to have a separate annotation for
   pointers to struct sized_memory_description *, to handle this, since it
   wasn't certain that objects would not refer to memory descriptions inside
   of previously-loaded objects (which would already have been relocated), but
   it works fine without that currently. Should this stop working the SIGSEGV
   will happen fairly close to this function, and XD_SIZED_MEMORY_DESCRIPTION
   has been added for clarity and potenitial future error-checking in any
   event. */
static const struct sized_memory_description *
pdump_load_lispdesc_indirect_description (const void *obj,
					  const struct sized_memory_description
					  *sdesc)
{
  const struct sized_memory_description *result;

  if (sdesc->description)
    {
#ifdef ERROR_CHECK_GC
      assert (lispdesc_indirect_description (obj, sdesc) == sdesc);
#endif
      return sdesc;
    }

  result = lispdesc_indirect_description (obj, sdesc);

  return (const struct sized_memory_description *) pdump_reloc_c_data (result);
}

/* Relocate a single memory block at DATA, described by DESC, from its
   assumed load location to its actual one by adding DELTA to all heap
   pointers in the block.  Does not recursively relocate any other memory
   blocks pointed to. (We already have a list of all memory blocks in the
   dump file.)  This is used once the dump data has been loaded back in,
   both for blocks sitting in the dumped data (former heap blocks) and in
   global data-segment blocks whose contents have been restored from the
   dumped data. */

static void
pdump_reloc_one (void *data, const struct memory_description *desc)
{
  int pos;

  for (pos = 0; desc[pos].type != XD_END; pos++)
    {
      const struct memory_description *desc1 = &desc[pos];
      void *rdata =
	(Rawbyte *) data + lispdesc_indirect_count (desc1->offset,
							desc, data);

    union_switcheroo:

      /* If the flag says don't dump, then don't dump. */
      if ((desc1->flags) & XD_FLAG_NO_PDUMP)
	continue;

      switch (desc1->type)
	{
	case XD_BYTECOUNT:
	case XD_ELEMCOUNT:
	case XD_HASHCODE:
	case XD_INT:
	case XD_LONG:
	case XD_INT_RESET:
	  break;
	case XD_OPAQUE_DATA_PTR:
	case XD_ASCII_STRING:
	case XD_BLOCK_PTR:
        case XD_BLOCK_OFFSET_PTR:
	case XD_LO_LINK:
	  {
	    void *ptr = *(void **)rdata;
            *(void **) rdata = pdump_reloc_lisp_data (ptr);
	    break;
	  }
	case XD_LISP_OBJECT:
	  {
	    Lisp_Object *pobj = (Lisp_Object *) rdata;

	    assert (desc1->data1 == 0);
	    *pobj = pdump_reloc_lisp_object (*pobj);
	    break;
	  }
	case XD_LISP_OBJECT_ARRAY:
	  {
	    EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
						     data);
	    int j;

	    for (j=0; j<num; j++)
	      {
		Lisp_Object *pobj = (Lisp_Object *) rdata + j;

		*pobj = pdump_reloc_lisp_object (*pobj);
	      }
	    break;
	  }
	case XD_BLOCK_ARRAY:
	  {
	    EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc,
						     data);
	    int j;
	    const struct sized_memory_description *sdesc =
	      pdump_load_lispdesc_indirect_description (data, desc1->data2.descr);
	    Bytecount size = lispdesc_block_size (rdata, sdesc);

	    /* Note: We are recursing over data in the block itself */
	    for (j = 0; j < num; j++)
	      pdump_reloc_one ((Rawbyte *) rdata + j * size,
			       sdesc->description);

	    break;
	  }
	case XD_UNION:
	case XD_UNION_DYNAMIC_SIZE:
	  desc1 = lispdesc_process_xd_union (desc1, desc, data);
	  if (desc1)
	    goto union_switcheroo;
	  break;

	case XD_FUNCTION_POINTER:
	  {
	    lisp_fn_t *pptr = (lisp_fn_t *) rdata;

 	    *pptr = pdump_reloc_c_func (*pptr);
	    break;
	  }

	case XD_BLOCK_DATA_PTR:
	case XD_MEMORY_DESCRIPTION:
	case XD_SIZED_MEMORY_DESCRIPTION:
	  {
	    void **pptr = (void **) rdata;

            *pptr = pdump_reloc_c_data (*pptr);
	    break;
	  }

	case XD_OPAQUE_PTR_CONVERTIBLE:
	  {
	    pdump_cv_ptr_load_info *p = pdump_loaded_cv_ptr + *(EMACS_INT *)rdata;
	    if (!p->adr)
	      p->adr = desc1->data2.funcs->deconvert(0, pdump_start +
						     p->save_offset, p->size);
	    *(void **)rdata = p->adr;
	    break;
	  }

	case XD_OPAQUE_DATA_CONVERTIBLE:
	  {
	    EMACS_INT dest_offset = (Rawbyte *)rdata - pdump_start;
	    pdump_cv_data_dump_info *p;

	    for(p = pdump_loaded_cv_data; p->dest_offset != dest_offset; p++);

	    desc1->data2.funcs->deconvert(rdata, pdump_start + p->save_offset,
					  p->size);
	    break;
	  }

	default:
	  pdump_unsupported_dump_type (desc1->type, 0);
	}
    }
}

static void
pdump_allocate_offset (pdump_block_list_elt *elt,
		       const struct memory_description *UNUSED (desc))
{
  Bytecount size = elt->count * elt->size;
  elt->save_offset = cur_offset;
  if (size > max_size)
    max_size = size;
  cur_offset += size;
}

/* Write out to global file descriptor PDUMP_OUT the result of an
   external element.  It's just opaque data. */

static void
pdump_dump_cv_data (pdump_cv_data_info *elt)
{
  retry_fwrite (elt->data, elt->size, 1, pdump_out);
}

static void
pdump_dump_cv_ptr (pdump_cv_ptr_info *elt)
{
  retry_fwrite (elt->data, elt->size, 1, pdump_out);
}

static void
pdump_allocate_offset_cv_data (pdump_cv_data_info *elt)
{
  elt->save_offset = cur_offset;
  if (elt->size>max_size)
    max_size = elt->size;
  cur_offset += elt->size;
}

static void
pdump_allocate_offset_cv_ptr (pdump_cv_ptr_info *elt)
{
  elt->save_offset = cur_offset;
  if (elt->size>max_size)
    max_size = elt->size;
  cur_offset += elt->size;
}

/* Traverse through all the heap blocks, once the "register" stage of
   dumping has finished.  To compress space as much as possible, we
   logically sort all blocks by alignment, hitting all blocks with
   alignment == the maximum (which may be 8 bytes, for doubles), then
   all blocks with the next lower alignment (4 bytes), etc.

   Within each alignment we hit

   -- first the Lisp objects, type-by-type

   -- then the heap memory blocks that are not Lisp objects, description-by-
      description -- i.e. all blocks with the same description will be
      placed together

   -- then the "opaque" data objects declared as XD_OPAQUE_DATA_PTR and
      XD_ASCII_STRING.

   The idea is to have as little blank space as possible in the laid-out
   data.

   For each item that we have hit, we process it by calling F, the function
   passed it.  In dumper.c, pdump_scan_by_alignment() is called twice with
   two different functions -- pdump_allocate_offset() in stage 2 to compute
   the offset to each block, and pdump_dump_data() in stage 3 to
   successively write each block to disk.

   It's extremely important that the SAME traversal order gets invoked
   in both stage 2 and 3.
*/

static void
pdump_scan_by_alignment (void (*f)(pdump_block_list_elt *,
				   const struct memory_description *),
			 void (*g)(pdump_cv_data_info *),
			 void (*h)(pdump_cv_ptr_info *))
{
  int align;

  for (align = ALIGNOF (max_align_t); align; align>>=1)
    {
      int i;
      pdump_block_list_elt *elt;

      for (i=0; i<lrecord_type_count; i++)
	if (pdump_object_table[i].align == align)
	  for (elt = pdump_object_table[i].first; elt; elt = elt->next)
	    f (elt, lrecord_implementations_table[i]->description);

      for (i=0; i<pdump_desc_table.count; i++)
	{
	  pdump_desc_list_elt list = pdump_desc_table.list[i];
	  if (list.list.align == align)
	    for (elt = list.list.first; elt; elt = elt->next)
	      f (elt, list.desc);
	}

      for (elt = pdump_opaque_data_list.first; elt; elt = elt->next)
	if (pdump_size_to_align (elt->size) == align)
	  f (elt, 0);

      for (i=0; i < Dynarr_length (pdump_cv_data); i++)
	if (pdump_size_to_align (Dynarr_atp (pdump_cv_data, i)->size) == align)
	  g (Dynarr_atp (pdump_cv_data, i));

      for (i=0; i < Dynarr_length (pdump_cv_ptr); i++)
	if (pdump_size_to_align (Dynarr_atp (pdump_cv_ptr, i)->size) == align)
	  h (Dynarr_atp (pdump_cv_ptr, i));
    }
}

static void
pdump_dump_cv_data_info (void)
{
  int i;
  Elemcount count = Dynarr_length (pdump_cv_data);
  pdump_cv_data_dump_info *data = alloca_array (pdump_cv_data_dump_info, count);
  for (i = 0; i < count; i++)
    {
      data[i].dest_offset = Dynarr_at (pdump_cv_data, i).dest_offset;
      data[i].save_offset = Dynarr_at (pdump_cv_data, i).save_offset;
      data[i].size        = Dynarr_at (pdump_cv_data, i).size;
    }

  PDUMP_ALIGN_OUTPUT (pdump_cv_data_dump_info);
  retry_fwrite (data, sizeof (pdump_cv_data_dump_info), count, pdump_out); 
}

static void
pdump_dump_cv_ptr_info (void)
{
  int i;
  Elemcount count = Dynarr_length (pdump_cv_ptr);
  pdump_cv_ptr_dump_info *data = alloca_array (pdump_cv_ptr_dump_info, count);
  for (i = 0; i < count; i++)
    {
      data[i].save_offset = Dynarr_at (pdump_cv_ptr, i).save_offset;
      data[i].size        = Dynarr_at (pdump_cv_ptr, i).size;
    }

  PDUMP_ALIGN_OUTPUT (pdump_cv_ptr_dump_info);
  retry_fwrite (data, sizeof (pdump_cv_ptr_dump_info), count, pdump_out); 
}

/* Dump out the root block pointers, part of stage 3 (the "WRITE" stage) of
   dumping.  For each pointer we dump out a structure containing the
   location of the pointer and its value, replaced by the appropriate
   offset into the dumped data. */

static void
pdump_dump_root_block_ptrs (void)
{
  int i;
  Elemcount count = Dynarr_length (pdump_root_block_ptrs);
  pdump_static_pointer *data = alloca_array (pdump_static_pointer, count);
  for (i = 0; i < count; i++)
    {
      data[i].address = Dynarr_atp (pdump_root_block_ptrs, i)->ptraddress;
      data[i].value   =
	(void *) pdump_get_block (* data[i].address)->save_offset;
    }
  PDUMP_ALIGN_OUTPUT (pdump_static_pointer);
  retry_fwrite (data, sizeof (pdump_static_pointer), count, pdump_out);
}

/* Dump out the root blocks, part of stage 3 (the "WRITE" stage) of
   dumping.  For each block we dump a structure containing info about the
   block (its location, size and description) and then the block itself,
   with its pointers replaced with offsets into the dump data. */

static void
pdump_dump_root_blocks (void)
{
  int i;
  for (i = 0; i < Dynarr_length (pdump_root_blocks); i++)
    {
      pdump_root_block info = Dynarr_at (pdump_root_blocks, i);
      PDUMP_WRITE_ALIGNED (pdump_root_block, info);

      if (info.desc)
	{
	  /* Copy to temporary buffer */
	  memcpy (pdump_buf, info.blockaddr, info.size);

	  /* Store new offsets into all pointers in block */
	  pdump_store_new_pointer_offsets (1, pdump_buf, info.blockaddr,
					   info.desc, info.size);
	}
      retry_fwrite (info.desc ? pdump_buf : info.blockaddr,
		    info.size, 1, pdump_out);
    }
}

static void
pdump_dump_rtables (void)
{
  int i;
  pdump_block_list_elt *elt;
  pdump_reloc_table rt;

  for (i=0; i<lrecord_type_count; i++)
    {
      elt = pdump_object_table[i].first;
      if (!elt)
	continue;
      rt.desc = lrecord_implementations_table[i]->description;
      rt.count = pdump_object_table[i].count;
      PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
      while (elt)
	{
	  EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset;
	  PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
	  elt = elt->next;
	}
    }

  rt.desc = 0;
  rt.count = 0;
  PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);

  for (i=0; i<pdump_desc_table.count; i++)
    {
      elt = pdump_desc_table.list[i].list.first;
      rt.desc = pdump_desc_table.list[i].desc;
      rt.count = pdump_desc_table.list[i].list.count;
      PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
      while (elt)
	{
	  EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset;
	  int j;
	  for (j=0; j<elt->count; j++)
	    {
	      PDUMP_WRITE_ALIGNED (EMACS_INT, rdata);
	      rdata += elt->size;
	    }
	  elt = elt->next;
	}
    }
  rt.desc = 0;
  rt.count = 0;
  PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt);
}

static void
pdump_dump_root_lisp_objects (void)
{
  Elemcount count = (Dynarr_length (pdump_root_lisp_objects) +
			 Dynarr_length (pdump_weak_object_chains));
  Elemcount i;

  PDUMP_WRITE_ALIGNED (Elemcount, count);
  PDUMP_ALIGN_OUTPUT (pdump_static_Lisp_Object);

  for (i = 0; i < Dynarr_length (pdump_root_lisp_objects); i++)
    {
      pdump_static_Lisp_Object obj;
      obj.address = Dynarr_at (pdump_root_lisp_objects, i);
      obj.value   = * obj.address;

      if (POINTER_TYPE_P (XTYPE (obj.value)))
	obj.value =
	  wrap_pointer_1 ((void *) pdump_get_block (XRECORD_LHEADER
						    (obj.value))->save_offset);

      PDUMP_WRITE (pdump_static_Lisp_Object, obj);
    }

  for (i = 0; i < Dynarr_length (pdump_weak_object_chains); i++)
    {
      pdump_block_list_elt *elt;
      pdump_static_Lisp_Object obj;

      obj.address = Dynarr_at (pdump_weak_object_chains, i);
      obj.value   = * obj.address;

      for (;;)
	{
	  const struct memory_description *desc;
	  int pos;
	  elt = pdump_get_block (XRECORD_LHEADER (obj.value));
	  if (elt)
	    break;
	  desc = XRECORD_LHEADER_IMPLEMENTATION (obj.value)->description;
	  for (pos = 0; desc[pos].type != XD_LO_LINK; pos++)
	    assert (desc[pos].type != XD_END);

	  /* #### Figure out how to handle indirect offsets here. */
	  assert (!XD_IS_INDIRECT (desc[pos].offset));
	  obj.value =
	    * (Lisp_Object *) (desc[pos].offset +
			       (Rawbyte *) (XRECORD_LHEADER (obj.value)));
	}
      obj.value = wrap_pointer_1 ((void *) elt->save_offset);

      PDUMP_WRITE (pdump_static_Lisp_Object, obj);
    }
}

static Lisp_Object
save_pdump_nil_lisp_objects (void)
{
  Elemcount len = Dynarr_length (pdump_nil_lisp_objects);
  Lisp_Object result = make_vector (len, Qnil);
  Elemcount count;

  for (count = 0; count < len; count++)
    {
      XVECTOR_DATA (result)[count] = *Dynarr_at (pdump_nil_lisp_objects, count);
      *Dynarr_at (pdump_nil_lisp_objects, count) = Qnil;
    }

  return result;
}

static Lisp_Object
restore_pdump_nil_lisp_objects (Lisp_Object saved)
{
  Elemcount len = XVECTOR_LENGTH (saved), count;

  for (count = 0; count < len; count++)
    {
      *Dynarr_at (pdump_nil_lisp_objects, count)
	= XVECTOR_DATA (saved)[count];
    }

  return Qnil;
}

static Lisp_Object
save_staticpros (void)
{
  Elemcount staticpros_len =  Dynarr_length (staticpros);
  Elemcount staticpros_nodump_len = Dynarr_length (staticpros_nodump);
  Lisp_Object saved_staticpros = make_vector (staticpros_len, Qnil);
  Lisp_Object saved_staticpros_nodump
    = make_vector (staticpros_nodump_len, Qnil);
  Lisp_Object result = Facons (STORE_VOID_IN_LISP ((void *) staticpros),
			       saved_staticpros,
			       Facons (STORE_VOID_IN_LISP
				       ((void *) staticpros_nodump),
				       saved_staticpros_nodump, Qnil));
  Elemcount count;
  Lisp_Object_ptr_dynarr *original_staticpros = staticpros;

  for (count = 0; count < staticpros_len; count++)
    {
      XVECTOR_DATA (saved_staticpros)[count] = *Dynarr_at (staticpros, count);
    }

  for (count = 0; count < staticpros_nodump_len; count++)
    {
      XVECTOR_DATA (saved_staticpros_nodump)[count]
	= *Dynarr_at (staticpros_nodump, count);
    }


  staticpros_nodump = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
  staticpros = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *);
  Dynarr_add_many (staticpros, Dynarr_begin (original_staticpros),
		   staticpros_len);

  return result;
}

static Lisp_Object
restore_staticpros (Lisp_Object saved)
{
  staticpros
    = (Lisp_Object_ptr_dynarr *) GET_VOID_FROM_LISP (XCAR (XCAR (saved)));
  saved = XCDR (saved);
  staticpros_nodump
    = (Lisp_Object_ptr_dynarr *) GET_VOID_FROM_LISP (XCAR (XCAR (saved)));
  /* Let the saved vectors be garbage-collected. */
  return Qnil;
}

/*########################################################################
  #                             Pdump                                    #
  ########################################################################

  [ben]

  DISCUSSION OF DUMPING:

  The idea of dumping is to record the state of XEmacs in a file, so that
  it can be reloaded later.  This avoids having to reload all of the basic
  Lisp code each time XEmacs is run, which is a rather time-consuming
  process.  (Less so on new machines, but still noticeable.  As an example
  of a program with similar issues but which does not have a dumping
  process and as a result has a slow startup time, consider Adobe Photoshop
  5.0 or Adobe Photoshop Elements 2.0.)

  We don't actually record ALL the state of XEmacs (some of it, for example,
  is dependent on the run-time environment and needs to be initialized
  whenever XEmacs is run), but whatever state we don't record needs to be
  reinitialized every time XEmacs is run.

  The old way of dumping was to make a new executable file with the data
  segment expanded to contain the heap and written out from memory.  This
  is what the unex* files did.  Unfortunately this process is extremely
  system-specific and breaks easily with OS changes.

  Another simple, more portable trick, the "static heap" method, involves
  replacing the allocator with our own allocator which allocates all space
  out of a very large array declared in our data segment until we run out,
  then uses the underlying malloc() to start allocating on the heap.  If we
  ensure that the large array is big enough to hold all data allocated
  during the dump stage, then all of the data we need to save is in the
  data segment, and it's easy to calculate the location and size of the
  data segment we want to save (we don't want to record and reinitialize
  the data segment of library functions) by using appropriately declared
  variables in the first and last file linked.  This method is known as the
  "static heap" method, and was used by the non-pdump version of the dumper
  under Cygwin, under VMS, and in Win-Emacs.

  The "static heap" method worked well in practice for some years.
  Nonetheless, a more complex method of dumping was written by Olivier
  Galibert, which required that structural descriptions of all data allocated
  in the heap be provided and the roots of all pointers into the heap be noted
  through function calls to the pdump API.  This way, all the heap data can be
  traversed and written out to a file, and then reloaded at run-time and the
  pointers relocated to point at the new location of the loaded data.  This is
  the "pdump" method used in this file.

  There are two advantages of "pdump" over the "static heap":

  (1) It doesn't require any tricks to calculate the beginning and end of
      the data segment, or even that the XEmacs section of the data segment
      be contiguous. (It's not clear whether this is an issue in practice.)
  (2) It can handle an OS that does not always load the static data segment at
      a predictable location.  The "static heap" method by its nature needed
      the data segment to stay in the same place from invocation to
      invocation, since it simply dumped out memory and reloaded it, without
      any pointer relocation. This is a major issue from about 2015 onwards,
      since most operating systems now use address space layout randomization,
      ASLR.


  DISCUSSION OF PDUMP WORKINGS:

  See man/internals/internals.texi for more information.

  NOTE that we have two kinds of memory to handle: memory on the heap
  (i.e. allocated through malloc()) or the like, and static memory in the
  data segment of the program, i.e. stuff declared as global or static.
  All heap memory needs to be written out to the dump file and reproduced
  (i.e. reloaded and any necessary relocations performed).  Data-segment
  memory that is not statically initialized (i.e. through declarations in
  the C code) needs either to be written out and reloaded, or
  reinitialized.  The former leaks the memory in the dump file that was used to
  store the pre-pdump values. In addition, any pointers in data-segment memory
  to heap memory must be written out, reloaded and relocated.

  Three basic steps to dumping out:

  (1) "REGISTER":
      Starting with all sources of relocatable memory (currently this means
      all data-segment pointers to heap memory -- see above about pointers
      to data-segment memory), recursively traverse the tree of pointers
      and "register" (make a note of) every memory block seen.

  (2) "LAYOUT":
      Go through all of the registered blocks and compute the location of
      each one in the dump data (i.e. the "offset" that will be added to
      the address corresponding to start of the loaded-in data to get the
      new pointer referring to this block).  The blocks will be laid out
      sequentially according to the order we traverse them.  Also note the
      maximum-sized block for use in step 3.

  (3) "WRITE":
      After writing some header stuff, go through all of the registered
      blocks and write out each one to the dump file.  Note that we are
      simply writing out the blocks sequentially as we see them, and our
      traversal path is identical to that in step 2, so blocks will end up
      at the locations computed for them.  In order to write out a block,
      first copy it to a temporary location (hence the maximum-block-size
      computation in the previous step), then for each relocatable pointer
      in the block, write in its place the offset to the heap block in the
      dump data.  When the dump data is loaded, the address of the
      beginning of the dump data will be added to the offset in each
      pointer, and thence become accurate.

  --ben
*/

void
pdump (void)
{
  int i;
  int none;
  pdump_header header;
  int speccount = specpdl_depth ();
  struct gcpro gcpro1, gcpro2;

  in_pdump = 1;

  pdump_object_table = xnew_array (pdump_block_list, lrecord_type_count);
  pdump_alert_undump_object = xnew_array (int, lrecord_type_count);

  assert (ALIGNOF (max_align_t) <= pdump_align_table[0]);

  for (i = 0; i < countof (pdump_align_table); i++)
    if (pdump_align_table[i] > ALIGNOF (max_align_t))
      pdump_align_table[i] = ALIGNOF (max_align_t);

  flush_all_buffer_local_cache ();

  /* It is unwise for actual development, but often very helpful from the
     debugger, to call Lisp during the dump process. Do the book-keeping for
     Lisp state properly for the following. */
  record_unwind_protect (restore_pdump_nil_lisp_objects,
			 save_pdump_nil_lisp_objects ());

  record_unwind_protect (restore_staticpros, save_staticpros ());

  /* Entries here are just fixnums pointing to Qt, appropriate for the hash
     table to be weak. Can't do that, however, since it adds an entry to 
     Vall_weak_hash_tables that shouldn't be dumped. */
  Vstaticpros_hash = make_lisp_hash_table (Dynarr_length (staticpros),
				           HASH_TABLE_NON_WEAK, Qeq);
  Vstaticpros_nodump_block_offsets
    = make_lisp_hash_table (Dynarr_length (staticpros) / 5,
                            HASH_TABLE_NON_WEAK, Qeq);

  GCPRO2 (Vstaticpros_hash, Vstaticpros_nodump_block_offsets);

  {
    Elemcount ii;
    for (ii = 0; ii < Dynarr_length (staticpros); ii++)
      {
        Fputhash (STORE_VOID_IN_LISP ((void *) Dynarr_at (staticpros, ii)),
                  Qt, Vstaticpros_hash);
      }
  }

  pdump_hash = xnew_array_and_zero (pdump_block_list_elt *, PDUMP_HASHSIZE);

  for (i = 0; i<lrecord_type_count; i++)
    {
      pdump_object_table[i].first = 0;
      pdump_object_table[i].align = ALIGNOF (max_align_t);
      pdump_object_table[i].count = 0;
      pdump_alert_undump_object[i] = 0;
    }
  pdump_desc_table.count = 0;
  pdump_desc_table.size = -1;

  pdump_opaque_data_list.first = 0;
  pdump_opaque_data_list.align = ALIGNOF (max_align_t);
  pdump_opaque_data_list.count = 0;
  pdump_depth = 0;

  pdump_cv_data = Dynarr_new2 (pdump_cv_data_info_dynarr, pdump_cv_data_info);
  pdump_cv_ptr  = Dynarr_new2 (pdump_cv_ptr_info_dynarr,  pdump_cv_ptr_info);

  /* (I) The "register" stage: Note all heap memory blocks to be relocated
     */

  /* Try various roots of accessibility: */

  /* (1) Lisp objects passed to dump_add_root_lisp_object(). */
  for (i = 0; i < Dynarr_length (pdump_root_lisp_objects); i++)
    pdump_register_object (* Dynarr_at (pdump_root_lisp_objects, i));

  /* (2) Register out the data-segment pointer variables to heap blocks. */
  for (i = 0; i < Dynarr_length (pdump_root_block_ptrs); i++)
    {
      pdump_root_block_ptr info = Dynarr_at (pdump_root_block_ptrs, i);
      pdump_register_block (*(info.ptraddress), info.desc->size,
			    info.desc->description, 1);
    }

  /* (3) Register out the data-segment blocks, maybe with pointers to heap
     blocks */
  pdump_min_shuffle_depth = 2; /* Pointers to Lisp_Objects within the
                                  data-segment blocks themselves are not in
                                  the heap and need to stay in STATICPROS,
                                  don't process them in
                                  shuffle_staticpros_if_needed(). */
  for (i = 0; i < Dynarr_length (pdump_root_blocks); i++)
    {
      pdump_root_block *info = Dynarr_atp (pdump_root_blocks, i);
      if (info->desc)
	{
	  /* Size may have been given as 0 meaning "compute later".
	     Compute now and update.  If no DESC, size must always be
	     correct as there is no other way of computing it. */
	  info->size = lispdesc_block_size_1 (info->blockaddr, info->size,
					      info->desc);
	  pdump_register_block_contents (info->blockaddr, info->size,
					 info->desc, 1);
	}
    }

  /* (4) Register STATICPROS and STATICPROS_NODUMP specially, they need to be
     processed after everything else has been examined. */
  pdump_min_shuffle_depth = 5; /* Avoid shuffling the entries in STATICPROS,
                                  STATICPROS_NODUMP themselves, a depth of 5
                                  takes us beyond the processing of
                                  XD_BLOCK_DATA_PTR and XD_BLOCK_PTR. */
  pdump_register_block (staticpros, staticpros_description.size,
			staticpros_description.description, 1);

  pdump_register_block (staticpros_nodump,
			staticpros_nodump_description.size,
			staticpros_nodump_description.description, 1);

  /* The following smells fishy but isn't. It's needed needed to have
     staticpros and staticpros_nodump (the pointers themselves) restored
     correctly, and since the calls are after pdump_root_block_ptrs have been
     processed, pdump_register_block() won't be called on them twice.  */
  dump_add_root_block_ptr (&staticpros, &staticpros_description);
  dump_add_root_block_ptr (&staticpros_nodump, &staticpros_nodump_description);

  none = 1;
  for (i = 0; i < lrecord_type_count; i++)
    if (pdump_alert_undump_object[i])
      {
	if (none)
	  stderr_out ("Undumpable types list :\n");
	none = 0;
	stderr_out ("  - %s (%d)\n",
                    LRECORD_IMPLEMENTATION_IBYTE_NAME
                    (lrecord_implementations_table[i]),
		    pdump_alert_undump_object[i]);
      }
  if (!none)
    {
      in_pdump = 0;
      UNGCPRO;
      return;
    }

  /* (II) The "layout" stage: Compute the offsets and max-size */

  /* (1) Determine header size */
  memcpy (header.signature, PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN);
  header.id = dump_id;
  header.reloc_address = 0;
  header.Fcons_address = (EMACS_UINT) (&Fcons);
  header.lisp_object_description_address
    = (EMACS_UINT) (&lisp_object_description);
  header.nb_root_block_ptrs = Dynarr_length (pdump_root_block_ptrs);
  header.nb_root_blocks = Dynarr_length (pdump_root_blocks);
  header.nb_cv_data = Dynarr_length (pdump_cv_data);
  header.nb_cv_ptr =  Dynarr_length (pdump_cv_ptr);

  cur_offset = MAX_ALIGN_SIZE (sizeof (header));
  max_size = 0;

  /* (2) Traverse all heap blocks and compute their offsets; keep track
         of maximum block size seen */
  pdump_scan_by_alignment (pdump_allocate_offset,
			   pdump_allocate_offset_cv_data,
			   pdump_allocate_offset_cv_ptr);
  cur_offset = MAX_ALIGN_SIZE (cur_offset);
  header.stab_offset = cur_offset;

  /* (3) Update maximum size based on root blocks and root block pointers. */
  for (i = 0; i < Dynarr_length (pdump_root_blocks); i++)
    {
      pdump_root_block info = Dynarr_at (pdump_root_blocks, i);

      /* If no DESC, no relocation needed and we copy directly instead of
	 into a temp buffer. */
      if (info.desc)
	{
	  if (info.size > max_size)
	    max_size = info.size;
	}
    }

  /* (III) The "write "stage: Dump out the data, storing the offsets in
           place of pointers whenever we write out memory blocks */

  pdump_buf = xmalloc (max_size);

  /* EMACS_DUMP_FILE_NAME must be entirely ASCII to make this Mule-safe.
     We can't use qxe_open() because on Windows that creates a new Lisp string
     (see Vmswindows_read_link_hash) which pdump doesn't know about, and it
     chokes. */
  ASSERT_ASCTEXT_ASCII (EMACS_DUMP_FILE_NAME);
  pdump_fd = open (EMACS_DUMP_FILE_NAME,
		   O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY, 0666);
  if (pdump_fd < 0)
    report_file_error ("Unable to open dump file",
		       build_ascstring (EMACS_DUMP_FILE_NAME));
  pdump_out = fdopen (pdump_fd, "w");
  if (pdump_out == NULL)
    report_file_error ("Unable to open dump file for writing",
		       build_ascstring (EMACS_DUMP_FILE_NAME));

  retry_fwrite (&header, sizeof (header), 1, pdump_out);
  PDUMP_ALIGN_OUTPUT (max_align_t);

  for (i = 0; i < Dynarr_length (pdump_cv_data); i++)
    {
      pdump_cv_data_info *elt = Dynarr_atp (pdump_cv_data, i);
      elt->dest_offset =
	pdump_get_block (elt->object)->save_offset + elt->offset;
    }

  for (i = 0; i < Dynarr_length (pdump_cv_ptr); i++)
    Dynarr_at (pdump_cv_ptr, i).index = i;

  pdump_scan_by_alignment (pdump_dump_data, pdump_dump_cv_data, pdump_dump_cv_ptr);

  for (i = 0; i < Dynarr_length (pdump_cv_data); i++)
    {
      pdump_cv_data_info *elt = Dynarr_atp (pdump_cv_data, i);
      if(elt->fcts->convert_free)
	elt->fcts->convert_free(elt->object, elt->data, elt->size);
    }

  for (i = 0; i < Dynarr_length (pdump_cv_ptr); i++)
    {
      pdump_cv_ptr_info *elt = Dynarr_atp (pdump_cv_ptr, i);
      if(elt->fcts->convert_free)
	elt->fcts->convert_free(elt->object, elt->data, elt->size);
    }

  if (FSEEK (pdump_out, header.stab_offset, SEEK_SET) == -1)
    {
      report_file_error ("Unable to fseek dump file",
			 build_ascstring (EMACS_DUMP_FILE_NAME));
    }

  pdump_dump_cv_data_info ();
  pdump_dump_cv_ptr_info ();
  pdump_dump_root_block_ptrs ();
  pdump_dump_root_blocks ();
  pdump_dump_rtables ();
  pdump_dump_root_lisp_objects ();

  retry_fclose (pdump_out);
  /* pdump_fd is already closed by the preceding call to fclose.
  retry_close (pdump_fd); */

  free (pdump_buf);

  free (pdump_hash);

  UNGCPRO;
  unbind_to (speccount);

  in_pdump = 0;
}

static int
pdump_load_check (void)
{
  return (!memcmp (((pdump_header *) pdump_start)->signature,
		   PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN)
	  && ((pdump_header *)pdump_start)->id == dump_id);
}

/*----------------------------------------------------------------------*/
/*			Reading the dump file				*/
/*----------------------------------------------------------------------*/
static int
pdump_load_finish (void)
{
  int i;
  Rawbyte *p;
  EMACS_INT count;
  pdump_header *header = (pdump_header *) pdump_start;

  pdump_end = pdump_start + pdump_length;

  dump_delta = ((EMACS_INT) pdump_start) - header->reloc_address;
  c_code_delta = ((EMACS_UINT) &Fcons) - header->Fcons_address;
  c_data_delta = ((EMACS_UINT) &lisp_object_description)
    - header->lisp_object_description_address;

  p = pdump_start + header->stab_offset;

  /* Get the cv_data array */
  p = (Rawbyte *) ALIGN_PTR (p, pdump_cv_data_dump_info);
  pdump_loaded_cv_data = (pdump_cv_data_dump_info *)p;
  p += header->nb_cv_data*sizeof(pdump_cv_data_dump_info);

  /* Build the cv_ptr array */
  p = (Rawbyte *) ALIGN_PTR (p, pdump_cv_ptr_dump_info);
  pdump_loaded_cv_ptr =
    alloca_array (pdump_cv_ptr_load_info, header->nb_cv_ptr);
  for (i = 0; i < header->nb_cv_ptr; i++)
    {
      pdump_cv_ptr_dump_info info = PDUMP_READ (p, pdump_cv_ptr_dump_info);
      pdump_loaded_cv_ptr[i].save_offset = info.save_offset;
      pdump_loaded_cv_ptr[i].size        = info.size;
      pdump_loaded_cv_ptr[i].adr         = 0;
    }


  /* Put back the pdump_root_block_ptrs */
  p = (Rawbyte *) ALIGN_PTR (p, pdump_static_pointer);
  for (i = 0; i < header->nb_root_block_ptrs; i++)
    {
      pdump_static_pointer ptr = PDUMP_READ (p, pdump_static_pointer);
      ptr.address = (void **) pdump_reloc_c_data ((void *) ptr.address);
      ptr.value = pdump_reloc_lisp_data (ptr.value);
      (* ptr.address) = ptr.value;
    }

  /* Put back the pdump_root_blocks and relocate */
  for (i = 0; i < header->nb_root_blocks; i++)
    {
      pdump_root_block info = PDUMP_READ_ALIGNED (p, pdump_root_block);
      info.blockaddr = pdump_reloc_c_data (info.blockaddr);
      info.desc
        = (const struct memory_description *) pdump_reloc_c_data (info.desc);
      memcpy ((void *) info.blockaddr, p, info.size);
      if (info.desc)
	pdump_reloc_one ((void *) info.blockaddr, info.desc);
      p += info.size;
    }

  /* Relocate the heap objects */
  pdump_rt_list = p;
  count = 2;
  for (;;)
    {
      pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
      p = (Rawbyte *) ALIGN_PTR (p, Rawbyte *);
      if (rt.desc)
	{
	  Rawbyte **reloc = (Rawbyte **) p;
          rt.desc
            = (const struct memory_description *) pdump_reloc_c_data (rt.desc);
	  for (i = 0; i < rt.count; i++)
	    {
	      reloc[i]
                = (Rawbyte *) pdump_reloc_lisp_data ((void *) reloc[i]);
	      pdump_reloc_one (reloc[i], rt.desc);
	    }
	  p += rt.count * sizeof (Rawbyte *);
	}
      else if (!(--count))
	  break;
    }

  /* Put the pdump_root_lisp_objects variables in place */
  i = PDUMP_READ_ALIGNED (p, Elemcount);
  p = (Rawbyte *) ALIGN_PTR (p, pdump_static_Lisp_Object);
  while (i--)
    {
      pdump_static_Lisp_Object obj = PDUMP_READ (p, pdump_static_Lisp_Object);

      obj.address = (Lisp_Object *) pdump_reloc_c_data ((void *) obj.address);
      obj.value = pdump_reloc_lisp_object (obj.value);

      (* obj.address) = obj.value;
    }

  /* Final cleanups */
  /*   reorganize hash tables */
  p = pdump_rt_list;
  for (;;)
    {
      pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table);
      p = (Rawbyte *) ALIGN_PTR (p, Lisp_Object);
      if (!rt.desc)
	break;
      rt.desc
        = (const struct memory_description *) pdump_reloc_c_data (rt.desc);
      if (rt.desc == hash_table_description)
	{
	  for (i = 0; i < rt.count; i++)
	    pdump_reorganize_hash_table (PDUMP_READ (p, Lisp_Object));
	  break;
	}
      else
	p += sizeof (Lisp_Object) * rt.count;
    }

  return 1;
}



#ifdef WIN32_NATIVE
static int
pdump_resource_get (void)
{
  HRSRC hRes;			/* Handle to dump resource */
  HRSRC hResLoad;		/* Handle to loaded dump resource */
  DWORD previous_protection;

  /* See Q126630 which describes how Windows NT and 95 trap writes to
     resource sections and duplicate the page to allow the write to proceed.
     It also describes how to make the resource section read/write (and hence
     private to each process).  Doing this avoids the exceptions and related
     overhead, but causes the resource section to be private to each process
     that is running XEmacs.  Since the resource section contains little
     other than the dumped data, which should be private to each process, we
     make the whole resource section read/write so we don't have to copy it. */

  hRes = FindResourceA (NULL, MAKEINTRESOURCE (101), "DUMP");
  if (hRes == NULL)
    return 0;

  /* Found it, use the data in the resource */
  hResLoad = (HRSRC) LoadResource (NULL, hRes);
  if (hResLoad == NULL)
    return 0;

  pdump_start = (Rawbyte *) LockResource (hResLoad);
  if (pdump_start == NULL)
    return 0;

  pdump_length = SizeofResource (NULL, hRes);
  if (pdump_length <= (Bytecount) sizeof (pdump_header))
    {
      pdump_start = 0;
      return 0;
    }

  if (!VirtualProtect(pdump_start, pdump_length, PAGE_READWRITE,
		      &previous_protection))
    {
      return 0;
    }

  return 1;
}

#elif defined (DUMP_IN_EXEC)

static int
pdump_resource_get (void)
{
  pdump_start = dumped_data_get ();
  pdump_length = dumped_data_size ();

  return 1;
}

#else

#ifndef HAVE_SNPRINTF

static int
snprintf (char *output, size_t size, const char *format, ...)
{
  va_list vargs;
  int retval;

  va_start (vargs, format);
  retval = vsprintf (output, format, vargs);
  va_end (vargs);

  /* Systems where this is needed are getting fewer and fewer in number. The
     big one was mswindows, where this code is not relevant. */
  if (retval >= (int) size)
    {
      fprintf (stderr, "%s",
	       EMACS_PROGNAME ": buffer overflow finding dumpfile.\n");
      exit (3);
    }

  return retval;
}

#endif

static void (*pdump_free) (void);

static void
pdump_file_free (void)
{
  xfree (pdump_start);
}

#ifdef HAVE_MMAP
static void
pdump_file_unmap (void)
{
  munmap (pdump_start, pdump_length);
}
#endif

static int
pdump_file_get (const Wexttext *path)
{
  int fd = wext_retry_open (path, O_RDONLY | OPEN_BINARY);
  if (fd < 0)
    return 0;

  pdump_length = lseek (fd, 0, SEEK_END);
  if (pdump_length < (Bytecount) sizeof (pdump_header))
    {
      retry_close (fd);
      return 0;
    }

  if (lseek (fd, 0, SEEK_SET) == -1)
    {
      retry_close (fd);
      return 0;
    }

#ifdef HAVE_MMAP
/* Unix 98 requires that sys/mman.h define MAP_FAILED,
   but many earlier implementations don't. */
# ifndef MAP_FAILED
#  define MAP_FAILED ((void *) -1L)
# endif
  pdump_start =
    (Rawbyte *) mmap (0, pdump_length, PROT_READ|PROT_WRITE, MAP_PRIVATE,
			  fd, 0);
  if (pdump_start != (Rawbyte *) MAP_FAILED)
    {
      pdump_free = pdump_file_unmap;
      retry_close (fd);
      return 1;
    }
#endif /* HAVE_MMAP */

  pdump_start = xnew_array (Rawbyte, pdump_length);
  pdump_free = pdump_file_free;
  retry_read (fd, pdump_start, pdump_length);

  retry_close (fd);
  return 1;
}

/* Longest relative path we will encounter. */
#define DUMP_SLACK sizeof ("../lib/" EMACS_PROGNAME "-" EMACS_VERSION	\
                           "/" EMACS_CONFIGURATION "/" EMACS_PROGNAME   \
			   "-12345678.dmp")
static int
pdump_file_try (const Extbyte *dirname, const Extbyte *basename)
{
  Bytecount dirlen = strlen (dirname);
  Bytecount file_try_size = dirlen + strlen (basename) + DUMP_SLACK;
  Extbyte *file_try_path = alloca_extbytes (file_try_size);
  Bytecount res;

#if defined (PATH_EXEC) || defined (PATH_PREFIX)
  const Extbyte sepstring[] = { DEFAULT_DIRECTORY_SEP, '\0' };
#endif

  /* We look for the dump file in a strictly limited number of places (one if
     -no-configured-paths was supplied, two otherwise), with a view to avoiding
     security holes (see LD_LIBRARY_PATH and the associated setuid restrictions
     for motivation; ld.so(8) on Linux). The dump file contains function
     pointers and executable Lisp code and should be regarded as equivalent to
     executable code. We don't look for it at all if DUMP_IN_EXEC is defined.

     If it is in the directory XEmacs was invocated from, we can be reasonably
     sure that anyone who can write to it can write to XEmacs anyway.

     If it is in the expected PATH_EXEC directory, that is a sign it was
     intentionally installed there. Similar reasoning, if it is in the expected
     subdirectory under PATH_PREFIX, or if it is an expected relative directory
     to the XEmacs binary; but only check one of these options depending on
     what was set at compile time. */

  res = snprintf (file_try_path, file_try_size, "%s%s.dmp", dirname,
		  basename);
  if (res < file_try_size && pdump_file_get (file_try_path))
    {
      if (pdump_load_check ())
	return 1;
      pdump_free ();
    }

  if (inhibit_configured_paths)
    {
      return 0;
    }

  /* This reflects --with-archlibdir on the configure line. */
#ifdef PATH_EXEC
  res = snprintf (file_try_path, file_try_size,
		  "%s%s%s-%08x.dmp", 
		  PATH_EXEC,
		  IS_DIRECTORY_SEP (PATH_EXEC[sizeof (PATH_EXEC) - 2])
		  ? "" : sepstring, basename, dump_id);

  if (res < file_try_size && pdump_file_get (file_try_path))
    {
      if (pdump_load_check ())
	return 1;
      pdump_free ();
    }
  return 0;
#elif defined (PATH_PREFIX)
  res = snprintf (file_try_path, file_try_size,
		  "%s%slib%c%s-%s%c%s%c%s-%08x.dmp",
		  PATH_PREFIX, 
		  IS_DIRECTORY_SEP (PATH_PREFIX[sizeof (PATH_PREFIX) - 2])
		  ? "" : sepstring,
		  DEFAULT_DIRECTORY_SEP,
		  EMACS_PROGNAME, EMACS_VERSION, 
		  DEFAULT_DIRECTORY_SEP,
		  EMACS_CONFIGURATION,
		  DEFAULT_DIRECTORY_SEP,
		  EMACS_PROGNAME,
		  dump_id);
  if (res < file_try_size && pdump_file_get (file_try_path))
    {
      if (pdump_load_check ())
	return 1;
      pdump_free ();
    }
  return 0;
#else
  {
    const Extbyte *p = dirname + strlen (dirname);
    Extbyte *superdirname = alloca_extbytes (p - dirname + 1);
    struct stat statbuf;

    /* pdump_load() should have supplied us a filename with a trailing
       directory separator. */
    assert (IS_DIRECTORY_SEP (p[-1]));
    p--;

    do
      {
	while (p != dirname && !IS_DIRECTORY_SEP (p[-1]))
	  p--;

	if (p != dirname)
	  {
	    /* See pdump_load() for the reasoning with this. */
	    memcpy (superdirname, dirname, p - dirname);
	    superdirname[p - dirname] = '\0';
	    if (stat (superdirname, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
	      {
		break;
	      }
	    else
	      {
		/* Not a directory; theoretically a shift_jis or GBK
		   basename. Move back past the directory separator, try
		   again. If we fail on repeat, give up. */
		p--;
	      }
	  }
      }
    while (p != dirname);

    if (p == dirname)
      {
	/* Cannot determine containing directory from path. May be something
	   like ./; ./xemacs is a very reasonable way to invoke XEmacs.
	   Attempt with ../, which is strictly non-portable, but the systems
	   that don't support it are very thin on the ground as of 2024. */
	res = snprintf (file_try_path, file_try_size,
			"%s..%clib%c%s-%s%c%s%c%s-%08x.dmp",
			dirname, DEFAULT_DIRECTORY_SEP, DEFAULT_DIRECTORY_SEP,
			EMACS_PROGNAME, EMACS_VERSION, DEFAULT_DIRECTORY_SEP,
			EMACS_CONFIGURATION, DEFAULT_DIRECTORY_SEP,
			EMACS_PROGNAME, dump_id);
      }
    else
      {
	/* Use a full path name. */
	res = snprintf (file_try_path, file_try_size,
			"%slib%c%s-%s%c%s%c%s-%08x.dmp",
			superdirname, DEFAULT_DIRECTORY_SEP,
			EMACS_PROGNAME, EMACS_VERSION, DEFAULT_DIRECTORY_SEP,
			EMACS_CONFIGURATION, DEFAULT_DIRECTORY_SEP,
			EMACS_PROGNAME, dump_id);
      }

    if (res < file_try_size && pdump_file_get (file_try_path))
      {
	if (pdump_load_check ())
	  return 1;
	pdump_free ();
      }
  }
#endif /* !PATH_PREFIX */

  return 0;
}

#endif /* !DUMP_IN_EXEC */

#ifdef DUMP_IN_EXEC

Boolint
pdump_load (const Extbyte * UNUSED (argv0))
{
  if (pdump_resource_get () && pdump_load_check ())
    {
      pdump_load_finish ();
      in_pdump = 0;
      return 1;
    }

  in_pdump = 0;
  return 0;
}

#else

Boolint
pdump_load (const Extbyte *argv0)
{
  const Extbyte *dir, *p, *path;
  Extbyte *dirname;
  Bytecount pathlen, baselen;
  struct stat statbuf;

  /* This function and pdump_file_try() deal with Extbytes; this is
     appropriate, since we do not yet have any significant language processing
     available. */
  in_pdump = 1;
  dir = argv0;
  if (dir[0] == '-')
    {
      /* XEmacs as a login shell. */
      dir = getenv ("SHELL"); /* not egetenv -- not yet initialized and we
				 want external-format data */
      if (!dir)
	{
	  /* No, not actually a login shell, or possibly not a POSIX-oriented
	     system. Use argv[0]. */
	  dir = argv0;
	}
    }

  p = dir + strlen (dir);
  do
    {
      while (p != dir && !IS_DIRECTORY_SEP (p[-1]))
	p--;

      if (p != dir)
	{
	  /* The POSIX rule is that if a directory separator is anywhere in
	     ARGV0, then $PATH should not be examined, the file is either an
	     absolute path or relative to the current directory.

	     However, if LC_CTYPE reflects either shift_jis or GBK, a directory
	     separator may be the second byte of a multibyte sequence and thus
	     it is not to be interpreted as a directory separator. This is
	     going to be rare on normal Unix-ish systems (the usual file system
	     encoding for Japanese is EUC-JP, which does not have this issue),
	     but theoretically might happen with other systems.

	     Windows native, the main platform likely to use shift_jis, does
	     not use this code, since it has good support for inline dump
	     files; DUMP_IN_EXEC is preferred in general, given less likelihood
	     of security holes, but on non-Windows platforms it is only
	     reliable with binary patching of the built executable (our old
	     approach, deprecated with codesigning) or with reproducible builds
	     (common but not universal).

	     We shouldn't setlocale() and use mbsrtowcs() and friends instead,
	     this is too early and will throw off the locale handling later in
	     startup.

	     Instead, check if the presumptive directory exists and is a
	     directory; if it doesn't, look for it in $PATH. */
	  dirname = alloca_extbytes (p - dir + 1);

	  memcpy (dirname, dir, p - dir);
	  dirname[p - dir] = '\0';
	  if (stat (dirname, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
	    {
	      if (pdump_file_try (dirname, p))
		{
		  pdump_load_finish ();
		  in_pdump = 0;
		  return 1;
		}
	      else
		{
		  in_pdump = 0;
		  return 0;
		}
	    }
	  else
	    {
	      /* Not a directory; theoretically a shift_jis or GBK
		 basename. Move back past the directory separator, try
		 again. If we fail on repeat, examine PATH. */
	      p--;
	    }
	}
    }
  while (p != dir);
      
  path = getenv ("PATH"); /* not egetenv, not yet initialised. */

  if (!path)
    {
      /* PATH not set, argv[0] does not include a slash, fail. */
      in_pdump = 0;
      return 0;
    }

  pathlen = strlen (path);
  baselen = strlen (p);
  dirname = alloca_extbytes (pathlen + baselen + 2);

  while (path)
    {
      Bytecount dirlen;
      const Extbyte *dirend = strchr (path, SEPCHAR);

      if (dirend)
	{
	  if (dirend == path)
	    {
	      dirname[0] = '.';
	      dirlen = 1;
	    }
	  else
	    {
	      dirlen = dirend - path;
	      memcpy (dirname, path, dirlen);
	    }
          path = dirend + 1; /* Advance past SEPCHAR. */
	}
      else
	{
	  dirlen = strlen (path);
	  memcpy (dirname, path, dirlen);
          path = NULL;
	}

      if (!IS_DIRECTORY_SEP (dirname[dirlen - 1]))
	{
	  dirname[dirlen] = DEFAULT_DIRECTORY_SEP;
	  dirlen++;
	}

      memcpy (dirname + dirlen, p, baselen);
      dirname[dirlen + baselen] = '\0';

      /* Check the *filename* for exec access here. */
      if (access (dirname, X_OK) == 0 && stat (dirname, &statbuf) == 0
	  && !S_ISDIR (statbuf.st_mode))
	{
	  dirname[dirlen] = '\0';
	  if (pdump_file_try (dirname, p))
	    {
	      pdump_load_finish ();
	      in_pdump = 0;
	      return 1;
	    }

	  /* Failed with this candidate, keep trying the other directories. */
	}
    }

  in_pdump = 0;
  return 0;
}

#endif /* !DUMP_IN_EXEC */

/* dumper.c ends here */
