aboutsummaryrefslogtreecommitdiff
path: root/cddl/contrib/opensolaris/cmd
diff options
context:
space:
mode:
authorJohn Birrell <jb@FreeBSD.org>2008-04-25 23:33:18 +0000
committerJohn Birrell <jb@FreeBSD.org>2008-04-25 23:33:18 +0000
commitd876124d6ae9d56da5b4ff4c6015efd1d0c9222a (patch)
treee6cfdef2a8b9488b25517d80411d703ccb765e26 /cddl/contrib/opensolaris/cmd
parent2056b4037cdba1def880066fab7cca98df5b4d86 (diff)
parent2de84d2572206157cf33d1fb75463eeb42ae8e42 (diff)
downloadsrc-d876124d6ae9d56da5b4ff4c6015efd1d0c9222a.tar.gz
src-d876124d6ae9d56da5b4ff4c6015efd1d0c9222a.zip
This commit was generated by cvs2svn to compensate for changes in r178525,
which included commits to RCS files with non-trunk default branches.
Notes
Notes: svn path=/head/; revision=178526
Diffstat (limited to 'cddl/contrib/opensolaris/cmd')
-rw-r--r--cddl/contrib/opensolaris/cmd/mdb/tools/common/die.c87
-rw-r--r--cddl/contrib/opensolaris/cmd/mdb/tools/common/util.h51
-rw-r--r--cddl/contrib/opensolaris/cmd/sgs/include/_string_table.h126
-rw-r--r--cddl/contrib/opensolaris/cmd/sgs/include/alist.h280
-rw-r--r--cddl/contrib/opensolaris/cmd/sgs/include/debug.h1017
-rw-r--r--cddl/contrib/opensolaris/cmd/sgs/include/sgs.h296
-rw-r--r--cddl/contrib/opensolaris/cmd/sgs/include/string_table.h63
-rw-r--r--cddl/contrib/opensolaris/cmd/sgs/messages/sgs.ident62
-rw-r--r--cddl/contrib/opensolaris/cmd/sgs/tools/common/findprime.c56
-rw-r--r--cddl/contrib/opensolaris/cmd/sgs/tools/common/sgsmsg.c1210
-rw-r--r--cddl/contrib/opensolaris/cmd/sgs/tools/common/string_table.c685
11 files changed, 3933 insertions, 0 deletions
diff --git a/cddl/contrib/opensolaris/cmd/mdb/tools/common/die.c b/cddl/contrib/opensolaris/cmd/mdb/tools/common/die.c
new file mode 100644
index 000000000000..602969e1abb3
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/mdb/tools/common/die.c
@@ -0,0 +1,87 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+#include <elf.h>
+
+#include <util.h>
+
+void
+die(char *format, ...)
+{
+ va_list ap;
+ int err = errno;
+#if !defined(sun)
+ const char *progname = getprogname();
+#endif
+
+ (void) fprintf(stderr, "%s: ", progname);
+
+ va_start(ap, format);
+ /* LINTED - variable format specifier */
+ (void) vfprintf(stderr, format, ap);
+ va_end(ap);
+
+ if (format[strlen(format) - 1] != '\n')
+ (void) fprintf(stderr, ": %s\n", strerror(err));
+
+#if defined(__FreeBSD__)
+ exit(0);
+#else
+ exit(1);
+#endif
+}
+
+void
+elfdie(char *format, ...)
+{
+ va_list ap;
+#if !defined(sun)
+ const char *progname = getprogname();
+#endif
+
+ (void) fprintf(stderr, "%s: ", progname);
+
+ va_start(ap, format);
+ /* LINTED - variable format specifier */
+ (void) vfprintf(stderr, format, ap);
+ va_end(ap);
+
+ if (format[strlen(format) - 1] != '\n')
+ (void) fprintf(stderr, ": %s\n", elf_errmsg(elf_errno()));
+
+#if defined(__FreeBSD__)
+ exit(0);
+#else
+ exit(1);
+#endif
+}
diff --git a/cddl/contrib/opensolaris/cmd/mdb/tools/common/util.h b/cddl/contrib/opensolaris/cmd/mdb/tools/common/util.h
new file mode 100644
index 000000000000..a0932ada0d57
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/mdb/tools/common/util.h
@@ -0,0 +1,51 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#ifndef _UTIL_H
+#define _UTIL_H
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <libelf.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int findelfsecidx(Elf *, char *);
+
+extern void die(char *, ...);
+extern void elfdie(char *, ...);
+
+#if defined(sun)
+extern const char *progname;
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _UTIL_H */
diff --git a/cddl/contrib/opensolaris/cmd/sgs/include/_string_table.h b/cddl/contrib/opensolaris/cmd/sgs/include/_string_table.h
new file mode 100644
index 000000000000..2d2963f9dab7
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/sgs/include/_string_table.h
@@ -0,0 +1,126 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#ifndef __STRING_TABLE_DOT_H
+#define __STRING_TABLE_DOT_H
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <sys/types.h>
+#include <sys/avl.h>
+#include <string_table.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * A string is represented in a string table using two values: length, and
+ * value. Grouping all the strings of a given length together allows for
+ * efficient matching of tail strings, as each input string value is hashed.
+ * Each string table uses a 2-level AVL tree of AVL trees to represent this
+ * organization.
+ *
+ * The outer (main) AVL tree contains LenNode structures. The search key for
+ * nodes on this main tree is the string length. Each such node represents
+ * all strings of a given length, and all strings of that length are found
+ * within.
+ *
+ * The strings within each LenNode are maintained using a secondary AVL tree
+ * of StrNode structures. The search key for this inner tree is the string
+ * itself. The strings are maintained in lexical order.
+ */
+typedef struct {
+ avl_node_t sn_avlnode; /* AVL book-keeping */
+ const char *sn_str; /* string */
+ size_t sn_refcnt; /* reference count */
+} StrNode;
+
+typedef struct {
+ avl_node_t ln_avlnode; /* AVL book-keeping */
+ avl_tree_t *ln_strtree; /* AVL tree of associated strings */
+ size_t ln_strlen; /* length of associated strings */
+} LenNode;
+
+/*
+ * Define a master string data item. Other strings may be suffixes of this
+ * string. The final string table will consist of the master string values,
+ * laid end to end, with the other strings referencing their tails.
+ */
+typedef struct str_master Str_master;
+
+struct str_master {
+ const char *sm_str; /* pointer to master string */
+ Str_master *sm_next; /* used for tracking master strings */
+ size_t sm_strlen; /* length of master string */
+ uint_t sm_hashval; /* hashval of master string */
+ size_t sm_stroff; /* offset into destination strtab */
+};
+
+/*
+ * Define a hash data item. This item represents an individual string that has
+ * been input into the String hash table. The string may either be a suffix of
+ * another string, or a master string.
+ */
+typedef struct str_hash Str_hash;
+
+struct str_hash {
+ size_t hi_strlen; /* string length */
+ size_t hi_refcnt; /* number of references to str */
+ uint_t hi_hashval; /* hash for string */
+ Str_master *hi_mstr; /* pointer to master string */
+ Str_hash *hi_next; /* next entry in hash bucket */
+};
+
+/*
+ * Controlling data structure for a String Table.
+ */
+struct str_tbl {
+ avl_tree_t *st_lentree; /* AVL tree of string lengths */
+ char *st_strbuf; /* string buffer */
+ Str_hash **st_hashbcks; /* hash buckets */
+ Str_master *st_mstrlist; /* list of all master strings */
+ size_t st_fullstrsize; /* uncompressed table size */
+ size_t st_nextoff; /* next available string */
+ size_t st_strsize; /* compressed size */
+ size_t st_strcnt; /* number of strings */
+ uint_t st_hbckcnt; /* number of buckets in */
+ /* hashlist */
+ uint_t st_flags;
+};
+
+#define FLG_STTAB_COMPRESS 0x01 /* compressed string table */
+#define FLG_STTAB_COOKED 0x02 /* offset has been assigned */
+
+/*
+ * Starting value for use with string hashing functions inside of string_table.c
+ */
+#define HASHSEED 5381
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __STRING_TABLE_DOT_H */
diff --git a/cddl/contrib/opensolaris/cmd/sgs/include/alist.h b/cddl/contrib/opensolaris/cmd/sgs/include/alist.h
new file mode 100644
index 000000000000..c27160bd35cc
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/sgs/include/alist.h
@@ -0,0 +1,280 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Define an Alist, a list maintained as a reallocable array, and a for() loop
+ * macro to generalize its traversal. Note that the array can be reallocated
+ * as it is being traversed, thus the offset of each element is recomputed from
+ * the start of the structure.
+ */
+
+#ifndef _ALIST_H
+#define _ALIST_H
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <sys/types.h>
+#if defined(sun)
+#include <sys/machelf.h>
+#else
+#include <sys/elf.h>
+#endif
+
+/*
+ * An Alist implements array lists. The functionality is similar to
+ * that of a linked list. However, an Alist is represented by a single
+ * contigious allocation of memory. The head of the memory is a header
+ * that contains control information for the list. Following the header
+ * is an array used to hold the user data. In the type definitions that
+ * follow, we define these as an array with a single element, but when
+ * we allocate the memory, we actually allocate the amount of memory needed.
+ *
+ * There are two "flavors" of array list:
+ *
+ * Alist - Contain arbitrary data, usually structs.
+ * APlist - Contain pointers to data allocated elsewhere.
+ *
+ * This differentiation is useful, because pointer lists are heavily
+ * used, and support a slightly different set of operations that are
+ * unique to their purpose.
+ *
+ * Array lists are initially represented by a NULL pointer. The memory
+ * for the list is only allocated if an item is inserted. This is very
+ * efficient for data structures that may or may not be needed for a
+ * given linker operation --- you only pay for what you use. In addition:
+ *
+ * - Array lists grow as needed (memory is reallocated as necessary)
+ * - Data is kept contiguously (no unused holes in between elements)
+ * at the beginning of the data area. This locality has
+ * good cache behavior, as access to adjacent items are
+ * highly likely to be in the same page of memory.
+ * - Insert/Delete operations at the end of the list are very
+ * efficient. However, insert/delete operations elsewhere
+ * will cause a relatively expensive overlapped memory
+ * copy of the data following the insert/delete location.
+ * - As with any generic memory alloctor (i.e. malloc()/free()),
+ * array lists are not type safe for the data they contain.
+ * Data is managed as (void *) pointers to data of a given
+ * length, so the Alist module cannot prevent the caller from
+ * inserting/extracting the wrong type of data. The caller
+ * must guard against this.
+ * - To free an array list, simply call the standard free() function
+ * on the list pointer.
+ */
+
+
+
+/*
+ * Aliste is used to represent list indexes, offsets, and sizes.
+ */
+typedef size_t Aliste;
+
+
+
+/*
+ * Alist is used to hold non-pointer items --- usually structs:
+ * - There must be an even number of Aliste fields before the
+ * al_data field. This ensures that al_data will have
+ * an alignment of 8, no matter whether sizeof(Aliste)
+ * is 4 or 8. That means that al_data will have sufficient
+ * alignment for any use, just like memory allocated via
+ * malloc().
+ * - al_nitems and al_next are redundant, in that they are
+ * directly related:
+ * al_next = al_nitems * al_size
+ * We do this to make ALIST_TRAVERSE_BYOFFSET maximally
+ * efficient. This doesn't waste space, because of the
+ * requirement to have an even # of Alist fields (above).
+ *
+ * Note that Alists allow the data to be referenced by 0 based array
+ * index, or by their byte offset from the start of the Alist memory
+ * allocation. The index form is preferred for most use, as it is simpler.
+ * However, by-offset access is used by rtld link maps, and this ability
+ * is convenient in that case.
+ */
+typedef struct {
+ Aliste al_arritems; /* # of items in al_data allocation */
+ Aliste al_nitems; /* # items (index of next avail item) */
+ Aliste al_next; /* offset of next available al_data[] */
+ Aliste al_size; /* size of each al_data[] item */
+ void *al_data[1]; /* data (can grow) */
+} Alist;
+
+/*
+ * APlist is a variant of Alist that contains pointers. There are several
+ * benefits to this special type:
+ * - API is simpler
+ * - Pointers are used directly, instead of requiring a
+ * pointer-to-pointer double indirection.
+ * - The implementation is slightly more efficient.
+ * - Operations that make particular sense for pointers
+ * can be supported without confusing the API for the
+ * regular Alists.
+ */
+typedef struct {
+ Aliste apl_arritems; /* # of items in apl_data allocation */
+ Aliste apl_nitems; /* # items (index of next avail item) */
+ void *apl_data[1]; /* data area: (arrcnt * size) bytes */
+} APlist;
+
+
+/*
+ * The ALIST_OFF_DATA and APLIST_OFF_DATA macros give the byte offset
+ * from the start of an array list to the first byte of the data area
+ * used to hold user data. The same trick used by the standard offsetof()
+ * macro is used.
+ */
+#define ALIST_OFF_DATA ((size_t)(((Alist *)0)->al_data))
+#define APLIST_OFF_DATA ((size_t)(((APlist *)0)->apl_data))
+
+
+/*
+ * The TRAVERSE macros are intended to be used within a for(), and
+ * cause the resulting loop to iterate over each item in the loop,
+ * in order.
+ * ALIST_TRAVERSE: Traverse over the items in an Alist,
+ * using the zero based item array index to refer to
+ * each item.
+ * ALIST_TRAVERSE_BY_OFFSET: Traverse over the items in an
+ * Alist using the byte offset from the head of the
+ * Alist pointer to refer to each item. It should be noted
+ * that the first such offset is given by ALIST_OFF_DATA,
+ * and as such, there will never be a 0 offset. Some code
+ * uses this fact to treat 0 as a reserved value with
+ * special meaning.
+ *
+ * By-offset access is convenient for some parts of
+ * rtld, where a value of 0 is used to indicate an
+ * uninitialized link map control.
+ *
+ * APLIST_TRAVERSE: Traverse over the pointers in an APlist, using
+ * the zero based item array index to refer to each pointer.
+ */
+
+/*
+ * Within the loop:
+ *
+ * LIST - Pointer to Alist structure for list
+ * IDX - The current item index
+ * OFF - The current item offset
+ * DATA - Pointer to item
+ */
+#define ALIST_TRAVERSE(LIST, IDX, DATA) \
+ (IDX) = 0, \
+ ((LIST) != NULL) && ((DATA) = (void *)(LIST)->al_data); \
+ \
+ ((LIST) != NULL) && ((IDX) < (LIST)->al_nitems); \
+ \
+ (IDX)++, \
+ (DATA) = (void *) (((LIST)->al_size * (IDX)) + (char *)(LIST)->al_data)
+
+#define ALIST_TRAVERSE_BY_OFFSET(LIST, OFF, DATA) \
+ (((LIST) != NULL) && ((OFF) = ALIST_OFF_DATA) && \
+ (((DATA) = (void *)((char *)(LIST) + (OFF))))); \
+ \
+ (((LIST) != NULL) && ((OFF) < (LIST)->al_next)); \
+ \
+ (((OFF) += ((LIST)->al_size)), \
+ ((DATA) = (void *)((char *)(LIST) + (OFF))))
+
+/*
+ * Within the loop:
+ *
+ * LIST - Pointer to APlist structure for list
+ * IDX - The current item index
+ * PTR - item value
+ *
+ * Note that this macro is designed to ensure that PTR retains the
+ * value of the final pointer in the list after exiting the for loop,
+ * and to avoid dereferencing an out of range address. This is done by
+ * doing the dereference in the middle expression, using the comma
+ * operator to ensure that a NULL pointer won't stop the loop.
+ */
+#define APLIST_TRAVERSE(LIST, IDX, PTR) \
+ (IDX) = 0; \
+ \
+ ((LIST) != NULL) && ((IDX) < (LIST)->apl_nitems) && \
+ (((PTR) = ((LIST)->apl_data)[IDX]), 1); \
+ \
+ (IDX)++
+
+
+/*
+ * Possible values returned by aplist_test()
+ */
+typedef enum {
+ ALE_ALLOCFAIL = 0, /* Memory allocation error */
+ ALE_EXISTS = 1, /* alist entry already exists */
+ ALE_NOTFND = 2, /* item not found and insert not required */
+ ALE_CREATE = 3 /* alist entry created */
+} aplist_test_t;
+
+
+/*
+ * Access to an Alist item by index or offset. This is needed because the
+ * size of an item in an Alist is not known by the C compiler, and we
+ * have to do the indexing arithmetic explicitly.
+ *
+ * For an APlist, index the apl_data field directly --- No macro is needed.
+ */
+#define alist_item(_lp, _idx) \
+ ((void *)(ALIST_OFF_DATA + ((_idx) * (_lp)->al_size) + (char *)(_lp)))
+#define alist_item_by_offset(_lp, _off) \
+ ((void *)((_off) + (char *)(_lp)))
+
+/*
+ * # of items currently found in a list. These macros handle the case
+ * where the list has not been allocated yet.
+ */
+#define alist_nitems(_lp) (((_lp) == NULL) ? 0 : (_lp)->al_nitems)
+#define aplist_nitems(_lp) (((_lp) == NULL) ? 0 : (_lp)->apl_nitems)
+
+
+extern void *alist_append(Alist **, const void *, size_t, Aliste);
+extern void alist_delete(Alist *, Aliste *);
+extern void alist_delete_by_offset(Alist *, Aliste *);
+extern void *alist_insert(Alist **, const void *, size_t,
+ Aliste, Aliste);
+extern void *alist_insert_by_offset(Alist **, const void *, size_t,
+ Aliste, Aliste);
+extern void alist_reset(Alist *);
+
+
+extern void *aplist_append(APlist **, const void *, Aliste);
+extern void aplist_delete(APlist *, Aliste *);
+extern int aplist_delete_value(APlist *, const void *);
+extern void *aplist_insert(APlist **, const void *,
+ Aliste, Aliste idx);
+extern void aplist_reset(APlist *);
+extern aplist_test_t aplist_test(APlist **, const void *, Aliste);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ALIST_H */
diff --git a/cddl/contrib/opensolaris/cmd/sgs/include/debug.h b/cddl/contrib/opensolaris/cmd/sgs/include/debug.h
new file mode 100644
index 000000000000..0a42f8dd9e9b
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/sgs/include/debug.h
@@ -0,0 +1,1017 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#ifndef _DEBUG_H
+#define _DEBUG_H
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+/*
+ * Global include file for lddbg debugging.
+ *
+ * ld(1) and ld.so.1(1) carry out all diagnostic debugging calls via lazy
+ * loading the library liblddbg.so. Thus debugging is always enabled. The
+ * utility elfdump(1) is explicitly dependent upon this library. There are two
+ * categories of routines defined in this library:
+ *
+ * o Debugging routines that have specific linker knowledge, and test the
+ * class of debugging allowable before proceeding, start with the `Dbg_'
+ * prefix.
+ *
+ * o Lower level routines that provide generic ELF structure interpretation
+ * start with the `Elf_' prefix. These latter routines are the only
+ * routines used by the elfdump(1) utility.
+ */
+#include <sgs.h>
+#include <libld.h>
+#include <rtld.h>
+#include <gelf.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Define Dbg_*() interface flags. These flags direct the debugging routine to
+ * generate different diagnostics, thus the strings themselves are maintained
+ * in the debugging library.
+ */
+#define DBG_SUP_ENVIRON 1
+#define DBG_SUP_CMDLINE 2
+#define DBG_SUP_DEFAULT 3
+
+#define DBG_CONF_IGNORE 1 /* configuration processing errors */
+#define DBG_CONF_VERSION 2
+#define DBG_CONF_PRCFAIL 3
+#define DBG_CONF_CORRUPT 4
+#define DBG_CONF_ABIMISMATCH 5
+
+#define DBG_ORDER_INFO_RANGE 1 /* sh_link out of range */
+#define DBG_ORDER_INFO_ORDER 2 /* sh_info also ordered */
+#define DBG_ORDER_LINK_OUTRANGE 3 /* sh_link out of range */
+#define DBG_ORDER_FLAGS 4 /* sh_flags do not match */
+#define DBG_ORDER_CYCLIC 5 /* sh_link cyclic */
+#define DBG_ORDER_LINK_ERROR 6 /* sh_link (one) has an error */
+
+#define DBG_INIT_SORT 1 /* calling init from sorted order */
+#define DBG_INIT_PEND 2 /* calling pending init */
+#define DBG_INIT_DYN 3 /* dynamically triggered init */
+#define DBG_INIT_DONE 4 /* init completed */
+
+#define DBG_DLSYM_DEF 0
+#define DBG_DLSYM_NEXT 1
+#define DBG_DLSYM_DEFAULT 2
+#define DBG_DLSYM_SELF 3
+#define DBG_DLSYM_PROBE 4
+#define DBG_DLSYM_SINGLETON 5
+
+#define DBG_DLCLOSE_NULL 0
+#define DBG_DLCLOSE_IGNORE 1
+#define DBG_DLCLOSE_RESCAN 2
+
+#define DBG_WAIT_INIT 1
+#define DBG_WAIT_FINI 2
+#define DBG_WAIT_SYMBOL 3
+
+#define DBG_SYM_REDUCE_GLOBAL 1 /* reporting global symbols to local */
+#define DBG_SYM_REDUCE_RETAIN 2 /* reporting non reduced local syms */
+
+/*
+ * Group handle operations - passed to Dbg_file_hdl_title(). Indicate why
+ * handle dependencies are being manipulated.
+ */
+#define DBG_HDL_CREATE 0 /* handle creation */
+#define DBG_HDL_ADD 1 /* addition to existing handle */
+#define DBG_HDL_DELETE 2 /* deletion from a handle */
+#define DBG_HDL_ORPHAN 3 /* handle being moved to orphan list */
+#define DBG_HDL_REINST 4 /* handle reinstated from orphan list */
+
+/*
+ * Group handle dependency operations - passed to Dbg_file_hdl_action().
+ * Identify the dependencies that are associated with a handle.
+ */
+#define DBG_DEP_ADD 0 /* dependency added */
+#define DBG_DEP_UPDATE 1 /* dependency updated */
+#define DBG_DEP_DELETE 2 /* dependency deleted */
+#define DBG_DEP_REMOVE 3 /* dependency removed from handle */
+#define DBG_DEP_REMAIN 4 /* dependency must remain on handle */
+#define DBG_DEP_ORPHAN 5 /* dependency must remain an orphan */
+#define DBG_DEP_REINST 6 /* dependency reinstated from orphan */
+
+/*
+ * Binding information, indicating the result of a symbol binding. Can also
+ * indicate the reference as being EXTERN or PARENT. Binding information is
+ * used to augment diagnostic binding information (which in turn can be used by
+ * lari(1)), and to enable ldd(1) -p processing.
+ */
+#define DBG_BINFO_FOUND 0x0001 /* information regarding binding */
+#define DBG_BINFO_DIRECT 0x0002 /* bound directly */
+#define DBG_BINFO_COPYREF 0x0004 /* bound to copy relocated reference */
+#define DBG_BINFO_FILTEE 0x0008 /* bound to filtee */
+#define DBG_BINFO_INTERPOSE 0x0010 /* bound to an identified interposer */
+#define DBG_BINFO_PLTADDR 0x0020 /* bound to executables undefined plt */
+#define DBG_BINFO_MSK 0x0fff
+
+#define DBG_BINFO_REF_EXTERN 0x1000 /* reference to EXTERN */
+#define DBG_BINFO_REF_PARENT 0x2000 /* reference to PARENT */
+#define DBG_BINFO_REF_MSK 0xf000
+
+
+#define DBG_CAP_INITIAL 0
+#define DBG_CAP_IGNORE 1
+#define DBG_CAP_OLD 2
+#define DBG_CAP_NEW 3
+#define DBG_CAP_RESOLVED 4
+
+#define DBG_REL_START 1
+#define DBG_REL_FINISH 2
+#define DBG_REL_NONE 3
+
+#define DBG_NL_STD 0 /* newline controllers - standard and */
+#define DBG_NL_FRC 2 /* forced. */
+
+#define DBG_BNDREJ_NODIR 0 /* bind rejected, direct to nodirect */
+#define DBG_BNDREJ_SINGLE 1 /* bind rejected, singleton without */
+ /* default search model */
+#define DBG_BNDREJ_NUM DBG_BNDREJ_SINGLE
+
+/*
+ * Define a debug descriptor, and a user macro that inspects the descriptor as
+ * a means of triggering a class of diagnostic output.
+ */
+typedef struct {
+ uint_t d_class; /* debugging classes */
+ uint_t d_extra; /* extra information for classes */
+ APlist *d_list; /* associated strings */
+} Dbg_desc;
+
+extern Dbg_desc *dbg_desc;
+
+#define DBG_ENABLED (dbg_desc->d_class)
+#define DBG_CALL(func) if (DBG_ENABLED) func
+
+/*
+ * Most debugging tokens are interpreted within liblddbg, and thus any flags
+ * within d_class are only meaningful to this library. The following flags
+ * extend the d_class diagnostic, and are maintained in d_extra. These flags
+ * may be interpreted by the debugging library itself or from the callers
+ * dbg_print() routine.
+ */
+#define DBG_E_DETAIL 0x0001 /* add detail to a class */
+#define DBG_E_LONG 0x0002 /* use long names (ie. no truncation) */
+
+#define DBG_E_STDNL 0x0010 /* standard newline indicator */
+
+#define DBG_E_SNAME 0x0100 /* prepend simple name (ld only) */
+#define DBG_E_FNAME 0x0200 /* prepend full name (ld only) */
+#define DBG_E_CLASS 0x0400 /* prepend ELF class (ld only) */
+#define DBG_E_LMID 0x0800 /* prepend link-map id (ld.so.1 only) */
+#define DBG_E_DEMANGLE 0x1000 /* demangle symbol names */
+
+#define DBG_NOTDETAIL() !(dbg_desc->d_extra & DBG_E_DETAIL)
+#define DBG_NOTLONG() !(dbg_desc->d_extra & DBG_E_LONG)
+
+#define DBG_ISSNAME() (dbg_desc->d_extra & DBG_E_SNAME)
+#define DBG_ISFNAME() (dbg_desc->d_extra & DBG_E_FNAME)
+#define DBG_ISCLASS() (dbg_desc->d_extra & DBG_E_CLASS)
+#define DBG_ISLMID() (dbg_desc->d_extra & DBG_E_LMID)
+#define DBG_ISDEMANGLE() \
+ (dbg_desc->d_extra & DBG_E_DEMANGLE)
+
+/*
+ * Print routine, this must be supplied by the application. The initial
+ * argument may provide a link-map list to associate with the format statement
+ * that follows.
+ */
+/* PRINTFLIKE2 */
+extern void dbg_print(Lm_list *, const char *, ...);
+
+extern uintptr_t Dbg_setup(const char *, Dbg_desc *);
+
+/*
+ * Establish ELF32 and ELF64 class Dbg_*() interfaces.
+ */
+#if defined(_ELF64)
+
+#define Dbg_demangle_name Dbg64_demangle_name
+
+#define Dbg_bind_global Dbg64_bind_global
+#define Dbg_bind_plt_summary Dbg64_bind_plt_summary
+#define Dbg_bind_pltpad_from Dbg64_bind_pltpad_from
+#define Dbg_bind_pltpad_to Dbg64_bind_pltpad_to
+#define Dbg_bind_reject Dbg64_bind_reject
+#define Dbg_bind_weak Dbg64_bind_weak
+
+#define Dbg_cap_val_hw1 Dbg64_cap_val_hw1
+#define Dbg_cap_hw_candidate Dbg64_cap_hw_candidate
+#define Dbg_cap_hw_filter Dbg64_cap_hw_filter
+#define Dbg_cap_mapfile Dbg64_cap_mapfile
+#define Dbg_cap_sec_entry Dbg64_cap_sec_entry
+#define Dbg_cap_sec_title Dbg64_cap_sec_title
+
+#define Dbg_ent_entry Dbg64_ent_entry
+#define Dbg_ent_print Dbg64_ent_print
+
+#define Dbg_file_analyze Dbg64_file_analyze
+#define Dbg_file_aout Dbg64_file_aout
+#define Dbg_file_ar Dbg64_file_ar
+#define Dbg_file_ar_rescan Dbg64_file_ar_rescan
+#define Dbg_file_bind_entry Dbg64_file_bind_entry
+#define Dbg_file_bindings Dbg64_file_bindings
+#define Dbg_file_cleanup Dbg64_file_cleanup
+#define Dbg_file_cntl Dbg64_file_cntl
+#define Dbg_file_config_dis Dbg64_file_config_dis
+#define Dbg_file_config_obj Dbg64_file_config_obj
+#define Dbg_file_del_rescan Dbg64_file_del_rescan
+#define Dbg_file_delete Dbg64_file_delete
+#define Dbg_file_dlclose Dbg64_file_dlclose
+#define Dbg_file_dldump Dbg64_file_dldump
+#define Dbg_file_dlopen Dbg64_file_dlopen
+#define Dbg_file_elf Dbg64_file_elf
+#define Dbg_file_filtee Dbg64_file_filtee
+#define Dbg_file_filter Dbg64_file_filter
+#define Dbg_file_fixname Dbg64_file_fixname
+#define Dbg_file_generic Dbg64_file_generic
+#define Dbg_file_hdl_action Dbg64_file_hdl_action
+#define Dbg_file_hdl_collect Dbg64_file_hdl_collect
+#define Dbg_file_hdl_title Dbg64_file_hdl_title
+#define Dbg_file_lazyload Dbg64_file_lazyload
+#define Dbg_file_ldso Dbg64_file_ldso
+#define Dbg_file_mode_promote Dbg64_file_mode_promote
+#define Dbg_file_modified Dbg64_file_modified
+#define Dbg_file_needed Dbg64_file_needed
+#define Dbg_file_output Dbg64_file_output
+#define Dbg_file_preload Dbg64_file_preload
+#define Dbg_file_prot Dbg64_file_prot
+#define Dbg_file_rejected Dbg64_file_rejected
+#define Dbg_file_reuse Dbg64_file_reuse
+#define Dbg_file_skip Dbg64_file_skip
+
+#define Dbg_got_display Dbg64_got_display
+
+#define Dbg_libs_audit Dbg64_libs_audit
+#define Dbg_libs_find Dbg64_libs_find
+#define Dbg_libs_found Dbg64_libs_found
+#define Dbg_libs_ignore Dbg64_libs_ignore
+#define Dbg_libs_init Dbg64_libs_init
+#define Dbg_libs_l Dbg64_libs_l
+#define Dbg_libs_path Dbg64_libs_path
+#define Dbg_libs_req Dbg64_libs_req
+#define Dbg_libs_update Dbg64_libs_update
+#define Dbg_libs_yp Dbg64_libs_yp
+#define Dbg_libs_ylu Dbg64_libs_ylu
+
+#define Dbg_map_dash Dbg64_map_dash
+#define Dbg_map_ent Dbg64_map_ent
+#define Dbg_map_parse Dbg64_map_parse
+#define Dbg_map_pipe Dbg64_map_pipe
+#define Dbg_map_seg Dbg64_map_seg
+#define Dbg_map_set_atsign Dbg64_map_set_atsign
+#define Dbg_map_set_equal Dbg64_map_set_equal
+#define Dbg_map_size_new Dbg64_map_size_new
+#define Dbg_map_size_old Dbg64_map_size_old
+#define Dbg_map_sort_fini Dbg64_map_sort_fini
+#define Dbg_map_sort_orig Dbg64_map_sort_orig
+#define Dbg_map_symbol Dbg64_map_symbol
+#define Dbg_map_version Dbg64_map_version
+
+#define Dbg_move_adjexpandreloc Dbg64_move_adjexpandreloc
+#define Dbg_move_adjmovereloc Dbg64_move_adjmovereloc
+#define Dbg_move_data Dbg64_move_data
+#define Dbg_move_entry1 Dbg64_move_entry1
+#define Dbg_move_entry2 Dbg64_move_entry2
+#define Dbg_move_expand Dbg64_move_expand
+#define Dbg_move_input Dbg64_move_input
+#define Dbg_move_outmove Dbg64_move_outmove
+#define Dbg_move_outsctadj Dbg64_move_outsctadj
+#define Dbg_move_parexpn Dbg64_move_parexpn
+
+#define Dbg_reloc_apply_reg Dbg64_reloc_apply_reg
+#define Dbg_reloc_apply_val Dbg64_reloc_apply_val
+#define Dbg_reloc_ars_entry Dbg64_reloc_ars_entry
+#define Dbg_reloc_copy Dbg64_reloc_copy
+#define Dbg_reloc_discard Dbg64_reloc_discard
+#define Dbg_reloc_doact Dbg64_reloc_doact
+#define Dbg_reloc_doact_title Dbg64_reloc_doact_title
+#define Dbg_reloc_dooutrel Dbg64_reloc_dooutrel
+#define Dbg_reloc_entry Dbg64_reloc_entry
+#define Dbg_reloc_error Dbg64_reloc_error
+#define Dbg_reloc_generate Dbg64_reloc_generate
+#define Dbg_reloc_in Dbg64_reloc_in
+#define Dbg_reloc_ors_entry Dbg64_reloc_ors_entry
+#define Dbg_reloc_out Dbg64_reloc_out
+#define Dbg_reloc_proc Dbg64_reloc_proc
+#define Dbg_reloc_run Dbg64_reloc_run
+#define Dbg_reloc_transition Dbg64_reloc_transition
+#define Dbg_reloc_sloppycomdat Dbg64_reloc_sloppycomdat
+
+#define Dbg_sec_added Dbg64_sec_added
+#define Dbg_sec_created Dbg64_sec_created
+#define Dbg_sec_discarded Dbg64_sec_discarded
+#define Dbg_sec_genstr_compress Dbg64_sec_genstr_compress
+#define Dbg_sec_group Dbg64_sec_group
+#define Dbg_sec_in Dbg64_sec_in
+#define Dbg_sec_order_error Dbg64_sec_order_error
+#define Dbg_sec_order_list Dbg64_sec_order_list
+#define Dbg_sec_strtab Dbg64_sec_strtab
+#define Dbg_sec_unsup_strmerge Dbg64_sec_unsup_strmerge
+
+#define Dbg_seg_desc_entry Dbg64_seg_desc_entry
+#define Dbg_seg_entry Dbg64_seg_entry
+#define Dbg_seg_list Dbg64_seg_list
+#define Dbg_seg_os Dbg64_seg_os
+#define Dbg_seg_title Dbg64_seg_title
+
+#define Dbg_shdr_modified Dbg64_shdr_modified
+
+#define Dbg_statistics_ar Dbg64_statistics_ar
+#define Dbg_statistics_ld Dbg64_statistics_ld
+
+#define Dbg_support_action Dbg64_support_action
+#define Dbg_support_load Dbg64_support_load
+#define Dbg_support_req Dbg64_support_req
+
+#define Dbg_syminfo_entry Dbg64_syminfo_entry
+#define Dbg_syminfo_title Dbg64_syminfo_title
+
+#define Dbg_syms_ar_checking Dbg64_syms_ar_checking
+#define Dbg_syms_ar_entry Dbg64_syms_ar_entry
+#define Dbg_syms_ar_resolve Dbg64_syms_ar_resolve
+#define Dbg_syms_ar_title Dbg64_syms_ar_title
+#define Dbg_syms_created Dbg64_syms_created
+#define Dbg_syms_discarded Dbg64_syms_discarded
+#define Dbg_syms_dlsym Dbg64_syms_dlsym
+#define Dbg_syms_dup_sort_addr Dbg64_syms_dup_sort_addr
+#define Dbg_syms_entered Dbg64_syms_entered
+#define Dbg_syms_entry Dbg64_syms_entry
+#define Dbg_syms_global Dbg64_syms_global
+#define Dbg_syms_ignore Dbg64_syms_ignore
+#define Dbg_syms_ignore_gnuver Dbg64_syms_ignore_gnuver
+#define Dbg_syms_lazy_rescan Dbg64_syms_lazy_rescan
+#define Dbg_syms_lookup Dbg64_syms_lookup
+#define Dbg_syms_new Dbg64_syms_new
+#define Dbg_syms_old Dbg64_syms_old
+#define Dbg_syms_process Dbg64_syms_process
+#define Dbg_syms_reduce Dbg64_syms_reduce
+#define Dbg_syms_reloc Dbg64_syms_reloc
+#define Dbg_syms_resolved Dbg64_syms_resolved
+#define Dbg_syms_resolving Dbg64_syms_resolving
+#define Dbg_syms_sec_entry Dbg64_syms_sec_entry
+#define Dbg_syms_sec_title Dbg64_syms_sec_title
+#define Dbg_syms_spec_title Dbg64_syms_spec_title
+#define Dbg_syms_updated Dbg64_syms_updated
+#define Dbg_syms_up_title Dbg64_syms_up_title
+
+#define Dbg_util_broadcast Dbg64_util_broadcast
+#define Dbg_util_call_array Dbg64_util_call_array
+#define Dbg_util_call_fini Dbg64_util_call_fini
+#define Dbg_util_call_init Dbg64_util_call_init
+#define Dbg_util_call_main Dbg64_util_call_main
+#define Dbg_util_collect Dbg64_util_collect
+#define Dbg_util_dbnotify Dbg64_util_dbnotify
+#define Dbg_util_edge_in Dbg64_util_edge_in
+#define Dbg_util_edge_out Dbg64_util_edge_out
+#define Dbg_util_intoolate Dbg64_util_intoolate
+#define Dbg_util_lcinterface Dbg64_util_lcinterface
+#define Dbg_util_nl Dbg64_util_nl
+#define Dbg_util_no_init Dbg64_util_no_init
+#define Dbg_util_scc_entry Dbg64_util_scc_entry
+#define Dbg_util_scc_title Dbg64_util_scc_title
+#define Dbg_util_str Dbg64_util_str
+#define Dbg_util_wait Dbg64_util_wait
+
+#define Dbg_unused_file Dbg64_unused_file
+#define Dbg_unused_lcinterface Dbg64_unused_lcinterface
+#define Dbg_unused_path Dbg64_unused_path
+#define Dbg_unused_sec Dbg64_unused_sec
+#define Dbg_unused_unref Dbg64_unused_unref
+
+#define Dbg_ver_avail_entry Dbg64_ver_avail_entry
+#define Dbg_ver_avail_title Dbg64_ver_avail_title
+#define Dbg_ver_def_title Dbg64_ver_def_title
+#define Dbg_ver_desc_entry Dbg64_ver_desc_entry
+#define Dbg_ver_need_entry Dbg64_ver_need_entry
+#define Dbg_ver_need_title Dbg64_ver_need_title
+#define Dbg_ver_nointerface Dbg64_ver_nointerface
+#define Dbg_ver_symbol Dbg64_ver_symbol
+
+#else
+
+#define Dbg_demangle_name Dbg32_demangle_name
+
+#define Dbg_bind_global Dbg32_bind_global
+#define Dbg_bind_plt_summary Dbg32_bind_plt_summary
+#define Dbg_bind_reject Dbg32_bind_reject
+#define Dbg_bind_weak Dbg32_bind_weak
+
+#define Dbg_cap_val_hw1 Dbg32_cap_val_hw1
+#define Dbg_cap_hw_candidate Dbg32_cap_hw_candidate
+#define Dbg_cap_hw_filter Dbg32_cap_hw_filter
+#define Dbg_cap_mapfile Dbg32_cap_mapfile
+#define Dbg_cap_sec_entry Dbg32_cap_sec_entry
+#define Dbg_cap_sec_title Dbg32_cap_sec_title
+
+#define Dbg_ent_entry Dbg32_ent_entry
+#define Dbg_ent_print Dbg32_ent_print
+
+#define Dbg_file_analyze Dbg32_file_analyze
+#define Dbg_file_aout Dbg32_file_aout
+#define Dbg_file_ar Dbg32_file_ar
+#define Dbg_file_ar_rescan Dbg32_file_ar_rescan
+#define Dbg_file_bind_entry Dbg32_file_bind_entry
+#define Dbg_file_bindings Dbg32_file_bindings
+#define Dbg_file_cleanup Dbg32_file_cleanup
+#define Dbg_file_cntl Dbg32_file_cntl
+#define Dbg_file_config_dis Dbg32_file_config_dis
+#define Dbg_file_config_obj Dbg32_file_config_obj
+#define Dbg_file_del_rescan Dbg32_file_del_rescan
+#define Dbg_file_delete Dbg32_file_delete
+#define Dbg_file_dlclose Dbg32_file_dlclose
+#define Dbg_file_dldump Dbg32_file_dldump
+#define Dbg_file_dlopen Dbg32_file_dlopen
+#define Dbg_file_elf Dbg32_file_elf
+#define Dbg_file_filtee Dbg32_file_filtee
+#define Dbg_file_filter Dbg32_file_filter
+#define Dbg_file_fixname Dbg32_file_fixname
+#define Dbg_file_generic Dbg32_file_generic
+#define Dbg_file_hdl_action Dbg32_file_hdl_action
+#define Dbg_file_hdl_collect Dbg32_file_hdl_collect
+#define Dbg_file_hdl_title Dbg32_file_hdl_title
+#define Dbg_file_lazyload Dbg32_file_lazyload
+#define Dbg_file_ldso Dbg32_file_ldso
+#define Dbg_file_mode_promote Dbg32_file_mode_promote
+#define Dbg_file_modified Dbg32_file_modified
+#define Dbg_file_needed Dbg32_file_needed
+#define Dbg_file_output Dbg32_file_output
+#define Dbg_file_preload Dbg32_file_preload
+#define Dbg_file_prot Dbg32_file_prot
+#define Dbg_file_rejected Dbg32_file_rejected
+#define Dbg_file_reuse Dbg32_file_reuse
+#define Dbg_file_skip Dbg32_file_skip
+
+#define Dbg_got_display Dbg32_got_display
+
+#define Dbg_libs_audit Dbg32_libs_audit
+#define Dbg_libs_find Dbg32_libs_find
+#define Dbg_libs_found Dbg32_libs_found
+#define Dbg_libs_ignore Dbg32_libs_ignore
+#define Dbg_libs_init Dbg32_libs_init
+#define Dbg_libs_l Dbg32_libs_l
+#define Dbg_libs_path Dbg32_libs_path
+#define Dbg_libs_req Dbg32_libs_req
+#define Dbg_libs_update Dbg32_libs_update
+#define Dbg_libs_yp Dbg32_libs_yp
+#define Dbg_libs_ylu Dbg32_libs_ylu
+
+#define Dbg_map_dash Dbg32_map_dash
+#define Dbg_map_ent Dbg32_map_ent
+#define Dbg_map_parse Dbg32_map_parse
+#define Dbg_map_pipe Dbg32_map_pipe
+#define Dbg_map_seg Dbg32_map_seg
+#define Dbg_map_set_atsign Dbg32_map_set_atsign
+#define Dbg_map_set_equal Dbg32_map_set_equal
+#define Dbg_map_size_new Dbg32_map_size_new
+#define Dbg_map_size_old Dbg32_map_size_old
+#define Dbg_map_sort_fini Dbg32_map_sort_fini
+#define Dbg_map_sort_orig Dbg32_map_sort_orig
+#define Dbg_map_symbol Dbg32_map_symbol
+#define Dbg_map_version Dbg32_map_version
+
+#define Dbg_move_adjexpandreloc Dbg32_move_adjexpandreloc
+#define Dbg_move_adjmovereloc Dbg32_move_adjmovereloc
+#define Dbg_move_data Dbg32_move_data
+#define Dbg_move_entry1 Dbg32_move_entry1
+#define Dbg_move_entry2 Dbg32_move_entry2
+#define Dbg_move_expand Dbg32_move_expand
+#define Dbg_move_input Dbg32_move_input
+#define Dbg_move_outmove Dbg32_move_outmove
+#define Dbg_move_outsctadj Dbg32_move_outsctadj
+#define Dbg_move_parexpn Dbg32_move_parexpn
+
+#define Dbg_reloc_apply_reg Dbg32_reloc_apply_reg
+#define Dbg_reloc_apply_val Dbg32_reloc_apply_val
+#define Dbg_reloc_ars_entry Dbg32_reloc_ars_entry
+#define Dbg_reloc_copy Dbg32_reloc_copy
+#define Dbg_reloc_discard Dbg32_reloc_discard
+#define Dbg_reloc_doact Dbg32_reloc_doact
+#define Dbg_reloc_doact_title Dbg32_reloc_doact_title
+#define Dbg_reloc_dooutrel Dbg32_reloc_dooutrel
+#define Dbg_reloc_entry Dbg32_reloc_entry
+#define Dbg_reloc_error Dbg32_reloc_error
+#define Dbg_reloc_generate Dbg32_reloc_generate
+#define Dbg_reloc_in Dbg32_reloc_in
+#define Dbg_reloc_ors_entry Dbg32_reloc_ors_entry
+#define Dbg_reloc_out Dbg32_reloc_out
+#define Dbg_reloc_proc Dbg32_reloc_proc
+#define Dbg_reloc_run Dbg32_reloc_run
+#define Dbg_reloc_transition Dbg32_reloc_transition
+#define Dbg_reloc_sloppycomdat Dbg32_reloc_sloppycomdat
+
+#define Dbg_sec_added Dbg32_sec_added
+#define Dbg_sec_created Dbg32_sec_created
+#define Dbg_sec_discarded Dbg32_sec_discarded
+#define Dbg_sec_genstr_compress Dbg32_sec_genstr_compress
+#define Dbg_sec_group Dbg32_sec_group
+#define Dbg_sec_in Dbg32_sec_in
+#define Dbg_sec_order_error Dbg32_sec_order_error
+#define Dbg_sec_order_list Dbg32_sec_order_list
+#define Dbg_sec_strtab Dbg32_sec_strtab
+#define Dbg_sec_unsup_strmerge Dbg32_sec_unsup_strmerge
+
+#define Dbg_seg_desc_entry Dbg32_seg_desc_entry
+#define Dbg_seg_entry Dbg32_seg_entry
+#define Dbg_seg_list Dbg32_seg_list
+#define Dbg_seg_os Dbg32_seg_os
+#define Dbg_seg_title Dbg32_seg_title
+
+#define Dbg_shdr_modified Dbg32_shdr_modified
+
+#define Dbg_statistics_ar Dbg32_statistics_ar
+#define Dbg_statistics_ld Dbg32_statistics_ld
+
+#define Dbg_support_action Dbg32_support_action
+#define Dbg_support_load Dbg32_support_load
+#define Dbg_support_req Dbg32_support_req
+
+#define Dbg_syminfo_entry Dbg32_syminfo_entry
+#define Dbg_syminfo_title Dbg32_syminfo_title
+
+#define Dbg_syms_ar_checking Dbg32_syms_ar_checking
+#define Dbg_syms_ar_entry Dbg32_syms_ar_entry
+#define Dbg_syms_ar_resolve Dbg32_syms_ar_resolve
+#define Dbg_syms_ar_title Dbg32_syms_ar_title
+#define Dbg_syms_created Dbg32_syms_created
+#define Dbg_syms_discarded Dbg32_syms_discarded
+#define Dbg_syms_dlsym Dbg32_syms_dlsym
+#define Dbg_syms_dup_sort_addr Dbg32_syms_dup_sort_addr
+#define Dbg_syms_entered Dbg32_syms_entered
+#define Dbg_syms_entry Dbg32_syms_entry
+#define Dbg_syms_global Dbg32_syms_global
+#define Dbg_syms_ignore Dbg32_syms_ignore
+#define Dbg_syms_ignore_gnuver Dbg32_syms_ignore_gnuver
+#define Dbg_syms_lazy_rescan Dbg32_syms_lazy_rescan
+#define Dbg_syms_lookup Dbg32_syms_lookup
+#define Dbg_syms_lookup_aout Dbg32_syms_lookup_aout
+#define Dbg_syms_new Dbg32_syms_new
+#define Dbg_syms_old Dbg32_syms_old
+#define Dbg_syms_process Dbg32_syms_process
+#define Dbg_syms_reduce Dbg32_syms_reduce
+#define Dbg_syms_reloc Dbg32_syms_reloc
+#define Dbg_syms_resolved Dbg32_syms_resolved
+#define Dbg_syms_resolving Dbg32_syms_resolving
+#define Dbg_syms_sec_entry Dbg32_syms_sec_entry
+#define Dbg_syms_sec_title Dbg32_syms_sec_title
+#define Dbg_syms_spec_title Dbg32_syms_spec_title
+#define Dbg_syms_updated Dbg32_syms_updated
+#define Dbg_syms_up_title Dbg32_syms_up_title
+
+#define Dbg_util_broadcast Dbg32_util_broadcast
+#define Dbg_util_call_array Dbg32_util_call_array
+#define Dbg_util_call_fini Dbg32_util_call_fini
+#define Dbg_util_call_init Dbg32_util_call_init
+#define Dbg_util_call_main Dbg32_util_call_main
+#define Dbg_util_collect Dbg32_util_collect
+#define Dbg_util_dbnotify Dbg32_util_dbnotify
+#define Dbg_util_edge_in Dbg32_util_edge_in
+#define Dbg_util_edge_out Dbg32_util_edge_out
+#define Dbg_util_intoolate Dbg32_util_intoolate
+#define Dbg_util_lcinterface Dbg32_util_lcinterface
+#define Dbg_util_nl Dbg32_util_nl
+#define Dbg_util_no_init Dbg32_util_no_init
+#define Dbg_util_scc_entry Dbg32_util_scc_entry
+#define Dbg_util_scc_title Dbg32_util_scc_title
+#define Dbg_util_str Dbg32_util_str
+#define Dbg_util_wait Dbg32_util_wait
+
+#define Dbg_unused_file Dbg32_unused_file
+#define Dbg_unused_lcinterface Dbg32_unused_lcinterface
+#define Dbg_unused_path Dbg32_unused_path
+#define Dbg_unused_sec Dbg32_unused_sec
+#define Dbg_unused_unref Dbg32_unused_unref
+
+#define Dbg_ver_avail_entry Dbg32_ver_avail_entry
+#define Dbg_ver_avail_title Dbg32_ver_avail_title
+#define Dbg_ver_def_title Dbg32_ver_def_title
+#define Dbg_ver_desc_entry Dbg32_ver_desc_entry
+#define Dbg_ver_need_entry Dbg32_ver_need_entry
+#define Dbg_ver_need_title Dbg32_ver_need_title
+#define Dbg_ver_nointerface Dbg32_ver_nointerface
+#define Dbg_ver_symbol Dbg32_ver_symbol
+
+#endif
+
+/*
+ * External Dbg_*() interface routines.
+ */
+extern void Dbg_args_files(Lm_list *, int, char *);
+extern void Dbg_args_flags(Lm_list *, int, int);
+extern void Dbg_audit_ignore(Rt_map *);
+extern void Dbg_audit_interface(Lm_list *, const char *, const char *);
+extern void Dbg_audit_lib(Lm_list *, const char *);
+extern void Dbg_audit_object(Lm_list *, const char *, const char *);
+extern void Dbg_audit_symval(Lm_list *, const char *, const char *,
+ const char *, Addr, Addr);
+extern void Dbg_audit_skip(Lm_list *, const char *, const char *);
+extern void Dbg_audit_terminate(Lm_list *, const char *);
+extern void Dbg_audit_version(Lm_list *, const char *, ulong_t);
+
+extern void Dbg_bind_global(Rt_map *, Addr, Off, Xword, Pltbindtype,
+ Rt_map *, Addr, Off, const char *, uint_t);
+extern void Dbg_bind_plt_summary(Lm_list *, Half, Word, Word, Word, Word,
+ Word, Word);
+#if defined(_ELF64)
+extern void Dbg_bind_pltpad_from(Rt_map *, Addr, const char *);
+extern void Dbg_bind_pltpad_to(Rt_map *, Addr, const char *, const char *);
+#endif
+extern void Dbg_bind_reject(Rt_map *, Rt_map *, const char *, int);
+extern void Dbg_bind_weak(Rt_map *, Addr, Addr, const char *);
+
+extern void Dbg_cap_hw_candidate(Lm_list *, const char *);
+extern void Dbg_cap_hw_filter(Lm_list *, const char *, Rt_map *);
+extern void Dbg_cap_mapfile(Lm_list *, Xword, Xword, Half);
+extern void Dbg_cap_sec_entry(Lm_list *, uint_t, Xword, Xword, Half);
+extern void Dbg_cap_sec_title(Ofl_desc *);
+extern void Dbg_cap_val_hw1(Lm_list *, Xword, Half);
+
+extern const char *
+ Dbg_demangle_name(const char *);
+
+extern void Dbg_ent_entry(Lm_list *, Half, Ent_desc *);
+extern void Dbg_ent_print(Lm_list *, Half, List *, Boolean);
+
+extern void Dbg_file_analyze(Rt_map *);
+extern void Dbg_file_aout(Lm_list *, const char *, ulong_t, ulong_t,
+ ulong_t, const char *, Aliste);
+extern void Dbg_file_ar(Lm_list *, const char *, int);
+extern void Dbg_file_ar_rescan(Lm_list *);
+extern void Dbg_file_bind_entry(Lm_list *, Bnd_desc *);
+extern void Dbg_file_bindings(Rt_map *, int);
+extern void Dbg_file_cleanup(Lm_list *, const char *, Aliste);
+extern void Dbg_file_cntl(Lm_list *, Aliste, Aliste);
+extern void Dbg_file_config_dis(Lm_list *, const char *, int);
+extern void Dbg_file_config_obj(Lm_list *, const char *, const char *,
+ const char *);
+extern void Dbg_file_del_rescan(Lm_list *);
+extern void Dbg_file_delete(Rt_map *);
+extern void Dbg_file_dlclose(Lm_list *, const char *, int);
+extern void Dbg_file_dldump(Rt_map *, const char *, int);
+extern void Dbg_file_dlopen(Rt_map *, const char *, int *, int);
+extern void Dbg_file_elf(Lm_list *, const char *, ulong_t, ulong_t,
+ ulong_t, ulong_t, const char *, Aliste);
+extern void Dbg_file_filtee(Lm_list *, const char *, const char *, int);
+extern void Dbg_file_filter(Lm_list *, const char *, const char *, int);
+extern void Dbg_file_fixname(Lm_list *, const char *, const char *);
+extern void Dbg_file_generic(Lm_list *, Ifl_desc *);
+extern void Dbg_file_hdl_action(Grp_hdl *, Rt_map *, int, uint_t);
+extern void Dbg_file_hdl_collect(Grp_hdl *, const char *);
+extern void Dbg_file_hdl_title(int);
+extern void Dbg_file_lazyload(Rt_map *, const char *, const char *);
+extern void Dbg_file_ldso(Rt_map *, char **, auxv_t *, const char *,
+ Aliste);
+extern void Dbg_file_mode_promote(Rt_map *, int);
+extern void Dbg_file_modified(Lm_list *, const char *, const char *,
+ const char *, int, int, Elf *, Elf *);
+extern void Dbg_file_needed(Rt_map *, const char *);
+extern void Dbg_file_output(Ofl_desc *);
+extern void Dbg_file_preload(Lm_list *, const char *);
+extern void Dbg_file_prot(Rt_map *, int);
+extern void Dbg_file_rejected(Lm_list *, Rej_desc *, Half mach);
+extern void Dbg_file_reuse(Lm_list *, const char *, const char *);
+extern void Dbg_file_skip(Lm_list *, const char *, const char *);
+
+extern void Dbg_got_display(Ofl_desc *, Off, int, Word, size_t);
+
+extern void Dbg_libs_audit(Lm_list *, const char *, const char *);
+extern void Dbg_libs_find(Lm_list *, const char *);
+extern void Dbg_libs_found(Lm_list *, const char *, int);
+extern void Dbg_libs_ignore(Lm_list *, const char *);
+extern void Dbg_libs_init(Lm_list *, List *, List *);
+extern void Dbg_libs_l(Lm_list *, const char *, const char *);
+extern void Dbg_libs_path(Lm_list *, const char *, uint_t, const char *);
+extern void Dbg_libs_req(Lm_list *, const char *, const char *,
+ const char *);
+extern void Dbg_libs_update(Lm_list *, List *, List *);
+extern void Dbg_libs_yp(Lm_list *, const char *);
+extern void Dbg_libs_ylu(Lm_list *, const char *, const char *, int);
+
+extern void Dbg_map_dash(Lm_list *, const char *, Sdf_desc *);
+extern void Dbg_map_ent(Lm_list *, Boolean, Ent_desc *, Ofl_desc *);
+extern void Dbg_map_parse(Lm_list *, const char *);
+extern void Dbg_map_pipe(Lm_list *, Sg_desc *, const char *, const Word);
+extern void Dbg_map_seg(Ofl_desc *, int, Sg_desc *);
+extern void Dbg_map_set_atsign(Boolean);
+extern void Dbg_map_set_equal(Boolean);
+extern void Dbg_map_size_new(Lm_list *, const char *);
+extern void Dbg_map_size_old(Ofl_desc *, Sym_desc *);
+extern void Dbg_map_sort_fini(Lm_list *, Sg_desc *);
+extern void Dbg_map_sort_orig(Lm_list *, Sg_desc *);
+extern void Dbg_map_symbol(Ofl_desc *, Sym_desc *);
+extern void Dbg_map_version(Lm_list *, const char *, const char *, int);
+
+extern void Dbg_move_adjexpandreloc(Lm_list *, Xword, const char *);
+extern void Dbg_move_adjmovereloc(Lm_list *, Xword, Xword, const char *);
+extern void Dbg_move_data(Rt_map *);
+extern void Dbg_move_entry1(Lm_list *, int, Move *, Sym_desc *);
+extern void Dbg_move_entry2(Lm_list *, Move *, Word, const char *);
+extern void Dbg_move_expand(Lm_list *, Move *, Addr);
+extern void Dbg_move_input(Lm_list *, const char *);
+extern void Dbg_move_outmove(Lm_list *, const char *);
+extern void Dbg_move_outsctadj(Lm_list *, Sym_desc *);
+extern void Dbg_move_parexpn(Lm_list *, const char *, const char *);
+
+extern void Dbg_reloc_apply_reg(Lm_list *, int, Half, Xword, Xword);
+extern void Dbg_reloc_apply_val(Lm_list *, int, Xword, Xword);
+extern void Dbg_reloc_ars_entry(Lm_list *, int, Word, Half, Rel_desc *);
+extern void Dbg_reloc_copy(Rt_map *, Rt_map *, const char *, int);
+extern void Dbg_reloc_discard(Lm_list *, Half, Rel_desc *);
+extern void Dbg_reloc_doact(Lm_list *, int, Half, Word, Word, Xword, Xword,
+ const char *, Os_desc *);
+extern void Dbg_reloc_doact_title(Lm_list *);
+extern void Dbg_reloc_dooutrel(Lm_list *, Word);
+extern void Dbg_reloc_entry(Lm_list *, const char *, Half, Word, void *,
+ const char *, const char *, const char *);
+extern void Dbg_reloc_error(Lm_list *, int, Half, Word, void *,
+ const char *);
+extern void Dbg_reloc_generate(Lm_list *, Os_desc *, Word);
+extern void Dbg_reloc_in(Lm_list *, int, Half, Word, void *, const char *,
+ const char *);
+extern void Dbg_reloc_ors_entry(Lm_list *, int, Word, Half, Rel_desc *);
+extern void Dbg_reloc_out(Ofl_desc *, int, Word, void *, const char *,
+ const char *);
+extern void Dbg_reloc_proc(Lm_list *, Os_desc *, Is_desc *, Is_desc *);
+extern void Dbg_reloc_run(Rt_map *, uint_t, int, int);
+extern void Dbg_reloc_transition(Lm_list *, Half, Word, Rel_desc *);
+extern void Dbg_reloc_sloppycomdat(Lm_list *, const char *, Sym_desc *);
+
+extern void Dbg_sec_added(Lm_list *, Os_desc *, Sg_desc *);
+extern void Dbg_sec_created(Lm_list *, Os_desc *, Sg_desc *);
+extern void Dbg_sec_discarded(Lm_list *, Is_desc *, Is_desc *);
+extern void Dbg_sec_genstr_compress(Lm_list *, const char *,
+ Xword, Xword);
+extern void Dbg_sec_group(Lm_list *, Is_desc *, Group_desc *);
+extern void Dbg_sec_in(Lm_list *, Is_desc *);
+extern void Dbg_sec_order_error(Lm_list *, Ifl_desc *, Word, int);
+extern void Dbg_sec_order_list(Ofl_desc *, int);
+extern void Dbg_sec_strtab(Lm_list *, Os_desc *, Str_tbl *);
+extern void Dbg_sec_unsup_strmerge(Lm_list *, Is_desc *);
+
+extern void Dbg_seg_desc_entry(Lm_list *, Half, int, Sg_desc *);
+extern void Dbg_seg_entry(Ofl_desc *, int, Sg_desc *);
+extern void Dbg_seg_list(Lm_list *, Half, List *);
+extern void Dbg_seg_os(Ofl_desc *, Os_desc *, int);
+extern void Dbg_seg_title(Lm_list *);
+
+extern void Dbg_shdr_modified(Lm_list *, const char *, Half, Shdr *, Shdr *,
+ const char *);
+
+extern void Dbg_statistics_ar(Ofl_desc *);
+extern void Dbg_statistics_ld(Ofl_desc *);
+
+extern void Dbg_support_action(Lm_list *, const char *, const char *,
+ Support_ndx, const char *);
+extern void Dbg_support_load(Lm_list *, const char *, const char *);
+extern void Dbg_support_req(Lm_list *, const char *, int);
+
+extern void Dbg_syminfo_entry(Lm_list *, Word, Syminfo *, Sym *,
+ const char *, Dyn *);
+extern void Dbg_syminfo_title(Lm_list *);
+
+extern void Dbg_syms_ar_checking(Lm_list *, Xword, Elf_Arsym *,
+ const char *);
+extern void Dbg_syms_ar_entry(Lm_list *, Xword, Elf_Arsym *);
+extern void Dbg_syms_ar_resolve(Lm_list *, Xword, Elf_Arsym *,
+ const char *, int);
+extern void Dbg_syms_ar_title(Lm_list *, const char *, int);
+extern void Dbg_syms_created(Lm_list *, const char *);
+extern void Dbg_syms_discarded(Lm_list *, Sym_desc *);
+extern void Dbg_syms_dlsym(Rt_map *, const char *, int *, const char *,
+ int);
+extern void Dbg_syms_dup_sort_addr(Lm_list *, const char *, const char *,
+ const char *, Addr);
+extern void Dbg_syms_entered(Ofl_desc *, Sym *, Sym_desc *);
+extern void Dbg_syms_entry(Lm_list *, Word, Sym_desc *);
+extern void Dbg_syms_global(Lm_list *, Word, const char *);
+extern void Dbg_syms_ignore(Ofl_desc *, Sym_desc *);
+extern void Dbg_syms_ignore_gnuver(Rt_map *, const char *, Word, Versym);
+extern void Dbg_syms_lazy_rescan(Lm_list *, const char *);
+extern void Dbg_syms_lookup(Rt_map *, const char *, const char *);
+#if !(defined(_ELF64))
+extern void Dbg_syms_lookup_aout(Lm_list *, const char *);
+#endif
+extern void Dbg_syms_new(Ofl_desc *, Sym *, Sym_desc *);
+extern void Dbg_syms_old(Ofl_desc *, Sym_desc *);
+extern void Dbg_syms_process(Lm_list *, Ifl_desc *);
+extern void Dbg_syms_reduce(Ofl_desc *, int, Sym_desc *, int,
+ const char *);
+extern void Dbg_syms_reloc(Ofl_desc *, Sym_desc *);
+extern void Dbg_syms_resolved(Ofl_desc *, Sym_desc *);
+extern void Dbg_syms_resolving(Ofl_desc *, Word, const char *, int, int,
+ Sym *, Sym *, Sym_desc *, Ifl_desc *);
+extern void Dbg_syms_sec_entry(Lm_list *, Word, Sg_desc *, Os_desc *);
+extern void Dbg_syms_sec_title(Lm_list *);
+extern void Dbg_syms_spec_title(Lm_list *);
+extern void Dbg_syms_updated(Ofl_desc *, Sym_desc *, const char *);
+extern void Dbg_syms_up_title(Lm_list *);
+
+extern void Dbg_tls_modactivity(Lm_list *, void *, uint_t);
+extern void Dbg_tls_static_block(Lm_list *, void *, ulong_t, ulong_t);
+extern void Dbg_tls_static_resv(Rt_map *, ulong_t, ulong_t);
+
+extern void Dbg_util_broadcast(Rt_map *);
+extern void Dbg_util_call_array(Rt_map *, void *, int, Word);
+extern void Dbg_util_call_fini(Rt_map *);
+extern void Dbg_util_call_init(Rt_map *, int);
+extern void Dbg_util_call_main(Rt_map *);
+extern void Dbg_util_collect(Rt_map *, int, int);
+extern void Dbg_util_dbnotify(Lm_list *, rd_event_e, r_state_e);
+extern void Dbg_util_edge_in(Lm_list *, Rt_map *, uint_t, Rt_map *,
+ int, int);
+extern void Dbg_util_edge_out(Rt_map *, Rt_map *);
+extern void Dbg_util_intoolate(Rt_map *);
+extern void Dbg_util_lcinterface(Rt_map *, int, char *);
+extern void Dbg_util_nl(Lm_list *, int);
+extern void Dbg_util_no_init(Rt_map *);
+extern void Dbg_util_str(Lm_list *, const char *);
+extern void Dbg_util_scc_entry(Rt_map *, uint_t);
+extern void Dbg_util_scc_title(Lm_list *, int);
+extern void Dbg_util_wait(Rt_map *, Rt_map *, int);
+
+extern void Dbg_unused_file(Lm_list *, const char *, int, uint_t);
+extern void Dbg_unused_lcinterface(Rt_map *, Rt_map *, int);
+extern void Dbg_unused_path(Lm_list *, const char *, uint_t, uint_t,
+ const char *);
+extern void Dbg_unused_sec(Lm_list *, Is_desc *);
+extern void Dbg_unused_unref(Rt_map *, const char *);
+
+extern void Dbg_ver_avail_entry(Lm_list *, Ver_index *, const char *);
+extern void Dbg_ver_avail_title(Lm_list *, const char *);
+extern void Dbg_ver_def_title(Lm_list *, const char *);
+extern void Dbg_ver_desc_entry(Lm_list *, Ver_desc *);
+extern void Dbg_ver_need_entry(Lm_list *, Half, const char *,
+ const char *);
+extern void Dbg_ver_need_title(Lm_list *, const char *);
+extern void Dbg_ver_nointerface(Lm_list *, const char *);
+extern void Dbg_ver_symbol(Lm_list *, const char *);
+
+/*
+ * Define Elf_*() interface flags.
+ */
+#define ELF_DBG_ELFDUMP 1
+#define ELF_DBG_RTLD 2
+#define ELF_DBG_LD 3
+
+/*
+ * Define generic Elf_*() interfaces.
+ */
+extern void Elf_syminfo_entry(Lm_list *, Word, Syminfo *, const char *,
+ const char *);
+extern void Elf_syminfo_title(Lm_list *);
+
+/*
+ * Establish ELF32 and ELF64 class Elf_*() interfaces.
+ */
+#if defined(_ELF64)
+
+#define Elf_cap_entry Elf64_cap_entry
+#define Elf_cap_title Elf64_cap_title
+
+#define Elf_demangle_name Elf64_demangle_name
+#define Elf_dyn_entry Elf64_dyn_entry
+#define Elf_dyn_null_entry Elf64_dyn_null_entry
+#define Elf_dyn_title Elf64_dyn_title
+
+#define Elf_ehdr Elf64_ehdr
+
+#define Elf_got_entry Elf64_got_entry
+#define Elf_got_title Elf64_got_title
+
+#define Elf_reloc_apply_reg Elf64_reloc_apply_reg
+#define Elf_reloc_apply_val Elf64_reloc_apply_val
+#define Elf_reloc_entry_1 Elf64_reloc_entry_1
+#define Elf_reloc_entry_2 Elf64_reloc_entry_2
+#define Elf_reloc_title Elf64_reloc_title
+
+#define Elf_phdr Elf64_phdr
+
+#define Elf_shdr Elf64_shdr
+
+#define Elf_syms_table_entry Elf64_syms_table_entry
+#define Elf_syms_table_title Elf64_syms_table_title
+
+#define Elf_ver_def_title Elf64_ver_def_title
+#define Elf_ver_line_1 Elf64_ver_line_1
+#define Elf_ver_line_2 Elf64_ver_line_2
+#define Elf_ver_line_3 Elf64_ver_line_3
+#define Elf_ver_line_4 Elf64_ver_line_4
+#define Elf_ver_line_5 Elf64_ver_line_5
+#define Elf_ver_need_title Elf64_ver_need_title
+
+#else
+
+#define Elf_cap_entry Elf32_cap_entry
+#define Elf_cap_title Elf32_cap_title
+
+#define Elf_demangle_name Elf32_demangle_name
+#define Elf_dyn_entry Elf32_dyn_entry
+#define Elf_dyn_null_entry Elf32_dyn_null_entry
+#define Elf_dyn_title Elf32_dyn_title
+
+#define Elf_ehdr Elf32_ehdr
+
+#define Elf_got_entry Elf32_got_entry
+#define Elf_got_title Elf32_got_title
+
+#define Elf_reloc_apply_reg Elf32_reloc_apply_reg
+#define Elf_reloc_apply_val Elf32_reloc_apply_val
+#define Elf_reloc_entry_1 Elf32_reloc_entry_1
+#define Elf_reloc_entry_2 Elf32_reloc_entry_2
+#define Elf_reloc_title Elf32_reloc_title
+
+#define Elf_phdr Elf32_phdr
+
+#define Elf_shdr Elf32_shdr
+
+#define Elf_syms_table_entry Elf32_syms_table_entry
+#define Elf_syms_table_title Elf32_syms_table_title
+
+#define Elf_ver_def_title Elf32_ver_def_title
+#define Elf_ver_line_1 Elf32_ver_line_1
+#define Elf_ver_line_2 Elf32_ver_line_2
+#define Elf_ver_line_3 Elf32_ver_line_3
+#define Elf_ver_line_4 Elf32_ver_line_4
+#define Elf_ver_line_5 Elf32_ver_line_5
+#define Elf_ver_need_title Elf32_ver_need_title
+
+#endif
+
+extern void Elf_cap_entry(Lm_list *, Cap *, int, Half);
+extern void Elf_cap_title(Lm_list *);
+
+extern const char \
+ *Elf_demangle_name(const char *);
+extern void Elf_dyn_entry(Lm_list *, Dyn *, int, const char *, Half);
+extern void Elf_dyn_null_entry(Lm_list *, Dyn *, int, int);
+extern void Elf_dyn_title(Lm_list *);
+
+extern void Elf_ehdr(Lm_list *, Ehdr *, Shdr *);
+
+extern void Elf_got_entry(Lm_list *, Sword, Addr, Xword, Half,
+ uchar_t, uchar_t, Word, void *, const char *);
+extern void Elf_got_title(Lm_list *);
+
+extern void Elf_phdr(Lm_list *, Half, Phdr *);
+
+extern void Elf_reloc_apply_val(Lm_list *, int, Xword, Xword);
+extern void Elf_reloc_apply_reg(Lm_list *, int, Half, Xword, Xword);
+extern void Elf_reloc_entry_1(Lm_list *, int, const char *, Half, Word,
+ void *, const char *, const char *, const char *);
+extern void Elf_reloc_entry_2(Lm_list *, int, const char *, Word,
+ const char *, Off, Sxword, const char *, const char *,
+ const char *);
+extern void Elf_reloc_title(Lm_list *, int, Word);
+
+extern void Elf_shdr(Lm_list *, Half, Shdr *);
+
+extern void Elf_syms_table_entry(Lm_list *, int, const char *, Half, Sym *,
+ Versym, int, const char *, const char *);
+extern void Elf_syms_table_title(Lm_list *, int);
+
+extern void Elf_ver_def_title(Lm_list *);
+extern void Elf_ver_line_1(Lm_list *, const char *, const char *,
+ const char *, const char *);
+extern void Elf_ver_line_2(Lm_list *, const char *, const char *);
+extern void Elf_ver_line_3(Lm_list *, const char *, const char *,
+ const char *);
+extern void Elf_ver_line_4(Lm_list *, const char *);
+extern void Elf_ver_line_5(Lm_list *, const char *, const char *);
+extern void Elf_ver_need_title(Lm_list *, int);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _DEBUG_H */
diff --git a/cddl/contrib/opensolaris/cmd/sgs/include/sgs.h b/cddl/contrib/opensolaris/cmd/sgs/include/sgs.h
new file mode 100644
index 000000000000..34669cf60b3a
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/sgs/include/sgs.h
@@ -0,0 +1,296 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright (c) 1988 AT&T
+ * All Rights Reserved
+ *
+ *
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Global include file for all sgs.
+ */
+
+#ifndef _SGS_H
+#define _SGS_H
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* <assert.h> keys off of NDEBUG */
+#ifdef DEBUG
+#undef NDEBUG
+#else
+#define NDEBUG
+#endif
+
+#ifndef _ASM
+#include <sys/types.h>
+#if defined(sun)
+#include <sys/machelf.h>
+#else
+#include <elf.h>
+#endif
+#include <stdlib.h>
+#include <libelf.h>
+#include <assert.h>
+#include <alist.h>
+#endif /* _ASM */
+
+/*
+ * Software identification.
+ */
+#define SGS ""
+#define SGU_PKG "Software Generation Utilities"
+#define SGU_REL "(SGU) Solaris-ELF (4.0)"
+
+
+#ifndef _ASM
+
+/*
+ * link_ver_string[] contains a version string for use by the link-editor
+ * and all other linker components. It is found in libconv, and is
+ * generated by sgs/libconv/common/bld_vernote.ksh. That script produces
+ * libconv/{plat}/vernote.s, which is in turn assembled/linked into
+ * libconv.
+ */
+extern const char link_ver_string[];
+/*
+ * Macro to round to next double word boundary.
+ */
+#define S_DROUND(x) (((x) + sizeof (double) - 1) & ~(sizeof (double) - 1))
+
+/*
+ * General align and round macros.
+ */
+#define S_ALIGN(x, a) ((x) & ~(((a) ? (a) : 1) - 1))
+#define S_ROUND(x, a) ((x) + (((a) ? (a) : 1) - 1) & ~(((a) ? (a) : 1) - 1))
+
+/*
+ * Bit manipulation macros; generic bit mask and is `v' in the range
+ * supportable in `n' bits?
+ */
+#define S_MASK(n) ((1 << (n)) -1)
+#define S_INRANGE(v, n) (((-(1 << (n)) - 1) < (v)) && ((v) < (1 << (n))))
+
+
+/*
+ * Yet another definition of the OFFSETOF macro, used with the AVL routines.
+ */
+#define SGSOFFSETOF(s, m) ((size_t)(&(((s *)0)->m)))
+
+/*
+ * When casting between integer and pointer types, gcc will complain
+ * if the integer type used is not large enough to hold the pointer
+ * value without loss. Although a dubious practice in general, this
+ * is sometimes done by design. In those cases, the general solution
+ * is to introduce an intermediate cast to widen the integer value. The
+ * CAST_PTRINT macro does this, and its use documents the fact that
+ * the programmer is doing that sort of cast.
+ */
+#ifdef __GNUC__
+#define CAST_PTRINT(cast, value) ((cast)(uintptr_t)value)
+#else
+#define CAST_PTRINT(cast, value) ((cast)value)
+#endif
+
+/*
+ * General typedefs.
+ */
+typedef enum {
+ FALSE = 0,
+ TRUE = 1
+} Boolean;
+
+/*
+ * Types of errors (used by eprintf()), together with a generic error return
+ * value.
+ */
+typedef enum {
+ ERR_NONE,
+ ERR_WARNING,
+ ERR_FATAL,
+ ERR_ELF,
+ ERR_NUM /* Must be last */
+} Error;
+
+#if defined(_LP64) && !defined(_ELF64)
+#define S_ERROR (~(uint_t)0)
+#else
+#define S_ERROR (~(uintptr_t)0)
+#endif
+
+/*
+ * LIST_TRAVERSE() is used as the only "argument" of a "for" loop to
+ * traverse a linked list. The node pointer `node' is set to each node in
+ * turn and the corresponding data pointer is copied to `data'. The macro
+ * is used as in
+ * for (LIST_TRAVERSE(List *list, Listnode *node, void *data)) {
+ * process(data);
+ * }
+ */
+#define LIST_TRAVERSE(L, N, D) \
+ (void) (((N) = (L)->head) != NULL && ((D) = (N)->data) != NULL); \
+ (N) != NULL; \
+ (void) (((N) = (N)->next) != NULL && ((D) = (N)->data) != NULL)
+
+typedef struct listnode Listnode;
+typedef struct list List;
+
+struct listnode { /* a node on a linked list */
+ void *data; /* the data item */
+ Listnode *next; /* the next element */
+};
+
+struct list { /* a linked list */
+ Listnode *head; /* the first element */
+ Listnode *tail; /* the last element */
+};
+
+
+#ifdef _SYSCALL32
+typedef struct listnode32 Listnode32;
+typedef struct list32 List32;
+
+struct listnode32 { /* a node on a linked list */
+ Elf32_Addr data; /* the data item */
+ Elf32_Addr next; /* the next element */
+};
+
+struct list32 { /* a linked list */
+ Elf32_Addr head; /* the first element */
+ Elf32_Addr tail; /* the last element */
+};
+#endif /* _SYSCALL32 */
+
+
+/*
+ * Structure to maintain rejected files elf information. Files that are not
+ * applicable to the present link-edit are rejected and a search for an
+ * appropriate file may be resumed. The first rejected files information is
+ * retained so that a better error diagnostic can be given should an appropriate
+ * file not be located.
+ */
+typedef struct {
+ ushort_t rej_type; /* SGS_REJ_ value */
+ ushort_t rej_flag; /* additional information */
+ uint_t rej_info; /* numeric and string information */
+ const char *rej_str; /* associated with error */
+ const char *rej_name; /* object name - expanded library */
+ /* name and archive members */
+} Rej_desc;
+
+#define SGS_REJ_NONE 0
+#define SGS_REJ_MACH 1 /* wrong ELF machine type */
+#define SGS_REJ_CLASS 2 /* wrong ELF class (32-bit/64-bit) */
+#define SGS_REJ_DATA 3 /* wrong ELF data format (MSG/LSB) */
+#define SGS_REJ_TYPE 4 /* bad ELF type */
+#define SGS_REJ_BADFLAG 5 /* bad ELF flags value */
+#define SGS_REJ_MISFLAG 6 /* mismatched ELF flags value */
+#define SGS_REJ_VERSION 7 /* mismatched ELF/lib version */
+#define SGS_REJ_HAL 8 /* HAL R1 extensions required */
+#define SGS_REJ_US3 9 /* Sun UltraSPARC III extensions */
+ /* required */
+#define SGS_REJ_STR 10 /* generic error - info is a string */
+#define SGS_REJ_UNKFILE 11 /* unknown file type */
+#define SGS_REJ_HWCAP_1 12 /* hardware capabilities mismatch */
+
+/*
+ * For those source files used both inside and outside of the
+ * libld source base (tools/common/string_table.c) we can
+ * automatically switch between the allocation models
+ * based off of the 'cc -DUSE_LIBLD_MALLOC' flag.
+ */
+#ifdef USE_LIBLD_MALLOC
+#define calloc(x, a) libld_malloc(((size_t)x) * ((size_t)a))
+#define free libld_free
+#define malloc libld_malloc
+#define realloc libld_realloc
+
+#define libld_calloc(x, a) libld_malloc(((size_t)x) * ((size_t)a))
+extern void libld_free(void *);
+extern void *libld_malloc(size_t);
+extern void *libld_realloc(void *, size_t);
+#endif
+
+
+/*
+ * Data structures (defined in libld.h).
+ */
+typedef struct ent_desc Ent_desc;
+typedef struct group_desc Group_desc;
+typedef struct ifl_desc Ifl_desc;
+typedef struct is_desc Is_desc;
+typedef struct isa_desc Isa_desc;
+typedef struct isa_opt Isa_opt;
+typedef struct mv_desc Mv_desc;
+typedef struct ofl_desc Ofl_desc;
+typedef struct os_desc Os_desc;
+typedef struct rel_cache Rel_cache;
+typedef struct sdf_desc Sdf_desc;
+typedef struct sdv_desc Sdv_desc;
+typedef struct sg_desc Sg_desc;
+typedef struct sort_desc Sort_desc;
+typedef struct sec_order Sec_order;
+typedef struct sym_desc Sym_desc;
+typedef struct sym_aux Sym_aux;
+typedef struct sym_avlnode Sym_avlnode;
+typedef struct uts_desc Uts_desc;
+typedef struct ver_desc Ver_desc;
+typedef struct ver_index Ver_index;
+typedef struct audit_desc Audit_desc;
+typedef struct audit_info Audit_info;
+typedef struct audit_list Audit_list;
+
+/*
+ * Data structures defined in machrel.h.
+ */
+typedef struct rel_desc Rel_desc;
+
+/*
+ * Data structures defined in rtld.h.
+ */
+typedef struct lm_list Lm_list;
+#ifdef _SYSCALL32
+typedef struct lm_list32 Lm_list32;
+#endif /* _SYSCALL32 */
+
+/*
+ * For the various utilities that include sgs.h
+ */
+extern int assfail(const char *, const char *, int);
+extern void eprintf(Lm_list *, Error, const char *, ...);
+extern char *sgs_demangle(char *);
+extern uint_t sgs_str_hash(const char *);
+extern uint_t findprime(uint_t);
+
+#endif /* _ASM */
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif /* _SGS_H */
diff --git a/cddl/contrib/opensolaris/cmd/sgs/include/string_table.h b/cddl/contrib/opensolaris/cmd/sgs/include/string_table.h
new file mode 100644
index 000000000000..e42f81779f17
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/sgs/include/string_table.h
@@ -0,0 +1,63 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#ifndef _STRING_TABLE_DOT_H
+#define _STRING_TABLE_DOT_H
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <sys/types.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Exported, opaque string table handle.
+ */
+typedef struct str_tbl Str_tbl;
+
+/*
+ * Exported string table functions.
+ */
+extern int st_delstring(Str_tbl *, const char *);
+extern void st_destroy(Str_tbl *);
+extern size_t st_getstrtab_sz(Str_tbl *);
+extern const char *st_getstrbuf(Str_tbl *);
+extern int st_insert(Str_tbl *, const char *);
+extern Str_tbl *st_new(uint_t);
+extern int st_setstrbuf(Str_tbl *, char *, size_t);
+extern int st_setstring(Str_tbl *, const char *, size_t *);
+
+/*
+ * Exported flags values for st_new().
+ */
+#define FLG_STNEW_COMPRESS 0x01 /* compressed string table */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _STRING_TABLE_DOT_H */
diff --git a/cddl/contrib/opensolaris/cmd/sgs/messages/sgs.ident b/cddl/contrib/opensolaris/cmd/sgs/messages/sgs.ident
new file mode 100644
index 000000000000..6afbf5ff5e26
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/sgs/messages/sgs.ident
@@ -0,0 +1,62 @@
+#
+# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+# Use is subject to license terms.
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+# ident "%Z%%M% %I% %E% SMI"
+#
+#
+# Global message identifiers for the sgs utilities. This information is read
+# by sgsmsg(1l) using the -i option.
+# Each utilities message file references one of the `MSGID' identifiers. Its
+# associated numeric setid is used when creating catgets(3c) messages, the
+# string domain is used when creating gettext(3i) messages.
+#
+
+mesgid setid domain
+
+MSG_ID_RTLD 1 SUNW_OST_SGS /* sgs/rtld */
+MSG_ID_LIBRTLD 2 SUNW_OST_SGS /* sgs/librtld */
+MSG_ID_LIBLD 3 SUNW_OST_SGS /* sgs/libld */
+MSG_ID_LIBLDDBG 4 SUNW_OST_SGS /* sgs/liblddbg */
+MSG_ID_LIBLDSTAB 5 SUNW_OST_SGS /* sgs/libldstab */
+MSG_ID_LIBRTLD_DB 6 SUNW_OST_SGS /* sgs/librtld_db */
+MSG_ID_LIBPROF 7 SUNW_OST_SGS /* sgs/libprof */
+MSG_ID_LIBCRLE 8 SUNW_OST_SGS /* sgs/libcrle */
+
+MSG_ID_LIBELF 10 SUNW_OST_SGS /* sgs/libelf */
+
+MSG_ID_LD 20 SUNW_OST_SGS /* sgs/ld */
+MSG_ID_LDD 21 SUNW_OST_SGS /* sgs/ldd */
+MSG_ID_PVS 22 SUNW_OST_SGS /* sgs/pvs */
+MSG_ID_CRLE 23 SUNW_OST_SGS /* sgs/crle */
+MSG_ID_ELFDUMP 24 SUNW_OST_SGS /* sgs/elfdump */
+MSG_ID_MOE 25 SUNW_OST_SGS /* sgs/moe */
+MSG_ID_ELFEDIT 26 SUNW_OST_SGS /* sgs/elfedit */
+MSG_ID_ELFEDIT_CAP 27 SUNW_OST_SGS /* cap: */
+MSG_ID_ELFEDIT_DYN 27 SUNW_OST_SGS /* dyn: */
+MSG_ID_ELFEDIT_EHDR 27 SUNW_OST_SGS /* ehdr: */
+MSG_ID_ELFEDIT_PHDR 27 SUNW_OST_SGS /* phdr: */
+MSG_ID_ELFEDIT_SHDR 27 SUNW_OST_SGS /* shdr: */
+MSG_ID_ELFEDIT_STR 27 SUNW_OST_SGS /* str: */
+MSG_ID_ELFEDIT_SYM 27 SUNW_OST_SGS /* sym: */
+MSG_ID_ELFEDIT_SYMINFO 27 SUNW_OST_SGS /* syminfo: */
+MSG_ID_ELFWRAP 28 SUNW_OST_SGS /* sgs/elfwrap */
diff --git a/cddl/contrib/opensolaris/cmd/sgs/tools/common/findprime.c b/cddl/contrib/opensolaris/cmd/sgs/tools/common/findprime.c
new file mode 100644
index 000000000000..299fa21362c7
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/sgs/tools/common/findprime.c
@@ -0,0 +1,56 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <sys/types.h>
+#include <sgs.h>
+
+/*
+ * function that will find a prime'ish number. Usefull for
+ * hashbuckets and related things.
+ */
+uint_t
+findprime(uint_t count)
+{
+ uint_t h, f;
+
+ if (count <= 3)
+ return (3);
+
+
+ /*
+ * Check to see if divisible by two, if so
+ * increment.
+ */
+ if ((count & 0x1) == 0)
+ count++;
+
+ for (h = count, f = 2; f * f <= h; f++)
+ if ((h % f) == 0)
+ h += f = 1;
+ return (h);
+}
diff --git a/cddl/contrib/opensolaris/cmd/sgs/tools/common/sgsmsg.c b/cddl/contrib/opensolaris/cmd/sgs/tools/common/sgsmsg.c
new file mode 100644
index 000000000000..9b2e37b507d6
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/sgs/tools/common/sgsmsg.c
@@ -0,0 +1,1210 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * sgsmsg generates several message files from an input template file. Messages
+ * are constructed for use with gettext(3i) - the default - or catgets(3c). The
+ * files generate are:
+ *
+ * msg.h a header file containing definitions for each message. The -h
+ * option triggers the creation of these definitions and specifies
+ * the name to use.
+ *
+ * msg.c a data array of message strings. The msg.h definitions are
+ * offsets into this array. The -d option triggers the creation of
+ * these definitions and specifies the name to use.
+ *
+ * messages a message file suitable for catgets(3c) or gettext(3i) use. The
+ * -m option triggers this output and specifies the filename to be
+ * used.
+ *
+ * The template file is processed based on the first character of each line:
+ *
+ * # or $ entries are copied (as is) to the message file (messages).
+ *
+ * @ token(s) entries are translated. Two translations are possible dependent
+ * on whether one or more tokens are supplied:
+ *
+ * A single token is interpreted as one of two reserved message
+ * output indicators, or a message identifier. The reserved output
+ * indicator _START_ enables output to the message file - Note that
+ * the occurance of any other @ token will also enable message
+ * output. The reserved output indicator _END_ disables output to
+ * the message file. The use of these two indicators provides for
+ * only those message strings that require translation to be output
+ * to the message file.
+ *
+ * Besides the reserved output indicators, a single token is taken
+ * to be a message identifier which will be subsituted for a
+ * `setid' for catgets(3c) output, or a `domain' name for
+ * gettext(3i) output. This value is determine by substituting the
+ * token for the associated definition found in the message
+ * identifier file (specified with the -i option).
+ *
+ * Multiple tokens are taken to be a message definition followed by
+ * the associated message string. The message string is copied to
+ * the data array being built in msg.c. The index into this array
+ * becomes the `message' identifier created in the msg.h file.
+ */
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <limits.h>
+#include <string.h>
+#include <ctype.h>
+#include <errno.h>
+#include <sys/param.h>
+
+#include <sgs.h>
+#include <_string_table.h>
+
+/*
+ * Define any error message strings.
+ */
+static const char
+ * Errmsg_malt = "sgsmsg: file %s: line %d: malformed input "
+ "at line\n",
+ * Errmsg_nmem = "sgsmsg: memory allocation failed: %s\n",
+ * Errmsg_opne = "sgsmsg: file %s: open failed: %s\n",
+ * Errmsg_wrte = "sgsmsg: file %s: write failed: %s\n",
+ * Errmsg_read = "sgsmsg: file %s: read failed %s\n",
+ * Errmsg_stnw = "sgsmsg: st_new(): failed: %s\n",
+ * Errmsg_stin = "sgsmsg: Str_tbl insert failed: %s\n",
+ * Errmsg_mnfn = "sgsmsg: message not found in Str_tbl: %s\n",
+ * Errmsg_use = "usage: sgsmsg [-clv] [-d mesgdata] [-h mesgdefs] "
+ "[-m messages] [-n name] [-i mesgident] file ...\n";
+
+/*
+ * Define all output filenames and associated descriptors.
+ */
+static FILE *fddefs, *fddata, *fdmsgs, *fdmids, *fddesc;
+static char *fldefs, *fldata, *flmsgs, *flmids, *fldesc;
+static FILE *fdlint;
+static char fllint[MAXPATHLEN];
+
+static uint_t vflag; /* verbose flag */
+static Str_tbl *stp; /* string table */
+
+/*
+ * Define any default strings.
+ */
+static const char
+ *nmlint = "/tmp/sgsmsg.lint",
+ *interface = "sgs_msg",
+ *start = "_START_",
+ *end = "_END_";
+
+/*
+ * Define any default flags and data items.
+ */
+static int cflag = 0, lflag = 0, prtmsgs = 0, line, ptr = 1, msgid = 0;
+static char *mesgid = 0, *setid = 0, *domain = 0;
+
+typedef struct msg_string {
+ char *ms_defn;
+ char *ms_message;
+ struct msg_string *ms_next;
+} msg_string;
+
+static msg_string *msg_head;
+static msg_string *msg_tail;
+
+/*
+ * message_append() is responsible for both inserting strings into
+ * the master Str_tbl as well as maintaining a list of the
+ * DEFINITIONS associated with each string.
+ *
+ * The list of strings is traversed at the end once the full
+ * Str_tbl has been constructed - and string offsets can be
+ * assigned.
+ */
+static void
+message_append(const char *defn, const char *message)
+{
+ msg_string *msg;
+ if ((msg = calloc(sizeof (msg_string), 1)) == 0) {
+ (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
+ exit(1);
+ }
+
+ /*
+ * Initialize the string table.
+ */
+ if ((stp == 0) && ((stp = st_new(FLG_STNEW_COMPRESS)) == NULL)) {
+ (void) fprintf(stderr, Errmsg_stnw, strerror(errno));
+ exit(1);
+ }
+
+
+ if ((msg->ms_defn = strdup(defn)) == 0) {
+ (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
+ exit(1);
+ }
+ if ((msg->ms_message = strdup(message)) == 0) {
+ (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
+ exit(1);
+ }
+
+ if (st_insert(stp, msg->ms_message) == -1) {
+ (void) fprintf(stderr, Errmsg_stin,
+ message);
+ exit(1);
+ }
+
+ if (msg_head == 0) {
+ msg_head = msg_tail = msg;
+ return;
+ }
+ msg_tail->ms_next = msg;
+ msg_tail = msg;
+}
+
+/*
+ * Initialize a setid value. Given a setid definition determine its numeric
+ * value from the specified message identifier file (specified with the -i
+ * option). Return a pointer to the numeric string.
+ */
+static int
+getmesgid(char *id)
+{
+ char *buffer, *token, *_mesgid = 0, *_setid = 0, *_domain = 0;
+
+ /*
+ * If we're being asked to interpret a message id but the user didn't
+ * provide the required message identifier file (-i option) we're in
+ * trouble.
+ */
+ if (flmids == 0) {
+ (void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
+ "unable to process mesgid\n\t"
+ "no message identifier file specified "
+ "(see -i option)\n", fldesc, line, id);
+ return (1);
+ }
+
+ if ((buffer = malloc(LINE_MAX)) == 0) {
+ (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
+ return (1);
+ }
+
+ /*
+ * Read the message identifier file and locate the required mesgid.
+ */
+ rewind(fdmids);
+ while (fgets(buffer, LINE_MAX, fdmids) != NULL) {
+ if ((token = strstr(buffer, id)) == NULL)
+ continue;
+
+ /*
+ * Establish individual strings for the mesgid, setid and domain
+ * values.
+ */
+ _mesgid = token;
+ while (!(isspace(*token)))
+ token++;
+ *token++ = 0;
+
+ while (isspace(*token))
+ token++;
+ _setid = token;
+ while (!(isspace(*token)))
+ token++;
+ *token++ = 0;
+
+ while (isspace(*token))
+ token++;
+ _domain = token;
+ while (!(isspace(*token)))
+ token++;
+ *token = 0;
+ break;
+ }
+
+ /*
+ * Did we find a match?
+ */
+ if ((_mesgid == 0) || (_setid == 0) || (_domain == 0)) {
+ (void) fprintf(stderr, "sgsmsg: file %s: line %d: mesgid %s: "
+ "unable to process mesgid\n\t"
+ "identifier does not exist in file %s\n",
+ fldesc, line, id, flmids);
+ return (1);
+ }
+
+ /*
+ * Have we been here before?
+ */
+ if (mesgid) {
+ if (cflag == 1) {
+ /*
+ * If we're being asked to process more than one mesgid
+ * warn the user that only one mesgid can be used for
+ * the catgets(3c) call.
+ */
+ (void) fprintf(stderr, "sgsmsg: file %s: line %d: "
+ "setid %s: warning: multiple mesgids "
+ "encountered\n\t"
+ "last setting used in messaging code\n",
+ fldesc, line, id);
+ }
+ }
+
+ mesgid = _mesgid;
+ setid = _setid;
+ domain = _domain;
+
+ /*
+ * Generate the message file output (insure output flag is enabled).
+ */
+ if (prtmsgs != -1)
+ prtmsgs = 1;
+ if (fdmsgs && (prtmsgs == 1)) {
+ if (cflag == 1) {
+ if (fprintf(fdmsgs, "$quote \"\n$set %s\n",
+ setid) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, flmsgs,
+ strerror(errno));
+ return (1);
+ }
+ } else {
+ if (fprintf(fdmsgs, "domain\t\"%s\"\n", domain) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, flmsgs,
+ strerror(errno));
+ return (1);
+ }
+ }
+ }
+
+ /*
+ * For catgets(3c) output generate a setid definition in the message
+ * definition file.
+ */
+ if (fddefs && (cflag == 1) &&
+ (fprintf(fddefs, "#define\t%s\t%s\n\n", mesgid, setid) < 0)) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ return (0);
+}
+
+/*
+ * Dump contents of String Table to standard out
+ */
+static void
+dump_stringtab(Str_tbl *stp)
+{
+ uint_t cnt;
+
+ if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0) {
+ (void) printf("string table full size: %ld: uncompressed\n",
+ stp->st_fullstrsize);
+ return;
+ }
+
+ (void) printf("string table full size: %ld compressed down to: %ld\n\n",
+ stp->st_fullstrsize, stp->st_strsize);
+ (void) printf("string table compression information [%d buckets]:\n",
+ stp->st_hbckcnt);
+
+ for (cnt = 0; cnt < stp->st_hbckcnt; cnt++) {
+ Str_hash *sthash = stp->st_hashbcks[cnt];
+
+ if (sthash == 0)
+ continue;
+
+ (void) printf(" bucket: [%d]\n", cnt);
+
+ while (sthash) {
+ size_t stroff = sthash->hi_mstr->sm_strlen -
+ sthash->hi_strlen;
+
+ if (stroff == 0) {
+ (void) printf(" [%ld]: '%s' <master>\n",
+ sthash->hi_refcnt, sthash->hi_mstr->sm_str);
+ } else {
+ (void) printf(" [%ld]: '%s' <suffix of: "
+ "'%s'>\n", sthash->hi_refcnt,
+ &sthash->hi_mstr->sm_str[stroff],
+ sthash->hi_mstr->sm_str);
+ }
+ sthash = sthash->hi_next;
+ }
+ }
+}
+
+/*
+ * Initialize the message definition header file stream.
+ */
+static int
+init_defs(void)
+{
+ static char guard[FILENAME_MAX + 6];
+ char *optr;
+ const char *iptr, *_ptr;
+
+ /*
+ * Establish a header guard name using the files basename.
+ */
+ for (iptr = 0, _ptr = fldefs; _ptr && (*_ptr != '\0'); _ptr++) {
+ if (*_ptr == '/')
+ iptr = _ptr + 1;
+ }
+ if (iptr == 0)
+ iptr = fldefs;
+
+ optr = guard;
+ for (*optr++ = '_'; iptr && (*iptr != '\0'); iptr++, optr++) {
+ if (*iptr == '.') {
+ *optr++ = '_';
+ *optr++ = 'D';
+ *optr++ = 'O';
+ *optr++ = 'T';
+ *optr = '_';
+ } else
+ *optr = toupper(*iptr);
+ }
+
+ if (fprintf(fddefs, "#ifndef\t%s\n#define\t%s\n\n", guard, guard) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ if (fprintf(fddefs, "#ifndef\t__lint\n\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ /*
+ * add "typedef int Msg;"
+ */
+ if (fprintf(fddefs, "typedef int\tMsg;\n\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ /*
+ * If the associated data array is global define a prototype.
+ * Define a macro to access the array elements.
+ */
+ if (lflag == 0) {
+ if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
+ interface) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs,
+ strerror(errno));
+ return (1);
+ }
+ }
+ if (fprintf(fddefs, "#define\tMSG_ORIG(x)\t&__%s[x]\n\n",
+ interface) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ /*
+ * Generate a prototype to access the associated data array.
+ */
+ if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
+ interface) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+ if (fprintf(fddefs, "#define\tMSG_INTL(x)\t_%s(x)\n\n",
+ interface) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ return (0);
+}
+
+
+/*
+ * Finish the message definition header file.
+ */
+static int
+fini_defs(void)
+{
+ if (fprintf(fddefs, "\n#else\t/* __lint */\n\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ /*
+ * When __lint is defined, Msg is a char *. This allows lint to
+ * check our format strings against it's arguments.
+ */
+ if (fprintf(fddefs, "\ntypedef char *\tMsg;\n\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ if (fprintf(fddefs, "extern\tconst char *\t_%s(Msg);\n\n",
+ interface) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ if (lflag == 0) {
+ if (fprintf(fddefs, "extern\tconst char\t__%s[];\n\n",
+ interface) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs,
+ strerror(errno));
+ return (1);
+ }
+ }
+
+ if (fprintf(fddefs,
+ "#define MSG_ORIG(x)\tx\n#define MSG_INTL(x)\tx\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ /*
+ * Copy the temporary lint defs file into the new header.
+ */
+ if (fdlint) {
+ long size;
+ char *buf;
+
+ size = ftell(fdlint);
+ (void) rewind(fdlint);
+
+ if ((buf = malloc(size)) == 0) {
+ (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
+ return (1);
+ }
+ if (fread(buf, size, 1, fdlint) == 0) {
+ (void) fprintf(stderr, Errmsg_read, fllint,
+ strerror(errno));
+ return (1);
+ }
+ if (fwrite(buf, size, 1, fddefs) == 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs,
+ strerror(errno));
+ return (1);
+ }
+ (void) free(buf);
+ }
+
+ if (fprintf(fddefs, "\n#endif\t/* __lint */\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ if (fprintf(fddefs, "\n#endif\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldefs, strerror(errno));
+ return (1);
+ }
+
+ return (0);
+}
+
+/*
+ * The entire messaging file has been scanned - and all strings have been
+ * inserted into the string_table. We can now walk the message queue
+ * and create the '#define <DEFN>' for each string - with the strings
+ * assigned offset into the string_table.
+ */
+static int
+output_defs(void)
+{
+ msg_string *msg;
+ size_t stbufsize;
+ char *stbuf;
+
+ stbufsize = st_getstrtab_sz(stp);
+ if ((stbuf = malloc(stbufsize)) == 0) {
+ (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
+ exit(1);
+ }
+ (void) st_setstrbuf(stp, stbuf, stbufsize);
+ for (msg = msg_head; msg; msg = msg->ms_next) {
+ size_t stoff;
+ if ((st_setstring(stp, msg->ms_message, &stoff)) == -1) {
+ (void) fprintf(stderr, Errmsg_mnfn, msg->ms_message);
+ return (1);
+ }
+ if (fprintf(fddefs, "\n#define\t%s\t%ld\n",
+ msg->ms_defn, stoff) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ fldefs, strerror(errno));
+ return (1);
+ }
+ if (fddefs && fprintf(fddefs, "#define\t%s_SIZE\t%d\n",
+ msg->ms_defn, strlen(msg->ms_message)) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ fldefs, strerror(errno));
+ return (1);
+ }
+ }
+ return (0);
+}
+
+
+/*
+ * Finish off the data structure definition.
+ */
+static int
+output_data(void)
+{
+ size_t stbufsize;
+ size_t ndx;
+ size_t column = 1;
+ const char *stbuf;
+ const char *fmtstr;
+
+ stbufsize = st_getstrtab_sz(stp);
+ stbuf = st_getstrbuf(stp);
+
+ assert(stbuf);
+
+ /*
+ * Determine from the local flag whether the data declaration should
+ * be static.
+ */
+ if (lflag)
+ fmtstr = (const char *)"static const";
+ else
+ fmtstr = (const char *)"const";
+
+ if (fprintf(fddata, "\n%s char __%s[%ld] = { ",
+ fmtstr, interface, stbufsize) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
+ return (1);
+ }
+
+ for (ndx = 0; ndx < (stbufsize - 1); ndx++) {
+ if (column == 1) {
+ if (fddata && fprintf(fddata,
+ "\n/* %4ld */ 0x%.2x,", ndx,
+ (unsigned char)stbuf[ndx]) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ fldata, strerror(errno));
+ return (1);
+ }
+ } else {
+ if (fddata && fprintf(fddata, " 0x%.2x,",
+ (unsigned char)stbuf[ndx]) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ fldata, strerror(errno));
+ return (1);
+ }
+ }
+
+ if (column++ == 10)
+ column = 1;
+ }
+
+ if (column == 1)
+ fmtstr = "\n\t0x%.2x };\n";
+ else
+ fmtstr = " 0x%.2x };\n";
+
+ if (fprintf(fddata, fmtstr, (unsigned char)stbuf[stbufsize - 1]) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, fldata, strerror(errno));
+ return (1);
+ }
+
+ return (0);
+}
+
+static int
+file()
+{
+ char buffer[LINE_MAX], * token;
+ uint_t bufsize;
+ char *token_buffer;
+ int escape = 0;
+
+ if ((token_buffer = malloc(LINE_MAX)) == 0) {
+ (void) fprintf(stderr, Errmsg_nmem, strerror(errno));
+ return (1);
+ }
+ bufsize = LINE_MAX;
+
+ line = 1;
+
+ while ((token = fgets(buffer, LINE_MAX, fddesc)) != NULL) {
+ char defn[PATH_MAX], * _defn, * str;
+ int len;
+
+ switch (*token) {
+ case '#':
+ case '$':
+ if (escape) {
+ (void) fprintf(stderr, Errmsg_malt, fldesc,
+ line);
+ return (1);
+ }
+
+ /*
+ * If a msgid has been output a msgstr must follow
+ * before we digest the new token. A msgid is only set
+ * if fdmsgs is in use.
+ */
+ if (msgid) {
+ msgid = 0;
+ if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ flmsgs, strerror(errno));
+ return (1);
+ }
+ }
+
+ /*
+ * Pass lines directly through to the output message
+ * file.
+ */
+ if (fdmsgs && (prtmsgs == 1)) {
+ char comment;
+
+ if (cflag == 0)
+ comment = '#';
+ else
+ comment = '$';
+
+ if (fprintf(fdmsgs, "%c%s", comment,
+ ++token) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ flmsgs, strerror(errno));
+ return (1);
+ }
+ }
+ break;
+
+ case '@':
+ if (escape) {
+ (void) fprintf(stderr, Errmsg_malt, fldesc,
+ line);
+ return (1);
+ }
+
+ /*
+ * If a msgid has been output a msgstr must follow
+ * before we digest the new token.
+ */
+ if (msgid) {
+ msgid = 0;
+ if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ flmsgs, strerror(errno));
+ return (1);
+ }
+ }
+
+ /*
+ * Determine whether we have one or more tokens.
+ */
+ token++;
+ while (isspace(*token)) /* rid any whitespace */
+ token++;
+ _defn = token; /* definition start */
+ while (!(isspace(*token)))
+ token++;
+ *token++ = 0;
+
+ while (isspace(*token)) /* rid any whitespace */
+ token++;
+
+ /*
+ * Determine whether the single token is one of the
+ * reserved message output delimiters otherwise
+ * translate it as a message identifier.
+ */
+ if (*token == 0) {
+ if (strcmp(_defn, start) == 0)
+ prtmsgs = 1;
+ else if (strcmp(_defn, end) == 0)
+ prtmsgs = -1;
+ else if (getmesgid(_defn) == 1)
+ return (1);
+ break;
+ }
+
+ /*
+ * Multiple tokens are translated by taking the first
+ * token as the message definition, and the rest of the
+ * line as the message itself. A message line ending
+ * with an escape ('\') is expected to be continued on
+ * the next line.
+ */
+ if (prtmsgs != -1)
+ prtmsgs = 1;
+ if (fdmsgs && (prtmsgs == 1)) {
+ /*
+ * For catgets(3c) make sure a message
+ * identifier has been established (this is
+ * normally a domain for gettext(3i), but for
+ * sgsmsg use this could be argued as being
+ * redundent). Also make sure that the message
+ * definitions haven't exceeeded the maximum
+ * value allowed by gencat(1) before generating
+ * any message file entries.
+ */
+ if (cflag == 1) {
+ if (setid == 0) {
+ (void) fprintf(stderr, "file "
+ "%s: no message identifier "
+ "has been established\n",
+ fldesc);
+ return (1);
+ }
+ if (ptr > NL_MSGMAX) {
+ (void) fprintf(stderr, "file "
+ "%s: message definition "
+ "(%d) exceeds allowable "
+ "limit (NL_MSGMAX)\n",
+ fldesc, ptr);
+ return (1);
+ }
+ }
+
+ /*
+ * For catgets(3c) write the definition and the
+ * message string to the message file. For
+ * gettext(3i) write the message string as a
+ * mesgid - indicate a mesgid has been output
+ * so that a msgstr can follow.
+ */
+ if (cflag == 1) {
+ if (fprintf(fdmsgs, "%d\t%s", ptr,
+ token) < 0) {
+ (void) fprintf(stderr,
+ Errmsg_wrte, flmsgs,
+ strerror(errno));
+ return (1);
+ }
+ } else {
+ if (fprintf(fdmsgs, "msgid\t\"") < 0) {
+ (void) fprintf(stderr,
+ Errmsg_wrte, flmsgs,
+ strerror(errno));
+ return (1);
+ }
+ msgid = 1;
+ }
+ }
+
+ /*
+ * The message itself is a quoted string as this makes
+ * embedding spaces at the start (or the end) of the
+ * string very easy.
+ */
+ if (*token != '"') {
+ (void) fprintf(stderr, Errmsg_malt, fldesc,
+ line);
+ return (1);
+ }
+
+ (void) strcpy(defn, _defn);
+
+ /*
+ * Write the tag to the lint definitions.
+ */
+ if (fdlint) {
+ if (fprintf(fdlint, "\n#define\t%s\t",
+ _defn) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ fllint, strerror(errno));
+ return (1);
+ }
+ }
+
+ len = 0;
+
+ /*
+ * Write each character of the message string to the
+ * data array. Translate any escaped characters - use
+ * the same specially recognized characters as defined
+ * by gencat(1).
+ */
+message:
+ if (*token == '"') {
+ if (fdlint &&
+ (fprintf(fdlint, "%c", *token) < 0)) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ fllint, strerror(errno));
+ return (1);
+ }
+ token++;
+ }
+ while (*token) {
+ char _token;
+
+ if ((*token == '\\') && (escape == 0)) {
+ escape = 1;
+ if (fdlint && (*(token + 1) != '\n') &&
+ fprintf(fdlint, "%c", *token) < 0) {
+ (void) fprintf(stderr,
+ Errmsg_wrte, fllint,
+ strerror(errno));
+ return (1);
+ }
+ token++;
+ continue;
+ }
+ if (escape) {
+ if (*token == 'n')
+ _token = '\n';
+ else if (*token == 't')
+ _token = '\t';
+ else if (*token == 'v')
+ _token = '\v';
+ else if (*token == 'b')
+ _token = '\b';
+ else if (*token == 'f')
+ _token = '\f';
+ else if (*token == '\\')
+ _token = '\\';
+ else if (*token == '"')
+ _token = '"';
+ else if (*token == '\n')
+ break;
+ else
+ _token = *token;
+
+ if (fdmsgs && (prtmsgs == 1) &&
+ (fprintf(fdmsgs, "\\") < 0)) {
+ (void) fprintf(stderr,
+ Errmsg_wrte, flmsgs,
+ strerror(errno));
+ return (1);
+ }
+ } else {
+ /*
+ * If this is the trailing quote then
+ * thats the last of the message string.
+ * Eat up any remaining white space and
+ * unless an escape character is found
+ * terminate the data string with a 0.
+ */
+ /* BEGIN CSTYLED */
+ if (*token == '"') {
+ if (fdlint && (fprintf(fdlint,
+ "%c", *token) < 0)) {
+ (void) fprintf(stderr,
+ Errmsg_wrte, fllint,
+ strerror(errno));
+ return (1);
+ }
+
+ if (fdmsgs && (prtmsgs == 1) &&
+ (fprintf(fdmsgs, "%c",
+ *token) < 0)) {
+ (void) fprintf(stderr,
+ Errmsg_wrte, flmsgs,
+ strerror(errno));
+ return (1);
+ }
+
+ while (*++token) {
+ if (*token == '\n')
+ break;
+ }
+ _token = '\0';
+ } else
+ _token = *token;
+ /* END CSTYLED */
+ }
+
+ if (fdmsgs && (prtmsgs == 1) &&
+ (fprintf(fdmsgs, "%c", *token) < 0)) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ flmsgs, strerror(errno));
+ return (1);
+ }
+
+ if (fdlint && fprintf(fdlint,
+ "%c", *token) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ fllint, strerror(errno));
+ return (1);
+ }
+
+ if (len >= bufsize) {
+ bufsize += LINE_MAX;
+ if ((token_buffer = realloc(
+ token_buffer, bufsize)) == 0) {
+ (void) fprintf(stderr,
+ Errmsg_nmem,
+ strerror(errno));
+ return (1);
+ }
+ }
+ token_buffer[len] = _token;
+ ptr++, token++, len++;
+ escape = 0;
+
+ if (_token == '\0')
+ break;
+ }
+
+ /*
+ * After the complete message string has been processed
+ * (including its continuation beyond one line), create
+ * a string size definition.
+ */
+ if (escape == 0) {
+ const char *form = "#define\t%s_SIZE\t%d\n";
+
+ token_buffer[len] = '\0';
+
+ message_append(defn, token_buffer);
+
+ if (fdlint && fprintf(fdlint, form, defn,
+ (len - 1)) < 0) {
+ (void) fprintf(stderr, Errmsg_wrte,
+ fllint, strerror(errno));
+ return (1);
+ }
+ }
+ break;
+
+ default:
+ /*
+ * Empty lines are passed through to the message file.
+ */
+ while (isspace(*token))
+ token++;
+
+ if (*token == 0) {
+ if (msgid || (fdmsgs && (prtmsgs == 1))) {
+ /*
+ * If a msgid has been output a msgstr
+ * must follow before we digest the new
+ * token.
+ */
+ if (msgid) {
+ msgid = 0;
+ str = "msgstr\t\"\"\n\n";
+ } else
+ str = "\n";
+
+ if (fprintf(fdmsgs, str) < 0) {
+ (void) fprintf(stderr,
+ Errmsg_wrte, flmsgs,
+ strerror(errno));
+ return (1);
+ }
+ }
+ break;
+ }
+
+ /*
+ * If an escape is in effect then any tokens are taken
+ * to be message continuations.
+ */
+ if (escape) {
+ escape = 0;
+ goto message;
+ }
+
+ (void) fprintf(stderr, "file %s: line %d: invalid "
+ "input does not start with #, $ or @\n", fldesc,
+ line);
+ return (1);
+ }
+ line++;
+ }
+
+ free(token_buffer);
+
+ return (0);
+}
+
+int
+main(int argc, char ** argv)
+{
+ opterr = 0;
+ while ((line = getopt(argc, argv, "cd:h:lm:n:i:v")) != EOF) {
+ switch (line) {
+ case 'c': /* catgets instead of gettext */
+ cflag = 1;
+ break;
+ case 'd': /* new message data filename */
+ fldata = optarg; /* (msg.c is default) */
+ break;
+ case 'h': /* new message defs filename */
+ fldefs = optarg; /* (msg.h is default) */
+ break;
+ case 'i': /* input message ids from */
+ flmids = optarg; /* from this file */
+ break;
+ case 'l': /* define message data arrays */
+ lflag = 1; /* to be local (static) */
+ break;
+ case 'm': /* generate message database */
+ flmsgs = optarg; /* to this file */
+ break;
+ case 'n': /* new data array and func */
+ interface = optarg; /* name (msg is default) */
+ break;
+ case 'v':
+ vflag = 1; /* set verbose flag */
+ break;
+ case '?':
+ (void) fprintf(stderr, Errmsg_use, argv[0]);
+ exit(1);
+ default:
+ break;
+ }
+ }
+
+ /*
+ * Validate the we have been given at least one input file.
+ */
+ if ((argc - optind) < 1) {
+ (void) fprintf(stderr, Errmsg_use);
+ exit(1);
+ }
+
+ /*
+ * Open all the required output files.
+ */
+ if (fldefs) {
+ if ((fddefs = fopen(fldefs, "w+")) == NULL) {
+ (void) fprintf(stderr, Errmsg_opne, fldefs,
+ strerror(errno));
+ return (1);
+ }
+ }
+ if (fldata) {
+ if (fldefs && (strcmp(fldefs, fldata) == 0))
+ fddata = fddefs;
+ else if ((fddata = fopen(fldata, "w+")) == NULL) {
+ (void) fprintf(stderr, Errmsg_opne, fldata,
+ strerror(errno));
+ return (1);
+ }
+ }
+ if (fddefs && fddata) {
+ (void) sprintf(fllint, "%s.%d", nmlint, (int)getpid());
+ if ((fdlint = fopen(fllint, "w+")) == NULL) {
+ (void) fprintf(stderr, Errmsg_opne, fllint,
+ strerror(errno));
+ return (1);
+ }
+ }
+ if (flmsgs) {
+ if ((fdmsgs = fopen(flmsgs, "w+")) == NULL) {
+ (void) fprintf(stderr, Errmsg_opne, flmsgs,
+ strerror(errno));
+ return (1);
+ }
+ }
+ if (flmids) {
+ if ((fdmids = fopen(flmids, "r")) == NULL) {
+ (void) fprintf(stderr, Errmsg_opne, flmids,
+ strerror(errno));
+ return (1);
+ }
+ }
+
+
+ /*
+ * Initialize the message definition and message data streams.
+ */
+ if (fddefs) {
+ if (init_defs())
+ return (1);
+ }
+
+ /*
+ * Read the input message file, and for each line process accordingly.
+ */
+ for (; optind < argc; optind++) {
+ int err;
+
+ fldesc = argv[optind];
+
+ if ((fddesc = fopen(fldesc, "r")) == NULL) {
+ (void) fprintf(stderr, Errmsg_opne, fldesc,
+ strerror(errno));
+ return (1);
+ }
+ err = file();
+ (void) fclose(fddesc);
+
+ if (err != 0)
+ return (1);
+ }
+
+ /*
+ * If a msgid has been output a msgstr must follow before we end the
+ * file.
+ */
+ if (msgid) {
+ msgid = 0;
+ if (fprintf(fdmsgs, "msgstr\t\"\"\n") < 0) {
+ (void) fprintf(stderr, Errmsg_wrte, flmsgs,
+ strerror(errno));
+ return (1);
+ }
+ }
+
+ if (fdmids)
+ (void) fclose(fdmids);
+ if (fdmsgs)
+ (void) fclose(fdmsgs);
+
+ if (fddefs) {
+ if (output_defs())
+ return (1);
+ }
+
+ /*
+ * Finish off any generated data and header file.
+ */
+ if (fldata) {
+ if (output_data())
+ return (1);
+ }
+ if (fddefs) {
+ if (fini_defs())
+ return (1);
+ }
+
+ if (vflag)
+ dump_stringtab(stp);
+
+ /*
+ * Close up everything and go home.
+ */
+ if (fddata)
+ (void) fclose(fddata);
+ if (fddefs && (fddefs != fddata))
+ (void) fclose(fddefs);
+ if (fddefs && fddata) {
+ (void) fclose(fdlint);
+ (void) unlink(fllint);
+ }
+
+ if (stp)
+ st_destroy(stp);
+
+ return (0);
+}
diff --git a/cddl/contrib/opensolaris/cmd/sgs/tools/common/string_table.c b/cddl/contrib/opensolaris/cmd/sgs/tools/common/string_table.c
new file mode 100644
index 000000000000..e174acaf0419
--- /dev/null
+++ b/cddl/contrib/opensolaris/cmd/sgs/tools/common/string_table.c
@@ -0,0 +1,685 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <_string_table.h>
+#include <strings.h>
+#include <sgs.h>
+#include <stdio.h>
+
+/*
+ * This file provides the interfaces to build a Str_tbl suitable for use by
+ * either the sgsmsg message system, or a standard ELF string table (SHT_STRTAB)
+ * as created by ld(1).
+ *
+ * There are two modes which can be used when constructing a string table:
+ *
+ * st_new(0)
+ * standard string table - no compression. This is the
+ * traditional, fast method.
+ *
+ * st_new(FLG_STTAB_COMPRESS)
+ * builds a compressed string table which both eliminates
+ * duplicate strings, and permits strings with common suffixes
+ * (atexit vs. exit) to overlap in the table. This provides space
+ * savings for many string tables. Although more work than the
+ * traditional method, the algorithms used are designed to scale
+ * and keep any overhead at a minimum.
+ *
+ * These string tables are built with a common interface in a two-pass manner.
+ * The first pass finds all of the strings required for the string-table and
+ * calculates the size required for the final string table.
+ *
+ * The second pass allocates the string table, populates the strings into the
+ * table and returns the offsets the strings have been assigned.
+ *
+ * The calling sequence to build and populate a string table is:
+ *
+ * st_new(); // initialize strtab
+ *
+ * st_insert(st1); // first pass of strings ...
+ * // calculates size required for
+ * // string table
+ *
+ * st_delstring(st?); // remove string previously
+ * // inserted
+ * st_insert(stN);
+ *
+ * st_getstrtab_sz(); // freezes strtab and computes
+ * // size of table.
+ *
+ * st_setstrbuf(); // associates a final destination
+ * // for the string table
+ *
+ * st_setstring(st1); // populate the string table
+ * ... // offsets are based off of second
+ * // pass through the string table
+ * st_setstring(stN);
+ *
+ * st_destroy(); // tear down string table
+ * // structures.
+ *
+ * String Suffix Compression Algorithm:
+ *
+ * Here's a quick high level overview of the Suffix String
+ * compression algorithm used. First - the heart of the algorithm
+ * is a Hash table list which represents a dictionary of all unique
+ * strings inserted into the string table. The hash function for
+ * this table is a standard string hash except that the hash starts
+ * at the last character in the string (&str[n - 1]) and works towards
+ * the first character in the function (&str[0]). As we compute the
+ * HASH value for a given string, we also compute the hash values
+ * for all of the possible suffix strings for that string.
+ *
+ * As we compute the hash - at each character see if the current
+ * suffix string for that hash is already present in the table. If
+ * it is, and the string is a master string. Then change that
+ * string to a suffix string of the new string being inserted.
+ *
+ * When the final hash value is found (hash for str[0...n]), check
+ * to see if it is in the hash table - if so increment the reference
+ * count for the string. If it is not yet in the table, insert a
+ * new hash table entry for a master string.
+ *
+ * The above method will find all suffixes of a given string given
+ * that the strings are inserted from shortest to longest. That is
+ * why this is a two phase method, we first collect all of the
+ * strings and store them based off of their length in an AVL tree.
+ * Once all of the strings have been submitted we then start the
+ * hash table build by traversing the AVL tree in order and
+ * inserting the strings from shortest to longest as described
+ * above.
+ */
+
+/* LINTLIBRARY */
+
+static int
+avl_len_compare(const void *n1, const void *n2)
+{
+ size_t len1, len2;
+
+ len1 = ((LenNode *)n1)->ln_strlen;
+ len2 = ((LenNode *)n2)->ln_strlen;
+
+ if (len1 == len2)
+ return (0);
+ if (len2 < len1)
+ return (1);
+ return (-1);
+}
+
+static int
+avl_str_compare(const void *n1, const void *n2)
+{
+ const char *str1, *str2;
+ int rc;
+
+ str1 = ((StrNode *)n1)->sn_str;
+ str2 = ((StrNode *)n2)->sn_str;
+
+ rc = strcmp(str1, str2);
+ if (rc > 0)
+ return (1);
+ if (rc < 0)
+ return (-1);
+ return (0);
+}
+
+/*
+ * Return an initialized Str_tbl - returns NULL on failure.
+ *
+ * flags:
+ * FLG_STTAB_COMPRESS - build a compressed string table
+ */
+Str_tbl *
+st_new(uint_t flags)
+{
+ Str_tbl *stp;
+
+ if ((stp = calloc(sizeof (Str_tbl), 1)) == NULL)
+ return (NULL);
+
+ /*
+ * Start with a leading '\0' - it's tradition.
+ */
+ stp->st_strsize = stp->st_fullstrsize = stp->st_nextoff = 1;
+
+ /*
+ * Do we compress this string table?
+ */
+ stp->st_flags = flags;
+ if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0)
+ return (stp);
+
+ if ((stp->st_lentree = calloc(sizeof (avl_tree_t), 1)) == NULL)
+ return (NULL);
+
+ avl_create(stp->st_lentree, &avl_len_compare, sizeof (LenNode),
+ SGSOFFSETOF(LenNode, ln_avlnode));
+
+ return (stp);
+}
+
+/*
+ * Insert a new string into the Str_tbl. There are two AVL trees used.
+ *
+ * . The first LenNode AVL tree maintains a tree of nodes based on string
+ * sizes.
+ * . Each LenNode maintains a StrNode AVL tree for each string. Large
+ * applications have been known to contribute thousands of strings of
+ * the same size. Should strings need to be removed (-z ignore), then
+ * the string AVL tree makes this removal efficient and scalable.
+ */
+int
+st_insert(Str_tbl *stp, const char *str)
+{
+ size_t len;
+ StrNode *snp, sn = { 0 };
+ LenNode *lnp, ln = { 0 };
+ avl_index_t where;
+
+ /*
+ * String table can't have been cooked
+ */
+ assert((stp->st_flags & FLG_STTAB_COOKED) == 0);
+
+ /*
+ * Null strings always point to the head of the string
+ * table - no reason to keep searching.
+ */
+ if ((len = strlen(str)) == 0)
+ return (0);
+
+ stp->st_fullstrsize += len + 1;
+ stp->st_strcnt++;
+
+ if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0)
+ return (0);
+
+ /*
+ * From the controlling string table, determine which LenNode AVL node
+ * provides for this string length. If the node doesn't exist, insert
+ * a new node to represent this string length.
+ */
+ ln.ln_strlen = len;
+ if ((lnp = avl_find(stp->st_lentree, &ln, &where)) == NULL) {
+ if ((lnp = calloc(sizeof (LenNode), 1)) == NULL)
+ return (-1);
+ lnp->ln_strlen = len;
+ avl_insert(stp->st_lentree, lnp, where);
+
+ if ((lnp->ln_strtree = calloc(sizeof (avl_tree_t), 1)) == NULL)
+ return (0);
+
+ avl_create(lnp->ln_strtree, &avl_str_compare, sizeof (StrNode),
+ SGSOFFSETOF(StrNode, sn_avlnode));
+ }
+
+ /*
+ * From the string length AVL node determine whether a StrNode AVL node
+ * provides this string. If the node doesn't exist, insert a new node
+ * to represent this string.
+ */
+ sn.sn_str = str;
+ if ((snp = avl_find(lnp->ln_strtree, &sn, &where)) == NULL) {
+ if ((snp = calloc(sizeof (StrNode), 1)) == NULL)
+ return (-1);
+ snp->sn_str = str;
+ avl_insert(lnp->ln_strtree, snp, where);
+ }
+ snp->sn_refcnt++;
+
+ return (0);
+}
+
+/*
+ * Remove a previously inserted string from the Str_tbl.
+ */
+int
+st_delstring(Str_tbl *stp, const char *str)
+{
+ size_t len;
+ LenNode *lnp, ln = { 0 };
+ StrNode *snp, sn = { 0 };
+
+ /*
+ * String table can't have been cooked
+ */
+ assert((stp->st_flags & FLG_STTAB_COOKED) == 0);
+
+ len = strlen(str);
+ stp->st_fullstrsize -= len + 1;
+
+ if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0)
+ return (0);
+
+ /*
+ * Determine which LenNode AVL node provides for this string length.
+ */
+ ln.ln_strlen = len;
+ if ((lnp = avl_find(stp->st_lentree, &ln, 0)) != NULL) {
+ sn.sn_str = str;
+ if ((snp = avl_find(lnp->ln_strtree, &sn, 0)) != NULL) {
+ /*
+ * Reduce the reference count, and if zero remove the
+ * node.
+ */
+ if (--snp->sn_refcnt == 0)
+ avl_remove(lnp->ln_strtree, snp);
+ return (0);
+ }
+ }
+
+ /*
+ * No strings of this length, or no string itself - someone goofed.
+ */
+ return (-1);
+}
+
+/*
+ * Tear down a String_Table structure.
+ */
+void
+st_destroy(Str_tbl *stp)
+{
+ Str_hash *sthash, *psthash;
+ Str_master *mstr, *pmstr;
+ uint_t i;
+
+ /*
+ * cleanup the master strings
+ */
+ for (mstr = stp->st_mstrlist, pmstr = 0; mstr;
+ mstr = mstr->sm_next) {
+ if (pmstr)
+ free(pmstr);
+ pmstr = mstr;
+ }
+ if (pmstr)
+ free(pmstr);
+
+ if (stp->st_hashbcks) {
+ for (i = 0; i < stp->st_hbckcnt; i++) {
+ for (sthash = stp->st_hashbcks[i], psthash = 0;
+ sthash; sthash = sthash->hi_next) {
+ if (psthash)
+ free(psthash);
+ psthash = sthash;
+ }
+ if (psthash)
+ free(psthash);
+ }
+ free(stp->st_hashbcks);
+ }
+ free(stp);
+}
+
+
+/*
+ * For a given string - copy it into the buffer associated with
+ * the string table - and return the offset it has been assigned.
+ *
+ * If a value of '-1' is returned - the string was not found in
+ * the Str_tbl.
+ */
+int
+st_setstring(Str_tbl *stp, const char *str, size_t *stoff)
+{
+ size_t stlen;
+ uint_t hashval;
+ Str_hash *sthash;
+ Str_master *mstr;
+ int i;
+
+ /*
+ * String table *must* have been previously cooked
+ */
+ assert(stp->st_strbuf);
+
+ assert(stp->st_flags & FLG_STTAB_COOKED);
+ stlen = strlen(str);
+ /*
+ * Null string always points to head of string table
+ */
+ if (stlen == 0) {
+ *stoff = 0;
+ return (0);
+ }
+
+ if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0) {
+ size_t _stoff;
+
+ stlen++; /* count for trailing '\0' */
+ _stoff = stp->st_nextoff;
+ /*
+ * Have we overflowed our assigned buffer?
+ */
+ if ((_stoff + stlen) > stp->st_fullstrsize)
+ return (-1);
+ memcpy(stp->st_strbuf + _stoff, str, stlen);
+ *stoff = _stoff;
+ stp->st_nextoff += stlen;
+ return (0);
+ }
+
+ /*
+ * Calculate reverse hash for string.
+ */
+ hashval = HASHSEED;
+ for (i = stlen; i >= 0; i--) {
+ hashval = ((hashval << 5) + hashval) +
+ str[i]; /* h = ((h * 33) + c) */
+ }
+
+ for (sthash = stp->st_hashbcks[hashval % stp->st_hbckcnt]; sthash;
+ sthash = sthash->hi_next) {
+ const char *hstr;
+
+ if (sthash->hi_hashval != hashval)
+ continue;
+
+ hstr = &sthash->hi_mstr->sm_str[sthash->hi_mstr->sm_strlen -
+ sthash->hi_strlen];
+ if (strcmp(str, hstr) == 0)
+ break;
+ }
+
+ /*
+ * Did we find the string?
+ */
+ if (sthash == 0)
+ return (-1);
+
+ /*
+ * Has this string been copied into the string table?
+ */
+ mstr = sthash->hi_mstr;
+ if (mstr->sm_stroff == 0) {
+ size_t mstrlen = mstr->sm_strlen + 1;
+
+ mstr->sm_stroff = stp->st_nextoff;
+
+ /*
+ * Have we overflowed our assigned buffer?
+ */
+ if ((mstr->sm_stroff + mstrlen) > stp->st_fullstrsize)
+ return (-1);
+
+ (void) memcpy(stp->st_strbuf + mstr->sm_stroff,
+ mstr->sm_str, mstrlen);
+ stp->st_nextoff += mstrlen;
+ }
+
+ /*
+ * Calculate offset of (sub)string.
+ */
+ *stoff = mstr->sm_stroff + mstr->sm_strlen - sthash->hi_strlen;
+
+ return (0);
+}
+
+
+static int
+st_hash_insert(Str_tbl *stp, const char *str, size_t len)
+{
+ int i;
+ uint_t hashval = HASHSEED;
+ uint_t bckcnt = stp->st_hbckcnt;
+ Str_hash **hashbcks = stp->st_hashbcks;
+ Str_hash *sthash;
+ Str_master *mstr = 0;
+
+ /*
+ * We use a classic 'Bernstein k=33' hash function. But
+ * instead of hashing from the start of the string to the
+ * end, we do it in reverse.
+ *
+ * This way - we are essentially building all of the
+ * suffix hashvalues as we go. We can check to see if
+ * any suffixes already exist in the tree as we generate
+ * the hash.
+ */
+ for (i = len; i >= 0; i--) {
+ hashval = ((hashval << 5) + hashval) +
+ str[i]; /* h = ((h * 33) + c) */
+
+ for (sthash = hashbcks[hashval % bckcnt];
+ sthash; sthash = sthash->hi_next) {
+ const char *hstr;
+ Str_master *_mstr;
+
+ if (sthash->hi_hashval != hashval)
+ continue;
+
+ _mstr = sthash->hi_mstr;
+ hstr = &_mstr->sm_str[_mstr->sm_strlen -
+ sthash->hi_strlen];
+
+ if (strcmp(&str[i], hstr))
+ continue;
+
+ if (i == 0) {
+ /*
+ * Entry already in table, increment refcnt and
+ * get out.
+ */
+ sthash->hi_refcnt++;
+ return (0);
+ } else {
+ /*
+ * If this 'suffix' is presently a 'master
+ * string, then take over it's record.
+ */
+ if (sthash->hi_strlen == _mstr->sm_strlen) {
+ /*
+ * we should only do this once.
+ */
+ assert(mstr == 0);
+ mstr = _mstr;
+ }
+ }
+ }
+ }
+
+ /*
+ * Do we need a new master string, or can we take over
+ * one we already found in the table?
+ */
+ if (mstr == 0) {
+ /*
+ * allocate a new master string
+ */
+ if ((mstr = calloc(sizeof (Str_hash), 1)) == 0)
+ return (-1);
+ mstr->sm_next = stp->st_mstrlist;
+ stp->st_mstrlist = mstr;
+ stp->st_strsize += len + 1;
+ } else {
+ /*
+ * We are taking over a existing master string, the string size
+ * only increments by the difference between the current string
+ * and the previous master.
+ */
+ assert(len > mstr->sm_strlen);
+ stp->st_strsize += len - mstr->sm_strlen;
+ }
+
+ if ((sthash = calloc(sizeof (Str_hash), 1)) == 0)
+ return (-1);
+
+ mstr->sm_hashval = sthash->hi_hashval = hashval;
+ mstr->sm_strlen = sthash->hi_strlen = len;
+ mstr->sm_str = str;
+ sthash->hi_refcnt = 1;
+ sthash->hi_mstr = mstr;
+
+ /*
+ * Insert string element into head of hash list
+ */
+ hashval = hashval % bckcnt;
+ sthash->hi_next = hashbcks[hashval];
+ hashbcks[hashval] = sthash;
+ return (0);
+}
+
+/*
+ * Return amount of space required for the string table.
+ */
+size_t
+st_getstrtab_sz(Str_tbl *stp)
+{
+ assert(stp->st_fullstrsize > 0);
+
+ if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0) {
+ stp->st_flags |= FLG_STTAB_COOKED;
+ return (stp->st_fullstrsize);
+ }
+
+ if ((stp->st_flags & FLG_STTAB_COOKED) == 0) {
+ LenNode *lnp;
+ void *cookie;
+
+ stp->st_flags |= FLG_STTAB_COOKED;
+ /*
+ * allocate a hash table about the size of # of
+ * strings input.
+ */
+ stp->st_hbckcnt = findprime(stp->st_strcnt);
+ if ((stp->st_hashbcks =
+ calloc(sizeof (Str_hash), stp->st_hbckcnt)) == NULL)
+ return (0);
+
+ /*
+ * We now walk all of the strings in the list, from shortest to
+ * longest, and insert them into the hashtable.
+ */
+ if ((lnp = avl_first(stp->st_lentree)) == NULL) {
+ /*
+ * Is it possible we have an empty string table, if so,
+ * the table still contains '\0', so return the size.
+ */
+ if (avl_numnodes(stp->st_lentree) == 0) {
+ assert(stp->st_strsize == 1);
+ return (stp->st_strsize);
+ }
+ return (0);
+ }
+
+ while (lnp) {
+ StrNode *snp;
+
+ /*
+ * Walk the string lists and insert them into the hash
+ * list. Once a string is inserted we no longer need
+ * it's entry, so the string can be freed.
+ */
+ for (snp = avl_first(lnp->ln_strtree); snp;
+ snp = AVL_NEXT(lnp->ln_strtree, snp)) {
+ if (st_hash_insert(stp, snp->sn_str,
+ lnp->ln_strlen) == -1)
+ return (0);
+ }
+
+ /*
+ * Now that the strings have been copied, walk the
+ * StrNode tree and free all the AVL nodes. Note,
+ * avl_destroy_nodes() beats avl_remove() as the
+ * latter balances the nodes as they are removed.
+ * We just want to tear the whole thing down fast.
+ */
+ cookie = NULL;
+ while ((snp = avl_destroy_nodes(lnp->ln_strtree,
+ &cookie)) != NULL)
+ free(snp);
+ avl_destroy(lnp->ln_strtree);
+ free(lnp->ln_strtree);
+ lnp->ln_strtree = NULL;
+
+ /*
+ * Move on to the next LenNode.
+ */
+ lnp = AVL_NEXT(stp->st_lentree, lnp);
+ }
+
+ /*
+ * Now that all of the strings have been freed, walk the
+ * LenNode tree and free all of the AVL nodes. Note,
+ * avl_destroy_nodes() beats avl_remove() as the latter
+ * balances the nodes as they are removed. We just want to
+ * tear the whole thing down fast.
+ */
+ cookie = NULL;
+ while ((lnp = avl_destroy_nodes(stp->st_lentree,
+ &cookie)) != NULL)
+ free(lnp);
+ avl_destroy(stp->st_lentree);
+ free(stp->st_lentree);
+ stp->st_lentree = 0;
+ }
+
+ assert(stp->st_strsize > 0);
+ assert(stp->st_fullstrsize >= stp->st_strsize);
+
+ return (stp->st_strsize);
+}
+
+/*
+ * Associate a buffer with a string table.
+ */
+const char *
+st_getstrbuf(Str_tbl *stp)
+{
+ return (stp->st_strbuf);
+}
+
+int
+st_setstrbuf(Str_tbl *stp, char *stbuf, size_t bufsize)
+{
+ assert(stp->st_flags & FLG_STTAB_COOKED);
+
+ if ((stp->st_flags & FLG_STTAB_COMPRESS) == 0) {
+ if (bufsize < stp->st_fullstrsize)
+ return (-1);
+ } else {
+ if (bufsize < stp->st_strsize)
+ return (-1);
+ }
+
+ stp->st_strbuf = stbuf;
+#ifdef DEBUG
+ /*
+ * for debug builds - start with a stringtable filled in
+ * with '0xff'. This makes it very easy to find wholes
+ * which we failed to fill in - in the strtab.
+ */
+ memset(stbuf, 0xff, bufsize);
+ stbuf[0] = '\0';
+#else
+ memset(stbuf, 0x0, bufsize);
+#endif
+ return (0);
+}