xref: /titanic_50/usr/src/cmd/sgs/elfedit/common/_elfedit.h (revision 4f680cc668fa6cf678c531083400ade9a9c7934c)
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 /*
23*4f680cc6SAli Bahrami  * Copyright 2009 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	<setjmp.h>
31d29b2c44Sab196087 #include	<libtecla.h>
32d29b2c44Sab196087 #include	<elfedit.h>
33d29b2c44Sab196087 
34d29b2c44Sab196087 /*
35d29b2c44Sab196087  * Local include file for elfedit.
36d29b2c44Sab196087  */
37d29b2c44Sab196087 #ifdef	__cplusplus
38d29b2c44Sab196087 extern "C" {
39d29b2c44Sab196087 #endif
40d29b2c44Sab196087 
41d29b2c44Sab196087 
42d29b2c44Sab196087 /*
43d29b2c44Sab196087  * Maximum command line, and history
44d29b2c44Sab196087  */
45d29b2c44Sab196087 #define	ELFEDIT_MAXCMD	1024
46d29b2c44Sab196087 #define	ELFEDIT_MAXHIST	1024
47d29b2c44Sab196087 
48d29b2c44Sab196087 /* Maximum number of command completion arguments */
49d29b2c44Sab196087 #define	ELFEDIT_MAXCPLARGS	128
50d29b2c44Sab196087 
51d29b2c44Sab196087 /* Maximum length of a module name */
52d29b2c44Sab196087 #define	ELFEDIT_MAXMODNAM	64
53d29b2c44Sab196087 
54d29b2c44Sab196087 
55d29b2c44Sab196087 /*
56d29b2c44Sab196087  * In elfedit.h, you will find elfedit32_cmd_t and elfedit64_cmd_t
57d29b2c44Sab196087  * typedefs. These types are identical, except for the definition
58d29b2c44Sab196087  * of the cmd_func and cmd_cplfunc function pointers. These function
59d29b2c44Sab196087  * pointers have different argument definitions that reflect the
60d29b2c44Sab196087  * different object state definition blocks for the 32 and 64-bit cases.
61d29b2c44Sab196087  * Yet, From a strictly machine based view, these two types are identical
62d29b2c44Sab196087  * in size and layout:
63d29b2c44Sab196087  *
64d29b2c44Sab196087  *	- At the machine level, all function pointers are simply
65d29b2c44Sab196087  *		machine sized words containing an address.
66d29b2c44Sab196087  *
67d29b2c44Sab196087  *	- Other than the function pointers, the remaining fields
68d29b2c44Sab196087  *		are exactly the same in both cases.
69d29b2c44Sab196087  *
70d29b2c44Sab196087  * The vast majority of elfedit's internals that examine elfedit_cmd_t
71d29b2c44Sab196087  * are looking at the non-function pointer fields. It simplfiies
72d29b2c44Sab196087  * a great deal of code if we can treat elfedit32_cmd_t and elfedit64_cmd_t
73d29b2c44Sab196087  * as equivalent types for this purpose. In C++, we would do this with
74d29b2c44Sab196087  * a superclass. In C, we do it by defining another variant named
75d29b2c44Sab196087  * elfeditGC_cmd_t (GC stands for "Generic Class"). The function pointers
76d29b2c44Sab196087  * are replaced with (void *) pointers. This variant has the same size
77d29b2c44Sab196087  * and layout as the others. We use it internally to represent either type.
78d29b2c44Sab196087  * In the cases where we need to use the function pointers, we first cast
79d29b2c44Sab196087  * them to the proper type for the ELFCLASS being processed.
80d29b2c44Sab196087  *
81d29b2c44Sab196087  * The existance of elfeditGC_cmd_t implies the need for elfeditGC_module_t,
82d29b2c44Sab196087  * for the same reasons.
83d29b2c44Sab196087  *
84d29b2c44Sab196087  * It is extremely important that these definitions exactly mirror the
85d29b2c44Sab196087  * definitions in elfedit.h.
86d29b2c44Sab196087  */
87d29b2c44Sab196087 typedef struct {
88d29b2c44Sab196087 	void			*cmd_func;
89d29b2c44Sab196087 	void			*cmd_cplfunc;
90d29b2c44Sab196087 	const char		**cmd_name;
91d29b2c44Sab196087 	elfedit_i18nhdl_t	cmd_desc;
92d29b2c44Sab196087 	elfedit_i18nhdl_t	cmd_help;
93d29b2c44Sab196087 	elfedit_cmd_optarg_t	*cmd_opt;
94d29b2c44Sab196087 	elfedit_cmd_optarg_t	*cmd_args;
95d29b2c44Sab196087 } elfeditGC_cmd_t;
96d29b2c44Sab196087 
97d29b2c44Sab196087 
98d29b2c44Sab196087 typedef struct {
99d29b2c44Sab196087 	elfedit_module_version_t mod_version;
100d29b2c44Sab196087 	const char		*mod_name;
101d29b2c44Sab196087 	elfedit_i18nhdl_t	mod_desc;
102d29b2c44Sab196087 	elfeditGC_cmd_t		*mod_cmds;
103d29b2c44Sab196087 	elfedit_mod_i18nhdl_to_str_func_t mod_i18nhdl_to_str;
104d29b2c44Sab196087 } elfeditGC_module_t;
105d29b2c44Sab196087 
106d29b2c44Sab196087 
107d29b2c44Sab196087 /*
108d29b2c44Sab196087  * The result of parsing a user command is one of these blocks entered
109d29b2c44Sab196087  * at the end of state.user_cmd. They encapsulate the arguments and
110d29b2c44Sab196087  * the command function to call. In combination with an elfedit_obj_state_t,
111d29b2c44Sab196087  * they contain everything needed to execute a specified operation. A single
112d29b2c44Sab196087  * call to free() suffices to release the ELFEDIT_USER_CMD and any memory
113d29b2c44Sab196087  * it references.
114d29b2c44Sab196087  */
115d29b2c44Sab196087 typedef struct user_cmd_t {
116d29b2c44Sab196087 	struct user_cmd_t *ucmd_next;	/* Commands are kept in linked list */
117d29b2c44Sab196087 	int		ucmd_argc;	/* # of arguments to command */
118d29b2c44Sab196087 	const char	**ucmd_argv;	/* Argument strings */
119d29b2c44Sab196087 	char		*ucmd_orig_str;	/* Command string as entered by user */
120d29b2c44Sab196087 	elfeditGC_module_t *ucmd_mod;	/* Module defining command */
121d29b2c44Sab196087 	elfeditGC_cmd_t	*ucmd_cmd;	/* Command to call */
122d29b2c44Sab196087 	int		ucmd_ostyle_set;	/* True if there is a per-cmd */
123d29b2c44Sab196087 						/* 	output style active */
124d29b2c44Sab196087 	elfedit_outstyle_t ucmd_ostyle; /* Per-cmd output style, if active */
125d29b2c44Sab196087 } USER_CMD_T;
126d29b2c44Sab196087 
127d29b2c44Sab196087 /*
128d29b2c44Sab196087  * MODLIST_T is used to manage module definitions. Note that a simple linked
129d29b2c44Sab196087  * list is used to maintain the set of active modules. This can be easily
130d29b2c44Sab196087  * changed if the number of modules grows to a point where the lookup
131d29b2c44Sab196087  * time is noticible.
132d29b2c44Sab196087  */
133d29b2c44Sab196087 typedef struct moddef_t {
134d29b2c44Sab196087 	struct moddef_t		*ml_next;	/* Used for list of open mods */
135d29b2c44Sab196087 	elfeditGC_module_t	*ml_mod;	/* The module definition */
136d29b2c44Sab196087 	void			*ml_dl_hdl;	/* dlopen() handle for lib */
137d29b2c44Sab196087 	const char		*ml_path;	/* Path used to open lib */
138d29b2c44Sab196087 } MODLIST_T;
139d29b2c44Sab196087 
140d29b2c44Sab196087 
141d29b2c44Sab196087 /*
142d29b2c44Sab196087  * Type of the global variable used to maintain elfedit state.
143d29b2c44Sab196087  */
144d29b2c44Sab196087 typedef struct {
145d29b2c44Sab196087 	MODLIST_T *modlist;		/* List of loaded commands */
146d29b2c44Sab196087 	elfedit_flag_t	flags;		/* ELFEDIT_F_ command line options */
147d29b2c44Sab196087 	elfedit_outstyle_t outstyle;	/* Output style */
148d29b2c44Sab196087 	struct {
149d29b2c44Sab196087 		int present;		/* True if there is a source file. */
150d29b2c44Sab196087 					/*	 False otherwise */
151d29b2c44Sab196087 		/*
152d29b2c44Sab196087 		 * The remaining file fields are not to be accessed
153d29b2c44Sab196087 		 * unless present is True.
154d29b2c44Sab196087 		 */
155d29b2c44Sab196087 		const char *infile;	/* Name of source file */
156d29b2c44Sab196087 		const char *outfile;	/* Name of file being edited */
157d29b2c44Sab196087 		int unlink_on_exit;	/* TRUE to unlink outfile on exit  */
158d29b2c44Sab196087 		int dirty;		/* TRUE if outfile needs to be saved */
159d29b2c44Sab196087 	} file;
160d29b2c44Sab196087 	struct {		/* Jump buffer used for ELFEDIT_MSG_ERR */
161d29b2c44Sab196087 		int active;	/*	True if MSG_ERR jumps to outer loop */
162d29b2c44Sab196087 		sigjmp_buf env;	/*	jump environment buffer */
163d29b2c44Sab196087 	} msg_jbuf;
164d29b2c44Sab196087 	struct {			/* Search path used to find modules */
165d29b2c44Sab196087 		size_t n;		/*	# of path segments */
166d29b2c44Sab196087 		const char **seg;	/*	path segments */
167d29b2c44Sab196087 	} modpath;
168d29b2c44Sab196087 	struct {		/* Linked list of user commands to execute */
169d29b2c44Sab196087 		size_t n;		/* # of commands */
170d29b2c44Sab196087 		USER_CMD_T *list;	/* head of list */
171d29b2c44Sab196087 		USER_CMD_T *tail;	/* points at last element of list */
172d29b2c44Sab196087 	} ucmd;
173d29b2c44Sab196087 	struct {			/* Pager related state */
174d29b2c44Sab196087 		FILE *fptr;		/* Output file */
175d29b2c44Sab196087 	} pager;
176d29b2c44Sab196087 	struct {
177d29b2c44Sab196087 		int	is_tty;		/* True in stdin is a tty */
178d29b2c44Sab196087 		int	full_tty;	/* True if stdin and stdout are tty */
179d29b2c44Sab196087 		int	in_tecla;	/* gl_get_line() is active */
180d29b2c44Sab196087 		GetLine	*gl;		/* getline object */
181d29b2c44Sab196087 	} input;
182d29b2c44Sab196087 	struct {		/* ELF file state */
183d29b2c44Sab196087 		int elfclass;		/* ELFCLASS of file being edited */
184*4f680cc6SAli Bahrami 		int elfconst_ehdr_change;	/* ELF header has changed. */
185*4f680cc6SAli Bahrami 						/*	Recheck elfconst strs */
186d29b2c44Sab196087 		/*
187d29b2c44Sab196087 		 * Information for the ELF object being edited.
188d29b2c44Sab196087 		 * The value of elfclass determines which of these
189d29b2c44Sab196087 		 * fields is valid in the current session. This is
190d29b2c44Sab196087 		 * only usable if file.present is True. Otherwise, there
191d29b2c44Sab196087 		 * is no object state, and these pointers will be NULL.
192d29b2c44Sab196087 		 */
193d29b2c44Sab196087 		union {
194d29b2c44Sab196087 			elfedit32_obj_state_t *s32;	/* ELFCLASS32 */
195d29b2c44Sab196087 			elfedit64_obj_state_t *s64;	/* ELFCLASS64 */
196d29b2c44Sab196087 		} obj_state;
197d29b2c44Sab196087 	} elf;
198d29b2c44Sab196087 	USER_CMD_T *cur_cmd;	 /* NULL, or currently executing command */
199d29b2c44Sab196087 } STATE_T;
200d29b2c44Sab196087 
201d29b2c44Sab196087 
202d29b2c44Sab196087 
203d29b2c44Sab196087 /*
204d29b2c44Sab196087  * Type of item argument to elfedit_next_optarg(), used to pull together
205d29b2c44Sab196087  * the information for a single command option or argument, handling
206d29b2c44Sab196087  * the ELFEDIT_CMDOA_F_VALUE and ELFEDIT_CMDOA_F_INHERIT cases.
207d29b2c44Sab196087  */
208d29b2c44Sab196087 typedef struct {
209d29b2c44Sab196087 	const char		*oai_name;	/* Name of option */
210d29b2c44Sab196087 	const char		*oai_vname;	/* Name of value field if */
211d29b2c44Sab196087 						/* ELFEDIT_CMDOA_F_VALUE */
212d29b2c44Sab196087 	elfedit_i18nhdl_t	oai_help;	/* Help text for option */
213d29b2c44Sab196087 	elfedit_cmd_oa_flag_t	oai_flags;	/* Additional attributes */
214d29b2c44Sab196087 	elfedit_cmd_oa_mask_t	oai_idmask;	/* Returned by elfedit_getopt */
215d29b2c44Sab196087 	elfedit_cmd_oa_mask_t	oai_excmask;	/* mutual exclusion mask */
216d29b2c44Sab196087 } elfedit_optarg_item_t;
217d29b2c44Sab196087 
218d29b2c44Sab196087 
219d29b2c44Sab196087 
220d29b2c44Sab196087 /* Global state is accessible between elfedit files */
221d29b2c44Sab196087 extern STATE_T state;
222d29b2c44Sab196087 
223d29b2c44Sab196087 /* Exported by sys.c, used in elfedit.c to initialize builtin sys module */
224d29b2c44Sab196087 extern MODLIST_T *elfedit_sys_init(elfedit_module_version_t version);
225d29b2c44Sab196087 
226d29b2c44Sab196087 /* Exported by util.c, used by elfedit.c and sys.c to process output style */
227d29b2c44Sab196087 extern int elfedit_atooutstyle(const char *str, elfedit_outstyle_t *outstyle);
228d29b2c44Sab196087 
229d29b2c44Sab196087 /*
230d29b2c44Sab196087  * getopt related routines that are not public
231d29b2c44Sab196087  */
232d29b2c44Sab196087 extern void elfedit_set_cmd_outstyle(const char *str);
233d29b2c44Sab196087 
234d29b2c44Sab196087 /* elfedit internal functions used by sys module */
235d29b2c44Sab196087 extern void elfedit_exit(int status);
236d29b2c44Sab196087 extern elfeditGC_cmd_t *elfedit_find_command(const char *name, int must_exist,
237d29b2c44Sab196087     elfeditGC_module_t **mod_ret);
238d29b2c44Sab196087 extern const char *elfedit_format_command_usage(elfeditGC_module_t *mod,
239d29b2c44Sab196087     elfeditGC_cmd_t *cmd, const char *wrap_str, size_t cur_col);
240d29b2c44Sab196087 extern elfeditGC_module_t *elfedit_load_module(const char *name, int must_exist,
241d29b2c44Sab196087     int allow_abs_path);
242d29b2c44Sab196087 extern void elfedit_load_moddir(const char *dirpath, int must_exist,
243d29b2c44Sab196087     int abs_path);
244d29b2c44Sab196087 extern void elfedit_load_modpath(void);
245d29b2c44Sab196087 extern void elfedit_unload_module(const char *name);
246d29b2c44Sab196087 extern void elfedit_next_optarg(elfedit_cmd_optarg_t **optarg,
247d29b2c44Sab196087     elfedit_optarg_item_t *item);
248d29b2c44Sab196087 extern const char *elfedit_optarg_helpstr(elfeditGC_module_t *mod,
249d29b2c44Sab196087     elfedit_optarg_item_t *item);
250d29b2c44Sab196087 
251d29b2c44Sab196087 
252d29b2c44Sab196087 /* Used by elfedit_getopt_init() to access options array for command */
253d29b2c44Sab196087 elfeditGC_cmd_t *elfedit_curcmd(void);
254d29b2c44Sab196087 
255d29b2c44Sab196087 /* elfedit_machelf functions used by elfedit */
256d29b2c44Sab196087 extern	void elfedit32_init_obj_state(const char *file, int fd, Elf *elf);
257d29b2c44Sab196087 extern	void elfedit64_init_obj_state(const char *file, int fd, Elf *elf);
258d29b2c44Sab196087 
259d29b2c44Sab196087 #ifdef	__cplusplus
260d29b2c44Sab196087 }
261d29b2c44Sab196087 #endif
262d29b2c44Sab196087 
263d29b2c44Sab196087 #endif	/* __ELFEDIT_H */
264