elfedit.h revision d29b2c4438482eb00488be49a1f5d6835f455546
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _ELFEDIT_H
#define _ELFEDIT_H
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <libelf.h>
#include <stdarg.h>
/* The following are here to support use of elfedit_msg() */
#include <libintl.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* elfedit uses elfedit_printf() to produce generic output to stdout.
* elfedit_msg() is used to produce error message, or specific types
* of terse informational messages:
*
* ELFEDIT_MSG_ERR:
* Issues an error to stderr. elfedit_msg() does not return
* to the caller. Control returns to the outer loop in
* interactive use. elfedit exits in non-interactive use.
*
* ELFEDIT_MSG_FATAL:
* Issues an error to stderr. elfedit_msg() exits the process,
* and does not return to the caller.
*
* ELFEDIT_MSG_USAGE:
* Issues an elfedit usage message to stderr, and
* returns to the caller.
*
* ELFEDIT_MSG_CMDUSAGE
* Issues an elfedit usage message to stderr, and
* does not return to the caller.
*
* ELFEDIT_MSG_DEBUG
* If the ELFEDIT_F_DEBUG flag is set, the message
* is printed to stdout, otherwise no output is produced.
* elfedit_msg() returns to the caller.
*
* ELFEDIT_MSG_QUIET
* This is a very special case, intended to handle the
* case where the pager subprocess exits before we are
* done producing output (the user presses 'q'). It acts
* just like ELFEDIT_MSG_ERR, except that no message is
* actually printed.
*
* In the cases where elfedit_msg() does not return to the caller, the
* behavior depends on the mode of execution. If running in interactive
* mode (reading from a tty), control is returned directly to the outer
* elfedit control loop to read another command. If not running in interactive
* mode, elfedit exits with a non-zero status.
*/
typedef enum {
ELFEDIT_MSG_ERR = 0,
ELFEDIT_MSG_FATAL = 1,
ELFEDIT_MSG_USAGE = 2,
ELFEDIT_MSG_CMDUSAGE = 3,
ELFEDIT_MSG_DEBUG = 4,
/*
* Information for a single ELF section.
*
* NOTE: sec_xshndx
* A symbol table can have an associated SHT_SYMTAB_SHNDX section. This
* happens when the number of sections is too large to fit in the
* ELF symbol st_shndx field, which is a 16-bit value. The sec_xshndx
* field will be SHN_UNDEF if there is no such section, and will be
* the section index of the extended section index section assocated
* with the symbol table otherwise.
*
* NOTE: sec_versym
* Symbol table sections can have an SHT_SUNW_VERSYM section that
* contains its version indices. Other types of section will have
* this field set to SHN_UNDEF.
*/
typedef struct {
const char *sec_name; /* Name of section */
typedef struct {
const char *sec_name;
#ifdef _ELF64
#define elfedit_section_t elfedit64_section_t
#else
#define elfedit_section_t elfedit32_section_t
#endif
/*
* We maintain extra information for symbol tables. We look them
* up frequently, so we want to eliminate expensive linear searches
* of the entire section header array. Also, symbol tables usually
* have associated parallal sections (syminfo, versym, extended indexes, etc)
* and we want to eliminate repeated linear lookups for them, as well as
* the basic error checking that is necessary to ensure they match the
* symbol table they're given.
*
* This extra information is kept in elfedit_symtab_t structs. Each field
* is a section index, with SHN_UNDEF used for those that do not apply.
*/
typedef struct {
typedef struct {
#ifdef _ELF64
#define elfedit_symtab_t elfedit64_symtab_t
#else
#define elfedit_symtab_t elfedit32_symtab_t
#endif
/*
* Information for a single ELF object.
*
* note:
* elfedit is intended to be an expert's tool, capable of modifying
* nearly everything in the file, whether or not such modifications
* are a good idea. At the same time, elfedit, via libelf, relies
* on the contents of the object to properly locate information in
* the file. As this is the same information that elfedit allows the
* user to modify, it should be obvious that the potential exists
* for users to corrupt the file to the degree that elfedit itself
* may fail, or produce spurious results. We allow such changes for
* several reasons:
*
* 1) Such corruption does not happen in the most obvious and
* useful operations elfedit supports, but comes as a result
* of modifying fields that contain size and offset information
* used to navigate the file. Non-ELF developers have
* little practical reason to change such things.
*
* 2) Producing a corrupt ELF file can be very useful
*
* 3) ELF is sufficiently complex that no absolute guarantees can
* be made about "safe" operations, beyond the basic
* and obvious things that are of practical use.
*
* One way we protect ourselves is via the information cached in
* the elfedit_obj_state_t structure at startup. By using this
* information, rather than constantly fetching it via libelf,
* we protect ourselves against many user changes, such as changing the
*
* Of course, we make no assurances that that we will be able to
* read the resulting file in a subsequent session.
*/
typedef struct {
const char *os_file; /* Path to ELF file */
int os_fd; /* Open file descriptor */
/* string table section */
typedef struct {
const char *os_file;
int os_fd;
#ifdef _ELF64
#else
#endif
/*
* Bit values for editor state.
*/
typedef enum {
/*
* Type used to represent the output style for printing ELF values.
*
* DEFAULT - Output is in 'elfdump' style, designed for human eyes.
* Headers, and additional information are shown.
* SIMPLE - Output is simple, consisting only of the target item.
* Integer values are shown as symbolic constants when possible,
* and integers otherwise.
* NUM - Like SIMPLE, except integer values are always shown as
* integer constants, and strings are shown as the integer
* offset into the string table.
*/
typedef enum {
/*
* The elfedit_module_t, and the types it references, are defined
* by loadable elfedit modules, and used by elfedit. These structures
* need to communicate internationalized strings for elfedit to print.
*
* We want to leave the choice of internationalization APIs, as well as
* the decision about whether or not to even to it to the individual
* modules. Hence, we do not use a simple (const char *) pointer to
* communicate potentially internationalized strings. Instead, we define
* elfedit_i18nhdl_t, an opaque type guaranteed to be large enough
* to hold a pointer. Each module casts the handle needed to access the
* string to this type. Each module also supplies a function
* (mod_i18nhdl_to_str field of elfedit_module_t) that given one
* of these opaque keys, will return a (const char *) pointer to the
* actual string, for elfedit to print.
*
* If the underlying module doesn't want to implement i18n support,
* all it has to do is cast the strings to elfedit_i18nhdl_t and
* back.
*/
typedef uintptr_t elfedit_i18nhdl_t;
/*
* Macro to handle casting international string "handles" to the
* elfedit_i18nhdl_t opaque type.
*/
/*
* Return values from command functions
*/
typedef enum {
ELFEDIT_CMDRET_NONE = 0, /* Nothing to report */
/*
* Prototype of an implementation function for an edit command. Note that
* commands do not return a status:
* - Success is indicated by a normal return.
* - The command indicates a fatal error by calling elfedit_msg() with the
* ELFEDIT_MSG_ERR type, in which case execution does not return
* to the command, and the elfedit command loop knows that an
* error occurred.
* - The command is responsible for using the standard libelf
* mechanisms to indicate when changes have been made to
* the ELF file.
*/
#ifdef _ELF64
#else
#endif
/*
* An elfedit command (elfedit_cmd_t) has a cmd_cpl field that
* can be set to a command completion function. If such a function
* is present (non-NULL), and the user presses the tab key at the
* command line while the cursor is at a plain (non option) argument,
* elfedit calls the function, passing it all the tokens up through
* the one needing completion. The function can use elfedit_cpl_match()
* to enter possible alternatives. Additionally, there are helper
* functions built on top of elfedit_cpl_match() that simplify common cases.
*
* elfedit_cpl_ato[iu]() - enter matches from elfedit_ato[iu]_sym_t
* mappings.
* elfedit_cpl_atoconst() - Enter matches for well known constants
* elfedit_cpl_command() - enter matches for all known commands
* elfedit_cpl_mod() - enter matches for all known modules.
*
* The completion function is passed the following arguments:
*
* obj_state - Object state. Will be NULL if elfedit session does not
* have an active object. The completion function must test
* the pointer before using it.
* cpldata - Completion data, to be passed to elfedit_cpl_match()
* or the helper functions built on it to register alternative
* strings.
* argc, argv - The tokens from the start of the line throught
* the one needing completion, which will always
* be cmdcpl_argv[cmdcpl_argc - 1].
* num_opt - A count of the optional arguments (those starting with
* '-' at the beginning of argv. This means that argv[num_opt]
* is the first plain argument, and the 1-based positional
* number of the plain argument for which command completion
* is needed is (argc - num_opt).
*/
#ifdef _ELF64
#else
#endif
/*
* are used to represent each option and plain argument accepted
* by a command, via the cmd_opt and cmd_args fields in the
* command definition (elfedit_cmd_t). Each descriptor consists
* of a name, a help string (formatted for display via sys:help),
* and a flags field that conveys extra information about the
* item:
*
* ELFEDIT_CMDOA_F_OPT
* The item is optional. This flag is implicit for options
* and need only be set for plain arguments.
*
* ELFEDIT_CMDOA_F_VALUE
* The item has a value, which is found in the following
* item. This flag only has meaning for options, and should
* not be set for plain arguments. The descriptor for the
* value is found in the next array element, and only the
* oa_name field is used (the other should be set t 0).
*
* ELFEDIT_CMDOA_F_MULT
* More than one of the specified items may be specified
*
* ELFEDIT_CMDOA_F_INHERIT
* This is an item for which a common definition exists.
* Elfedit will substitute the standard values for the
* name, help text, and flags. This enforces consistency
* in documentation, plus it is easier for the module author.
* When ELFEDIT_CMDOA_F_INHERIT is set:
* - oa_name should be set to one of the ELFEDIT_STDOA_
* values to specifiy which standard item is being
* inherited.
* - oa_help must be set to NULL.
* - It is an error to set any other flags with
* ELFEDIT_CMDOA_F_INHERIT.
* - oa_idmask and oa_excmask are used in the normal way.
*
* The oa_idmask and oa_excmask fields are used to identify options,
* and to support mutual exclusion (when two or more options cannot be
* used together). They are ignored for arguments, and should be set to 0.
* oa_idmask is used to uniquely identify each item. When elfedit_getopt()
* matches an option, it returns the value of oa_idmask to the caller to
* indicate which option was matched. elfedit enforces the following rules
* for oa_idmask, and will refuse to load a module that does not follow them:
* - The value of oa_idmask must be 0, or have a value that
* is a power of 2 (i.e. only has one bit set).
* - Each item that sets a non-0 value for oa_idmask must have
* a unique value.
* - If oa_idmask is 0, oa_excmask must be 0 also.
* - oa_excmask is set to 0 if an item is not mutually exclusive
* to any other item. Otherwise, it should set the bit
* values representing the items it is mutually exclusive to.
* - An oa_idmask value of 0 can be used for any item that
* the module does not need to identify, and which
* is not mutually exclusive to any other item.
* As elfedit_getopt() processes items, it maintains a bitmask combining the
* oa_idmask fields of all the options already seen. For each option, it uses
* oa_excmask to check for conflicts.
*
* note: elfedit enforces the rule that options consist of a '-'
* character followed by at least one character when a module
* is loaded.
*/
typedef enum {
typedef u_longlong_t elfedit_cmd_oa_mask_t;
typedef struct {
const char *oa_name; /* Name of option */
/* elfedit_getopt */
/* for use by caller */
/*
* These values define the standard options and arguments that a module
* can inherit using the ELFEDIT_CMDOA_F_INHERIT flag (described above).
* New items must be added at the end --- reordering the list will
* require all modules to be rebuilt.
*
* Note: 0 cannot be used as a ELFEDIT_STDOA_ value, because a NULL
* value of oa_name is used to terminate argument and options lists.
* Therefore, these values start at 1.
*/
/*
* Definition of a command
*
* This structure includes an elfedit_cmd_func_t pointer, which has
* different definitions for different ELFCLASS. Rather than needlessly
* complicate the code with three versions of this type, and any
* type that uses it, we simply use the GenericClass type. elfedit
* will always cast this to the correct type before calling a module.
*
* cmd_name is an array of pointers to the names for the command.
* The "primary" name should always be first, followed by any alias
* names. The final element of the array must be a NULL pointer,
* which terminates the list. Every command is required to have at
* least one name, so code is allowed to assume that the first element
* of cmd_name is non-NULL, and contains the primary name.
*
* Many modules provide a "default" command, which is a command
* that is run if only the module name is specified, followed
* by a colon (i.e. "sym:"). The way this is implemented is to
* give the desired default command an empty string as an alias.
* Note that the primary name cannot be an empty string, only the
* alias name.
*
* cmd_opts and cmd_args are each an array of elfedit_cmd_argdesc_t
* structures, that describe the options and plain arguments accepted
* by the command. These arrays are used to general help text for
* the commands. The cmd_opts array is also used to provide command
* completion for options. Both of these arrays are terminated by
* a final NULL element (all fields zero).
*/
typedef struct {
const char **cmd_name; /* Cmd names (null term.) */
typedef struct {
const char **cmd_name;
#ifdef _ELF64
#define elfedit_cmd_t elfedit64_cmd_t
#else
#define elfedit_cmd_t elfedit32_cmd_t
#endif
/*
* elfedit modules version themselves so that we can alter the definition
* of elfedit_module_t in a backward compatible way.
*/
typedef enum {
ELFEDIT_VER_NONE = 0,
ELFEDIT_VER_CURRENT = 1,
ELFEDIT_VER_NUM = 2
/*
* Each module returns a pointer to an elfedit_module_t, describing
* what commands the module provides.
*
* Note: mod_cmds is a NULL terminated array of command defs. This
* means that the final element in the array should have all of its
* fields set to NULL.
*
* The mod_i18nhdl_to_str function pointer is explained above
* with the definition of elfedit_i18nhdl_t.
*/
typedef const char *(* elfedit_mod_i18nhdl_to_str_func_t)(elfedit_i18nhdl_t);
typedef struct {
const char *mod_name; /* Name of module */
/* i18n -> (char *) fcn */
typedef struct {
const char *mod_name;
#ifdef _ELF64
#define elfedit_module_t elfedit64_module_t
#else
#define elfedit_module_t elfedit32_module_t
#endif
/*
* Each module is a sharable library, expected to provide a single global
* function, named elfedit_init(), with the following prototype.
*/
/*
* Core elfedit functions exported for use by modules
*/
extern void elfedit_command_usage(void);
extern void elfedit_cpl_command(void *cpldata);
extern elfedit_flag_t elfedit_flags(void);
extern elfedit_outstyle_t elfedit_outstyle(void);
extern void elfedit_pager_init(void);
extern void elfedit_printf(const char *format, ...);
/*
* Core elfedit functions exported for use by sys: module only
*/
/*
* elfedit modules are expected to define two functions, one for
* each ELFCLASS. Define a generic name for this function, based on
* the class being supported by the including module.
*/
#ifdef _ELF64
#define elfedit_init elfedit64_init
#else
#define elfedit_init elfedit32_init
#endif
/*
* It is common to search the dynamic section for specific elements.
* Structures of this type are used to represent the contents of such
* elements in a systematic way. The elfedit_dyn_elt_init() function
* is used to prepare these strucutres for use.
*/
typedef struct {
int dn_seen; /* True if this item has been seen */
typedef struct {
int dn_seen;
#ifdef _ELF64
#define elfedit_dyn_elt_t elfedit64_dyn_elt_t
#else
#define elfedit_dyn_elt_t elfedit32_dyn_elt_t
#endif
/*
* The elfedit_atoi() and elfedit_atoui() functions can optionally
* accept an array of these structures, giving symbolic names that
* will be accepted instead of numeric codes. If such an array is
* present, the supplied string has it's leading and trailing whitespace
* removed and is then compared to the list, and if there is a match,
* the corresponding integer value is returned.
*
* The final array element must have its name field set to NULL.
*/
typedef u_longlong_t elfedit_atoui_t;
typedef struct {
const char *sym_name;
typedef longlong_t elfedit_atoi_t;
typedef struct {
const char *sym_name;
/*
* The elfedit_atoconst*() functions are built on top of the atoui routines.
* These routines accept an elfedit_const_t code instead of a
* pointer to an elfedit_atoui_sym_t array, and use internally
* predefined tables of elfedit_atoui_sym_t in order to do the desired
* mappings. elfedit modules are encouraged to use these standard
* tables instead of defining their own elfedit_atoui_sym_t arrays.
*
* note:
* - The values assigned here must be in agreement with the
* sym_table[] array defined in elfconst.c.
* - Once defined, these values must not change. Reordering the
* list will require all modules to be rebuilt, and will
* break backward compatability. New items should be
* added to the end.
*/
typedef enum {
ELFEDIT_CONST_OUTSTYLE = 0, /* elfedit output styles */
/* section types */
/*
* Given an elfedit_const_t, return the array of elfedit_atoui_sym_t
* entries that it represents.
*/
/*
* Return the elfedit_atoui_t array that corresponds to the
* CA_SUNW_HW_1 hardware capabiliies field for a given
* machine type.
*/
/*
* ato[u]i and const routines, used to turn strings into numeric values,
* with support for mapping symbol names to numbers, and range checking.
*/
const elfedit_atoi_sym_t *sym);
const elfedit_atoui_sym_t *sym);
elfedit_atoi_t *v);
elfedit_atoui_t *);
elfedit_atoui_t *);
const elfedit_atoi_sym_t *sym);
const elfedit_atoui_sym_t *sym);
/*
* Convenience functions built on top of the ato[u]i routines.
*/
/*
* elfedit provides a getopt utility for use by the module commands.
* elfedit_getopt_state_t is the state block used by elfedit_getopt().
* elfedit_getopt_ret_t is the definition of the values returned to
* the user by elfedit_getopt() when an option is matched. Elfedit
* getopt processing is done as follows:
*
* 1) The caller initializes an elfedit_getopt_state_t struct via
* a call to elfedit_getopt_init(). The contents of this structure
* must not be accessed by the caller, as they are all private and
* subject to change.
* 2) Repeated calls are made to elfedit_getopt(), as long as it returns
* a non-NULL pointer to an elfedit_getopt_ret_t structure. If the
* matched option has a value (ELFEDIT_CMDOA_F_VALUE), then the gor_value
* field contains the pointer to the string. Otherwise, gor_value is NULL.
* them. Once elfedit_getopt() returns NULL to indicate that there are no
* reference the plain arguments.
*/
typedef struct {
/* elfedit_cmd_optarg_t. Can be */
/* used to quickly identify opt */
const char *gor_value; /* Opt value if ELFEDIT_CMDOA_F_VALUE */
/* Otherwise, NULL */
typedef struct {
int *go_argc; /* Pointer to # of options */
const char ***go_argv; /* Ptr to array of opt strs */
/* seen options */
int go_done; /* True if last option seen */
const char *go_sglgrp; /* Group of 1-letter opts */
/*
* getopt related routines
*/
extern void elfedit_getopt_init(elfedit_getopt_state_t *,
int *, const char ***);
/*
* Additional utility functions exported for use by modules
*/
const char *str);
const char *str);
extern void elfedit32_modified_data(elfedit32_section_t *s);
extern void elfedit64_modified_data(elfedit64_section_t *s);
extern void elfedit32_modified_shdr(elfedit32_section_t *s);
extern void elfedit64_modified_shdr(elfedit64_section_t *s);
const char *shnam);
const char *shnam);
extern elfedit32_section_t *elfedit32_sec_getcap(
extern elfedit64_section_t *elfedit64_sec_getcap(
extern elfedit32_section_t *elfedit32_sec_getdyn(
extern elfedit64_section_t *elfedit64_sec_getdyn(
extern elfedit32_section_t *elfedit32_sec_getstr(
extern elfedit64_section_t *elfedit64_sec_getstr(
/*
* Map the generic names for each of the ELFCLASS specific routines
* above to reference the proper routine for the current compilation.
*/
#ifdef _ELF64
#else
#endif
#ifdef __cplusplus
}
#endif
#endif /* _ELFEDIT_H */