Project

General

Profile

Submit #3145 ยป patch-libelf.patch

davshao, 09/03/2018 11:42 PM

View differences:

contrib/elftoolchain/libelf/Makefile
# $Id: Makefile 2605 2012-10-02 17:52:20Z jkoshy $
# $Id: Makefile 3594 2018-04-11 18:26:50Z jkoshy $
TOP= ${.CURDIR}/..
contrib/elftoolchain/libelf/_libelf_config.h
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: _libelf_config.h 3168 2015-02-24 19:17:47Z emaste $
* $Id: _libelf_config.h 3566 2017-08-31 02:28:40Z emaste $
*/
#if defined(__APPLE__) || defined(__DragonFly__)
......
#define LIBELF_BYTEORDER ELFDATA2MSB
#define LIBELF_CLASS ELFCLASS32
#elif defined(__riscv) && (__riscv_xlen == 64)
#define LIBELF_ARCH EM_RISCV
#define LIBELF_BYTEORDER ELFDATA2LSB
#define LIBELF_CLASS ELFCLASS64
#elif defined(__sparc__)
#define LIBELF_ARCH EM_SPARCV9
contrib/elftoolchain/libelf/elf.3
.\" Copyright (c) 2006-2008,2011 Joseph Koshy. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" This software is provided by Joseph Koshy ``as is'' and
.\" any express or implied warranties, including, but not limited to, the
.\" implied warranties of merchantability and fitness for a particular purpose
.\" are disclaimed. in no event shall Joseph Koshy be liable
.\" for any direct, indirect, incidental, special, exemplary, or consequential
.\" damages (including, but not limited to, procurement of substitute goods
.\" or services; loss of use, data, or profits; or business interruption)
.\" however caused and on any theory of liability, whether in contract, strict
.\" liability, or tort (including negligence or otherwise) arising in any way
.\" out of the use of this software, even if advised of the possibility of
.\" such damage.
.\"
.\" $Id: elf.3 3195 2015-05-12 17:22:19Z emaste $
.\"
.Dd July 28, 2014
.Os
.Dt ELF 3
.Sh NAME
.Nm elf
.Nd API for manipulating ELF objects
.Sh LIBRARY
.Lb libelf
.Sh SYNOPSIS
.In libelf.h
.Sh DESCRIPTION
The
.Lb libelf
provides functions that allow an application to read and manipulate
ELF object files, and to read
.Xr ar 1
archives.
The library allows the manipulation of ELF objects in a byte ordering
and word-size independent way, allowing an application to read and
create ELF objects for 32 and 64 bit architectures and for little-
and big-endian machines.
The library is capable of processing ELF objects that use extended
section numbering.
.Pp
This manual page serves to provide an overview of the functionality in
the ELF library.
Further information may found in the manual pages for individual
.Xr ELF 3
functions that comprise the library.
.Ss ELF Concepts
As described in
.Xr elf 5 ,
ELF files contain several data structures that are laid out in a
specific way.
ELF files begin with an
.Dq Executable Header ,
and may contain an optional
.Dq Program Header Table ,
and optional data in the form of ELF
.Dq sections .
A
.Dq Section Header Table
describes the content of the data in these sections.
.Pp
ELF objects have an associated
.Dq "ELF class"
which denotes the natural machine word size for the architecture
the object is associated with.
Objects for 32 bit architectures have an ELF class of
.Dv ELFCLASS32 .
Objects for 64 bit architectures have an ELF class of
.Dv ELFCLASS64 .
.Pp
ELF objects also have an associated
.Dq endianness
which denotes the endianness of the machine architecture associated
with the object.
This may be
.Dv ELFDATA2LSB
for little-endian architectures and
.Dv ELFDATA2MSB
for big-endian architectures.
.Pp
ELF objects are also associated with an API version number.
This version number determines the layout of the individual components
of an ELF file and the semantics associated with these.
.Ss Data Representation And Translation
The
.Xr ELF 3
library distinguishes between
.Dq native
representations of ELF data structures and their
.Dq file
representations.
.Pp
An application would work with ELF data in its
.Dq native
representation, i.e., using the native byteorder and alignment mandated
by the processor the application is running on.
The
.Dq file
representation of the same data could use a different byte ordering
and follow different constraints on object alignment than these native
constraints.
.Pp
Accordingly, the
.Xr ELF 3
library offers translation facilities
.Xr ( elf32_xlatetof 3 ,
.Xr elf32_xlatetom 3 ,
.Xr elf64_xlatetof 3
and
.Xr elf64_xlatetom 3 )
to and from these
representations and also provides higher-level APIs that retrieve and store
data from the ELF object in a transparent manner.
.Ss Library Working Version
Conceptually, there are three version numbers associated with an
application using the ELF library to manipulate ELF objects:
.Bl -bullet -compact -offset indent
.It
The ELF version that the application was compiled against.
This version determines the ABI expected by the application.
.It
The ELF version of the ELF object being manipulated by the
application through the ELF library.
.It
The ELF version (or set of versions) supported by the ELF library itself.
.El
.Pp
In order to facilitate working with ELF objects of differing versions,
the ELF library requires the application to call the
.Fn elf_version
function before invoking many of its operations, in order to inform
the library of the application's desired working version.
.Pp
In the current implementation, all three versions have to be
.Dv EV_CURRENT .
.Ss Namespace use
The ELF library uses the following prefixes:
.Bl -tag -width "ELF_F_*"
.It Dv elf_
Used for class-independent functions.
.It Dv elf32_
Used for functions working with 32 bit ELF objects.
.It Dv elf64_
Used for functions working with 64 bit ELF objects.
.It Dv Elf_
Used for class-independent data types.
.It Dv ELF_C_
Used for command values used in a few functions.
These symbols are defined as members of the
.Vt Dv Elf_Cmd
enumeration.
.It Dv ELF_E_
Used for error numbers.
.It Dv ELF_F_
Used for flags.
.It Dv ELF_K_
These constants define the kind of file associated with an ELF
descriptor.
See
.Xr elf_kind 3 .
The symbols are defined by the
.Vt Elf_Kind
enumeration.
.It Dv ELF_T_
These values are defined by the
.Vt Elf_Type
enumeration, and denote the types of ELF data structures
that can be present in an ELF object.
.El
.Pp
In addition, the library uses symbols with prefixes
.Dv _ELF
and
.Dv _libelf
for its internal use.
.Ss Descriptors
Applications communicate with the library using descriptors.
These are:
.Bl -tag -width ".Vt Elf_Data"
.It Vt Elf
An
.Vt Elf
descriptor represents an ELF object or an
.Xr ar 1
archive.
It is allocated using one of the
.Fn elf_begin
or
.Fn elf_memory
functions.
An
.Vt Elf
descriptor can be used to read and write data to an ELF file.
An
.Vt Elf
descriptor can be associated with zero or more
.Vt Elf_Scn
section descriptors.
.Pp
Given an ELF descriptor, the application may retrieve the ELF
object's class-dependent
.Dq "Executable Header"
structures using the
.Fn elf32_getehdr
or
.Fn elf64_getehdr
functions.
A new Ehdr structure may be allocated using the
.Fn elf64_newehdr
or
.Fn elf64_newehdr
functions.
.Pp
The
.Dq "Program Header Table"
associated with an ELF descriptor may be allocated using the
.Fn elf32_getphdr
or
.Fn elf64_getphdr
functions.
A new program header table may be allocated or an existing table
resized using the
.Fn elf32_newphdr
or
.Fn elf64_newphdr
functions.
.Pp
The
.Vt Elf
structure is opaque and has no members visible to the
application.
.\" TODO describe the Elf_Arhdr and Elf_Arsym structures.
.It Vt Elf_Data
An
.Vt Elf_Data
data structure describes an individual chunk of a ELF file as
represented in memory.
It has the following application-visible members:
.Bl -tag -width ".Vt unsigned int d_version" -compact
.It Vt "uint64_t d_align"
The in-file alignment of the data buffer within its containing ELF section.
This value must be non-zero and a power of two.
.It Vt "void *d_buf"
A pointer to data in memory.
.It Vt "uint64_t d_off"
The offset within the containing section where this descriptor's data
would be placed.
This field will be computed by the library unless the application
requests full control of the ELF object's layout.
.It Vt "uint64_t d_size"
The number of bytes of data in this descriptor.
.It Vt "Elf_Type d_type"
The ELF type (see below) of the data in this descriptor.
.It Vt "unsigned int d_version"
The operating version for the data in this buffer.
.El
.Pp
.Vt Elf_Data
descriptors are usually associated with
.Vt Elf_Scn
descriptors.
Existing data descriptors associated with an ELF section may be
structures are retrieved using the
.Fn elf_getdata
and
.Fn elf_rawdata
functions.
The
.Fn elf_newdata
function may be used to attach new data descriptors to an ELF section.
.It Vt Elf_Scn
.Vt Elf_Scn
descriptors represent a section in an ELF object.
.Pp
They are retrieved using the
.Fn elf_getscn
function.
An application may iterate through the existing sections of an ELF
object using the
.Fn elf_nextscn
function.
New sections may be allocated using the
.Fn elf_newscn
function.
.Pp
The
.Vt Elf_Scn
descriptor is opaque and contains no application modifiable fields.
.El
.Ss Supported Elf Types
The following ELF datatypes are supported by the library.
.Pp
.Bl -tag -width ".Dv ELF_T_SYMINFO" -compact
.It Dv ELF_T_ADDR
Machine addresses.
.It Dv ELF_T_BYTE
Byte data.
The library will not attempt to translate byte data.
.It Dv ELF_T_CAP
Software and hardware capability records.
.It Dv ELF_T_DYN
Records used in a section of type
.Dv SHT_DYNAMIC .
.It Dv ELF_T_EHDR
ELF executable header.
.It Dv ELF_T_GNUHASH
GNU-style hash tables.
.It Dv ELF_T_HALF
16-bit unsigned words.
.It Dv ELF_T_LWORD
64 bit unsigned words.
.It Dv ELF_T_MOVE
ELF Move records.
.\".It Dv ELF_T_MOVEP
.\" As yet unsupported.
.It Dv ELF_T_NOTE
ELF Note structures.
.It Dv ELF_T_OFF
File offsets.
.It Dv ELF_T_PHDR
ELF program header table entries.
.It Dv ELF_T_REL
ELF relocation entries.
.It Dv ELF_T_RELA
ELF relocation entries with addends.
.It Dv ELF_T_SHDR
ELF section header entries.
.It Dv ELF_T_SWORD
Signed 32-bit words.
.It Dv ELF_T_SXWORD
Signed 64-bit words.
.It Dv ELF_T_SYMINFO
ELF symbol information.
.It Dv ELF_T_SYM
ELF symbol table entries.
.It Dv ELF_T_VDEF
Symbol version definition records.
.It Dv ELF_T_VNEED
Symbol version requirement records.
.It Dv ELF_T_WORD
Unsigned 32-bit words.
.It Dv ELF_T_XWORD
Unsigned 64-bit words.
.El
.Pp
The symbol
.Dv ELF_T_NUM
denotes the number of Elf types known to the library.
.Pp
The following table shows the mapping between ELF section types
defined in
.Xr elf 5
and the types supported by the library.
.Bl -column ".Dv SHT_PREINIT_ARRAY" ".Dv ELF_T_SYMINFO"
.It Em Section Type Ta Em "Library Type" Ta Em Description
.It Dv SHT_DYNAMIC Ta Dv ELF_T_DYN Ta Xo
.Sq .dynamic
section entries.
.Xc
.It Dv SHT_DYNSYM Ta Dv ELF_T_SYM Ta Symbols for dynamic linking.
.It Dv SHT_FINI_ARRAY Ta Dv ELF_T_ADDR Ta Termination function pointers.
.It Dv SHT_GNU_HASH Ta Dv ELF_T_GNUHASH Ta GNU hash sections.
.It Dv SHT_GNU_LIBLIST Ta Dv ELF_T_WORD Ta List of libraries to be pre-linked.
.It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions.
.It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements.
.It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols.
.It Dv SHT_GROUP Ta Dv ELF_T_WORD Ta Section group marker.
.It Dv SHT_HASH Ta Dv ELF_T_HASH Ta Symbol hashes.
.It Dv SHT_INIT_ARRAY Ta Dv ELF_T_ADDR Ta Initialization function pointers.
.It Dv SHT_NOBITS Ta Dv ELF_T_BYTE Ta Xo
Empty sections.
See
.Xr elf 5 .
.Xc
.It Dv SHT_NOTE Ta Dv ELF_T_NOTE Ta ELF note records.
.It Dv SHT_PREINIT_ARRAY Ta Dv ELF_T_ADDR Ta Pre-initialization function pointers.
.It Dv SHT_PROGBITS Ta Dv ELF_T_BYTE Ta Machine code.
.It Dv SHT_REL Ta Dv ELF_T_REL Ta ELF relocation records.
.It Dv SHT_RELA Ta Dv ELF_T_RELA Ta Relocation records with addends.
.It Dv SHT_STRTAB Ta Dv ELF_T_BYTE Ta String tables.
.It Dv SHT_SYMTAB Ta Dv ELF_T_SYM Ta Symbol tables.
.It Dv SHT_SYMTAB_SHNDX Ta Dv ELF_T_WORD Ta Used with extended section numbering.
.It Dv SHT_SUNW_dof Ta Dv ELF_T_BYTE Ta Xo
Used by
.Xr dtrace 1 .
.Xc
.It Dv SHT_SUNW_move Ta Dv ELF_T_MOVE Ta ELF move records.
.It Dv SHT_SUNW_syminfo Ta Dv ELF_T_SYMINFO Ta Additional symbol flags.
.It Dv SHT_SUNW_verdef Ta Dv ELF_T_VDEF Ta Xo
Same as
.Dv SHT_GNU_verdef .
.Xc
.It Dv SHT_SUNW_verneed Ta Dv ELF_T_VNEED Ta Xo
Same as
.Dv SHT_GNU_verneed .
.Xc
.It Dv SHT_SUNW_versym Ta Dv ELF_T_HALF Ta Xo
Same as
.Dv SHT_GNU_versym .
.Xc
.El
.Pp
Section types in the range
.Ns [ Dv SHT_LOOS ,
.Dv SHT_HIUSER ]
are otherwise considered to be of type
.Dv ELF_T_BYTE .
.Ss Functional Grouping
This section contains a brief overview of the available functionality
in the ELF library.
Each function listed here is described further in its own manual page.
.Bl -tag -width indent
.It "Archive Access"
.Bl -tag -compact
.It Fn elf_getarsym
Retrieve the archive symbol table.
.It Fn elf_getarhdr
Retrieve the archive header for an object.
.It Fn elf_getbase
Retrieve the offset of a member inside an archive.
.It Fn elf_next
Iterate through an
.Xr ar 1
archive.
.It Fn elf_rand
Random access inside an
.Xr ar 1
archive.
.El
.It "Data Structures"
.Bl -tag -compact
.It Fn elf_getdata
Retrieve translated data for an ELF section.
.It Fn elf_getscn
Retrieve the section descriptor for a named section.
.It Fn elf_ndxscn
Retrieve the index for a section.
.It Fn elf_newdata
Add a new
.Vt Elf_Data
descriptor to an ELF section.
.It Fn elf_newscn
Add a new section descriptor to an ELF descriptor.
.It Fn elf_nextscn
Iterate through the sections in an ELF object.
.It Fn elf_rawdata
Retrieve untranslated data for an ELF section.
.It Fn elf_rawfile
Return a pointer to the untranslated file contents for an ELF object.
.It Fn elf32_getehdr , Fn elf64_getehdr
Retrieve the Executable Header in an ELF object.
.It Fn elf32_getphdr , Fn elf64_getphdr
Retrieve the Program Header Table in an ELF object.
.It Fn elf32_getshdr , Fn elf64_getshdr
Retrieve the ELF section header associated with an
.Vt Elf_Scn
descriptor.
.It Fn elf32_newehdr , Fn elf64_newehdr
Allocate an Executable Header in an ELF object.
.It Fn elf32_newphdr , Fn elf64_newphdr
Allocate or resize the Program Header Table in an ELF object.
.El
.It "Data Translation"
.Bl -tag -compact
.It Fn elf32_xlatetof , Fn elf64_xlatetof
Translate an ELF data structure from its native representation to its
file representation.
.It Fn elf32_xlatetom , Fn elf64_xlatetom
Translate an ELF data structure from its file representation to a
native representation.
.El
.It "Error Reporting"
.Bl -tag -compact
.It Fn elf_errno
Retrieve the current error.
.It Fn elf_errmsg
Retrieve a human readable description of the current error.
.El
.It "Initialization"
.Bl -tag -compact
.It Fn elf_begin
Opens an
.Xr ar 1
archive or ELF object given a file descriptor.
.It Fn elf_end
Close an ELF descriptor and release all its resources.
.It Fn elf_memory
Opens an
.Xr ar 1
archive or ELF object present in a memory arena.
.It Fn elf_version
Sets the operating version.
.El
.It "IO Control"
.Bl -tag -width ".Fn elf_setshstrndx" -compact
.It Fn elf_cntl
Manage the association between and ELF descriptor and its underlying file.
.It Fn elf_flagdata
Mark an
.Vt Elf_Data
descriptor as dirty.
.It Fn elf_flagehdr
Mark the ELF Executable Header in an ELF descriptor as dirty.
.It Fn elf_flagphdr
Mark the ELF Program Header Table in an ELF descriptor as dirty.
.It Fn elf_flagscn
Mark an
.Vt Elf_Scn
descriptor as dirty.
.It Fn elf_flagshdr
Mark an ELF Section Header as dirty.
.It Fn elf_setshstrndx
Set the index of the section name string table for the ELF object.
.It Fn elf_update
Recompute ELF object layout and optionally write the modified object
back to the underlying file.
.El
.It "Queries"
.Bl -tag -width ".Fn elf_getshstrndx" -compact
.It Fn elf32_checksum , Fn elf64_checkum
Compute checksum of an ELF object.
.It Fn elf_getident
Retrieve the identification bytes for an ELF object.
.It Fn elf_getshnum
Retrieve the number of sections in an ELF object.
.It Fn elf_getshstrndx
Retrieve the section index of the section name string table in
an ELF object.
.It Fn elf_hash
Compute the ELF hash value of a string.
.It Fn elf_kind
Query the kind of object associated with an ELF descriptor.
.It Fn elf32_fsize , Fn elf64_fsize
Return the size of the file representation of an ELF type.
.El
.El
.Ss Controlling ELF Object Layout
In the usual mode of operation, library will compute section
offsets and alignments based on the contents of an ELF descriptor's
sections without need for further intervention by the
application.
.Pp
However, if the application wishes to take complete charge of the
layout of the ELF file, it may set the
.Dv ELF_F_LAYOUT
flag on an ELF descriptor using
.Xr elf_flagelf 3 ,
following which the library will use the data offsets and alignments
specified by the application when laying out the file.
Application control of file layout is described further in the
.Xr elf_update 3
manual page.
.Pp
Gaps in between sections will be filled with the fill character
set by function
.Fn elf_fill .
.Ss Error Handling
In case an error is encountered, these library functions set an
internal error number and signal the presence of the error by
returning an special return value.
The application can check the
current error number by calling
.Xr elf_errno 3 .
A human readable description of the recorded error is available by
calling
.Xr elf_errmsg 3 .
.Ss Memory Management Rules
The library keeps track of all
.Vt Elf_Scn
and
.Vt Elf_Data
descriptors associated with an ELF descriptor and recovers them
when the descriptor is closed using
.Xr elf_end 3 .
Thus the application must not call
.Xr free 3
on data structures allocated by the ELF library.
.Pp
Conversely the library will not
free data that it has not allocated.
As an example, an application may call
.Xr elf_newdata 3
to allocate a new
.Vt Elf_Data
descriptor and can set the
.Va d_off
member of the descriptor to point to a region of memory allocated
using
.Xr malloc 3 .
It is the applications responsibility to free this arena, though the
library will reclaim the space used by the
.Vt Elf_Data
descriptor itself.
.Sh SEE ALSO
.Xr gelf 3 ,
.Xr elf 5
.Sh HISTORY
The original ELF(3) API was developed for Unix System V.
The current implementation of the ELF(3) API appeared in
.Fx 7.0 .
.Sh AUTHORS
The ELF library was written by
.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org .
contrib/elftoolchain/libelf/elf_begin.3
.\" Copyright (c) 2006,2008-2011 Joseph Koshy. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" This software is provided by Joseph Koshy ``as is'' and
.\" any express or implied warranties, including, but not limited to, the
.\" implied warranties of merchantability and fitness for a particular purpose
.\" are disclaimed. in no event shall Joseph Koshy be liable
.\" for any direct, indirect, incidental, special, exemplary, or consequential
.\" damages (including, but not limited to, procurement of substitute goods
.\" or services; loss of use, data, or profits; or business interruption)
.\" however caused and on any theory of liability, whether in contract, strict
.\" liability, or tort (including negligence or otherwise) arising in any way
.\" out of the use of this software, even if advised of the possibility of
.\" such damage.
.\"
.\" $Id: elf_begin.3 3182 2015-04-10 16:08:10Z emaste $
.\"
.Dd December 11, 2011
.Os
.Dt ELF_BEGIN 3
.Sh NAME
.Nm elf_begin
.Nd open an ELF file or ar(1) archive
.Sh LIBRARY
.Lb libelf
.Sh SYNOPSIS
.In libelf.h
.Ft "Elf *"
.Fn elf_begin "int fd" "Elf_Cmd cmd" "Elf *elf"
.Sh DESCRIPTION
Function
.Fn elf_begin
is used to open ELF files and
.Xr ar 1
archives for further processing by other APIs in the
.Xr elf 3
library.
It is also used to access individual ELF members of an
.Xr ar 1
archive in combination with the
.Xr elf_next 3
and
.Xr elf_rand 3
APIs.
.Pp
Argument
.Ar fd
is an open file descriptor returned from an
.Xr open 2
system call.
Function
.Fn elf_begin
uses argument
.Ar fd
for reading or writing depending on the value of argument
.Ar cmd .
Argument
.Ar elf
is primarily used for iterating through archives.
.Pp
The argument
.Ar cmd
can have the following values:
.Bl -tag -width "ELF_C_WRITE"
.It ELF_C_NULL
Causes
.Fn elf_begin
to return NULL.
Arguments
.Ar fd
and
.Ar elf
are ignored, and no additional error is signalled.
.It ELF_C_READ
This value is to be when the application wishes to examine (but not
modify) the contents of the file specified by the arguments
.Ar fd
and
.Ar elf .
It can be used for both
.Xr ar 1
archives and for ELF objects.
.Pp
If argument
.Ar elf
is NULL, the library will allocate a new ELF descriptor for the file
being processed.
The argument
.Ar fd
should have been opened for reading.
.Pp
If argument
.Ar elf
is not NULL, and references a regular ELF file previously opened with
.Fn elf_begin ,
then the activation count for the descriptor referenced by argument
.Ar elf
is incremented.
The value in argument
.Ar fd
should match that used to open the descriptor argument
.Ar elf .
.Pp
If argument
.Ar elf
is not NULL, and references a descriptor for an
.Xr ar 1
archive opened earlier with
.Fn elf_begin ,
a descriptor for an element in the archive is returned as
described in the section
.Sx "Processing ar(1) archives"
below.
The value for argument
.Ar fd
should match that used to open the archive earlier.
.Pp
If argument
.Ar elf
is not NULL, and references an
.Xr ar 1
archive opened earlier with
.Fn elf_memory ,
then the value of the argument
.Ar fd
is ignored.
.It Dv ELF_C_RDWR
This command is used to prepare an ELF file for reading and writing.
This command is not supported for
.Xr ar 1
archives.
.Pp
Argument
.Ar fd
should have been opened for reading and writing.
If argument
.Ar elf
is NULL, the library will allocate a new ELF descriptor for
the file being processed.
If the argument
.Ar elf
is non-null, it should point to a descriptor previously
allocated with
.Fn elf_begin
with the same values for arguments
.Ar fd
and
.Ar cmd ;
in this case the library will increment the activation count for descriptor
.Ar elf
and return the same descriptor.
.Pp
Changes to the in-memory image of the ELF file may be written back to
disk using the
.Xr elf_update 3
function.
.It Dv ELF_C_WRITE
This command is used when the application wishes to create a new ELF
file.
Argument
.Ar fd
should have been opened for writing.
Argument
.Ar elf
is ignored, and the previous contents of file referenced by argument
.Ar fd
are overwritten.
.El
.Ss Processing ar(1) archives
An
.Xr ar 1
archive may be opened in read mode (with argument
.Ar cmd
set to
.Dv ELF_C_READ )
using
.Fn elf_begin
or
.Fn elf_memory .
The returned ELF descriptor can be passed into to
subsequent calls to
.Fn elf_begin
to access individual members of the archive.
.Pp
Random access within an opened archive is possible using
the
.Xr elf_next 3
and
.Xr elf_rand 3
functions.
.Pp
The symbol table of the archive may be retrieved
using
.Xr elf_getarsym 3 .
.Sh RETURN VALUES
The function returns a pointer to a ELF descriptor if successful, or NULL
if an error occurred.
.Sh EXAMPLES
To iterate through the members of an
.Xr ar 1
archive, use:
.Bd -literal -offset indent
Elf_Cmd c;
Elf *ar_e, *elf_e;
\&...
c = ELF_C_READ;
if ((ar_e = elf_begin(fd, c, (Elf *) 0)) == 0) {
\&... handle error in opening the archive ...
}
while ((elf_e = elf_begin(fd, c, ar_e)) != 0) {
\&... process member referenced by elf_e here ...
c = elf_next(elf_e);
elf_end(elf_e);
}
.Ed
.Pp
To create a new ELF file, use:
.Bd -literal -offset indent
int fd;
Elf *e;
\&...
if ((fd = open("filename", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) {
\&... handle the error from open(2) ...
}
if ((e = elf_begin(fd, ELF_C_WRITE, (Elf *) 0)) == 0) {
\&... handle the error from elf_begin() ...
}
\&... create the ELF image using other elf(3) APIs ...
elf_update(e, ELF_C_WRITE);
elf_end(e);
.Ed
.Sh ERRORS
Function
.Fn elf_begin
can fail with the following errors:
.Bl -tag -width "[ELF_E_RESOURCE]"
.It Bq Er ELF_E_ARCHIVE
The archive denoted by argument
.Ar elf
could not be parsed.
.It Bq Er ELF_E_ARGUMENT
The value in argument
.Ar cmd
was unrecognized.
.It Bq Er ELF_E_ARGUMENT
A non-null value for argument
.Ar elf
was specified when
.Ar cmd
was set to
.Dv ELF_C_RDWR .
.It Bq Er ELF_E_ARGUMENT
The value of argument
.Ar fd
differs from the one the ELF descriptor
.Ar elf
was created with.
.It Bq Er ELF_E_ARGUMENT
Argument
.Ar cmd
differs from the value specified when ELF descriptor
.Ar elf
was created.
.It Bq Er ELF_E_ARGUMENT
An
.Xr ar 1
archive was opened with
.Ar cmd
set to
.Dv ELF_C_RDWR .
.It Bq Er ELF_E_ARGUMENT
The file referenced by argument
.Ar fd
was empty.
.It Bq Er ELF_E_ARGUMENT
The underlying file for argument
.Ar fd
was of an unsupported type.
.It Bq Er ELF_E_IO
The file descriptor in argument
.Ar fd
was invalid.
.It Bq Er ELF_E_IO
The file descriptor in argument
.Ar fd
could not be read or written to.
.It Bq Er ELF_E_RESOURCE
An out of memory condition was encountered.
.It Bq Er ELF_E_SEQUENCE
Function
.Fn elf_begin
was called before a working version was established with
.Xr elf_version 3 .
.It Bq Er ELF_E_VERSION
The ELF object referenced by argument
.Ar fd
was of an unsupported ELF version.
.El
.Sh SEE ALSO
.Xr elf 3 ,
.Xr elf_end 3 ,
.Xr elf_errno 3 ,
.Xr elf_memory 3 ,
.Xr elf_next 3 ,
.Xr elf_rand 3 ,
.Xr elf_update 3 ,
.Xr gelf 3
contrib/elftoolchain/libelf/elf_cntl.3
.\" Copyright (c) 2006,2008 Joseph Koshy. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" This software is provided by Joseph Koshy ``as is'' and
.\" any express or implied warranties, including, but not limited to, the
.\" implied warranties of merchantability and fitness for a particular purpose
.\" are disclaimed. in no event shall Joseph Koshy be liable
.\" for any direct, indirect, incidental, special, exemplary, or consequential
.\" damages (including, but not limited to, procurement of substitute goods
.\" or services; loss of use, data, or profits; or business interruption)
.\" however caused and on any theory of liability, whether in contract, strict
.\" liability, or tort (including negligence or otherwise) arising in any way
.\" out of the use of this software, even if advised of the possibility of
.\" such damage.
.\"
.\" $Id: elf_cntl.3 3181 2015-04-10 13:22:51Z emaste $
.\"
.Dd August 9, 2006
.Os
.Dt ELF_CNTL 3
.Sh NAME
.Nm elf_cntl
.Nd control an elf file descriptor
.Sh LIBRARY
.Lb libelf
.Sh SYNOPSIS
.In libelf.h
.Ft int
.Fn elf_cntl "Elf *elf" "Elf_Cmd cmd"
.Sh DESCRIPTION
Function
.Fn elf_cntl
controls the ELF library's subsequent use of the file descriptor
used to create ELF descriptor
.Ar elf .
.Pp
Argument
.Ar cmd
informs the library of the action to be taken:
.Bl -tag -width "ELF_C_FDDONE"
.It Dv ELF_C_FDDONE
This value instructs the ELF library not to perform any further
I/O on the file descriptor associated with argument
.Ar elf .
For ELF descriptors opened with mode
.Ar ELF_C_WRITE
or
.Ar ELF_C_RDWR
subsequent
.Fn elf_update
operations on the descriptor will fail.
.It Dv ELF_C_FDREAD
This value instructs the ELF library to read in all necessary
data associated with ELF descriptor
.Ar elf
into memory so that the underlying file descriptor can be
safely closed with command
.Dv ELF_C_FDDONE .
.El
.Pp
Argument
.Ar elf
must be an ELF descriptor associated with a file system object
(e.g., an
.Xr ar 1
archive, an ELF file, or other data file).
.Sh IMPLEMENTATION NOTES
Due to use of
.Xr mmap 2
internally, this function is a no-op for ELF objects opened in
.Dv ELF_C_READ
mode.
.Sh RETURN VALUES
Function
.Fn elf_cntl
returns 0 on success, or -1 if an error was detected.
.Sh ERRORS
.Bl -tag -width "[ELF_E_RESOURCE]"
.It Bq Er ELF_E_ARCHIVE
Argument
.Ar elf
is a descriptor for an archive member.
.It Bq Er ELF_E_ARGUMENT
Argument
.Ar elf
was NULL.
.It Bq Er ELF_E_ARGUMENT
Argument
.Ar cmd
was not recognized.
.It Bq Er ELF_E_MODE
An
.Dv ELF_C_FDREAD
operation was requested on an ELF descriptor opened
for writing.
.El
.Sh SEE ALSO
.Xr elf 3 ,
.Xr elf_begin 3 ,
.Xr elf_end 3 ,
.Xr elf_next 3 ,
.Xr elf_update 3 ,
.Xr gelf 3
contrib/elftoolchain/libelf/elf_data.c
#include "_libelf.h"
ELFTC_VCSID("$Id: elf_data.c 3177 2015-03-30 18:19:41Z emaste $");
ELFTC_VCSID("$Id: elf_data.c 3466 2016-05-11 18:35:44Z emaste $");
Elf_Data *
elf_getdata(Elf_Scn *s, Elf_Data *ed)
......
if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST ||
elftype > ELF_T_LAST || (sh_type != SHT_NOBITS &&
sh_offset + sh_size > (uint64_t) e->e_rawsize)) {
(sh_offset > e->e_rawsize || sh_size > e->e_rawsize - sh_offset))) {
LIBELF_SET_ERROR(SECTION, 0);
return (NULL);
}
......
return (NULL);
}
if (sh_type != SHT_NOBITS &&
(sh_offset > e->e_rawsize || sh_size > e->e_rawsize - sh_offset)) {
LIBELF_SET_ERROR(SECTION, 0);
return (NULL);
}
if ((d = _libelf_allocate_data(s)) == NULL)
return (NULL);
contrib/elftoolchain/libelf/elf_end.3
.\" Copyright (c) 2006,2008 Joseph Koshy. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" This software is provided by Joseph Koshy ``as is'' and
.\" any express or implied warranties, including, but not limited to, the
.\" implied warranties of merchantability and fitness for a particular purpose
.\" are disclaimed. in no event shall Joseph Koshy be liable
.\" for any direct, indirect, incidental, special, exemplary, or consequential
.\" damages (including, but not limited to, procurement of substitute goods
.\" or services; loss of use, data, or profits; or business interruption)
.\" however caused and on any theory of liability, whether in contract, strict
.\" liability, or tort (including negligence or otherwise) arising in any way
.\" out of the use of this software, even if advised of the possibility of
.\" such damage.
.\"
.\" $Id: elf_end.3 189 2008-07-20 10:38:08Z jkoshy $
.\"
.Dd June 29, 2006
.Os
.Dt ELF_END 3
.Sh NAME
.Nm elf_end
.Nd release an ELF descriptor
.Sh LIBRARY
.Lb libelf
.Sh SYNOPSIS
.In libelf.h
.Ft int
.Fn elf_end "Elf *elf"
.Sh DESCRIPTION
Function
.Fn elf_end
is used to release the resources associated with an ELF descriptor
pointed to by argument
.Ar elf .
This descriptor must have been allocated by a previous call to
.Xr elf_begin 3
or
.Xr elf_memory 3 .
For programming convenience, a NULL value is permitted for argument
.Ar elf .
.Pp
A call to
.Fn elf_end
decrements the activation count for descriptor
.Ar elf
by one.
The resources associated with the descriptor are only released
with its activation count goes to zero.
.Pp
Once function
.Fn elf_end
returns zero, the ELF descriptor
.Ar elf
will no longer be valid and should not be used further.
.Sh RETURN VALUES
Function
.Fn elf_end
returns the current value of the ELF descriptor
.Ar elf Ap s
activation count, or zero if argument
.Ar elf
was NULL.
.Sh SEE ALSO
.Xr elf 3 ,
.Xr elf_begin 3 ,
.Xr elf_memory 3 ,
.Xr gelf 3
contrib/elftoolchain/libelf/elf_errmsg.3
.\" Copyright (c) 2006,2008 Joseph Koshy. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" This software is provided by Joseph Koshy ``as is'' and
.\" any express or implied warranties, including, but not limited to, the
.\" implied warranties of merchantability and fitness for a particular purpose
.\" are disclaimed. in no event shall Joseph Koshy be liable
.\" for any direct, indirect, incidental, special, exemplary, or consequential
.\" damages (including, but not limited to, procurement of substitute goods
.\" or services; loss of use, data, or profits; or business interruption)
.\" however caused and on any theory of liability, whether in contract, strict
.\" liability, or tort (including negligence or otherwise) arising in any way
.\" out of the use of this software, even if advised of the possibility of
.\" such damage.
.\"
.\" $Id: elf_errmsg.3 189 2008-07-20 10:38:08Z jkoshy $
.\"
.Dd June 11, 2006
.Os
.Dt ELF_ERRMSG 3
.Sh NAME
.Nm elf_errmsg ,
.Nm elf_errno
.Nd ELF library error message handling
.Sh LIBRARY
.Lb libelf
.Sh SYNOPSIS
.In libelf.h
.Ft int
.Fn elf_errno "void"
.Ft "const char *"
.Fn elf_errmsg "int error"
.Sh DESCRIPTION
When an error occurs during an ELF library API call, the library
encodes the error using an error number and stores the error number
internally for retrieval by the application at a later point of time.
Error numbers may contain an OS supplied error code in addition to
an ELF API specific error code.
An error number value of zero indicates no error.
.Pp
Function
.Fn elf_errno
is used to retrieve the last error recorded by the ELF library.
Invoking this function has the side-effect of resetting the
ELF library's recorded error number to zero.
.Pp
The function
.Fn elf_errmsg
returns a null-terminated string with a human readable
description of the error specified in argument
.Ar error .
A zero value for argument
.Ar error
retrieves the most recent error encountered by the ELF
library.
An argument value of -1 behaves identically, except that
it guarantees a non-NULL return from
.Fn elf_errmsg .
.Sh RETURN VALUES
Function
.Fn elf_errno
returns a non-zero value encoding the last error encountered
by the ELF library, or zero if no error was encountered.
.Pp
Function
.Fn elf_errmsg
returns a pointer to library local storage for non-zero values
of argument
.Ar error .
With a zero argument, the function will return a NULL pointer if no
error had been encountered by the library, or will return a pointer to
library local storage containing an appropriate message otherwise.
.Sh EXAMPLES
Clearing the ELF library's recorded error number can be accomplished
by invoking
.Fn elf_errno
and discarding its return value.
.Bd -literal -offset indent
/* clear error */
(void) elf_errno();
.Ed
.Pp
Retrieving a human-readable description of the current error number
can be done with the following snippet:
.Bd -literal -offset indent
int err;
const char *errmsg;
\&...
err = elf_errno();
if (err != 0)
errmsg = elf_errmsg(err);
.Ed
.Sh SEE ALSO
.Xr elf 3 ,
.Xr gelf 3
.Sh BUGS
Function
.Fn elf_errmsg
is not localized.
contrib/elftoolchain/libelf/elf_fill.3
.\" Copyright (c) 2006,2008 Joseph Koshy. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" This software is provided by Joseph Koshy ``as is'' and
.\" any express or implied warranties, including, but not limited to, the
.\" implied warranties of merchantability and fitness for a particular purpose
.\" are disclaimed. in no event shall Joseph Koshy be liable
.\" for any direct, indirect, incidental, special, exemplary, or consequential
.\" damages (including, but not limited to, procurement of substitute goods
.\" or services; loss of use, data, or profits; or business interruption)
.\" however caused and on any theory of liability, whether in contract, strict
.\" liability, or tort (including negligence or otherwise) arising in any way
.\" out of the use of this software, even if advised of the possibility of
.\" such damage.
.\"
.\" $Id: elf_fill.3 189 2008-07-20 10:38:08Z jkoshy $
.\"
.Dd June 11, 2006
.Os
.Dt ELF_FILL 3
.Sh NAME
.Nm elf_fill
.Nd set fill byte for inter-section padding
.Sh LIBRARY
.Lb libelf
.Sh SYNOPSIS
.In libelf.h
.Ft void
.Fn elf_fill "int fill"
.Sh DESCRIPTION
Function
.Fn elf_fill
allows an application to specify a fill value for the padding inserted
between two sections of an ELF file to meet section alignment
constraints.
By default the ELF library uses zero bytes for padding.
.Pp
The ELF library will only pad bytes if the
.Dv ELF_F_LAYOUT
flag is not set for the ELF file.
.Sh SEE ALSO
.Xr elf 3 ,
.Xr elf_flagelf 3 ,
... This diff was truncated because it exceeds the maximum size that can be displayed.
    (1-1/1)