d29b2c4438482eb00488be49a1f5d6835f455546ab * CDDL HEADER START
d29b2c4438482eb00488be49a1f5d6835f455546ab * The contents of this file are subject to the terms of the
d29b2c4438482eb00488be49a1f5d6835f455546ab * Common Development and Distribution License (the "License").
d29b2c4438482eb00488be49a1f5d6835f455546ab * You may not use this file except in compliance with the License.
d29b2c4438482eb00488be49a1f5d6835f455546ab * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
d29b2c4438482eb00488be49a1f5d6835f455546ab * See the License for the specific language governing permissions
d29b2c4438482eb00488be49a1f5d6835f455546ab * and limitations under the License.
d29b2c4438482eb00488be49a1f5d6835f455546ab * When distributing Covered Code, include this CDDL HEADER in each
d29b2c4438482eb00488be49a1f5d6835f455546ab * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
d29b2c4438482eb00488be49a1f5d6835f455546ab * If applicable, add the following below this CDDL HEADER, with the
d29b2c4438482eb00488be49a1f5d6835f455546ab * fields enclosed by brackets "[]" replaced with your own identifying
d29b2c4438482eb00488be49a1f5d6835f455546ab * information: Portions Copyright [yyyy] [name of copyright owner]
d29b2c4438482eb00488be49a1f5d6835f455546ab * CDDL HEADER END
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
d29b2c4438482eb00488be49a1f5d6835f455546ab * Use is subject to license terms.
33f5ff17089e3a43e6e730bf80384c233123dbd9Milan Jurik * Copyright 2012 Milan Jurik. All rights reserved.
d29b2c4438482eb00488be49a1f5d6835f455546ab * Capabilities section
d29b2c4438482eb00488be49a1f5d6835f455546ab * This module uses shared code for several of the commands.
d29b2c4438482eb00488be49a1f5d6835f455546ab * It is sometimes necessary to know which specific command
d29b2c4438482eb00488be49a1f5d6835f455546ab * is active.
d29b2c4438482eb00488be49a1f5d6835f455546abtypedef enum {
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Dump command, used as module default to display dynamic section */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Commands that do not correspond directly to a specific DT tag */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Commands that embody tag specific knowledge */
d29b2c4438482eb00488be49a1f5d6835f455546ab * We supply this function for the msg module
d29b2c4438482eb00488be49a1f5d6835f455546abconst char *
d29b2c4438482eb00488be49a1f5d6835f455546ab * This function is supplied to elfedit through our elfedit_module_t
d29b2c4438482eb00488be49a1f5d6835f455546ab * definition. It translates the opaque elfedit_i18nhdl_t handles
d29b2c4438482eb00488be49a1f5d6835f455546ab * in our module interface into the actual strings for elfedit to
d29b2c4438482eb00488be49a1f5d6835f455546ab * This module uses Msg codes for its i18n handle type.
d29b2c4438482eb00488be49a1f5d6835f455546ab * So the translation is simply to use MSG_INTL() to turn
d29b2c4438482eb00488be49a1f5d6835f455546ab * it into a string and return it.
d29b2c4438482eb00488be49a1f5d6835f455546abstatic const char *
d29b2c4438482eb00488be49a1f5d6835f455546ab * The cap_opt_t enum specifies a bit value for every optional
d29b2c4438482eb00488be49a1f5d6835f455546ab * argument allowed by a command in this module.
d29b2c4438482eb00488be49a1f5d6835f455546abtypedef enum {
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans CAP_OPT_F_CAPID = 4, /* -capid id: elt limited to given */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* capabilities group */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans CAP_OPT_F_CAPNDX = 8, /* -capndx: elt is tag index, */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* not name */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans CAP_OPT_F_OR = 16, /* -or: OR (|) values to dest */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans CAP_OPT_F_STRVAL = 32 /* -s: value is string, not integer */
d29b2c4438482eb00488be49a1f5d6835f455546ab * A variable of type ARGSTATE is used by each command to maintain
d29b2c4438482eb00488be49a1f5d6835f455546ab * information about the arguments and related things. It is
d29b2c4438482eb00488be49a1f5d6835f455546ab * initialized by process_args(), and used by the other routines.
d29b2c4438482eb00488be49a1f5d6835f455546abtypedef struct {
d29b2c4438482eb00488be49a1f5d6835f455546ab elfedit_section_t *sec; /* Capabilities section reference */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans Word grp_start_ndx; /* capabilities group starting index */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans Word grp_end_ndx; /* capabilities group ending index */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans struct { /* String table */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Lookup the string table associated with the capabilities
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * argstate - Argument state block
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * required - If TRUE, failure to obtain a string table should be
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * considered to be an error.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * If a string table is found, argstate->str is updated to reference it.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * If no string table is found, and required is TRUE, an error is issued
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * and this routine does not return to the caller. Otherwise, this
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * routine returns quietly without modifying argstate->str.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evansargstate_add_str(ARGSTATE *argstate, Boolean required)
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* String table already loaded? */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * We can't proceed if the capabilities section does not have
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * an associated string table.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* Error if the operation requires a string table */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_NOSTRTAB),
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans argstate->str.sec = elfedit_sec_getstr(argstate->obj_state,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Given an index into the capabilities array, locate the index of the
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * initial element in its capabilities group, and the number of elements
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * in the group.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evanscap_group_extents(ARGSTATE *argstate, Word ndx, Word *ret_start_ndx,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * The group starts with a non-NULL tag that is either the
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * first tag in the array, or is preceded by a NULL tag.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans while ((ndx > 0) && (argstate->cap.data[ndx].c_tag == CA_SUNW_NULL))
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans while ((ndx > 0) && (argstate->cap.data[ndx - 1].c_tag != CA_SUNW_NULL))
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * The group is terminated by a series of 1 or more NULL tags.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans (argstate->cap.data[ndx + 1].c_tag == CA_SUNW_NULL))
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * If a CA_SUNW_ID element exists within the current capabilities group
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * in the given argument state, return the string pointer to the name.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Otherwise return a pointer to a descriptive "noname" string.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evansstatic const char *
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans for (; ndx <= argstate->cap.grp_end_ndx; ndx++, cap++) {
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans MSG_INTL(MSG_STR_OBJECT) : MSG_INTL(MSG_STR_NONAME));
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Given an index into the capabilities array, set the argstate cap.grp_*
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * fields to reflect the capabilities group containing the index.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * The group concept is used to limit operations to a related group
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * of capabilities, and prevent insert/delete/move operations from
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * spilling across groups.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans cap_group_extents(argstate, ndx, &argstate->cap.grp_start_ndx,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans elfedit_msg(ELFEDIT_MSG_DEBUG, MSG_INTL(MSG_DEBUG_CAPGRP),
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans EC_WORD(argstate->cap.sec->sec_shndx), argstate->cap.sec->sec_name,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans EC_WORD(argstate->cap.grp_end_ndx), cap_group_id(argstate));
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Given an index into the capabilities array, issue a group title for
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * the capabilities group that contains it.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans cap_group_extents(argstate, ndx, &loc_argstate.cap.grp_start_ndx,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans EC_WORD(loc_argstate.cap.grp_end_ndx), cap_group_id(&loc_argstate));
d29b2c4438482eb00488be49a1f5d6835f455546ab * Standard argument processing for cap module
d29b2c4438482eb00488be49a1f5d6835f455546ab * obj_state, argc, argv - Standard command arguments
d29b2c4438482eb00488be49a1f5d6835f455546ab * argstate - Address of ARGSTATE block to be initialized
d29b2c4438482eb00488be49a1f5d6835f455546ab * On success, *argstate is initialized. On error,
d29b2c4438482eb00488be49a1f5d6835f455546ab * an error is issued and this routine does not return.
d29b2c4438482eb00488be49a1f5d6835f455546abstatic void
d29b2c4438482eb00488be49a1f5d6835f455546abprocess_args(elfedit_obj_state_t *obj_state, int argc, const char *argv[],
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Add each new option to the options mask */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans while ((getopt_ret = elfedit_getopt(&getopt_state)) != NULL) {
d29b2c4438482eb00488be49a1f5d6835f455546ab /* If there may be an arbitrary amount of output, use a pager */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Return the updated values of argc/argv */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Locate the capabilities section */
d29b2c4438482eb00488be49a1f5d6835f455546ab argstate->cap.sec = elfedit_sec_getcap(obj_state, &argstate->cap.data,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * If -capid was specified, locate the specified capabilities group,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * and narrow the section data to use only that group. Otherwise,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * use the whole array.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * -capid requires the capability section to have an
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * associated string table.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans elfedit_msg(ELFEDIT_MSG_ERR, MSG_INTL(MSG_ERR_BADCAPID),
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans argstate->cap.grp_end_ndx = argstate->cap.num - 1;
d29b2c4438482eb00488be49a1f5d6835f455546ab * Print ELF capabilities values, taking the calling command, and output style
d29b2c4438482eb00488be49a1f5d6835f455546ab * into account.
d29b2c4438482eb00488be49a1f5d6835f455546ab * cmd - CAP_CMD_T_* value giving identify of caller
d29b2c4438482eb00488be49a1f5d6835f455546ab * autoprint - If True, output is only produced if the elfedit
d29b2c4438482eb00488be49a1f5d6835f455546ab * autoprint flag is set. If False, output is always produced.
d29b2c4438482eb00488be49a1f5d6835f455546ab * argstate - Argument state block
d29b2c4438482eb00488be49a1f5d6835f455546ab * print_type - Specifies which capabilities elements to display.
d29b2c4438482eb00488be49a1f5d6835f455546ab * ndx = If print_type is PRINT_CAP_T_NDX, displays the index specified.
d29b2c4438482eb00488be49a1f5d6835f455546ab * Otherwise ignored.
d29b2c4438482eb00488be49a1f5d6835f455546abtypedef enum {
d29b2c4438482eb00488be49a1f5d6835f455546ab /* given by arg */
d29b2c4438482eb00488be49a1f5d6835f455546abstatic void
d29b2c4438482eb00488be49a1f5d6835f455546abprint_cap(CAP_CMD_T cmd, int autoprint, ARGSTATE *argstate,
d29b2c4438482eb00488be49a1f5d6835f455546ab if (autoprint && ((elfedit_flags() & ELFEDIT_F_AUTOPRINT) == 0))
d29b2c4438482eb00488be49a1f5d6835f455546ab * Pick an output style. cap:dump is required to use the default
d29b2c4438482eb00488be49a1f5d6835f455546ab * style. The other commands use the current output style.
d29b2c4438482eb00488be49a1f5d6835f455546ab /* How many elements do we examine? */
d29b2c4438482eb00488be49a1f5d6835f455546ab return; /* Out of range */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* Load string table if there is one */
98c080d502548e68bb9815459ea56e6ae282c430Rod Evans str = (const char *)argstate->str.sec->sec_data->d_buf;
d29b2c4438482eb00488be49a1f5d6835f455546ab * If we are only displaying certain tag types and
d29b2c4438482eb00488be49a1f5d6835f455546ab * this isn't one of those, move on to next element.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans if ((print_type == PRINT_CAP_T_TAG) && (cap->c_tag != arg)) {
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * If capability type requires a string table, and we don't
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * have one, force an error.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * If CAP_CMD_T_TAG, and not in default output
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * style, display the tag rather than the value.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* Displaying the value in simple or numeric mode */
d29b2c4438482eb00488be49a1f5d6835f455546ab * If nothing was output under the print types that are
d29b2c4438482eb00488be49a1f5d6835f455546ab * based on tag type, issue an error saying it doesn't exist.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans argstate->cap.sec->sec_name, argstate->cap.grp_start_ndx,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans argstate->cap.grp_end_ndx, cap_group_id(argstate),
d29b2c4438482eb00488be49a1f5d6835f455546ab * Process the elt argument: This will be a tag type if -capndx is
d29b2c4438482eb00488be49a1f5d6835f455546ab * not present and this is a print request. It will be an index otherwise.
d29b2c4438482eb00488be49a1f5d6835f455546ab * argstate - Argument state block
d29b2c4438482eb00488be49a1f5d6835f455546ab * arg - Argument string to be converted into an index
d29b2c4438482eb00488be49a1f5d6835f455546ab * argname - String giving the name by which the argument is
d29b2c4438482eb00488be49a1f5d6835f455546ab * referred in the online help for the command.
d29b2c4438482eb00488be49a1f5d6835f455546ab * print_request - True if the command is to print the current
d29b2c4438482eb00488be49a1f5d6835f455546ab * value(s) and return without changing anything.
d29b2c4438482eb00488be49a1f5d6835f455546ab * print_type - Address of variable containing PRINT_CAP_T_
d29b2c4438482eb00488be49a1f5d6835f455546ab * code specifying how the elements will be displayed.
d29b2c4438482eb00488be49a1f5d6835f455546ab * If print_request is False: arg is converted into an integer value.
d29b2c4438482eb00488be49a1f5d6835f455546ab * If -capndx was used, we convert it into an integer. If it was not
d29b2c4438482eb00488be49a1f5d6835f455546ab * used, then arg is a tag name --- we find the first capabilities entry
d29b2c4438482eb00488be49a1f5d6835f455546ab * that matches. If no entry matches, and there is an extra CA_NULL,
d29b2c4438482eb00488be49a1f5d6835f455546ab * it is added. Otherwise an error is issued. *print_type is set
d29b2c4438482eb00488be49a1f5d6835f455546ab * to PRINT_CAP_T_NDX.
d29b2c4438482eb00488be49a1f5d6835f455546ab * If print_request is True: If -capndx was used, arg is converted into
d29b2c4438482eb00488be49a1f5d6835f455546ab * an integer value, *print_type is set to PRINT_CAP_T_NDX, and
d29b2c4438482eb00488be49a1f5d6835f455546ab * the value is returned. If -capndx was not used, *print_type is set to
d29b2c4438482eb00488be49a1f5d6835f455546ab * PRINT_CAP_T_TAG, and the tag value is returned.
d29b2c4438482eb00488be49a1f5d6835f455546abarg_to_index(ARGSTATE *argstate, const char *arg, const char *argname,
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Assume we are returning an index, alter as needed below */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * If -capndx was used, this is a simple numeric index.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Determine its capability group because some operations
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * (move, delete) are limited to operate within it.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans if ((argstate->optmask & CAP_OPT_F_CAPNDX) != 0) {
d29b2c4438482eb00488be49a1f5d6835f455546ab /* The argument is a CA_ tag type, not a numeric index */
d29b2c4438482eb00488be49a1f5d6835f455546ab ca_value = (Word) elfedit_atoconst(arg, ELFEDIT_CONST_CA);
d29b2c4438482eb00488be49a1f5d6835f455546ab * If this is a printing request, then we let print_cap() show
d29b2c4438482eb00488be49a1f5d6835f455546ab * all the items with this tag type.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * If we haven't determined a capability group yet, either via
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * -capid, or -capndx, then make it the initial group, which
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * represent the object capabilities.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Locate the first entry with the given tag type within the
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * capabilities group.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * If we hit a NULL, then only more NULLs can follow it and
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * there's no need to look further. If there is more than
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * one NULL, we can grab the first one and turn it into
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * an element of the desired type.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans if (argstate->cap.data[ndx].c_tag == CA_SUNW_NULL) {
d29b2c4438482eb00488be49a1f5d6835f455546ab /* No room to create one, so we're out of options and must fail */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans argstate->cap.sec->sec_name, argstate->cap.grp_start_ndx,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans argstate->cap.grp_end_ndx, cap_group_id(argstate), arg);
d29b2c4438482eb00488be49a1f5d6835f455546ab /*NOTREACHED*/
d29b2c4438482eb00488be49a1f5d6835f455546ab return (0); /* For lint */
d29b2c4438482eb00488be49a1f5d6835f455546ab * Argument processing for the bitmask commands. Convert the arguments
d29b2c4438482eb00488be49a1f5d6835f455546ab * to integer form, apply -and/-cmp/-or, and return the resulting value.
d29b2c4438482eb00488be49a1f5d6835f455546ab * argstate - Argument state block
d29b2c4438482eb00488be49a1f5d6835f455546ab * orig - Value of original bitmask
d29b2c4438482eb00488be49a1f5d6835f455546ab * const_sym - NULL, or array of name->integer mappings for
d29b2c4438482eb00488be49a1f5d6835f455546ab * applicable symbolic constant names.
d29b2c4438482eb00488be49a1f5d6835f455546abflag_bitop(ARGSTATE *argstate, Word orig, const elfedit_atoui_sym_t *const_sym)
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Collect the arguments */
d29b2c4438482eb00488be49a1f5d6835f455546ab flags |= (Word) elfedit_atoui(argstate->argv[i], const_sym);
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Complement the value? */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Perform any requested bit operations */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Common processing for capabilities value setting.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * argstate - Argument state block
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * cap - capabilities data pointer
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * ndx - capabilities data index
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * cap_ndx - capabilities section index
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * cap_name - capabilities section name
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * cap_tag - capabilities tag
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * const_type - data conversion type
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evanscap_set(ARGSTATE *argstate, Cap *cap, Word ndx, Word cap_ndx,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans const char *cap_name, Xword cap_tag, elfedit_const_t const_type)
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans Half mach = argstate->obj_state->os_ehdr->e_machine;
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* Set the value */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans elfedit_msg(ELFEDIT_MSG_DEBUG, MSG_INTL(MSG_DEBUG_BSB_OK),
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans conv_cap_val(cap_tag, ocap, mach, CONV_FMT_NOBKT, &buf1));
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans elfedit_msg(ELFEDIT_MSG_DEBUG, MSG_INTL(MSG_DEBUG_BSB_CHG),
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans conv_cap_val(cap_tag, ocap, mach, CONV_FMT_NOBKT, &buf1),
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans conv_cap_val(cap_tag, ncap, mach, CONV_FMT_NOBKT, &buf2));
d29b2c4438482eb00488be49a1f5d6835f455546ab * Common body for the cap: module commands. These commands
d29b2c4438482eb00488be49a1f5d6835f455546ab * share a large amount of common behavior, so it is convenient
d29b2c4438482eb00488be49a1f5d6835f455546ab * to centralize things and use the cmd argument to handle the
d29b2c4438482eb00488be49a1f5d6835f455546ab * small differences.
d29b2c4438482eb00488be49a1f5d6835f455546ab * cmd - One of the CAP_CMD_T_* constants listed above, specifying
d29b2c4438482eb00488be49a1f5d6835f455546ab * which command to implement.
d29b2c4438482eb00488be49a1f5d6835f455546ab * obj_state, argc, argv - Standard command arguments
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Process the optional arguments */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Check number of arguments, gather information */
d29b2c4438482eb00488be49a1f5d6835f455546ab switch (cmd) {
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:dump can accept an optional index argument */
d29b2c4438482eb00488be49a1f5d6835f455546ab ndx = arg_to_index(&argstate, elfedit_atoconst_value_to_str(
d29b2c4438482eb00488be49a1f5d6835f455546ab ndx = arg_to_index(&argstate, elfedit_atoconst_value_to_str(
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans ndx = arg_to_index(&argstate, elfedit_atoconst_value_to_str(
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans MSG_ORIG(MSG_STR_VALUE), print_only, &print_type);
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Note expected: All commands should have been caught above */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* If this is a request to print current values, do it and return */
d29b2c4438482eb00488be49a1f5d6835f455546ab switch (cmd) {
d29b2c4438482eb00488be49a1f5d6835f455546ab * CAP_CMD_T_DUMP can't get here: It is a print-only
d29b2c4438482eb00488be49a1f5d6835f455546ab * command.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * We want to limit the deleted elements to be
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * in the range of the current capabilities group,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * and for the resulting NULL elements to be inserted
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * at the end of the group, rather than at the end
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * of the section. To do this, we set the array length
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * in the call to the delete function so that it thinks
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * the array ends with the current group.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * The delete function will catch attempts to delete
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * past this virtual end, but the error message will
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * not make sense to the user. In order to prevent that,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * we check for the condition here and provide a more
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * useful error.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans elfedit_array_elts_delete(msg_prefix, cap, sizeof (Cap),
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * Moves are required to be self contained within
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * the bounds of the selected capability group.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * The move utility function contains bounds checking,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * but is not sub-array aware. Hence, we bounds check
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * check it here, and then hand of the validated
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * operation to the move utility function to execute.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans elfedit_array_elts_move(msg_prefix, cap, sizeof (save),
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans ret = cap_set(&argstate, cap, ndx, cap_ndx, cap_name,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans ret = cap_set(&argstate, cap, ndx, cap_ndx, cap_name,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans ret = cap_set(&argstate, cap, ndx, cap_ndx, cap_name,
d29b2c4438482eb00488be49a1f5d6835f455546ab * If we modified the capabilities section header, tell libelf.
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Do autoprint */
d29b2c4438482eb00488be49a1f5d6835f455546ab * Command completion functions for the commands
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans * -capid command completion: Supply all CA_SUNW_ID names found in the object.
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evanscpl_capid_opt(elfedit_obj_state_t *obj_state, void *cpldata, int argc,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans (strcmp(argv[argc - 2], MSG_ORIG(MSG_STR_MINUS_CAPID)) != 0))
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans cap_sec = elfedit_sec_getcap(obj_state, &cap, &num);
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* If no associated string table, we have no strings to complete */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans str_sec = elfedit_sec_getstr(obj_state, cap_sec->sec_shdr->sh_info, 0);
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans str_sec, cap->c_un.c_val, ELFEDIT_MSG_ERR, 0), 0);
d29b2c4438482eb00488be49a1f5d6835f455546ab * Command completion for the first argument, which specifies
d29b2c4438482eb00488be49a1f5d6835f455546ab * the capabilities element to use. Examines the options to see if
d29b2c4438482eb00488be49a1f5d6835f455546ab * -capndx is present, and if not, supplies the completion
d29b2c4438482eb00488be49a1f5d6835f455546ab * strings for argument 1.
d29b2c4438482eb00488be49a1f5d6835f455546ab/*ARGSUSED*/
d29b2c4438482eb00488be49a1f5d6835f455546abstatic void
d29b2c4438482eb00488be49a1f5d6835f455546abcpl_eltarg(elfedit_obj_state_t *obj_state, void *cpldata, int argc,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* -capid id_name */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans cpl_capid_opt(obj_state, cpldata, argc, argv, num_opt);
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Make sure it's the first argument */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* Is -capndx present? If so, we don't complete tag types */
d29b2c4438482eb00488be49a1f5d6835f455546ab for (i = 0; i < num_opt; i++)
d29b2c4438482eb00488be49a1f5d6835f455546ab if (strcmp(argv[i], MSG_ORIG(MSG_STR_MINUS_CAPNDX)) == 0)
d29b2c4438482eb00488be49a1f5d6835f455546ab * Supply capability tag names. There are very few of these, so
d29b2c4438482eb00488be49a1f5d6835f455546ab * rather than worry about whether a given tag exists in the
d29b2c4438482eb00488be49a1f5d6835f455546ab * file or not, we simply serve up all the possibilities.
d29b2c4438482eb00488be49a1f5d6835f455546ab/*ARGSUSED*/
d29b2c4438482eb00488be49a1f5d6835f455546abstatic void
d29b2c4438482eb00488be49a1f5d6835f455546abcpl_tag(elfedit_obj_state_t *obj_state, void *cpldata, int argc,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* -capid id_name */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans cpl_capid_opt(obj_state, cpldata, argc, argv, num_opt);
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* First plain argument */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* The second argument is always a tag value */
d29b2c4438482eb00488be49a1f5d6835f455546ab/*ARGSUSED*/
d29b2c4438482eb00488be49a1f5d6835f455546abstatic void
d29b2c4438482eb00488be49a1f5d6835f455546abcpl_hw1(elfedit_obj_state_t *obj_state, void *cpldata, int argc,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* -capid id_name */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans cpl_capid_opt(obj_state, cpldata, argc, argv, num_opt);
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* This routine allows multiple flags to be specified */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans elfedit_cpl_atoconst(cpldata, ELFEDIT_CONST_HW1_SUNW);
d29b2c4438482eb00488be49a1f5d6835f455546ab/*ARGSUSED*/
d29b2c4438482eb00488be49a1f5d6835f455546abstatic void
d29b2c4438482eb00488be49a1f5d6835f455546abcpl_sf1(elfedit_obj_state_t *obj_state, void *cpldata, int argc,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* -capid id_name */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans cpl_capid_opt(obj_state, cpldata, argc, argv, num_opt);
d29b2c4438482eb00488be49a1f5d6835f455546ab /* This routine allows multiple flags to be specified */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evanscpl_hw2(elfedit_obj_state_t *obj_state, void *cpldata, int argc,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* -capid id_name */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans cpl_capid_opt(obj_state, cpldata, argc, argv, num_opt);
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* This routine allows multiple flags to be specified */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans elfedit_cpl_atoconst(cpldata, ELFEDIT_CONST_HW2_SUNW);
d29b2c4438482eb00488be49a1f5d6835f455546ab * Implementation functions for the commands
d29b2c4438482eb00488be49a1f5d6835f455546abcmd_dump(elfedit_obj_state_t *obj_state, int argc, const char *argv[])
d29b2c4438482eb00488be49a1f5d6835f455546ab return (cmd_body(CAP_CMD_T_DUMP, obj_state, argc, argv));
d29b2c4438482eb00488be49a1f5d6835f455546abcmd_tag(elfedit_obj_state_t *obj_state, int argc, const char *argv[])
d29b2c4438482eb00488be49a1f5d6835f455546abcmd_value(elfedit_obj_state_t *obj_state, int argc, const char *argv[])
d29b2c4438482eb00488be49a1f5d6835f455546ab return (cmd_body(CAP_CMD_T_VALUE, obj_state, argc, argv));
d29b2c4438482eb00488be49a1f5d6835f455546abcmd_delete(elfedit_obj_state_t *obj_state, int argc, const char *argv[])
d29b2c4438482eb00488be49a1f5d6835f455546ab return (cmd_body(CAP_CMD_T_DELETE, obj_state, argc, argv));
d29b2c4438482eb00488be49a1f5d6835f455546abcmd_move(elfedit_obj_state_t *obj_state, int argc, const char *argv[])
d29b2c4438482eb00488be49a1f5d6835f455546ab return (cmd_body(CAP_CMD_T_MOVE, obj_state, argc, argv));
d29b2c4438482eb00488be49a1f5d6835f455546abcmd_hw1(elfedit_obj_state_t *obj_state, int argc, const char *argv[])
d29b2c4438482eb00488be49a1f5d6835f455546abcmd_sf1(elfedit_obj_state_t *obj_state, int argc, const char *argv[])
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evanscmd_hw2(elfedit_obj_state_t *obj_state, int argc, const char *argv[])
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans return (cmd_body(CAP_CMD_T_HW2, obj_state, argc, argv));
d29b2c4438482eb00488be49a1f5d6835f455546ab/*ARGSUSED*/
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* For commands that only accept -capid, -and, -cmp, -o, and -or */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans static elfedit_cmd_optarg_t opt_ostyle_capid_bitop[] = {
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_OPTDESC_CAPID) */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans ELFEDIT_I18NHDL(MSG_OPTDESC_CAPID), ELFEDIT_CMDOA_F_VALUE,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* For commands that only accept -capid and -capndx */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans static elfedit_cmd_optarg_t opt_capid_capndx[] = {
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_OPTDESC_CAPID) */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans ELFEDIT_I18NHDL(MSG_OPTDESC_CAPID), ELFEDIT_CMDOA_F_VALUE,
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_OPTDESC_CAPNDX) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:dump */
d29b2c4438482eb00488be49a1f5d6835f455546ab MSG_ORIG(MSG_STR_EMPTY), /* "" makes this the default command */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_ARGDESC_ELT) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:tag */
d29b2c4438482eb00488be49a1f5d6835f455546ab static const char *name_tag[] = { MSG_ORIG(MSG_CMD_TAG), NULL };
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_OPTDESC_CAPID) */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans ELFEDIT_I18NHDL(MSG_OPTDESC_CAPID), ELFEDIT_CMDOA_F_VALUE,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_OPTDESC_CAPNDX) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_A1_TAG_ELT) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_A2_TAG_VALUE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:value */
d29b2c4438482eb00488be49a1f5d6835f455546ab static const char *name_value[] = { MSG_ORIG(MSG_CMD_VALUE), NULL };
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_OPTDESC_CAPID) */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans ELFEDIT_I18NHDL(MSG_OPTDESC_CAPID), ELFEDIT_CMDOA_F_VALUE,
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_OPTDESC_CAPNDX) */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_OPTDESC_S) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_ARGDESC_ELT) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_A2_VALUE_VALUE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:delete */
d29b2c4438482eb00488be49a1f5d6835f455546ab static const char *name_delete[] = { MSG_ORIG(MSG_CMD_DELETE), NULL };
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_ARGDESC_ELT) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_A2_DELETE_COUNT) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:move */
d29b2c4438482eb00488be49a1f5d6835f455546ab static const char *name_move[] = { MSG_ORIG(MSG_CMD_MOVE), NULL };
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_ARGDESC_ELT) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_A2_MOVE_DST_INDEX) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_A3_MOVE_COUNT) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:hw1 */
d29b2c4438482eb00488be49a1f5d6835f455546ab static const char *name_hw1[] = { MSG_ORIG(MSG_CMD_HW1), NULL };
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_A1_HW1_VALUE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:sf1 */
d29b2c4438482eb00488be49a1f5d6835f455546ab static const char *name_sf1[] = { MSG_ORIG(MSG_CMD_SF1), NULL };
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_A1_SF1_VALUE) */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* cap:hw2 */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans static const char *name_hw2[] = { MSG_ORIG(MSG_CMD_HW2), NULL };
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_A1_HW2_VALUE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:dump */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_DESC_DUMP) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_HELP_DUMP) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:tag */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_DESC_TAG) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_HELP_TAG) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:value */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_DESC_VALUE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_HELP_VALUE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:delete */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_DESC_DELETE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_HELP_DELETE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:move */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_DESC_MOVE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_HELP_MOVE) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:hw1 */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_DESC_HW1) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_HELP_HW1) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* cap:sf1 */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_DESC_SF1) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_HELP_SF1) */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* cap:hw2 */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_DESC_HW2) */
08278a5e91755ccdb5850c19d21d42fb2e16b50eRod Evans /* MSG_INTL(MSG_HELP_HW2) */
d29b2c4438482eb00488be49a1f5d6835f455546ab /* MSG_INTL(MSG_MOD_DESC) */
d29b2c4438482eb00488be49a1f5d6835f455546ab return (&module);