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