xref: /titanic_51/usr/src/cmd/sgs/include/elfedit.h (revision 08278a5e91755ccdb5850c19d21d42fb2e16b50e)
1d29b2c44Sab196087 /*
2d29b2c44Sab196087  * CDDL HEADER START
3d29b2c44Sab196087  *
4d29b2c44Sab196087  * The contents of this file are subject to the terms of the
5d29b2c44Sab196087  * Common Development and Distribution License (the "License").
6d29b2c44Sab196087  * You may not use this file except in compliance with the License.
7d29b2c44Sab196087  *
8d29b2c44Sab196087  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9d29b2c44Sab196087  * or http://www.opensolaris.org/os/licensing.
10d29b2c44Sab196087  * See the License for the specific language governing permissions
11d29b2c44Sab196087  * and limitations under the License.
12d29b2c44Sab196087  *
13d29b2c44Sab196087  * When distributing Covered Code, include this CDDL HEADER in each
14d29b2c44Sab196087  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15d29b2c44Sab196087  * If applicable, add the following below this CDDL HEADER, with the
16d29b2c44Sab196087  * fields enclosed by brackets "[]" replaced with your own identifying
17d29b2c44Sab196087  * information: Portions Copyright [yyyy] [name of copyright owner]
18d29b2c44Sab196087  *
19d29b2c44Sab196087  * CDDL HEADER END
20d29b2c44Sab196087  */
21d29b2c44Sab196087 
22d29b2c44Sab196087 /*
2369112eddSAli Bahrami  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24d29b2c44Sab196087  * Use is subject to license terms.
25d29b2c44Sab196087  */
26d29b2c44Sab196087 
27d29b2c44Sab196087 #ifndef	_ELFEDIT_H
28d29b2c44Sab196087 #define	_ELFEDIT_H
29d29b2c44Sab196087 
30d29b2c44Sab196087 #include	<stdio.h>
31d29b2c44Sab196087 #include	<stdlib.h>
32d29b2c44Sab196087 #include	<sys/types.h>
33d29b2c44Sab196087 #include	<libelf.h>
34d29b2c44Sab196087 #include	<stdarg.h>
35d29b2c44Sab196087 
36d29b2c44Sab196087 /* The following are here to support use of elfedit_msg() */
37d29b2c44Sab196087 #include	<sys/machelf.h>		/* EC_ macros */
38d29b2c44Sab196087 #include	<libintl.h>
39d29b2c44Sab196087 
40d29b2c44Sab196087 #ifdef	__cplusplus
41d29b2c44Sab196087 extern "C" {
42d29b2c44Sab196087 #endif
43d29b2c44Sab196087 
44d29b2c44Sab196087 
45d29b2c44Sab196087 /*
46d29b2c44Sab196087  * elfedit uses elfedit_printf() to produce generic output to stdout.
47d29b2c44Sab196087  * elfedit_msg() is used to produce error message, or specific types
48d29b2c44Sab196087  * of terse informational messages:
49d29b2c44Sab196087  *
50d29b2c44Sab196087  *	ELFEDIT_MSG_ERR:
51d29b2c44Sab196087  *		Issues an error to stderr. elfedit_msg() does not return
52d29b2c44Sab196087  *		to the caller. Control returns to the outer loop in
53d29b2c44Sab196087  *		interactive use. elfedit exits in non-interactive use.
54d29b2c44Sab196087  *
55d29b2c44Sab196087  *	ELFEDIT_MSG_FATAL:
56d29b2c44Sab196087  *		Issues an error to stderr. elfedit_msg() exits the process,
57d29b2c44Sab196087  *		and does not return to the caller.
58d29b2c44Sab196087  *
59d29b2c44Sab196087  *	ELFEDIT_MSG_USAGE:
60d29b2c44Sab196087  *		Issues an elfedit usage message to stderr, and
61d29b2c44Sab196087  *		returns to the caller.
62d29b2c44Sab196087  *
63d29b2c44Sab196087  *	ELFEDIT_MSG_CMDUSAGE
64d29b2c44Sab196087  *		Issues an elfedit usage message to stderr, and
65d29b2c44Sab196087  *		does not return to the caller.
66d29b2c44Sab196087  *
67d29b2c44Sab196087  *	ELFEDIT_MSG_DEBUG
68d29b2c44Sab196087  *		If the ELFEDIT_F_DEBUG flag is set, the message
69d29b2c44Sab196087  *		is printed to stdout, otherwise no output is produced.
70d29b2c44Sab196087  *		elfedit_msg() returns to the caller.
71d29b2c44Sab196087  *
72d29b2c44Sab196087  *	ELFEDIT_MSG_QUIET
73d29b2c44Sab196087  *		This is a very special case, intended to handle the
74d29b2c44Sab196087  *		case where the pager subprocess exits before we are
75d29b2c44Sab196087  *		done producing output (the user presses 'q'). It acts
76d29b2c44Sab196087  *		just like ELFEDIT_MSG_ERR, except that no message is
77d29b2c44Sab196087  *		actually printed.
78d29b2c44Sab196087  *
79d29b2c44Sab196087  * In the cases where elfedit_msg() does not return to the caller, the
80d29b2c44Sab196087  * behavior depends on the mode of execution. If running in interactive
81d29b2c44Sab196087  * mode (reading from a tty), control is returned directly to the outer
82d29b2c44Sab196087  * elfedit control loop to read another command. If not running in interactive
83d29b2c44Sab196087  * mode, elfedit exits with a non-zero status.
84d29b2c44Sab196087  */
85d29b2c44Sab196087 typedef enum {
86d29b2c44Sab196087 	ELFEDIT_MSG_ERR = 0,
87d29b2c44Sab196087 	ELFEDIT_MSG_FATAL = 1,
88d29b2c44Sab196087 	ELFEDIT_MSG_USAGE = 2,
89d29b2c44Sab196087 	ELFEDIT_MSG_CMDUSAGE = 3,
90d29b2c44Sab196087 	ELFEDIT_MSG_DEBUG = 4,
91d29b2c44Sab196087 	ELFEDIT_MSG_QUIET = 5
92d29b2c44Sab196087 } elfedit_msg_t;
93d29b2c44Sab196087 
94d29b2c44Sab196087 
95d29b2c44Sab196087 /*
96d29b2c44Sab196087  * Information for a single ELF section.
97d29b2c44Sab196087  *
98d29b2c44Sab196087  * NOTE: sec_xshndx
99d29b2c44Sab196087  *	A symbol table can have an associated SHT_SYMTAB_SHNDX section. This
100d29b2c44Sab196087  *	happens when the number of sections is too large to fit in the
101d29b2c44Sab196087  *	ELF symbol st_shndx field, which is a 16-bit value. The sec_xshndx
102d29b2c44Sab196087  *	field will be SHN_UNDEF if there is no such section, and will be
103d29b2c44Sab196087  *	the section index of the extended section index section assocated
104d29b2c44Sab196087  *	with the symbol table otherwise.
105d29b2c44Sab196087  *
106d29b2c44Sab196087  * NOTE: sec_versym
107d29b2c44Sab196087  *	Symbol table sections can have an SHT_SUNW_VERSYM section that
108d29b2c44Sab196087  *	contains its version indices. Other types of section will have
109d29b2c44Sab196087  *	this field set to SHN_UNDEF.
110d29b2c44Sab196087  */
111d29b2c44Sab196087 typedef struct {
112d29b2c44Sab196087 	Elf32_Word	sec_shndx;	/* Section index */
113d29b2c44Sab196087 	Elf_Scn		*sec_scn;	/* Section descriptor */
114d29b2c44Sab196087 	Elf32_Shdr	*sec_shdr;	/* Section header */
115d29b2c44Sab196087 	Elf_Data	*sec_data;	/* Data region of section */
116d29b2c44Sab196087 	const char	*sec_name;	/* Name of section */
117d29b2c44Sab196087 } elfedit32_section_t;
118d29b2c44Sab196087 
119d29b2c44Sab196087 typedef struct {
120d29b2c44Sab196087 	Elf64_Word	sec_shndx;
121d29b2c44Sab196087 	Elf_Scn		*sec_scn;
122d29b2c44Sab196087 	Elf64_Shdr	*sec_shdr;
123d29b2c44Sab196087 	Elf_Data	*sec_data;
124d29b2c44Sab196087 	const char	*sec_name;
125d29b2c44Sab196087 } elfedit64_section_t;
126d29b2c44Sab196087 
127d29b2c44Sab196087 #ifdef _ELF64
128d29b2c44Sab196087 #define	elfedit_section_t	elfedit64_section_t
129d29b2c44Sab196087 #else
130d29b2c44Sab196087 #define	elfedit_section_t	elfedit32_section_t
131d29b2c44Sab196087 #endif
132d29b2c44Sab196087 
133d29b2c44Sab196087 
134d29b2c44Sab196087 /*
135d29b2c44Sab196087  * We maintain extra information for symbol tables. We look them
136d29b2c44Sab196087  * up frequently, so we want to eliminate expensive linear searches
137d29b2c44Sab196087  * of the entire section header array. Also, symbol tables usually
138d29b2c44Sab196087  * have associated parallal sections (syminfo, versym, extended indexes, etc)
139d29b2c44Sab196087  * and we want to eliminate repeated linear lookups for them, as well as
140d29b2c44Sab196087  * the basic error checking that is necessary to ensure they match the
141d29b2c44Sab196087  * symbol table they're given.
142d29b2c44Sab196087  *
143d29b2c44Sab196087  * This extra information is kept in elfedit_symtab_t structs. Each field
144d29b2c44Sab196087  * is a section index, with SHN_UNDEF used for those that do not apply.
145d29b2c44Sab196087  */
146d29b2c44Sab196087 typedef struct {
147d29b2c44Sab196087 	Elf32_Word	symt_shndx;	/* Symbol table section index */
148d29b2c44Sab196087 	Elf32_Word	symt_xshndx;	/* Index of extended index section */
149d29b2c44Sab196087 	Elf32_Word	symt_syminfo;	/* Index of versym section */
150d29b2c44Sab196087 	Elf32_Word	symt_versym;	/* Index of versym section */
151d29b2c44Sab196087 } elfedit32_symtab_t;
152d29b2c44Sab196087 
153d29b2c44Sab196087 typedef struct {
154d29b2c44Sab196087 	Elf64_Word	symt_shndx;
155d29b2c44Sab196087 	Elf64_Word	symt_xshndx;
156d29b2c44Sab196087 	Elf64_Word	symt_versym;
157d29b2c44Sab196087 	Elf64_Word	symt_syminfo;
158d29b2c44Sab196087 } elfedit64_symtab_t;
159d29b2c44Sab196087 
160d29b2c44Sab196087 #ifdef _ELF64
161d29b2c44Sab196087 #define	elfedit_symtab_t	elfedit64_symtab_t
162d29b2c44Sab196087 #else
163d29b2c44Sab196087 #define	elfedit_symtab_t	elfedit32_symtab_t
164d29b2c44Sab196087 #endif
165d29b2c44Sab196087 
166d29b2c44Sab196087 
167d29b2c44Sab196087 /*
168d29b2c44Sab196087  * Information for a single ELF object.
169d29b2c44Sab196087  *
170d29b2c44Sab196087  * note:
171d29b2c44Sab196087  *	elfedit is intended to be an expert's tool, capable of modifying
172d29b2c44Sab196087  *	nearly everything in the file, whether or not such modifications
173d29b2c44Sab196087  *	are a good idea. At the same time, elfedit, via libelf, relies
174d29b2c44Sab196087  *	on the contents of the object to properly locate information in
175d29b2c44Sab196087  *	the file. As this is the same information that elfedit allows the
176d29b2c44Sab196087  *	user to modify, it should be obvious that the potential exists
177d29b2c44Sab196087  *	for users to corrupt the file to the degree that elfedit itself
178d29b2c44Sab196087  *	may fail, or produce spurious results. We allow such changes for
179d29b2c44Sab196087  *	several reasons:
180d29b2c44Sab196087  *
181d29b2c44Sab196087  *	1) Such corruption does not happen in the most obvious and
182d29b2c44Sab196087  *		useful operations elfedit supports, but comes as a result
183d29b2c44Sab196087  *		of modifying fields that contain size and offset information
184d29b2c44Sab196087  *		used to navigate the file. Non-ELF developers have
185d29b2c44Sab196087  *		little practical reason to change such things.
186d29b2c44Sab196087  *
187d29b2c44Sab196087  *	2) Producing a corrupt ELF file can be very useful
188d29b2c44Sab196087  *		for R&D and/or testing purposes.
189d29b2c44Sab196087  *
190d29b2c44Sab196087  *	3) ELF is sufficiently complex that no absolute guarantees can
191d29b2c44Sab196087  *		be made about "safe" operations, beyond the basic
192d29b2c44Sab196087  *		and obvious things that are of practical use.
193d29b2c44Sab196087  *
194d29b2c44Sab196087  *	One way we protect ourselves is via the information cached in
195d29b2c44Sab196087  *	the elfedit_obj_state_t structure at startup. By using this
196d29b2c44Sab196087  *	information, rather than constantly fetching it via libelf,
197d29b2c44Sab196087  *	we protect ourselves against many user changes, such as changing the
198d29b2c44Sab196087  *	program or section header offsets, or similar size/position fields.
199d29b2c44Sab196087  *
200d29b2c44Sab196087  *	Of course, we make no assurances that that we will be able to
201d29b2c44Sab196087  *	read the resulting file in a subsequent session.
202d29b2c44Sab196087  */
203d29b2c44Sab196087 typedef struct {
204d29b2c44Sab196087 	const char		*os_file;	/* Path to ELF file */
205d29b2c44Sab196087 	int			os_fd;		/* Open file descriptor */
206d29b2c44Sab196087 	Elf			*os_elf;	/* ELF descriptor */
207d29b2c44Sab196087 	Elf32_Ehdr		*os_ehdr;	/* ELF header */
208d29b2c44Sab196087 	Elf32_Word		os_dynndx;	/* Index of dynamic section */
209d29b2c44Sab196087 	size_t			os_shstrndx;	/* Index of section header */
210d29b2c44Sab196087 						/*	string table section */
211d29b2c44Sab196087 	size_t			os_shnum;	/* # of sections in file */
212d29b2c44Sab196087 	elfedit32_section_t	*os_secarr;	/* Section data */
213d29b2c44Sab196087 	size_t			os_phnum;	/* # of program headers */
214d29b2c44Sab196087 	Elf32_Phdr		*os_phdr;	/* Program header array */
215d29b2c44Sab196087 	size_t			os_symtabnum;	/* # items in os_symtab[] */
216d29b2c44Sab196087 	elfedit32_symtab_t	*os_symtab;	/* Array of symbol tbl info  */
217d29b2c44Sab196087 } elfedit32_obj_state_t;
218d29b2c44Sab196087 
219d29b2c44Sab196087 typedef struct {
220d29b2c44Sab196087 	const char		*os_file;
221d29b2c44Sab196087 	int			os_fd;
222d29b2c44Sab196087 	Elf			*os_elf;
223d29b2c44Sab196087 	Elf64_Ehdr		*os_ehdr;
224d29b2c44Sab196087 	Elf64_Word		os_dynndx;
225d29b2c44Sab196087 	size_t			os_shstrndx;
226d29b2c44Sab196087 	size_t			os_shnum;
227d29b2c44Sab196087 	elfedit64_section_t	*os_secarr;
228d29b2c44Sab196087 	size_t			os_phnum;
229d29b2c44Sab196087 	Elf64_Phdr		*os_phdr;
230d29b2c44Sab196087 	size_t			os_symtabnum;
231d29b2c44Sab196087 	elfedit64_symtab_t	*os_symtab;
232d29b2c44Sab196087 } elfedit64_obj_state_t;
233d29b2c44Sab196087 
234d29b2c44Sab196087 #ifdef _ELF64
235d29b2c44Sab196087 #define	elfedit_obj_state_t	elfedit64_obj_state_t
236d29b2c44Sab196087 #else
237d29b2c44Sab196087 #define	elfedit_obj_state_t	elfedit32_obj_state_t
238d29b2c44Sab196087 #endif
239d29b2c44Sab196087 
240d29b2c44Sab196087 
241d29b2c44Sab196087 /*
242d29b2c44Sab196087  * Bit values for editor state.
243d29b2c44Sab196087  */
244d29b2c44Sab196087 typedef enum {
245d29b2c44Sab196087 	ELFEDIT_F_AUTOPRINT = 1, /* Print informational text about edits */
246d29b2c44Sab196087 	ELFEDIT_F_DEBUG = 2,	/* Print informational text about operations */
247d29b2c44Sab196087 	ELFEDIT_F_READONLY = 4,	/* File is processed readonly */
248d29b2c44Sab196087 } elfedit_flag_t;
249d29b2c44Sab196087 
250d29b2c44Sab196087 /*
251d29b2c44Sab196087  * Type used to represent the output style for printing ELF values.
252d29b2c44Sab196087  *
253d29b2c44Sab196087  * DEFAULT - Output is in 'elfdump' style, designed for human eyes.
254d29b2c44Sab196087  *	Headers, and additional information are shown.
255d29b2c44Sab196087  * SIMPLE - Output is simple, consisting only of the target item.
256d29b2c44Sab196087  *	Integer values are shown as symbolic constants when possible,
257d29b2c44Sab196087  *	and integers otherwise.
258d29b2c44Sab196087  * NUM - Like SIMPLE, except integer values are always shown as
259d29b2c44Sab196087  *	integer constants, and strings are shown as the integer
260d29b2c44Sab196087  *	offset into the string table.
261d29b2c44Sab196087  */
262d29b2c44Sab196087 typedef enum {
263d29b2c44Sab196087 	ELFEDIT_OUTSTYLE_DEFAULT = 0,
264d29b2c44Sab196087 	ELFEDIT_OUTSTYLE_SIMPLE = 1,
265d29b2c44Sab196087 	ELFEDIT_OUTSTYLE_NUM = 2
266d29b2c44Sab196087 } elfedit_outstyle_t;
267d29b2c44Sab196087 
268d29b2c44Sab196087 
269d29b2c44Sab196087 /*
270d29b2c44Sab196087  * The elfedit_module_t, and the types it references, are defined
271d29b2c44Sab196087  * by loadable elfedit modules, and used by elfedit. These structures
272d29b2c44Sab196087  * need to communicate internationalized strings for elfedit to print.
273d29b2c44Sab196087  *
274d29b2c44Sab196087  * We want to leave the choice of internationalization APIs, as well as
275d29b2c44Sab196087  * the decision about whether or not to even to it to the individual
276d29b2c44Sab196087  * modules. Hence, we do not use a simple (const char *) pointer to
277d29b2c44Sab196087  * communicate potentially internationalized strings. Instead, we define
278d29b2c44Sab196087  * elfedit_i18nhdl_t, an opaque type guaranteed to be large enough
279d29b2c44Sab196087  * to hold a pointer. Each module casts the handle needed to access the
280d29b2c44Sab196087  * string to this type. Each module also supplies a function
281d29b2c44Sab196087  * (mod_i18nhdl_to_str field of elfedit_module_t) that given one
282d29b2c44Sab196087  * of these opaque keys, will return a (const char *) pointer to the
283d29b2c44Sab196087  * actual string, for elfedit to print.
284d29b2c44Sab196087  *
285d29b2c44Sab196087  * If the underlying module doesn't want to implement i18n support,
286d29b2c44Sab196087  * all it has to do is cast the strings to elfedit_i18nhdl_t and
287d29b2c44Sab196087  * back.
288d29b2c44Sab196087  */
289d29b2c44Sab196087 typedef uintptr_t elfedit_i18nhdl_t;
290d29b2c44Sab196087 
291d29b2c44Sab196087 
292d29b2c44Sab196087 
293d29b2c44Sab196087 /*
294d29b2c44Sab196087  * Macro to handle casting international string "handles" to the
295d29b2c44Sab196087  * elfedit_i18nhdl_t opaque type.
296d29b2c44Sab196087  */
297d29b2c44Sab196087 #define	ELFEDIT_I18NHDL(_i18n_str_ref) ((elfedit_i18nhdl_t)_i18n_str_ref)
298d29b2c44Sab196087 
299d29b2c44Sab196087 
300d29b2c44Sab196087 /*
301d29b2c44Sab196087  * Return values from command functions
302d29b2c44Sab196087  */
303d29b2c44Sab196087 typedef enum {
304d29b2c44Sab196087 	ELFEDIT_CMDRET_NONE = 0,	/* Nothing to report */
305d29b2c44Sab196087 	ELFEDIT_CMDRET_MOD = 1,		/* Command modified output ELF file */
3064f680cc6SAli Bahrami 	ELFEDIT_CMDRET_MOD_OS_MACH = 2, /* As per _MOD, include ELF header */
3074f680cc6SAli Bahrami 					/*	osabi or machine change */
3084f680cc6SAli Bahrami 	ELFEDIT_CMDRET_FLUSH = 3	/* Output file flushed: elf_update() */
309d29b2c44Sab196087 } elfedit_cmdret_t;
310d29b2c44Sab196087 
311d29b2c44Sab196087 /*
312d29b2c44Sab196087  * Prototype of an implementation function for an edit command. Note that
313d29b2c44Sab196087  * commands do not return a status:
314d29b2c44Sab196087  *	- Success is indicated by a normal return.
315d29b2c44Sab196087  *	- The command indicates a fatal error by calling elfedit_msg() with the
316d29b2c44Sab196087  *		ELFEDIT_MSG_ERR type, in which case execution does not return
317d29b2c44Sab196087  *		to the command, and the elfedit command loop knows that an
318d29b2c44Sab196087  *		error occurred.
319d29b2c44Sab196087  *	- The command is responsible for using the standard libelf
320d29b2c44Sab196087  *		mechanisms to indicate when changes have been made to
321d29b2c44Sab196087  *		the ELF file.
322d29b2c44Sab196087  */
323d29b2c44Sab196087 typedef elfedit_cmdret_t elfedit32_cmd_func_t(elfedit32_obj_state_t *state,
324d29b2c44Sab196087     int argc, const char *argv[]);
325d29b2c44Sab196087 typedef elfedit_cmdret_t elfedit64_cmd_func_t(elfedit64_obj_state_t *state,
326d29b2c44Sab196087     int argc, const char *argv[]);
327d29b2c44Sab196087 #ifdef _ELF64
328d29b2c44Sab196087 #define	elfedit_cmd_func_t	elfedit64_cmd_func_t
329d29b2c44Sab196087 #else
330d29b2c44Sab196087 #define	elfedit_cmd_func_t	elfedit32_cmd_func_t
331d29b2c44Sab196087 #endif
332d29b2c44Sab196087 
333d29b2c44Sab196087 
334d29b2c44Sab196087 /*
335d29b2c44Sab196087  * An elfedit command (elfedit_cmd_t) has a cmd_cpl field that
336d29b2c44Sab196087  * can be set to a command completion function. If such a function
337d29b2c44Sab196087  * is present (non-NULL), and the user presses the tab key at the
338d29b2c44Sab196087  * command line while the cursor is at a plain (non option) argument,
339d29b2c44Sab196087  * elfedit calls the function, passing it all the tokens up through
340d29b2c44Sab196087  * the one needing completion.  The function can use elfedit_cpl_match()
341d29b2c44Sab196087  * to enter possible alternatives.  Additionally, there are helper
342d29b2c44Sab196087  * functions built on top of elfedit_cpl_match() that simplify common cases.
343d29b2c44Sab196087  *
344d29b2c44Sab196087  *	elfedit_cpl_ato[iu]() - enter matches from elfedit_ato[iu]_sym_t
345d29b2c44Sab196087  *		mappings.
346d29b2c44Sab196087  *	elfedit_cpl_atoconst() - Enter matches for well known constants
347d29b2c44Sab196087  *	elfedit_cpl_command() - enter matches for all known commands
348d29b2c44Sab196087  *	elfedit_cpl_mod() - enter matches for all known modules.
34955ef6355Sab196087  *	elfedit_cpl_ndx() - enter numeric index as a match
350d29b2c44Sab196087  *
351d29b2c44Sab196087  * The completion function is passed the following arguments:
352d29b2c44Sab196087  *
353d29b2c44Sab196087  *	obj_state - Object state. Will be NULL if elfedit session does not
354d29b2c44Sab196087  *		have an active object. The completion function must test
355d29b2c44Sab196087  *		the pointer before using it.
356d29b2c44Sab196087  *	cpldata - Completion data, to be passed to elfedit_cpl_match()
357d29b2c44Sab196087  *		or the helper functions built on it to register alternative
358d29b2c44Sab196087  *		strings.
35969112eddSAli Bahrami  *	argc, argv - The tokens from the start of the line through
360d29b2c44Sab196087  *		the one needing completion, which will always
361d29b2c44Sab196087  *		be cmdcpl_argv[cmdcpl_argc - 1].
362d29b2c44Sab196087  *	num_opt - A count of the optional arguments (those starting with
363d29b2c44Sab196087  *		'-' at the beginning of argv. This means that argv[num_opt]
364d29b2c44Sab196087  *		is the first plain argument, and the 1-based positional
365d29b2c44Sab196087  *		number of the plain argument for which command completion
366d29b2c44Sab196087  *		is needed is (argc - num_opt).
367d29b2c44Sab196087  */
368d29b2c44Sab196087 typedef void elfedit32_cmdcpl_func_t(elfedit32_obj_state_t *state,
369d29b2c44Sab196087     void *cpldata, int argc, const char *argv[], int num_opt);
370d29b2c44Sab196087 typedef void elfedit64_cmdcpl_func_t(elfedit64_obj_state_t *state,
371d29b2c44Sab196087     void *cpldata, int argc, const char *argv[], int num_opt);
372d29b2c44Sab196087 #ifdef _ELF64
373d29b2c44Sab196087 #define	elfedit_cmdcpl_func_t	elfedit64_cmdcpl_func_t
374d29b2c44Sab196087 #else
375d29b2c44Sab196087 #define	elfedit_cmdcpl_func_t	elfedit32_cmdcpl_func_t
376d29b2c44Sab196087 #endif
377d29b2c44Sab196087 
378d29b2c44Sab196087 
379d29b2c44Sab196087 
380d29b2c44Sab196087 
381d29b2c44Sab196087 /*
382d29b2c44Sab196087  * Command option/argument descriptor. These structures
383d29b2c44Sab196087  * are used to represent each option and plain argument accepted
384d29b2c44Sab196087  * by a command, via the cmd_opt and cmd_args fields in the
385d29b2c44Sab196087  * command definition (elfedit_cmd_t). Each descriptor consists
386d29b2c44Sab196087  * of a name, a help string (formatted for display via sys:help),
387d29b2c44Sab196087  * and a flags field that conveys extra information about the
388d29b2c44Sab196087  * item:
389d29b2c44Sab196087  *
390d29b2c44Sab196087  *	ELFEDIT_CMDOA_F_OPT
391d29b2c44Sab196087  *	The item is optional. This flag is implicit for options
392d29b2c44Sab196087  *	and need only be set for plain arguments.
393d29b2c44Sab196087  *
394d29b2c44Sab196087  *	ELFEDIT_CMDOA_F_VALUE
395d29b2c44Sab196087  *	The item has a value, which is found in the following
396d29b2c44Sab196087  *	item. This flag only has meaning for options, and should
397d29b2c44Sab196087  *	not be set for plain arguments. The descriptor for the
398d29b2c44Sab196087  *	value is found in the next array element, and only the
399d29b2c44Sab196087  *	oa_name field is used (the other should be set t 0).
400d29b2c44Sab196087  *
401d29b2c44Sab196087  *	ELFEDIT_CMDOA_F_MULT
402d29b2c44Sab196087  *	More than one of the specified items may be specified
403d29b2c44Sab196087  *
404d29b2c44Sab196087  *	ELFEDIT_CMDOA_F_INHERIT
405d29b2c44Sab196087  *	This is an item for which a common definition exists.
406d29b2c44Sab196087  *	Elfedit will substitute the standard values for the
407d29b2c44Sab196087  *	name, help text, and flags. This enforces consistency
408d29b2c44Sab196087  *	in documentation, plus it is easier for the module author.
409d29b2c44Sab196087  *	When ELFEDIT_CMDOA_F_INHERIT is set:
410d29b2c44Sab196087  *		- oa_name should be set to one of the ELFEDIT_STDOA_
411d29b2c44Sab196087  *			values to specifiy which standard item is being
412d29b2c44Sab196087  *			inherited.
413d29b2c44Sab196087  *		- oa_help must be set to NULL.
414d29b2c44Sab196087  *		- It is an error to set any other flags with
415d29b2c44Sab196087  *			ELFEDIT_CMDOA_F_INHERIT.
416d29b2c44Sab196087  *		- oa_idmask and oa_excmask are used in the normal way.
417d29b2c44Sab196087  *
418d29b2c44Sab196087  * The oa_idmask and oa_excmask fields are used to identify options,
419d29b2c44Sab196087  * and to support mutual exclusion (when two or more options cannot be
420d29b2c44Sab196087  * used together). They are ignored for arguments, and should be set to 0.
421d29b2c44Sab196087  * oa_idmask is used to uniquely identify each item. When elfedit_getopt()
422d29b2c44Sab196087  * matches an option, it returns the value of oa_idmask to the caller to
423d29b2c44Sab196087  * indicate which option was matched. elfedit enforces the following rules
424d29b2c44Sab196087  * for oa_idmask, and will refuse to load a module that does not follow them:
425d29b2c44Sab196087  *	- The value of oa_idmask must be 0, or have a value that
426d29b2c44Sab196087  *		is a power of 2 (i.e. only has one bit set).
427d29b2c44Sab196087  *	- Each item that sets a non-0 value for oa_idmask must have
428d29b2c44Sab196087  *		a unique value.
429d29b2c44Sab196087  *	- If oa_idmask is 0, oa_excmask must be 0 also.
430d29b2c44Sab196087  *	- oa_excmask is set to 0 if an item is not mutually exclusive
431d29b2c44Sab196087  *		to any other item. Otherwise, it should set the bit
432d29b2c44Sab196087  *		values representing the items it is mutually exclusive to.
433d29b2c44Sab196087  *	- An oa_idmask value of 0 can be used for any item that
434d29b2c44Sab196087  *		the module does not need to identify, and which
435d29b2c44Sab196087  *		is not mutually exclusive to any other item.
436d29b2c44Sab196087  * As elfedit_getopt() processes items, it maintains a bitmask combining the
437d29b2c44Sab196087  * oa_idmask fields of all the options already seen. For each option, it uses
438d29b2c44Sab196087  * oa_excmask to check for conflicts.
439d29b2c44Sab196087  *
440d29b2c44Sab196087  * note: elfedit enforces the rule that options consist of a '-'
441d29b2c44Sab196087  *	character followed by at least one character when a module
442d29b2c44Sab196087  *	is loaded.
443d29b2c44Sab196087  */
444d29b2c44Sab196087 typedef enum {
445d29b2c44Sab196087 	ELFEDIT_CMDOA_F_OPT =	1,	/* Item is optional */
446d29b2c44Sab196087 	ELFEDIT_CMDOA_F_VALUE =	2,	/* Item has a value arg following */
447d29b2c44Sab196087 	ELFEDIT_CMDOA_F_MULT =	4,	/* More than one are allowed */
448d29b2c44Sab196087 	ELFEDIT_CMDOA_F_INHERIT = 8,	/* Inherit definition: See above */
449d29b2c44Sab196087 } elfedit_cmd_oa_flag_t;
450d29b2c44Sab196087 
451d29b2c44Sab196087 typedef u_longlong_t elfedit_cmd_oa_mask_t;
452d29b2c44Sab196087 
453d29b2c44Sab196087 typedef struct {
454d29b2c44Sab196087 	const char		*oa_name;	/* Name of option */
455d29b2c44Sab196087 	elfedit_i18nhdl_t	oa_help;	/* Help text for option */
456d29b2c44Sab196087 	elfedit_cmd_oa_flag_t	oa_flags;	/* Additional attributes */
457d29b2c44Sab196087 	elfedit_cmd_oa_mask_t	oa_idmask;	/* Unique id, returned by */
458d29b2c44Sab196087 						/* 	elfedit_getopt */
459d29b2c44Sab196087 						/*	for use by caller */
460d29b2c44Sab196087 	elfedit_cmd_oa_mask_t	oa_excmask;	/* Mutual exclusion mask */
461d29b2c44Sab196087 } elfedit_cmd_optarg_t;
462d29b2c44Sab196087 
463d29b2c44Sab196087 
464d29b2c44Sab196087 
465d29b2c44Sab196087 /*
466d29b2c44Sab196087  * These values define the standard options and arguments that a module
467d29b2c44Sab196087  * can inherit using the ELFEDIT_CMDOA_F_INHERIT flag (described above).
468d29b2c44Sab196087  * New items must be added at the end --- reordering the list will
469d29b2c44Sab196087  * require all modules to be rebuilt.
470d29b2c44Sab196087  *
471d29b2c44Sab196087  * Note: 0 cannot be used as a ELFEDIT_STDOA_ value, because a NULL
472d29b2c44Sab196087  *	value of oa_name is used to terminate argument and options lists.
473d29b2c44Sab196087  *	Therefore, these values start at 1.
474d29b2c44Sab196087  */
475d29b2c44Sab196087 #define	ELFEDIT_STDOA_OPT_O		((const char *) 1)	/* -o ostyle */
476d29b2c44Sab196087 #define	ELFEDIT_STDOA_OPT_AND		((const char *) 2)	/* -and */
477d29b2c44Sab196087 #define	ELFEDIT_STDOA_OPT_CMP		((const char *) 3)	/* -cmp */
478d29b2c44Sab196087 #define	ELFEDIT_STDOA_OPT_OR		((const char *) 4)	/* -or */
479d29b2c44Sab196087 
480d29b2c44Sab196087 #define	ELFEDIT_NUM_STDOA	4	/* # of ELFEDIT_STDOA_ definitions */
481d29b2c44Sab196087 
482d29b2c44Sab196087 
483d29b2c44Sab196087 
484d29b2c44Sab196087 /*
485d29b2c44Sab196087  * Definition of a command
486d29b2c44Sab196087  *
487d29b2c44Sab196087  * This structure includes an elfedit_cmd_func_t pointer, which has
488d29b2c44Sab196087  * different definitions for different ELFCLASS. Rather than needlessly
489d29b2c44Sab196087  * complicate the code with three versions of this type, and any
490d29b2c44Sab196087  * type that uses it, we simply use the GenericClass type. elfedit
491d29b2c44Sab196087  * will always cast this to the correct type before calling a module.
492d29b2c44Sab196087  *
493d29b2c44Sab196087  * cmd_name is an array of pointers to the names for the command.
494d29b2c44Sab196087  * The "primary" name should always be first, followed by any alias
495d29b2c44Sab196087  * names. The final element of the array must be a NULL pointer,
496d29b2c44Sab196087  * which terminates the list. Every command is required to have at
497d29b2c44Sab196087  * least one name, so code is allowed to assume that the first element
498d29b2c44Sab196087  * of cmd_name is non-NULL, and contains the primary name.
499d29b2c44Sab196087  *
500d29b2c44Sab196087  * Many modules provide a "default" command, which is a command
501d29b2c44Sab196087  * that is run if only the module name is specified, followed
502d29b2c44Sab196087  * by a colon (i.e. "sym:"). The way this is implemented is to
503d29b2c44Sab196087  * give the desired default command an empty string as an alias.
504d29b2c44Sab196087  * Note that the primary name cannot be an empty string, only the
505d29b2c44Sab196087  * alias name.
506d29b2c44Sab196087  *
507d29b2c44Sab196087  * cmd_opts and cmd_args are each an array of elfedit_cmd_argdesc_t
508d29b2c44Sab196087  * structures, that describe the options and plain arguments accepted
509d29b2c44Sab196087  * by the command. These arrays are used to general help text for
510d29b2c44Sab196087  * the commands. The cmd_opts array is also used to provide command
511d29b2c44Sab196087  * completion for options. Both of these arrays are terminated by
512d29b2c44Sab196087  * a final NULL element (all fields zero).
513d29b2c44Sab196087  */
514d29b2c44Sab196087 typedef struct {
515d29b2c44Sab196087 	elfedit32_cmd_func_t	*cmd_func;	/* Implementation */
516d29b2c44Sab196087 	elfedit32_cmdcpl_func_t	*cmd_cplfunc;	/* Completion function */
517d29b2c44Sab196087 	const char		**cmd_name;	/* Cmd names (null term.) */
518d29b2c44Sab196087 	elfedit_i18nhdl_t	cmd_desc;	/* Short desc. of cmd purpose */
519d29b2c44Sab196087 	elfedit_i18nhdl_t	cmd_help;	/* Help text for the command */
520d29b2c44Sab196087 	elfedit_cmd_optarg_t	*cmd_opt;	/* Options */
521d29b2c44Sab196087 	elfedit_cmd_optarg_t	*cmd_args;	/* Plain arguments */
522d29b2c44Sab196087 } elfedit32_cmd_t;
523d29b2c44Sab196087 
524d29b2c44Sab196087 typedef struct {
525d29b2c44Sab196087 	elfedit64_cmd_func_t	*cmd_func;
526d29b2c44Sab196087 	elfedit64_cmdcpl_func_t	*cmd_cplfunc;
527d29b2c44Sab196087 	const char		**cmd_name;
528d29b2c44Sab196087 	elfedit_i18nhdl_t	cmd_desc;
529d29b2c44Sab196087 	elfedit_i18nhdl_t	cmd_help;
530d29b2c44Sab196087 	elfedit_cmd_optarg_t	*cmd_opt;
531d29b2c44Sab196087 	elfedit_cmd_optarg_t	*cmd_args;
532d29b2c44Sab196087 } elfedit64_cmd_t;
533d29b2c44Sab196087 
534d29b2c44Sab196087 #ifdef _ELF64
535d29b2c44Sab196087 #define	elfedit_cmd_t		elfedit64_cmd_t
536d29b2c44Sab196087 #else
537d29b2c44Sab196087 #define	elfedit_cmd_t		elfedit32_cmd_t
538d29b2c44Sab196087 #endif
539d29b2c44Sab196087 
540d29b2c44Sab196087 
541d29b2c44Sab196087 
542d29b2c44Sab196087 /*
543d29b2c44Sab196087  * elfedit modules version themselves so that we can alter the definition
544d29b2c44Sab196087  * of elfedit_module_t in a backward compatible way.
545d29b2c44Sab196087  */
546d29b2c44Sab196087 typedef enum {
547d29b2c44Sab196087 	ELFEDIT_VER_NONE = 0,
548d29b2c44Sab196087 	ELFEDIT_VER_CURRENT = 1,
549d29b2c44Sab196087 	ELFEDIT_VER_NUM = 2
550d29b2c44Sab196087 } elfedit_module_version_t;
551d29b2c44Sab196087 
552d29b2c44Sab196087 
553d29b2c44Sab196087 /*
554d29b2c44Sab196087  * Each module returns a pointer to an elfedit_module_t, describing
555d29b2c44Sab196087  * what commands the module provides.
556d29b2c44Sab196087  *
557d29b2c44Sab196087  * Note: mod_cmds is a NULL terminated array of command defs. This
558d29b2c44Sab196087  * means that the final element in the array should have all of its
559d29b2c44Sab196087  * fields set to NULL.
560d29b2c44Sab196087  *
561d29b2c44Sab196087  * The mod_i18nhdl_to_str function pointer is explained above
562d29b2c44Sab196087  * with the definition of elfedit_i18nhdl_t.
563d29b2c44Sab196087  */
564d29b2c44Sab196087 typedef const char *(* elfedit_mod_i18nhdl_to_str_func_t)(elfedit_i18nhdl_t);
565d29b2c44Sab196087 
566d29b2c44Sab196087 typedef struct {
567d29b2c44Sab196087 	elfedit_module_version_t mod_version;	/* version */
568d29b2c44Sab196087 	const char		*mod_name;	/* Name of module */
569d29b2c44Sab196087 	elfedit_i18nhdl_t	mod_desc;	/* Short desc. of mod purpose */
570d29b2c44Sab196087 	elfedit32_cmd_t		*mod_cmds;	/* Array of command defs */
571d29b2c44Sab196087 						/* i18n -> (char *) fcn */
572d29b2c44Sab196087 	elfedit_mod_i18nhdl_to_str_func_t mod_i18nhdl_to_str;
573d29b2c44Sab196087 } elfedit32_module_t;
574d29b2c44Sab196087 
575d29b2c44Sab196087 typedef struct {
576d29b2c44Sab196087 	elfedit_module_version_t mod_version;
577d29b2c44Sab196087 	const char		*mod_name;
578d29b2c44Sab196087 	elfedit_i18nhdl_t	mod_desc;
579d29b2c44Sab196087 	elfedit64_cmd_t		*mod_cmds;
580d29b2c44Sab196087 	elfedit_mod_i18nhdl_to_str_func_t mod_i18nhdl_to_str;
581d29b2c44Sab196087 } elfedit64_module_t;
582d29b2c44Sab196087 
583d29b2c44Sab196087 #ifdef _ELF64
584d29b2c44Sab196087 #define	elfedit_module_t	elfedit64_module_t
585d29b2c44Sab196087 #else
586d29b2c44Sab196087 #define	elfedit_module_t	elfedit32_module_t
587d29b2c44Sab196087 #endif
588d29b2c44Sab196087 
589d29b2c44Sab196087 
590d29b2c44Sab196087 /*
591d29b2c44Sab196087  * Each module is a sharable library, expected to provide a single global
592d29b2c44Sab196087  * function, named elfedit_init(), with the following prototype.
593d29b2c44Sab196087  */
594d29b2c44Sab196087 typedef elfedit_module_t *elfedit_init_func_t(elfedit_module_version_t version);
595d29b2c44Sab196087 
596d29b2c44Sab196087 
597cce0e03bSab196087 /*
598cce0e03bSab196087  * Prototype for elfedit_write(), and for outfunc argument
599cce0e03bSab196087  * to elfedit_str_to_c_literal().
600cce0e03bSab196087  */
601cce0e03bSab196087 typedef void elfedit_write_func_t(const void *ptr, size_t size);
602d29b2c44Sab196087 
603d29b2c44Sab196087 
604d29b2c44Sab196087 /*
605d29b2c44Sab196087  * Core elfedit functions exported for use by modules
606d29b2c44Sab196087  */
607d29b2c44Sab196087 extern void elfedit_command_usage(void);
608d29b2c44Sab196087 extern void elfedit_cpl_command(void *cpldata);
609d29b2c44Sab196087 extern void elfedit_cpl_match(void *cpldata, const char *str, int casefold);
61055ef6355Sab196087 extern void elfedit_cpl_ndx(void *cpldata, uint_t ndx);
611d29b2c44Sab196087 extern void elfedit_elferr(const char *file, const char *libelf_rtn_name);
612d29b2c44Sab196087 extern elfedit_flag_t elfedit_flags(void);
613d29b2c44Sab196087 extern void *elfedit_malloc(const char *item_name, size_t size);
614d29b2c44Sab196087 extern void elfedit_msg(elfedit_msg_t type, const char *format, ...);
615d29b2c44Sab196087 extern elfedit_outstyle_t elfedit_outstyle(void);
616d29b2c44Sab196087 extern void elfedit_pager_init(void);
617d29b2c44Sab196087 extern void elfedit_printf(const char *format, ...);
618d29b2c44Sab196087 extern void *elfedit_realloc(const char *item_name, void *ptr, size_t size);
619cce0e03bSab196087 extern void elfedit_str_to_c_literal(const char *str,
620cce0e03bSab196087     elfedit_write_func_t *outfunc);
621cce0e03bSab196087 extern elfedit_write_func_t elfedit_write;
622d29b2c44Sab196087 
623d29b2c44Sab196087 /*
624d29b2c44Sab196087  * Core elfedit functions exported for use by sys: module only
625d29b2c44Sab196087  */
626d29b2c44Sab196087 extern void elfedit_cpl_module(void *cpldata, int load_all_modules);
627d29b2c44Sab196087 
628d29b2c44Sab196087 
629d29b2c44Sab196087 /*
630d29b2c44Sab196087  * elfedit modules are expected to define two functions, one for
631d29b2c44Sab196087  * each ELFCLASS. Define a generic name for this function, based on
632d29b2c44Sab196087  * the class being supported by the including module.
633d29b2c44Sab196087  */
634d29b2c44Sab196087 #ifdef _ELF64
635d29b2c44Sab196087 #define	elfedit_init		elfedit64_init
636d29b2c44Sab196087 #else
637d29b2c44Sab196087 #define	elfedit_init		elfedit32_init
638d29b2c44Sab196087 #endif
639d29b2c44Sab196087 
640d29b2c44Sab196087 
641d29b2c44Sab196087 
642d29b2c44Sab196087 /*
643d29b2c44Sab196087  * It is common to search the dynamic section for specific elements.
644d29b2c44Sab196087  * Structures of this type are used to represent the contents of such
645d29b2c44Sab196087  * elements in a systematic way. The elfedit_dyn_elt_init() function
646d29b2c44Sab196087  * is used to prepare these strucutres for use.
647d29b2c44Sab196087  */
648d29b2c44Sab196087 typedef struct {
649d29b2c44Sab196087 	int		dn_seen;	/* True if this item has been seen */
650d29b2c44Sab196087 	Elf32_Word	dn_ndx;		/* Index of item in dynamic array */
651d29b2c44Sab196087 	Elf32_Dyn	dn_dyn;		/* Contents of dynamic item */
652d29b2c44Sab196087 } elfedit32_dyn_elt_t;
653d29b2c44Sab196087 
654d29b2c44Sab196087 typedef struct {
655d29b2c44Sab196087 	int		dn_seen;
656d29b2c44Sab196087 	Elf64_Word	dn_ndx;
657d29b2c44Sab196087 	Elf64_Dyn	dn_dyn;
658d29b2c44Sab196087 } elfedit64_dyn_elt_t;
659d29b2c44Sab196087 
660d29b2c44Sab196087 #ifdef _ELF64
661d29b2c44Sab196087 #define	elfedit_dyn_elt_t	elfedit64_dyn_elt_t
662d29b2c44Sab196087 #else
663d29b2c44Sab196087 #define	elfedit_dyn_elt_t	elfedit32_dyn_elt_t
664d29b2c44Sab196087 #endif
665d29b2c44Sab196087 
666d29b2c44Sab196087 /*
667d29b2c44Sab196087  * The elfedit_atoi() and elfedit_atoui() functions can optionally
668d29b2c44Sab196087  * accept an array of these structures, giving symbolic names that
669d29b2c44Sab196087  * will be accepted instead of numeric codes. If such an array is
670d29b2c44Sab196087  * present, the supplied string has it's leading and trailing whitespace
671d29b2c44Sab196087  * removed and is then compared to the list, and if there is a match,
672d29b2c44Sab196087  * the corresponding integer value is returned.
673d29b2c44Sab196087  *
674d29b2c44Sab196087  * The final array element must have its name field set to NULL.
675d29b2c44Sab196087  */
676d29b2c44Sab196087 typedef u_longlong_t elfedit_atoui_t;
677d29b2c44Sab196087 typedef struct {
678d29b2c44Sab196087 	const char	*sym_name;
679d29b2c44Sab196087 	elfedit_atoui_t	sym_value;
680d29b2c44Sab196087 } elfedit_atoui_sym_t;
681d29b2c44Sab196087 typedef longlong_t elfedit_atoi_t;
682d29b2c44Sab196087 typedef struct {
683d29b2c44Sab196087 	const char	*sym_name;
684d29b2c44Sab196087 	elfedit_atoi_t	sym_value;
685d29b2c44Sab196087 } elfedit_atoi_sym_t;
686d29b2c44Sab196087 
687d29b2c44Sab196087 
688d29b2c44Sab196087 /*
689d29b2c44Sab196087  * The elfedit_atoconst*() functions are built on top of the atoui routines.
690d29b2c44Sab196087  * These routines accept an elfedit_const_t code instead of a
691d29b2c44Sab196087  * pointer to an elfedit_atoui_sym_t array, and use internally
692d29b2c44Sab196087  * predefined tables of elfedit_atoui_sym_t in order to do the desired
693d29b2c44Sab196087  * mappings. elfedit modules are encouraged to use these standard
694d29b2c44Sab196087  * tables instead of defining their own elfedit_atoui_sym_t arrays.
695d29b2c44Sab196087  *
696d29b2c44Sab196087  * note:
697d29b2c44Sab196087  *	- The values assigned here must be in agreement with the
698d29b2c44Sab196087  *		sym_table[] array defined in elfconst.c.
699d29b2c44Sab196087  *	- Once defined, these values must not change. Reordering the
700d29b2c44Sab196087  *		list will require all modules to be rebuilt, and will
701d29b2c44Sab196087  *		break backward compatability. New items should be
702d29b2c44Sab196087  *		added to the end.
703d29b2c44Sab196087  */
704d29b2c44Sab196087 typedef enum {
705d29b2c44Sab196087 	ELFEDIT_CONST_OUTSTYLE =	0,	/* elfedit output styles  */
706d29b2c44Sab196087 	ELFEDIT_CONST_OUTSTYLE_MO =	1,	/* ostyles with -o prefix */
707d29b2c44Sab196087 	ELFEDIT_CONST_BOOL =		2,	/* boolean names */
7084f680cc6SAli Bahrami 	ELFEDIT_CONST_SHT_STRTAB =	3,	/* ELF SHT_STRTAB */
7094f680cc6SAli Bahrami 	ELFEDIT_CONST_SHT_SYMTAB =	4,	/* ELF SHT_SYMTAB */
7104f680cc6SAli Bahrami 	ELFEDIT_CONST_SHT_DYNSYM =	5,	/* ELF SHT_DYNSYM */
7114f680cc6SAli Bahrami 	ELFEDIT_CONST_SHT_LDYNSYM =	6,	/* ELF SHT_SUNW_LDYNSYM */
7124f680cc6SAli Bahrami 	ELFEDIT_CONST_SHN =		7,	/* ELF SHN_ section indexes  */
7134f680cc6SAli Bahrami 	ELFEDIT_CONST_SHT =		8,	/* ELF SHT_ section types  */
7144f680cc6SAli Bahrami 	ELFEDIT_CONST_SHT_ALLSYMTAB =	9,	/* ELF SHT_ symbol table */
715d29b2c44Sab196087 						/*	section types */
716d29b2c44Sab196087 	ELFEDIT_CONST_DT =		10,	/* Dynamic tags: DT_ */
717d29b2c44Sab196087 	ELFEDIT_CONST_DF =		11,	/* DT_FLAGS bits */
718d29b2c44Sab196087 	ELFEDIT_CONST_DF_P1 =		12,	/* DF_POSFLAG_1 bits */
719d29b2c44Sab196087 	ELFEDIT_CONST_DF_1 =		13,	/* DT_FLAGS_1 bits */
720d29b2c44Sab196087 	ELFEDIT_CONST_DTF_1 =		14,	/* DT_FEATURE_1 bits */
721d29b2c44Sab196087 	ELFEDIT_CONST_EI =		15,	/* ELF header e_ident indexes */
722d29b2c44Sab196087 	ELFEDIT_CONST_ET =		16,	/* Ehdr obj type */
723d29b2c44Sab196087 	ELFEDIT_CONST_ELFCLASS =	17,	/* Ehdr wordsize (32,64) */
724d29b2c44Sab196087 	ELFEDIT_CONST_ELFDATA =		18,	/* Ehdr endian */
725d29b2c44Sab196087 	ELFEDIT_CONST_EF =		19,	/* Ehdr flags */
726d29b2c44Sab196087 	ELFEDIT_CONST_EV =		20,	/* Ehdr version */
727d29b2c44Sab196087 	ELFEDIT_CONST_EM =		21,	/* Ehdr machine */
728d29b2c44Sab196087 	ELFEDIT_CONST_ELFOSABI =	22,	/* Ehdr ABI */
7294f680cc6SAli Bahrami 	ELFEDIT_CONST_EAV =		23,	/* Ehdr ABI version */
7304f680cc6SAli Bahrami 	ELFEDIT_CONST_PT =		24,	/* Phdr type */
7314f680cc6SAli Bahrami 	ELFEDIT_CONST_PF =		25,	/* Phdr flags */
7324f680cc6SAli Bahrami 	ELFEDIT_CONST_SHF =		26,	/* Shdr flags */
7334f680cc6SAli Bahrami 	ELFEDIT_CONST_STB =		27,	/* Sym binding */
7344f680cc6SAli Bahrami 	ELFEDIT_CONST_STT =		28,	/* Sym type */
7354f680cc6SAli Bahrami 	ELFEDIT_CONST_STV =		29,	/* Sym visibility */
7364f680cc6SAli Bahrami 	ELFEDIT_CONST_SYMINFO_BT =	30,	/* Syminfo boundto */
7374f680cc6SAli Bahrami 	ELFEDIT_CONST_SYMINFO_FLG =	31,	/* Syminfo flags */
7384f680cc6SAli Bahrami 	ELFEDIT_CONST_CA =		32,	/* Capabilities tags */
739*08278a5eSRod Evans 	ELFEDIT_CONST_HW1_SUNW =	33,	/* hardware capabilities */
740d29b2c44Sab196087 	ELFEDIT_CONST_SF1_SUNW =	34,	/* software capabilities */
741*08278a5eSRod Evans 	ELFEDIT_CONST_HW2_SUNW =	35,	/* hardware capabilities */
7424f680cc6SAli Bahrami 
743*08278a5eSRod Evans 	ELFEDIT_CONST_NUM =		36,	/* # of constant types */
744d29b2c44Sab196087 } elfedit_const_t;
745d29b2c44Sab196087 
746d29b2c44Sab196087 /*
747d29b2c44Sab196087  * Given an elfedit_const_t, return the array of elfedit_atoui_sym_t
748d29b2c44Sab196087  * entries that it represents.
749d29b2c44Sab196087  */
750d29b2c44Sab196087 extern elfedit_atoui_sym_t *elfedit_const_to_atoui(elfedit_const_t const_type);
751d29b2c44Sab196087 
752d29b2c44Sab196087 /*
753d29b2c44Sab196087  * ato[u]i and const routines, used to turn strings into numeric values,
754d29b2c44Sab196087  * with support for mapping symbol names to numbers, and range checking.
755d29b2c44Sab196087  */
756d29b2c44Sab196087 extern elfedit_atoi_t elfedit_atoi(const char *str,
757d29b2c44Sab196087     const elfedit_atoi_sym_t *sym);
758d29b2c44Sab196087 extern elfedit_atoui_t elfedit_atoui(const char *str,
759d29b2c44Sab196087     const elfedit_atoui_sym_t *sym);
760d29b2c44Sab196087 extern elfedit_atoui_t elfedit_atoconst(const char *str,
761d29b2c44Sab196087     elfedit_const_t const_type);
762d29b2c44Sab196087 
763d29b2c44Sab196087 extern int elfedit_atoi2(const char *str, const elfedit_atoi_sym_t *sym,
764d29b2c44Sab196087     elfedit_atoi_t *v);
765d29b2c44Sab196087 extern int elfedit_atoui2(const char *str, const elfedit_atoui_sym_t *sym,
766d29b2c44Sab196087     elfedit_atoui_t *);
767d29b2c44Sab196087 extern int elfedit_atoconst2(const char *str, elfedit_const_t const_type,
768d29b2c44Sab196087     elfedit_atoui_t *);
769d29b2c44Sab196087 
770d29b2c44Sab196087 extern elfedit_atoi_t elfedit_atoi_range(const char *str,
771d29b2c44Sab196087     const char *item_name, elfedit_atoi_t min, elfedit_atoi_t max,
772d29b2c44Sab196087     const elfedit_atoi_sym_t *sym);
773d29b2c44Sab196087 extern elfedit_atoui_t elfedit_atoui_range(const char *str,
774d29b2c44Sab196087     const char *item_name, elfedit_atoui_t min, elfedit_atoui_t max,
775d29b2c44Sab196087     const elfedit_atoui_sym_t *sym);
776d29b2c44Sab196087 extern elfedit_atoui_t elfedit_atoconst_range(const char *str,
777d29b2c44Sab196087     const char *item_name, elfedit_atoui_t min, elfedit_atoui_t max,
778d29b2c44Sab196087     elfedit_const_t const_type);
779d29b2c44Sab196087 
780d29b2c44Sab196087 extern int elfedit_atoi_range2(const char *str, elfedit_atoi_t min,
781d29b2c44Sab196087     elfedit_atoi_t max, const elfedit_atoi_sym_t *sym, elfedit_atoi_t *v);
782d29b2c44Sab196087 extern int elfedit_atoui_range2(const char *str, elfedit_atoui_t min,
783d29b2c44Sab196087     elfedit_atoui_t max, const elfedit_atoui_sym_t *sym, elfedit_atoui_t *v);
784d29b2c44Sab196087 extern int elfedit_atoconst_range2(const char *str, elfedit_atoui_t min,
785d29b2c44Sab196087     elfedit_atoui_t max, elfedit_const_t const_type, elfedit_atoui_t *v);
786d29b2c44Sab196087 
787d29b2c44Sab196087 extern const char *elfedit_atoi_value_to_str(const elfedit_atoi_sym_t *sym,
788d29b2c44Sab196087     elfedit_atoi_t value, int required);
789d29b2c44Sab196087 extern const char *elfedit_atoui_value_to_str(const elfedit_atoui_sym_t *sym,
790d29b2c44Sab196087     elfedit_atoui_t value, int required);
791d29b2c44Sab196087 extern const char *elfedit_atoconst_value_to_str(elfedit_const_t const_type,
792d29b2c44Sab196087     elfedit_atoui_t value, int required);
793d29b2c44Sab196087 
794d29b2c44Sab196087 extern void elfedit_cpl_atoi(void *cpldata, const elfedit_atoi_sym_t *sym);
795d29b2c44Sab196087 extern void elfedit_cpl_atoui(void *cpldata, const elfedit_atoui_sym_t *sym);
796d29b2c44Sab196087 extern void elfedit_cpl_atoconst(void *cpldata, elfedit_const_t const_type);
797d29b2c44Sab196087 
798d29b2c44Sab196087 
799d29b2c44Sab196087 /*
800d29b2c44Sab196087  * Convenience functions built on top of the ato[u]i routines.
801d29b2c44Sab196087  */
802d29b2c44Sab196087 extern int elfedit_atobool(const char *str, const char *item_name);
803d29b2c44Sab196087 extern elfedit_atoui_t elfedit_atoshndx(const char *str, size_t shnum);
804d29b2c44Sab196087 
805d29b2c44Sab196087 
806d29b2c44Sab196087 /*
807d29b2c44Sab196087  * elfedit provides a getopt utility for use by the module commands.
808d29b2c44Sab196087  * elfedit_getopt_state_t is the state block used by elfedit_getopt().
809d29b2c44Sab196087  * elfedit_getopt_ret_t is the definition of the values returned to
810d29b2c44Sab196087  * the user by elfedit_getopt() when an option is matched. Elfedit
811d29b2c44Sab196087  * getopt processing is done as follows:
812d29b2c44Sab196087  *
813d29b2c44Sab196087  * 1) The caller initializes an elfedit_getopt_state_t struct via
814d29b2c44Sab196087  *	a call to elfedit_getopt_init(). The contents of this structure
815d29b2c44Sab196087  *	must not be accessed by the caller, as they are all private and
816d29b2c44Sab196087  *	subject to change.
817d29b2c44Sab196087  * 2) Repeated calls are made to elfedit_getopt(), as long as it returns
818d29b2c44Sab196087  *	a non-NULL pointer to an elfedit_getopt_ret_t structure. If the
819d29b2c44Sab196087  *	matched option has a value (ELFEDIT_CMDOA_F_VALUE), then the gor_value
820d29b2c44Sab196087  *	field contains the pointer to the string. Otherwise, gor_value is NULL.
821d29b2c44Sab196087  * 3) As elfedit_getopt() consumes optional arguments from the argc/argv
822d29b2c44Sab196087  *	passed to elfedit_getopt_init(), it adjusts argc/argc to skip over
823d29b2c44Sab196087  *	them. Once elfedit_getopt() returns NULL to indicate that there are no
824d29b2c44Sab196087  *	more options to match, argc/argv have been adjusted so that they
825d29b2c44Sab196087  *	reference the plain arguments.
826d29b2c44Sab196087  */
827d29b2c44Sab196087 typedef struct {
828d29b2c44Sab196087 	elfedit_cmd_oa_mask_t gor_idmask;	/* oa_idmask from matching */
829d29b2c44Sab196087 					/*	elfedit_cmd_optarg_t. Can be */
830d29b2c44Sab196087 					/*	used to quickly identify opt */
831d29b2c44Sab196087 	const char	*gor_value;	/* Opt value if ELFEDIT_CMDOA_F_VALUE */
832d29b2c44Sab196087 					/*	Otherwise, NULL */
833d29b2c44Sab196087 } elfedit_getopt_ret_t;
834d29b2c44Sab196087 typedef struct {
835d29b2c44Sab196087 	int			*go_argc;	/* Pointer to # of options */
836d29b2c44Sab196087 	const char		***go_argv;	/* Ptr to array of opt strs */
837d29b2c44Sab196087 	elfedit_cmd_optarg_t	*go_optarg;	/* Array of allowed options */
838d29b2c44Sab196087 	elfedit_cmd_oa_mask_t	go_idmask;	/* Combined id masks of all */
839d29b2c44Sab196087 						/*	seen options */
840d29b2c44Sab196087 	int			go_done;	/* True if last option seen */
841d29b2c44Sab196087 	const char		*go_sglgrp;	/* Group of 1-letter opts */
842d29b2c44Sab196087 	elfedit_getopt_ret_t	go_ret;		/* Data returned to user */
843d29b2c44Sab196087 } elfedit_getopt_state_t;
844d29b2c44Sab196087 
845d29b2c44Sab196087 
846d29b2c44Sab196087 
847d29b2c44Sab196087 /*
848d29b2c44Sab196087  * getopt related routines
849d29b2c44Sab196087  */
850d29b2c44Sab196087 extern void elfedit_getopt_init(elfedit_getopt_state_t *,
851d29b2c44Sab196087     int *, const char ***);
852d29b2c44Sab196087 extern elfedit_getopt_ret_t *elfedit_getopt(elfedit_getopt_state_t *);
853d29b2c44Sab196087 
854d29b2c44Sab196087 
855d29b2c44Sab196087 
856d29b2c44Sab196087 /*
857d29b2c44Sab196087  * Additional utility functions exported for use by modules
858d29b2c44Sab196087  */
859d29b2c44Sab196087 extern void elfedit_array_elts_delete(const char *name_str, void *data_start,
860d29b2c44Sab196087     size_t entsize, size_t num_ent, size_t start_ndx, size_t cnt);
861d29b2c44Sab196087 
862d29b2c44Sab196087 extern void elfedit_array_elts_move(const char *name_str, void *data_start,
863d29b2c44Sab196087     size_t entsize, size_t num_ent, size_t srcndx,
864d29b2c44Sab196087     size_t dstndx, size_t cnt, void *scr_item);
865d29b2c44Sab196087 
866d29b2c44Sab196087 extern int elfedit_bits_set(u_longlong_t v, int sizeof_orig_v);
867d29b2c44Sab196087 
868d29b2c44Sab196087 extern void elfedit32_dyn_elt_init(elfedit32_dyn_elt_t *dyn_elt);
869d29b2c44Sab196087 extern void elfedit64_dyn_elt_init(elfedit64_dyn_elt_t *dyn_elt);
870d29b2c44Sab196087 
871d29b2c44Sab196087 extern void elfedit32_dyn_elt_save(elfedit32_dyn_elt_t *elt, Elf32_Word ndx,
872d29b2c44Sab196087     Elf32_Dyn *dyn);
873d29b2c44Sab196087 extern void elfedit64_dyn_elt_save(elfedit64_dyn_elt_t *elt, Elf64_Word ndx,
874d29b2c44Sab196087     Elf64_Dyn *dyn);
875d29b2c44Sab196087 
876d29b2c44Sab196087 const char *elfedit32_dyn_offset_to_str(elfedit32_section_t *strsec,
877d29b2c44Sab196087     elfedit32_dyn_elt_t *dynelt);
878d29b2c44Sab196087 const char *elfedit64_dyn_offset_to_str(elfedit64_section_t *strsec,
879d29b2c44Sab196087     elfedit64_dyn_elt_t *dynelt);
880d29b2c44Sab196087 
8814f680cc6SAli Bahrami extern int elfedit32_dynstr_getpad(elfedit32_obj_state_t *obj_state,
8824f680cc6SAli Bahrami     elfedit32_section_t *dynsec, elfedit32_dyn_elt_t *dyn_strpad);
8834f680cc6SAli Bahrami extern int elfedit64_dynstr_getpad(elfedit64_obj_state_t *obj_state,
8844f680cc6SAli Bahrami     elfedit64_section_t *dynsec, elfedit64_dyn_elt_t *dyn_strpad);
885d29b2c44Sab196087 
886d29b2c44Sab196087 extern Elf32_Word elfedit32_dynstr_insert(elfedit32_section_t *dynsec,
887d29b2c44Sab196087     elfedit32_section_t *strsec, elfedit32_dyn_elt_t *dyn_strpad,
888d29b2c44Sab196087     const char *str);
889d29b2c44Sab196087 extern Elf64_Word elfedit64_dynstr_insert(elfedit64_section_t *dynsec,
890d29b2c44Sab196087     elfedit64_section_t *strsec, elfedit64_dyn_elt_t *dyn_strpad,
891d29b2c44Sab196087     const char *str);
892d29b2c44Sab196087 
893d29b2c44Sab196087 extern void elfedit32_modified_data(elfedit32_section_t *s);
894d29b2c44Sab196087 extern void elfedit64_modified_data(elfedit64_section_t *s);
895d29b2c44Sab196087 
896d29b2c44Sab196087 extern void elfedit32_modified_ehdr(elfedit32_obj_state_t *obj_state);
897d29b2c44Sab196087 extern void elfedit64_modified_ehdr(elfedit64_obj_state_t *obj_state);
898d29b2c44Sab196087 
899d29b2c44Sab196087 extern void elfedit32_modified_phdr(elfedit32_obj_state_t *obj_state);
900d29b2c44Sab196087 extern void elfedit64_modified_phdr(elfedit64_obj_state_t *obj_state);
901d29b2c44Sab196087 
902d29b2c44Sab196087 extern void elfedit32_modified_shdr(elfedit32_section_t *s);
903d29b2c44Sab196087 extern void elfedit64_modified_shdr(elfedit64_section_t *s);
904d29b2c44Sab196087 
905d29b2c44Sab196087 extern Elf32_Word elfedit32_name_to_shndx(elfedit32_obj_state_t *obj_state,
906d29b2c44Sab196087     const char *shnam);
907d29b2c44Sab196087 extern Elf64_Word elfedit64_name_to_shndx(elfedit64_obj_state_t *obj_state,
908d29b2c44Sab196087     const char *shnam);
909d29b2c44Sab196087 
910d29b2c44Sab196087 extern int elfedit32_name_to_symndx(elfedit32_section_t *symsec,
911d29b2c44Sab196087     elfedit32_section_t *strsec, const char *name, elfedit_msg_t msg_type,
912d29b2c44Sab196087     Elf32_Word *ret_symndx);
913d29b2c44Sab196087 extern int elfedit64_name_to_symndx(elfedit64_section_t *symsec,
914d29b2c44Sab196087     elfedit64_section_t *strsec, const char *name, elfedit_msg_t msg_type,
915d29b2c44Sab196087     Elf64_Word *ret_symndx);
916d29b2c44Sab196087 
917d29b2c44Sab196087 extern const char *elfedit32_offset_to_str(elfedit32_section_t *strsec,
918d29b2c44Sab196087     Elf32_Word offset, elfedit_msg_t msg_type, int debug_msg);
919d29b2c44Sab196087 extern const char *elfedit64_offset_to_str(elfedit64_section_t *strsec,
920d29b2c44Sab196087     Elf64_Word offset, elfedit_msg_t msg_type, int debug_msg);
921d29b2c44Sab196087 
922d29b2c44Sab196087 extern int elfedit32_sec_findstr(elfedit32_section_t *sec, Elf32_Word tail_ign,
923d29b2c44Sab196087     const char *str, Elf32_Word *ret_offset);
924d29b2c44Sab196087 extern int elfedit64_sec_findstr(elfedit64_section_t *sec, Elf64_Word tail_ign,
925d29b2c44Sab196087     const char *str, Elf64_Word *ret_offset);
926d29b2c44Sab196087 
927cce0e03bSab196087 extern elfedit32_section_t *elfedit32_sec_get(
928cce0e03bSab196087     elfedit32_obj_state_t *obj_state, Elf32_Word shndx);
929cce0e03bSab196087 extern elfedit64_section_t *elfedit64_sec_get(
930cce0e03bSab196087     elfedit64_obj_state_t *obj_state, Elf64_Word shndx);
931cce0e03bSab196087 
932d29b2c44Sab196087 extern elfedit32_section_t *elfedit32_sec_getcap(
933d29b2c44Sab196087     elfedit32_obj_state_t *obj_state, Elf32_Cap **cap, Elf32_Word *num);
934d29b2c44Sab196087 extern elfedit64_section_t *elfedit64_sec_getcap(
935d29b2c44Sab196087     elfedit64_obj_state_t *obj_state, Elf64_Cap **cap, Elf64_Word *num);
936d29b2c44Sab196087 
937d29b2c44Sab196087 extern elfedit32_section_t *elfedit32_sec_getdyn(
938d29b2c44Sab196087     elfedit32_obj_state_t *obj_state, Elf32_Dyn **dyn, Elf32_Word *num);
939d29b2c44Sab196087 extern elfedit64_section_t *elfedit64_sec_getdyn(
940d29b2c44Sab196087     elfedit64_obj_state_t *obj_state, Elf64_Dyn **dyn, Elf64_Word *num);
941d29b2c44Sab196087 
942d29b2c44Sab196087 extern elfedit32_section_t *elfedit32_sec_getstr(
94355ef6355Sab196087     elfedit32_obj_state_t *obj_state, Elf32_Word shndx, int);
944d29b2c44Sab196087 extern elfedit64_section_t *elfedit64_sec_getstr(
94555ef6355Sab196087     elfedit64_obj_state_t *obj_state, Elf64_Word shndx, int);
946d29b2c44Sab196087 
947d29b2c44Sab196087 extern elfedit32_section_t *elfedit32_sec_getsyminfo(
948d29b2c44Sab196087     elfedit32_obj_state_t *obj_state, Elf32_Syminfo **syminfo, Elf32_Word *num);
949d29b2c44Sab196087 extern elfedit64_section_t *elfedit64_sec_getsyminfo(
950d29b2c44Sab196087     elfedit64_obj_state_t *obj_state, Elf64_Syminfo **syminfo, Elf64_Word *num);
951d29b2c44Sab196087 
952d29b2c44Sab196087 extern elfedit32_section_t *elfedit32_sec_getsymtab(
953d29b2c44Sab196087     elfedit32_obj_state_t *obj_state, int by_index, Elf32_Word index,
954d29b2c44Sab196087     const char *name, Elf32_Sym **sym, Elf32_Word *num,
955d29b2c44Sab196087     elfedit32_symtab_t **aux_info);
956d29b2c44Sab196087 extern elfedit64_section_t *elfedit64_sec_getsymtab(
957d29b2c44Sab196087     elfedit64_obj_state_t *obj_state, int by_index, Elf64_Word index,
958d29b2c44Sab196087     const char *name, Elf64_Sym **sym, Elf64_Word *num,
959d29b2c44Sab196087     elfedit64_symtab_t **aux_info);
960d29b2c44Sab196087 
961d29b2c44Sab196087 extern elfedit32_section_t *elfedit32_sec_getversym(
962d29b2c44Sab196087     elfedit32_obj_state_t *obj_state, elfedit32_section_t *symsec,
963d29b2c44Sab196087     Elf32_Versym **versym, Elf32_Word *num);
964d29b2c44Sab196087 extern elfedit64_section_t *elfedit64_sec_getversym(
965d29b2c44Sab196087     elfedit64_obj_state_t *obj_state, elfedit64_section_t *symsec,
966d29b2c44Sab196087     Elf64_Versym **versym, Elf64_Word *num);
967d29b2c44Sab196087 
968d29b2c44Sab196087 extern elfedit32_section_t *elfedit32_sec_getxshndx(
969d29b2c44Sab196087     elfedit32_obj_state_t *obj_state, elfedit32_section_t *symsec,
970d29b2c44Sab196087     Elf32_Word **xshndx, Elf32_Word *num);
971d29b2c44Sab196087 extern elfedit64_section_t *elfedit64_sec_getxshndx(
972d29b2c44Sab196087     elfedit64_obj_state_t *obj_state, elfedit64_section_t *symsec,
973d29b2c44Sab196087     Elf64_Word **xshndx, Elf64_Word *num);
974d29b2c44Sab196087 
9754f680cc6SAli Bahrami extern int elfedit32_sec_issymtab(elfedit32_obj_state_t *obj_state,
9764f680cc6SAli Bahrami     elfedit32_section_t *sec, int issue_err, elfedit_atoui_sym_t **atoui_list);
9774f680cc6SAli Bahrami extern int elfedit64_sec_issymtab(elfedit64_obj_state_t *obj_state,
9784f680cc6SAli Bahrami     elfedit64_section_t *sec, int issue_err, elfedit_atoui_sym_t **atoui_list);
979d29b2c44Sab196087 
980d29b2c44Sab196087 extern const char *elfedit32_sec_msgprefix(elfedit32_section_t *sec);
981d29b2c44Sab196087 extern const char *elfedit64_sec_msgprefix(elfedit64_section_t *sec);
982d29b2c44Sab196087 
983d29b2c44Sab196087 extern const char *elfedit32_shndx_to_name(elfedit32_obj_state_t *obj_state,
984d29b2c44Sab196087     Elf32_Word shndx);
985d29b2c44Sab196087 extern const char *elfedit64_shndx_to_name(elfedit64_obj_state_t *obj_state,
986d29b2c44Sab196087     Elf64_Word shndx);
987d29b2c44Sab196087 
988d29b2c44Sab196087 extern Elf32_Word elfedit32_strtab_insert(elfedit32_obj_state_t *obj_state,
989d29b2c44Sab196087     elfedit32_section_t *strsec, elfedit32_section_t *dynsec, const char *str);
990d29b2c44Sab196087 extern Elf64_Word elfedit64_strtab_insert(elfedit64_obj_state_t *obj_state,
991d29b2c44Sab196087     elfedit64_section_t *strsec, elfedit64_section_t *dynsec, const char *str);
992d29b2c44Sab196087 
993d29b2c44Sab196087 extern void elfedit32_strtab_insert_test(elfedit32_obj_state_t *obj_state,
994d29b2c44Sab196087     elfedit32_section_t *strsec, elfedit32_section_t *dynsec, const char *str);
995d29b2c44Sab196087 extern void elfedit64_strtab_insert_test(elfedit64_obj_state_t *obj_state,
996d29b2c44Sab196087     elfedit64_section_t *strsec, elfedit64_section_t *dynsec, const char *str);
997d29b2c44Sab196087 
9984f680cc6SAli Bahrami extern int elfedit32_test_osabi(elfedit32_obj_state_t *obj_state, uchar_t osabi,
9994f680cc6SAli Bahrami     int issue_err);
10004f680cc6SAli Bahrami extern int elfedit64_test_osabi(elfedit64_obj_state_t *obj_state, uchar_t osabi,
10014f680cc6SAli Bahrami     int issue_err);
10024f680cc6SAli Bahrami 
1003d29b2c44Sab196087 extern Elf32_Word elfedit32_type_to_shndx(elfedit32_obj_state_t *obj_state,
1004d29b2c44Sab196087     Elf32_Word shtype);
1005d29b2c44Sab196087 extern Elf64_Word elfedit64_type_to_shndx(elfedit64_obj_state_t *obj_state,
1006d29b2c44Sab196087     Elf64_Word shtype);
1007d29b2c44Sab196087 
1008d29b2c44Sab196087 
1009d29b2c44Sab196087 
1010d29b2c44Sab196087 /*
1011d29b2c44Sab196087  * Map the generic names for each of the ELFCLASS specific routines
1012d29b2c44Sab196087  * above to reference the proper routine for the current compilation.
1013d29b2c44Sab196087  */
1014d29b2c44Sab196087 #ifdef _ELF64
1015d29b2c44Sab196087 #define	elfedit_dyn_elt_init		elfedit64_dyn_elt_init
1016d29b2c44Sab196087 #define	elfedit_dyn_elt_save		elfedit64_dyn_elt_save
1017d29b2c44Sab196087 #define	elfedit_dyn_offset_to_str	elfedit64_dyn_offset_to_str
1018d29b2c44Sab196087 #define	elfedit_dynstr_getpad		elfedit64_dynstr_getpad
1019d29b2c44Sab196087 #define	elfedit_dynstr_insert		elfedit64_dynstr_insert
1020d29b2c44Sab196087 #define	elfedit_modified_data		elfedit64_modified_data
1021d29b2c44Sab196087 #define	elfedit_modified_ehdr		elfedit64_modified_ehdr
1022d29b2c44Sab196087 #define	elfedit_modified_phdr		elfedit64_modified_phdr
1023d29b2c44Sab196087 #define	elfedit_modified_shdr		elfedit64_modified_shdr
1024d29b2c44Sab196087 #define	elfedit_name_to_shndx		elfedit64_name_to_shndx
1025d29b2c44Sab196087 #define	elfedit_name_to_symndx		elfedit64_name_to_symndx
1026d29b2c44Sab196087 #define	elfedit_offset_to_str		elfedit64_offset_to_str
1027d29b2c44Sab196087 #define	elfedit_sec_findstr		elfedit64_sec_findstr
1028cce0e03bSab196087 #define	elfedit_sec_get			elfedit64_sec_get
1029d29b2c44Sab196087 #define	elfedit_sec_getcap		elfedit64_sec_getcap
1030d29b2c44Sab196087 #define	elfedit_sec_getdyn		elfedit64_sec_getdyn
1031d29b2c44Sab196087 #define	elfedit_sec_getstr		elfedit64_sec_getstr
1032d29b2c44Sab196087 #define	elfedit_sec_getsyminfo		elfedit64_sec_getsyminfo
1033d29b2c44Sab196087 #define	elfedit_sec_getsymtab		elfedit64_sec_getsymtab
1034d29b2c44Sab196087 #define	elfedit_sec_getversym		elfedit64_sec_getversym
1035d29b2c44Sab196087 #define	elfedit_sec_getxshndx		elfedit64_sec_getxshndx
1036d29b2c44Sab196087 #define	elfedit_sec_issymtab		elfedit64_sec_issymtab
1037d29b2c44Sab196087 #define	elfedit_shndx_to_name		elfedit64_shndx_to_name
1038d29b2c44Sab196087 #define	elfedit_sec_msgprefix		elfedit64_sec_msgprefix
1039d29b2c44Sab196087 #define	elfedit_strtab_insert		elfedit64_strtab_insert
1040d29b2c44Sab196087 #define	elfedit_strtab_insert_test	elfedit64_strtab_insert_test
10414f680cc6SAli Bahrami #define	elfedit_test_osabi		elfedit64_test_osabi
1042d29b2c44Sab196087 #define	elfedit_type_to_shndx		elfedit64_type_to_shndx
1043d29b2c44Sab196087 #else
1044d29b2c44Sab196087 #define	elfedit_dyn_elt_init		elfedit32_dyn_elt_init
1045d29b2c44Sab196087 #define	elfedit_dyn_elt_save		elfedit32_dyn_elt_save
1046d29b2c44Sab196087 #define	elfedit_dyn_offset_to_str	elfedit32_dyn_offset_to_str
1047d29b2c44Sab196087 #define	elfedit_dynstr_getpad		elfedit32_dynstr_getpad
1048d29b2c44Sab196087 #define	elfedit_dynstr_insert		elfedit32_dynstr_insert
1049d29b2c44Sab196087 #define	elfedit_modified_data		elfedit32_modified_data
1050d29b2c44Sab196087 #define	elfedit_modified_ehdr		elfedit32_modified_ehdr
1051d29b2c44Sab196087 #define	elfedit_modified_phdr		elfedit32_modified_phdr
1052d29b2c44Sab196087 #define	elfedit_modified_shdr		elfedit32_modified_shdr
1053d29b2c44Sab196087 #define	elfedit_name_to_shndx		elfedit32_name_to_shndx
1054d29b2c44Sab196087 #define	elfedit_name_to_symndx		elfedit32_name_to_symndx
1055d29b2c44Sab196087 #define	elfedit_offset_to_str		elfedit32_offset_to_str
1056d29b2c44Sab196087 #define	elfedit_sec_findstr		elfedit32_sec_findstr
1057cce0e03bSab196087 #define	elfedit_sec_get			elfedit32_sec_get
1058d29b2c44Sab196087 #define	elfedit_sec_getcap		elfedit32_sec_getcap
1059d29b2c44Sab196087 #define	elfedit_sec_getdyn		elfedit32_sec_getdyn
1060d29b2c44Sab196087 #define	elfedit_sec_getstr		elfedit32_sec_getstr
1061d29b2c44Sab196087 #define	elfedit_sec_getsyminfo		elfedit32_sec_getsyminfo
1062d29b2c44Sab196087 #define	elfedit_sec_getsymtab		elfedit32_sec_getsymtab
1063d29b2c44Sab196087 #define	elfedit_sec_getversym		elfedit32_sec_getversym
1064d29b2c44Sab196087 #define	elfedit_sec_getxshndx		elfedit32_sec_getxshndx
1065d29b2c44Sab196087 #define	elfedit_sec_issymtab		elfedit32_sec_issymtab
1066d29b2c44Sab196087 #define	elfedit_shndx_to_name		elfedit32_shndx_to_name
1067d29b2c44Sab196087 #define	elfedit_sec_msgprefix		elfedit32_sec_msgprefix
1068d29b2c44Sab196087 #define	elfedit_strtab_insert		elfedit32_strtab_insert
1069d29b2c44Sab196087 #define	elfedit_strtab_insert_test	elfedit32_strtab_insert_test
10704f680cc6SAli Bahrami #define	elfedit_test_osabi		elfedit32_test_osabi
1071d29b2c44Sab196087 #define	elfedit_type_to_shndx		elfedit32_type_to_shndx
1072d29b2c44Sab196087 #endif
1073d29b2c44Sab196087 
1074d29b2c44Sab196087 
1075d29b2c44Sab196087 #ifdef	__cplusplus
1076d29b2c44Sab196087 }
1077d29b2c44Sab196087 #endif
1078d29b2c44Sab196087 
1079d29b2c44Sab196087 #endif	/* _ELFEDIT_H */
1080