2 .\" Copyright (c) 2008, Sun Microsystems Inc. All
4 .\" 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.
5 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
6 .\" 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]
7 .TH ELFEDIT 1 "Jan 28, 2008"
9 elfedit \- examine or edit ELF files
13 \fBelfedit\fR [\fB-adr\fR] [\fB-e\fR \fIcmd\fR] [\fB-L\fR \fIpath\fR] [\fB-o\fR \fBdefault\fR | \fBsimple\fR | \fBnum\fR]
14 [\fIinfile\fR] [\fIoutfile\fR]
20 \fBelfedit\fR is a tool for examining or modifying the contents of an existing
21 ELF object. Specifically, \fBelfedit\fR is used to modify the ELF metadata
22 contained in the object. Access is provided to most of the ELF data contained
23 in an object, including the ELF header, section header table, program header
24 table, dynamic section, hardware and software capabilities, string tables, and
29 \fBelfedit\fR processes commands from the command line (\fB-e\fR option) or
30 from standard input. If standard input is a terminal, \fBelfedit\fR provides
31 terminal editing capabilities, as well as extensive command completion. ELF
32 uses many standard symbolic names for special integer values and bit masks.
33 \fBelfedit\fR is aware of most possible completions for such names. You can
34 press TAB at any point while entering an \fBelfedit\fR command to cause
35 \fBelfedit\fR to display a usage message and any known completions for the text
36 at the current cursor.
39 \fBelfedit\fR functionality is organized in the form of modules. Each module
40 delivers a set of commands, focused on related functionality. A command is
41 specified by combining the module and command names with a colon (\fB:\fR)
42 delimiter, with no intervening white space. For example, \fBdyn:runpath\fR
43 refers to the \fBrunpath\fR command provided by the \fBdyn\fR module. Module
44 names must be unique. The command names within a given module are unique within
45 that module, but the same command names can be used in more than one module.
48 Some modules designate one of their commands to be the default command for that
49 module. This command is run when the user specifies only a module name. Most
50 \fBelfedit\fR modules supply a command named dump, which produces the same
51 information displayed by the \fBelfdump\fR utility for the part of the ELF file
52 covered by the module. It is common for a module to specify dump as its default
56 The syntax used to execute an \fBelfedit\fR command is intended to be familiar
57 to anyone who uses UNIX command line utilities. It consists of white space
58 delimited tokens. The first token is the command name. Options, which are
59 arguments that start with the hyphen (\fB-\fR) character follow the command.
60 Plain arguments (operands) follow the options. There can be \fB0\fR or more
61 options and operands for a given command, but if they are present, options
62 always precede plain arguments. The special option, \fB--\fR, (two hyphens) can
63 be used to delimit the end of the options. When it is encountered, any
64 remaining arguments are considered to be plain arguments even if they start
68 The interpretation of the characters in an \fBelfedit\fR token depends on the
69 style of quoting used:
76 Outside of single (') or double (") quotes, backslash (\) acts as an escape
77 character. When a backslash character is seen, \fBelfedit\fR ignores it, and
78 treats the character following it literally (even if the following character is
79 itself a backslash). This feature can be used to insert a white space character
80 into a string argument to a command without having it split the string into two
81 separate tokens. Similarly, it can be used to insert a quote or backslash as a
91 Within single quotes ('), white space characters do not delimit tokens, and are
92 interpreted as literal characters within the token. Double quote (") and
93 backslash (\) characters are interpreted as literal characters, and have no
103 Within double quotes ("), white space characters do not delimit tokens. Single
104 quote characters are interpreted literally and do not have a quoting function.
105 Backslash (\) is an escape character which operates similarly to the way it is
106 used in the C programming language within a string literal:
203 An octal constant, where ooo is one to three octal digits (0...7)
206 Any other character following a backslash is an error.
211 The core commands belong to an internal module named \fBsys\fR. All other
212 modules are packaged as dynamically loadable sharable objects. \fBelfedit\fR
213 loads modules on demand, when a command that requires it is executed, or as the
214 result of executing the \fBsys:load\fR command. Due to its special built in
215 status, and because its commands are used heavily, \fBelfedit\fR allows you to
216 specify commands from the \fBsys\fR module without including the \fBsys:\fR
217 prefix, for example, \fBload\fR rather than \fBsys:load\fR. To access a command
218 from any other module, you must specify the full \fImodule\fR\fB:\fR\fIcmd\fR
222 \fBelfedit\fR is delivered with the following standard modules:
292 \fBSyminfo\fR Section
301 Core built in \fBelfedit\fR commands
304 .SS "Status And Command Documentation"
307 Status And Command Documentation
310 The status (\fBsys:status\fR) command displays information about the current
311 \fBelfedit\fR session:
316 Input and output files
338 Included with every \fBelfedit\fR module is extensive online documentation for
339 every command, in a format similar to UNIX manual pages. The \fBhelp\fR
340 (\fBsys:help\fR) command is used to display this information. To learn more
341 about \fBelfedit\fR, start \fBelfedit\fR and use the help command without
354 \fBelfedit\fR displays a welcome message with more information about
355 \fBelfedit\fR, and on how to use the help system.
358 To obtain summary information for a module:
369 To obtain the full documentation for a specific command provided by a module:
373 > help module:command
380 Using the \fBdyn\fR module and \fBdyn:runpath\fR commands as examples:
392 help (\fBsys:help\fR) can be used to obtain help on itself:
401 .SS "Module Search Path"
404 \fBelfedit\fR modules are implemented as sharable objects which are loaded on
405 demand. When a module is required, \fBelfedit\fR searches a module path in
406 order to locate the sharable object that implements the module. The path is a
407 sequence of directory names delimited by colon (\fB:\fR) characters. In
408 addition to normal characters, the path can also contain any of the following
416 Expands to the current instruction set architecture (ISA) name (\fBsparc\fR,
417 \fBsparcv9\fR, \fBi386\fR, \fBamd64\fR).
426 Expands to the 64-bit ISA. This is the same thing as \fB%i\fR for 64-bit
427 versions of \fBelfedit\fR, but expands to the empty string for 32-bit versions.
436 Expands to the old value of the path being modified. This is useful for
437 appending or prepending directories to the default path.
446 Root of file system tree holding the \fBelfedit\fR program, assuming that
447 \fBelfedit\fR is installed as \fBusr/bin/elfedi\fRt within the tree. On a
448 standard system, this is simply the standard system root directory (\fB/\fR).
449 On a development system, where the copy of \fBelfedit\fR can be installed
450 elsewhere, the use of \fB%r\fR can be used to ensure that the matching set of
460 Expands to a single \fB%\fR character
465 The default module search path for \fBelfedit\fR is:
469 %r/usr/lib/elfedit/%I
476 Expanding the tokens, this is:
480 \fB\fB/usr/lib/elfedit\fR\fR
489 \fB\fB/usr/lib/elfedit/sparcv9\fR\fR
492 64-bit \fBelfedit\fR (\fBsparc\fR)
498 \fB\fB/usr/lib/elfedit/amd64\fR\fR
501 64-bit \fBelfedit\fR (\fBx86\fR)
506 The default search path can be changed by setting the \fBELFEDIT_PATH\fR
507 environment variable, or by using the \fB-L\fR command line option. If you
508 specify both, the \fB-L\fR option supersedes the environment variable.
512 The following options are supported:
519 Enable \fBautoprint\fR mode. When \fBautoprint\fR is enabled, \fBelfedit\fR
520 prints the modified values that result when the ELF file is modified. This
521 output is shown in the current output style, which can be changed using the
522 \fB-o\fR option. The default output style is the style used by the
523 \fBelfdump\fR(1) utility. \fBautoprint\fR mode is the default when
524 \fBelfedit\fR is used interactively (when \fBstdin\fR and \fBstdout\fR are
525 terminals). Therefore, the \fB-a\fR option only has meaning when \fBelfedit\fR
526 is used in non-interactive contexts. To disable \fBautoprint\fR in an
527 interactive session, use the \fBelfedit\fR command:
544 If set, this option causes \fBelfedit\fR to issue informational messages
545 describing its internal operations and details of the ELF object being
546 processed. This can be useful when a deep understanding of the operation being
547 carried out is desired.
553 \fB\fB-e\fR \fIcmd\fR\fR
556 Specifies an edit command. Multiple \fB-e\fR options can be specified. If edit
557 commands are present on the command line, \fBelfedit\fR operates in batch mode.
558 After opening the file, \fBelfedit\fR executes each command in the order given,
559 after which the modified file is saved and \fBelfedit\fR exits. Batch mode is
560 useful for performing simple operations from shell scripts and makefiles.
566 \fB\fB-L\fR \fIpath\fR\fR
569 Sets default path for locating \fBelfedit\fR modules. Modules are described in
570 \fBModule Search Path\fR section of this manual page..
576 \fB\fB-o\fR \fBdefault\fR | \fBsimple\fR | \fBnum\fR\fR
579 The style used to display ELF data. This option establishes the current style
580 for the session. It can be changed from within the \fBelfedit\fR session by
581 using the set (\fBsys:set\fR) command, or by providing \fB-o\fR options to the
582 individual commands executed within the session.
589 The default style is to display output in a format intended for human viewing.
590 This style is similar to that used by the \fBelfdump\fR utility.
599 Integer values are always shown in integer form. Strings are shown as the
600 integer offset into the containing string table.
609 When displaying strings from within the ELF file, only the string is displayed.
610 Integer values are displayed as symbolic constants if possible, and in integer
611 form otherwise. No titles, headers, or other supplemental output is shown.
622 Read-only mode. The input file is opened for read-only access, and the results
623 of the edit session are not saved. \fBelfedit\fR does not allow the
624 \fIoutfile\fR argument when \fB-r\fR is specified. Read-only mode is highly
625 recommended when there is no intention to modify the file. In addition to
626 providing extra protection against accidental modification, it allows for the
627 examination of files for which the user does not have write permission.
633 The following operands are supported:
640 Input file containing an ELF object to process.
642 This can be an executable (\fBET_EXEC\fR), shared object (\fBET_DYN\fR), or
643 relocatable object file, (\fBET_REL\fR). Archives are not directly supported.
644 To edit an object in an archive, you must extract the object, edit the copy,
645 and then insert it back into the archive.
647 If no \fIinfile\fR is present, \fBelfedit\fR runs in a limited mode that only
648 allows executing commands from the \fBsys:\fR module. This mode is primarily to
649 allow access to the command documentation available from the help
650 (\fBsys:help\fR) command.
652 If \fIinfile\fR is present, and no \fIoutfile\fR is given, \fBelfedit\fR edits
653 the file in place, and writes the results into the same file, causing the
654 original file contents to be overwritten. It is usually recommended that
655 \fBelfedit\fR not be used in this mode, and that an output file be specified.
656 Once the resulting file has been tested and validated, it can be moved into the
657 place of the original file.
659 The \fB-r\fR option can be used to open \fIinfile\fR for read-only access. This
660 can be useful for examining an existing file that you do not wish to modify.
669 Output file. If both \fIinfile\fR and \fIoutfile\fR are present, \fIinfile\fR
670 is opened for read-only access, and the modified object contents are written to
677 When supported by the system, \fBelfedit\fR runs as a 64-bit application,
678 capable of processing files greater than or equal to 2 Gbytes (2^31 bytes).
681 At startup, \fBelfedit\fR uses \fBlibelf\fR to open the input file and cache a
682 copy of its contents in memory for editing. It can then execute one or more
683 commands. A session finishes by optionally writing the modified object to the
684 output file, and then exiting.
687 If no \fIinfile\fR is present, \fBelfedit\fR runs in a limited mode that only
688 allows executing commands from the \fBsys\fR module. This mode is primarily to
689 allow access to the command documentation available from the help
690 (\fBsys:help\fR) command.
693 If one or more \fB-e\fR options are specified, the commands they supply are
694 executed in the order given. \fBelfedit\fR adds implicit calls to write
695 (\fBsys:write\fR) and quit (\fBsys:qui\fRt) immediately following the given
696 commands, causing the output file to be written and the \fBelfedit\fR process
697 to exit. This form of use is convenient in shell scripts and makefiles.
700 If no \fB-e\fR options are specified, \fBelfedit\fR reads commands from
701 \fBstdin\fR and executes them in the order given. The caller must explicitly
702 issue the write (\fBsys:write\fR) and quit (\fBsys:quit\fR) commands to save
703 their work and exit when running in this mode.
707 The following exit values are returned:
714 Successful completion.
723 A fatal error occurred.
732 Invalid command line options were specified.
738 In the following examples, interactive use of \fBelfedit\fR is shown with the
739 shell prompt (\fB%\fR) and the \fBelfedit\fR prompt (\fB>\fR). Neither of these
740 characters should be entered by the user.
742 \fBExample 1 \fRChanging the Runpath of an Executable
745 The following example presupposes an executable named \fBprog\fR, installed in
746 a bin directory that has an adjacent lib directory for sharable objects. The
747 following command sets the \fBrunpath\fR of that executable to the \fBlib\fR
753 elfedit -e 'dyn:runpath $ORIGIN/../lib'
760 The use of single quotes with the argument to the \fB-e\fR option is necessary
761 to ensure that the shell passes the entire command as a single argument to
766 Alternatively, the same operation can be done using \fBelfedit\fR in its
773 > dyn:runpath $ORIGIN/../lib
775 [30] RUNPATH 0x3e6 $ORIGIN/../lib
784 The addition or modification of elements such as \fBrunpath\fR or needed
785 entries might only be achievable when \fBpadding\fR exists within the objects.
789 \fBExample 2 \fRRemoving a Hardware Capability Bit
792 Objects that require optional hardware support to run are built with a
793 capability section that contains a mask of bits specifying which capabilities
794 they need. The runtime linker (\fBld.so.1\fR) checks this mask against the
795 attributes of the running system to determine whether a given object is able to
796 be run by the current system. Programs that require abilities not available on
797 the system are prevented from running.
801 This check prevents a naive program that does not explicitly check for the
802 hardware support it requires from crashing in a confusing manner. However, it
803 can be inconvenient for a program that is written to explicitly check the
804 system capabilities at runtime. Such a program might have optimized code to use
805 when the hardware supports it while providing a generic fallback version that
806 can be run, albeit more slowly, otherwise. In this case, the hardware
807 compatibility mask prevents such a program from running on the older hardware.
808 In such a case, removing the relevant bit from the mask allows the program to
813 The following example removes the \fBAV_386_SSE\fR3 hardware capability from an
814 x86 binary that uses the SSE3 CPU extension. This transfers responsibility for
815 validating the ability to use SSE3 from the runtime linker to the program
821 elfedit -e 'cap:hw1 -and -cmp sse3' prog
827 \fBExample 3 \fRReading Information From an Object
830 \fBelfedit\fR can be used to extract specific targeted information from an
831 object. The following shell command reads the number of section headers
832 contained in the file \fB/usr/bin/ls\fR:
837 % SHNUM=`elfedit -r -onum -e 'ehdr:e_shnum' /usr/bin/ls`
846 You might get a different value, depending on the version of Solaris and type
847 of machine that you are using. The \fB-r\fR option causes the file to be opened
848 read-only, allowing a user with ordinary access permissions to open the file,
849 and protecting against accidental damage to an important system executable. The
850 \fBnum\fR output style is used in order to obtain only the desired value,
851 without any extraneous text.
855 Similarly, the following extracts the symbol type of the symbol \fBunlink\fR
856 from the C runtime library:
861 % TYPE=`elfedit -r -osimple -e 'sym:st_type unlink' /lib/libc.so`
868 .SH ENVIRONMENT VARIABLES
872 \fB\fBELFEDIT_PATH\fR\fR
875 Alters the default module search path. Module search paths are discussed in the
876 \fBModule Search Path\fR section of this manual page.
882 \fB\fBLD_NOEXEC_64\fR\fR
885 Suppresses the automatic execution of the 64-bit \fBelfedit\fR. By default, the
886 64-bit version of \fBelfedit\fR runs if the system is 64-bit capable.
895 Interactively delivers output from \fBelfedit\fR to the screen. If not set,
896 \fBmore\fR is used. See \fBmore\fR(1).
903 \fB\fB/usr/lib/elfedit\fR\fR
906 Default directory for \fBelfedit\fR modules that are loaded on demand to supply
913 \fB\fB~/.teclarc\fR\fR
916 Personal \fBtecla\fR customization file for command line editing. See
923 See \fBattributes\fR(5) for descriptions of the following attributes:
931 ATTRIBUTE TYPE ATTRIBUTE VALUE
933 Interface Stability Committed
939 \fBdump\fR(1), \fBelfdump\fR(1), \fBld.so.1\fR(1), \fBmore\fR(1), \fBnm\fR(1),
940 \fBpvs\fR(1), \fBelf\fR(3ELF), \fBlibelf\fR(3LIB), \fBtecla\fR(5),
944 \fILinker and Libraries Guide\fR
948 \fBelfedit\fR is designed to be a tool for testing and development of the ELF
949 system. It offers the ability to examine and change nearly every piece of ELF
950 metadata in the object. It quietly allows edits that can produce an invalid or
951 unusable ELF file. The user is expected to have knowledge of the ELF format and
952 of the rules and conventions that govern them. The \fILinker and Libraries
953 Guide\fR can be helpful when using \fBelfedit\fR.
956 \fBelfedit\fR allows the user to alter the ELF metadata in an object, but
957 cannot understand or alter the code of the actual program. Setting ELF
958 attributes such as types, sizes, alignments, and so forth in a manner that does
959 not agree with the actual contents of the file is therefore likely to yield a
960 broken and unusable output object. Such changes might be useful for testing of
961 linker components, but should be avoided otherwise.
964 Higher level operations, such as the use of the \fBdyn:runpath\fR command to
965 change the \fBrunpath\fR of an object, are safe, and can be carried out without
966 the sort of risk detailed in this section.
970 Not every ELF operation supported by \fBelfedit\fR can be successfully carried
971 out on every ELF object. \fBelfedit\fR is constrained by the existing sections
975 One area of particular interest is that \fBelfedit\fR might not be able to
976 modify the \fBrunpath\fR of a given object. To modify a \fBrunpath\fR, the
977 following must be true:
982 The desired string must already exist in the dynamic string table, or there
983 must be enough reserved space within this section for the new string to be
984 added. If your object has a string table reservation area, the value of the
985 \fB\&.dynamic DT_SUNW_STRPAD\fR element indicates the size of the area. The
986 following \fBelfedit\fR command can be used to check this:
990 % elfedit -r -e 'dyn:tag DT_SUNW_STRPAD' file
1000 The dynamic section must already have a \fBrunpath\fR element, or there must be
1001 an unused dynamic slot available where one can be inserted. To test for the
1002 presence of an existing \fBrunpath\fR:
1006 % elfedit -r -e 'dyn:runpath' file
1011 A dynamic section uses an element of type \fBDT_NULL\fR to terminate the array
1012 found in that section. The final \fBDT_NULL\fR cannot be changed, but if there
1013 are more than one of these, \fBelfedit\fR can convert one of them into a
1014 \fBrunpath\fR element. To test for extra dynamic slots:
1018 % elfedit -r -e 'dyn:tag DT_NULL' file
1026 Older objects do not have the extra space necessary to complete such
1027 operations. The space necessary to do so was introduced in the Solaris Express
1028 Community Edition release.
1031 When an operation fails, the detailed information printed using the \fB-d\fR
1032 (debug) option can be very helpful in uncovering the reason why.
1035 \fBelfedit\fR modules follow a convention by which commands that directly
1036 manipulate a field in an ELF structure have the same name as the field, while
1037 commands that implement higher level concepts do not. For instance, the command
1038 to manipulate the \fBe_flags\fR field in the ELF header is named
1039 \fBehdr:e_flags\fR. Therefore, you generally find the command to modify ELF
1040 fields by identifying the module and looking for a command with the name of the