Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add the possibility to build with a bundled libelf via: the WITH_BUNDLE_ELF macro
Baptiste Daroussin committed 13 years ago
commit a8ccf3648cb34640ffa96f1b8c1177a285df0d2a
parent 4d7de58
114 files changed +18531 -2
modified external/Makefile
@@ -1,4 +1,8 @@
SUBDIR=	sqlite \
	libyaml

+
.if defined(WITH_BUNDLED_LIBELF)
+
SUBDIR+= libelf
+
.endif
+

.include <bsd.subdir.mk>
added external/libelf/Makefile
@@ -0,0 +1,76 @@
+
# $FreeBSD: head/lib/libelf/Makefile 238741 2012-07-24 16:03:28Z ache $
+

+
LIB=	elf
+

+
SRCS=	elf_begin.c						\
+
	elf_cntl.c						\
+
	elf_end.c elf_errmsg.c elf_errno.c			\
+
	elf_data.c						\
+
	elf_fill.c						\
+
	elf_flag.c						\
+
	elf_getarhdr.c						\
+
	elf_getarsym.c						\
+
	elf_getbase.c						\
+
	elf_getident.c						\
+
	elf_hash.c						\
+
	elf_kind.c						\
+
	elf_memory.c						\
+
	elf_next.c						\
+
	elf_rand.c						\
+
	elf_rawfile.c						\
+
	elf_phnum.c						\
+
	elf_shnum.c						\
+
	elf_shstrndx.c						\
+
	elf_scn.c						\
+
	elf_strptr.c						\
+
	elf_update.c						\
+
	elf_version.c						\
+
	gelf_cap.c						\
+
	gelf_checksum.c						\
+
	gelf_dyn.c						\
+
	gelf_ehdr.c						\
+
	gelf_getclass.c						\
+
	gelf_fsize.c						\
+
	gelf_move.c						\
+
	gelf_phdr.c						\
+
	gelf_rel.c						\
+
	gelf_rela.c						\
+
	gelf_shdr.c						\
+
	gelf_sym.c						\
+
	gelf_syminfo.c						\
+
	gelf_symshndx.c						\
+
	gelf_xlate.c						\
+
	libelf.c						\
+
	libelf_align.c						\
+
	libelf_allocate.c					\
+
	libelf_ar.c						\
+
	libelf_ar_util.c					\
+
	libelf_checksum.c					\
+
	libelf_data.c						\
+
	libelf_ehdr.c						\
+
	libelf_extended.c					\
+
	libelf_phdr.c						\
+
	libelf_shdr.c						\
+
	libelf_xlate.c						\
+
	${GENSRCS}
+

+
INTERNALLIB=
+
INCS=	libelf.h gelf.h
+

+
GENSRCS=	libelf_fsize.c libelf_msize.c libelf_convert.c
+
CLEANFILES=	${GENSRCS}
+
CLEANDIRS=	sys
+
CFLAGS+=	-I${.CURDIR} -I.
+

+
NO_MAN=	yes
+

+
libelf_convert.c:	elf_types.m4 libelf_convert.m4
+
libelf_fsize.c:		elf_types.m4 libelf_fsize.m4
+
libelf_msize.c:		elf_types.m4 libelf_msize.m4
+

+
.include <bsd.lib.mk>
+

+
# Keep the .SUFFIXES line after the include of bsd.lib.mk
+
.SUFFIXES:	.m4 .c
+
.m4.c:
+
	m4 -D SRCDIR=${.CURDIR} ${.IMPSRC} > ${.TARGET}
added external/libelf/README
@@ -0,0 +1,12 @@
+
# $FreeBSD: releng/9.1/lib/libelf/README 186758 2009-01-05 05:14:26Z jkoshy $
+
# $NetBSD$
+

+
libelf: a BSD-licensed implementation of the ELF(3)/GELF(3) API.
+

+
Documentation:
+
 * Manual page elf.3 contains an overview of the library.  Other
+
   manual pages document individual APIs in the library.
+
 * A tutorial "libelf by Example" is available at:
+
   http://people.freebsd.org/~jkoshy/download/libelf/article.html
+
	
+
For ongoing development please see http://elftoolchain.sourceforge.net/
added external/libelf/Version.map
@@ -0,0 +1,105 @@
+
/*
+
 * $FreeBSD: releng/9.1/lib/libelf/Version.map 210345 2010-07-21 12:14:50Z kaiw $
+
 */
+
FBSD_1.0 {
+
global:
+
	elf32_checksum;
+
	elf32_fsize;
+
	elf32_getehdr;
+
	elf32_getphdr;
+
	elf32_getshdr;
+
	elf32_newehdr;
+
	elf32_newphdr;
+
	elf32_xlatetof;
+
	elf32_xlatetom;
+
	elf64_checksum;
+
	elf64_fsize;
+
	elf64_getehdr;
+
	elf64_getphdr;
+
	elf64_getshdr;
+
	elf64_newehdr;
+
	elf64_newphdr;
+
	elf64_xlatetof;
+
	elf64_xlatetom;
+
	elf_begin;
+
	elf_cntl;
+
	elf_end;
+
	elf_errmsg;
+
	elf_errno;
+
	elf_fill;
+
	elf_flagdata;
+
	elf_flagehdr;
+
	elf_flagelf;
+
	elf_flagphdr;
+
	elf_flagscn;
+
	elf_flagshdr;
+
	elf_getarhdr;
+
	elf_getarsym;
+
	elf_getbase;
+
	elf_getdata;
+
	elf_getident;
+
	elf_getscn;
+
	elf_getphdrnum;
+
	elf_getphnum;
+
	elf_getshdrnum;
+
	elf_getshnum;
+
	elf_getshdrstrndx;
+
	elf_getshstrndx;
+
	elf_hash;
+
	elf_kind;
+
	elf_memory;
+
	elf_ndxscn;
+
	elf_newdata;
+
	elf_newscn;
+
	elf_next;
+
	elf_nextscn;
+
	elf_rand;
+
	elf_rawdata;
+
	elf_rawfile;
+
	elf_setshstrndx;
+
	elf_strptr;
+
	elf_update;
+
	elf_version;
+
	gelf_checksum;
+
	gelf_fsize;
+
	gelf_getcap;
+
	gelf_getclass;
+
	gelf_getdyn;
+
	gelf_getehdr;
+
	gelf_getmove;
+
	gelf_getphdr;
+
	gelf_getrel;
+
	gelf_getrela;
+
	gelf_getshdr;
+
	gelf_getsym;
+
	gelf_getsyminfo;
+
	gelf_getsymshndx;
+
	gelf_newehdr;
+
	gelf_newphdr;
+
	gelf_update_cap;
+
	gelf_update_dyn;
+
	gelf_update_ehdr;
+
	gelf_update_move;
+
	gelf_update_phdr;
+
	gelf_update_rel;
+
	gelf_update_rela;
+
	gelf_update_shdr;
+
	gelf_update_sym;
+
	gelf_update_syminfo;
+
	gelf_update_symshndx;
+
	gelf_xlatetof;
+
	gelf_xlatetom;
+
local:
+
	*;
+
};
+

+
/*
+
 * Private symbols, mostly test hooks
+
 */
+
FBSDprivate_1.0 {
+
global:
+
	_libelf_set_error;
+
	_libelf_get_max_error;
+
	_libelf_get_no_error_message;
+
	_libelf_get_unknown_error_message;
+
};
added external/libelf/_libelf.h
@@ -0,0 +1,198 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 *
+
 * $FreeBSD: releng/9.1/lib/libelf/_libelf.h 210348 2010-07-21 12:54:34Z kaiw $
+
 */
+

+
#ifndef	__LIBELF_H_
+
#define	__LIBELF_H_
+

+
#include <sys/queue.h>
+

+
#ifndef	NULL
+
#define NULL 	((void *) 0)
+
#endif
+

+
/*
+
 * Library-private data structures.
+
 */
+

+
#define LIBELF_MSG_SIZE	256
+

+
struct _libelf_globals {
+
	int		libelf_arch;
+
	unsigned int	libelf_byteorder;
+
	int		libelf_class;
+
	int		libelf_error;
+
	int		libelf_fillchar;
+
	unsigned int	libelf_version;
+
	char		libelf_msg[LIBELF_MSG_SIZE];
+
};
+

+
extern struct _libelf_globals _libelf;
+

+
#define	LIBELF_PRIVATE(N)	(_libelf.libelf_##N)
+

+
#define	LIBELF_ELF_ERROR_MASK	0xFF
+
#define	LIBELF_OS_ERROR_SHIFT	8
+

+
#define	LIBELF_SET_ERROR(E, O) do {					\
+
	LIBELF_PRIVATE(error) = ((ELF_E_##E & LIBELF_ELF_ERROR_MASK)|	\
+
	    ((O) << LIBELF_OS_ERROR_SHIFT));				\
+
	} while (0)
+

+
#define	LIBELF_ADJUST_AR_SIZE(S)	(((S) + 1U) & ~1U)
+

+
/*
+
 * Flags for library internal use.  These use the upper 16 bits of a
+
 * flags field.
+
 */
+
#define	LIBELF_F_MALLOCED	0x010000 /* whether data was malloc'ed */
+
#define	LIBELF_F_MMAP		0x020000 /* whether e_rawfile was mmap'ed */
+
#define	LIBELF_F_SHDRS_LOADED	0x040000 /* whether all shdrs were read in */
+

+
struct _Elf {
+
	int		e_activations;	/* activation count */
+
	Elf_Arhdr	*e_arhdr;	/* header for archive members */
+
	unsigned int	e_byteorder;	/* ELFDATA* */
+
	int		e_class;	/* ELFCLASS*  */
+
	Elf_Cmd		e_cmd;		/* ELF_C_* used at creation time */
+
	int		e_fd;		/* associated file descriptor */
+
	unsigned int	e_flags;	/* ELF_F_*, LIBELF_F_* flags */
+
	Elf_Kind	e_kind;		/* ELF_K_* */
+
	Elf		*e_parent; 	/* non-NULL for archive members */
+
	char	 	*e_rawfile;	/* uninterpreted bytes */
+
	size_t		e_rawsize;	/* size of uninterpreted bytes */
+
	unsigned int	e_version;	/* file version */
+

+
	union {
+
		struct {		/* ar(1) archives */
+
			off_t	e_next;	/* set by elf_rand()/elf_next() */
+
			int	e_nchildren;
+
			char	*e_rawstrtab;	/* file name strings */
+
			size_t	e_rawstrtabsz;
+
			char	*e_rawsymtab;	/* symbol table */
+
			size_t	e_rawsymtabsz;
+
			Elf_Arsym *e_symtab;
+
			size_t	e_symtabsz;
+
		} e_ar;
+
		struct {		/* regular ELF files */
+
			union {
+
				Elf32_Ehdr *e_ehdr32;
+
				Elf64_Ehdr *e_ehdr64;
+
			} e_ehdr;
+
			union {
+
				Elf32_Phdr *e_phdr32;
+
				Elf64_Phdr *e_phdr64;
+
			} e_phdr;
+
			STAILQ_HEAD(, _Elf_Scn)	e_scn;	/* section list */
+
			size_t	e_nphdr;	/* number of Phdr entries */
+
			size_t	e_nscn;		/* number of sections */
+
			size_t	e_strndx;	/* string table section index */
+
		} e_elf;
+
	} e_u;
+
};
+

+
struct _Elf_Scn {
+
	union {
+
		Elf32_Shdr	s_shdr32;
+
		Elf64_Shdr	s_shdr64;
+
	} s_shdr;
+
	STAILQ_HEAD(, _Elf_Data) s_data;	/* list of Elf_Data descriptors */
+
	STAILQ_HEAD(, _Elf_Data) s_rawdata;	/* raw data for this section */
+
	STAILQ_ENTRY(_Elf_Scn) s_next;
+
	struct _Elf	*s_elf;		/* parent ELF descriptor */
+
	unsigned int	s_flags;	/* flags for the section as a whole */
+
	size_t		s_ndx;		/* index# for this section */
+
	uint64_t	s_offset;	/* managed by elf_update() */
+
	uint64_t	s_rawoff;	/* original offset in the file */
+
	uint64_t	s_size;		/* managed by elf_update() */
+
};
+

+

+
enum {
+
	ELF_TOFILE,
+
	ELF_TOMEMORY
+
};
+

+
#define	LIBELF_COPY_U32(DST,SRC,NAME)	do {		\
+
		if ((SRC)->NAME > UINT_MAX) {		\
+
			LIBELF_SET_ERROR(RANGE, 0);	\
+
			return (0);			\
+
		}					\
+
		(DST)->NAME = (SRC)->NAME;		\
+
	} while (0)
+

+
#define	LIBELF_COPY_S32(DST,SRC,NAME)	do {		\
+
		if ((SRC)->NAME > INT_MAX ||		\
+
		    (SRC)->NAME < INT_MIN) {		\
+
			LIBELF_SET_ERROR(RANGE, 0);	\
+
			return (0);			\
+
		}					\
+
		(DST)->NAME = (SRC)->NAME;		\
+
	} while (0)
+

+

+
/*
+
 * Prototypes
+
 */
+

+
Elf_Data *_libelf_allocate_data(Elf_Scn *_s);
+
Elf	*_libelf_allocate_elf(void);
+
Elf_Scn	*_libelf_allocate_scn(Elf *_e, size_t _ndx);
+
Elf_Arhdr *_libelf_ar_gethdr(Elf *_e);
+
Elf	*_libelf_ar_open(Elf *_e);
+
Elf	*_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
+
int	_libelf_ar_get_member(char *_s, size_t _sz, int _base, size_t *_ret);
+
char	*_libelf_ar_get_string(const char *_buf, size_t _sz, int _rawname);
+
char	*_libelf_ar_get_name(char *_buf, size_t _sz, Elf *_e);
+
int	_libelf_ar_get_number(char *_buf, size_t _sz, int _base, size_t *_ret);
+
Elf_Arsym *_libelf_ar_process_symtab(Elf *_ar, size_t *_dst);
+
unsigned long _libelf_checksum(Elf *_e, int _elfclass);
+
void	*_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
+
int	_libelf_falign(Elf_Type _t, int _elfclass);
+
size_t	_libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version,
+
    size_t count);
+
int	(*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass))
+
	    (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap);
+
void	*_libelf_getphdr(Elf *_e, int _elfclass);
+
void	*_libelf_getshdr(Elf_Scn *_scn, int _elfclass);
+
void	_libelf_init_elf(Elf *_e, Elf_Kind _kind);
+
int	_libelf_load_scn(Elf *e, void *ehdr);
+
int	_libelf_malign(Elf_Type _t, int _elfclass);
+
size_t	_libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
+
void	*_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
+
Elf_Data *_libelf_release_data(Elf_Data *_d);
+
Elf	*_libelf_release_elf(Elf *_e);
+
Elf_Scn	*_libelf_release_scn(Elf_Scn *_s);
+
int	_libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum);
+
int	_libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum);
+
int	_libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass,
+
    size_t _shstrndx);
+
Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s,
+
    unsigned int _encoding, int _elfclass, int _direction);
+
int	_libelf_xlate_shtype(uint32_t _sht);
+

+
#endif	/* __LIBELF_H_ */
added external/libelf/elf.3
@@ -0,0 +1,580 @@
+
.\" Copyright (c) 2006,2007 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf.3 236438 2012-06-02 08:47:26Z joel $
+
.\"
+
.Dd October 21, 2007
+
.Dt ELF 3
+
.Os
+
.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
+
.Po
+
.Xr elf32_xlatetof 3 ,
+
.Xr elf32_xlatetom 3 ,
+
.Xr elf64_xlatetof 3
+
and
+
.Xr elf64_xlatetom 3
+
.Pc
+
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 elf_*
+
Used for class-independent functions.
+
.It elf32_*
+
Used for functions working with 32 bit ELF objects.
+
.It elf64_*
+
Used for functions working with 64 bit ELF objects.
+
.It Elf_*
+
Used for class-independent data types.
+
.It ELF_C_*
+
Used for command values used in a few functions.
+
These symbols are defined as members of the
+
.Vt Elf_Cmd
+
enumeration.
+
.It ELF_E_*
+
Used for error numbers.
+
.It ELF_F_*
+
Used for flags.
+
.It 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 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
+
.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 a power of two.
+
.It Vt "uint64_t d_off"
+
The offset with the containing section where this descriptors 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 "void *d_buf"
+
A pointer to data in memory.
+
.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
+
function.
+
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_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_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_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_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.
+
.El
+
.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 2n
+
.It "Archive Access"
+
.Bl -tag -width 17n -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 -width 17n -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 sectino.
+
.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 -width 17n -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 -width 17n -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 -width 17n -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 area.
+
.It Fn elf_version
+
Sets the operating version.
+
.El
+
.It "IO Control"
+
.Bl -tag -width 17n -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 17n -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 area, 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 jkoshy@FreeBSD.org .
added external/libelf/elf_begin.3
@@ -0,0 +1,280 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_begin.3 236626 2012-06-05 18:19:52Z joel $
+
.\"
+
.Dd June 20, 2010
+
.Dt ELF_BEGIN 3
+
.Os
+
.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 argument
+
.Ar fd .
+
It can be used for both
+
.Xr ar 1
+
archives and for regular ELF files.
+
.Pp
+
Argument
+
.Ar fd
+
should have been opened for reading.
+
If argument
+
.Ar elf
+
is NULL, the library will allocate a new ELF descriptor for
+
the file being processed.
+
If argument
+
.Ar elf
+
is not NULL, and references a regular ELF file previously opened with
+
.Fn elf_begin ,
+
then the activation count for
+
.Ar elf
+
is incremented.
+
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.
+
.It Dv ELF_C_RDWR
+
The command is used to prepare an ELF file for reading and writing.
+
It is not supported for 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.
+
Changes to the in-memory image of the ELF file are written back to
+
disk using the
+
.Xr elf_update 3
+
function.
+
.Pp
+
This command is not valid for
+
.Xr ar 1
+
archives.
+
.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
+
An unrecognized value was specified in argument
+
.Ar cmd .
+
.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_IO
+
Function
+
.Fn elf_begin
+
was unable to truncate a file opened for writing using
+
.Dv ELF_C_WRITE .
+
.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 .
+
.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
added external/libelf/elf_begin.c
@@ -0,0 +1,159 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_begin.c 210351 2010-07-21 13:18:57Z kaiw $");
+

+
#include <sys/types.h>
+
#include <sys/errno.h>
+
#include <sys/mman.h>
+
#include <sys/stat.h>
+

+
#include <ar.h>
+
#include <ctype.h>
+
#include <libelf.h>
+
#include <unistd.h>
+

+
#include "_libelf.h"
+

+
static Elf *
+
_libelf_open_object(int fd, Elf_Cmd c)
+
{
+
	Elf *e;
+
	void *m;
+
	struct stat sb;
+

+
	/*
+
	 * 'Raw' files are always mapped with 'PROT_READ'.  At
+
	 * elf_update(3) time for files opened with ELF_C_RDWR the
+
	 * mapping is unmapped, file data is written to using write(2)
+
	 * and then the raw data is immediately mapped back in.
+
	 */
+
	if (fstat(fd, &sb) < 0) {
+
		LIBELF_SET_ERROR(IO, errno);
+
		return (NULL);
+
	}
+

+
	m = NULL;
+
	if ((m = mmap(NULL, (size_t) sb.st_size, PROT_READ, MAP_PRIVATE, fd,
+
	    (off_t) 0)) == MAP_FAILED) {
+
		LIBELF_SET_ERROR(IO, errno);
+
		return (NULL);
+
	}
+

+
	if ((e = elf_memory(m, (size_t) sb.st_size)) == NULL) {
+
		(void) munmap(m, (size_t) sb.st_size);
+
		return (NULL);
+
	}
+

+
	e->e_flags |= LIBELF_F_MMAP;
+
	e->e_fd = fd;
+
	e->e_cmd = c;
+

+
	if (c == ELF_C_RDWR && e->e_kind == ELF_K_AR) {
+
		(void) elf_end(e);
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	return (e);
+
}
+

+
Elf *
+
elf_begin(int fd, Elf_Cmd c, Elf *a)
+
{
+
	Elf *e;
+

+
	e = NULL;
+

+
	if (LIBELF_PRIVATE(version) == EV_NONE) {
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		return (NULL);
+
	}
+

+
	switch (c) {
+
	case ELF_C_NULL:
+
		return (NULL);
+

+
	case ELF_C_WRITE:
+

+
		if (a != NULL) { /* not allowed for ar(1) archives. */
+
			LIBELF_SET_ERROR(ARGUMENT, 0);
+
			return (NULL);
+
		}
+

+
		/*
+
		 * Check writeability of `fd' immediately and fail if
+
		 * not writeable.
+
		 */
+
		if (ftruncate(fd, (off_t) 0) < 0) {
+
			LIBELF_SET_ERROR(IO, errno);
+
			return (NULL);
+
		}
+

+
		if ((e = _libelf_allocate_elf()) != NULL) {
+
			_libelf_init_elf(e, ELF_K_ELF);
+
			e->e_byteorder = LIBELF_PRIVATE(byteorder);
+
			e->e_fd = fd;
+
			e->e_cmd = c;
+
		}
+
		return (e);
+

+
	case ELF_C_RDWR:
+
		if (a != NULL) { /* not allowed for ar(1) archives. */
+
			LIBELF_SET_ERROR(ARGUMENT, 0);
+
			return (NULL);
+
		}
+
		/*FALLTHROUGH*/
+
	case ELF_C_READ:
+
		/*
+
		 * Descriptor `a' could be for a regular ELF file, or
+
		 * for an ar(1) archive.  If descriptor `a' was opened
+
		 * using a valid file descriptor, we need to check if
+
		 * the passed in `fd' value matches the original one.
+
		 */
+
		if (a &&
+
		    ((a->e_fd != -1 && a->e_fd != fd) || c != a->e_cmd)) {
+
			LIBELF_SET_ERROR(ARGUMENT, 0);
+
			return (NULL);
+
		}
+
		break;
+

+
	default:
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+

+
	}
+

+
	if (a == NULL)
+
		e = _libelf_open_object(fd, c);
+
	else if (a->e_kind == ELF_K_AR)
+
		e = _libelf_ar_open_member(a->e_fd, c, a);
+
	else
+
		(e = a)->e_activations++;
+

+
	return (e);
+
}
added external/libelf/elf_cntl.3
@@ -0,0 +1,111 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_cntl.3 219210 2011-03-02 21:59:53Z uqs $
+
.\"
+
.Dd August 9, 2006
+
.Dt ELF_CNTL 3
+
.Os
+
.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
added external/libelf/elf_cntl.c
@@ -0,0 +1,59 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_cntl.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
int
+
elf_cntl(Elf *e, Elf_Cmd c)
+
{
+
	if (e == NULL ||
+
	    (c != ELF_C_FDDONE && c != ELF_C_FDREAD)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (-1);
+
	}
+

+
	if (e->e_parent) {
+
		LIBELF_SET_ERROR(ARCHIVE, 0);
+
		return (-1);
+
	}
+

+
	if (c == ELF_C_FDREAD) {
+
		if (e->e_cmd == ELF_C_WRITE) {
+
			LIBELF_SET_ERROR(MODE, 0);
+
			return (-1);
+
		}
+
		else
+
			return (0);
+
	}
+

+
	e->e_fd = -1;
+
	return 0;
+
}
added external/libelf/elf_data.c
@@ -0,0 +1,239 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_data.c 221595 2011-05-07 11:04:36Z kaiw $");
+

+
#include <assert.h>
+
#include <errno.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+

+
#include "_libelf.h"
+

+

+
Elf_Data *
+
elf_getdata(Elf_Scn *s, Elf_Data *d)
+
{
+
	Elf *e;
+
	size_t fsz, msz, count;
+
	int elfclass, elftype;
+
	unsigned int sh_type;
+
	uint64_t sh_align, sh_offset, sh_size;
+
	int (*xlate)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
+

+
	if (s == NULL || (e = s->s_elf) == NULL || e->e_kind != ELF_K_ELF ||
+
	    (d != NULL && s != d->d_scn)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL)
+
		return (d);
+

+
	if (d != NULL)
+
		return (STAILQ_NEXT(d, d_next));
+

+
	if (e->e_rawfile == NULL) {
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		return (NULL);
+
	}
+

+
	elfclass = e->e_class;
+

+
	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
+

+
	if (elfclass == ELFCLASS32) {
+
		sh_type   = s->s_shdr.s_shdr32.sh_type;
+
		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
+
		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
+
		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
+
	} else {
+
		sh_type   = s->s_shdr.s_shdr64.sh_type;
+
		sh_offset = s->s_shdr.s_shdr64.sh_offset;
+
		sh_size   = s->s_shdr.s_shdr64.sh_size;
+
		sh_align  = s->s_shdr.s_shdr64.sh_addralign;
+
	}
+

+
	if (sh_type == SHT_NULL)
+
		return (NULL);
+

+
	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)) {
+
		LIBELF_SET_ERROR(SECTION, 0);
+
		return (NULL);
+
	}
+

+
	if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
+
            (elftype, (size_t) 1, e->e_version)) == 0) {
+
		LIBELF_SET_ERROR(UNIMPL, 0);
+
		return (NULL);
+
	}
+

+
	if (sh_size % fsz) {
+
		LIBELF_SET_ERROR(SECTION, 0);
+
		return (NULL);
+
	}
+

+
	count = sh_size / fsz;
+

+
	msz = _libelf_msize(elftype, elfclass, e->e_version);
+

+
	assert(msz > 0);
+

+
	if ((d = _libelf_allocate_data(s)) == NULL)
+
		return (NULL);
+

+
	d->d_buf     = NULL;
+
	d->d_off     = 0;
+
	d->d_align   = sh_align;
+
	d->d_size    = msz * count;
+
	d->d_type    = elftype;
+
	d->d_version = e->e_version;
+

+
	if (sh_type == SHT_NOBITS || sh_size == 0) {
+
	        STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
+
		return (d);
+
        }
+

+
	if ((d->d_buf = malloc(msz*count)) == NULL) {
+
		(void) _libelf_release_data(d);
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	d->d_flags  |= LIBELF_F_MALLOCED;
+

+
	xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
+
	if (!(*xlate)(d->d_buf, d->d_size, e->e_rawfile + sh_offset, count,
+
	    e->e_byteorder != LIBELF_PRIVATE(byteorder))) {
+
		_libelf_release_data(d);
+
		LIBELF_SET_ERROR(DATA, 0);
+
		return (NULL);
+
	}
+

+
	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
+

+
	return (d);
+
}
+

+
Elf_Data *
+
elf_newdata(Elf_Scn *s)
+
{
+
	Elf *e;
+
	Elf_Data *d;
+

+
	if (s == NULL || (e = s->s_elf) == NULL ||
+
	    e->e_kind != ELF_K_ELF) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	/*
+
	 * elf_newdata() has to append a data descriptor, so
+
	 * bring in existing section data if not already present.
+
	 */
+
	if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data))
+
		if (elf_getdata(s, NULL) == NULL)
+
			return (NULL);
+

+
	if ((d = _libelf_allocate_data(s)) == NULL)
+
		return (NULL);
+

+
	STAILQ_INSERT_TAIL(&s->s_data, d, d_next);
+

+
	d->d_align = 1;
+
	d->d_buf = NULL;
+
	d->d_off = (uint64_t) ~0;
+
	d->d_size = 0;
+
	d->d_type = ELF_T_BYTE;
+
	d->d_version = LIBELF_PRIVATE(version);
+

+
	(void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY);
+

+
	return (d);
+
}
+

+
/*
+
 * Retrieve a data descriptor for raw (untranslated) data for section
+
 * `s'.
+
 */
+

+
Elf_Data *
+
elf_rawdata(Elf_Scn *s, Elf_Data *d)
+
{
+
	Elf *e;
+
	int elf_class;
+
	uint32_t sh_type;
+
	uint64_t sh_align, sh_offset, sh_size;
+

+
	if (s == NULL || (e = s->s_elf) == NULL ||
+
	    e->e_kind != ELF_K_ELF || e->e_rawfile == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL)
+
		return (d);
+

+
	if (d != NULL)
+
		return (STAILQ_NEXT(d, d_next));
+

+
	elf_class = e->e_class;
+

+
	assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64);
+

+
	if (elf_class == ELFCLASS32) {
+
		sh_type   = s->s_shdr.s_shdr32.sh_type;
+
		sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
+
		sh_size   = (uint64_t) s->s_shdr.s_shdr32.sh_size;
+
		sh_align  = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
+
	} else {
+
		sh_type   = s->s_shdr.s_shdr64.sh_type;
+
		sh_offset = s->s_shdr.s_shdr64.sh_offset;
+
		sh_size   = s->s_shdr.s_shdr64.sh_size;
+
		sh_align  = s->s_shdr.s_shdr64.sh_addralign;
+
	}
+

+
	if (sh_type == SHT_NULL)
+
		return (NULL);
+

+
	if ((d = _libelf_allocate_data(s)) == NULL)
+
		return (NULL);
+

+
	d->d_buf     = (sh_type == SHT_NOBITS || sh_size == 0) ? NULL :
+
	    e->e_rawfile + sh_offset;
+
	d->d_off     = 0;
+
	d->d_align   = sh_align;
+
	d->d_size    = sh_size;
+
	d->d_type    = ELF_T_BYTE;
+
	d->d_version = e->e_version;
+

+
	STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next);
+

+
	return (d);
+
}
added external/libelf/elf_end.3
@@ -0,0 +1,76 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_end.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd June 29, 2006
+
.Dt ELF_END 3
+
.Os
+
.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
added external/libelf/elf_end.c
@@ -0,0 +1,88 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_end.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <sys/mman.h>
+

+
#include <assert.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+

+
#include "_libelf.h"
+

+
int
+
elf_end(Elf *e)
+
{
+
	Elf *sv;
+
	Elf_Scn *scn, *tscn;
+

+
	if (e == NULL || e->e_activations == 0)
+
		return (0);
+

+
	if (--e->e_activations > 0)
+
		return (e->e_activations);
+

+
	assert(e->e_activations == 0);
+

+
	while (e && e->e_activations == 0) {
+
		switch (e->e_kind) {
+
		case ELF_K_AR:
+
			/*
+
			 * If we still have open child descriptors, we
+
			 * need to defer reclaiming resources till all
+
			 * the child descriptors for the archive are
+
			 * closed.
+
			 */
+
			if (e->e_u.e_ar.e_nchildren > 0)
+
				return (0);
+
			break;
+
		case ELF_K_ELF:
+
			/*
+
			 * Reclaim all section descriptors.
+
			 */
+
			STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn)
+
 				scn = _libelf_release_scn(scn);
+
			break;
+
		case ELF_K_NUM:
+
			assert(0);
+
		default:
+
			break;
+
		}
+

+
		if (e->e_flags & LIBELF_F_MMAP)
+
			(void) munmap(e->e_rawfile, e->e_rawsize);
+

+
		sv = e;
+
		if ((e = e->e_parent) != NULL)
+
			e->e_u.e_ar.e_nchildren--;
+
		sv = _libelf_release_elf(sv);
+
	}
+

+
	return (0);
+
}
+

added external/libelf/elf_errmsg.3
@@ -0,0 +1,107 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_errmsg.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd June 11, 2006
+
.Dt ELF_ERRMSG 3
+
.Os
+
.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.
added external/libelf/elf_errmsg.c
@@ -0,0 +1,100 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: head/lib/libelf/elf_errmsg.c 241720 2012-10-19 05:43:38Z ed $");
+

+
#include <libelf.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Retrieve a human readable translation for an error message.
+
 */
+

+
static const char *_libelf_errors[] = {
+
#define	DEFINE_ERROR(N,S)	[ELF_E_##N] = S
+
	DEFINE_ERROR(NONE,	"No Error"),
+
	DEFINE_ERROR(ARCHIVE,	"Malformed ar(1) archive"),
+
	DEFINE_ERROR(ARGUMENT,	"Invalid argument"),
+
	DEFINE_ERROR(CLASS,	"ELF class mismatch"),
+
	DEFINE_ERROR(DATA,	"Invalid data buffer descriptor"),
+
	DEFINE_ERROR(HEADER,	"Missing or malformed ELF header"),
+
	DEFINE_ERROR(IO,	"I/O error"),
+
	DEFINE_ERROR(LAYOUT,	"Layout constraint violation"),
+
	DEFINE_ERROR(MODE,	"Incorrect ELF descriptor mode"),
+
	DEFINE_ERROR(RANGE,	"Value out of range of target"),
+
	DEFINE_ERROR(RESOURCE,	"Resource exhaustion"),
+
	DEFINE_ERROR(SECTION,	"Invalid section descriptor"),
+
	DEFINE_ERROR(SEQUENCE,	"API calls out of sequence"),
+
	DEFINE_ERROR(UNIMPL,	"Unimplemented feature"),
+
	DEFINE_ERROR(VERSION,	"Unknown ELF API version"),
+
	DEFINE_ERROR(NUM,	"Unknown error")
+
#undef	DEFINE_ERROR
+
};
+

+
const char *
+
elf_errmsg(int error)
+
{
+
	int oserr;
+

+
	if (error == 0 && (error = LIBELF_PRIVATE(error)) == 0)
+
	    return NULL;
+
	else if (error == -1)
+
	    error = LIBELF_PRIVATE(error);
+

+
	oserr = error >> LIBELF_OS_ERROR_SHIFT;
+
	error &= LIBELF_ELF_ERROR_MASK;
+

+
	if (error < 0 || error >= ELF_E_NUM)
+
		return _libelf_errors[ELF_E_NUM];
+
	if (oserr) {
+
		strlcpy(LIBELF_PRIVATE(msg), _libelf_errors[error],
+
		    sizeof(LIBELF_PRIVATE(msg)));
+
		strlcat(LIBELF_PRIVATE(msg), ": ", sizeof(LIBELF_PRIVATE(msg)));
+
		strlcat(LIBELF_PRIVATE(msg), strerror(oserr),
+
		    sizeof(LIBELF_PRIVATE(msg)));
+
		return (const char *)&LIBELF_PRIVATE(msg);
+
	}
+
	return _libelf_errors[error];
+
}
+

+
#if	defined(LIBELF_TEST_HOOKS)
+

+
const char *
+
_libelf_get_unknown_error_message(void)
+
{
+
	return _libelf_errors[ELF_E_NUM];
+
}
+

+
const char *
+
_libelf_get_no_error_message(void)
+
{
+
	return _libelf_errors[0];
+
}
+

+
#endif	/* LIBELF_TEST_HOOKS */
added external/libelf/elf_errno.c
@@ -0,0 +1,58 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_errno.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
int
+
elf_errno(void)
+
{
+
	int old;
+

+
	old = LIBELF_PRIVATE(error);
+
	LIBELF_PRIVATE(error) = 0;
+
	return (old & LIBELF_ELF_ERROR_MASK);
+
}
+

+
#if	defined(LIBELF_TEST_HOOKS)
+

+
int
+
_libelf_get_max_error(void)
+
{
+
	return ELF_E_NUM;
+
}
+

+
void
+
_libelf_set_error(int error)
+
{
+
	LIBELF_PRIVATE(error) = error;
+
}
+

+
#endif	/* LIBELF_TEST_HOOKS */
added external/libelf/elf_fill.3
@@ -0,0 +1,52 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_fill.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd June 11, 2006
+
.Dt ELF_FILL 3
+
.Os
+
.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 ,
+
.Xr gelf 3
added external/libelf/elf_fill.c
@@ -0,0 +1,38 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_fill.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
void
+
elf_fill(int fill)
+
{
+
	LIBELF_PRIVATE(fillchar) = fill;
+
}
added external/libelf/elf_flag.c
@@ -0,0 +1,164 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_flag.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
unsigned int
+
elf_flagdata(Elf_Data *d, Elf_Cmd c, unsigned int flags)
+
{
+
	Elf *e;
+
	Elf_Scn *scn;
+
	unsigned int r;
+

+
	if (d == NULL)
+
		return (0);
+

+
	if ((c != ELF_C_SET && c != ELF_C_CLR) || (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL || e->e_kind != ELF_K_ELF ||
+
	    (flags & ~ELF_F_DIRTY) != 0) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (c == ELF_C_SET)
+
	    r = scn->s_flags |= flags;
+
	else
+
	    r = scn->s_flags &= ~flags;
+

+
	return (r);
+
}
+

+
unsigned int
+
elf_flagehdr(Elf *e, Elf_Cmd c, unsigned int flags)
+
{
+
	int ec;
+
	void *ehdr;
+

+
	if (e == NULL)
+
		return (0);
+

+
	if ((c != ELF_C_SET && c != ELF_C_CLR) ||
+
	    (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32)
+
		ehdr = e->e_u.e_elf.e_ehdr.e_ehdr32;
+
	else
+
		ehdr = e->e_u.e_elf.e_ehdr.e_ehdr64;
+

+
	if (ehdr == NULL) {
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		return (0);
+
	}
+

+
	return (elf_flagelf(e, c, flags));
+
}
+

+
unsigned int
+
elf_flagelf(Elf *e, Elf_Cmd c, unsigned int flags)
+
{
+
	int r;
+

+
	if (e == NULL)
+
		return (0);
+

+
	if ((c != ELF_C_SET && c != ELF_C_CLR) ||
+
	    (e->e_kind != ELF_K_ELF) ||
+
	    (flags & ~(ELF_F_DIRTY|ELF_F_LAYOUT)) != 0) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (c == ELF_C_SET)
+
		r = e->e_flags |= flags;
+
	else
+
		r = e->e_flags &= ~flags;
+
	return (r);
+
}
+

+
unsigned int
+
elf_flagphdr(Elf *e, Elf_Cmd c, unsigned int flags)
+
{
+
	int ec;
+
	void *phdr;
+

+
	if (e == NULL)
+
		return (0);
+

+
	if ((c != ELF_C_SET && c != ELF_C_CLR) ||
+
	    (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32)
+
		phdr = e->e_u.e_elf.e_phdr.e_phdr32;
+
	else
+
		phdr = e->e_u.e_elf.e_phdr.e_phdr64;
+

+
	if (phdr == NULL) {
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		return (0);
+
	}
+

+
	return (elf_flagelf(e, c, flags));
+
}
+

+
unsigned int
+
elf_flagscn(Elf_Scn *s, Elf_Cmd c, unsigned int flags)
+
{
+
	int r;
+

+
	if (s == NULL)
+
		return (0);
+

+
	if ((c != ELF_C_SET && c != ELF_C_CLR) ||
+
	    (flags & ~ELF_F_DIRTY) != 0) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (c == ELF_C_SET)
+
		r = s->s_flags |= flags;
+
	else
+
		r = s->s_flags &= ~flags;
+
	return (r);
+
}
+

+
unsigned int
+
elf_flagshdr(Elf_Scn *s, Elf_Cmd c, unsigned int flags)
+
{
+
	return (elf_flagscn(s, c, flags));
+
}
added external/libelf/elf_flagdata.3
@@ -0,0 +1,159 @@
+
.\" Copyright (c) 2006,2007 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_flagdata.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd October 22, 2007
+
.Dt ELF_FLAGDATA 3
+
.Os
+
.Sh NAME
+
.Nm elf_flagdata ,
+
.Nm elf_flagehdr ,
+
.Nm elf_flagelf ,
+
.Nm elf_flagphdr ,
+
.Nm elf_flagscn ,
+
.Nm elf_flagshdr
+
.Nd manipulate flags associated with ELF(3) data structures
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "unsigned int"
+
.Fn elf_flagdata "Elf_Data *data" "Elf_Cmd cmd" "unsigned int flags"
+
.Ft "unsigned int"
+
.Fn elf_flagehdr "Elf *elf" "Elf_Cmd cmd" "unsigned int flags"
+
.Ft "unsigned int"
+
.Fn elf_flagelf "Elf *elf" "Elf_Cmd cmd" "unsigned int flags"
+
.Ft "unsigned int"
+
.Fn elf_flagphdr "Elf *elf" "Elf_Cmd cmd" "unsigned int flags"
+
.Ft "unsigned int"
+
.Fn elf_flagscn "Elf_Scn *scn" "Elf_Cmd cmd" "unsigned int flags"
+
.Ft "unsigned int"
+
.Fn elf_flagshdr "Elf_Scn *scn" "Elf_Cmd cmd" "unsigned int flags"
+
.Sh DESCRIPTION
+
These functions are used to query, set or reset flags on data
+
structures associated with an ELF file.
+
.Pp
+
Arguments
+
.Ar data ,
+
.Ar elf
+
and
+
.Ar scn
+
denote the data structures whose flags need to be changed.
+
These values are allowed to be NULL to simplify error handling in
+
application code.
+
.Pp
+
Argument
+
.Ar cmd
+
may have the following values:
+
.Bl -tag -width ELF_C_SET
+
.It Dv ELF_C_CLR
+
The argument
+
.Ar flags
+
specifies the flags to be cleared.
+
.It Dv ELF_C_SET
+
The argument
+
.Ar flags
+
specifies the flags to be set.
+
.El
+
.Pp
+
The argument
+
.Ar flags
+
is allowed to have the following flags set:
+
.Bl -tag -width ELF_F_LAYOUT
+
.It Dv ELF_F_DIRTY
+
Mark the associated data structure as needing to be written back
+
to the underlying file.
+
A subsequent call to
+
.Xr elf_update 3
+
will resynchronize the library's internal data structures.
+
.It Dv ELF_F_LAYOUT
+
This flag is only valid with the
+
.Fn elf_flagelf
+
API.
+
It informs the library that the application will take
+
responsibility for the layout of the file and that the library is
+
not to insert any padding in between sections.
+
.El
+
.Pp
+
Marking a given data structure as
+
.Dq dirty
+
affects all of its contained elements.
+
Thus marking an ELF descriptor
+
.Ar elf
+
with
+
.Fn elf_flagelf "elf" "ELF_C_SET" "ELF_F_DIRTY"
+
means that the entire contents of the descriptor are
+
.Dq dirty .
+
.Pp
+
Using a value of zero for argument
+
.Ar flags
+
will return the current set of flags for the data structure being
+
queried.
+
.Sh RETURN VALUES
+
These functions return the updated flags is successful, and zero if
+
an error is detected.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
An unsupported value was used for the
+
.Ar cmd
+
argument.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar flags
+
had unsupported flags set.
+
.It Bq Er ELF_E_ARGUMENT
+
The argument
+
.Ar elf
+
was not a descriptor for an ELF object.
+
.It Bq Er ELF_E_SEQUENCE
+
Function
+
.Fn elf_flagehdr
+
was called without an executable header being allocated.
+
.It Bq Er ELF_E_SEQUENCE
+
Function
+
.Fn elf_flagphdr
+
was called without a program header being allocated.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_newehdr 3 ,
+
.Xr elf32_newphdr 3 ,
+
.Xr elf32_newshdr 3 ,
+
.Xr elf64_newehdr 3 ,
+
.Xr elf64_newphdr 3 ,
+
.Xr elf64_newshdr 3 ,
+
.Xr elf_newdata 3 ,
+
.Xr elf_update 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_newehdr 3 ,
+
.Xr gelf_newphdr 3 ,
+
.Xr gelf_newshdr 3 ,
+
.Xr gelf_update_dyn 3 ,
+
.Xr gelf_update_move 3 ,
+
.Xr gelf_update_rel 3 ,
+
.Xr gelf_update_rela 3 ,
+
.Xr gelf_update_sym 3 ,
+
.Xr gelf_update_syminfo 3
added external/libelf/elf_getarhdr.3
@@ -0,0 +1,97 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_getarhdr.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 15, 2006
+
.Dt ELF_GETARHDR 3
+
.Os
+
.Sh NAME
+
.Nm elf_getarhdr
+
.Nd retrieve ar(1) header for an archive member
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf_Arhdr *"
+
.Fn elf_getarhdr "Elf *elf"
+
.Sh DESCRIPTION
+
The
+
.Fn elf_getarhdr
+
function returns a pointer to an archive member header for
+
a descriptor
+
.Ar elf .
+
This descriptor must have been returned by a prior call to
+
.Xr elf_begin 3 ,
+
and must be a descriptor for a member inside an
+
.Xr ar 1
+
archive.
+
.Pp
+
Structure
+
.Vt Elf_Arhdr
+
includes the following members:
+
.Bl -tag -width indent
+
.It Vt "char *" Va ar_name
+
A pointer to a null terminated string containing the translated
+
name of the archive member.
+
.It Vt "char *" Va ar_rawname
+
A pointer to a null terminated string containing the untranslated
+
name for the archive member, including all
+
.Xr ar 1
+
formatting characters and trailing white space.
+
.It Vt time_t Va ar_date
+
The timestamp associated with the member.
+
.It Vt uid_t Va ar_uid
+
The uid of the creator of the member.
+
.It Vt gid_t Va ar_gid
+
The gid of the creator of the member.
+
.It Vt mode_t Va ar_mode
+
The file mode of the member.
+
.It Vt size_t Va ar_size
+
The size of the member in bytes.
+
.El
+
.Sh RETURN VALUES
+
This function returns a valid pointer to an
+
.Vt Elf_Arhdr
+
structure if successful, or NULL if an error is encountered.
+
.Sh ERRORS
+
Function
+
.Fn elf_getarhdr
+
may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for a member of an
+
.Xr ar 1
+
archive.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_begin 3 ,
+
.Xr elf_getarsym 3 ,
+
.Xr elf_memory 3
added external/libelf/elf_getarhdr.c
@@ -0,0 +1,48 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_getarhdr.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
Elf_Arhdr *
+
elf_getarhdr(Elf *e)
+
{
+
	Elf_Arhdr *arh;
+

+
	if (e == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if ((arh = e->e_arhdr) != NULL)
+
		return (arh);
+

+
	return (_libelf_ar_gethdr(e));
+
}
added external/libelf/elf_getarsym.3
@@ -0,0 +1,130 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_getarsym.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 15, 2006
+
.Dt ELF_GETARSYM 3
+
.Os
+
.Sh NAME
+
.Nm elf_getarsym
+
.Nd retrieve the symbol table of an archive
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf_Arsym *"
+
.Fn elf_getarsym "Elf *elf" "size_t *ptr"
+
.Sh DESCRIPTION
+
The function
+
.Fn elf_getarsym
+
retrieves the symbol table for an
+
.Xr ar 1
+
archive, if one is available.
+
.Pp
+
Argument
+
.Ar elf
+
should be a descriptor for an
+
.Xr ar 1
+
archive opened using
+
.Fn elf_begin
+
or
+
.Fn elf_memory .
+
.Pp
+
If the archive
+
.Ar elf
+
contains a symbol table with n entries, this function returns a
+
pointer to an array of n+1
+
.Vt Elf_Arsym
+
structures.
+
An
+
.Vt Elf_Arsym
+
structure has the following elements:
+
.Bl -tag -width indent -compact
+
.It Vt "char *" Va as_name
+
This structure member is a pointer to a null-terminated symbol name.
+
.It Vt "off_t" Va as_off
+
This structure member contains the byte offset from the beginning of the archive to
+
the header for the archive member.
+
This value is suitable for use with
+
.Xr elf_rand 3 .
+
.It Vt "unsigned long" Va as_hash
+
This structure member contains a portable hash value for the symbol
+
name, as computed by
+
.Xr elf_hash 3 .
+
.El
+
.Pp
+
The last entry of the returned array will have a NULL value for member
+
.Va as_name ,
+
a zero value for member
+
.Va as_off
+
and an illegal value of ~0UL for
+
.Va as_hash .
+
.Pp
+
If argument
+
.Ar ptr
+
is non-null, the
+
.Fn elf_getarsym
+
function will store the number of table entries returned (including the
+
sentinel entry at the end) into the location it points to.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_getarsym
+
returns a pointer to an array of
+
.Vt Elf_Arsym
+
structures if successful, or a NULL
+
pointer if an error was encountered.
+
.Pp
+
If argument
+
.Ar ptr
+
is non-null and there was no error, the library will store the
+
number of archive symbol entries returned into the location it
+
points to.
+
If argument
+
.Ar ptr
+
is non-null and an error was encountered, the library will
+
set the location pointed to by it to zero.
+
.Sh ERRORS
+
Function
+
.Fn elf_getarsym
+
may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for an
+
.Xr ar 1
+
archive.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_begin 3 ,
+
.Xr elf_getarhdr 3 ,
+
.Xr elf_hash 3 ,
+
.Xr elf_memory 3 ,
+
.Xr elf_next 3 ,
+
.Xr elf_rand 3
added external/libelf/elf_getarsym.c
@@ -0,0 +1,56 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_getarsym.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
Elf_Arsym *
+
elf_getarsym(Elf *ar, size_t *ptr)
+
{
+
	size_t n;
+
	Elf_Arsym *symtab;
+

+
	n = 0;
+
	symtab = NULL;
+

+
	if (ar == NULL || ar->e_kind != ELF_K_AR)
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
	else if ((symtab = ar->e_u.e_ar.e_symtab) != NULL)
+
		n = ar->e_u.e_ar.e_symtabsz;
+
	else if (ar->e_u.e_ar.e_rawsymtab)
+
		symtab = _libelf_ar_process_symtab(ar, &n);
+
	else
+
		LIBELF_SET_ERROR(ARCHIVE, 0);
+

+
	if (ptr)
+
		*ptr = n;
+
	return (symtab);
+
}
+

added external/libelf/elf_getbase.3
@@ -0,0 +1,71 @@
+
.\" Copyright (c) 2006,2008,2010 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_getbase.3 209122 2010-06-13 10:58:50Z kaiw $
+
.\"
+
.Dd June 6, 2010
+
.Dt ELF_GETBASE 3
+
.Os
+
.Sh NAME
+
.Nm elf_getbase
+
.Nd get the base offset for an object file
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft off_t
+
.Fn elf_getbase "Elf *elf"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_getbase
+
returns the file offset to the first byte of the object referenced by ELF
+
descriptor
+
.Ar elf .
+
.Pp
+
For descriptors referencing members of archives, the returned offset is
+
the file offset of the member in its containing archive.
+
For descriptors to regular objects, the returned offset is (vacuously)
+
zero.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_getbase
+
returns a valid file offset if successful, or
+
.Pq Vt off_t
+
.Li -1
+
in case of an error.
+
.Sh ERRORS
+
Function
+
.Fn elf_getbase
+
may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getarhdr 3 ,
+
.Xr elf_getident 3 ,
+
.Xr elf_rawfile 3 ,
+
.Xr gelf 3
added external/libelf/elf_getbase.c
@@ -0,0 +1,47 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_getbase.c 209122 2010-06-13 10:58:50Z kaiw $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
off_t
+
elf_getbase(Elf *e)
+
{
+
	if (e == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return ((off_t) -1);
+
	}
+

+
	if (e->e_parent == NULL)
+
		return ((off_t) 0);
+

+
	return ((off_t) ((uintptr_t) e->e_rawfile -
+
	    (uintptr_t) e->e_parent->e_rawfile));
+
}
added external/libelf/elf_getdata.3
@@ -0,0 +1,200 @@
+
.\" Copyright (c) 2006,2008,2010-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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_getdata.3 235286 2012-05-11 20:06:46Z gjb $
+
.\"
+
.Dd January 26, 2011
+
.Dt ELF_GETDATA 3
+
.Os
+
.Sh NAME
+
.Nm elf_getdata ,
+
.Nm elf_newdata ,
+
.Nm elf_rawdata
+
.Nd iterate through or allocate section data
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf_Data *"
+
.Fn elf_getdata "Elf_Scn *scn" "Elf_Data *data"
+
.Ft "Elf_Data *"
+
.Fn elf_newdata "Elf_Scn *scn"
+
.Ft "Elf_Data *"
+
.Fn elf_rawdata "Elf_Scn *scn" "Elf_Data *data"
+
.Sh DESCRIPTION
+
These functions are used to access and manipulate data descriptors
+
associated with section descriptors.
+
Data descriptors used by the ELF library are described in
+
.Xr elf 3 .
+
.Pp
+
Function
+
.Fn elf_getdata
+
will return the next data descriptor associated with section descriptor
+
.Ar scn .
+
The returned data descriptor will be setup to contain translated data.
+
Argument
+
.Ar data
+
may be NULL, in which case the function returns the first data descriptor
+
associated with section
+
.Ar scn .
+
If argument
+
.Ar data
+
is not NULL, it must be a pointer to a data descriptor associated with
+
section descriptor
+
.Ar scn ,
+
and function
+
.Fn elf_getdata
+
will return a pointer to the next data descriptor for the section,
+
or NULL when the end of the section's descriptor list is reached.
+
.Pp
+
Function
+
.Fn elf_newdata
+
will allocate a new data descriptor and append it to the list of data
+
descriptors associated with section descriptor
+
.Ar scn .
+
The new data descriptor will be initialized as follows:
+
.Bl -tag -width "d_version" -compact -offset indent
+
.It Va d_align
+
Set to 1.
+
.It Va d_buf
+
Initialized to NULL.
+
.It Va d_off
+
Set to (off_t) -1.
+
This field is under application control if the
+
.Dv ELF_F_LAYOUT
+
flag was set on the ELF descriptor.
+
.It Va d_size
+
Set to zero.
+
.It Va d_type
+
Initialized to
+
.Dv ELF_T_BYTE .
+
.It Va d_version
+
Set to the current working version of the library, as set by
+
.Xr elf_version 3 .
+
.El
+
The application must set these values as appropriate before
+
calling
+
.Xr elf_update 3 .
+
Section
+
.Ar scn
+
must be associated with an ELF file opened for writing.
+
If the application has not requested full control of layout by
+
setting the
+
.Dv ELF_F_LAYOUT
+
flag on descriptor
+
.Ar elf ,
+
then the data referenced by the returned descriptor will be positioned
+
after the existing content of the section, honoring the file alignment
+
specified in member
+
.Va d_align .
+
On successful completion of a call to
+
.Fn elf_newdata ,
+
the ELF library will mark the section
+
.Ar scn
+
as
+
.Dq dirty .
+
.Pp
+
Function
+
.Fn elf_rawdata
+
is used to step through the data descriptors associated with
+
section
+
.Ar scn .
+
In contrast to function
+
.Fn elf_getdata ,
+
this function returns untranslated data.
+
If argument
+
.Ar data
+
is NULL, the first data descriptor associated with section
+
.Ar scn
+
is returned.
+
If argument
+
.Ar data
+
is not NULL, is must be a data descriptor associated with
+
section
+
.Ar scn ,
+
and function
+
.Fn elf_rawdata
+
will return the next data descriptor in the list, or NULL
+
if no further descriptors are present.
+
Function
+
.Fn elf_rawdata
+
always returns
+
.Vt Elf_Data
+
structures of type
+
.Dv ELF_T_BYTE .
+
.Ss Special handling of zero-sized and SHT_NOBITS sections
+
For sections of type
+
.Dv SHT_NOBITS ,
+
and for zero-sized sections,
+
the functions
+
.Fn elf_getdata
+
and
+
.Fn elf_rawdata
+
return a pointer to a valid
+
.Vt Elf_Data
+
structure that has its
+
.Va d_buf
+
member set to NULL and its
+
.Va d_size
+
member set to the size of the section.
+
.Pp
+
If an application wishes to create a section of type
+
.Dv SHT_NOBITS ,
+
it should add a data buffer to the section using function
+
.Fn elf_newdata .
+
It should then set the
+
.Va d_buf
+
and
+
.Va d_size
+
members of the returned
+
.Vt Elf_Data
+
structure to NULL and the desired size of the section respectively.
+
.Sh RETURN VALUES
+
These functions return a valid pointer to a data descriptor if successful, or
+
NULL if an error occurs.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar scn
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar data
+
was not associated with section descriptor
+
.Ar scn .
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_flagdata 3 ,
+
.Xr elf_flagscn 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr elf_getshdr 3 ,
+
.Xr elf_newscn 3 ,
+
.Xr elf_rawfile 3 ,
+
.Xr elf_update 3 ,
+
.Xr elf_version 3 ,
+
.Xr gelf 3
added external/libelf/elf_getident.3
@@ -0,0 +1,83 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_getident.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd July 3, 2006
+
.Dt ELF_GETIDENT 3
+
.Os
+
.Sh NAME
+
.Nm elf_getident
+
.Nd return the initial bytes of a file
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft char *
+
.Fn elf_getident "Elf *elf" "size_t *sz"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_getident
+
returns a pointer to the initial bytes of the file for descriptor
+
.Ar elf .
+
.Pp
+
If argument
+
.Ar sz
+
is non-null, the size of the identification area returned is written
+
to the location pointed to by
+
.Ar sz .
+
This location is set to zero on errors.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_getident
+
will return a non-NULL pointer to the initial bytes of the file if
+
successful, or NULL if an error condition is detected.
+
.Sh ERRORS
+
Function
+
.Fn elf_getident
+
can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
A NULL value was passed in for argument
+
.Ar elf .
+
.It Bq Er ELF_E_SEQUENCE
+
ELF descriptor
+
.Ar elf
+
was opened for writing and function
+
.Fn elf_getident
+
was called before a call to
+
.Xr elf_update 3 .
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf_getarhdr 3 ,
+
.Xr elf_getbase 3 ,
+
.Xr elf_getflags 3 ,
+
.Xr elf_kind 3 ,
+
.Xr elf_rawfile 3 ,
+
.Xr elf_update 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getclass 3 ,
+
.Xr gelf_getehdr 3
added external/libelf/elf_getident.c
@@ -0,0 +1,67 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_getident.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <ar.h>
+
#include <assert.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
char *
+
elf_getident(Elf *e, size_t *sz)
+
{
+

+
	if (e == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		goto error;
+
	}
+

+
	if (e->e_cmd == ELF_C_WRITE && e->e_rawfile == NULL) {
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		goto error;
+
	}
+

+
	assert(e->e_kind != ELF_K_AR || e->e_cmd == ELF_C_READ);
+

+
	if (sz) {
+
		if (e->e_kind == ELF_K_AR)
+
			*sz = SARMAG;
+
		else if (e->e_kind == ELF_K_ELF)
+
			*sz = EI_NIDENT;
+
		else
+
			*sz = e->e_rawsize;
+
	}
+

+
	return (e->e_rawfile);
+

+
 error:
+
	if (sz)
+
		*sz = 0;
+
	return (NULL);
+
}
added external/libelf/elf_getphdrnum.3
@@ -0,0 +1,85 @@
+
.\" 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_getphdrnum.3 233520 2012-03-26 19:23:57Z joel $
+
.\"
+
.Dd August 5, 2009
+
.Dt ELF_GETPHDRNUM 3
+
.Os
+
.Sh NAME
+
.Nm elf_getphdrnum
+
.Nd return the number of program headers in an ELF file
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft int
+
.Fn elf_getphdrnum "Elf *elf" "size_t *phnum"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_getphdrnum
+
retrieves the number of ELF program headers associated with descriptor
+
.Ar elf
+
and stores it into the location pointed to by argument
+
.Ar phnum .
+
.Pp
+
This routine allows applications to uniformly process both normal ELF
+
objects and ELF objects that use extended numbering.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_getphdrnum
+
returns a zero value if successful, or -1 in case of an error.
+
.Sh ERRORS
+
Function
+
.Fn elf_getphnum
+
can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
A NULL value was passed in for argument
+
.Ar elf .
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not for an ELF file.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
lacks an ELF Executable Header.
+
.It Bq Er ELF_E_HEADER
+
The ELF Executable Header associated with argument
+
.Ar elf
+
was corrupt.
+
.It Bq Er ELF_E_SECTION
+
The section header at index
+
.Dv SHN_UNDEF
+
was corrupt.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf_getident 3 ,
+
.Xr elf_getshdrnum 3 ,
+
.Xr elf_getshdrstrndx 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getehdr 3
added external/libelf/elf_getphnum.3
@@ -0,0 +1,92 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_getphnum.3 233462 2012-03-25 12:13:24Z joel $
+
.\"
+
.Dd August 5, 2009
+
.Dt ELF_GETPHNUM 3
+
.Os
+
.Sh NAME
+
.Nm elf_getphnum
+
.Nd return the number of program headers in an ELF file
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft int
+
.Fn elf_getphnum "Elf *elf" "size_t *phnum"
+
.Sh DESCRIPTION
+
This function is deprecated.
+
Please use function
+
.Xr elf_getphdrnum 3
+
instead.
+
.Pp
+
Function
+
.Fn elf_getphnum
+
retrieves the number of ELF program headers associated with descriptor
+
.Ar elf
+
and stores it into the location pointed to by argument
+
.Ar phnum .
+
.Pp
+
This routine allows applications to uniformly process both normal ELF
+
objects and ELF objects that use extended numbering.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_getphnum
+
returns a non-zero value if successful, or zero in case of an
+
error.
+
.Sh ERRORS
+
Function
+
.Fn elf_getphnum
+
can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
A NULL value was passed in for argument
+
.Ar elf .
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not for an ELF file.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
lacks an ELF Executable Header.
+
.It Bq Er ELF_E_HEADER
+
The ELF Executable Header associated with argument
+
.Ar elf
+
was corrupt.
+
.It Bq Er ELF_E_SECTION
+
The section header at index
+
.Dv SHN_UNDEF
+
was corrupt.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf_getident 3 ,
+
.Xr elf_getphdrnum 3 ,
+
.Xr elf_getshdrnum 3 ,
+
.Xr elf_getshdrstrndx 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getehdr 3
added external/libelf/elf_getscn.3
@@ -0,0 +1,151 @@
+
.\" Copyright (c) 2006,2007 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_getscn.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd October 22, 2007
+
.Dt ELF_GETSCN 3
+
.Os
+
.Sh NAME
+
.Nm elf_getscn ,
+
.Nm elf_ndxscn ,
+
.Nm elf_newscn ,
+
.Nm elf_nextscn
+
.Nd get/allocate section information for an ELF object
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf_Scn *"
+
.Fn elf_getscn "Elf *elf" "size_t index"
+
.Ft size_t
+
.Fn elf_ndxscn "Elf_Scn *scn"
+
.Ft "Elf_Scn *"
+
.Fn elf_newscn "Elf *elf"
+
.Ft "Elf_Scn *"
+
.Fn elf_nextscn "Elf *elf" "Elf_Scn *scn"
+
.Sh DESCRIPTION
+
These functions are used to iterate through the sections associated
+
with an ELF descriptor.
+
.Pp
+
Function
+
.Fn elf_getscn
+
will return a section descriptor for the section at index
+
.Ar index
+
in the object denoted by ELF descriptor
+
.Ar elf .
+
An error will be signalled if the specified section does not
+
exist.
+
.Pp
+
Function
+
.Fn elf_ndxscn
+
returns the section table index associated with section descriptor
+
.Ar scn .
+
.Pp
+
Function
+
.Fn elf_newscn
+
creates a new section and appends it to the list of sections
+
associated with descriptor
+
.Ar elf .
+
The library will automatically increment the
+
.Va e_shnum
+
field of the ELF header associated with descriptor
+
.Ar elf ,
+
and will set the
+
.Dv ELF_F_DIRTY
+
flag on the returned section descriptor.
+
For ELF descriptors opened for writing, the ELF library will
+
automatically create an empty section at index zero
+
.Dv ( SHN_UNDEF )
+
on the first call to
+
.Fn elf_newscn .
+
.Pp
+
Function
+
.Fn elf_nextscn
+
takes a section descriptor
+
.Ar scn
+
and returns a pointer to the section descriptor at the next higher
+
index.
+
Argument
+
.Ar scn
+
is allowed to be NULL, in which case this function will return a
+
pointer to the section descriptor at index 1.
+
If no further sections are present, function
+
.Fn elf_nextscn
+
will return a NULL pointer.
+
.Sh RETURN VALUES
+
Functions
+
.Fn elf_getscn ,
+
.Fn elf_newscn
+
and
+
.Fn elf_nextscn
+
return a valid pointer to a section descriptor if successful, or
+
NULL if an error occurs.
+
.Pp
+
Function
+
.Fn elf_ndxscn
+
returns a valid section table index if successful, or
+
.Dv SHN_UNDEF
+
if an error occurs.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar elf
+
or
+
.Ar scn
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar index
+
exceeded the current number of sections in the ELF object.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for an ELF file.
+
.It Bq Er ELF_E_ARGUMENT
+
Section descriptor
+
.Ar scn
+
was not associated with ELF descriptor
+
.Ar elf .
+
.It Bq Er ELF_E_CLASS
+
Descriptor
+
.Ar elf
+
was of an unknown ELF class.
+
.It Bq Er ELF_E_SECTION
+
Argument
+
.Ar elf
+
specified extended section numbering in the ELF header with the section header at
+
index
+
.Dv SHN_UNDEF
+
not being of type
+
.Dv SHT_NULL .
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_flagdata 3 ,
+
.Xr elf_flagscn 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getshdr 3 ,
+
.Xr gelf 3
added external/libelf/elf_getshdrnum.3
@@ -0,0 +1,77 @@
+
.\" 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_getshdrnum.3 233520 2012-03-26 19:23:57Z joel $
+
.\"
+
.Dd August 4, 2009
+
.Dt ELF_GETSHDRNUM 3
+
.Os
+
.Sh NAME
+
.Nm elf_getshdrnum
+
.Nd return the number of sections in an ELF file
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft int
+
.Fn elf_getshdrnum "Elf *elf" "size_t *shnum"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_getshdrnum
+
retrieves the number of ELF sections associated with descriptor
+
.Ar elf
+
and stores it into the location pointed to by argument
+
.Ar shnum .
+
.Pp
+
This routine allows applications to uniformly process both normal ELF
+
objects, and ELF objects that use extended section numbering.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_getshdrnum
+
returns zero value if successful, or -1 in case of an error.
+
.Sh ERRORS
+
Function
+
.Fn elf_getshdrnum
+
can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
A NULL value was passed in for argument
+
.Ar elf .
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not for an ELF file.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
lacks an ELF Executable header.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf_getident 3 ,
+
.Xr elf_getphdrnum 3 ,
+
.Xr elf_getshdrstrndx 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getehdr 3
added external/libelf/elf_getshdrstrndx.3
@@ -0,0 +1,78 @@
+
.\" 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_getshdrstrndx.3 233520 2012-03-26 19:23:57Z joel $
+
.\"
+
.Dd August 5, 2009
+
.Dt ELF_GETSHDRSTRNDX 3
+
.Os
+
.Sh NAME
+
.Nm elf_getshdrstrndx
+
.Nd retrieve the index of the section name string table
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft int
+
.Fn elf_getshdrstrndx "Elf *elf" "size_t *ndxptr"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_getshdrstrndx
+
retrieves the section index of the string table containing section
+
names from descriptor
+
.Ar elf
+
and stores it into the location pointed to by argument
+
.Ar ndxptr .
+
.Pp
+
This function allow applications to process both normal ELF
+
objects and ELF objects that use extended section numbering uniformly.
+
.Sh RETURN VALUES
+
These functions return zero if successful, or -1 in case of an error.
+
.Sh ERRORS
+
These functions can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
A NULL value was passed in for argument
+
.Ar elf .
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not for an ELF file.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
lacks an ELF Executable header.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
contained a value in the reserved range of section indices.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf_getident 3 ,
+
.Xr elf_getphdrnum 3 ,
+
.Xr elf_getshdrnum 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getehdr 3
added external/libelf/elf_getshnum.3
@@ -0,0 +1,83 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_getshnum.3 233462 2012-03-25 12:13:24Z joel $
+
.\"
+
.Dd August 5, 2009
+
.Dt ELF_GETSHNUM 3
+
.Os
+
.Sh NAME
+
.Nm elf_getshnum
+
.Nd return the number of sections in an ELF file
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft int
+
.Fn elf_getshnum "Elf *elf" "size_t *shnum"
+
.Sh DESCRIPTION
+
This function is deprecated.
+
Please use
+
.Xr elf_getshdrnum 3
+
instead.
+
.Pp
+
Function
+
.Fn elf_getshnum
+
retrieves the number of ELF sections associated with descriptor
+
.Ar elf
+
and stores it into the location pointed to by argument
+
.Ar shnum .
+
.Pp
+
This routine allows applications to uniformly process both normal ELF
+
objects, and ELF objects that use extended section numbering.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_getshnum
+
returns a non-zero value if successful, or zero in case of an
+
error.
+
.Sh ERRORS
+
Function
+
.Fn elf_getshnum
+
can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
A NULL value was passed in for argument
+
.Ar elf .
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not for an ELF file.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
lacks an ELF Executable header.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf_getident 3 ,
+
.Xr elf_getphdrnum 3 ,
+
.Xr elf_getshdrstrndx 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getehdr 3
added external/libelf/elf_getshstrndx.3
@@ -0,0 +1,93 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_getshstrndx.3 233462 2012-03-25 12:13:24Z joel $
+
.\"
+
.Dd August 5, 2009
+
.Dt ELF_GETSHSTRNDX 3
+
.Os
+
.Sh NAME
+
.Nm elf_getshstrndx ,
+
.Nm elf_setshstrndx
+
.Nd retrieve/update the index of the section name string table
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft int
+
.Fn elf_getshstrndx "Elf *elf" "size_t *ndxptr"
+
.Ft int
+
.Fn elf_setshstrndx "Elf *elf" "size_t ndx"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_getshstrndx
+
retrieves the section index of the string table containing section
+
names from descriptor
+
.Ar elf
+
and stores it into the location pointed to by argument
+
.Ar ndxptr .
+
Function
+
.Fn elf_getshstrndx
+
is deprecated.
+
Please use
+
.Xr elf_getshdrstrndx 3
+
instead.
+
.Pp
+
Function
+
.Fn elf_setshstrndx
+
sets the index of the section name string table to argument
+
.Ar ndx .
+
.Pp
+
These routines allow applications to process both normal ELF
+
objects and ELF objects that use extended section numbering uniformly.
+
.Sh RETURN VALUES
+
These functions return a non-zero value if successful, or zero in case
+
of an error.
+
.Sh ERRORS
+
These functions can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
A NULL value was passed in for argument
+
.Ar elf .
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not for an ELF file.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
lacks an ELF Executable header.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
contained a value in the reserved range of section indices.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf_getident 3 ,
+
.Xr elf_getphdrnum 3 ,
+
.Xr elf_getshdrnum 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getehdr 3
added external/libelf/elf_hash.3
@@ -0,0 +1,57 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_hash.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 15, 2006
+
.Dt ELF_HASH 3
+
.Os
+
.Sh NAME
+
.Nm elf_hash
+
.Nd compute a hash value for a string
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "unsigned long"
+
.Fn elf_hash "const char *name"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_hash
+
computes a portable hash value for the null terminated string
+
pointed to by argument
+
.Ar name .
+
.Pp
+
The hash value returned is will be identical across
+
machines of different architectures.
+
This allows hash tables to be built on one machine and
+
correctly used on another of a different architecture.
+
The hash value returned is also guaranteed
+
.Em not
+
to be the bit pattern of all ones (~0UL).
+
.Sh IMPLEMENTATION NOTES
+
The library internally uses unsigned 32 bit arithmetic to compute
+
the hash value.
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr gelf 3
added external/libelf/elf_hash.c
@@ -0,0 +1,54 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_hash.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
/*
+
 * This elf_hash function is defined by the System V ABI.  It must be
+
 * kept compatible with "src/libexec/rtld-elf/rtld.c".
+
 */
+

+
unsigned long
+
elf_hash(const char *name)
+
{
+
	unsigned long h, t;
+
	const unsigned char *s;
+

+
	s = (const unsigned char *) name;
+
	h = t = 0;
+

+
	for (; *s != '\0'; h = h & ~t) {
+
		h = (h << 4) + *s++;
+
		t = h & 0xF0000000UL;
+
		if (t)
+
			h ^= t >> 24;
+
	}
+

+
	return (h);
+
}
added external/libelf/elf_kind.3
@@ -0,0 +1,71 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_kind.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd June 1, 2006
+
.Dt ELF_KIND 3
+
.Os
+
.Sh NAME
+
.Nm elf_kind
+
.Nd determine ELF file type
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft Elf_Kind
+
.Fn elf_kind "Elf *elf"
+
.Sh DESCRIPTION
+
The
+
.Fn elf_kind
+
function identifies the kind of file associated with its argument
+
.Ar elf .
+
The argument
+
.Ar elf
+
is allowed to be NULL.
+
.Sh RETURN VALUES
+
The
+
.Fn elf_kind
+
function returns one of the following values:
+
.Bl -tag -width indent
+
.It Dv ELF_K_AR
+
The file associated with argument
+
.Ar elf
+
is an archive.
+
.It Dv ELF_K_ELF
+
The file associated with argument
+
.Ar elf
+
is an ELF file.
+
.It Dv ELF_K_NONE
+
The argument
+
.Ar elf
+
was NULL, or the ELF library could not determine the type of the file
+
associated with argument
+
.Ar elf ,
+
or an error occurred when processing.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_begin 3 ,
+
.Xr elf_getident 3 ,
+
.Xr gelf 3
added external/libelf/elf_kind.c
@@ -0,0 +1,43 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_kind.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
Elf_Kind
+
elf_kind(Elf *e)
+
{
+
	if (e == NULL)
+
		return (ELF_K_NONE);
+
	if (e->e_kind == ELF_K_AR ||
+
	    e->e_kind == ELF_K_ELF)
+
		return (e->e_kind);
+
	return (ELF_K_NONE);
+
}
added external/libelf/elf_memory.3
@@ -0,0 +1,122 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_memory.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd June 28, 2006
+
.Dt ELF_MEMORY 3
+
.Os
+
.Sh NAME
+
.Nm elf_memory
+
.Nd process an ELF or ar(1) archive mapped into memory
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf *"
+
.Fn elf_memory "char *image" "size_t size"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_memory
+
is used to process an ELF file or
+
.Xr ar 1
+
archive whose image is present in memory.
+
.Pp
+
Argument
+
.Ar image
+
points to the start of the memory image of the file or archive.
+
Argument
+
.Ar size
+
contains the size in bytes of the memory image.
+
.Pp
+
The ELF descriptor is created for reading (i.e., analogous to the
+
use of
+
.Xr elf_begin 3
+
with a command argument value of
+
.Dv ELF_C_READ Ns ).
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_memory
+
returns a pointer to a new ELF descriptor if successful, or NULL if an
+
error occurred.
+
.Pp
+
The return value may be queried for the file type using
+
.Xr elf_kind 3 .
+
.Sh EXAMPLES
+
To read parse an elf file, use:
+
.Bd -literal -offset indent
+
int fd;
+
void *p;
+
struct stat sb;
+
Elf *e;
+
\&...
+
if ((fd = open("./elf-file", O_RDONLY)) < 0 ||
+
    fstat(fd, &sb) < 0 ||
+
    (p = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t) 0)) ==
+
    MAP_FAILED) {
+
	... handle system error ...
+
}
+

+
if ((e = elf_memory(p, sb.st_size)) == NULL) {
+
	... handle elf(3) error ...
+
}
+
\&... use ELF descriptor "e" here ...
+
.Ed
+
.Sh ERRORS
+
Function
+
.Fn elf_memory
+
can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
A NULL value was used for argument
+
.Ar image
+
or the value of argument
+
.Ar sz
+
was zero.
+
.It Bq Er ELF_E_HEADER
+
The header of the ELF object contained an unsupported value in its
+
.Va e_ident[EI_CLASS]
+
field.
+
.It Bq Er ELF_E_HEADER
+
The header of the ELF object contained an unsupported value in its
+
.Va e_ident[EI_DATA]
+
field.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected.
+
.It Bq Er ELF_E_SEQUENCE
+
Function
+
.Fn elf_memory
+
was called before a working version was set using
+
.Xr elf_version 3 .
+
.It Bq Er ELF_E_VERSION
+
The argument
+
.Ar image
+
corresponds to an ELF file with an unsupported version.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_begin 3 ,
+
.Xr elf_end 3 ,
+
.Xr elf_errno 3 ,
+
.Xr elf_kind 3 ,
+
.Xr gelf 3
added external/libelf/elf_memory.c
@@ -0,0 +1,91 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_memory.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <ar.h>
+
#include <libelf.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
Elf *
+
elf_memory(char *image, size_t sz)
+
{
+
	Elf *e;
+

+
	if (LIBELF_PRIVATE(version) == EV_NONE) {
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		return (NULL);
+
	}
+

+
	if (image == NULL || sz == 0) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if ((e = _libelf_allocate_elf()) == NULL)
+
		return (NULL);
+

+
	e->e_cmd = ELF_C_READ;
+
	e->e_rawfile = image;
+
	e->e_rawsize = sz;
+

+
#undef	LIBELF_IS_ELF
+
#define	LIBELF_IS_ELF(P) ((P)[EI_MAG0] == ELFMAG0 && 		\
+
	(P)[EI_MAG1] == ELFMAG1 && (P)[EI_MAG2] == ELFMAG2 &&	\
+
	(P)[EI_MAG3] == ELFMAG3)
+

+
	if (sz > EI_NIDENT && LIBELF_IS_ELF(image)) {
+
		_libelf_init_elf(e, ELF_K_ELF);
+
		e->e_class = image[EI_CLASS];
+
		e->e_byteorder = image[EI_DATA];
+
		e->e_version = image[EI_VERSION];
+

+
		if (e->e_version > EV_CURRENT) {
+
			e = _libelf_release_elf(e);
+
			LIBELF_SET_ERROR(VERSION, 0);
+
			return (NULL);
+
		}
+

+
		if ((e->e_byteorder != ELFDATA2LSB && e->e_byteorder !=
+
 		    ELFDATA2MSB) || (e->e_class != ELFCLASS32 && e->e_class !=
+
		    ELFCLASS64)) {
+
			e = _libelf_release_elf(e);
+
			LIBELF_SET_ERROR(HEADER, 0);
+
			return (NULL);
+
		}
+

+
	} else if (sz >= SARMAG &&
+
	    strncmp(image, ARMAG, (size_t) SARMAG) == 0) {
+
		_libelf_init_elf(e, ELF_K_AR);
+
		e = _libelf_ar_open(e);
+
	} else
+
		_libelf_init_elf(e, ELF_K_NONE);
+

+
	return (e);
+
}
added external/libelf/elf_next.3
@@ -0,0 +1,96 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_next.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd June 17, 2006
+
.Dt ELF_NEXT 3
+
.Os
+
.Sh NAME
+
.Nm elf_next
+
.Nd provide sequential access to the next archive member
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft Elf_Cmd
+
.Fn elf_next "Elf *elf"
+
.Sh DESCRIPTION
+
The
+
.Fn elf_next
+
function causes the ELF archive descriptor corresponding to argument
+
.Ar elf
+
to be adjusted to provide access to the next member in
+
the archive on a subsequent call to
+
.Fn elf_begin .
+
.Pp
+
The return value of
+
.Fn elf_next
+
is suitable for use in a loop invoking
+
.Fn elf_begin .
+
.Sh RETURN VALUES
+
If successful, function
+
.Fn elf_next
+
returns the value
+
.Dv ELF_C_READ .
+
Otherwise, if argument
+
.Ar elf
+
was not associated with an archive, or if it was
+
.Dv NULL ,
+
or if any other error occurred, the value
+
.Dv ELF_C_NULL
+
is returned.
+
.Sh EXAMPLES
+
To process all the members of an archive use:
+
.Bd -literal -offset indent
+
Elf_Cmd cmd;
+
Elf *archive, *e;
+
\&...
+
cmd = ELF_C_READ;
+
archive = elf_begin(fd, cmd, NULL);
+
while ((e = elf_begin(fd, cmd, archive)) != (Elf *) 0)
+
{
+
	... process `e' here ...
+

+
	cmd = elf_next(e);
+
	elf_end(e);
+
}
+
elf_end(archive);
+
.Ed
+
.Sh ERRORS
+
Function
+
.Fn elf_next
+
may fail with the following error:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not associated with a containing
+
.Xr ar 1
+
archive.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_begin 3 ,
+
.Xr elf_end 3 ,
+
.Xr elf_rand 3
added external/libelf/elf_next.c
@@ -0,0 +1,61 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_next.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <ar.h>
+
#include <assert.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
Elf_Cmd
+
elf_next(Elf *e)
+
{
+
	off_t next;
+
	Elf *parent;
+

+
	if (e == NULL)
+
		return (ELF_C_NULL);
+

+
	 if ((parent = e->e_parent) == NULL) {
+
		 LIBELF_SET_ERROR(ARGUMENT, 0);
+
		 return (ELF_C_NULL);
+
	 }
+

+
	assert (parent->e_kind == ELF_K_AR);
+
	assert (parent->e_cmd == ELF_C_READ);
+
	assert((uintptr_t) e->e_rawfile % 2 == 0);
+
	assert(e->e_rawfile > parent->e_rawfile);
+

+
	next = e->e_rawfile - parent->e_rawfile + e->e_rawsize;
+
	next = (next + 1) & ~1;	/* round up to an even boundary */
+

+
	parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ? (off_t) 0 : next;
+

+
	return (ELF_C_READ);
+
}
added external/libelf/elf_phnum.c
@@ -0,0 +1,66 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_phnum.c 210345 2010-07-21 12:14:50Z kaiw $");
+

+
#include <ar.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
static int
+
_libelf_getphdrnum(Elf *e, size_t *phnum)
+
{
+
	void *eh;
+
	int ec;
+

+
	if (e == NULL || e->e_kind != ELF_K_ELF ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (-1);
+
	}
+

+
	if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
+
		return (-1);
+

+
	*phnum = e->e_u.e_elf.e_nphdr;
+

+
	return (0);
+
}
+

+
int
+
elf_getphdrnum(Elf *e, size_t *phnum)
+
{
+
	return (_libelf_getphdrnum(e, phnum));
+
}
+

+
/* Deprecated API */
+
int
+
elf_getphnum(Elf *e, size_t *phnum)
+
{
+
	return (_libelf_getphdrnum(e, phnum) >= 0);
+
}
added external/libelf/elf_rand.3
@@ -0,0 +1,118 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_rand.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd June 17, 2006
+
.Dt ELF_RAND 3
+
.Os
+
.Sh NAME
+
.Nm elf_rand
+
.Nd provide sequential access to the next archive member
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft off_t
+
.Fn elf_rand "Elf *archive" "off_t offset"
+
.Sh DESCRIPTION
+
The
+
.Fn elf_rand
+
function causes the ELF descriptor
+
.Ar archive
+
to be adjusted so that the next call to
+
.Xr elf_begin 3
+
will provide access to the archive member at byte offset
+
.Ar offset
+
in the archive.
+
Argument
+
.Ar offset
+
is the byte offset from the start of the archive to the beginning of
+
the archive header for the desired member.
+
.Pp
+
Archive member offsets may be retrieved using the
+
.Xr elf_getarsym 3
+
function.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_rand
+
returns
+
.Ar offset
+
if successful or zero in case of an error.
+
.Sh EXAMPLES
+
To process all the members of an archive use:
+
.Bd -literal -offset indent
+
off_t off;
+
Elf *archive, *e;
+
\&...
+
cmd = ELF_C_READ;
+
archive = elf_begin(fd, cmd, NULL);
+
while ((e = elf_begin(fd, cmd, archive)) != (Elf *) 0)
+
{
+
	... process `e' here ...
+
	elf_end(e);
+

+
	off = ...new value...;
+
	if (elf_rand(archive, off) != off) {
+
		... process error ...
+
	}
+
}
+
elf_end(archive);
+
.Ed
+
.Pp
+
To rewind an archive, use:
+
.Bd -literal -offset indent
+
Elf *archive;
+
\&...
+
if (elf_rand(archive, SARMAG) != SARMAG) {
+
	... error ...
+
}
+
.Ed
+
.Sh ERRORS
+
Function
+
.Fn elf_rand
+
may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar archive
+
was null.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar archive
+
was not a descriptor for an
+
.Xr ar 1
+
archive.
+
.It Bq Er ELF_E_ARCHIVE
+
Argument
+
.Ar offset
+
did not correspond to the start of an archive member header.
+
.El
+
.Sh SEE ALSO
+
.Xr ar 1 ,
+
.Xr elf 3 ,
+
.Xr elf_begin 3 ,
+
.Xr elf_end 3 ,
+
.Xr elf_getarsym 3 ,
+
.Xr elf_next 3 ,
+
.Xr gelf 3
added external/libelf/elf_rand.c
@@ -0,0 +1,58 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_rand.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <ar.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
off_t
+
elf_rand(Elf *ar, off_t offset)
+
{
+
	struct ar_hdr *arh;
+

+
	if (ar == NULL || ar->e_kind != ELF_K_AR ||
+
	    (offset & 1) || offset < SARMAG ||
+
	    offset + sizeof(struct ar_hdr) >= ar->e_rawsize) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return 0;
+
	}
+

+
	arh = (struct ar_hdr *) (ar->e_rawfile + offset);
+

+
	/* a too simple sanity check */
+
	if (arh->ar_fmag[0] != '`' || arh->ar_fmag[1] != '\n') {
+
		LIBELF_SET_ERROR(ARCHIVE, 0);
+
		return 0;
+
	}
+

+
	ar->e_u.e_ar.e_next = offset;
+

+
	return (offset);
+
}
added external/libelf/elf_rawfile.3
@@ -0,0 +1,76 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_rawfile.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd July 3, 2006
+
.Dt ELF_RAWFILE 3
+
.Os
+
.Sh NAME
+
.Nm elf_rawfile
+
.Nd return uninterpreted contents of an ELF file
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft char *
+
.Fn elf_rawfile "Elf *elf" "size_t *sz"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_rawfile
+
returns the uninterpreted contents of the file referenced by ELF descriptor
+
.Ar elf .
+
.Pp
+
If argument
+
.Ar sz
+
is non-null, the function stores the file's size in bytes
+
in the location to which it points.
+
A value of zero is written to this location if an error is
+
encountered.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_rawfile
+
returns a valid pointer if successful or NULL if an error occurs.
+
.Sh ERRORS
+
Function
+
.Fn elf_rawfile
+
may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL.
+
.It Bq Er ELF_E_SEQUENCE
+
Argument
+
.Ar elf
+
was opened for writing and function
+
.Fn elf_rawfile
+
was invoked before
+
.Xr elf_update 3 .
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getident 3 ,
+
.Xr elf_kind 3 ,
+
.Xr elf_update 3
added external/libelf/elf_rawfile.c
@@ -0,0 +1,52 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_rawfile.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
char *
+
elf_rawfile(Elf *e, size_t *sz)
+
{
+
	char *ptr;
+
	size_t size;
+

+
	size = e ? e->e_rawsize : 0;
+
	ptr = NULL;
+

+
	if (e == NULL)
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
	else if ((ptr = e->e_rawfile) == NULL && e->e_cmd == ELF_C_WRITE)
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+

+
	if (sz)
+
		*sz = size;
+

+
	return (ptr);
+
}
added external/libelf/elf_scn.c
@@ -0,0 +1,229 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_scn.c 210338 2010-07-21 10:25:02Z kaiw $");
+

+
#include <assert.h>
+
#include <errno.h>
+
#include <gelf.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Load an ELF section table and create a list of Elf_Scn structures.
+
 */
+
int
+
_libelf_load_scn(Elf *e, void *ehdr)
+
{
+
	int ec, swapbytes;
+
	size_t fsz, i, shnum;
+
	uint64_t shoff;
+
	char *src;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+
	Elf_Scn *scn;
+
	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
+

+
	assert(e != NULL);
+
	assert(ehdr != NULL);
+
	assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0);
+

+
#define	CHECK_EHDR(E,EH)	do {				\
+
		if (fsz != (EH)->e_shentsize ||			\
+
		    shoff + fsz * shnum > e->e_rawsize) {	\
+
			LIBELF_SET_ERROR(HEADER, 0);		\
+
			return (0);				\
+
		}						\
+
	} while (0)
+

+
	ec = e->e_class;
+
	fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
+
	assert(fsz > 0);
+

+
	shnum = e->e_u.e_elf.e_nscn;
+

+
	if (ec == ELFCLASS32) {
+
		eh32 = (Elf32_Ehdr *) ehdr;
+
		shoff = (uint64_t) eh32->e_shoff;
+
		CHECK_EHDR(e, eh32);
+
	} else {
+
		eh64 = (Elf64_Ehdr *) ehdr;
+
		shoff = eh64->e_shoff;
+
		CHECK_EHDR(e, eh64);
+
	}
+

+
	xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
+

+
	swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder);
+
	src = e->e_rawfile + shoff;
+

+
	/*
+
	 * If the file is using extended numbering then section #0
+
	 * would have already been read in.
+
	 */
+

+
	i = 0;
+
	if (!STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
+
		assert(STAILQ_FIRST(&e->e_u.e_elf.e_scn) ==
+
		    STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next));
+

+
		i = 1;
+
		src += fsz;
+
	}
+

+
	for (; i < shnum; i++, src += fsz) {
+
		if ((scn = _libelf_allocate_scn(e, i)) == NULL)
+
			return (0);
+

+
		(*xlator)((char *) &scn->s_shdr, sizeof(scn->s_shdr), src,
+
		    (size_t) 1, swapbytes);
+

+
		if (ec == ELFCLASS32) {
+
			scn->s_offset = scn->s_rawoff =
+
			    scn->s_shdr.s_shdr32.sh_offset;
+
			scn->s_size = scn->s_shdr.s_shdr32.sh_size;
+
		} else {
+
			scn->s_offset = scn->s_rawoff =
+
			    scn->s_shdr.s_shdr64.sh_offset;
+
			scn->s_size = scn->s_shdr.s_shdr64.sh_size;
+
		}
+
	}
+

+
	e->e_flags |= LIBELF_F_SHDRS_LOADED;
+

+
	return (1);
+
}
+

+

+
Elf_Scn *
+
elf_getscn(Elf *e, size_t index)
+
{
+
	int ec;
+
	void *ehdr;
+
	Elf_Scn *s;
+

+
	if (e == NULL || e->e_kind != ELF_K_ELF ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
+
		return (NULL);
+

+
	if (e->e_cmd != ELF_C_WRITE &&
+
	    (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
+
	    _libelf_load_scn(e, ehdr) == 0)
+
		return (NULL);
+

+
	STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)
+
		if (s->s_ndx == index)
+
			return (s);
+

+
	LIBELF_SET_ERROR(ARGUMENT, 0);
+
	return (NULL);
+
}
+

+
size_t
+
elf_ndxscn(Elf_Scn *s)
+
{
+
	if (s == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (SHN_UNDEF);
+
	}
+
	return (s->s_ndx);
+
}
+

+
Elf_Scn *
+
elf_newscn(Elf *e)
+
{
+
	int ec;
+
	void *ehdr;
+
	Elf_Scn *scn;
+

+
	if (e == NULL || e->e_kind != ELF_K_ELF) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
+
		LIBELF_SET_ERROR(CLASS, 0);
+
		return (NULL);
+
	}
+

+
	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
+
		return (NULL);
+

+
	/*
+
	 * The application may be asking for a new section descriptor
+
	 * on an ELF object opened with ELF_C_RDWR or ELF_C_READ.  We
+
	 * need to bring in the existing section information before
+
	 * appending a new one to the list.
+
	 *
+
	 * Per the ELF(3) API, an application is allowed to open a
+
	 * file using ELF_C_READ, mess with its internal structure and
+
	 * use elf_update(...,ELF_C_NULL) to compute its new layout.
+
	 */
+
	if (e->e_cmd != ELF_C_WRITE &&
+
	    (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
+
	    _libelf_load_scn(e, ehdr) == 0)
+
		return (NULL);
+

+
	if (STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) {
+
		assert(e->e_u.e_elf.e_nscn == 0);
+
		if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF)) ==
+
		    NULL)
+
			return (NULL);
+
		e->e_u.e_elf.e_nscn++;
+
	}
+

+
	assert(e->e_u.e_elf.e_nscn > 0);
+

+
	if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL)
+
		return (NULL);
+

+
	e->e_u.e_elf.e_nscn++;
+

+
	(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
+

+
	return (scn);
+
}
+

+
Elf_Scn *
+
elf_nextscn(Elf *e, Elf_Scn *s)
+
{
+
	if (e == NULL || (e->e_kind != ELF_K_ELF) ||
+
	    (s && s->s_elf != e)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	return (s == NULL ? elf_getscn(e, (size_t) 1) :
+
	    STAILQ_NEXT(s, s_next));
+
}
added external/libelf/elf_shnum.c
@@ -0,0 +1,66 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_shnum.c 210345 2010-07-21 12:14:50Z kaiw $");
+

+
#include <ar.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
static int
+
_libelf_getshdrnum(Elf *e, size_t *shnum)
+
{
+
	void *eh;
+
	int ec;
+

+
	if (e == NULL || e->e_kind != ELF_K_ELF ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (-1);
+
	}
+

+
	if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
+
		return (-1);
+

+
	*shnum = e->e_u.e_elf.e_nscn;
+

+
	return (0);
+
}
+

+
int
+
elf_getshdrnum(Elf *e, size_t *shnum)
+
{
+
	return (_libelf_getshdrnum(e, shnum));
+
}
+

+
/* Deprecated API. */
+
int
+
elf_getshnum(Elf *e, size_t *shnum)
+
{
+
	return (_libelf_getshdrnum(e, shnum) >= 0);
+
}
added external/libelf/elf_shstrndx.c
@@ -0,0 +1,81 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_shstrndx.c 210345 2010-07-21 12:14:50Z kaiw $");
+

+
#include <ar.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
static int
+
_libelf_getshdrstrndx(Elf *e, size_t *strndx)
+
{
+
	void *eh;
+
	int ec;
+

+
	if (e == NULL || e->e_kind != ELF_K_ELF ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (-1);
+
	}
+

+
	if ((eh = _libelf_ehdr(e, ec, 0)) == NULL)
+
		return (-1);
+

+
	*strndx = e->e_u.e_elf.e_strndx;
+

+
	return (0);
+
}
+

+
int
+
elf_getshdrstrndx(Elf *e, size_t *strndx)
+
{
+
	return (_libelf_getshdrstrndx(e, strndx));
+
}
+

+
int
+
elf_getshstrndx(Elf *e, size_t *strndx)	/* Deprecated API. */
+
{
+
	return (_libelf_getshdrstrndx(e, strndx) >= 0);
+
}
+

+
int
+
elf_setshstrndx(Elf *e, size_t strndx)
+
{
+
	void *eh;
+
	int ec;
+

+
	if (e == NULL || e->e_kind != ELF_K_ELF ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
+
	    ((eh = _libelf_ehdr(e, ec, 0)) == NULL)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	return (_libelf_setshstrndx(e, eh, ec, strndx));
+
}
added external/libelf/elf_strptr.3
@@ -0,0 +1,116 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_strptr.3 235286 2012-05-11 20:06:46Z gjb $
+
.\"
+
.Dd December 16, 2006
+
.Dt ELF_STRPTR 3
+
.Os
+
.Sh NAME
+
.Nm elf_strptr
+
.Nd retrieve a string pointer in a string table
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "char *"
+
.Fn elf_strptr "Elf *elf" "size_t scndx" "size_t stroffset"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_strptr
+
allows an application to convert a string table offset to a string
+
pointer, correctly translating the offset in the presence
+
of multiple
+
.Vt Elf_Data
+
descriptors covering the contents of the section.
+
.Pp
+
Argument
+
.Ar elf
+
is a descriptor for an ELF object.
+
Argument
+
.Ar scndx
+
is the section index for an ELF string table.
+
Argument
+
.Ar stroffset
+
is the index of the desired string in the string
+
table.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_strptr
+
returns a valid pointer on success or NULL in case an error was
+
encountered.
+
.Sh ERRORS
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for an ELF object.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar scndx
+
was not the section index for a string table.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar stroffset
+
exceeded the size of the string table.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar stroffset
+
index an unallocated region of the string table.
+
.It Bq Er ELF_E_DATA
+
Offset
+
.Ar stroffset
+
indexed a region that was not covered by any Elf_Data
+
descriptor.
+
.It Bq Er ELF_E_DATA
+
An erroneous
+
.Vt Elf_Data
+
descriptor was part of the section specified by argument
+
.Ar scndx .
+
.It Bq Er ELF_E_HEADER
+
ELF descriptor
+
.Ar elf
+
contained an invalid section header.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected.
+
.It Bq Er ELF_E_SECTION
+
Section
+
.Ar scndx
+
contained a malformed section header.
+
.It Bq Er ELF_E_SECTION
+
The ELF descriptor in argument
+
.Ar elf
+
did not adhere to the conventions used for extended numbering.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getshdr 3 ,
+
.Xr elf64_getshdr 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_rawdata 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getshdr 3
added external/libelf/elf_strptr.c
@@ -0,0 +1,133 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_strptr.c 179241 2008-05-23 07:35:36Z jb $");
+

+
#include <sys/param.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Convert an ELF section#,offset pair to a string pointer.
+
 */
+

+
char *
+
elf_strptr(Elf *e, size_t scndx, size_t offset)
+
{
+
	Elf_Scn *s;
+
	Elf_Data *d;
+
	size_t alignment, count;
+
	GElf_Shdr shdr;
+

+
	if (e == NULL || e->e_kind != ELF_K_ELF) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if ((s = elf_getscn(e, scndx)) == NULL ||
+
	    gelf_getshdr(s, &shdr) == NULL)
+
		return (NULL);
+

+
	if (/*shdr.sh_type != SHT_STRTAB || */
+
	    offset >= shdr.sh_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	d = NULL;
+
	if (e->e_flags & ELF_F_LAYOUT) {
+

+
		/*
+
		 * The application is taking responsibility for the
+
		 * ELF object's layout, so we can directly translate
+
		 * an offset to a `char *' address using the `d_off'
+
		 * members of Elf_Data descriptors.
+
		 */
+
		while ((d = elf_getdata(s, d)) != NULL) {
+

+
			if (d->d_buf == 0 || d->d_size == 0)
+
				continue;
+

+
			if (d->d_type != ELF_T_BYTE) {
+
				LIBELF_SET_ERROR(DATA, 0);
+
				return (NULL);
+
			}
+

+
			if (offset >= d->d_off &&
+
			    offset < d->d_off + d->d_size)
+
				return ((char *) d->d_buf + offset - d->d_off);
+
		}
+
	} else {
+
		/*
+
		 * Otherwise, the `d_off' members are not useable and
+
		 * we need to compute offsets ourselves, taking into
+
		 * account 'holes' in coverage of the section introduced
+
		 * by alignment requirements.
+
		 */
+
		count = (size_t) 0;	/* cumulative count of bytes seen */
+
		while ((d = elf_getdata(s, d)) != NULL && count <= offset) {
+

+
			if (d->d_buf == NULL || d->d_size == 0)
+
				continue;
+

+
			if (d->d_type != ELF_T_BYTE) {
+
				LIBELF_SET_ERROR(DATA, 0);
+
				return (NULL);
+
			}
+

+
			if ((alignment = d->d_align) > 1) {
+
				if ((alignment & (alignment - 1)) != 0) {
+
					LIBELF_SET_ERROR(DATA, 0);
+
					return (NULL);
+
				}
+
				count = roundup2(count, alignment);
+
			}
+

+
			if (offset < count) {
+
				/* offset starts in the 'hole' */
+
				LIBELF_SET_ERROR(ARGUMENT, 0);
+
				return (NULL);
+
			}
+

+
			if (offset < count + d->d_size) {
+
				if (d->d_buf != NULL)
+
					return ((char *) d->d_buf +
+
					    offset - count);
+
				LIBELF_SET_ERROR(DATA, 0);
+
				return (NULL);
+
			}
+

+
			count += d->d_size;
+
		}
+
	}
+

+
	LIBELF_SET_ERROR(ARGUMENT, 0);
+
	return (NULL);
+
}
added external/libelf/elf_types.m4
@@ -0,0 +1,315 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 *
+
 * $FreeBSD: releng/9.1/lib/libelf/elf_types.m4 210341 2010-07-21 10:57:22Z kaiw $
+
 */
+

+
/*
+
 * ELF types, defined in the "enum Elf_Type" API.
+
 *
+
 * The members of the list form a 3-tuple: (name, C-type-suffix, OSversion).
+
 * + `name' is an Elf_Type symbol without the `ELF_T_' prefix.
+
 * + `C-type-suffix' is the suffix for Elf32_ and Elf64_ type names.
+
 * + `version' is the OS version the symbol first appeared in.
+
 *
+
 * OS revisions of note are:
+
 * 600102 - The earliest (6.0-STABLE) version supported by this code.
+
 * 700009 - Symbol versioning and ELF64 type changes.
+
 * 700025 - More ELF types and the introduction of libelf.
+
 */
+

+
define(`ELF_TYPE_LIST',
+
	``ADDR,		Addr,	600102',
+
	`BYTE,		Byte,	600102',
+
	`CAP,		Cap,	700025',
+
	`DYN,		Dyn,	600102',
+
	`EHDR,		Ehdr,	600102',
+
	`GNUHASH,	-,	800062',
+
	`HALF,		Half,	600102',
+
	`LWORD,		Lword,	700025',
+
	`MOVE,		Move,	700025',
+
	`MOVEP,		MoveP,	700025',
+
	`NOTE,		Note,	600102',
+
	`OFF,		Off,	600102',
+
	`PHDR,		Phdr,	600102',
+
	`REL,		Rel,	600102',
+
	`RELA,		Rela,	600102',
+
	`SHDR,		Shdr,	600102',
+
	`SWORD,		Sword,	600102',
+
	`SXWORD,	Sxword,	700009',
+
	`SYMINFO,	Syminfo, 700025',
+
	`SYM,		Sym,	600102',
+
	`VDEF,		Verdef,	700009',
+
	`VNEED,		Verneed, 700009',
+
	`WORD,		Word,	600102',
+
	`XWORD,		Xword,	700009',
+
	`NUM,		_,	_'')
+

+
/*
+
 * DEFINE_STRUCT(NAME,MEMBERLIST...)
+
 *
+
 * Map a type name to its members.
+
 *
+
 * Each member-list element comprises of pairs of (field name, type),
+
 * in the sequence used in the file representation of `NAME'.
+
 *
+
 * Each member list element comprises a pair containing a field name
+
 * and a basic type.  Basic types include IDENT, HALF, WORD, LWORD,
+
 * ADDR{32,64}, OFF{32,64}, SWORD, XWORD, SXWORD.
+
 *
+
 * The last element of a member list is the null element: `_,_'.
+
 */
+

+
define(`DEFINE_STRUCT',`define(`$1_DEF',shift($@))dnl')
+

+
DEFINE_STRUCT(`Elf32_Cap',
+
	``c_tag,	WORD',
+
	`c_un.c_val,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Cap',
+
	``c_tag,	XWORD',
+
	`c_un.c_val,	XWORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Dyn',
+
	``d_tag,	SWORD',
+
	`d_un.d_ptr,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Dyn',
+
	``d_tag,	SXWORD',
+
	`d_un.d_ptr,	XWORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Ehdr',
+
	``e_ident,	IDENT',
+
	`e_type, 	HALF',
+
	`e_machine,	HALF',
+
	`e_version,	WORD',
+
	`e_entry,	ADDR',
+
	`e_phoff,	OFF',
+
	`e_shoff,	OFF',
+
	`e_flags,	WORD',
+
	`e_ehsize,	HALF',
+
	`e_phentsize,	HALF',
+
	`e_phnum,	HALF',
+
	`e_shentsize,	HALF',
+
	`e_shnum,	HALF',
+
	`e_shstrndx,	HALF',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Ehdr',
+
	``e_ident,	IDENT',
+
	`e_type, 	HALF',
+
	`e_machine,	HALF',
+
	`e_version,	WORD',
+
	`e_entry,	ADDR',
+
	`e_phoff,	OFF',
+
	`e_shoff,	OFF',
+
	`e_flags,	WORD',
+
	`e_ehsize,	HALF',
+
	`e_phentsize,	HALF',
+
	`e_phnum,	HALF',
+
	`e_shentsize,	HALF',
+
	`e_shnum,	HALF',
+
	`e_shstrndx,	HALF',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Move',
+
	``m_value,	LWORD',
+
	`m_info,	WORD',
+
	`m_poffset,	WORD',
+
	`m_repeat,	HALF',
+
	`m_stride,	HALF',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Move',
+
	``m_value,	LWORD',
+
	`m_info,	XWORD',
+
	`m_poffset,	XWORD',
+
	`m_repeat,	HALF',
+
	`m_stride,	HALF',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Phdr',
+
	``p_type,	WORD',
+
	`p_offset,	OFF',
+
	`p_vaddr,	ADDR',
+
	`p_paddr,	ADDR',
+
	`p_filesz,	WORD',
+
	`p_memsz,	WORD',
+
	`p_flags,	WORD',
+
	`p_align,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Phdr',
+
	``p_type,	WORD',
+
	`p_flags,	WORD',
+
	`p_offset,	OFF',
+
	`p_vaddr,	ADDR',
+
	`p_paddr,	ADDR',
+
	`p_filesz,	XWORD',
+
	`p_memsz,	XWORD',
+
	`p_align,	XWORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Rel',
+
	``r_offset,	ADDR',
+
	`r_info,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Rel',
+
	``r_offset,	ADDR',
+
	`r_info,	XWORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Rela',
+
	``r_offset,	ADDR',
+
	`r_info,	WORD',
+
	`r_addend,	SWORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Rela',
+
	``r_offset,	ADDR',
+
	`r_info,	XWORD',
+
	`r_addend,	SXWORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Shdr',
+
	``sh_name,	WORD',
+
	`sh_type,	WORD',
+
	`sh_flags,	WORD',
+
	`sh_addr,	ADDR',
+
	`sh_offset,	OFF',
+
	`sh_size,	WORD',
+
	`sh_link,	WORD',
+
	`sh_info,	WORD',
+
	`sh_addralign,	WORD',
+
	`sh_entsize,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Shdr',
+
	``sh_name,	WORD',
+
	`sh_type,	WORD',
+
	`sh_flags,	XWORD',
+
	`sh_addr,	ADDR',
+
	`sh_offset,	OFF',
+
	`sh_size,	XWORD',
+
	`sh_link,	WORD',
+
	`sh_info,	WORD',
+
	`sh_addralign,	XWORD',
+
	`sh_entsize,	XWORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Sym',
+
	``st_name,	WORD',
+
	`st_value,	ADDR',
+
	`st_size,	WORD',
+
	`st_info,	BYTE',
+
	`st_other,	BYTE',
+
	`st_shndx,	HALF',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Sym',
+
	``st_name,	WORD',
+
	`st_info,	BYTE',
+
	`st_other,	BYTE',
+
	`st_shndx,	HALF',
+
	`st_value,	ADDR',
+
	`st_size,	XWORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Syminfo',
+
	``si_boundto,	HALF',
+
	`si_flags,	HALF',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Syminfo',
+
	``si_boundto,	HALF',
+
	`si_flags,	HALF',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Verdaux',
+
	``vda_name,	WORD',
+
	`vda_next,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Verdaux',
+
	``vda_name,	WORD',
+
	`vda_next,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Verdef',
+
	``vd_version,	HALF',
+
	`vd_flags,	HALF',
+
	`vd_ndx,	HALF',
+
	`vd_cnt,	HALF',
+
	`vd_hash,	WORD',
+
	`vd_aux,	WORD',
+
	`vd_next,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Verdef',
+
	``vd_version,	HALF',
+
	`vd_flags,	HALF',
+
	`vd_ndx,	HALF',
+
	`vd_cnt,	HALF',
+
	`vd_hash,	WORD',
+
	`vd_aux,	WORD',
+
	`vd_next,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Verneed',
+
	``vn_version,	HALF',
+
	`vn_cnt,	HALF',
+
	`vn_file,	WORD',
+
	`vn_aux,	WORD',
+
	`vn_next,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Verneed',
+
	``vn_version,	HALF',
+
	`vn_cnt,	HALF',
+
	`vn_file,	WORD',
+
	`vn_aux,	WORD',
+
	`vn_next,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf32_Vernaux',
+
	``vna_hash,	WORD',
+
	`vna_flags,	HALF',
+
	`vna_other,	HALF',
+
	`vna_name,	WORD',
+
	`vna_next,	WORD',
+
	`_,_'')
+

+
DEFINE_STRUCT(`Elf64_Vernaux',
+
	``vna_hash,	WORD',
+
	`vna_flags,	HALF',
+
	`vna_other,	HALF',
+
	`vna_name,	WORD',
+
	`vna_next,	WORD',
+
	`_,_'')
added external/libelf/elf_update.3
@@ -0,0 +1,286 @@
+
.\" Copyright (c) 2006,2007-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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/elf_update.3 226436 2011-10-16 14:30:28Z eadler $
+
.\"
+
.Dd March 19, 2008
+
.Dt ELF_UPDATE 3
+
.Os
+
.Sh NAME
+
.Nm elf_update
+
.Nd update an ELF descriptor
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft off_t
+
.Fn elf_update "Elf *elf" "Elf_Cmd cmd"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_update
+
causes the library to recalculate the structure of an ELF
+
object and optionally write out the image of the object
+
to file.
+
.Pp
+
Argument
+
.Ar elf
+
is a descriptor to an ELF object.
+
Argument
+
.Ar cmd
+
can take on the following values:
+
.Bl -tag -width "ELF_C_WRITE"
+
.It Dv ELF_C_NULL
+
The library will recalculate structural information flagging
+
modified structures with the
+
.Dv ELF_F_DIRTY
+
flag, but will not write back data to the underlying file image.
+
.It Dv ELF_C_WRITE
+
The library will recalculate structural information and will
+
also write the new image to the underlying file.
+
.El
+
.Ss File Layout
+
If the
+
.Dv ELF_F_LAYOUT
+
flag has been set on the ELF descriptor, the application assumes full
+
responsibility for the layout of the ELF object.
+
If this flag is not set, the ELF library will compute the layout of the
+
file from its associated section descriptors.
+
.Pp
+
It is the application's responsibility to manage the following
+
structure members in the ELF file:
+
.Bl -tag -width indent
+
.It "Executable Header"
+
The ELF executable header is described in
+
.Xr elf 5 .
+
The following members of the ELF executable header are the application's
+
responsibility:
+
.Pp
+
.Bl -tag -width "e_ident[EI_OSABI]" -compact
+
.It Va e_entry
+
Set to the desired entry address for executables.
+
.It Va e_flags
+
Set to the desired processor specific flags.
+
.It Va "e_ident[EI_DATA]"
+
Must be set to one of
+
.Dv ELFDATA2LSB
+
or
+
.Dv ELFDATA2MSB .
+
.It Va "e_ident[EI_OSABI]"
+
Set to the OS ABI desired.
+
For
+
.Fx
+
executables, this field should be set to
+
.Dv ELFOSABI_FREEBSD .
+
.It Va e_machine
+
Set to the desired machine architecture, one of the
+
.Dv EM_*
+
values in
+
.In sys/elf_common.h .
+
.It Va e_phoff
+
If the application is managing the object's layout, it must
+
set this field to the file offset of the ELF program header table.
+
.It Va e_shoff
+
If the application is managing the object's layout, it must
+
set this field to the file offset of the ELF section header table.
+
.It Va e_shstrndx
+
Set to the index of the string table containing
+
section names.
+
.It Va e_type
+
Set to the type of the ELF object, one of the
+
.Dv ET_*
+
values in
+
.In sys/elf_common.h .
+
.It Va e_version
+
Set to the desired version of the ELF object.
+
.El
+
.It "Program Header"
+
All fields of the entries in the program header table are
+
under application control.
+
.It "Section Header"
+
The ELF section header is described in
+
.Xr elf 5 .
+
The following members of the ELF section header are the
+
application's responsibility:
+
.Pp
+
.Bl -tag -width "sh_addralign" -compact
+
.It Va sh_addr
+
Set to the physical memory address where the section should reside.
+
.It Va sh_addralign
+
If the application is managing the file layout, it must set this
+
field to the desired alignment for the section's contents.
+
This value must be a power of two.
+
.It Va sh_entsize
+
Set to the size of each entry, for sections containing fixed size
+
elements, or set to zero for sections without fixed size elements.
+
If the application is not managing file layout, it may leave this
+
field as zero for those sections whose types known to the library.
+
.It Va sh_flags
+
Set to the desired section flags.
+
.It Va sh_info
+
Set as described in
+
.Xr elf 5 .
+
.It Va sh_link
+
Set as described in
+
.Xr elf 5 .
+
.It Va sh_name
+
Set to the index of the section's name in the string table containing
+
section names.
+
.It Va sh_offset
+
If the application is managing the file layout, it must set this
+
field to the file offset of the section's contents.
+
.It Va sh_size
+
If the application is managing the file layout, it must set this
+
field to the file size of the section's contents.
+
.It Va sh_type
+
Set to the type of the section.
+
.El
+
.El
+
.Pp
+
Gaps in the coverage of the file's contents will be set to the fill value
+
specified by
+
.Xr elf_fill 3 .
+
.Pp
+
If the application has requested full control over the file's layout by
+
setting the
+
.Dv ELF_F_LAYOUT
+
flag on the ELF descriptor, it should ensure that there are no
+
gaps in the coverage of the file's contents.
+
.Pp
+
All pointers to
+
.Vt Elf_Scn
+
and
+
.Vt Elf_Data
+
descriptors associated with descriptor
+
.Ar elf
+
should be considered as invalid after a call to
+
.Fn elf_update .
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_update
+
returns the total size of the file image if successful, or -1 if an
+
error occurred.
+
.Sh ERRORS
+
This function may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.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_ARGUMENT
+
The argument
+
.Ar elf
+
was not a descriptor for an ELF object.
+
.It Bq Er ELF_E_CLASS
+
The
+
.Va e_ident[EI_CLASS]
+
field of the executable header of argument
+
.Ar elf
+
did not match the class of the file.
+
.It Bq Er ELF_E_DATA
+
An
+
.Vt Elf_Data
+
descriptor contained in argument
+
.Ar elf
+
specified a type incompatible with its containing section.
+
.It Bq Er ELF_E_HEADER
+
The ELF header in argument
+
.Ar elf
+
requested a different byte order from the byte order already
+
associated with the file.
+
.It Bq Er ELF_E_IO
+
An I/O error was encountered.
+
.It Bq Er ELF_E_LAYOUT
+
An
+
.Vt Elf_Data
+
descriptor contained in argument
+
.Ar elf
+
specified an alignment incompatible with its containing section.
+
.It Bq Er ELF_E_LAYOUT
+
Argument
+
.Ar elf
+
contained section descriptors that overlapped in extent.
+
.It Bq Er ELF_E_LAYOUT
+
Argument
+
.Ar elf
+
contained section descriptors that were incorrectly aligned or were
+
too small for their data.
+
.It Bq Er ELF_E_LAYOUT
+
The flag
+
.Dv ELF_F_LAYOUT
+
was set on the Elf descriptor and the section header table overlapped
+
an extent in the object mapped by a section descriptor.
+
.It Bq Er ELF_E_MODE
+
An
+
.Dv ELF_C_WRITE
+
operation was requested with an ELF descriptor that was not opened for
+
writing or updating.
+
.It Bq Er ELF_E_SECTION
+
Argument
+
.Ar elf
+
contained a section with an unrecognized type.
+
.It Bq Er ELF_E_SECTION
+
The section header at index
+
.Dv SHN_UNDEF
+
had an illegal section type.
+
.It Bq Er ELF_E_SEQUENCE
+
An
+
.Dv ELF_C_WRITE
+
operation was requested after a prior call to
+
.Fn elf_cntl elf ELF_C_FDDONE
+
disassociated the ELF descriptor
+
.Ar elf
+
from its underlying file.
+
.It Bq Er ELF_E_VERSION
+
Argument
+
.Ar elf
+
had an unsupported version or contained an
+
.Vt Elf_Data
+
descriptor with an unsupported version.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf32_getphdr 3 ,
+
.Xr elf32_newehdr 3 ,
+
.Xr elf32_newphdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf64_getphdr 3 ,
+
.Xr elf64_newehdr 3 ,
+
.Xr elf64_newphdr 3 ,
+
.Xr elf_cntl 3 ,
+
.Xr elf_fill 3 ,
+
.Xr elf_flagehdr 3 ,
+
.Xr elf_flagelf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr elf_newdata 3 ,
+
.Xr elf_newscn 3 ,
+
.Xr elf_rawdata 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_newehdr 3 ,
+
.Xr gelf_newphdr 3 ,
+
.Xr elf 5
added external/libelf/elf_update.c
@@ -0,0 +1,914 @@
+
/*-
+
 * 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_update.c 212373 2010-09-09 18:51:50Z kaiw $");
+

+
#include <sys/mman.h>
+
#include <sys/param.h>
+

+
#include <assert.h>
+
#include <errno.h>
+
#include <gelf.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+
#include <string.h>
+
#include <unistd.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Update the internal data structures associated with an ELF object.
+
 * Returns the size in bytes the ELF object would occupy in its file
+
 * representation.
+
 *
+
 * After a successful call to this function, the following structures
+
 * are updated:
+
 *
+
 * - The ELF header is updated.
+
 * - All sections are sorted in order of ascending addresses and their
+
 *   section header table entries updated.   An error is signalled
+
 *   if an overlap was detected among sections.
+
 * - All data descriptors associated with a section are sorted in order
+
 *   of ascending addresses.  Overlaps, if detected, are signalled as
+
 *   errors.  Other sanity checks for alignments, section types etc. are
+
 *   made.
+
 *
+
 * After a resync_elf() successfully returns, the ELF descriptor is
+
 * ready for being handed over to _libelf_write_elf().
+
 *
+
 * File alignments:
+
 * PHDR - Addr
+
 * SHDR - Addr
+
 *
+
 * XXX: how do we handle 'flags'.
+
 */
+

+
/*
+
 * Compute the extents of a section, by looking at the data
+
 * descriptors associated with it.  The function returns zero if an
+
 * error was detected.  `*rc' holds the maximum file extent seen so
+
 * far.
+
 */
+
static int
+
_libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t *rc)
+
{
+
	int ec;
+
	Elf_Data *d, *td;
+
	unsigned int elftype;
+
	uint32_t sh_type;
+
	uint64_t d_align;
+
	uint64_t sh_align, sh_entsize, sh_offset, sh_size;
+
	uint64_t scn_size, scn_alignment;
+

+
	/*
+
	 * We need to recompute library private data structures if one
+
	 * or more of the following is true:
+
	 * - The underlying Shdr structure has been marked `dirty'.  Significant
+
	 *   fields include: `sh_offset', `sh_type', `sh_size', `sh_addralign'.
+
	 * - The Elf_Data structures part of this section have been marked
+
	 *   `dirty'.  Affected members include `d_align', `d_offset', `d_type',
+
	 *   and `d_size'.
+
	 * - The section as a whole is `dirty', e.g., it has been allocated
+
	 *   using elf_newscn(), or if a new Elf_Data structure was added using
+
	 *   elf_newdata().
+
	 *
+
	 * Each of these conditions would result in the ELF_F_DIRTY bit being
+
	 * set on the section descriptor's `s_flags' field.
+
	 */
+

+
	ec = e->e_class;
+

+
	if (ec == ELFCLASS32) {
+
		sh_type    = s->s_shdr.s_shdr32.sh_type;
+
		sh_align   = (uint64_t) s->s_shdr.s_shdr32.sh_addralign;
+
		sh_entsize = (uint64_t) s->s_shdr.s_shdr32.sh_entsize;
+
		sh_offset  = (uint64_t) s->s_shdr.s_shdr32.sh_offset;
+
		sh_size    = (uint64_t) s->s_shdr.s_shdr32.sh_size;
+
	} else {
+
		sh_type    = s->s_shdr.s_shdr64.sh_type;
+
		sh_align   = s->s_shdr.s_shdr64.sh_addralign;
+
		sh_entsize = s->s_shdr.s_shdr64.sh_entsize;
+
		sh_offset  = s->s_shdr.s_shdr64.sh_offset;
+
		sh_size    = s->s_shdr.s_shdr64.sh_size;
+
	}
+

+
	if (sh_type == SHT_NULL || sh_type == SHT_NOBITS)
+
		return (1);
+

+
	if ((s->s_flags & ELF_F_DIRTY) == 0) {
+
		if ((size_t) *rc < sh_offset + sh_size)
+
			*rc = sh_offset + sh_size;
+
		return (1);
+
	}
+

+
	elftype = _libelf_xlate_shtype(sh_type);
+
	if (elftype > ELF_T_LAST) {
+
		LIBELF_SET_ERROR(SECTION, 0);
+
		return (0);
+
	}
+

+
	/*
+
	 * Compute the extent of the data descriptors associated with
+
	 * this section.
+
	 */
+
	scn_alignment = 0;
+
	if (sh_align == 0)
+
		sh_align = _libelf_falign(elftype, ec);
+

+
	/* Compute the section alignment. */
+
	STAILQ_FOREACH(d, &s->s_data, d_next)  {
+
		if (d->d_type > ELF_T_LAST) {
+
			LIBELF_SET_ERROR(DATA, 0);
+
			return (0);
+
		}
+
		if (d->d_version != e->e_version) {
+
			LIBELF_SET_ERROR(VERSION, 0);
+
			return (0);
+
		}
+
		if ((d_align = d->d_align) == 0 || (d_align & (d_align - 1))) {
+
			LIBELF_SET_ERROR(DATA, 0);
+
			return (0);
+
		}
+
		if (d_align > scn_alignment)
+
			scn_alignment = d_align;
+
	}
+

+
	scn_size = 0L;
+

+
	STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
+
		if (e->e_flags & ELF_F_LAYOUT) {
+
			if ((uint64_t) d->d_off + d->d_size > scn_size)
+
				scn_size = d->d_off + d->d_size;
+
		} else {
+
			scn_size = roundup2(scn_size, d->d_align);
+
			d->d_off = scn_size;
+
			scn_size += d->d_size;
+
		}
+
	}
+

+
	/*
+
	 * If the application is requesting full control over the layout
+
	 * of the section, check its values for sanity.
+
	 */
+
	if (e->e_flags & ELF_F_LAYOUT) {
+
		if (scn_alignment > sh_align || sh_offset % sh_align ||
+
		    sh_size < scn_size) {
+
			LIBELF_SET_ERROR(LAYOUT, 0);
+
			return (0);
+
		}
+
	} else {
+
		/*
+
		 * Otherwise compute the values in the section header.
+
		 */
+

+
		if (scn_alignment > sh_align)
+
			sh_align = scn_alignment;
+

+
		/*
+
		 * If the section entry size is zero, try and fill in an
+
		 * appropriate entry size.  Per the elf(5) manual page
+
		 * sections without fixed-size entries should have their
+
		 * 'sh_entsize' field set to zero.
+
		 */
+
		if (sh_entsize == 0 &&
+
		    (sh_entsize = _libelf_fsize(elftype, ec, e->e_version,
+
		    (size_t) 1)) == 1)
+
			sh_entsize = 0;
+

+
		sh_size = scn_size;
+
		sh_offset = roundup(*rc, sh_align);
+

+
		if (ec == ELFCLASS32) {
+
			s->s_shdr.s_shdr32.sh_addralign = (uint32_t) sh_align;
+
			s->s_shdr.s_shdr32.sh_entsize   = (uint32_t) sh_entsize;
+
			s->s_shdr.s_shdr32.sh_offset    = (uint32_t) sh_offset;
+
			s->s_shdr.s_shdr32.sh_size      = (uint32_t) sh_size;
+
		} else {
+
			s->s_shdr.s_shdr64.sh_addralign = sh_align;
+
			s->s_shdr.s_shdr64.sh_entsize   = sh_entsize;
+
			s->s_shdr.s_shdr64.sh_offset    = sh_offset;
+
			s->s_shdr.s_shdr64.sh_size      = sh_size;
+
		}
+
	}
+

+
	if ((size_t) *rc < sh_offset + sh_size)
+
		*rc = sh_offset + sh_size;
+

+
	s->s_size = sh_size;
+
	s->s_offset = sh_offset;
+
	return (1);
+
}
+

+

+
/*
+
 * Insert a section in ascending order in the list
+
 */
+

+
static int
+
_libelf_insert_section(Elf *e, Elf_Scn *s)
+
{
+
	Elf_Scn *t, *prevt;
+
	uint64_t smax, smin, tmax, tmin;
+

+
	smin = s->s_offset;
+
	smax = smin + s->s_size;
+

+
	prevt = NULL;
+
	STAILQ_FOREACH(t, &e->e_u.e_elf.e_scn, s_next) {
+
		tmin = t->s_offset;
+
		tmax = tmin + t->s_size;
+

+
		if (tmax <= smin) {
+
			/*
+
			 * 't' lies entirely before 's': ...| t |...| s |...
+
			 */
+
			prevt = t;
+
			continue;
+
		} else if (smax <= tmin)
+
			/*
+
			 * 's' lies entirely before 't', and after 'prevt':
+
			 *      ...| prevt |...| s |...| t |...
+
			 */
+
			break;
+
		else {	/* 's' and 't' overlap. */
+
			LIBELF_SET_ERROR(LAYOUT, 0);
+
			return (0);
+
		}
+
	}
+

+
	if (prevt)
+
		STAILQ_INSERT_AFTER(&e->e_u.e_elf.e_scn, prevt, s, s_next);
+
	else
+
		STAILQ_INSERT_HEAD(&e->e_u.e_elf.e_scn, s, s_next);
+
	return (1);
+
}
+

+
static off_t
+
_libelf_resync_sections(Elf *e, off_t rc)
+
{
+
	int ec;
+
	off_t nrc;
+
	size_t sh_type, shdr_start, shdr_end;
+
	Elf_Scn *s, *ts;
+

+
	ec = e->e_class;
+

+
	/*
+
	 * Make a pass through sections, computing the extent of each
+
	 * section. Order in increasing order of addresses.
+
	 */
+

+
	nrc = rc;
+
	STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next)
+
		if (_libelf_compute_section_extents(e, s, &nrc) == 0)
+
			return ((off_t) -1);
+

+
	STAILQ_FOREACH_SAFE(s, &e->e_u.e_elf.e_scn, s_next, ts) {
+
		if (ec == ELFCLASS32)
+
			sh_type = s->s_shdr.s_shdr32.sh_type;
+
		else
+
			sh_type = s->s_shdr.s_shdr64.sh_type;
+

+
		if (sh_type == SHT_NOBITS || sh_type == SHT_NULL)
+
			continue;
+

+
		if (s->s_offset < (uint64_t) rc) {
+
			if (s->s_offset + s->s_size < (uint64_t) rc) {
+
				/*
+
				 * Try insert this section in the
+
				 * correct place in the list,
+
				 * detecting overlaps if any.
+
				 */
+
				STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn,
+
				    s_next);
+
				if (_libelf_insert_section(e, s) == 0)
+
					return ((off_t) -1);
+
			} else {
+
				LIBELF_SET_ERROR(LAYOUT, 0);
+
				return ((off_t) -1);
+
			}
+
		} else
+
			rc = s->s_offset + s->s_size;
+
	}
+

+
	/*
+
	 * If the application is controlling file layout, check for an
+
	 * overlap between this section's extents and the SHDR table.
+
	 */
+
	if (e->e_flags & ELF_F_LAYOUT) {
+

+
		if (e->e_class == ELFCLASS32)
+
			shdr_start = e->e_u.e_elf.e_ehdr.e_ehdr32->e_shoff;
+
		else
+
			shdr_start = e->e_u.e_elf.e_ehdr.e_ehdr64->e_shoff;
+

+
		shdr_end = shdr_start + _libelf_fsize(ELF_T_SHDR, e->e_class,
+
		    e->e_version, e->e_u.e_elf.e_nscn);
+

+
		STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) {
+
			if (s->s_offset >= shdr_end ||
+
			    s->s_offset + s->s_size <= shdr_start)
+
				continue;
+
			LIBELF_SET_ERROR(LAYOUT, 0);
+
			return ((off_t) -1);
+
		}
+
	}
+

+
	assert(nrc == rc);
+

+
	return (rc);
+
}
+

+
static off_t
+
_libelf_resync_elf(Elf *e)
+
{
+
	int ec, eh_class, eh_type;
+
	unsigned int eh_byteorder, eh_version;
+
	size_t align, fsz;
+
	size_t phnum, shnum;
+
	off_t rc, phoff, shoff;
+
	void *ehdr;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+

+
	rc = 0;
+

+
	ec = e->e_class;
+

+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	/*
+
	 * Prepare the EHDR.
+
	 */
+
	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
+
		return ((off_t) -1);
+

+
	eh32 = ehdr;
+
	eh64 = ehdr;
+

+
	if (ec == ELFCLASS32) {
+
		eh_byteorder = eh32->e_ident[EI_DATA];
+
		eh_class     = eh32->e_ident[EI_CLASS];
+
		phoff        = (uint64_t) eh32->e_phoff;
+
		shoff        = (uint64_t) eh32->e_shoff;
+
		eh_type      = eh32->e_type;
+
		eh_version   = eh32->e_version;
+
	} else {
+
		eh_byteorder = eh64->e_ident[EI_DATA];
+
		eh_class     = eh64->e_ident[EI_CLASS];
+
		phoff        = eh64->e_phoff;
+
		shoff        = eh64->e_shoff;
+
		eh_type      = eh64->e_type;
+
		eh_version   = eh64->e_version;
+
	}
+

+
	if (eh_version == EV_NONE)
+
		eh_version = EV_CURRENT;
+

+
	if (eh_version != e->e_version) {	/* always EV_CURRENT */
+
		LIBELF_SET_ERROR(VERSION, 0);
+
		return ((off_t) -1);
+
	}
+

+
	if (eh_class != e->e_class) {
+
		LIBELF_SET_ERROR(CLASS, 0);
+
		return ((off_t) -1);
+
	}
+

+
	if (e->e_cmd != ELF_C_WRITE && eh_byteorder != e->e_byteorder) {
+
		LIBELF_SET_ERROR(HEADER, 0);
+
		return ((off_t) -1);
+
	}
+

+
	shnum = e->e_u.e_elf.e_nscn;
+
	phnum = e->e_u.e_elf.e_nphdr;
+

+
	e->e_byteorder = eh_byteorder;
+

+
#define	INITIALIZE_EHDR(E,EC,V)	do {					\
+
		(E)->e_ident[EI_MAG0] = ELFMAG0;			\
+
		(E)->e_ident[EI_MAG1] = ELFMAG1;			\
+
		(E)->e_ident[EI_MAG2] = ELFMAG2;			\
+
		(E)->e_ident[EI_MAG3] = ELFMAG3;			\
+
		(E)->e_ident[EI_CLASS] = (EC);				\
+
		(E)->e_ident[EI_VERSION] = (V);				\
+
		(E)->e_ehsize = _libelf_fsize(ELF_T_EHDR, (EC), (V),	\
+
		    (size_t) 1);					\
+
		(E)->e_phentsize = (phnum == 0) ? 0 : _libelf_fsize(	\
+
		    ELF_T_PHDR, (EC), (V), (size_t) 1);			\
+
		(E)->e_shentsize = _libelf_fsize(ELF_T_SHDR, (EC), (V),	\
+
		    (size_t) 1);					\
+
	} while (0)
+

+
	if (ec == ELFCLASS32)
+
		INITIALIZE_EHDR(eh32, ec, eh_version);
+
	else
+
		INITIALIZE_EHDR(eh64, ec, eh_version);
+

+
	(void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
+

+
	rc += _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1);
+

+
	/*
+
	 * Compute the layout the program header table, if one is
+
	 * present.  The program header table needs to be aligned to a
+
	 * `natural' boundary.
+
	 */
+
	if (phnum) {
+
		fsz = _libelf_fsize(ELF_T_PHDR, ec, eh_version, phnum);
+
		align = _libelf_falign(ELF_T_PHDR, ec);
+

+
		if (e->e_flags & ELF_F_LAYOUT) {
+
			/*
+
			 * Check offsets for sanity.
+
			 */
+
			if (rc > phoff) {
+
				LIBELF_SET_ERROR(HEADER, 0);
+
				return ((off_t) -1);
+
			}
+

+
			if (phoff % align) {
+
				LIBELF_SET_ERROR(LAYOUT, 0);
+
				return ((off_t) -1);
+
			}
+

+
		} else
+
			phoff = roundup(rc, align);
+

+
		rc = phoff + fsz;
+
	} else
+
		phoff = 0;
+

+
	/*
+
	 * Compute the layout of the sections associated with the
+
	 * file.
+
	 */
+

+
	if (e->e_cmd != ELF_C_WRITE &&
+
	    (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 &&
+
	    _libelf_load_scn(e, ehdr) == 0)
+
		return ((off_t) -1);
+

+
	if ((rc = _libelf_resync_sections(e, rc)) < 0)
+
		return ((off_t) -1);
+

+
	/*
+
	 * Compute the space taken up by the section header table, if
+
	 * one is needed.  If ELF_F_LAYOUT is asserted, the
+
	 * application may have placed the section header table in
+
	 * between existing sections, so the net size of the file need
+
	 * not increase due to the presence of the section header
+
	 * table.
+
	 */
+
	if (shnum) {
+
		fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, (size_t) 1);
+
		align = _libelf_falign(ELF_T_SHDR, ec);
+

+
		if (e->e_flags & ELF_F_LAYOUT) {
+
			if (shoff % align) {
+
				LIBELF_SET_ERROR(LAYOUT, 0);
+
				return ((off_t) -1);
+
			}
+
		} else
+
			shoff = roundup(rc, align);
+

+
		if (shoff + fsz * shnum > (size_t) rc)
+
			rc = shoff + fsz * shnum;
+
	} else
+
		shoff = 0;
+

+
	/*
+
	 * Set the fields of the Executable Header that could potentially use
+
	 * extended numbering.
+
	 */
+
	_libelf_setphnum(e, ehdr, ec, phnum);
+
	_libelf_setshnum(e, ehdr, ec, shnum);
+

+
	/*
+
	 * Update the `e_phoff' and `e_shoff' fields if the library is
+
	 * doing the layout.
+
	 */
+
	if ((e->e_flags & ELF_F_LAYOUT) == 0) {
+
		if (ec == ELFCLASS32) {
+
			eh32->e_phoff = (uint32_t) phoff;
+
			eh32->e_shoff = (uint32_t) shoff;
+
		} else {
+
			eh64->e_phoff = (uint64_t) phoff;
+
			eh64->e_shoff = (uint64_t) shoff;
+
		}
+
	}
+

+
	return (rc);
+
}
+

+
/*
+
 * Write out the contents of a section.
+
 */
+

+
static off_t
+
_libelf_write_scn(Elf *e, char *nf, Elf_Scn *s, off_t rc)
+
{
+
	int ec;
+
	size_t fsz, msz, nobjects;
+
	uint32_t sh_type;
+
	uint64_t sh_off, sh_size;
+
	int elftype;
+
	Elf_Data *d, dst;
+

+
	if ((ec = e->e_class) == ELFCLASS32) {
+
		sh_type = s->s_shdr.s_shdr32.sh_type;
+
		sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size;
+
	} else {
+
		sh_type = s->s_shdr.s_shdr64.sh_type;
+
		sh_size = s->s_shdr.s_shdr64.sh_size;
+
	}
+

+
	/*
+
	 * Ignore sections that do not allocate space in the file.
+
	 */
+
	if (sh_type == SHT_NOBITS || sh_type == SHT_NULL || sh_size == 0)
+
		return (rc);
+

+
	elftype = _libelf_xlate_shtype(sh_type);
+
	assert(elftype >= ELF_T_FIRST && elftype <= ELF_T_LAST);
+

+
	sh_off = s->s_offset;
+
	assert(sh_off % _libelf_falign(elftype, ec) == 0);
+

+
	/*
+
	 * If the section has a `rawdata' descriptor, and the section
+
	 * contents have not been modified, use its contents directly.
+
	 * The `s_rawoff' member contains the offset into the original
+
	 * file, while `s_offset' contains its new location in the
+
	 * destination.
+
	 */
+

+
	if (STAILQ_EMPTY(&s->s_data)) {
+

+
		if ((d = elf_rawdata(s, NULL)) == NULL)
+
			return ((off_t) -1);
+

+
		STAILQ_FOREACH(d, &s->s_rawdata, d_next) {
+
			if ((uint64_t) rc < sh_off + d->d_off)
+
				(void) memset(nf + rc,
+
				    LIBELF_PRIVATE(fillchar), sh_off +
+
				    d->d_off - rc);
+
			rc = sh_off + d->d_off;
+

+
			assert(d->d_buf != NULL);
+
			assert(d->d_type == ELF_T_BYTE);
+
			assert(d->d_version == e->e_version);
+

+
			(void) memcpy(nf + rc,
+
			    e->e_rawfile + s->s_rawoff + d->d_off, d->d_size);
+

+
			rc += d->d_size;
+
		}
+

+
		return (rc);
+
	}
+

+
	/*
+
	 * Iterate over the set of data descriptors for this section.
+
	 * The prior call to _libelf_resync_elf() would have setup the
+
	 * descriptors for this step.
+
	 */
+

+
	dst.d_version = e->e_version;
+

+
	STAILQ_FOREACH(d, &s->s_data, d_next) {
+

+
		msz = _libelf_msize(d->d_type, ec, e->e_version);
+

+
		if ((uint64_t) rc < sh_off + d->d_off)
+
			(void) memset(nf + rc,
+
			    LIBELF_PRIVATE(fillchar), sh_off + d->d_off - rc);
+

+
		rc = sh_off + d->d_off;
+

+
		assert(d->d_buf != NULL);
+
		assert(d->d_version == e->e_version);
+
		assert(d->d_size % msz == 0);
+

+
		nobjects = d->d_size / msz;
+

+
		fsz = _libelf_fsize(d->d_type, ec, e->e_version, nobjects);
+

+
		dst.d_buf    = nf + rc;
+
		dst.d_size   = fsz;
+

+
		if (_libelf_xlate(&dst, d, e->e_byteorder, ec, ELF_TOFILE) ==
+
		    NULL)
+
			return ((off_t) -1);
+

+
		rc += fsz;
+
	}
+

+
	return ((off_t) rc);
+
}
+

+
/*
+
 * Write out the file image.
+
 *
+
 * The original file could have been mapped in with an ELF_C_RDWR
+
 * command and the application could have added new content or
+
 * re-arranged its sections before calling elf_update().  Consequently
+
 * its not safe to work `in place' on the original file.  So we
+
 * malloc() the required space for the updated ELF object and build
+
 * the object there and write it out to the underlying file at the
+
 * end.  Note that the application may have opened the underlying file
+
 * in ELF_C_RDWR and only retrieved/modified a few sections.  We take
+
 * care to avoid translating file sections unnecessarily.
+
 *
+
 * Gaps in the coverage of the file by the file's sections will be
+
 * filled with the fill character set by elf_fill(3).
+
 */
+

+
static off_t
+
_libelf_write_elf(Elf *e, off_t newsize)
+
{
+
	int ec;
+
	off_t maxrc, rc;
+
	size_t fsz, msz, phnum, shnum;
+
	uint64_t phoff, shoff;
+
	void *ehdr;
+
	char *newfile;
+
	Elf_Data dst, src;
+
	Elf_Scn *scn, *tscn;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+

+
	assert(e->e_kind == ELF_K_ELF);
+
	assert(e->e_cmd != ELF_C_READ);
+
	assert(e->e_fd >= 0);
+

+
	if ((newfile = malloc((size_t) newsize)) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, errno);
+
		return ((off_t) -1);
+
	}
+

+
	ec = e->e_class;
+

+
	ehdr = _libelf_ehdr(e, ec, 0);
+
	assert(ehdr != NULL);
+

+
	phnum = e->e_u.e_elf.e_nphdr;
+

+
	if (ec == ELFCLASS32) {
+
		eh32 = (Elf32_Ehdr *) ehdr;
+

+
		phoff = (uint64_t) eh32->e_phoff;
+
		shnum = eh32->e_shnum;
+
		shoff = (uint64_t) eh32->e_shoff;
+
	} else {
+
		eh64 = (Elf64_Ehdr *) ehdr;
+

+
		phoff = eh64->e_phoff;
+
		shnum = eh64->e_shnum;
+
		shoff = eh64->e_shoff;
+
	}
+

+
	fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
+
	msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version);
+

+
	(void) memset(&dst, 0, sizeof(dst));
+
	(void) memset(&src, 0, sizeof(src));
+

+
	src.d_buf     = ehdr;
+
	src.d_size    = msz;
+
	src.d_type    = ELF_T_EHDR;
+
	src.d_version = dst.d_version = e->e_version;
+

+
	rc = 0;
+

+
	dst.d_buf     = newfile + rc;
+
	dst.d_size    = fsz;
+

+
	if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) ==
+
	    NULL)
+
		goto error;
+

+
	rc += fsz;
+

+
	/*
+
	 * Write the program header table if present.
+
	 */
+

+
	if (phnum != 0 && phoff != 0) {
+
		assert((unsigned) rc <= phoff);
+

+
		fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum);
+

+
		assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0);
+
		assert(fsz > 0);
+

+
		src.d_buf = _libelf_getphdr(e, ec);
+
		src.d_version = dst.d_version = e->e_version;
+
		src.d_type = ELF_T_PHDR;
+
		src.d_size = phnum * _libelf_msize(ELF_T_PHDR, ec,
+
		    e->e_version);
+

+
		dst.d_size = fsz;
+

+
		if ((uint64_t) rc < phoff)
+
			(void) memset(newfile + rc,
+
			    LIBELF_PRIVATE(fillchar), phoff - rc);
+

+
		dst.d_buf = newfile + rc;
+

+
		if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) ==
+
		    NULL)
+
			goto error;
+

+
		rc = phoff + fsz;
+
	}
+

+
	/*
+
	 * Write out individual sections.
+
	 */
+

+
	STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next)
+
 		if ((rc = _libelf_write_scn(e, newfile, scn, rc)) < 0)
+
			goto error;
+

+
	/*
+
	 * Write out the section header table, if required.  Note that
+
	 * if flag ELF_F_LAYOUT has been set the section header table
+
	 * could reside in between byte ranges mapped by section
+
	 * descriptors.
+
	 */
+
	if (shnum != 0 && shoff != 0) {
+
		if ((uint64_t) rc < shoff)
+
			(void) memset(newfile + rc,
+
			    LIBELF_PRIVATE(fillchar), shoff - rc);
+

+
		maxrc = rc;
+
		rc = shoff;
+

+
		assert(rc % _libelf_falign(ELF_T_SHDR, ec) == 0);
+

+
		src.d_type = ELF_T_SHDR;
+
		src.d_size = _libelf_msize(ELF_T_SHDR, ec, e->e_version);
+
		src.d_version = dst.d_version = e->e_version;
+

+
		fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1);
+

+
		STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next) {
+
			if (ec == ELFCLASS32)
+
				src.d_buf = &scn->s_shdr.s_shdr32;
+
			else
+
				src.d_buf = &scn->s_shdr.s_shdr64;
+

+
			dst.d_size = fsz;
+
			dst.d_buf = newfile + rc + scn->s_ndx * fsz;
+

+
			if (_libelf_xlate(&dst, &src, e->e_byteorder, ec,
+
				ELF_TOFILE) != &dst)
+
				goto error;
+
		}
+

+
		rc += e->e_u.e_elf.e_nscn * fsz;
+
		if (maxrc > rc)
+
			rc = maxrc;
+
	}
+

+
	assert(rc == newsize);
+

+
	/*
+
	 * Write out the constructed contents and remap the file in
+
	 * read-only.
+
	 */
+

+
	if (e->e_rawfile && munmap(e->e_rawfile, e->e_rawsize) < 0) {
+
		LIBELF_SET_ERROR(IO, errno);
+
		goto error;
+
	}
+

+
	if (write(e->e_fd, newfile, (size_t) newsize) != newsize ||
+
	    lseek(e->e_fd, (off_t) 0, SEEK_SET) < 0) {
+
		LIBELF_SET_ERROR(IO, errno);
+
		goto error;
+
	}
+

+
	if (e->e_cmd != ELF_C_WRITE) {
+
		if ((e->e_rawfile = mmap(NULL, (size_t) newsize, PROT_READ,
+
		    MAP_PRIVATE, e->e_fd, (off_t) 0)) == MAP_FAILED) {
+
			LIBELF_SET_ERROR(IO, errno);
+
			goto error;
+
		}
+
		e->e_rawsize = newsize;
+
	}
+

+
	/*
+
	 * Reset flags, remove existing section descriptors and
+
	 * {E,P}HDR pointers so that a subsequent elf_get{e,p}hdr()
+
	 * and elf_getscn() will function correctly.
+
	 */
+

+
	e->e_flags &= ~ELF_F_DIRTY;
+

+
	STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn)
+
		_libelf_release_scn(scn);
+

+
	if (ec == ELFCLASS32) {
+
		free(e->e_u.e_elf.e_ehdr.e_ehdr32);
+
		if (e->e_u.e_elf.e_phdr.e_phdr32)
+
			free(e->e_u.e_elf.e_phdr.e_phdr32);
+

+
		e->e_u.e_elf.e_ehdr.e_ehdr32 = NULL;
+
		e->e_u.e_elf.e_phdr.e_phdr32 = NULL;
+
	} else {
+
		free(e->e_u.e_elf.e_ehdr.e_ehdr64);
+
		if (e->e_u.e_elf.e_phdr.e_phdr64)
+
			free(e->e_u.e_elf.e_phdr.e_phdr64);
+

+
		e->e_u.e_elf.e_ehdr.e_ehdr64 = NULL;
+
		e->e_u.e_elf.e_phdr.e_phdr64 = NULL;
+
	}
+

+
	free(newfile);
+

+
	return (rc);
+

+
 error:
+
	free(newfile);
+

+
	return ((off_t) -1);
+
}
+

+
off_t
+
elf_update(Elf *e, Elf_Cmd c)
+
{
+
	int ec;
+
	off_t rc;
+

+
	rc = (off_t) -1;
+

+
	if (e == NULL || e->e_kind != ELF_K_ELF ||
+
	    (c != ELF_C_NULL && c != ELF_C_WRITE)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (rc);
+
	}
+

+
	if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) {
+
		LIBELF_SET_ERROR(CLASS, 0);
+
		return (rc);
+
	}
+

+
	if (e->e_version == EV_NONE)
+
		e->e_version = EV_CURRENT;
+

+
	if (c == ELF_C_WRITE && e->e_cmd == ELF_C_READ) {
+
		LIBELF_SET_ERROR(MODE, 0);
+
		return (rc);
+
	}
+

+
	if ((rc = _libelf_resync_elf(e)) < 0)
+
		return (rc);
+

+
	if (c == ELF_C_NULL)
+
		return (rc);
+

+
	if (e->e_cmd == ELF_C_READ) {
+
		/*
+
		 * This descriptor was opened in read-only mode or by
+
		 * elf_memory().
+
		 */
+
		if (e->e_fd)
+
			LIBELF_SET_ERROR(MODE, 0);
+
		else
+
			LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return ((off_t) -1);
+
	}
+

+
	if (e->e_fd < 0) {
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		return ((off_t) -1);
+
	}
+

+
	return (_libelf_write_elf(e, rc));
+
}
added external/libelf/elf_version.3
@@ -0,0 +1,95 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/elf_version.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd June 1, 2006
+
.Dt ELF_VERSION 3
+
.Os
+
.Sh NAME
+
.Nm elf_version
+
.Nd retrieve or set ELF library operating version
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft unsigned int
+
.Fn elf_version "unsigned int version"
+
.Sh DESCRIPTION
+
The
+
.Fn elf_version
+
function is used to query the current operating version of the ELF
+
library, and to inform the ELF library about the application's desired
+
operating version.
+
.Pp
+
If the argument
+
.Ar version
+
is
+
.Dv EV_NONE ,
+
the
+
.Fn elf_version
+
function returns the currently configured operating version for the
+
ELF library.
+
.Pp
+
If the argument
+
.Ar version
+
is not
+
.Dv EV_NONE ,
+
and if argument
+
.Ar version
+
is supported by the ELF library, function
+
.Fn elf_version
+
sets the library's operating version to
+
.Ar version ,
+
and returns the previous value of the operating version.
+
If argument
+
.Ar version
+
cannot be supported, then the
+
.Fn elf_version
+
function returns
+
.Dv EV_NONE .
+
.Sh RETURN VALUES
+
The
+
.Fn elf_version
+
function returns the currently configured ELF library version, or
+
.Dv EV_NONE
+
if an unsupported version is requested.
+
.Sh EXAMPLES
+
An application program would inform the ELF library about its desired
+
operating version and check for an error using the following code
+
snippet:
+
.Bd -literal -offset indent
+
if (elf_version(EV_CURRENT) == EV_NONE)
+
	err(EX_SOFTWARE, "ELF library too old");
+
.Ed
+
.Sh ERRORS
+
Function
+
.Fn elf_version
+
may fail with the following error:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er "ELF_E_VERSION"
+
An unsupported library version number was requested.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr gelf 3
added external/libelf/elf_version.c
@@ -0,0 +1,51 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/elf_version.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
unsigned int
+
elf_version(unsigned int v)
+
{
+
	unsigned int old;
+

+
	if ((old = LIBELF_PRIVATE(version)) == EV_NONE)
+
		old = EV_CURRENT;
+

+
	if (v == EV_NONE)
+
		return old;
+
	if (v > EV_CURRENT) {
+
		LIBELF_SET_ERROR(VERSION, 0);
+
		return EV_NONE;
+
	}
+

+
	LIBELF_PRIVATE(version) = v;
+
	return (old);
+
}
added external/libelf/gelf.3
@@ -0,0 +1,201 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/gelf.3 236438 2012-06-02 08:47:26Z joel $
+
.\"
+
.Dd September 1, 2006
+
.Dt GELF 3
+
.Os
+
.Sh NAME
+
.Nm GElf
+
.Nd class-independent API for ELF manipulation
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Sh DESCRIPTION
+
This manual page describes a class independent API for manipulating
+
ELF objects.
+
This API allows an application to operate on ELF descriptors without
+
needing to the know the ELF class of the descriptor.
+
.Pp
+
The GElf API may be used alongside the ELF API without restriction.
+
.Ss GElf Data Structures
+
The GElf API defines the following class-independent data structures:
+
.Bl -tag -width GElf_Sxword
+
.It Vt GElf_Addr
+
A representation of ELF addresses.
+
.It Vt GElf_Dyn
+
A class-independent representation of ELF
+
.Sy .dynamic
+
section entries.
+
.It Vt GElf_Ehdr
+
A class-independent representation of an ELF Executable Header.
+
.It Vt GElf_Half
+
An unsigned 16 bit quantity.
+
.It Vt GElf_Off
+
A class-independent representation of a ELF offset.
+
.It Vt GElf_Phdr
+
A class-independent representation of an ELF Program Header Table
+
entry.
+
.It Vt GElf_Rel
+
A class-independent representation of an ELF relocation entry.
+
.It Vt GElf_Rela
+
A class-independent representation of an ELF relocation entry with
+
addend.
+
.It Vt GElf_Shdr
+
A class-independent representation of an ELF Section Header Table
+
entry.
+
.It Vt GElf_Sword
+
A signed 32 bit quantity.
+
.It Vt GElf_Sxword
+
A signed 64 bit quantity.
+
.It Vt GElf_Sym
+
A class-independent representation of an ELF symbol table entry.
+
.It Vt GElf_Word
+
An unsigned 32 bit quantity.
+
.It Vt GElf_Xword
+
An unsigned 64 bit quantity.
+
.El
+
.Pp
+
These data structures are sized to be compatible with the
+
corresponding 64 bit ELF structures, and have the same internal
+
structure as their 64 bit class-dependent counterparts.
+
Class-dependent ELF structures are described in
+
.Xr elf 5 .
+
.Ss GElf Programming Model
+
GElf functions always return a
+
.Em copy
+
of the underlying (class-dependent) ELF data structure.
+
The programming model with GElf is as follows:
+
.Bl -enum
+
.It
+
An application will retrieve data from an ELF descriptor using a
+
.Fn gelf_get_*
+
function.
+
This will copy out data into a private
+
.Vt GElf_*
+
data structure.
+
.It
+
The application will work with its private copy of the GElf
+
structure.
+
.It
+
Once done, the application copies the new values back to the
+
underlying ELF data structure using the
+
.Fn gelf_update_*
+
functions.
+
.It
+
The application will then use the
+
.Fn elf_flag*
+
APIs to indicate to the ELF library that an ELF data structure is dirty.
+
.El
+
.Pp
+
When updating an underlying 32 bit ELF data structure, the GElf
+
routines will signal an error if a GElf value is out of range
+
for the underlying ELF data type.
+
.Ss Namespace use
+
The GElf interface uses the following symbols:
+
.Bl -tag -width 8n
+
.It GElf_*
+
Class-independent data types.
+
.It gelf_*
+
For functions defined in the API set.
+
.El
+
.Ss GElf Programming APIs
+
This section provides an overview of the GElf programming APIs.
+
Further information is provided in the manual page of each function
+
listed here.
+
.Bl -tag -width 2n
+
.It "Allocating ELF Data Structures"
+
.Bl -tag -width 19n -compact
+
.It Fn gelf_newehdr
+
Allocate a new ELF Executable Header.
+
.It Fn gelf_newphdr
+
Allocate a new ELF Program Header Table.
+
.El
+
.It "Data Translation"
+
.Bl -tag -width 19n -compact
+
.It Fn gelf_xlatetof
+
Translate the native representation of an ELF data structure to its
+
file representation.
+
.It Fn gelf_xlatetom
+
Translate from the file representation of an ELF data structure to a
+
native representation.
+
.El
+
.It "Retrieving ELF Data"
+
.Bl -tag -width 19n -compact
+
.It Fn gelf_getdyn
+
Retrieve an ELF
+
.Sy .dynamic
+
table entry.
+
.It Fn gelf_getehdr
+
Retrieve an ELF Executable Header from the underlying ELF descriptor.
+
.It Fn gelf_getphdr
+
Retrieve an ELF Program Header Table entry from the underlying ELF descriptor.
+
.It Fn gelf_getrel
+
Retrieve an ELF relocation entry.
+
.It Fn gelf_getrela
+
Retrieve an ELF relocation entry with addend.
+
.It Fn gelf_getshdr
+
Retrieve an ELF Section Header Table entry from the underlying ELF descriptor.
+
.It Fn gelf_getsym
+
Retrieve an ELF symbol table entry.
+
.El
+
.It Queries
+
.Bl -tag -width 19n -compact
+
.It Fn gelf_checksum
+
Retrieves the ELF checksum for an ELF descriptor.
+
.It Fn gelf_fsize
+
Retrieves the size of the file representation of an ELF type.
+
.It Fn gelf_getclass
+
Retrieves the ELF class of an ELF descriptor.
+
.El
+
.It "Updating ELF Data"
+
.Bl -tag -width 19n -compact
+
.It Fn gelf_update_dyn
+
Copy back an ELF
+
.Sy .dynamic
+
Table entry.
+
.It Fn gelf_update_phdr
+
Copy back an ELF Program Header Table entry.
+
.It Fn gelf_update_rel
+
Copy back an ELF relocation entry.
+
.It Fn gelf_update_rela
+
Copy back an ELF relocation with addend entry.
+
.It Fn gelf_update_shdr
+
Copy back an ELF Section Header Table entry.
+
.It Fn gelf_update_sym
+
Copy back an ELF symbol table entry.
+
.El
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf 5
+
.Sh HISTORY
+
The GELF(3) API first appeared in System V Release 4.
+
This implementation of the API first appeared in
+
.Fx 7.0 .
+
.Sh AUTHORS
+
The GElf API was implemented by
+
.An "Joseph Koshy"
+
.Aq jkoshy@FreeBSD.org .
added external/libelf/gelf.h
@@ -0,0 +1,113 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 *
+
 * $FreeBSD: releng/9.1/lib/libelf/gelf.h 167315 2007-03-08 04:01:30Z jkoshy $
+
 */
+

+
#ifndef	_GELF_H_
+
#define	_GELF_H_
+

+
#include <sys/cdefs.h>
+

+
#include <libelf.h>
+
#include <osreldate.h>
+

+
typedef Elf64_Addr	GElf_Addr;	/* Addresses */
+
typedef Elf64_Half	GElf_Half;	/* Half words (16 bit) */
+
typedef Elf64_Off	GElf_Off;	/* Offsets */
+
typedef Elf64_Sword	GElf_Sword;	/* Signed words (32 bit) */
+
typedef Elf64_Sxword	GElf_Sxword;	/* Signed long words (64 bit) */
+
typedef Elf64_Word	GElf_Word;	/* Unsigned words (32 bit) */
+
typedef Elf64_Xword	GElf_Xword;	/* Unsigned long words (64 bit) */
+

+
typedef Elf64_Dyn	GElf_Dyn;	/* ".dynamic" section entries */
+
typedef Elf64_Ehdr	GElf_Ehdr;	/* ELF header */
+
typedef Elf64_Phdr	GElf_Phdr;	/* Program header */
+
typedef Elf64_Shdr	GElf_Shdr;	/* Section header */
+
typedef Elf64_Sym	GElf_Sym;	/* Symbol table entries */
+
typedef Elf64_Rel	GElf_Rel;	/* Relocation entries */
+
typedef Elf64_Rela	GElf_Rela;	/* Relocation entries with addend */
+

+
#if	__FreeBSD_version >= 700025
+
typedef	Elf64_Cap	GElf_Cap;	/* SW/HW capabilities */
+
typedef Elf64_Move	GElf_Move;	/* Move entries */
+
typedef Elf64_Syminfo	GElf_Syminfo;	/* Symbol information */
+
#endif
+

+
#define	GELF_M_INFO			ELF64_M_INFO
+
#define	GELF_M_SIZE			ELF64_M_SIZE
+
#define	GELF_M_SYM			ELF64_M_SYM
+

+
#define	GELF_R_INFO			ELF64_R_INFO
+
#define	GELF_R_SYM			ELF64_R_SYM
+
#define	GELF_R_TYPE			ELF64_R_TYPE
+
#define	GELF_R_TYPE_DATA		ELF64_R_TYPE_DATA
+
#define	GELF_R_TYPE_ID			ELF64_R_TYPE_ID
+
#define	GELF_R_TYPE_INFO		ELF64_R_TYPE_INFO
+

+
#define	GELF_ST_BIND			ELF64_ST_BIND
+
#define	GELF_ST_INFO			ELF64_ST_INFO
+
#define	GELF_ST_TYPE			ELF64_ST_TYPE
+
#define	GELF_ST_VISIBILITY		ELF64_ST_VISIBILITY
+

+
__BEGIN_DECLS
+
long		gelf_checksum(Elf *_elf);
+
size_t		gelf_fsize(Elf *_elf, Elf_Type _type, size_t _count,
+
			unsigned int _version);
+
int		gelf_getclass(Elf *_elf);
+
GElf_Dyn	*gelf_getdyn(Elf_Data *_data, int _index, GElf_Dyn *_dst);
+
GElf_Ehdr	*gelf_getehdr(Elf *_elf, GElf_Ehdr *_dst);
+
GElf_Phdr	*gelf_getphdr(Elf *_elf, int _index, GElf_Phdr *_dst);
+
GElf_Rel	*gelf_getrel(Elf_Data *_src, int _index, GElf_Rel *_dst);
+
GElf_Rela	*gelf_getrela(Elf_Data *_src, int _index, GElf_Rela *_dst);
+
GElf_Shdr	*gelf_getshdr(Elf_Scn *_scn, GElf_Shdr *_dst);
+
GElf_Sym	*gelf_getsym(Elf_Data *_src, int _index, GElf_Sym *_dst);
+
GElf_Sym	*gelf_getsymshndx(Elf_Data *_src, Elf_Data *_shindexsrc,
+
			int _index, GElf_Sym *_dst, Elf32_Word *_shindexdst);
+
void *		gelf_newehdr(Elf *_elf, int _class);
+
void *		gelf_newphdr(Elf *_elf, size_t _phnum);
+
int		gelf_update_dyn(Elf_Data *_dst, int _index, GElf_Dyn *_src);
+
int		gelf_update_ehdr(Elf *_elf, GElf_Ehdr *_src);
+
int		gelf_update_phdr(Elf *_elf, int _index, GElf_Phdr *_src);
+
int		gelf_update_rel(Elf_Data *_dst, int _index, GElf_Rel *_src);
+
int		gelf_update_rela(Elf_Data *_dst, int _index, GElf_Rela *_src);
+
int		gelf_update_shdr(Elf_Scn *_dst, GElf_Shdr *_src);
+
int		gelf_update_sym(Elf_Data *_dst, int _index, GElf_Sym *_src);
+
int		gelf_update_symshndx(Elf_Data *_symdst, Elf_Data *_shindexdst,
+
			int _index, GElf_Sym *_symsrc, Elf32_Word _shindexsrc);
+
Elf_Data 	*gelf_xlatetof(Elf *_elf, Elf_Data *_dst, const Elf_Data *_src, unsigned int _encode);
+
Elf_Data 	*gelf_xlatetom(Elf *_elf, Elf_Data *_dst, const Elf_Data *_src, unsigned int _encode);
+

+
#if	__FreeBSD_version >= 700025
+
GElf_Cap	*gelf_getcap(Elf_Data *_data, int _index, GElf_Cap *_cap);
+
GElf_Move	*gelf_getmove(Elf_Data *_src, int _index, GElf_Move *_dst);
+
GElf_Syminfo	*gelf_getsyminfo(Elf_Data *_src, int _index, GElf_Syminfo *_dst);
+
int		gelf_update_cap(Elf_Data *_dst, int _index, GElf_Cap *_src);
+
int		gelf_update_move(Elf_Data *_dst, int _index, GElf_Move *_src);
+
int		gelf_update_syminfo(Elf_Data *_dst, int _index, GElf_Syminfo *_src);
+
#endif
+
__END_DECLS
+

+
#endif	/* _GELF_H_ */
added external/libelf/gelf_cap.c
@@ -0,0 +1,150 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_cap.c 165317 2006-12-18 05:40:01Z jkoshy $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+
#include <osreldate.h>
+

+
#include "_libelf.h"
+

+
#if	__FreeBSD_version >= 700025
+

+
GElf_Cap *
+
gelf_getcap(Elf_Data *d, int ndx, GElf_Cap *dst)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Cap *cap32;
+
	Elf64_Cap *cap64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || dst == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_CAP) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_CAP, ec, e->e_version);
+

+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+

+
		cap32 = (Elf32_Cap *) d->d_buf + ndx;
+

+
		dst->c_tag  = cap32->c_tag;
+
		dst->c_un.c_val = (Elf64_Xword) cap32->c_un.c_val;
+

+
	} else {
+

+
		cap64 = (Elf64_Cap *) d->d_buf + ndx;
+

+
		*dst = *cap64;
+
	}
+

+
	return (dst);
+
}
+

+
int
+
gelf_update_cap(Elf_Data *d, int ndx, GElf_Cap *gc)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Cap *cap32;
+
	Elf64_Cap *cap64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || gc == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_CAP) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	msz = _libelf_msize(ELF_T_CAP, ec, e->e_version);
+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		cap32 = (Elf32_Cap *) d->d_buf + ndx;
+

+
		LIBELF_COPY_U32(cap32, gc, c_tag);
+
		LIBELF_COPY_U32(cap32, gc, c_un.c_val);
+
	} else {
+
		cap64 = (Elf64_Cap *) d->d_buf + ndx;
+

+
		*cap64 = *gc;
+
	}
+

+
	return (1);
+
}
+

+
#endif	/* __FreeBSD_version >= 700025 */
added external/libelf/gelf_checksum.3
@@ -0,0 +1,115 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_checksum.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 29, 2006
+
.Dt GELF_CHECKSUM 3
+
.Os
+
.Sh NAME
+
.Nm elf32_checksum ,
+
.Nm elf64_checksum ,
+
.Nm gelf_checksum
+
.Nd return the checksum of an ELF object
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft long
+
.Fn elf32_checksum "Elf *elf"
+
.Ft long
+
.Fn elf64_checksum "Elf *elf"
+
.In gelf.h
+
.Ft long
+
.Fn gelf_checksum "Elf *elf"
+
.Sh DESCRIPTION
+
These functions return a simple checksum of the ELF object described
+
by their argument
+
.Ar elf .
+
The checksum is computed in way that allows its value to remain
+
unchanged in presence of modifications to the ELF object by utilities
+
like
+
.Xr strip 1 .
+
.Pp
+
Function
+
.Fn elf32_checksum
+
returns a checksum for an ELF descriptor
+
.Ar elf
+
of class
+
.Dv ELFCLASS32 .
+
.Pp
+
Function
+
.Fn elf64_checksum
+
returns a checksum for an ELF descriptor
+
.Ar elf
+
of class
+
.Dv ELFCLASS64 .
+
.Pp
+
Function
+
.Fn gelf_checksum
+
provides a class-independent way retrieving the checksum
+
for ELF object
+
.Ar elf .
+
.Sh RETURN VALUES
+
These functions return the checksum of the ELF object, or zero in case
+
an error was encountered.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for an ELF file.
+
.It Bq Er ELF_E_ARGUMENT
+
The ELF descriptor
+
.Ar elf
+
was not opened for reading or updating.
+
.It Bq Er ELF_E_CLASS
+
For functions
+
.Fn elf32_checksum
+
and
+
.Fn elf64_checksum ,
+
ELF descriptor
+
.Ar elf
+
did not match the class of the called function.
+
.It Bq Er ELF_E_HEADER
+
The ELF object specified by argument
+
.Ar elf
+
had a malformed executable header.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected during processing.
+
.It Bq Er ELF_E_SECTION
+
The ELF object specified by argument
+
.Ar elf
+
contained a section with a malformed section header.
+
.It Bq Er ELF_E_VERSION
+
The ELF object was of an unsupported version.
+
.El
+
.Sh SEE ALSO
+
.Xr strip 1 ,
+
.Xr elf 3 ,
+
.Xr gelf 3
added external/libelf/gelf_checksum.c
@@ -0,0 +1,57 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_checksum.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <gelf.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
long
+
elf32_checksum(Elf *e)
+
{
+
	return (_libelf_checksum(e, ELFCLASS32));
+
}
+

+
long
+
elf64_checksum(Elf *e)
+
{
+
	return (_libelf_checksum(e, ELFCLASS64));
+
}
+

+
long
+
gelf_checksum(Elf *e)
+
{
+
	int ec;
+
	if (e == NULL ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0L);
+
	}
+
	return (_libelf_checksum(e, ec));
+
}
added external/libelf/gelf_dyn.c
@@ -0,0 +1,144 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_dyn.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
GElf_Dyn *
+
gelf_getdyn(Elf_Data *d, int ndx, GElf_Dyn *dst)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Dyn *dyn32;
+
	Elf64_Dyn *dyn64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || dst == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_DYN, ec, e->e_version);
+

+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		dyn32 = (Elf32_Dyn *) d->d_buf + ndx;
+

+
		dst->d_tag      = dyn32->d_tag;
+
		dst->d_un.d_val = (Elf64_Xword) dyn32->d_un.d_val;
+

+
	} else {
+

+
		dyn64 = (Elf64_Dyn *) d->d_buf + ndx;
+

+
		*dst = *dyn64;
+
	}
+

+
	return (dst);
+
}
+

+
int
+
gelf_update_dyn(Elf_Data *d, int ndx, GElf_Dyn *ds)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Dyn *dyn32;
+
	Elf64_Dyn *dyn64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || ds == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	msz = _libelf_msize(ELF_T_DYN, ec, e->e_version);
+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		dyn32 = (Elf32_Dyn *) d->d_buf + ndx;
+

+
		LIBELF_COPY_S32(dyn32, ds, d_tag);
+
		LIBELF_COPY_U32(dyn32, ds, d_un.d_val);
+
	} else {
+
		dyn64 = (Elf64_Dyn *) d->d_buf + ndx;
+

+
		*dyn64 = *ds;
+
	}
+

+
	return (1);
+
}
added external/libelf/gelf_ehdr.c
@@ -0,0 +1,167 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_ehdr.c 210325 2010-07-21 08:58:52Z kaiw $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+
#include <libelf.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
Elf32_Ehdr *
+
elf32_getehdr(Elf *e)
+
{
+
	return (_libelf_ehdr(e, ELFCLASS32, 0));
+
}
+

+
Elf64_Ehdr *
+
elf64_getehdr(Elf *e)
+
{
+
	return (_libelf_ehdr(e, ELFCLASS64, 0));
+
}
+

+
GElf_Ehdr *
+
gelf_getehdr(Elf *e, GElf_Ehdr *d)
+
{
+
	int ec;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+

+
	if (d == NULL || e == NULL ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL)
+
			return (NULL);
+

+
		(void) memcpy(d->e_ident, eh32->e_ident, sizeof(eh32->e_ident));
+
		d->e_type		= eh32->e_type;
+
		d->e_machine		= eh32->e_machine;
+
		d->e_version		= eh32->e_version;
+
		d->e_entry		= eh32->e_entry;
+
		d->e_phoff		= eh32->e_phoff;
+
		d->e_shoff		= eh32->e_shoff;
+
		d->e_flags		= eh32->e_flags;
+
		d->e_ehsize		= eh32->e_ehsize;
+
		d->e_phentsize		= eh32->e_phentsize;
+
		d->e_phnum		= eh32->e_phnum;
+
		d->e_shentsize		= eh32->e_shentsize;
+
		d->e_shnum		= eh32->e_shnum;
+
		d->e_shstrndx		= eh32->e_shstrndx;
+

+
		return (d);
+
	}
+

+
	assert(ec == ELFCLASS64);
+

+
	if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL)
+
		return (NULL);
+
	*d = *eh64;
+

+
	return (d);
+
}
+

+
Elf32_Ehdr *
+
elf32_newehdr(Elf *e)
+
{
+
	return (_libelf_ehdr(e, ELFCLASS32, 1));
+
}
+

+
Elf64_Ehdr *
+
elf64_newehdr(Elf *e)
+
{
+
	return (_libelf_ehdr(e, ELFCLASS64, 1));
+
}
+

+
void *
+
gelf_newehdr(Elf *e, int ec)
+
{
+
	if (e != NULL &&
+
	    (ec == ELFCLASS32 || ec == ELFCLASS64))
+
		return (_libelf_ehdr(e, ec, 1));
+

+
	LIBELF_SET_ERROR(ARGUMENT, 0);
+
	return (NULL);
+
}
+

+
int
+
gelf_update_ehdr(Elf *e, GElf_Ehdr *s)
+
{
+
	int ec;
+
	void *ehdr;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+

+
	if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (e->e_cmd == ELF_C_READ) {
+
		LIBELF_SET_ERROR(MODE, 0);
+
		return (0);
+
	}
+

+
	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
+
		return (0);
+

+
	(void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY);
+

+
	if (ec == ELFCLASS64) {
+
		eh64 = (Elf64_Ehdr *) ehdr;
+
		*eh64 = *s;
+
		return (1);
+
	}
+

+
	eh32 = (Elf32_Ehdr *) ehdr;
+

+
	(void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident));
+

+
	eh32->e_type      = s->e_type;
+
	eh32->e_machine   = s->e_machine;
+
	eh32->e_version   = s->e_version;
+
	LIBELF_COPY_U32(eh32, s, e_entry);
+
	LIBELF_COPY_U32(eh32, s, e_phoff);
+
	LIBELF_COPY_U32(eh32, s, e_shoff);
+
	eh32->e_flags     = s->e_flags;
+
	eh32->e_ehsize    = s->e_ehsize;
+
	eh32->e_phentsize = s->e_phentsize;
+
	eh32->e_phnum     = s->e_phnum;
+
	eh32->e_shentsize = s->e_shentsize;
+
	eh32->e_shnum     = s->e_shnum;
+
	eh32->e_shstrndx  = s->e_shstrndx;
+

+
	return (1);
+
}
added external/libelf/gelf_fsize.3
@@ -0,0 +1,96 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_fsize.3 210340 2010-07-21 10:39:29Z kaiw $
+
.\"
+
.Dd February 5, 2008
+
.Dt GELF_FSIZE 3
+
.Os
+
.Sh NAME
+
.Nm gelf_fsize ,
+
.Nm elf32_fsize ,
+
.Nm elf64_fsize
+
.Nd return the size of a file type
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft size_t
+
.Fn elf32_fsize "Elf_Type type" "size_t count" "unsigned int version"
+
.Ft size_t
+
.Fn elf64_fsize "Elf_Type type" "size_t count" "unsigned int version"
+
.In gelf.h
+
.Ft size_t
+
.Fn gelf_fsize "Elf *elf" "Elf_Type type" "size_t count" "unsigned int version"
+
.Sh DESCRIPTION
+
These functions return the size in bytes of the file representation of
+
.Ar count
+
numbers of objects of ELF type
+
.Ar type .
+
For ELF types that are of variable length, these functions return a
+
size of one byte.
+
.Pp
+
Functions
+
.Fn elf32_fsize
+
and
+
.Fn elf64_fsize
+
return sizes for files of class
+
.Dv ELFCLASS32
+
and
+
.Dv ELFCLASS64
+
respectively.
+
Function
+
.Fn gelf_fsize
+
returns the size for the class of ELF descriptor
+
.Ar elf .
+
.Sh RETURN VALUES
+
These functions return a non-zero value in case of success, or zero in
+
case of an error.
+
.Sh ERRORS
+
These functions may fail with:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL in a call to
+
.Fn gelf_fsize .
+
.It Bq Er ELF_E_ARGUMENT
+
ELF descriptor
+
.Ar elf
+
had an unknown ELF class.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar type
+
contained an illegal value.
+
.It Bq Er ELF_E_UNIMPL
+
Support for ELF type
+
.Ar type
+
has not been implemented.
+
.It Bq Er ELF_E_VERSION
+
Argument
+
.Ar version
+
is not a supported version.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr gelf 3
added external/libelf/gelf_fsize.c
@@ -0,0 +1,61 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_fsize.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <gelf.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
size_t
+
elf32_fsize(Elf_Type t, size_t c, unsigned int v)
+
{
+
	return (_libelf_fsize(t, ELFCLASS32, v, c));
+
}
+

+
size_t
+
elf64_fsize(Elf_Type t, size_t c, unsigned int v)
+
{
+
	return (_libelf_fsize(t, ELFCLASS64, v, c));
+
}
+

+
size_t
+
gelf_fsize(Elf *e, Elf_Type t, size_t c, unsigned int v)
+
{
+

+
	if (e == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (e->e_class == ELFCLASS32 || e->e_class == ELFCLASS64)
+
		return (_libelf_fsize(t, e->e_class, v, c));
+

+
	LIBELF_SET_ERROR(ARGUMENT, 0);
+
	return (0);
+
}
added external/libelf/gelf_getcap.3
@@ -0,0 +1,121 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getcap.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 29, 2006
+
.Dt GELF_GETCAP 3
+
.Os
+
.Sh NAME
+
.Nm gelf_getcap ,
+
.Nm gelf_update_cap
+
.Nd read and update ELF capability information
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft "GElf_Cap *"
+
.Fn gelf_getcap "Elf_Data *data" "int ndx" "GElf_Cap *cap"
+
.Ft int
+
.Fn gelf_update_cap "Elf_Data *data" "int ndx" "GElf_Cap *cap"
+
.Sh DESCRIPTION
+
These convenience functions are used to retrieve and update class-dependent
+
.Vt Elf32_Cap
+
or
+
.Vt Elf64_Cap
+
information.
+
.Pp
+
Argument
+
.Ar data
+
is an
+
.Vt Elf_Data
+
descriptor associated with a section of type
+
.Dv SHT_SUNW_cap .
+
Argument
+
.Ar ndx
+
is the index of the entry being retrieved or updated.
+
The class-independent
+
.Vt GElf_Cap
+
structure is described in
+
.Xr gelf 3 .
+
.Pp
+
Function
+
.Fn gelf_getcap
+
retrieves the class-dependent entry at index
+
.Ar ndx
+
in data buffer
+
.Ar data
+
and copies it to the destination pointed to by argument
+
.Ar cap
+
after translation to class-independent form.
+
.Pp
+
Function
+
.Fn gelf_update_cap
+
converts the class-independent entry pointed to
+
by argument
+
.Ar cap
+
to class-dependent form, and writes it to the entry at index
+
.Ar ndx
+
in the data buffer described by argument
+
.Ar data .
+
Function
+
.Fn gelf_update_cap
+
signals an error if any of the values in the class-independent
+
representation exceeds the representable limits of the target
+
type.
+
.Sh RETURN VALUES
+
Function
+
.Fn gelf_getcap
+
returns the value of argument
+
.Ar cap
+
if successful, or NULL in case of an error.
+
Function
+
.Fn gelf_update_cap
+
returns a non-zero value if successful, or zero in case of an error.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar data
+
or
+
.Ar cap
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
was less than zero or larger than the number of entries in the data
+
descriptor.
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar data
+
was not associated with a section of type
+
.Dv SHT_SUNW_cap .
+
.It Bq Er ELF_E_RANGE
+
A value was not representable in the target type.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr gelf 3
added external/libelf/gelf_getclass.3
@@ -0,0 +1,61 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getclass.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd July 3, 2006
+
.Dt GELF_GETCLASS 3
+
.Os
+
.Sh NAME
+
.Nm gelf_getclass
+
.Nd retrieve the class of an ELF descriptor
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft int
+
.Fn gelf_getclass "Elf *elf"
+
.Sh DESCRIPTION
+
Function
+
.Fn gelf_getclass
+
returns the ELF class of the descriptor supplied in argument
+
.Ar elf .
+
.Sh RETURN VALUES
+
Function
+
.Fn gelf_getclass
+
will return one of
+
.Dv ELFCLASS32
+
or
+
.Dv ELFCLASS64
+
if the argument
+
.Ar elf
+
is a descriptor for an ELF file.
+
The value
+
.Dv ELFCLASSNONE
+
is returned if argument
+
.Ar elf
+
was null, or if it was not a descriptor for an ELF file.
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_kind 3 ,
+
.Xr gelf 3
added external/libelf/gelf_getclass.c
@@ -0,0 +1,38 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_getclass.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
int
+
gelf_getclass(Elf *e)
+
{
+
	return (e != NULL ? e->e_class : ELFCLASSNONE);
+
}
added external/libelf/gelf_getdyn.3
@@ -0,0 +1,123 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getdyn.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 29, 2006
+
.Dt GELF_GETDYN 3
+
.Os
+
.Sh NAME
+
.Nm gelf_getdyn ,
+
.Nm gelf_update_dyn
+
.Nd read and update ELF dynamic entries
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft "GElf_Dyn *"
+
.Fn gelf_getdyn "Elf_Data *data" "int ndx" "GElf_Dyn *dyn"
+
.Ft int
+
.Fn gelf_update_dyn "Elf_Data *data" "int ndx" "GElf_Dyn *dyn"
+
.Sh DESCRIPTION
+
These convenience functions are used to retrieve and update class-dependent
+
.Vt Elf32_Dyn
+
or
+
.Vt Elf64_Dyn
+
information in the
+
.Sy dynamic
+
table of an ELF object.
+
.Pp
+
Argument
+
.Ar data
+
is an
+
.Vt Elf_Data
+
descriptor associated with a section of type
+
.Dv SHT_DYNAMIC .
+
Argument
+
.Ar ndx
+
is the index of the entry being retrieved or updated.
+
The class-independent
+
.Vt GElf_Dyn
+
structure is described in
+
.Xr gelf 3 .
+
.Pp
+
Function
+
.Fn gelf_getdyn
+
retrieves the class-dependent entry at index
+
.Ar ndx
+
in data buffer
+
.Ar data
+
and copies it to the destination pointed to by argument
+
.Ar dyn
+
after translation to class-independent form.
+
.Pp
+
Function
+
.Fn gelf_update_dyn
+
converts the class-independent entry pointed to
+
by argument
+
.Ar dyn
+
to class-dependent form, and writes it to the entry at index
+
.Ar ndx
+
in the data buffer described by argument
+
.Ar data .
+
Function
+
.Fn gelf_update_dyn
+
signals an error if any of the values in the class-independent
+
representation exceeds the representable limits of the target
+
type.
+
.Sh RETURN VALUES
+
Function
+
.Fn gelf_getdyn
+
returns the value of argument
+
.Ar dyn
+
if successful, or NULL in case of an error.
+
Function
+
.Fn gelf_update_dyn
+
returns a non-zero value if successful, or zero in case of an error.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar data
+
or
+
.Ar dyn
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
was less than zero or larger than the number of entries in the data
+
descriptor.
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar data
+
was not associated with a section of type
+
.Dv SHT_DYNAMIC .
+
.It Bq Er ELF_E_RANGE
+
A value was not representable in the target type.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr gelf 3
added external/libelf/gelf_getehdr.3
@@ -0,0 +1,123 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getehdr.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd December 16, 2006
+
.Dt GELF_GETEHDR 3
+
.Os
+
.Sh NAME
+
.Nm elf32_getehdr ,
+
.Nm elf64_getehdr ,
+
.Nm gelf_getehdr
+
.Nd retrieve the object file header
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf32_Ehdr *"
+
.Fn elf32_getehdr "Elf *elf"
+
.Ft "Elf64_Ehdr *"
+
.Fn elf64_getehdr "Elf *elf"
+
.In gelf.h
+
.Ft "GElf_Ehdr *"
+
.Fn gelf_getehdr "Elf *elf" "GElf_Ehdr *dst"
+
.Sh DESCRIPTION
+
These functions retrieve the ELF object file
+
header from the ELF descriptor
+
.Ar elf
+
and return a translated header descriptor to their callers.
+
.Pp
+
Functions
+
.Fn elf32_getehdr
+
and
+
.Fn elf64_getehdr
+
return a pointer to the appropriate class-specific header descriptor
+
if it exists in the file referenced by descriptor
+
.Ar elf .
+
These functions return
+
.Dv NULL
+
if an ELF header was not found in file
+
.Ar elf .
+
.Pp
+
Function
+
.Fn gelf_getehdr
+
stores a translated copy of the header for ELF file
+
.Ar elf
+
into the descriptor pointed to by argument
+
.Ar dst .
+
It returns argument
+
.Ar dst
+
if successful or
+
.Dv NULL
+
in case of failure.
+
.Sh RETURN VALUES
+
These functions return a pointer to a translated header descriptor
+
if successful, or NULL on failure.
+
.Sh ERRORS
+
These functions can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
The argument
+
.Ar elf
+
was null.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for an ELF file.
+
.It Bq Er ELF_E_ARGUMENT
+
The elf class of descriptor
+
.Ar elf
+
was not recognized.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar dst
+
was null.
+
.It Bq Er ELF_E_CLASS
+
The ELF class of descriptor
+
.Ar elf
+
did not match that of the API function being called.
+
.It Bq Er ELF_E_HEADER
+
ELF descriptor
+
.Ar elf
+
does not have an associated header.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected during execution.
+
.It Bq Er ELF_E_SECTION
+
The ELF descriptor in argument
+
.Ar elf
+
did not adhere to the conventions used for extended numbering.
+
.It Bq Er ELF_E_VERSION
+
The ELF descriptor
+
.Ar elf
+
had an unsupported ELF version number.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_newehdr 3 ,
+
.Xr elf64_newehdr 3 ,
+
.Xr elf_flagehdr 3 ,
+
.Xr elf_getident 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_newehdr 3 ,
+
.Xr elf 5
added external/libelf/gelf_getmove.3
@@ -0,0 +1,120 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getmove.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 29, 2006
+
.Dt GELF_GETMOVE 3
+
.Os
+
.Sh NAME
+
.Nm gelf_getmove ,
+
.Nm gelf_update_move
+
.Nd read and update Elf Move information
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft "GElf_Move *"
+
.Fn gelf_getmove "Elf_Data *data" "int ndx" "GElf_Move *move"
+
.Ft int
+
.Fn gelf_update_move "Elf_Data *data" "int ndx" "GElf_Move *move"
+
.Sh DESCRIPTION
+
These convenience functions are used to retrieve and update class-dependent
+
.Vt Elf32_Move
+
and
+
.Vt Elf64_Move
+
structures in an ELF object.
+
.Pp
+
Argument
+
.Ar data
+
is an
+
.Vt Elf_Data
+
descriptor associated with a section of type
+
.Dv SHT_SUNW_move .
+
Argument
+
.Ar ndx
+
is the index of the move record being retrieved or updated.
+
The class-independent
+
.Vt GElf_Move
+
structure is described in
+
.Xr gelf 3 .
+
.Pp
+
Function
+
.Fn gelf_getmove
+
retrieves class-dependent move record at index
+
.Ar ndx
+
in data buffer
+
.Ar data
+
and copies it to the destination pointed to by argument
+
.Ar move
+
after translation to class-independent form.
+
.Pp
+
Function
+
.Fn gelf_update_move
+
converts the class-independent move information pointed to
+
by argument
+
.Ar move
+
to class-dependent form, and writes it to the move record at index
+
.Ar ndx
+
in the data buffer described by argument
+
.Ar data .
+
Function
+
.Fn gelf_update_move
+
signals an error if any of the values in the class-independent
+
representation exceeds the representable limits of the target
+
type.
+
.Sh RETURN VALUES
+
Function
+
.Fn gelf_getmove
+
returns the value of argument
+
.Ar move
+
if successful, or NULL in case of an error.
+
Function
+
.Fn gelf_update_move
+
returns a non-zero value if successful, or zero in case of an error.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar data
+
or
+
.Ar move
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
was less than zero or larger than the number of records in the data
+
descriptor.
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar data
+
was not associated with a section containing move information.
+
.It Bq Er ELF_E_RANGE
+
A value was not representable in the target type.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr gelf 3
added external/libelf/gelf_getphdr.3
@@ -0,0 +1,141 @@
+
.\" Copyright (c) 2006,2007 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/gelf_getphdr.3 235286 2012-05-11 20:06:46Z gjb $
+
.\"
+
.Dd October 21, 2007
+
.Dt GELF_GETPHDR 3
+
.Os
+
.Sh NAME
+
.Nm elf32_getphdr ,
+
.Nm elf64_getphdr ,
+
.Nm gelf_getphdr
+
.Nd retrieve an ELF program header table
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf32_Phdr *"
+
.Fn elf32_getphdr "Elf *elf"
+
.Ft "Elf64_Phdr *"
+
.Fn elf64_getphdr "Elf *elf"
+
.In gelf.h
+
.Ft "GElf_Phdr *"
+
.Fn gelf_getphdr "Elf *elf" "int index" "GElf_Phdr *dst"
+
.Sh DESCRIPTION
+
These functions retrieve and translate ELF program header information
+
from an ELF descriptor, if this information exists.
+
.Pp
+
Functions
+
.Fn elf32_getphdr
+
and
+
.Fn elf64_getphdr
+
return a pointer to an array of translated
+
.Vt Elf32_Phdr
+
and
+
.Vt Elf64_Phdr
+
descriptors respectively.
+
These descriptors are described in
+
.Xr elf 5 .
+
The number of entries in this array may be determined using the
+
.Xr elf_getphnum 3
+
function.
+
.Pp
+
Function
+
.Fn gelf_getphdr
+
will retrieve the program header table entry at index
+
.Ar index
+
from ELF descriptor
+
.Ar elf .
+
The translated program header table entry will be written to the
+
address pointed to be argument
+
.Ar dst .
+
.Pp
+
Applications may inform the library of modifications to a program header table entry
+
by using the
+
.Xr elf_flagphdr 3
+
API.
+
Applications using the
+
.Xr gelf 3
+
interface need to use the
+
.Xr gelf_update_phdr 3
+
API to copy modifications to a program header entry back to the underlying
+
ELF descriptor.
+
.Sh RETURN VALUES
+
The functions a valid pointer if successful, or NULL in case an error
+
was encountered.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for an ELF object.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar dst
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Index
+
.Ar index
+
was out of range.
+
.It Bq Er ELF_E_CLASS
+
The class of ELF descriptor
+
.Ar elf
+
did not match the expected class of the function being called.
+
.It Bq Er ELF_E_HEADER
+
ELF descriptor
+
.Ar elf
+
did not possess an executable header.
+
.It Bq Er ELF_E_HEADER
+
ELF descriptor
+
.Ar elf
+
had a corrupt executable header.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected.
+
.It Bq Er ELF_E_SECTION
+
The ELF descriptor in argument
+
.Ar elf
+
did not adhere to the conventions used for extended numbering.
+
.It Bq Er ELF_VERSION
+
ELF descriptor
+
.Ar elf
+
was of an unsupported version.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf32_newphdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf64_newphdr 3 ,
+
.Xr elf_flagphdr 3 ,
+
.Xr elf_getphnum 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getehdr 3 ,
+
.Xr gelf_newphdr 3 ,
+
.Xr gelf_update_phdr 3 ,
+
.Xr elf 5
added external/libelf/gelf_getrel.3
@@ -0,0 +1,121 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getrel.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 29, 2006
+
.Dt GELF_GETREL 3
+
.Os
+
.Sh NAME
+
.Nm gelf_getrel ,
+
.Nm gelf_update_rel
+
.Nd read and update ELF relocation entries
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft "GElf_Rel *"
+
.Fn gelf_getrel "Elf_Data *data" "int ndx" "GElf_Rel *rel"
+
.Ft int
+
.Fn gelf_update_rel "Elf_Data *data" "int ndx" "GElf_Rel *rel"
+
.Sh DESCRIPTION
+
These convenience functions are used to retrieve and update class-dependent
+
.Vt Elf32_Rel
+
or
+
.Vt Elf64_Rel
+
structures in an ELF object.
+
.Pp
+
Argument
+
.Ar data
+
is an
+
.Vt Elf_Data
+
descriptor associated with a section of type
+
.Dv SHT_REL .
+
Argument
+
.Ar ndx
+
is the index of the entry being retrieved or updated.
+
The class-independent
+
.Vt GElf_Rel
+
structure is described in
+
.Xr gelf 3 .
+
.Pp
+
Function
+
.Fn gelf_getrel
+
retrieves the class-dependent entry at index
+
.Ar ndx
+
in data buffer
+
.Ar data
+
and copies it to the destination pointed to by argument
+
.Ar rel
+
after translation to class-independent form.
+
.Pp
+
Function
+
.Fn gelf_update_rel
+
converts the class-independent entry pointed to
+
by argument
+
.Ar rel
+
to class-dependent form, and writes it to the entry at index
+
.Ar ndx
+
in the data buffer described by argument
+
.Ar data .
+
Function
+
.Fn gelf_update_rel
+
signals an error if any of the values in the class-independent
+
representation exceeds the representable limits of the target
+
type.
+
.Sh RETURN VALUES
+
Function
+
.Fn gelf_getrel
+
returns the value of argument
+
.Ar rel
+
if successful, or NULL in case of an error.
+
Function
+
.Fn gelf_update_rel
+
returns a non-zero value if successful, or zero in case of an error.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar data
+
or
+
.Ar rel
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
was less than zero or larger than the number of entries in the data
+
descriptor.
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar data
+
was not associated with a section of type
+
.Dv SHT_REL .
+
.It Bq Er ELF_E_RANGE
+
A value was not representable in the target type.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr gelf 3
added external/libelf/gelf_getrela.3
@@ -0,0 +1,121 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getrela.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 29, 2006
+
.Dt GELF_GETRELA 3
+
.Os
+
.Sh NAME
+
.Nm gelf_getrela ,
+
.Nm gelf_update_rela
+
.Nd read and update ELF relocation entries with addends
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft "GElf_Rela *"
+
.Fn gelf_getrela "Elf_Data *data" "int ndx" "GElf_Rela *rela"
+
.Ft int
+
.Fn gelf_update_rela "Elf_Data *data" "int ndx" "GElf_Rela *rela"
+
.Sh DESCRIPTION
+
These convenience functions are used to retrieve and update class-dependent
+
.Vt Elf32_Rela
+
or
+
.Vt Elf64_Rela
+
structures in an ELF object.
+
.Pp
+
Argument
+
.Ar data
+
is an
+
.Vt Elf_Data
+
descriptor associated with a section of type
+
.Dv SHT_RELA .
+
Argument
+
.Ar ndx
+
is the index of the entry being retrieved or updated.
+
The class-independent
+
.Vt GElf_Rela
+
structure is described in
+
.Xr gelf 3 .
+
.Pp
+
Function
+
.Fn gelf_getrela
+
retrieves the class-dependent entry at index
+
.Ar ndx
+
in data buffer
+
.Ar data
+
and copies it to the destination pointed to by argument
+
.Ar rela
+
after translation to class-independent form.
+
.Pp
+
Function
+
.Fn gelf_update_rela
+
converts the class-independent entry pointed to
+
by argument
+
.Ar rela
+
to class-dependent form, and writes it to the entry at index
+
.Ar ndx
+
in the data buffer described by argument
+
.Ar data .
+
Function
+
.Fn gelf_update_rela
+
signals an error if any of the values in the class-independent
+
representation exceeds the representable limits of the target
+
type.
+
.Sh RETURN VALUES
+
Function
+
.Fn gelf_getrela
+
returns the value of argument
+
.Ar rela
+
if successful, or NULL in case of an error.
+
Function
+
.Fn gelf_update_rela
+
returns a non-zero value if successful, or zero in case of an error.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar data
+
or
+
.Ar rela
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
was less than zero or larger than the number of entries in the data
+
descriptor.
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar data
+
was not associated with a section of type
+
.Dv SHT_RELA .
+
.It Bq Er ELF_E_RANGE
+
A value was not representable in the target type.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr gelf 3
added external/libelf/gelf_getshdr.3
@@ -0,0 +1,115 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getshdr.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 27, 2006
+
.Dt GELF_GETSHDR 3
+
.Os
+
.Sh NAME
+
.Nm elf32_getshdr ,
+
.Nm elf64_getshdr ,
+
.Nm gelf_getshdr
+
.Nd retrieve the class-dependent section header
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf32_Shdr *"
+
.Fn elf32_getshdr "Elf_Scn *scn"
+
.Ft "Elf64_Shdr *"
+
.Fn elf64_getshdr "Elf_Scn *scn"
+
.In gelf.h
+
.Ft "GElf_Shdr *"
+
.Fn gelf_getshdr "Elf_Scn *scn" "GElf_Shdr *shdr"
+
.Sh DESCRIPTION
+
These functions return a pointer to the ELF Section Header data
+
structure associated with section descriptor
+
.Ar scn .
+
.Pp
+
Function
+
.Fn elf32_getshdr
+
retrieves a pointer to an
+
.Vt Elf32_Shdr
+
structure.
+
Section descriptor
+
.Ar scn
+
must be associated with an ELF descriptor of class
+
.Dv ELFCLASS32 .
+
.Pp
+
Function
+
.Fn elf64_getshdr
+
retrieves a pointer to an
+
.Vt Elf64_Shdr
+
structure.
+
Section descriptor
+
.Ar scn
+
must be associated with an ELF descriptor of class
+
.Dv ELFCLASS64 .
+
.Pp
+
Function
+
.Fn gelf_getshdr
+
copies the values in the section header associated with argument
+
.Ar scn
+
to the structure pointed to be argument
+
.Ar dst .
+
The
+
.Vt GElf_Shdr
+
data structure is described in
+
.Xr gelf 3 .
+
.Sh RETURN VALUES
+
Functions
+
.Fn elf32_getshdr
+
and
+
.Fn elf64_getshdr
+
return a valid pointer to the appropriate section header on success
+
or NULL if an error was encountered.
+
.Pp
+
Function
+
.Fn gelf_getshdr
+
returns argument
+
.Ar dst
+
if successful, or NULL if an error was encountered.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar scn
+
or
+
.Ar shdr
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar scn
+
was not associated a descriptor for an ELF object.
+
.It Bq Er ELF_E_CLASS
+
The ELF class associated with the section descriptor
+
.Ar scn
+
did not match the class expected by the API.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_update_shdr 3
added external/libelf/gelf_getsym.3
@@ -0,0 +1,125 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getsym.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 29, 2006
+
.Dt GELF_GETSYM 3
+
.Os
+
.Sh NAME
+
.Nm gelf_getsym ,
+
.Nm gelf_update_sym
+
.Nd read and update symbol information
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft "GElf_Sym *"
+
.Fn gelf_getsym "Elf_Data *data" "int ndx" "GElf_Sym *sym"
+
.Ft int
+
.Fn gelf_update_sym "Elf_Data *data" "int ndx" "GElf_Sym *sym"
+
.Sh DESCRIPTION
+
These convenience functions are used to retrieve and update class-dependent
+
.Vt Elf32_Sym
+
and
+
.Vt Elf64_Sym
+
structures in an ELF object.
+
.Pp
+
Argument
+
.Ar data
+
is an
+
.Vt Elf_Data
+
descriptor associated with a section of type
+
.Dv SHT_SYMTAB ,
+
.Dv SHT_DYNSYM
+
or
+
.Dv SHT_GNU_versym .
+
Argument
+
.Ar ndx
+
is the index of the symbol being retrieved or updated.
+
The class-independent
+
.Vt GElf_Sym
+
structure is described in
+
.Xr gelf 3 .
+
.Pp
+
Function
+
.Fn gelf_getsym
+
retrieves class-dependent symbol information at index
+
.Ar ndx
+
in data buffer
+
.Ar data
+
and copies it to the destination pointed to by argument
+
.Ar sym
+
after translation to class-independent form.
+
.Pp
+
Function
+
.Fn gelf_update_sym
+
converts the class-independent symbol information pointed to
+
by argument
+
.Ar sym
+
to class-dependent form, and writes it to the symbol entry at index
+
.Ar ndx
+
in the data buffer described by argument
+
.Ar data .
+
Function
+
.Fn gelf_update_sym
+
signals an error if any of the values in the class-independent
+
representation exceeds the representable limits of the target
+
type.
+
.Sh RETURN VALUES
+
Function
+
.Fn gelf_getsym
+
returns the value of argument
+
.Ar sym
+
if successful, or NULL in case of an error.
+
Function
+
.Fn gelf_update_sym
+
returns a non-zero value if successful, or zero in case of an error.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar data
+
or
+
.Ar sym
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
was less than zero or larger than the number of symbols in the data
+
descriptor.
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar data
+
was not associated with a section containing symbol information.
+
.It Bq Er ELF_E_RANGE
+
A value was not representable in the target type.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getsyminfo 3 ,
+
.Xr gelf_update_syminfo 3
added external/libelf/gelf_getsyminfo.3
@@ -0,0 +1,115 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getsyminfo.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 29, 2006
+
.Dt GELF_GETSYMINFO 3
+
.Os
+
.Sh NAME
+
.Nm gelf_getsyminfo ,
+
.Nm gelf_update_syminfo
+
.Nd read and update symbol information
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft "GElf_Syminfo *"
+
.Fn gelf_getsyminfo "Elf_Data *data" "int ndx" "GElf_Syminfo *syminfo"
+
.Ft int
+
.Fn gelf_update_syminfo "Elf_Data *data" "int ndx" "GElf_Syminfo *syminfo"
+
.Sh DESCRIPTION
+
These convenience functions are used to retrieve and update class-dependent
+
.Vt Elf32_Syminfo
+
and
+
.Vt Elf64_Syminfo
+
records in an ELF object.
+
.Pp
+
Argument
+
.Ar data
+
is an
+
.Vt Elf_Data
+
descriptor associated with a section of type
+
.Dv SHT_SUNW_syminfo .
+
Argument
+
.Ar ndx
+
is the index of the record being retrieved or updated.
+
The class-independent
+
.Vt GElf_Syminfo
+
structure is described in
+
.Xr gelf 3 .
+
.Pp
+
Function
+
.Fn gelf_getsyminfo
+
retrieves class-dependent record at index
+
.Ar ndx
+
in data buffer
+
.Ar data
+
and copies it to the destination pointed to by argument
+
.Ar syminfo
+
after translation to class-independent form.
+
.Pp
+
Function
+
.Fn gelf_update_syminfo
+
converts the class-independent record pointed to
+
by argument
+
.Ar syminfo
+
to class-dependent form, and writes it to the record at index
+
.Ar ndx
+
in the data buffer described by argument
+
.Ar data .
+
.Sh RETURN VALUES
+
Function
+
.Fn gelf_getsyminfo
+
returns the value of argument
+
.Ar syminfo
+
if successful, or NULL in case of an error.
+
Function
+
.Fn gelf_update_syminfo
+
returns a non-zero value if successful, or zero in case of an error.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar data
+
or
+
.Ar syminfo
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
was less than zero or larger than the number of symbols in the data
+
descriptor.
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar data
+
was not associated with a section containing symbol information.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getsym 3 ,
+
.Xr gelf_update_sym 3
added external/libelf/gelf_getsymshndx.3
@@ -0,0 +1,162 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_getsymshndx.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd November 5, 2006
+
.Dt GELF_GETSYMSHNDX 3
+
.Os
+
.Sh NAME
+
.Nm gelf_getsymshndx ,
+
.Nm gelf_update_symshndx
+
.Nd read and update symbol information using extended section indices
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft "GElf_Sym *"
+
.Fo gelf_getsymshndx
+
.Fa "Elf_Data *symdata"
+
.Fa "Elf_Data *xndxdata"
+
.Fa "int ndx"
+
.Fa "GElf_Sym *sym"
+
.Fa "Elf32_Word *xndxptr"
+
.Fc
+
.Ft int
+
.Fo gelf_update_symshndx
+
.Fa "Elf_Data *symdata"
+
.Fa "Elf_Data *xndxdata"
+
.Fa "int ndx"
+
.Fa "GElf_Sym *sym"
+
.Fa "Elf32_Word xndx"
+
.Fc
+
.Sh DESCRIPTION
+
These functions are analogous to
+
.Fn gelf_getsym
+
and
+
.Fn gelf_update_sym
+
respectively, but are capable of handling symbol tables using extended
+
section numbering.
+
.Pp
+
Argument
+
.Ar symdata
+
is an
+
.Vt Elf_Data
+
descriptor associated with a section of type
+
.Dv SHT_SYMTAB .
+
Argument
+
.Ar xndxdata
+
is an
+
.Vt Elf_Data
+
descriptor associated with a section of type
+
.Dv SHT_SYMTAB_SHNDX .
+
Argument
+
.Ar ndx
+
is the index of the symbol table entry being retrieved or updated.
+
Argument
+
.Ar sym
+
is a pointer to a class-independent
+
.Vt GElf_Sym
+
structure.
+
.Vt GElf_Sym
+
structures are described in detail in
+
.Xr gelf 3 .
+
.Pp
+
Function
+
.Fn gelf_getsymshndx
+
retrieves symbol information at index
+
.Ar ndx
+
from the data descriptor specified by argument
+
.Ar symdata
+
and stores in class-independent form in argument
+
.Ar sym .
+
In addition it retrieves the extended section index for the
+
symbol from data buffer
+
.Ar xndxdata
+
and stores it into the location pointed to by argument
+
.Ar xndxptr .
+
.Pp
+
Function
+
.Fn gelf_update_symshndx
+
updates the underlying symbol table entry in data
+
descriptor
+
.Ar symdata
+
with the information in argument
+
.Ar sym .
+
In addition it sets the extended section index in
+
data buffer
+
.Ar xndxdata
+
to the value of argument
+
.Ar xndx .
+
.Sh RETURN VALUES
+
Function
+
.Fn gelf_getsymshndx
+
returns the value of argument
+
.Ar sym
+
if successful, or NULL in case of an error.
+
.Pp
+
Function
+
.Fn gelf_update_symshndx
+
returns a non-zero value if successful, or zero in case of an error.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar symdata ,
+
.Ar xndxdata ,
+
.Ar xndxptr
+
or
+
.Ar sym
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
was less than zero, or too large for either of descriptors
+
.Ar symdata
+
or
+
.Ar xndxdata .
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar symdata
+
was not associated with a section of type
+
.Dv SHT_SYMTAB .
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar xndxdata
+
was not associated with a section of type
+
.Dv SHT_SYMTAB_SHNDX .
+
.It Bq Er ELF_E_ARGUMENT
+
Data descriptor
+
.Ar symdata
+
and
+
.Ar xndxdata
+
were associated with different ELF objects.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr elf_getscn 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getsym 3 ,
+
.Xr gelf_update_sym 3
added external/libelf/gelf_move.c
@@ -0,0 +1,156 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_move.c 165317 2006-12-18 05:40:01Z jkoshy $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+
#include <osreldate.h>
+

+
#include "_libelf.h"
+

+
#if	__FreeBSD_version >= 700025
+

+
GElf_Move *
+
gelf_getmove(Elf_Data *d, int ndx, GElf_Move *dst)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Move *move32;
+
	Elf64_Move *move64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || dst == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version);
+

+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+

+
		move32 = (Elf32_Move *) d->d_buf + ndx;
+

+
		dst->m_value   = move32->m_value;
+
		dst->m_info    = (Elf64_Xword) move32->m_info;
+
		dst->m_poffset = (Elf64_Xword) move32->m_poffset;
+
		dst->m_repeat  = move32->m_repeat;
+
		dst->m_stride = move32->m_stride;
+
	} else {
+

+
		move64 = (Elf64_Move *) d->d_buf + ndx;
+

+
		*dst = *move64;
+
	}
+

+
	return (dst);
+
}
+

+
int
+
gelf_update_move(Elf_Data *d, int ndx, GElf_Move *gm)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Move *move32;
+
	Elf64_Move *move64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || gm == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version);
+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		move32 = (Elf32_Move *) d->d_buf + ndx;
+

+
		move32->m_value  = gm->m_value;
+
		LIBELF_COPY_U32(move32, gm, m_info);
+
		LIBELF_COPY_U32(move32, gm, m_poffset);
+
		move32->m_repeat  = gm->m_repeat;
+
		move32->m_stride = gm->m_stride;
+

+
	} else {
+
		move64 = (Elf64_Move *) d->d_buf + ndx;
+

+
		*move64 = *gm;
+
	}
+

+
	return (1);
+
}
+

+
#endif	/* __FreeBSD_version >= 700025 */
added external/libelf/gelf_newehdr.3
@@ -0,0 +1,186 @@
+
.\" Copyright (c) 2006,2007 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.
+
.\"
+
.\" $FreeBSD: head/lib/libelf/gelf_newehdr.3 235286 2012-05-11 20:06:46Z gjb $
+
.\"
+
.Dd October 22, 2007
+
.Dt GELF_NEWEHDR 3
+
.Os
+
.Sh NAME
+
.Nm elf32_newehdr ,
+
.Nm elf64_newehdr ,
+
.Nm gelf_newehdr
+
.Nd retrieve or allocate the object file header
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf32_Ehdr *"
+
.Fn elf32_newehdr "Elf *elf"
+
.Ft "Elf64_Ehdr *"
+
.Fn elf64_newehdr "Elf *elf"
+
.In gelf.h
+
.Ft "void *"
+
.Fn gelf_newehdr "Elf *elf" "int elfclass"
+
.Sh DESCRIPTION
+
These functions retrieve the ELF header from the ELF descriptor
+
.Ar elf ,
+
allocating a new header if needed.
+
File data structures are translated to their in-memory representations
+
as described in
+
.Xr elf 3 .
+
.Pp
+
Function
+
.Fn elf32_newehdr
+
returns a pointer to a 32 bit
+
.Vt Elf32_Ehdr
+
structure.
+
Function
+
.Fn elf64_newehdr
+
returns a pointer to a 64 bit
+
.Vt Elf64_Ehdr
+
structure.
+
.Pp
+
When argument
+
.Ar elfclass
+
has value
+
.Dv ELFCLASS32 ,
+
function
+
.Fn gelf_newehdr
+
returns the value returned by
+
.Fn elf32_newehdr "elf" .
+
When argument
+
.Ar elfclass
+
has value
+
.Dv ELFCLASS64
+
it returns the value returned by
+
.Fn elf64_newehdr "elf" .
+
.Pp
+
If a fresh header structure is allocated, the members of the
+
structure are initialized as follows:
+
.Bl -tag -width indent
+
.It Va "e_ident[EI_MAG0..EI_MAG3]"
+
Identification bytes at offsets
+
.Dv EI_MAG0 ,
+
.Dv EI_MAG1 ,
+
.Dv EI_MAG2
+
and
+
.Dv EI_MAG3
+
are set to the ELF signature.
+
.It Va "e_ident[EI_CLASS]"
+
The identification byte at offset
+
.Dv EI_CLASS
+
is set to the ELF class associated with the function being called
+
or to argument
+
.Ar elfclass
+
for function
+
.Fn gelf_newehdr .
+
.It Va "e_ident[EI_DATA]"
+
The identification byte at offset
+
.Dv EI_DATA
+
is set to
+
.Dv ELFDATANONE .
+
.It Va "e_ident[EI_VERSION]"
+
The identification byte at offset
+
.Dv EI_VERSION
+
is set to the ELF library's operating version set by a prior call to
+
.Xr elf_version 3 .
+
.It Va e_machine
+
is set to
+
.Dv EM_NONE .
+
.It Va e_type
+
is set to
+
.Dv ELF_K_NONE .
+
.It Va e_version
+
is set to the ELF library's operating version set by a prior call to
+
.Xr elf_version 3 .
+
.El
+
.Pp
+
Other members of the header are set to zero.
+
The application is responsible for changing these values
+
as needed before calling
+
.Fn elf_update .
+
.Pp
+
If successful, these three functions set the
+
.Dv ELF_F_DIRTY
+
flag on ELF descriptor
+
.Ar elf .
+
.Sh RETURN VALUES
+
These functions return a pointer to a translated header descriptor
+
if successful, or NULL on failure.
+
.Sh ERRORS
+
These functions can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
The argument
+
.Ar elf
+
was null.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for an ELF object.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elfclass
+
had an unsupported value.
+
.It Bq Er ELF_E_ARGUMENT
+
The class of the ELF descriptor
+
.Ar elf
+
did not match that of the requested operation.
+
.It Bq Er ELF_E_ARGUMENT
+
For function
+
.Fn gelf_newehdr ,
+
the class of argument
+
.Ar elf
+
was not
+
.Dv ELFCLASSNONE
+
and did not match the argument
+
.Ar elfclass .
+
.It Bq Er ELF_E_CLASS
+
The ELF class of descriptor
+
.Ar elf
+
did not match that of the API function being called.
+
.It Bq Er ELF_E_HEADER
+
A malformed ELF header was detected.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected during execution.
+
.It Bq Er ELF_E_SECTION
+
The ELF descriptor in argument
+
.Ar elf
+
did not adhere to the conventions used for extended numbering.
+
.It Bq Er ELF_E_VERSION
+
The ELF descriptor
+
.Ar elf
+
had an unsupported ELF version number.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getehdr 3 ,
+
.Xr elf64_getehdr 3 ,
+
.Xr elf_flagdata 3 ,
+
.Xr elf_getident 3 ,
+
.Xr elf_update 3 ,
+
.Xr elf_version 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getehdr 3 ,
+
.Xr elf 5
added external/libelf/gelf_newphdr.3
@@ -0,0 +1,133 @@
+
.\" Copyright (c) 2006,2007 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_newphdr.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd October 22, 2007
+
.Dt GELF_NEWPHDR 3
+
.Os
+
.Sh NAME
+
.Nm elf32_newphdr ,
+
.Nm elf64_newphdr ,
+
.Nm gelf_newphdr
+
.Nd allocate an ELF program header table
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf32_Phdr *"
+
.Fn elf32_newphdr "Elf *elf" "size_t count"
+
.Ft "Elf64_Phdr *"
+
.Fn elf64_newphdr "Elf *elf" "size_t count"
+
.In gelf.h
+
.Ft "void *"
+
.Fn gelf_newphdr "Elf *elf" "size_t count"
+
.Sh DESCRIPTION
+
These functions allocate an ELF Program Header table
+
for an ELF descriptor.
+
.Vt Elf32_Phdr
+
and
+
.Vt Elf64_Phdr
+
descriptors are described further in
+
.Xr elf 5 .
+
.Pp
+
Functions
+
.Fn elf32_newphdr
+
and
+
.Fn elf64_newphdr
+
allocate a table of
+
.Ar count
+
.Vt Elf32_Phdr
+
and
+
.Vt Elf64_Phdr
+
descriptors respectively,
+
discarding any existing program header table
+
already present in the ELF descriptor
+
.Ar elf .
+
A value of zero for argument
+
.Ar count
+
may be used to delete an existing program header table
+
from an ELF descriptor.
+
.Pp
+
Function
+
.Fn gelf_newphdr
+
will return a table of
+
.Vt Elf32_Phdr
+
or
+
.Vt Elf64_Phdr
+
with
+
.Ar count
+
elements depending on the ELF class of ELF descriptor
+
.Ar elf .
+
.Pp
+
The functions set the
+
.Dv ELF_F_DIRTY
+
flag on the program header table.
+
All members of the returned array of Phdr structures
+
will be initialized to zero.
+
.Pp
+
After a successful call to these functions, the pointer returned
+
by a prior call to
+
.Fn elf32_getphdr
+
or
+
.Fn elf64_getphdr
+
on the same descriptor
+
.Ar elf
+
will no longer be valid.
+
.Sh RETURN VALUES
+
The functions a valid pointer if successful, or NULL in case an error
+
was encountered.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for an ELF object.
+
.It Bq Er ELF_E_CLASS
+
ELF descriptor
+
.Ar elf
+
was of an unrecognized class.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected.
+
.It Bq Er ELF_E_SEQUENCE
+
An executable header was not allocated for ELF descriptor
+
.Ar elf
+
before using these APIs.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf32_getphdr 3 ,
+
.Xr elf32_newehdr 3 ,
+
.Xr elf64_getphdr 3 ,
+
.Xr elf64_newehdr 3 ,
+
.Xr elf_flagphdr 3 ,
+
.Xr elf_getphnum 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getphdr 3 ,
+
.Xr gelf_newehdr 3 ,
+
.Xr elf 5
added external/libelf/gelf_phdr.c
@@ -0,0 +1,178 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_phdr.c 210325 2010-07-21 08:58:52Z kaiw $");
+

+
#include <sys/limits.h>
+

+
#include <gelf.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
Elf32_Phdr *
+
elf32_getphdr(Elf *e)
+
{
+
	return (_libelf_getphdr(e, ELFCLASS32));
+
}
+

+
Elf64_Phdr *
+
elf64_getphdr(Elf *e)
+
{
+
	return (_libelf_getphdr(e, ELFCLASS64));
+
}
+

+
GElf_Phdr *
+
gelf_getphdr(Elf *e, int index, GElf_Phdr *d)
+
{
+
	int ec;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+
	Elf32_Phdr *ep32;
+
	Elf64_Phdr *ep64;
+

+
	if (d == NULL || e == NULL ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) ||
+
	    (e->e_kind != ELF_K_ELF) || index < 0) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL ||
+
		    ((ep32 = _libelf_getphdr(e, ELFCLASS32)) == NULL))
+
			return (NULL);
+

+
		if (index >= eh32->e_phnum) {
+
			LIBELF_SET_ERROR(ARGUMENT, 0);
+
			return (NULL);
+
		}
+

+
		ep32 += index;
+

+
		d->p_type   = ep32->p_type;
+
		d->p_offset = ep32->p_offset;
+
		d->p_vaddr  = (Elf64_Addr) ep32->p_vaddr;
+
		d->p_paddr  = (Elf64_Addr) ep32->p_paddr;
+
		d->p_filesz = (Elf64_Xword) ep32->p_filesz;
+
		d->p_memsz  = (Elf64_Xword) ep32->p_memsz;
+
		d->p_flags  = ep32->p_flags;
+
		d->p_align  = (Elf64_Xword) ep32->p_align;
+

+
	} else {
+
		if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL ||
+
		    (ep64 = _libelf_getphdr(e, ELFCLASS64)) == NULL)
+
			return (NULL);
+

+
		if (index >= eh64->e_phnum) {
+
			LIBELF_SET_ERROR(ARGUMENT, 0);
+
			return (NULL);
+
		}
+

+
		ep64 += index;
+

+
		*d = *ep64;
+
	}
+

+
	return (d);
+
}
+

+
Elf32_Phdr *
+
elf32_newphdr(Elf *e, size_t count)
+
{
+
	return (_libelf_newphdr(e, ELFCLASS32, count));
+
}
+

+
Elf64_Phdr *
+
elf64_newphdr(Elf *e, size_t count)
+
{
+
	return (_libelf_newphdr(e, ELFCLASS64, count));
+
}
+

+
void *
+
gelf_newphdr(Elf *e, size_t count)
+
{
+
	if (e == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+
	return (_libelf_newphdr(e, e->e_class, count));
+
}
+

+
int
+
gelf_update_phdr(Elf *e, int ndx, GElf_Phdr *s)
+
{
+
	int ec, phnum;
+
	void *ehdr;
+
	Elf32_Phdr *ph32;
+
	Elf64_Phdr *ph64;
+

+
	if (s == NULL || e == NULL || e->e_kind != ELF_K_ELF ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (e->e_cmd == ELF_C_READ) {
+
		LIBELF_SET_ERROR(MODE, 0);
+
		return (0);
+
	}
+

+
	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
+
		return (0);
+

+
	if (ec == ELFCLASS32)
+
		phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
+
	else
+
		phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
+

+
	if (ndx < 0 || ndx > phnum) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	(void) elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
+

+
	if (ec == ELFCLASS64) {
+
		ph64 = e->e_u.e_elf.e_phdr.e_phdr64 + ndx;
+
		*ph64 = *s;
+
		return (1);
+
	}
+

+
	ph32 = e->e_u.e_elf.e_phdr.e_phdr32 + ndx;
+

+
	ph32->p_type     =  s->p_type;
+
	ph32->p_flags    =  s->p_flags;
+
	LIBELF_COPY_U32(ph32, s, p_offset);
+
	LIBELF_COPY_U32(ph32, s, p_vaddr);
+
	LIBELF_COPY_U32(ph32, s, p_paddr);
+
	LIBELF_COPY_U32(ph32, s, p_filesz);
+
	LIBELF_COPY_U32(ph32, s, p_memsz);
+
	LIBELF_COPY_U32(ph32, s, p_align);
+

+
	return (1);
+
}
added external/libelf/gelf_rel.c
@@ -0,0 +1,153 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_rel.c 176727 2008-03-02 06:33:10Z jkoshy $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
GElf_Rel *
+
gelf_getrel(Elf_Data *d, int ndx, GElf_Rel *dst)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Rel *rel32;
+
	Elf64_Rel *rel64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || dst == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_REL, ec, e->e_version);
+

+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		rel32 = (Elf32_Rel *) d->d_buf + ndx;
+

+
		dst->r_offset = (Elf64_Addr) rel32->r_offset;
+
		dst->r_info   = ELF64_R_INFO(
+
		    (Elf64_Xword) ELF32_R_SYM(rel32->r_info),
+
		    ELF32_R_TYPE(rel32->r_info));
+

+
	} else {
+

+
		rel64 = (Elf64_Rel *) d->d_buf + ndx;
+

+
		*dst = *rel64;
+
	}
+

+
	return (dst);
+
}
+

+
int
+
gelf_update_rel(Elf_Data *d, int ndx, GElf_Rel *dr)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Rel *rel32;
+
	Elf64_Rel *rel64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || dr == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	msz = _libelf_msize(ELF_T_REL, ec, e->e_version);
+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		rel32 = (Elf32_Rel *) d->d_buf + ndx;
+

+
		LIBELF_COPY_U32(rel32, dr, r_offset);
+

+
		if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) ||
+
		    ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) {
+
			LIBELF_SET_ERROR(RANGE, 0);
+
			return (0);
+
		}
+
		rel32->r_info = ELF32_R_INFO(ELF64_R_SYM(dr->r_info),
+
		    ELF64_R_TYPE(dr->r_info));
+
	} else {
+
		rel64 = (Elf64_Rel *) d->d_buf + ndx;
+

+
		*rel64 = *dr;
+
	}
+

+
	return (1);
+
}
added external/libelf/gelf_rela.c
@@ -0,0 +1,156 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_rela.c 176727 2008-03-02 06:33:10Z jkoshy $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
GElf_Rela *
+
gelf_getrela(Elf_Data *d, int ndx, GElf_Rela *dst)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Rela *rela32;
+
	Elf64_Rela *rela64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || dst == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_RELA, ec, e->e_version);
+

+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		rela32 = (Elf32_Rela *) d->d_buf + ndx;
+

+
		dst->r_offset = (Elf64_Addr) rela32->r_offset;
+
		dst->r_info   = ELF64_R_INFO(
+
		    (Elf64_Xword) ELF32_R_SYM(rela32->r_info),
+
		    ELF32_R_TYPE(rela32->r_info));
+
		dst->r_addend = (Elf64_Sxword) rela32->r_addend;
+

+
	} else {
+

+
		rela64 = (Elf64_Rela *) d->d_buf + ndx;
+

+
		*dst = *rela64;
+
	}
+

+
	return (dst);
+
}
+

+
int
+
gelf_update_rela(Elf_Data *d, int ndx, GElf_Rela *dr)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Rela *rela32;
+
	Elf64_Rela *rela64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || dr == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	msz = _libelf_msize(ELF_T_RELA, ec, e->e_version);
+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		rela32 = (Elf32_Rela *) d->d_buf + ndx;
+

+
		LIBELF_COPY_U32(rela32, dr, r_offset);
+

+
		if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) ||
+
		    ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) {
+
			LIBELF_SET_ERROR(RANGE, 0);
+
			return (0);
+
		}
+
		rela32->r_info = ELF32_R_INFO(ELF64_R_SYM(dr->r_info),
+
		    ELF64_R_TYPE(dr->r_info));
+

+
		LIBELF_COPY_S32(rela32, dr, r_addend);
+
	} else {
+
		rela64 = (Elf64_Rela *) d->d_buf + ndx;
+

+
		*rela64 = *dr;
+
	}
+

+
	return (1);
+
}
added external/libelf/gelf_shdr.c
@@ -0,0 +1,131 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_shdr.c 210325 2010-07-21 08:58:52Z kaiw $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
Elf32_Shdr *
+
elf32_getshdr(Elf_Scn *s)
+
{
+
	return (_libelf_getshdr(s, ELFCLASS32));
+
}
+

+
Elf64_Shdr *
+
elf64_getshdr(Elf_Scn *s)
+
{
+
	return (_libelf_getshdr(s, ELFCLASS64));
+
}
+

+
GElf_Shdr *
+
gelf_getshdr(Elf_Scn *s, GElf_Shdr *d)
+
{
+
	int ec;
+
	void *sh;
+
	Elf32_Shdr *sh32;
+
	Elf64_Shdr *sh64;
+

+
	if (d == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if ((sh = _libelf_getshdr(s, ELFCLASSNONE)) == NULL)
+
		return (NULL);
+

+
	ec = s->s_elf->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32) {
+
		sh32 = (Elf32_Shdr *) sh;
+

+
		d->sh_name      = sh32->sh_name;
+
		d->sh_type      = sh32->sh_type;
+
		d->sh_flags     = (Elf64_Xword) sh32->sh_flags;
+
		d->sh_addr      = (Elf64_Addr) sh32->sh_addr;
+
		d->sh_offset    = (Elf64_Off) sh32->sh_offset;
+
		d->sh_size      = (Elf64_Xword) sh32->sh_size;
+
		d->sh_link      = sh32->sh_link;
+
		d->sh_info      = sh32->sh_info;
+
		d->sh_addralign = (Elf64_Xword) sh32->sh_addralign;
+
		d->sh_entsize   = (Elf64_Xword) sh32->sh_entsize;
+
	} else {
+
		sh64 = (Elf64_Shdr *) sh;
+
		*d = *sh64;
+
	}
+

+
	return (d);
+
}
+

+
int
+
gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *s)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf32_Shdr *sh32;
+

+

+
	if (s == NULL || scn == NULL || (e = scn->s_elf) == NULL ||
+
	    e->e_kind != ELF_K_ELF ||
+
	    ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (e->e_cmd == ELF_C_READ) {
+
		LIBELF_SET_ERROR(MODE, 0);
+
		return (0);
+
	}
+

+
	(void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY);
+

+
	if (ec == ELFCLASS64) {
+
		scn->s_shdr.s_shdr64 = *s;
+
		return (1);
+
	}
+

+
	sh32 = &scn->s_shdr.s_shdr32;
+

+
	sh32->sh_name	 =  s->sh_name;
+
	sh32->sh_type	 =  s->sh_type;
+
	LIBELF_COPY_U32(sh32, s, sh_flags);
+
	LIBELF_COPY_U32(sh32, s, sh_addr);
+
	LIBELF_COPY_U32(sh32, s, sh_offset);
+
	LIBELF_COPY_U32(sh32, s, sh_size);
+
	sh32->sh_link	 =  s->sh_link;
+
	sh32->sh_info	 =  s->sh_info;
+
	LIBELF_COPY_U32(sh32, s, sh_addralign);
+
	LIBELF_COPY_U32(sh32, s, sh_entsize);
+

+
	return (1);
+
}
added external/libelf/gelf_sym.c
@@ -0,0 +1,154 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_sym.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
GElf_Sym *
+
gelf_getsym(Elf_Data *d, int ndx, GElf_Sym *dst)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Sym *sym32;
+
	Elf64_Sym *sym64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || dst == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_SYM, ec, e->e_version);
+

+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+

+
		sym32 = (Elf32_Sym *) d->d_buf + ndx;
+

+
		dst->st_name  = sym32->st_name;
+
		dst->st_value = (Elf64_Addr) sym32->st_value;
+
		dst->st_size  = (Elf64_Xword) sym32->st_size;
+
		dst->st_info  = ELF64_ST_INFO(ELF32_ST_BIND(sym32->st_info),
+
		    ELF32_ST_TYPE(sym32->st_info));
+
		dst->st_other = sym32->st_other;
+
		dst->st_shndx = sym32->st_shndx;
+
	} else {
+

+
		sym64 = (Elf64_Sym *) d->d_buf + ndx;
+

+
		*dst = *sym64;
+
	}
+

+
	return (dst);
+
}
+

+
int
+
gelf_update_sym(Elf_Data *d, int ndx, GElf_Sym *gs)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Sym *sym32;
+
	Elf64_Sym *sym64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || gs == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	msz = _libelf_msize(ELF_T_SYM, ec, e->e_version);
+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		sym32 = (Elf32_Sym *) d->d_buf + ndx;
+

+
		sym32->st_name  = gs->st_name;
+
		sym32->st_info  = gs->st_info;
+
		sym32->st_other = gs->st_other;
+
		sym32->st_shndx = gs->st_shndx;
+

+
		LIBELF_COPY_U32(sym32, gs, st_value);
+
		LIBELF_COPY_U32(sym32, gs, st_size);
+
	} else {
+
		sym64 = (Elf64_Sym *) d->d_buf + ndx;
+

+
		*sym64 = *gs;
+
	}
+

+
	return (1);
+
}
added external/libelf/gelf_syminfo.c
@@ -0,0 +1,151 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_syminfo.c 165317 2006-12-18 05:40:01Z jkoshy $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+
#include <osreldate.h>
+

+
#include "_libelf.h"
+

+
#if	__FreeBSD_version >= 700025
+

+
GElf_Syminfo *
+
gelf_getsyminfo(Elf_Data *d, int ndx, GElf_Syminfo *dst)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Syminfo *syminfo32;
+
	Elf64_Syminfo *syminfo64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || dst == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version);
+

+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+

+
		syminfo32 = (Elf32_Syminfo *) d->d_buf + ndx;
+

+
		dst->si_boundto = syminfo32->si_boundto;
+
		dst->si_flags   = syminfo32->si_flags;
+

+
	} else {
+

+
		syminfo64 = (Elf64_Syminfo *) d->d_buf + ndx;
+

+
		*dst = *syminfo64;
+
	}
+

+
	return (dst);
+
}
+

+
int
+
gelf_update_syminfo(Elf_Data *d, int ndx, GElf_Syminfo *gs)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	Elf32_Syminfo *syminfo32;
+
	Elf64_Syminfo *syminfo64;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (d == NULL || ndx < 0 || gs == NULL ||
+
	    (scn = d->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version);
+
	assert(msz > 0);
+

+
	if (msz * ndx >= d->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		syminfo32 = (Elf32_Syminfo *) d->d_buf + ndx;
+

+
		syminfo32->si_boundto  = gs->si_boundto;
+
		syminfo32->si_flags  = gs->si_flags;
+

+
	} else {
+
		syminfo64 = (Elf64_Syminfo *) d->d_buf + ndx;
+

+
		*syminfo64 = *gs;
+
	}
+

+
	return (1);
+
}
+

+
#endif	/* __FreeBSD_version >= 700025 */
added external/libelf/gelf_symshndx.c
@@ -0,0 +1,129 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_symshndx.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <sys/limits.h>
+

+
#include <assert.h>
+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
GElf_Sym *
+
gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst,
+
    Elf32_Word *shindex)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (gelf_getsym(d, ndx, dst) == 0)
+
		return (NULL);
+

+
	if (id == NULL || (scn = id->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL || (e != d->d_scn->s_elf) ||
+
	    shindex == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
+
	   id->d_type != ELF_T_WORD) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_WORD, ec, e->e_version);
+

+
	assert(msz > 0);
+

+
	if (msz * ndx >= id->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	*shindex = ((Elf32_Word *) id->d_buf)[ndx];
+

+
	return (dst);
+
}
+

+
int
+
gelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs,
+
    Elf32_Word xindex)
+
{
+
	int ec;
+
	Elf *e;
+
	Elf_Scn *scn;
+
	size_t msz;
+
	uint32_t sh_type;
+

+
	if (gelf_update_sym(d, ndx, gs) == 0)
+
		return (0);
+

+
	if (id == NULL || (scn = id->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL || (e != d->d_scn->s_elf)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	ec = e->e_class;
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (ec == ELFCLASS32)
+
		sh_type = scn->s_shdr.s_shdr32.sh_type;
+
	else
+
		sh_type = scn->s_shdr.s_shdr64.sh_type;
+

+
	if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD ||
+
	    d->d_type != ELF_T_WORD) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	msz = _libelf_msize(ELF_T_WORD, ec, e->e_version);
+
	assert(msz > 0);
+

+
	if (msz * ndx >= id->d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	*(((Elf32_Word *) id->d_buf) + ndx) = xindex;
+

+
	return (1);
+
}
added external/libelf/gelf_update_ehdr.3
@@ -0,0 +1,123 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_update_ehdr.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd August 27, 2006
+
.Dt GELF_UPDATE_EHDR 3
+
.Os
+
.Sh NAME
+
.Nm gelf_update_ehdr ,
+
.Nm gelf_update_phdr ,
+
.Nm gelf_update_shdr
+
.Nd update underlying ELF data structures
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In gelf.h
+
.Ft int
+
.Fn gelf_update_ehdr "Elf *elf" "GElf_Ehdr *ehdr"
+
.Ft int
+
.Fn gelf_update_phdr "Elf *elf" "int ndx" "GElf_Phdr *phdr"
+
.Ft int
+
.Fn gelf_update_shdr "Elf_Scn *scn" "GElf_Shdr *shdr"
+
.Sh DESCRIPTION
+
These functions are used to update ELF data structures on the underlying
+
ELF descriptor.
+
Class-dependent data structures in the underlying ELF descriptor
+
are updated using the data in the class-independent GElf descriptors
+
and the underlying ELF data structures are marked
+
.Dq dirty .
+
The conversion process signals an error if the values being copied
+
to the target ELF data structure would exceed representation
+
limits.
+
GElf descriptors are described in
+
.Xr gelf 3 .
+
.Pp
+
Function
+
.Fn gelf_update_ehdr
+
updates the ELF Executable Header with the values in the
+
class-independent executable header
+
.Ar ehdr .
+
.Pp
+
Function
+
.Fn gelf_update_phdr
+
updates the ELF Program Header structure at index
+
.Ar ndx
+
with the values in the class-independent program header
+
.Ar phdr .
+
.Pp
+
Function
+
.Fn gelf_update_shdr
+
updates the ELF Section Header structure associated with section
+
descriptor
+
.Ar scn
+
with the values in argument
+
.Ar shdr .
+
.Sh RETURN VALUES
+
These functions return a non-zero integer on success, or zero in case
+
of an error.
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar elf ,
+
.Ar ehdr ,
+
.Ar phdr ,
+
.Ar scn ,
+
or
+
.Ar shdr
+
were NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was not a descriptor for an ELF object.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
had an unsupported ELF class.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar ndx
+
exceeded the number of entries in the program header table.
+
.It Bq Er ELF_E_ARGUMENT
+
Section descriptor
+
.Ar scn
+
was not associated with an ELF descriptor.
+
.It Bq Er ELF_E_MODE
+
ELF descriptor
+
.Ar elf
+
was not opened for writing or updating.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_flagelf 3 ,
+
.Xr elf_flagphdr 3 ,
+
.Xr elf_flagshdr 3 ,
+
.Xr gelf 3 ,
+
.Xr gelf_getehdr 3 ,
+
.Xr gelf_getphdr 3 ,
+
.Xr gelf_getshdr 3
added external/libelf/gelf_xlate.c
@@ -0,0 +1,79 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/gelf_xlate.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <gelf.h>
+
#include <libelf.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+

+
Elf_Data *
+
elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
+
{
+
	return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOFILE);
+
}
+

+
Elf_Data *
+
elf64_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
+
{
+
	return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOFILE);
+
}
+

+
Elf_Data *
+
elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
+
{
+
	return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOMEMORY);
+
}
+

+
Elf_Data *
+
elf64_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
+
{
+
	return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOMEMORY);
+
}
+

+
Elf_Data *
+
gelf_xlatetom(Elf *e, Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
+
{
+
	if (e != NULL)
+
		return (_libelf_xlate(dst, src, encoding, e->e_class,
+
		    ELF_TOMEMORY));
+
	LIBELF_SET_ERROR(ARGUMENT, 0);
+
	return (NULL);
+
}
+

+
Elf_Data *
+
gelf_xlatetof(Elf *e, Elf_Data *dst, const Elf_Data *src, unsigned int encoding)
+
{
+
	if (e != NULL)
+
		return (_libelf_xlate(dst, src, encoding, e->e_class,
+
		    ELF_TOFILE));
+
	LIBELF_SET_ERROR(ARGUMENT, 0);
+
	return (NULL);
+
}
added external/libelf/gelf_xlatetof.3
@@ -0,0 +1,247 @@
+
.\" Copyright (c) 2006 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.
+
.\"
+
.\" $FreeBSD: releng/9.1/lib/libelf/gelf_xlatetof.3 206622 2010-04-14 19:08:06Z uqs $
+
.\"
+
.Dd July 24, 2006
+
.Dt GELF_XLATETOF 3
+
.Os
+
.Sh NAME
+
.Nm elf32_xlate ,
+
.Nm elf64_xlate ,
+
.Nm gelf_xlate
+
.Nd translate data between files and memory
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf_Data *"
+
.Fn elf32_xlatetof "Elf_Data *dst" "Elf_Data *src" "unsigned int encode"
+
.Ft "Elf_Data *"
+
.Fn elf32_xlatetom "Elf_Data *dst" "Elf_Data *src" "unsigned int encode"
+
.Ft "Elf_Data *"
+
.Fn elf64_xlatetof "Elf_Data *dst" "Elf_Data *src" "unsigned int encode"
+
.Ft "Elf_Data *"
+
.Fn elf64_xlatetom "Elf_Data *dst" "Elf_Data *src" "unsigned int encode"
+
.In gelf.h
+
.Ft "Elf_Data *"
+
.Fo gelf_xlatetof
+
.Fa "Elf *elf"
+
.Fa "Elf_Data *dst"
+
.Fa "Elf_Data *src"
+
.Fa "unsigned int encode"
+
.Fc
+
.Ft "Elf_Data *"
+
.Fo gelf_xlatetom
+
.Fa "Elf *elf"
+
.Fa "Elf_Data *dst"
+
.Fa "Elf_Data *src"
+
.Fa "unsigned int encode"
+
.Fc
+
.Sh DESCRIPTION
+
These functions translate between the file and memory representations
+
of ELF data structures.
+
The in-memory representation of an ELF data structure would confirm to
+
the byte ordering and data alignment restrictions dictated by the host
+
processor.
+
A file representation of the same data structure could use a non-native byte
+
ordering and in addition may be laid out differently with the file.
+
.Pp
+
Functions
+
.Fn elf32_xlatetom ,
+
.Fn elf64_xlatetom ,
+
and
+
.Fn gelf_xlatetom
+
translate data from file representations to native, in-memory representations.
+
Functions
+
.Fn elf32_xlatetof ,
+
.Fn elf64_xlatetof ,
+
and
+
.Fn gelf_xlatetof
+
translate data from in-memory representations to file representations.
+
.Pp
+
Argument
+
.Ar src
+
denotes an
+
.Vt Elf_Data
+
descriptor describing the source to be translated.
+
The following elements of the descriptor need to be set before
+
invoking these functions:
+
.Bl -hang -offset indent
+
.It Va d_buf
+
Set to a valid pointer value denoting the beginning of the data area
+
to be translated.
+
.It Va d_size
+
Set to the total size in bytes of the source data area to be
+
translated.
+
.It Va d_type
+
Set to the type of the source data being translated.
+
This value is one of the values defined in the
+
.Vt Elf_Type
+
enumeration.
+
The
+
.Vt Elf_Type
+
enumeration is described in
+
.Xr elf 3 .
+
.It Va d_version
+
Set to the version number of the ELF data structures being
+
translated.
+
Currently only version
+
.Dv EV_CURRENT
+
is supported.
+
.El
+
.Pp
+
Argument
+
.Ar dst
+
describes the destination buffer.
+
The following elements of the
+
.Vt Elf_Data
+
descriptor need to be set before invoking these functions:
+
.Bl -hang -offset indent
+
.It Va d_buf
+
Set to a valid pointer value that denotes the start of the destination
+
buffer that will hold translated data.
+
This value may be the same as that of the source buffer, in which case
+
an in-place conversion will be attempted.
+
.It Va d_size
+
Set to the size of the destination buffer in bytes.
+
This value will be modified if the function call succeeds.
+
.It Va d_version
+
Set to the desired version number of the destination.
+
Currently only version
+
.Dv EV_CURRENT
+
is supported.
+
.El
+
.Pp
+
These translations routines allow the source and destination buffers
+
to coincide, in which case an in-place translation will be done
+
if the destination is large enough to hold the translated data.
+
Other kinds of overlap between the source and destination buffers
+
are not permitted.
+
.Pp
+
On successful completion of the translation request the following
+
fields of the
+
.Ar dst
+
descriptor would be modified:
+
.Bl -hang -offset indent
+
.It Va d_size
+
Set to the size in bytes of the translated data.
+
.It Va d_type
+
Set to the
+
.Va d_type
+
value of the source data descriptor.
+
.El
+
.Pp
+
Argument
+
.Ar encode
+
specifies the encoding in which the file objects are represented.
+
It must be one of:
+
.Bl -hang -offset indent
+
.It Dv ELFDATANONE
+
File objects use the library's native byte ordering.
+
.It Dv ELFDATA2LSB
+
File objects use a little-endian ordering.
+
.It Dv ELFDATA2MSB
+
File objects use a big-endian ordering.
+
.El
+
.Pp
+
The functions
+
.Fn gelf_xlatetof
+
and
+
.Fn gelf_xlatetom
+
select the appropriate 32 or 64 bit translations based on the class of argument
+
.Ar elf .
+
.Sh RETURN VALUES
+
These functions return argument
+
.Ar dst
+
if successful, or NULL in case of an error.
+
.Sh EXAMPLES
+
TODO
+
.Sh ERRORS
+
These functions may fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARGUMENT
+
One of arguments
+
.Ar src ,
+
.Ar dst
+
or
+
.Ar elf
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Arguments
+
.Ar src
+
and
+
.Ar dst
+
were equal.
+
.It Bq Er ELF_E_ARGUMENT
+
The desired encoding parameter was not one of
+
.Dv ELFDATANONE ,
+
.Dv ELFDATA2LSB
+
or
+
.Dv ELFDATA2MSB .
+
.It Bq Er ELF_E_ARGUMENT
+
The
+
.Ar d_type
+
field of argument
+
.Ar src
+
specified an unsupported type.
+
.It Bq Er ELF_E_DATA
+
The
+
.Ar src
+
argument specified a buffer size that was not an integral multiple of
+
its underlying type.
+
.It Bq Er ELF_E_DATA
+
The
+
.Ar dst
+
argument specified a buffer size that was too small.
+
.It Bq Er ELF_E_DATA
+
Argument
+
.Ar dst
+
specified a destination buffer that overlaps with the source
+
buffer.
+
.It Bq Er ELF_E_DATA
+
The destination buffer for a conversion to memory had an alignment
+
inappropriate for the underlying ELF type.
+
.It Bq Er ELF_E_DATA
+
The source buffer for a conversion to file had an alignment
+
inappropriate for the underlying ELF type.
+
.It Bq Er ELF_E_UNIMPL
+
The version numbers for arguments
+
.Ar dst
+
and
+
.Ar src
+
were not identical.
+
.It Bq Er ELF_E_UNIMPL
+
The argument
+
.Ar src
+
requested conversion for a type which is not currently
+
supported.
+
.It Bq Er ELF_E_VERSION
+
Argument
+
.Ar src
+
specified an unsupported version number.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_getdata 3 ,
+
.Xr gelf 3
added external/libelf/libelf.c
@@ -0,0 +1,61 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <sys/param.h>
+

+
#include <machine/elf.h>
+
#include <machine/endian.h>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
struct _libelf_globals _libelf = {
+
	.libelf_arch		= ELF_ARCH,
+
	.libelf_byteorder	= ELF_TARG_DATA,
+
	.libelf_class		= ELF_TARG_CLASS,
+
	.libelf_error		= 0,
+
	.libelf_fillchar	= 0,
+
	.libelf_version		= EV_NONE
+
};
+

+

+
#if defined(LIBELF_TEST_HOOKS)
+
int
+
_libelf_get_elf_class(Elf *elf)
+
{
+
	return elf->e_class;
+
}
+

+
void
+
_libelf_set_elf_class(Elf *elf, int c)
+
{
+
	elf->e_class = c;
+
}
+
#endif	/* LIBELF_TEST_HOOKS */
added external/libelf/libelf.h
@@ -0,0 +1,255 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 *
+
 * $FreeBSD: releng/9.1/lib/libelf/libelf.h 210345 2010-07-21 12:14:50Z kaiw $
+
 */
+

+
#ifndef	_LIBELF_H_
+
#define	_LIBELF_H_
+

+
#include <sys/types.h>
+
#include <sys/elf32.h>
+
#include <sys/elf64.h>
+
#include <sys/queue.h>
+

+
/* Library private data structures */
+
typedef struct _Elf Elf;
+
typedef struct _Elf_Scn Elf_Scn;
+

+
/* File types */
+
typedef enum {
+
	ELF_K_NONE = 0,
+
	ELF_K_AR,	/* `ar' archives */
+
	ELF_K_COFF,	/* COFF files (unsupported) */
+
	ELF_K_ELF,	/* ELF files */
+
	ELF_K_NUM
+
} Elf_Kind;
+

+
#define	ELF_K_FIRST	ELF_K_NONE
+
#define	ELF_K_LAST	ELF_K_NUM
+

+
/* Data types */
+
typedef enum {
+
	ELF_T_ADDR,
+
	ELF_T_BYTE,
+
	ELF_T_CAP,
+
	ELF_T_DYN,
+
	ELF_T_EHDR,
+
	ELF_T_HALF,
+
	ELF_T_LWORD,
+
	ELF_T_MOVE,
+
	ELF_T_MOVEP,
+
	ELF_T_NOTE,
+
	ELF_T_OFF,
+
	ELF_T_PHDR,
+
	ELF_T_REL,
+
	ELF_T_RELA,
+
	ELF_T_SHDR,
+
	ELF_T_SWORD,
+
	ELF_T_SXWORD,
+
	ELF_T_SYMINFO,
+
	ELF_T_SYM,
+
	ELF_T_VDEF,
+
	ELF_T_VNEED,
+
	ELF_T_WORD,
+
	ELF_T_XWORD,
+
	ELF_T_GNUHASH,	/* GNU style hash tables. */
+
	ELF_T_NUM
+
} Elf_Type;
+

+
#define	ELF_T_FIRST	ELF_T_ADDR
+
#define	ELF_T_LAST	ELF_T_GNUHASH
+

+
/* Commands */
+
typedef enum {
+
	ELF_C_NULL = 0,
+
	ELF_C_CLR,
+
	ELF_C_FDDONE,
+
	ELF_C_FDREAD,
+
	ELF_C_RDWR,
+
	ELF_C_READ,
+
	ELF_C_SET,
+
	ELF_C_WRITE,
+
	ELF_C_NUM
+
} Elf_Cmd;
+

+
#define	ELF_C_FIRST	ELF_C_NULL
+
#define	ELF_C_LAST	ELF_C_NUM
+

+
/*
+
 * An `Elf_Data' structure describes data in an
+
 * ELF section.
+
 */
+
typedef struct _Elf_Data {
+
	/*
+
	 * `Public' members that are part of the ELF(3) API.
+
	 */
+
	uint64_t	d_align;
+
	void		*d_buf;
+
	uint64_t	d_off;
+
	uint64_t	d_size;
+
	Elf_Type	d_type;
+
	unsigned int	d_version;
+

+
	/*
+
	 * Members that are not part of the public API.
+
	 */
+
	Elf_Scn		*d_scn;		/* containing section */
+
	unsigned int	d_flags;
+
	STAILQ_ENTRY(_Elf_Data)	d_next;
+
} Elf_Data;
+

+
/*
+
 * An `Elf_Arhdr' structure describes an archive
+
 * header.
+
 */
+
typedef struct {
+
	time_t		ar_date;
+
	char		*ar_name;	/* archive member name */
+
	gid_t		ar_gid;
+
	mode_t		ar_mode;
+
	char		*ar_rawname;	/* 'raw' member name */
+
	size_t		ar_size;
+
	uid_t		ar_uid;
+
} Elf_Arhdr;
+

+
/*
+
 * An `Elf_Arsym' describes an entry in the archive
+
 * symbol table.
+
 */
+
typedef struct {
+
	off_t		as_off;		/* byte offset to member's header */
+
	unsigned long	as_hash;	/* elf_hash() value for name */
+
	char		*as_name; 	/* null terminated symbol name */
+
} Elf_Arsym;
+

+
/*
+
 * Error numbers.
+
 */
+

+
enum Elf_Error {
+
	ELF_E_NONE,	/* No error */
+
	ELF_E_ARCHIVE,	/* Malformed ar(1) archive */
+
	ELF_E_ARGUMENT,	/* Invalid argument */
+
	ELF_E_CLASS,	/* Mismatched ELF class */
+
	ELF_E_DATA,	/* Invalid data descriptor */
+
	ELF_E_HEADER,	/* Missing or malformed ELF header */
+
	ELF_E_IO,	/* I/O error */
+
	ELF_E_LAYOUT,	/* Layout constraint violation */
+
	ELF_E_MODE,	/* Wrong mode for ELF descriptor */
+
	ELF_E_RANGE,	/* Value out of range */
+
	ELF_E_RESOURCE,	/* Resource exhaustion */
+
	ELF_E_SECTION,	/* Invalid section descriptor */
+
	ELF_E_SEQUENCE,	/* API calls out of sequence */
+
	ELF_E_UNIMPL,	/* Feature is unimplemented */
+
	ELF_E_VERSION,	/* Unknown API version */
+
	ELF_E_NUM	/* Max error number */
+
};
+

+
/*
+
 * Flags defined by the API.
+
 */
+

+
#define	ELF_F_LAYOUT	0x001U	/* application will layout the file */
+
#define	ELF_F_DIRTY	0x002U	/* a section or ELF file is dirty */
+

+
__BEGIN_DECLS
+
Elf		*elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf);
+
int		elf_cntl(Elf *_elf, Elf_Cmd _cmd);
+
int		elf_end(Elf *_elf);
+
const char	*elf_errmsg(int _error);
+
int		elf_errno(void);
+
void		elf_fill(int _fill);
+
unsigned int	elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd, unsigned int _flags);
+
unsigned int	elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
+
unsigned int	elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
+
unsigned int	elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags);
+
unsigned int	elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
+
unsigned int	elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags);
+
Elf_Arhdr	*elf_getarhdr(Elf *_elf);
+
Elf_Arsym	*elf_getarsym(Elf *_elf, size_t *_ptr);
+
off_t		elf_getbase(Elf *_elf);
+
Elf_Data	*elf_getdata(Elf_Scn *, Elf_Data *);
+
char		*elf_getident(Elf *_elf, size_t *_ptr);
+
int		elf_getphdrnum(Elf *_elf, size_t *_dst);
+
int		elf_getphnum(Elf *_elf, size_t *_dst);	/* Deprecated */
+
Elf_Scn		*elf_getscn(Elf *_elf, size_t _index);
+
int		elf_getshdrnum(Elf *_elf, size_t *_dst);
+
int		elf_getshnum(Elf *_elf, size_t *_dst);	/* Deprecated */
+
int		elf_getshdrstrndx(Elf *_elf, size_t *_dst);
+
int		elf_getshstrndx(Elf *_elf, size_t *_dst); /* Deprecated */
+
unsigned long	elf_hash(const char *_name);
+
Elf_Kind	elf_kind(Elf *_elf);
+
Elf		*elf_memory(char *_image, size_t _size);
+
size_t		elf_ndxscn(Elf_Scn *_scn);
+
Elf_Data	*elf_newdata(Elf_Scn *_scn);
+
Elf_Scn		*elf_newscn(Elf *_elf);
+
Elf_Scn		*elf_nextscn(Elf *_elf, Elf_Scn *_scn);
+
Elf_Cmd		elf_next(Elf *_elf);
+
off_t		elf_rand(Elf *_elf, off_t _off);
+
Elf_Data	*elf_rawdata(Elf_Scn *_scn, Elf_Data *_data);
+
char		*elf_rawfile(Elf *_elf, size_t *_size);
+
int		elf_setshstrndx(Elf *_elf, size_t _shnum);
+
char		*elf_strptr(Elf *_elf, size_t _section, size_t _offset);
+
off_t		elf_update(Elf *_elf, Elf_Cmd _cmd);
+
unsigned int	elf_version(unsigned int _version);
+

+
long		elf32_checksum(Elf *_elf);
+
size_t		elf32_fsize(Elf_Type _type, size_t _count,
+
			unsigned int _version);
+
Elf32_Ehdr	*elf32_getehdr(Elf *_elf);
+
Elf32_Phdr	*elf32_getphdr(Elf *_elf);
+
Elf32_Shdr	*elf32_getshdr(Elf_Scn *_scn);
+
Elf32_Ehdr	*elf32_newehdr(Elf *_elf);
+
Elf32_Phdr	*elf32_newphdr(Elf *_elf, size_t _count);
+
Elf_Data	*elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
+
			unsigned int _enc);
+
Elf_Data	*elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
+
			unsigned int _enc);
+

+
long		elf64_checksum(Elf *_elf);
+
size_t		elf64_fsize(Elf_Type _type, size_t _count,
+
			unsigned int _version);
+
Elf64_Ehdr	*elf64_getehdr(Elf *_elf);
+
Elf64_Phdr	*elf64_getphdr(Elf *_elf);
+
Elf64_Shdr	*elf64_getshdr(Elf_Scn *_scn);
+
Elf64_Ehdr	*elf64_newehdr(Elf *_elf);
+
Elf64_Phdr	*elf64_newphdr(Elf *_elf, size_t _count);
+
Elf_Data	*elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src,
+
			unsigned int _enc);
+
Elf_Data	*elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src,
+
			unsigned int _enc);
+

+
#if	defined(LIBELF_TEST_HOOKS)
+
int		_libelf_get_elf_class(Elf *_elf);
+
int		_libelf_get_max_error(void);
+
const char	*_libelf_get_no_error_message(void);
+
const char	*_libelf_get_unknown_error_message(void);
+
void		_libelf_set_elf_class(Elf *_elf, int _class);
+
void		_libelf_set_error(int _error);
+
#endif	/* LIBELF_TEST_HOOKS */
+
__END_DECLS
+

+
#endif	/* _LIBELF_H_ */
added external/libelf/libelf_align.c
@@ -0,0 +1,164 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_align.c 210332 2010-07-21 09:56:42Z kaiw $");
+

+
#include <sys/types.h>
+

+
#include <machine/elf.h>
+
#include <machine/endian.h>
+

+
#include <libelf.h>
+
#include <osreldate.h>
+

+
#include "_libelf.h"
+

+
struct align {
+
	int a32;
+
	int a64;
+
};
+

+
#ifdef	__GNUC__
+
#define	MALIGN(N)	{					\
+
		.a32 = __alignof__(Elf32_##N),			\
+
		.a64 = __alignof__(Elf64_##N)			\
+
	}
+
#define	MALIGN64(V)	  {					\
+
		.a32 = 0,					\
+
		.a64 = __alignof__(Elf64_##V)			\
+
	}
+
#define	MALIGN_WORD()	{					\
+
		.a32 = __alignof__(int32_t),			\
+
		.a64 = __alignof__(int64_t)			\
+
	    }
+
#else
+
#error	Need the __alignof__ builtin.
+
#endif
+
#define	UNSUPPORTED()	{					\
+
		.a32 = 0,					\
+
		.a64 = 0					\
+
	}
+

+
static struct align malign[ELF_T_NUM] = {
+
	[ELF_T_ADDR]	= MALIGN(Addr),
+
	[ELF_T_BYTE]	= { .a32 = 1, .a64 = 1 },
+
#if	__FreeBSD_version >= 700025
+
	[ELF_T_CAP]	= MALIGN(Cap),
+
#endif
+
	[ELF_T_DYN]	= MALIGN(Dyn),
+
	[ELF_T_EHDR]	= MALIGN(Ehdr),
+
	[ELF_T_HALF]	= MALIGN(Half),
+
#if	__FreeBSD_version >= 700025
+
	[ELF_T_LWORD]	= MALIGN(Lword),
+
	[ELF_T_MOVE]	= MALIGN(Move),
+
#endif
+
	[ELF_T_MOVEP] 	= UNSUPPORTED(),
+
#if	__FreeBSD_version >= 700025
+
	[ELF_T_NOTE]	= MALIGN(Nhdr),
+
#endif
+
	[ELF_T_OFF]	= MALIGN(Off),
+
	[ELF_T_PHDR]	= MALIGN(Phdr),
+
	[ELF_T_REL]	= MALIGN(Rel),
+
	[ELF_T_RELA]	= MALIGN(Rela),
+
	[ELF_T_SHDR]	= MALIGN(Shdr),
+
	[ELF_T_SWORD]	= MALIGN(Sword),
+
	[ELF_T_SXWORD]	= MALIGN64(Sxword),
+
	[ELF_T_SYM]	= MALIGN(Sym),
+
#if	__FreeBSD_version >= 700025
+
	[ELF_T_SYMINFO]	= MALIGN(Syminfo),
+
#endif
+
#if	__FreeBSD_version >= 700009
+
	[ELF_T_VDEF]	= MALIGN(Verdef),
+
	[ELF_T_VNEED]	= MALIGN(Verneed),
+
#endif
+
	[ELF_T_WORD]	= MALIGN(Word),
+
	[ELF_T_XWORD]	= MALIGN64(Xword),
+
#if	__FreeBSD_version >= 800062
+
	[ELF_T_GNUHASH] = MALIGN_WORD()
+
#endif
+
};
+

+
int
+
_libelf_malign(Elf_Type t, int elfclass)
+
{
+
	if (t >= ELF_T_NUM || (int) t < 0)
+
		return (0);
+

+
	return (elfclass == ELFCLASS32 ? malign[t].a32 :
+
	    malign[t].a64);
+
}
+

+
#define	FALIGN(A32,A64)	{ .a32 = (A32), .a64 = (A64) }
+

+
static struct align falign[ELF_T_NUM] = {
+
	[ELF_T_ADDR]	= FALIGN(4,8),
+
	[ELF_T_BYTE]	= FALIGN(1,1),
+
#if	__FreeBSD_version >= 700025
+
	[ELF_T_CAP]	= FALIGN(4,8),
+
#endif
+
	[ELF_T_DYN]	= FALIGN(4,8),
+
	[ELF_T_EHDR]	= FALIGN(4,8),
+
	[ELF_T_HALF]	= FALIGN(2,2),
+
#if	__FreeBSD_version >= 700025
+
	[ELF_T_LWORD]	= FALIGN(8,8),
+
	[ELF_T_MOVE]	= FALIGN(8,8),
+
#endif
+
	[ELF_T_MOVEP] 	= UNSUPPORTED(),
+
#if	__FreeBSD_version >= 700025
+
	[ELF_T_NOTE]	= FALIGN(1,1),
+
#endif
+
	[ELF_T_OFF]	= FALIGN(4,8),
+
	[ELF_T_PHDR]	= FALIGN(4,8),
+
	[ELF_T_REL]	= FALIGN(4,8),
+
	[ELF_T_RELA]	= FALIGN(4,8),
+
	[ELF_T_SHDR]	= FALIGN(4,8),
+
	[ELF_T_SWORD]	= FALIGN(4,4),
+
	[ELF_T_SXWORD]	= FALIGN(0,8),
+
	[ELF_T_SYM]	= FALIGN(4,8),
+
#if	__FreeBSD_version >= 700025
+
	[ELF_T_SYMINFO]	= FALIGN(2,2),
+
#endif
+
#if	__FreeBSD_version >= 700009
+
	[ELF_T_VDEF]	= FALIGN(4,4),
+
	[ELF_T_VNEED]	= FALIGN(4,4),
+
#endif
+
	[ELF_T_WORD]	= FALIGN(4,4),
+
	[ELF_T_XWORD]	= FALIGN(0,8),
+
#if	__FreeBSD_version >= 800062
+
	[ELF_T_GNUHASH] = FALIGN(4,8)
+
#endif
+
};
+

+
int
+
_libelf_falign(Elf_Type t, int elfclass)
+
{
+
	if (t >= ELF_T_NUM || (int) t < 0)
+
		return (0);
+

+
	return (elfclass == ELFCLASS32 ? falign[t].a32 :
+
	    falign[t].a64);
+
}
added external/libelf/libelf_allocate.c
@@ -0,0 +1,209 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
/*
+
 * Internal APIs
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_allocate.c 166863 2007-02-21 08:14:22Z dumbbell $");
+

+
#include <sys/errno.h>
+

+
#include <assert.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
Elf *
+
_libelf_allocate_elf(void)
+
{
+
	Elf *e;
+

+
	if ((e = malloc(sizeof(*e))) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, errno);
+
		return NULL;
+
	}
+

+
	e->e_activations = 1;
+
	e->e_arhdr	 = NULL;
+
	e->e_byteorder   = ELFDATANONE;
+
	e->e_class       = ELFCLASSNONE;
+
	e->e_cmd         = ELF_C_NULL;
+
	e->e_fd          = -1;
+
	e->e_flags	 = 0;
+
	e->e_kind        = ELF_K_NONE;
+
	e->e_parent      = NULL;
+
	e->e_rawfile     = NULL;
+
	e->e_rawsize     = 0;
+
	e->e_version     = LIBELF_PRIVATE(version);
+

+
	(void) memset(&e->e_u, 0, sizeof(e->e_u));
+

+
	return (e);
+
}
+

+
void
+
_libelf_init_elf(Elf *e, Elf_Kind kind)
+
{
+
	assert(e != NULL);
+
	assert(e->e_kind == ELF_K_NONE);
+

+
	e->e_kind = kind;
+

+
	switch (kind) {
+
	case ELF_K_ELF:
+
		STAILQ_INIT(&e->e_u.e_elf.e_scn);
+
		break;
+
	default:
+
		break;
+
	}
+
}
+

+
#define	FREE(P)		do {				\
+
		if (P)					\
+
			free(P);			\
+
	} while (0)
+

+

+
Elf *
+
_libelf_release_elf(Elf *e)
+
{
+
	switch (e->e_kind) {
+
	case ELF_K_AR:
+
		FREE(e->e_u.e_ar.e_symtab);
+
		break;
+

+
	case ELF_K_ELF:
+
		switch (e->e_class) {
+
		case ELFCLASS32:
+
			FREE(e->e_u.e_elf.e_ehdr.e_ehdr32);
+
			FREE(e->e_u.e_elf.e_phdr.e_phdr32);
+
			break;
+
		case ELFCLASS64:
+
			FREE(e->e_u.e_elf.e_ehdr.e_ehdr64);
+
			FREE(e->e_u.e_elf.e_phdr.e_phdr64);
+
			break;
+
		}
+

+
		assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
+

+
		if (e->e_arhdr) {
+
			FREE(e->e_arhdr->ar_name);
+
			FREE(e->e_arhdr->ar_rawname);
+
			free(e->e_arhdr);
+
		}
+

+
		break;
+

+
	default:
+
		break;
+
	}
+

+
	free(e);
+

+
	return (NULL);
+
}
+

+
Elf_Data *
+
_libelf_allocate_data(Elf_Scn *s)
+
{
+
	Elf_Data *d;
+

+
	if ((d = calloc((size_t) 1, sizeof(Elf_Data))) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	d->d_scn = s;
+

+
	return (d);
+
}
+

+
Elf_Data *
+
_libelf_release_data(Elf_Data *d)
+
{
+

+
	if (d->d_flags & LIBELF_F_MALLOCED)
+
		free(d->d_buf);
+

+
	free(d);
+

+
	return (NULL);
+
}
+

+
Elf_Scn *
+
_libelf_allocate_scn(Elf *e, size_t ndx)
+
{
+
	Elf_Scn *s;
+

+
	if ((s = calloc((size_t) 1, sizeof(Elf_Scn))) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, errno);
+
		return (NULL);
+
	}
+

+
	s->s_elf = e;
+
	s->s_ndx = ndx;
+

+
	STAILQ_INIT(&s->s_data);
+
	STAILQ_INIT(&s->s_rawdata);
+

+
	STAILQ_INSERT_TAIL(&e->e_u.e_elf.e_scn, s, s_next);
+

+
	return (s);
+
}
+

+
Elf_Scn *
+
_libelf_release_scn(Elf_Scn *s)
+
{
+
	Elf *e;
+
	Elf_Data *d, *td;
+

+
	assert(s != NULL);
+

+
	STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
+
		STAILQ_REMOVE(&s->s_data, d, _Elf_Data, d_next);
+
		d = _libelf_release_data(d);
+
	}
+

+
	STAILQ_FOREACH_SAFE(d, &s->s_rawdata, d_next, td) {
+
		assert((d->d_flags & LIBELF_F_MALLOCED) == 0);
+
		STAILQ_REMOVE(&s->s_rawdata, d, _Elf_Data, d_next);
+
		d = _libelf_release_data(d);
+
	}
+

+
	e = s->s_elf;
+

+
	assert(e != NULL);
+

+
	STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn, s_next);
+

+
	free(s);
+

+
	return (NULL);
+
}
added external/libelf/libelf_ar.c
@@ -0,0 +1,272 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS `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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_ar.c 210348 2010-07-21 12:54:34Z kaiw $");
+

+
#include <ar.h>
+
#include <assert.h>
+
#include <ctype.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
#define	LIBELF_NALLOC_SIZE	16
+

+
/*
+
 * `ar' archive handling.
+
 *
+
 * `ar' archives start with signature `ARMAG'.  Each archive member is
+
 * preceded by a header containing meta-data for the member.  This
+
 * header is described in <ar.h> (struct ar_hdr).  The header always
+
 * starts on an even address.  File data is padded with "\n"
+
 * characters to keep this invariant.
+
 *
+
 * Special considerations for `ar' archives:
+
 *
+
 * The `ar' header only has space for a 16 character file name.  File
+
 * names are terminated with a '/', so this effectively leaves 15
+
 * characters for the actual file name.  In order to accomodate longer
+
 * file names, names may be stored in a separate 'string table' and
+
 * referenced indirectly by a member header.  The string table itself
+
 * appears as an archive member with name "// ".  An indirect file name
+
 * in an `ar' header matches the pattern "/[0-9]*". The digits form a
+
 * decimal number that corresponds to a byte offset into the string
+
 * table where the actual file name of the object starts.  Strings in
+
 * the string table are padded to start on even addresses.
+
 *
+
 * Archives may also have a symbol table (see ranlib(1)), mapping
+
 * program symbols to object files inside the archive.  A symbol table
+
 * uses a file name of "/ " in its archive header.  The symbol table
+
 * is structured as:
+
 *  - a 4-byte count of entries stored as a binary value, MSB first
+
 *  - 'n' 4-byte offsets, stored as binary values, MSB first
+
 *  - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded.
+
 *
+
 * If the symbol table and string table are is present in an archive
+
 * they must be the very first objects and in that order.
+
 */
+

+

+

+
Elf_Arhdr *
+
_libelf_ar_gethdr(Elf *e)
+
{
+
	Elf *parent;
+
	struct ar_hdr *arh;
+
	Elf_Arhdr *eh;
+
	size_t n;
+

+
	if ((parent = e->e_parent) == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	arh = (struct ar_hdr *) ((uintptr_t) e->e_rawfile - sizeof(struct ar_hdr));
+

+
	assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG);
+
	assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile + parent->e_rawsize -
+
	    sizeof(struct ar_hdr));
+

+
	if ((eh = malloc(sizeof(Elf_Arhdr))) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	e->e_arhdr = eh;
+
	eh->ar_name = eh->ar_rawname = NULL;
+

+
	if ((eh->ar_name = _libelf_ar_get_name(arh->ar_name, sizeof(arh->ar_name),
+
		 parent)) == NULL)
+
		goto error;
+

+
	if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10, &n) == 0)
+
		goto error;
+
	eh->ar_uid = (uid_t) n;
+

+
	if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10, &n) == 0)
+
		goto error;
+
	eh->ar_gid = (gid_t) n;
+

+
	if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8, &n) == 0)
+
		goto error;
+
	eh->ar_mode = (mode_t) n;
+

+
	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &n) == 0)
+
		goto error;
+
	eh->ar_size = n;
+

+
	if ((eh->ar_rawname = _libelf_ar_get_string(arh->ar_name,
+
		 sizeof(arh->ar_name), 1)) == NULL)
+
		goto error;
+

+
	return (eh);
+

+
 error:
+
	if (eh) {
+
		if (eh->ar_name)
+
			free(eh->ar_name);
+
		if (eh->ar_rawname)
+
			free(eh->ar_rawname);
+
		free(eh);
+
	}
+
	e->e_arhdr = NULL;
+

+
	return (NULL);
+
}
+

+
Elf *
+
_libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
+
{
+
	Elf *e;
+
	off_t next;
+
	struct ar_hdr *arh;
+
	size_t sz;
+

+
	assert(elf->e_kind == ELF_K_AR);
+

+
	next = elf->e_u.e_ar.e_next;
+

+
	/*
+
	 * `next' is only set to zero by elf_next() when the last
+
	 * member of an archive is processed.
+
	 */
+
	if (next == (off_t) 0)
+
		return (NULL);
+

+
	assert((next & 1) == 0);
+

+
	arh = (struct ar_hdr *) (elf->e_rawfile + next);
+

+
	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, &sz) == 0) {
+
		LIBELF_SET_ERROR(ARCHIVE, 0);
+
		return (NULL);
+
	}
+

+
	assert(sz > 0);
+

+
	arh++;	/* skip over archive member header */
+

+
	if ((e = elf_memory((char *) arh, sz)) == NULL)
+
		return (NULL);
+

+
	e->e_fd = fd;
+
	e->e_cmd = c;
+

+
	elf->e_u.e_ar.e_nchildren++;
+
	e->e_parent = elf;
+

+
	return (e);
+
}
+

+
/*
+
 * An ar(1) symbol table has the following layout:
+
 *
+
 * The first 4 bytes are a binary count of the number of entries in the
+
 * symbol table, stored MSB-first.
+
 *
+
 * Then there are 'n' 4-byte binary offsets, also stored MSB first.
+
 *
+
 * Following this, there are 'n' null-terminated strings.
+
 */
+

+
#define	GET_WORD(P, V) do {			\
+
		(V) = 0;			\
+
		(V) = (P)[0]; (V) <<= 8;	\
+
		(V) += (P)[1]; (V) <<= 8;	\
+
		(V) += (P)[2]; (V) <<= 8;	\
+
		(V) += (P)[3];			\
+
	} while (0)
+

+
#define	INTSZ	4
+

+
Elf_Arsym *
+
_libelf_ar_process_symtab(Elf *e, size_t *count)
+
{
+
	size_t n, nentries, off;
+
	Elf_Arsym *symtab, *sym;
+
	unsigned char  *p, *s, *end;
+

+
	assert(e != NULL);
+
	assert(count != NULL);
+

+
	if (e->e_u.e_ar.e_rawsymtabsz < INTSZ) {
+
		LIBELF_SET_ERROR(ARCHIVE, 0);
+
		return (NULL);
+
	}
+

+
	p = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
+
	end = p + e->e_u.e_ar.e_rawsymtabsz;
+

+
	GET_WORD(p, nentries);
+
	p += INTSZ;
+

+
	if (nentries == 0 || p + nentries * INTSZ >= end) {
+
		LIBELF_SET_ERROR(ARCHIVE, 0);
+
		return (NULL);
+
	}
+

+
	/* Allocate space for a nentries + a sentinel. */
+
	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	s = p + (nentries * INTSZ); /* start of the string table. */
+

+
	for (n = nentries, sym = symtab; n > 0; n--) {
+
		off = 0;
+

+
		GET_WORD(p, off);
+

+
		sym->as_off = off;
+
		sym->as_hash = elf_hash(s);
+
		sym->as_name = s;
+

+
		p += INTSZ;
+
		sym++;
+

+
		for (; s < end && *s++ != '\0';) /* skip to next string */
+
			;
+
		if (s > end) {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			free(symtab);
+
			return (NULL);
+
		}
+
	}
+

+
	/* Fill up the sentinel entry. */
+
	sym->as_name = NULL;
+
	sym->as_hash = ~0UL;
+
	sym->as_off = (off_t) 0;
+

+
	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
+
	e->e_u.e_ar.e_symtab = symtab;
+

+
	return (symtab);
+
}
added external/libelf/libelf_ar_util.c
@@ -0,0 +1,253 @@
+
/*-
+
 * Copyright (c) 2006,2009 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 THE AUTHOR AND CONTRIBUTORS `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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_ar_util.c 210348 2010-07-21 12:54:34Z kaiw $");
+

+
#include <ar.h>
+
#include <assert.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
+
 * number in the specified base.
+
 */
+
int
+
_libelf_ar_get_number(char *s, size_t sz, int base, size_t *ret)
+
{
+
	int c, v;
+
	size_t r;
+
	char *e;
+

+
	assert(base <= 10);
+

+
	e = s + sz;
+

+
	/* skip leading blanks */
+
	for (;s < e && (c = *s) == ' '; s++)
+
		;
+

+
	r = 0L;
+
	for (;s < e; s++) {
+
		if ((c = *s) == ' ')
+
			break;
+
		if (c < '0' || c > '9')
+
			return (0);
+
		v = c - '0';
+
		if (v >= base)		/* Illegal digit. */
+
			break;
+
		r *= base;
+
		r += v;
+
	}
+

+
	*ret = r;
+

+
	return (1);
+
}
+

+
/*
+
 * Retrieve a string from a name field.  If `rawname' is set, leave
+
 * ar(1) control characters in.
+
 */
+
char *
+
_libelf_ar_get_string(const char *buf, size_t bufsize, int rawname)
+
{
+
	const char *q;
+
	char *r;
+
	size_t sz;
+

+
	if (rawname)
+
		sz = bufsize + 1;
+
	else {
+
		/* Skip back over trailing blanks. */
+
		for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
+
			;
+

+
		if (q < buf) {
+
			/*
+
			 * If the input buffer only had blanks in it,
+
			 * return a zero-length string.
+
			 */
+
			buf = "";
+
			sz = 1;
+
		} else {
+
			/*
+
			 * Remove the trailing '/' character, but only
+
			 * if the name isn't one of the special names
+
			 * "/" and "//".
+
			 */
+
			if (q > buf + 1 ||
+
			    (q == (buf + 1) && *buf != '/'))
+
				q--;
+

+
			sz = q - buf + 2; /* Space for a trailing NUL. */
+
		}
+
	}
+

+
	if ((r = malloc(sz)) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	(void) strncpy(r, buf, sz);
+
	r[sz - 1] = '\0';
+

+
	return (r);
+
}
+

+
/*
+
 * Retrieve the full name of the archive member.
+
 */
+
char *
+
_libelf_ar_get_name(char *buf, size_t bufsize, Elf *e)
+
{
+
	char c, *q, *r, *s;
+
	size_t len;
+
	size_t offset;
+

+
	assert(e->e_kind == ELF_K_AR);
+

+
	if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') {
+
		/*
+
		 * The value in field ar_name is a decimal offset into
+
		 * the archive string table where the actual name
+
		 * resides.
+
		 */
+
		if (_libelf_ar_get_number(buf + 1, bufsize - 1, 10,
+
		    &offset) == 0) {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			return (NULL);
+
		}
+

+
		if (offset > e->e_u.e_ar.e_rawstrtabsz) {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			return (NULL);
+
		}
+

+
		s = q = e->e_u.e_ar.e_rawstrtab + offset;
+
		r = e->e_u.e_ar.e_rawstrtab + e->e_u.e_ar.e_rawstrtabsz;
+

+
		for (s = q; s < r && *s != '/'; s++)
+
			;
+
		len = s - q + 1; /* space for the trailing NUL */
+

+
		if ((s = malloc(len)) == NULL) {
+
			LIBELF_SET_ERROR(RESOURCE, 0);
+
			return (NULL);
+
		}
+

+
		(void) strncpy(s, q, len);
+
		s[len - 1] = '\0';
+

+
		return (s);
+
	}
+

+
	/*
+
	 * Normal 'name'
+
	 */
+
	return (_libelf_ar_get_string(buf, bufsize, 0));
+
}
+

+
/*
+
 * Open an 'ar' archive.
+
 */
+
Elf *
+
_libelf_ar_open(Elf *e)
+
{
+
	int i;
+
	char *s, *end;
+
	size_t sz;
+
	struct ar_hdr arh;
+

+
	e->e_kind = ELF_K_AR;
+
	e->e_u.e_ar.e_nchildren = 0;
+
	e->e_u.e_ar.e_next = (off_t) -1;
+

+
	/*
+
	 * Look for special members.
+
	 */
+

+
	s = e->e_rawfile + SARMAG;
+
	end = e->e_rawfile + e->e_rawsize;
+

+
	assert(e->e_rawsize > 0);
+

+
	/*
+
	 * Look for magic names "/ " and "// " in the first two entries
+
	 * of the archive.
+
	 */
+
	for (i = 0; i < 2; i++) {
+

+
		if (s + sizeof(arh) > end) {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			return (NULL);
+
		}
+

+
		(void) memcpy(&arh, s, sizeof(arh));
+

+
		if (arh.ar_fmag[0] != '`' || arh.ar_fmag[1] != '\n') {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			return (NULL);
+
		}
+

+
		if (arh.ar_name[0] != '/')	/* not a special symbol */
+
			break;
+

+
		if (_libelf_ar_get_number(arh.ar_size, sizeof(arh.ar_size),
+
			10, &sz) == 0) {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			return (NULL);
+
		}
+

+
		assert(sz > 0);
+

+
		s += sizeof(arh);
+

+
		if (arh.ar_name[1] == ' ') {	/* "/ " => symbol table */
+

+
			e->e_u.e_ar.e_rawsymtab = s;
+
			e->e_u.e_ar.e_rawsymtabsz = sz;
+

+
		} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
+

+
			/* "// " => string table for long file names */
+
			e->e_u.e_ar.e_rawstrtab = s;
+
			e->e_u.e_ar.e_rawstrtabsz = sz;
+
		}
+

+
		sz = LIBELF_ADJUST_AR_SIZE(sz);
+

+
		s += sz;
+
	}
+

+
	e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
+

+
	return (e);
+
}
added external/libelf/libelf_checksum.c
@@ -0,0 +1,99 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_checksum.c 165535 2006-12-25 02:22:22Z jkoshy $");
+

+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
static unsigned long
+
_libelf_sum(unsigned long c, const unsigned char *s, size_t size)
+
{
+
	if (s == NULL || size == 0)
+
		return (c);
+

+
	while (size--)
+
		c += *s++;
+

+
	return (c);
+
}
+

+
unsigned long
+
_libelf_checksum(Elf *e, int elfclass)
+
{
+
	size_t shn;
+
	Elf_Scn *scn;
+
	Elf_Data *d;
+
	unsigned long checksum;
+
	GElf_Ehdr eh;
+
	GElf_Shdr shdr;
+

+
	if (e == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0L);
+
	}
+

+
	if (e->e_class != elfclass) {
+
		LIBELF_SET_ERROR(CLASS, 0);
+
		return (0L);
+
	}
+

+
	if (gelf_getehdr(e, &eh) == NULL)
+
		return (0);
+

+
	/*
+
	 * Iterate over all sections in the ELF file, computing the
+
	 * checksum along the way.
+
	 *
+
	 * The first section is always SHN_UNDEF and can be skipped.
+
	 * Non-allocatable sections are skipped, as are sections that
+
	 * could be affected by utilities such as strip(1).
+
	 */
+

+
	checksum = 0;
+
	for (shn = 1; shn < e->e_u.e_elf.e_nscn; shn++) {
+
		if ((scn = elf_getscn(e, shn)) == NULL)
+
			return (0);
+
		if (gelf_getshdr(scn, &shdr) == NULL)
+
			return (0);
+
		if ((shdr.sh_flags & SHF_ALLOC) == 0 ||
+
		    shdr.sh_type == SHT_DYNAMIC ||
+
		    shdr.sh_type == SHT_DYNSYM)
+
			continue;
+

+
		d = NULL;
+
		while ((d = elf_rawdata(scn, d)) != NULL)
+
			checksum = _libelf_sum(checksum,
+
			    (unsigned char *) d->d_buf, d->d_size);
+
	}
+

+
	/*
+
	 * Return a 16-bit checksum compatible with Solaris.
+
	 */
+
	return (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL));
+
}
added external/libelf/libelf_convert.c
@@ -0,0 +1,3015 @@
+
/*-
+
 * 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 THE AUTHOR AND CONTRIBUTORS `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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_convert.m4 219126 2011-03-01 11:47:51Z brucec $");
+

+
#include <sys/types.h>
+
#include <sys/elf32.h>
+
#include <sys/elf64.h>
+

+
#include <assert.h>
+
#include <libelf.h>
+
#include <osreldate.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
/* WARNING: GENERATED FROM libelf_convert.m4. */
+

+
/*
+
 * Macros to swap various integral quantities.
+
 */
+

+
#define	SWAP_HALF(X) 	do {						\
+
		uint16_t _x = (uint16_t) (X);				\
+
		uint16_t _t = _x & 0xFF;				\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		(X) = _t;						\
+
	} while (0)
+
#define	SWAP_WORD(X) 	do {						\
+
		uint32_t _x = (uint32_t) (X);				\
+
		uint32_t _t = _x & 0xFF;				\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		(X) = _t;						\
+
	} while (0)
+
#define	SWAP_ADDR32(X)	SWAP_WORD(X)
+
#define	SWAP_OFF32(X)	SWAP_WORD(X)
+
#define	SWAP_SWORD(X)	SWAP_WORD(X)
+
#define	SWAP_WORD64(X)	do {						\
+
		uint64_t _x = (uint64_t) (X);				\
+
		uint64_t _t = _x & 0xFF;				\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		(X) = _t;						\
+
	} while (0)
+
#define	SWAP_ADDR64(X)	SWAP_WORD64(X)
+
#define	SWAP_LWORD(X)	SWAP_WORD64(X)
+
#define	SWAP_OFF64(X)	SWAP_WORD64(X)
+
#define	SWAP_SXWORD(X)	SWAP_WORD64(X)
+
#define	SWAP_XWORD(X)	SWAP_WORD64(X)
+

+
/*
+
 * Write out various integral values.  The destination pointer could
+
 * be unaligned.  Values are written out in native byte order.  The
+
 * destination pointer is incremented after the write.
+
 */
+
#define	WRITE_BYTE(P,X) do {						\
+
		char *const _p = (char *) (P);	\
+
		_p[0]		= (char) (X);			\
+
		(P)		= _p + 1;				\
+
	} while (0)
+
#define	WRITE_HALF(P,X)	do {						\
+
		uint16_t _t	= (X);					\
+
		char *const _p	= (char *) (P);	\
+
		const char *const _q = (char *) &_t;	\
+
		_p[0]		= _q[0];				\
+
		_p[1]		= _q[1];				\
+
		(P) 		= _p + 2;				\
+
	} while (0)
+
#define	WRITE_WORD(P,X)	do {						\
+
		uint32_t _t	= (X);					\
+
		char *const _p	= (char *) (P);	\
+
		const char *const _q = (char *) &_t;	\
+
		_p[0]		= _q[0];				\
+
		_p[1]		= _q[1];				\
+
		_p[2]		= _q[2];				\
+
		_p[3]		= _q[3];				\
+
		(P)		= _p + 4;				\
+
	} while (0)
+
#define	WRITE_ADDR32(P,X)	WRITE_WORD(P,X)
+
#define	WRITE_OFF32(P,X)	WRITE_WORD(P,X)
+
#define	WRITE_SWORD(P,X)	WRITE_WORD(P,X)
+
#define	WRITE_WORD64(P,X)	do {					\
+
		uint64_t _t	= (X);					\
+
		char *const _p	= (char *) (P);	\
+
		const char *const _q = (char *) &_t;	\
+
		_p[0]		= _q[0];				\
+
		_p[1]		= _q[1];				\
+
		_p[2]		= _q[2];				\
+
		_p[3]		= _q[3];				\
+
		_p[4]		= _q[4];				\
+
		_p[5]		= _q[5];				\
+
		_p[6]		= _q[6];				\
+
		_p[7]		= _q[7];				\
+
		(P)		= _p + 8;				\
+
	} while (0)
+
#define	WRITE_ADDR64(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_LWORD(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_OFF64(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_SXWORD(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_XWORD(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_IDENT(P,X)	do {					\
+
		(void) memcpy((P), (X), sizeof((X)));			\
+
		(P)		= (P) + EI_NIDENT;			\
+
	} while (0)
+

+
/*
+
 * Read in various integral values.  The source pointer could be
+
 * unaligned.  Values are read in native byte order.  The source
+
 * pointer is incremented appropriately.
+
 */
+

+
#define	READ_BYTE(P,X)	do {						\
+
		const char *const _p =				\
+
			(const char *) (P);			\
+
		(X)		= _p[0];				\
+
		(P)		= (P) + 1;				\
+
	} while (0)
+
#define	READ_HALF(P,X)	do {						\
+
		uint16_t _t;						\
+
		char *const _q = (char *) &_t;	\
+
		const char *const _p =				\
+
			(const char *) (P);			\
+
		_q[0]		= _p[0];				\
+
		_q[1]		= _p[1];				\
+
		(P)		= (P) + 2;				\
+
		(X)		= _t;					\
+
	} while (0)
+
#define	READ_WORD(P,X)	do {						\
+
		uint32_t _t;						\
+
		char *const _q = (char *) &_t;	\
+
		const char *const _p =				\
+
			(const char *) (P);			\
+
		_q[0]		= _p[0];				\
+
		_q[1]		= _p[1];				\
+
		_q[2]		= _p[2];				\
+
		_q[3]		= _p[3];				\
+
		(P)		= (P) + 4;				\
+
		(X)		= _t;					\
+
	} while (0)
+
#define	READ_ADDR32(P,X)	READ_WORD(P,X)
+
#define	READ_OFF32(P,X)		READ_WORD(P,X)
+
#define	READ_SWORD(P,X)		READ_WORD(P,X)
+
#define	READ_WORD64(P,X)	do {					\
+
		uint64_t _t;						\
+
		char *const _q = (char *) &_t;	\
+
		const char *const _p =				\
+
			(const char *) (P);			\
+
		_q[0]		= _p[0];				\
+
		_q[1]		= _p[1];				\
+
		_q[2]		= _p[2];				\
+
		_q[3]		= _p[3];				\
+
		_q[4]		= _p[4];				\
+
		_q[5]		= _p[5];				\
+
		_q[6]		= _p[6];				\
+
		_q[7]		= _p[7];				\
+
		(P)		= (P) + 8;				\
+
		(X)		= _t;					\
+
	} while (0)
+
#define	READ_ADDR64(P,X)	READ_WORD64(P,X)
+
#define	READ_LWORD(P,X)		READ_WORD64(P,X)
+
#define	READ_OFF64(P,X)		READ_WORD64(P,X)
+
#define	READ_SXWORD(P,X)	READ_WORD64(P,X)
+
#define	READ_XWORD(P,X)		READ_WORD64(P,X)
+
#define	READ_IDENT(P,X)		do {					\
+
		(void) memcpy((X), (P), sizeof((X)));			\
+
		(P)		= (P) + EI_NIDENT;			\
+
	} while (0)
+

+
#define	ROUNDUP2(V,N)	(V) = ((((V) + (N) - 1)) & ~((N) - 1))
+

+

+

+
/*
+
 * Sections of type ELF_T_BYTE are never byteswapped, consequently a
+
 * simple memcpy suffices for both directions of conversion.
+
 */
+

+
static int
+
libelf_cvt_BYTE_tox(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	(void) byteswap;
+
	if (dsz < count)
+
		return (0);
+
	if (dst != src)
+
		(void) memcpy(dst, src, count);
+
	return (1);
+
}
+

+
#if	__FreeBSD_version >= 600102 /* ADDR */
+

+
static int
+
libelf_cvt_ADDR32_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Addr t, *s = (Elf32_Addr *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_ADDR32(t);
+
		WRITE_ADDR32(dst,t);
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt_ADDR32_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Addr t, *d = (Elf32_Addr *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf32_Addr))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_ADDR32(src,t);
+
		SWAP_ADDR32(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt_ADDR64_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Addr t, *s = (Elf64_Addr *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_ADDR64(t);
+
		WRITE_ADDR64(dst,t);
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt_ADDR64_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Addr t, *d = (Elf64_Addr *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf64_Addr))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_ADDR64(src,t);
+
		SWAP_ADDR64(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* ADDR */
+
#if	__FreeBSD_version >= 600102 /* BYTE */
+

+
#endif /* BYTE */
+
#if	__FreeBSD_version >= 700025 /* CAP */
+

+
static int
+
libelf_cvt32_CAP_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Cap	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Cap *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Cap */
+
			SWAP_WORD(t.c_tag);
+
			SWAP_WORD(t.c_un.c_val);
+
			/**/
+
		}
+
		/* Write an Elf32_Cap */
+
		WRITE_WORD(dst,t.c_tag);
+
		WRITE_WORD(dst,t.c_un.c_val);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_CAP_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Cap	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Cap *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Cap */
+
			SWAP_XWORD(t.c_tag);
+
			SWAP_XWORD(t.c_un.c_val);
+
			/**/
+
		}
+
		/* Write an Elf64_Cap */
+
		WRITE_XWORD(dst,t.c_tag);
+
		WRITE_XWORD(dst,t.c_un.c_val);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_CAP_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Cap	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_CAP, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Cap *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Cap))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Cap */
+
		READ_WORD(s,t.c_tag);
+
		READ_WORD(s,t.c_un.c_val);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Cap */
+
			SWAP_WORD(t.c_tag);
+
			SWAP_WORD(t.c_un.c_val);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_CAP_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Cap	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_CAP, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Cap *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Cap))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Cap */
+
		READ_XWORD(s,t.c_tag);
+
		READ_XWORD(s,t.c_un.c_val);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Cap */
+
			SWAP_XWORD(t.c_tag);
+
			SWAP_XWORD(t.c_un.c_val);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* CAP */
+
#if	__FreeBSD_version >= 600102 /* DYN */
+

+
static int
+
libelf_cvt32_DYN_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Dyn	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Dyn *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Dyn */
+
			SWAP_SWORD(t.d_tag);
+
			SWAP_WORD(t.d_un.d_ptr);
+
			/**/
+
		}
+
		/* Write an Elf32_Dyn */
+
		WRITE_SWORD(dst,t.d_tag);
+
		WRITE_WORD(dst,t.d_un.d_ptr);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_DYN_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Dyn	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Dyn *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Dyn */
+
			SWAP_SXWORD(t.d_tag);
+
			SWAP_XWORD(t.d_un.d_ptr);
+
			/**/
+
		}
+
		/* Write an Elf64_Dyn */
+
		WRITE_SXWORD(dst,t.d_tag);
+
		WRITE_XWORD(dst,t.d_un.d_ptr);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_DYN_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Dyn	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_DYN, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Dyn *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Dyn))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Dyn */
+
		READ_SWORD(s,t.d_tag);
+
		READ_WORD(s,t.d_un.d_ptr);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Dyn */
+
			SWAP_SWORD(t.d_tag);
+
			SWAP_WORD(t.d_un.d_ptr);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_DYN_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Dyn	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_DYN, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Dyn *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Dyn))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Dyn */
+
		READ_SXWORD(s,t.d_tag);
+
		READ_XWORD(s,t.d_un.d_ptr);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Dyn */
+
			SWAP_SXWORD(t.d_tag);
+
			SWAP_XWORD(t.d_un.d_ptr);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* DYN */
+
#if	__FreeBSD_version >= 600102 /* EHDR */
+

+
static int
+
libelf_cvt32_EHDR_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Ehdr	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Ehdr *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Ehdr */
+
			SWAP_HALF(t.e_type);
+
			SWAP_HALF(t.e_machine);
+
			SWAP_WORD(t.e_version);
+
			SWAP_ADDR32(t.e_entry);
+
			SWAP_OFF32(t.e_phoff);
+
			SWAP_OFF32(t.e_shoff);
+
			SWAP_WORD(t.e_flags);
+
			SWAP_HALF(t.e_ehsize);
+
			SWAP_HALF(t.e_phentsize);
+
			SWAP_HALF(t.e_phnum);
+
			SWAP_HALF(t.e_shentsize);
+
			SWAP_HALF(t.e_shnum);
+
			SWAP_HALF(t.e_shstrndx);
+
			/**/
+
		}
+
		/* Write an Elf32_Ehdr */
+
		WRITE_IDENT(dst,t.e_ident);
+
		WRITE_HALF(dst,t.e_type);
+
		WRITE_HALF(dst,t.e_machine);
+
		WRITE_WORD(dst,t.e_version);
+
		WRITE_ADDR32(dst,t.e_entry);
+
		WRITE_OFF32(dst,t.e_phoff);
+
		WRITE_OFF32(dst,t.e_shoff);
+
		WRITE_WORD(dst,t.e_flags);
+
		WRITE_HALF(dst,t.e_ehsize);
+
		WRITE_HALF(dst,t.e_phentsize);
+
		WRITE_HALF(dst,t.e_phnum);
+
		WRITE_HALF(dst,t.e_shentsize);
+
		WRITE_HALF(dst,t.e_shnum);
+
		WRITE_HALF(dst,t.e_shstrndx);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_EHDR_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Ehdr	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Ehdr *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Ehdr */
+
			SWAP_HALF(t.e_type);
+
			SWAP_HALF(t.e_machine);
+
			SWAP_WORD(t.e_version);
+
			SWAP_ADDR64(t.e_entry);
+
			SWAP_OFF64(t.e_phoff);
+
			SWAP_OFF64(t.e_shoff);
+
			SWAP_WORD(t.e_flags);
+
			SWAP_HALF(t.e_ehsize);
+
			SWAP_HALF(t.e_phentsize);
+
			SWAP_HALF(t.e_phnum);
+
			SWAP_HALF(t.e_shentsize);
+
			SWAP_HALF(t.e_shnum);
+
			SWAP_HALF(t.e_shstrndx);
+
			/**/
+
		}
+
		/* Write an Elf64_Ehdr */
+
		WRITE_IDENT(dst,t.e_ident);
+
		WRITE_HALF(dst,t.e_type);
+
		WRITE_HALF(dst,t.e_machine);
+
		WRITE_WORD(dst,t.e_version);
+
		WRITE_ADDR64(dst,t.e_entry);
+
		WRITE_OFF64(dst,t.e_phoff);
+
		WRITE_OFF64(dst,t.e_shoff);
+
		WRITE_WORD(dst,t.e_flags);
+
		WRITE_HALF(dst,t.e_ehsize);
+
		WRITE_HALF(dst,t.e_phentsize);
+
		WRITE_HALF(dst,t.e_phnum);
+
		WRITE_HALF(dst,t.e_shentsize);
+
		WRITE_HALF(dst,t.e_shnum);
+
		WRITE_HALF(dst,t.e_shstrndx);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_EHDR_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Ehdr	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_EHDR, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Ehdr *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Ehdr))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Ehdr */
+
		READ_IDENT(s,t.e_ident);
+
		READ_HALF(s,t.e_type);
+
		READ_HALF(s,t.e_machine);
+
		READ_WORD(s,t.e_version);
+
		READ_ADDR32(s,t.e_entry);
+
		READ_OFF32(s,t.e_phoff);
+
		READ_OFF32(s,t.e_shoff);
+
		READ_WORD(s,t.e_flags);
+
		READ_HALF(s,t.e_ehsize);
+
		READ_HALF(s,t.e_phentsize);
+
		READ_HALF(s,t.e_phnum);
+
		READ_HALF(s,t.e_shentsize);
+
		READ_HALF(s,t.e_shnum);
+
		READ_HALF(s,t.e_shstrndx);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Ehdr */
+
			SWAP_HALF(t.e_type);
+
			SWAP_HALF(t.e_machine);
+
			SWAP_WORD(t.e_version);
+
			SWAP_ADDR32(t.e_entry);
+
			SWAP_OFF32(t.e_phoff);
+
			SWAP_OFF32(t.e_shoff);
+
			SWAP_WORD(t.e_flags);
+
			SWAP_HALF(t.e_ehsize);
+
			SWAP_HALF(t.e_phentsize);
+
			SWAP_HALF(t.e_phnum);
+
			SWAP_HALF(t.e_shentsize);
+
			SWAP_HALF(t.e_shnum);
+
			SWAP_HALF(t.e_shstrndx);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_EHDR_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Ehdr	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_EHDR, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Ehdr *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Ehdr))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Ehdr */
+
		READ_IDENT(s,t.e_ident);
+
		READ_HALF(s,t.e_type);
+
		READ_HALF(s,t.e_machine);
+
		READ_WORD(s,t.e_version);
+
		READ_ADDR64(s,t.e_entry);
+
		READ_OFF64(s,t.e_phoff);
+
		READ_OFF64(s,t.e_shoff);
+
		READ_WORD(s,t.e_flags);
+
		READ_HALF(s,t.e_ehsize);
+
		READ_HALF(s,t.e_phentsize);
+
		READ_HALF(s,t.e_phnum);
+
		READ_HALF(s,t.e_shentsize);
+
		READ_HALF(s,t.e_shnum);
+
		READ_HALF(s,t.e_shstrndx);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Ehdr */
+
			SWAP_HALF(t.e_type);
+
			SWAP_HALF(t.e_machine);
+
			SWAP_WORD(t.e_version);
+
			SWAP_ADDR64(t.e_entry);
+
			SWAP_OFF64(t.e_phoff);
+
			SWAP_OFF64(t.e_shoff);
+
			SWAP_WORD(t.e_flags);
+
			SWAP_HALF(t.e_ehsize);
+
			SWAP_HALF(t.e_phentsize);
+
			SWAP_HALF(t.e_phnum);
+
			SWAP_HALF(t.e_shentsize);
+
			SWAP_HALF(t.e_shnum);
+
			SWAP_HALF(t.e_shstrndx);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* EHDR */
+
#if	__FreeBSD_version >= 800062 /* GNUHASH */
+
                     
+
#endif /* GNUHASH */
+
#if	__FreeBSD_version >= 600102 /* HALF */
+

+
static int
+
libelf_cvt_HALF_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Half t, *s = (Elf64_Half *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_HALF(t);
+
		WRITE_HALF(dst,t);
+
	}
+

+
	return (1);
+
}
+

+
       
+
static int
+
libelf_cvt_HALF_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Half t, *d = (Elf64_Half *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf64_Half))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_HALF(src,t);
+
		SWAP_HALF(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* HALF */
+
#if	__FreeBSD_version >= 700025 /* LWORD */
+

+
static int
+
libelf_cvt_LWORD_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Lword t, *s = (Elf64_Lword *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_LWORD(t);
+
		WRITE_LWORD(dst,t);
+
	}
+

+
	return (1);
+
}
+

+
       
+
static int
+
libelf_cvt_LWORD_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Lword t, *d = (Elf64_Lword *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf64_Lword))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_LWORD(src,t);
+
		SWAP_LWORD(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* LWORD */
+
#if	__FreeBSD_version >= 700025 /* MOVE */
+

+
static int
+
libelf_cvt32_MOVE_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Move	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Move *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Move */
+
			SWAP_LWORD(t.m_value);
+
			SWAP_WORD(t.m_info);
+
			SWAP_WORD(t.m_poffset);
+
			SWAP_HALF(t.m_repeat);
+
			SWAP_HALF(t.m_stride);
+
			/**/
+
		}
+
		/* Write an Elf32_Move */
+
		WRITE_LWORD(dst,t.m_value);
+
		WRITE_WORD(dst,t.m_info);
+
		WRITE_WORD(dst,t.m_poffset);
+
		WRITE_HALF(dst,t.m_repeat);
+
		WRITE_HALF(dst,t.m_stride);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_MOVE_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Move	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Move *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Move */
+
			SWAP_LWORD(t.m_value);
+
			SWAP_XWORD(t.m_info);
+
			SWAP_XWORD(t.m_poffset);
+
			SWAP_HALF(t.m_repeat);
+
			SWAP_HALF(t.m_stride);
+
			/**/
+
		}
+
		/* Write an Elf64_Move */
+
		WRITE_LWORD(dst,t.m_value);
+
		WRITE_XWORD(dst,t.m_info);
+
		WRITE_XWORD(dst,t.m_poffset);
+
		WRITE_HALF(dst,t.m_repeat);
+
		WRITE_HALF(dst,t.m_stride);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_MOVE_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Move	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_MOVE, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Move *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Move))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Move */
+
		READ_LWORD(s,t.m_value);
+
		READ_WORD(s,t.m_info);
+
		READ_WORD(s,t.m_poffset);
+
		READ_HALF(s,t.m_repeat);
+
		READ_HALF(s,t.m_stride);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Move */
+
			SWAP_LWORD(t.m_value);
+
			SWAP_WORD(t.m_info);
+
			SWAP_WORD(t.m_poffset);
+
			SWAP_HALF(t.m_repeat);
+
			SWAP_HALF(t.m_stride);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_MOVE_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Move	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_MOVE, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Move *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Move))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Move */
+
		READ_LWORD(s,t.m_value);
+
		READ_XWORD(s,t.m_info);
+
		READ_XWORD(s,t.m_poffset);
+
		READ_HALF(s,t.m_repeat);
+
		READ_HALF(s,t.m_stride);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Move */
+
			SWAP_LWORD(t.m_value);
+
			SWAP_XWORD(t.m_info);
+
			SWAP_XWORD(t.m_poffset);
+
			SWAP_HALF(t.m_repeat);
+
			SWAP_HALF(t.m_stride);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* MOVE */
+
#if	__FreeBSD_version >= 700025 /* MOVEP */
+
                     
+
#endif /* MOVEP */
+
#if	__FreeBSD_version >= 600102 /* NOTE */
+

+
#endif /* NOTE */
+
#if	__FreeBSD_version >= 600102 /* OFF */
+

+
static int
+
libelf_cvt_OFF32_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Off t, *s = (Elf32_Off *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_OFF32(t);
+
		WRITE_OFF32(dst,t);
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt_OFF32_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Off t, *d = (Elf32_Off *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf32_Off))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_OFF32(src,t);
+
		SWAP_OFF32(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt_OFF64_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Off t, *s = (Elf64_Off *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_OFF64(t);
+
		WRITE_OFF64(dst,t);
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt_OFF64_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Off t, *d = (Elf64_Off *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf64_Off))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_OFF64(src,t);
+
		SWAP_OFF64(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* OFF */
+
#if	__FreeBSD_version >= 600102 /* PHDR */
+

+
static int
+
libelf_cvt32_PHDR_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Phdr	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Phdr *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Phdr */
+
			SWAP_WORD(t.p_type);
+
			SWAP_OFF32(t.p_offset);
+
			SWAP_ADDR32(t.p_vaddr);
+
			SWAP_ADDR32(t.p_paddr);
+
			SWAP_WORD(t.p_filesz);
+
			SWAP_WORD(t.p_memsz);
+
			SWAP_WORD(t.p_flags);
+
			SWAP_WORD(t.p_align);
+
			/**/
+
		}
+
		/* Write an Elf32_Phdr */
+
		WRITE_WORD(dst,t.p_type);
+
		WRITE_OFF32(dst,t.p_offset);
+
		WRITE_ADDR32(dst,t.p_vaddr);
+
		WRITE_ADDR32(dst,t.p_paddr);
+
		WRITE_WORD(dst,t.p_filesz);
+
		WRITE_WORD(dst,t.p_memsz);
+
		WRITE_WORD(dst,t.p_flags);
+
		WRITE_WORD(dst,t.p_align);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_PHDR_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Phdr	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Phdr *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Phdr */
+
			SWAP_WORD(t.p_type);
+
			SWAP_WORD(t.p_flags);
+
			SWAP_OFF64(t.p_offset);
+
			SWAP_ADDR64(t.p_vaddr);
+
			SWAP_ADDR64(t.p_paddr);
+
			SWAP_XWORD(t.p_filesz);
+
			SWAP_XWORD(t.p_memsz);
+
			SWAP_XWORD(t.p_align);
+
			/**/
+
		}
+
		/* Write an Elf64_Phdr */
+
		WRITE_WORD(dst,t.p_type);
+
		WRITE_WORD(dst,t.p_flags);
+
		WRITE_OFF64(dst,t.p_offset);
+
		WRITE_ADDR64(dst,t.p_vaddr);
+
		WRITE_ADDR64(dst,t.p_paddr);
+
		WRITE_XWORD(dst,t.p_filesz);
+
		WRITE_XWORD(dst,t.p_memsz);
+
		WRITE_XWORD(dst,t.p_align);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_PHDR_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Phdr	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_PHDR, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Phdr *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Phdr))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Phdr */
+
		READ_WORD(s,t.p_type);
+
		READ_OFF32(s,t.p_offset);
+
		READ_ADDR32(s,t.p_vaddr);
+
		READ_ADDR32(s,t.p_paddr);
+
		READ_WORD(s,t.p_filesz);
+
		READ_WORD(s,t.p_memsz);
+
		READ_WORD(s,t.p_flags);
+
		READ_WORD(s,t.p_align);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Phdr */
+
			SWAP_WORD(t.p_type);
+
			SWAP_OFF32(t.p_offset);
+
			SWAP_ADDR32(t.p_vaddr);
+
			SWAP_ADDR32(t.p_paddr);
+
			SWAP_WORD(t.p_filesz);
+
			SWAP_WORD(t.p_memsz);
+
			SWAP_WORD(t.p_flags);
+
			SWAP_WORD(t.p_align);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_PHDR_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Phdr	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_PHDR, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Phdr *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Phdr))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Phdr */
+
		READ_WORD(s,t.p_type);
+
		READ_WORD(s,t.p_flags);
+
		READ_OFF64(s,t.p_offset);
+
		READ_ADDR64(s,t.p_vaddr);
+
		READ_ADDR64(s,t.p_paddr);
+
		READ_XWORD(s,t.p_filesz);
+
		READ_XWORD(s,t.p_memsz);
+
		READ_XWORD(s,t.p_align);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Phdr */
+
			SWAP_WORD(t.p_type);
+
			SWAP_WORD(t.p_flags);
+
			SWAP_OFF64(t.p_offset);
+
			SWAP_ADDR64(t.p_vaddr);
+
			SWAP_ADDR64(t.p_paddr);
+
			SWAP_XWORD(t.p_filesz);
+
			SWAP_XWORD(t.p_memsz);
+
			SWAP_XWORD(t.p_align);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* PHDR */
+
#if	__FreeBSD_version >= 600102 /* REL */
+

+
static int
+
libelf_cvt32_REL_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Rel	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Rel *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Rel */
+
			SWAP_ADDR32(t.r_offset);
+
			SWAP_WORD(t.r_info);
+
			/**/
+
		}
+
		/* Write an Elf32_Rel */
+
		WRITE_ADDR32(dst,t.r_offset);
+
		WRITE_WORD(dst,t.r_info);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_REL_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Rel	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Rel *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Rel */
+
			SWAP_ADDR64(t.r_offset);
+
			SWAP_XWORD(t.r_info);
+
			/**/
+
		}
+
		/* Write an Elf64_Rel */
+
		WRITE_ADDR64(dst,t.r_offset);
+
		WRITE_XWORD(dst,t.r_info);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_REL_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Rel	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_REL, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Rel *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Rel))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Rel */
+
		READ_ADDR32(s,t.r_offset);
+
		READ_WORD(s,t.r_info);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Rel */
+
			SWAP_ADDR32(t.r_offset);
+
			SWAP_WORD(t.r_info);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_REL_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Rel	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_REL, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Rel *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Rel))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Rel */
+
		READ_ADDR64(s,t.r_offset);
+
		READ_XWORD(s,t.r_info);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Rel */
+
			SWAP_ADDR64(t.r_offset);
+
			SWAP_XWORD(t.r_info);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* REL */
+
#if	__FreeBSD_version >= 600102 /* RELA */
+

+
static int
+
libelf_cvt32_RELA_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Rela	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Rela *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Rela */
+
			SWAP_ADDR32(t.r_offset);
+
			SWAP_WORD(t.r_info);
+
			SWAP_SWORD(t.r_addend);
+
			/**/
+
		}
+
		/* Write an Elf32_Rela */
+
		WRITE_ADDR32(dst,t.r_offset);
+
		WRITE_WORD(dst,t.r_info);
+
		WRITE_SWORD(dst,t.r_addend);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_RELA_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Rela	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Rela *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Rela */
+
			SWAP_ADDR64(t.r_offset);
+
			SWAP_XWORD(t.r_info);
+
			SWAP_SXWORD(t.r_addend);
+
			/**/
+
		}
+
		/* Write an Elf64_Rela */
+
		WRITE_ADDR64(dst,t.r_offset);
+
		WRITE_XWORD(dst,t.r_info);
+
		WRITE_SXWORD(dst,t.r_addend);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_RELA_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Rela	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_RELA, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Rela *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Rela))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Rela */
+
		READ_ADDR32(s,t.r_offset);
+
		READ_WORD(s,t.r_info);
+
		READ_SWORD(s,t.r_addend);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Rela */
+
			SWAP_ADDR32(t.r_offset);
+
			SWAP_WORD(t.r_info);
+
			SWAP_SWORD(t.r_addend);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_RELA_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Rela	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_RELA, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Rela *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Rela))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Rela */
+
		READ_ADDR64(s,t.r_offset);
+
		READ_XWORD(s,t.r_info);
+
		READ_SXWORD(s,t.r_addend);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Rela */
+
			SWAP_ADDR64(t.r_offset);
+
			SWAP_XWORD(t.r_info);
+
			SWAP_SXWORD(t.r_addend);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* RELA */
+
#if	__FreeBSD_version >= 600102 /* SHDR */
+

+
static int
+
libelf_cvt32_SHDR_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Shdr	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Shdr *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Shdr */
+
			SWAP_WORD(t.sh_name);
+
			SWAP_WORD(t.sh_type);
+
			SWAP_WORD(t.sh_flags);
+
			SWAP_ADDR32(t.sh_addr);
+
			SWAP_OFF32(t.sh_offset);
+
			SWAP_WORD(t.sh_size);
+
			SWAP_WORD(t.sh_link);
+
			SWAP_WORD(t.sh_info);
+
			SWAP_WORD(t.sh_addralign);
+
			SWAP_WORD(t.sh_entsize);
+
			/**/
+
		}
+
		/* Write an Elf32_Shdr */
+
		WRITE_WORD(dst,t.sh_name);
+
		WRITE_WORD(dst,t.sh_type);
+
		WRITE_WORD(dst,t.sh_flags);
+
		WRITE_ADDR32(dst,t.sh_addr);
+
		WRITE_OFF32(dst,t.sh_offset);
+
		WRITE_WORD(dst,t.sh_size);
+
		WRITE_WORD(dst,t.sh_link);
+
		WRITE_WORD(dst,t.sh_info);
+
		WRITE_WORD(dst,t.sh_addralign);
+
		WRITE_WORD(dst,t.sh_entsize);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_SHDR_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Shdr	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Shdr *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Shdr */
+
			SWAP_WORD(t.sh_name);
+
			SWAP_WORD(t.sh_type);
+
			SWAP_XWORD(t.sh_flags);
+
			SWAP_ADDR64(t.sh_addr);
+
			SWAP_OFF64(t.sh_offset);
+
			SWAP_XWORD(t.sh_size);
+
			SWAP_WORD(t.sh_link);
+
			SWAP_WORD(t.sh_info);
+
			SWAP_XWORD(t.sh_addralign);
+
			SWAP_XWORD(t.sh_entsize);
+
			/**/
+
		}
+
		/* Write an Elf64_Shdr */
+
		WRITE_WORD(dst,t.sh_name);
+
		WRITE_WORD(dst,t.sh_type);
+
		WRITE_XWORD(dst,t.sh_flags);
+
		WRITE_ADDR64(dst,t.sh_addr);
+
		WRITE_OFF64(dst,t.sh_offset);
+
		WRITE_XWORD(dst,t.sh_size);
+
		WRITE_WORD(dst,t.sh_link);
+
		WRITE_WORD(dst,t.sh_info);
+
		WRITE_XWORD(dst,t.sh_addralign);
+
		WRITE_XWORD(dst,t.sh_entsize);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_SHDR_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Shdr	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_SHDR, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Shdr *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Shdr))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Shdr */
+
		READ_WORD(s,t.sh_name);
+
		READ_WORD(s,t.sh_type);
+
		READ_WORD(s,t.sh_flags);
+
		READ_ADDR32(s,t.sh_addr);
+
		READ_OFF32(s,t.sh_offset);
+
		READ_WORD(s,t.sh_size);
+
		READ_WORD(s,t.sh_link);
+
		READ_WORD(s,t.sh_info);
+
		READ_WORD(s,t.sh_addralign);
+
		READ_WORD(s,t.sh_entsize);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Shdr */
+
			SWAP_WORD(t.sh_name);
+
			SWAP_WORD(t.sh_type);
+
			SWAP_WORD(t.sh_flags);
+
			SWAP_ADDR32(t.sh_addr);
+
			SWAP_OFF32(t.sh_offset);
+
			SWAP_WORD(t.sh_size);
+
			SWAP_WORD(t.sh_link);
+
			SWAP_WORD(t.sh_info);
+
			SWAP_WORD(t.sh_addralign);
+
			SWAP_WORD(t.sh_entsize);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_SHDR_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Shdr	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_SHDR, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Shdr *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Shdr))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Shdr */
+
		READ_WORD(s,t.sh_name);
+
		READ_WORD(s,t.sh_type);
+
		READ_XWORD(s,t.sh_flags);
+
		READ_ADDR64(s,t.sh_addr);
+
		READ_OFF64(s,t.sh_offset);
+
		READ_XWORD(s,t.sh_size);
+
		READ_WORD(s,t.sh_link);
+
		READ_WORD(s,t.sh_info);
+
		READ_XWORD(s,t.sh_addralign);
+
		READ_XWORD(s,t.sh_entsize);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Shdr */
+
			SWAP_WORD(t.sh_name);
+
			SWAP_WORD(t.sh_type);
+
			SWAP_XWORD(t.sh_flags);
+
			SWAP_ADDR64(t.sh_addr);
+
			SWAP_OFF64(t.sh_offset);
+
			SWAP_XWORD(t.sh_size);
+
			SWAP_WORD(t.sh_link);
+
			SWAP_WORD(t.sh_info);
+
			SWAP_XWORD(t.sh_addralign);
+
			SWAP_XWORD(t.sh_entsize);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* SHDR */
+
#if	__FreeBSD_version >= 600102 /* SWORD */
+

+
static int
+
libelf_cvt_SWORD_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Sword t, *s = (Elf64_Sword *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_SWORD(t);
+
		WRITE_SWORD(dst,t);
+
	}
+

+
	return (1);
+
}
+

+
       
+
static int
+
libelf_cvt_SWORD_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Sword t, *d = (Elf64_Sword *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf64_Sword))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_SWORD(src,t);
+
		SWAP_SWORD(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* SWORD */
+
#if	__FreeBSD_version >= 700009 /* SXWORD */
+

+
static int
+
libelf_cvt_SXWORD_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Sxword t, *s = (Elf64_Sxword *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_SXWORD(t);
+
		WRITE_SXWORD(dst,t);
+
	}
+

+
	return (1);
+
}
+

+
       
+
static int
+
libelf_cvt_SXWORD_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Sxword t, *d = (Elf64_Sxword *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf64_Sxword))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_SXWORD(src,t);
+
		SWAP_SXWORD(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* SXWORD */
+
#if	__FreeBSD_version >= 700025 /* SYMINFO */
+

+
static int
+
libelf_cvt32_SYMINFO_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Syminfo	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Syminfo *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Syminfo */
+
			SWAP_HALF(t.si_boundto);
+
			SWAP_HALF(t.si_flags);
+
			/**/
+
		}
+
		/* Write an Elf32_Syminfo */
+
		WRITE_HALF(dst,t.si_boundto);
+
		WRITE_HALF(dst,t.si_flags);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_SYMINFO_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Syminfo	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Syminfo *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Syminfo */
+
			SWAP_HALF(t.si_boundto);
+
			SWAP_HALF(t.si_flags);
+
			/**/
+
		}
+
		/* Write an Elf64_Syminfo */
+
		WRITE_HALF(dst,t.si_boundto);
+
		WRITE_HALF(dst,t.si_flags);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_SYMINFO_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Syminfo	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_SYMINFO, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Syminfo *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Syminfo))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Syminfo */
+
		READ_HALF(s,t.si_boundto);
+
		READ_HALF(s,t.si_flags);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Syminfo */
+
			SWAP_HALF(t.si_boundto);
+
			SWAP_HALF(t.si_flags);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_SYMINFO_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Syminfo	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_SYMINFO, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Syminfo *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Syminfo))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Syminfo */
+
		READ_HALF(s,t.si_boundto);
+
		READ_HALF(s,t.si_flags);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Syminfo */
+
			SWAP_HALF(t.si_boundto);
+
			SWAP_HALF(t.si_flags);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* SYMINFO */
+
#if	__FreeBSD_version >= 600102 /* SYM */
+

+
static int
+
libelf_cvt32_SYM_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Sym	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Sym *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Sym */
+
			SWAP_WORD(t.st_name);
+
			SWAP_ADDR32(t.st_value);
+
			SWAP_WORD(t.st_size);
+
			SWAP_HALF(t.st_shndx);
+
			/**/
+
		}
+
		/* Write an Elf32_Sym */
+
		WRITE_WORD(dst,t.st_name);
+
		WRITE_ADDR32(dst,t.st_value);
+
		WRITE_WORD(dst,t.st_size);
+
		WRITE_BYTE(dst,t.st_info);
+
		WRITE_BYTE(dst,t.st_other);
+
		WRITE_HALF(dst,t.st_shndx);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_SYM_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Sym	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Sym *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Sym */
+
			SWAP_WORD(t.st_name);
+
			SWAP_HALF(t.st_shndx);
+
			SWAP_ADDR64(t.st_value);
+
			SWAP_XWORD(t.st_size);
+
			/**/
+
		}
+
		/* Write an Elf64_Sym */
+
		WRITE_WORD(dst,t.st_name);
+
		WRITE_BYTE(dst,t.st_info);
+
		WRITE_BYTE(dst,t.st_other);
+
		WRITE_HALF(dst,t.st_shndx);
+
		WRITE_ADDR64(dst,t.st_value);
+
		WRITE_XWORD(dst,t.st_size);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_SYM_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Sym	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_SYM, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Sym *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Sym))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Sym */
+
		READ_WORD(s,t.st_name);
+
		READ_ADDR32(s,t.st_value);
+
		READ_WORD(s,t.st_size);
+
		READ_BYTE(s,t.st_info);
+
		READ_BYTE(s,t.st_other);
+
		READ_HALF(s,t.st_shndx);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Sym */
+
			SWAP_WORD(t.st_name);
+
			SWAP_ADDR32(t.st_value);
+
			SWAP_WORD(t.st_size);
+
			SWAP_HALF(t.st_shndx);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_SYM_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Sym	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_SYM, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Sym *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Sym))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Sym */
+
		READ_WORD(s,t.st_name);
+
		READ_BYTE(s,t.st_info);
+
		READ_BYTE(s,t.st_other);
+
		READ_HALF(s,t.st_shndx);
+
		READ_ADDR64(s,t.st_value);
+
		READ_XWORD(s,t.st_size);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Sym */
+
			SWAP_WORD(t.st_name);
+
			SWAP_HALF(t.st_shndx);
+
			SWAP_ADDR64(t.st_value);
+
			SWAP_XWORD(t.st_size);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* SYM */
+
#if	__FreeBSD_version >= 700009 /* VDEF */
+

+
static int
+
libelf_cvt32_VDEF_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Verdef	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Verdef *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Verdef */
+
			SWAP_HALF(t.vd_version);
+
			SWAP_HALF(t.vd_flags);
+
			SWAP_HALF(t.vd_ndx);
+
			SWAP_HALF(t.vd_cnt);
+
			SWAP_WORD(t.vd_hash);
+
			SWAP_WORD(t.vd_aux);
+
			SWAP_WORD(t.vd_next);
+
			/**/
+
		}
+
		/* Write an Elf32_Verdef */
+
		WRITE_HALF(dst,t.vd_version);
+
		WRITE_HALF(dst,t.vd_flags);
+
		WRITE_HALF(dst,t.vd_ndx);
+
		WRITE_HALF(dst,t.vd_cnt);
+
		WRITE_WORD(dst,t.vd_hash);
+
		WRITE_WORD(dst,t.vd_aux);
+
		WRITE_WORD(dst,t.vd_next);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_VDEF_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Verdef	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Verdef *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Verdef */
+
			SWAP_HALF(t.vd_version);
+
			SWAP_HALF(t.vd_flags);
+
			SWAP_HALF(t.vd_ndx);
+
			SWAP_HALF(t.vd_cnt);
+
			SWAP_WORD(t.vd_hash);
+
			SWAP_WORD(t.vd_aux);
+
			SWAP_WORD(t.vd_next);
+
			/**/
+
		}
+
		/* Write an Elf64_Verdef */
+
		WRITE_HALF(dst,t.vd_version);
+
		WRITE_HALF(dst,t.vd_flags);
+
		WRITE_HALF(dst,t.vd_ndx);
+
		WRITE_HALF(dst,t.vd_cnt);
+
		WRITE_WORD(dst,t.vd_hash);
+
		WRITE_WORD(dst,t.vd_aux);
+
		WRITE_WORD(dst,t.vd_next);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_VDEF_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Verdef	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_VDEF, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Verdef *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Verdef))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Verdef */
+
		READ_HALF(s,t.vd_version);
+
		READ_HALF(s,t.vd_flags);
+
		READ_HALF(s,t.vd_ndx);
+
		READ_HALF(s,t.vd_cnt);
+
		READ_WORD(s,t.vd_hash);
+
		READ_WORD(s,t.vd_aux);
+
		READ_WORD(s,t.vd_next);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Verdef */
+
			SWAP_HALF(t.vd_version);
+
			SWAP_HALF(t.vd_flags);
+
			SWAP_HALF(t.vd_ndx);
+
			SWAP_HALF(t.vd_cnt);
+
			SWAP_WORD(t.vd_hash);
+
			SWAP_WORD(t.vd_aux);
+
			SWAP_WORD(t.vd_next);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_VDEF_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Verdef	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_VDEF, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Verdef *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Verdef))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Verdef */
+
		READ_HALF(s,t.vd_version);
+
		READ_HALF(s,t.vd_flags);
+
		READ_HALF(s,t.vd_ndx);
+
		READ_HALF(s,t.vd_cnt);
+
		READ_WORD(s,t.vd_hash);
+
		READ_WORD(s,t.vd_aux);
+
		READ_WORD(s,t.vd_next);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Verdef */
+
			SWAP_HALF(t.vd_version);
+
			SWAP_HALF(t.vd_flags);
+
			SWAP_HALF(t.vd_ndx);
+
			SWAP_HALF(t.vd_cnt);
+
			SWAP_WORD(t.vd_hash);
+
			SWAP_WORD(t.vd_aux);
+
			SWAP_WORD(t.vd_next);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* VDEF */
+
#if	__FreeBSD_version >= 700009 /* VNEED */
+

+
static int
+
libelf_cvt32_VNEED_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Verneed	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf32_Verneed *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf32_Verneed */
+
			SWAP_HALF(t.vn_version);
+
			SWAP_HALF(t.vn_cnt);
+
			SWAP_WORD(t.vn_file);
+
			SWAP_WORD(t.vn_aux);
+
			SWAP_WORD(t.vn_next);
+
			/**/
+
		}
+
		/* Write an Elf32_Verneed */
+
		WRITE_HALF(dst,t.vn_version);
+
		WRITE_HALF(dst,t.vn_cnt);
+
		WRITE_WORD(dst,t.vn_file);
+
		WRITE_WORD(dst,t.vn_aux);
+
		WRITE_WORD(dst,t.vn_next);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_VNEED_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Verneed	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf64_Verneed *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			/* Swap an Elf64_Verneed */
+
			SWAP_HALF(t.vn_version);
+
			SWAP_HALF(t.vn_cnt);
+
			SWAP_WORD(t.vn_file);
+
			SWAP_WORD(t.vn_aux);
+
			SWAP_WORD(t.vn_next);
+
			/**/
+
		}
+
		/* Write an Elf64_Verneed */
+
		WRITE_HALF(dst,t.vn_version);
+
		WRITE_HALF(dst,t.vn_cnt);
+
		WRITE_WORD(dst,t.vn_file);
+
		WRITE_WORD(dst,t.vn_aux);
+
		WRITE_WORD(dst,t.vn_next);
+
		/**/
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt32_VNEED_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf32_Verneed	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf32_fsize(ELF_T_VNEED, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf32_Verneed *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf32_Verneed))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf32_Verneed */
+
		READ_HALF(s,t.vn_version);
+
		READ_HALF(s,t.vn_cnt);
+
		READ_WORD(s,t.vn_file);
+
		READ_WORD(s,t.vn_aux);
+
		READ_WORD(s,t.vn_next);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf32_Verneed */
+
			SWAP_HALF(t.vn_version);
+
			SWAP_HALF(t.vn_cnt);
+
			SWAP_WORD(t.vn_file);
+
			SWAP_WORD(t.vn_aux);
+
			SWAP_WORD(t.vn_next);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
       
+
static int
+
libelf_cvt64_VNEED_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Verneed	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf64_fsize(ELF_T_VNEED, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf64_Verneed *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf64_Verneed))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		/* Read an Elf64_Verneed */
+
		READ_HALF(s,t.vn_version);
+
		READ_HALF(s,t.vn_cnt);
+
		READ_WORD(s,t.vn_file);
+
		READ_WORD(s,t.vn_aux);
+
		READ_WORD(s,t.vn_next);
+
		/**/
+
		if (byteswap) {
+
			/* Swap an Elf64_Verneed */
+
			SWAP_HALF(t.vn_version);
+
			SWAP_HALF(t.vn_cnt);
+
			SWAP_WORD(t.vn_file);
+
			SWAP_WORD(t.vn_aux);
+
			SWAP_WORD(t.vn_next);
+
			/**/
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* VNEED */
+
#if	__FreeBSD_version >= 600102 /* WORD */
+

+
static int
+
libelf_cvt_WORD_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Word t, *s = (Elf64_Word *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_WORD(t);
+
		WRITE_WORD(dst,t);
+
	}
+

+
	return (1);
+
}
+

+
       
+
static int
+
libelf_cvt_WORD_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Word t, *d = (Elf64_Word *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf64_Word))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_WORD(src,t);
+
		SWAP_WORD(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* WORD */
+
#if	__FreeBSD_version >= 700009 /* XWORD */
+

+
static int
+
libelf_cvt_XWORD_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Xword t, *s = (Elf64_Xword *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_XWORD(t);
+
		WRITE_XWORD(dst,t);
+
	}
+

+
	return (1);
+
}
+

+
       
+
static int
+
libelf_cvt_XWORD_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf64_Xword t, *d = (Elf64_Xword *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf64_Xword))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_XWORD(src,t);
+
		SWAP_XWORD(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+

+
#endif /* XWORD */
+

+

+
#if	__FreeBSD_version >= 800062
+
/*
+
 * Sections of type ELF_T_GNUHASH start with a header containing 4 32-bit
+
 * words.  Bloom filter data comes next, followed by hash buckets and the
+
 * hash chain.
+
 *
+
 * Bloom filter words are 64 bit wide on ELFCLASS64 objects and are 32 bit
+
 * wide on ELFCLASS32 objects.  The other objects in this section are 32
+
 * bits wide.
+
 *
+
 * Argument srcsz denotes the number of bytes to be converted.  In the
+
 * 32-bit case we need to translate srcsz to a count of 32-bit words.
+
 */
+

+
static int
+
libelf_cvt32_GNUHASH_tom(char *dst, size_t dsz, char *src, size_t srcsz,
+
    int byteswap)
+
{
+
	return (libelf_cvt_WORD_tom(dst, dsz, src, srcsz / sizeof(uint32_t),
+
	        byteswap));
+
}
+

+
static int
+
libelf_cvt32_GNUHASH_tof(char *dst, size_t dsz, char *src, size_t srcsz,
+
    int byteswap)
+
{
+
	return (libelf_cvt_WORD_tof(dst, dsz, src, srcsz / sizeof(uint32_t),
+
	        byteswap));
+
}
+

+
static int
+
libelf_cvt64_GNUHASH_tom(char *dst, size_t dsz, char *src, size_t srcsz,
+
    int byteswap)
+
{
+
	size_t sz;
+
	uint64_t t64, *bloom64;
+
	Elf_GNU_Hash_Header *gh;
+
	uint32_t n, nbuckets, nchains, maskwords, shift2, symndx, t32;
+
	uint32_t *buckets, *chains;
+

+
	sz = 4 * sizeof(uint32_t);	/* File header is 4 words long. */
+
	if (dsz < sizeof(Elf_GNU_Hash_Header) || srcsz < sz)
+
		return (0);
+

+
	/* Read in the section header and byteswap if needed. */
+
	READ_WORD(src, nbuckets);
+
	READ_WORD(src, symndx);
+
	READ_WORD(src, maskwords);
+
	READ_WORD(src, shift2);
+

+
	srcsz -= sz;
+

+
	if (byteswap) {
+
		SWAP_WORD(nbuckets);
+
		SWAP_WORD(symndx);
+
		SWAP_WORD(maskwords);
+
		SWAP_WORD(shift2);
+
	}
+

+
	/* Check source buffer and destination buffer sizes. */
+
	sz = nbuckets * sizeof(uint32_t) + maskwords * sizeof(uint64_t);
+
	if (srcsz < sz || dsz < sz + sizeof(Elf_GNU_Hash_Header))
+
		return (0);
+

+
	gh = (Elf_GNU_Hash_Header *) (uintptr_t) dst;
+
	gh->gh_nbuckets  = nbuckets;
+
	gh->gh_symndx    = symndx;
+
	gh->gh_maskwords = maskwords;
+
	gh->gh_shift2    = shift2;
+
	
+
	dsz -= sizeof(Elf_GNU_Hash_Header);
+
	dst += sizeof(Elf_GNU_Hash_Header);
+

+
	bloom64 = (uint64_t *) (uintptr_t) dst;
+

+
	/* Copy bloom filter data. */
+
	for (n = 0; n < maskwords; n++) {
+
		READ_XWORD(src, t64);
+
		if (byteswap)
+
			SWAP_XWORD(t64);
+
		bloom64[n] = t64;
+
	}
+

+
	/* The hash buckets follows the bloom filter. */
+
	dst += maskwords * sizeof(uint64_t);
+
	buckets = (uint32_t *) (uintptr_t) dst;
+

+
	for (n = 0; n < nbuckets; n++) {
+
		READ_WORD(src, t32);
+
		if (byteswap)
+
			SWAP_WORD(t32);
+
		buckets[n] = t32;
+
	}
+

+
	dst += nbuckets * sizeof(uint32_t);
+

+
	/* The hash chain follows the hash buckets. */
+
	dsz -= sz;
+
	srcsz -= sz;
+

+
	if (dsz < srcsz)	/* Destination lacks space. */
+
	        return (0);
+

+
	nchains = srcsz / sizeof(uint32_t);
+
	chains = (uint32_t *) (uintptr_t) dst;
+

+
	for (n = 0; n < nchains; n++) {
+
		READ_WORD(src, t32);
+
		if (byteswap)
+
			SWAP_WORD(t32);
+
		*chains++ = t32;
+
	}
+

+
	return (1);
+
}
+

+
static int
+
libelf_cvt64_GNUHASH_tof(char *dst, size_t dsz, char *src, size_t srcsz,
+
    int byteswap)
+
{
+
	uint32_t *s32;
+
	size_t sz, hdrsz;
+
	uint64_t *s64, t64;
+
	Elf_GNU_Hash_Header *gh;
+
	uint32_t maskwords, n, nbuckets, nchains, t0, t1, t2, t3, t32;
+

+
	hdrsz = 4 * sizeof(uint32_t);	/* Header is 4x32 bits. */
+
	if (dsz < hdrsz || srcsz < sizeof(Elf_GNU_Hash_Header))
+
		return (0);
+

+
	gh = (Elf_GNU_Hash_Header *) (uintptr_t) src;
+

+
	t0 = nbuckets = gh->gh_nbuckets;
+
	t1 = gh->gh_symndx;
+
	t2 = maskwords = gh->gh_maskwords;
+
	t3 = gh->gh_shift2;
+

+
	src   += sizeof(Elf_GNU_Hash_Header);
+
	srcsz -= sizeof(Elf_GNU_Hash_Header);
+
	dsz   -= hdrsz;
+

+
	sz = gh->gh_nbuckets * sizeof(uint32_t) + gh->gh_maskwords *
+
	    sizeof(uint64_t);
+

+
	if (srcsz < sz || dsz < sz)
+
		return (0);
+

+
 	/* Write out the header. */
+
	if (byteswap) {
+
		SWAP_WORD(t0);
+
		SWAP_WORD(t1);
+
		SWAP_WORD(t2);
+
		SWAP_WORD(t3);
+
	}
+

+
	WRITE_WORD(dst, t0);
+
	WRITE_WORD(dst, t1);
+
	WRITE_WORD(dst, t2);
+
	WRITE_WORD(dst, t3);
+

+
	/* Copy the bloom filter and the hash table. */
+
	s64 = (uint64_t *) (uintptr_t) src;
+
	for (n = 0; n < maskwords; n++) {
+
		t64 = *s64++;
+
		if (byteswap)
+
			SWAP_XWORD(t64);
+
		WRITE_WORD64(dst, t64);
+
	}
+

+
	s32 = (uint32_t *) s64;
+
	for (n = 0; n < nbuckets; n++) {
+
		t32 = *s32++;
+
		if (byteswap)
+
			SWAP_WORD(t32);
+
		WRITE_WORD(dst, t32);
+
	}
+

+
	srcsz -= sz;
+
	dsz   -= sz;
+

+
	/* Copy out the hash chains. */
+
	if (dsz < srcsz)
+
		return (0);
+

+
	nchains = srcsz / sizeof(uint32_t);
+
	for (n = 0; n < nchains; n++) {
+
		t32 = *s32++;
+
		if (byteswap)
+
			SWAP_WORD(t32);
+
		WRITE_WORD(dst, t32);
+
	}
+

+
	return (1);
+
}
+
#endif
+

+
/*
+
 * Elf_Note structures comprise a fixed size header followed by variable
+
 * length strings.  The fixed size header needs to be byte swapped, but
+
 * not the strings.
+
 *
+
 * Argument count denotes the total number of bytes to be converted.
+
 * The destination buffer needs to be at least count bytes in size.
+
 */
+
static int
+
libelf_cvt_NOTE_tom(char *dst, size_t dsz, char *src, size_t count, 
+
    int byteswap)
+
{
+
	uint32_t namesz, descsz, type;
+
	Elf_Note *en;
+
	size_t sz, hdrsz;
+

+
	if (dsz < count)	/* Destination buffer is too small. */
+
		return (0);
+

+
	hdrsz = 3 * sizeof(uint32_t);
+
	if (count < hdrsz)		/* Source too small. */
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count);
+
		return (1);
+
	}
+

+
	/* Process all notes in the section. */
+
	while (count > hdrsz) {
+
		/* Read the note header. */
+
		READ_WORD(src, namesz);
+
		READ_WORD(src, descsz);
+
		READ_WORD(src, type);
+

+
		/* Translate. */
+
		SWAP_WORD(namesz);
+
		SWAP_WORD(descsz);
+
		SWAP_WORD(type);
+

+
		/* Copy out the translated note header. */
+
		en = (Elf_Note *) (uintptr_t) dst;
+
		en->n_namesz = namesz;
+
		en->n_descsz = descsz;
+
		en->n_type = type;
+

+
		dsz -= sizeof(Elf_Note);
+
		dst += sizeof(Elf_Note);
+
		count -= hdrsz;
+

+
		ROUNDUP2(namesz, 4);
+
		ROUNDUP2(descsz, 4);
+

+
		sz = namesz + descsz;
+

+
		if (count < sz || dsz < sz)	/* Buffers are too small. */
+
			return (0);
+

+
		(void) memcpy(dst, src, sz);
+

+
		src += sz;
+
		dst += sz;
+

+
		count -= sz;
+
		dsz -= sz;
+
	}
+

+
	return (1);
+
}
+

+
static int
+
libelf_cvt_NOTE_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	uint32_t namesz, descsz, type;
+
	Elf_Note *en;
+
	size_t sz;
+

+
	if (dsz < count)
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count);
+
		return (1);
+
	}
+

+
	while (count > sizeof(Elf_Note)) {
+

+
		en = (Elf_Note *) (uintptr_t) src;
+
		namesz = en->n_namesz;
+
		descsz = en->n_descsz;
+
		type = en->n_type;
+

+
		SWAP_WORD(namesz);
+
		SWAP_WORD(descsz);
+
		SWAP_WORD(type);
+

+
		WRITE_WORD(dst, namesz);
+
		WRITE_WORD(dst, descsz);
+
		WRITE_WORD(dst, type);
+

+
		src += sizeof(Elf_Note);
+

+
		ROUNDUP2(namesz, 4);
+
		ROUNDUP2(descsz, 4);
+

+
		sz = namesz + descsz;
+

+
		if (count < sz)
+
			sz = count;
+

+
		(void) memcpy(dst, src, sz);
+

+
		src += sz;
+
		dst += sz;
+
		count -= sz;
+
	}
+

+
	return (1);
+
}
+

+
struct converters {
+
	int	(*tof32)(char *dst, size_t dsz, char *src, size_t cnt,
+
		    int byteswap);
+
	int	(*tom32)(char *dst, size_t dsz, char *src, size_t cnt,
+
		    int byteswap);
+
	int	(*tof64)(char *dst, size_t dsz, char *src, size_t cnt,
+
		    int byteswap);
+
	int	(*tom64)(char *dst, size_t dsz, char *src, size_t cnt,
+
		    int byteswap);
+
};
+

+

+

+
static struct converters cvt[ELF_T_NUM] = {
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_ADDR] = {
+
        .tof32 = libelf_cvt_ADDR32_tof, .tom32 = libelf_cvt_ADDR32_tom,
+
        .tof64 = libelf_cvt_ADDR64_tof, .tom64 = libelf_cvt_ADDR64_tom },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_CAP] = {
+
        .tof32 = libelf_cvt32_CAP_tof, .tom32 = libelf_cvt32_CAP_tom,
+
        .tof64 = libelf_cvt64_CAP_tof, .tom64 = libelf_cvt64_CAP_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_DYN] = {
+
        .tof32 = libelf_cvt32_DYN_tof, .tom32 = libelf_cvt32_DYN_tom,
+
        .tof64 = libelf_cvt64_DYN_tof, .tom64 = libelf_cvt64_DYN_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_EHDR] = {
+
        .tof32 = libelf_cvt32_EHDR_tof, .tom32 = libelf_cvt32_EHDR_tom,
+
        .tof64 = libelf_cvt64_EHDR_tof, .tom64 = libelf_cvt64_EHDR_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_HALF] = {
+
        .tof32 = libelf_cvt_HALF_tof, .tom32 = libelf_cvt_HALF_tom,
+
        .tof64 = libelf_cvt_HALF_tof, .tom64 = libelf_cvt_HALF_tom },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_LWORD] = {
+
        .tof32 = libelf_cvt_LWORD_tof, .tom32 = libelf_cvt_LWORD_tom,
+
        .tof64 = libelf_cvt_LWORD_tof, .tom64 = libelf_cvt_LWORD_tom },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_MOVE] = {
+
        .tof32 = libelf_cvt32_MOVE_tof, .tom32 = libelf_cvt32_MOVE_tom,
+
        .tof64 = libelf_cvt64_MOVE_tof, .tom64 = libelf_cvt64_MOVE_tom },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_MOVEP] = {
+
        .tof32 = NULL, .tom32 = NULL,
+
        .tof64 = NULL, .tom64 = NULL },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_OFF] = {
+
        .tof32 = libelf_cvt_OFF32_tof, .tom32 = libelf_cvt_OFF32_tom,
+
        .tof64 = libelf_cvt_OFF64_tof, .tom64 = libelf_cvt_OFF64_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_PHDR] = {
+
        .tof32 = libelf_cvt32_PHDR_tof, .tom32 = libelf_cvt32_PHDR_tom,
+
        .tof64 = libelf_cvt64_PHDR_tof, .tom64 = libelf_cvt64_PHDR_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_REL] = {
+
        .tof32 = libelf_cvt32_REL_tof, .tom32 = libelf_cvt32_REL_tom,
+
        .tof64 = libelf_cvt64_REL_tof, .tom64 = libelf_cvt64_REL_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_RELA] = {
+
        .tof32 = libelf_cvt32_RELA_tof, .tom32 = libelf_cvt32_RELA_tom,
+
        .tof64 = libelf_cvt64_RELA_tof, .tom64 = libelf_cvt64_RELA_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_SHDR] = {
+
        .tof32 = libelf_cvt32_SHDR_tof, .tom32 = libelf_cvt32_SHDR_tom,
+
        .tof64 = libelf_cvt64_SHDR_tof, .tom64 = libelf_cvt64_SHDR_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_SWORD] = {
+
        .tof32 = libelf_cvt_SWORD_tof, .tom32 = libelf_cvt_SWORD_tom,
+
        .tof64 = libelf_cvt_SWORD_tof, .tom64 = libelf_cvt_SWORD_tom },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_SXWORD] = {
+
        .tof32 = NULL, .tom32 = NULL,
+
        .tof64 = libelf_cvt_SXWORD_tof, .tom64 = libelf_cvt_SXWORD_tom },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_SYMINFO] = {
+
        .tof32 = libelf_cvt32_SYMINFO_tof, .tom32 = libelf_cvt32_SYMINFO_tom,
+
        .tof64 = libelf_cvt64_SYMINFO_tof, .tom64 = libelf_cvt64_SYMINFO_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_SYM] = {
+
        .tof32 = libelf_cvt32_SYM_tof, .tom32 = libelf_cvt32_SYM_tom,
+
        .tof64 = libelf_cvt64_SYM_tof, .tom64 = libelf_cvt64_SYM_tom },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_VDEF] = {
+
        .tof32 = libelf_cvt32_VDEF_tof, .tom32 = libelf_cvt32_VDEF_tom,
+
        .tof64 = libelf_cvt64_VDEF_tof, .tom64 = libelf_cvt64_VDEF_tom },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_VNEED] = {
+
        .tof32 = libelf_cvt32_VNEED_tof, .tom32 = libelf_cvt32_VNEED_tom,
+
        .tof64 = libelf_cvt64_VNEED_tof, .tom64 = libelf_cvt64_VNEED_tom },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_WORD] = {
+
        .tof32 = libelf_cvt_WORD_tof, .tom32 = libelf_cvt_WORD_tom,
+
        .tof64 = libelf_cvt_WORD_tof, .tom64 = libelf_cvt_WORD_tom },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_XWORD] = {
+
        .tof32 = NULL, .tom32 = NULL,
+
        .tof64 = libelf_cvt_XWORD_tof, .tom64 = libelf_cvt_XWORD_tom },
+
#endif
+

+

+
	/*
+
	 * Types that needs hand-coded converters follow.
+
	 */
+

+
	[ELF_T_BYTE] = {
+
		.tof32 = libelf_cvt_BYTE_tox,
+
		.tom32 = libelf_cvt_BYTE_tox,
+
		.tof64 = libelf_cvt_BYTE_tox,
+
		.tom64 = libelf_cvt_BYTE_tox
+
	},
+

+
#if	__FreeBSD_version >= 800062
+
	[ELF_T_GNUHASH] = {
+
		.tof32 = libelf_cvt32_GNUHASH_tof,
+
		.tom32 = libelf_cvt32_GNUHASH_tom,
+
		.tof64 = libelf_cvt64_GNUHASH_tof,
+
		.tom64 = libelf_cvt64_GNUHASH_tom
+
	},
+
#endif
+

+
	[ELF_T_NOTE] = {
+
		.tof32 = libelf_cvt_NOTE_tof,
+
		.tom32 = libelf_cvt_NOTE_tom,
+
		.tof64 = libelf_cvt_NOTE_tof,
+
		.tom64 = libelf_cvt_NOTE_tom
+
	}
+
};
+

+
int (*_libelf_get_translator(Elf_Type t, int direction, int elfclass))
+
 (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap)
+
{
+
	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
+
	assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
+

+
	if (t >= ELF_T_NUM ||
+
	    (elfclass != ELFCLASS32 && elfclass != ELFCLASS64) ||
+
	    (direction != ELF_TOFILE && direction != ELF_TOMEMORY))
+
		return (NULL);
+

+
	return ((elfclass == ELFCLASS32) ?
+
	    (direction == ELF_TOFILE ? cvt[t].tof32 : cvt[t].tom32) :
+
	    (direction == ELF_TOFILE ? cvt[t].tof64 : cvt[t].tom64));
+
}
added external/libelf/libelf_convert.m4
@@ -0,0 +1,909 @@
+
/*-
+
 * 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_convert.m4 219126 2011-03-01 11:47:51Z brucec $");
+

+
#include <sys/types.h>
+
#include <sys/elf32.h>
+
#include <sys/elf64.h>
+

+
#include <assert.h>
+
#include <libelf.h>
+
#include <osreldate.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
/* WARNING: GENERATED FROM __file__. */
+

+
/*
+
 * Macros to swap various integral quantities.
+
 */
+

+
#define	SWAP_HALF(X) 	do {						\
+
		uint16_t _x = (uint16_t) (X);				\
+
		uint16_t _t = _x & 0xFF;				\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		(X) = _t;						\
+
	} while (0)
+
#define	SWAP_WORD(X) 	do {						\
+
		uint32_t _x = (uint32_t) (X);				\
+
		uint32_t _t = _x & 0xFF;				\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		(X) = _t;						\
+
	} while (0)
+
#define	SWAP_ADDR32(X)	SWAP_WORD(X)
+
#define	SWAP_OFF32(X)	SWAP_WORD(X)
+
#define	SWAP_SWORD(X)	SWAP_WORD(X)
+
#define	SWAP_WORD64(X)	do {						\
+
		uint64_t _x = (uint64_t) (X);				\
+
		uint64_t _t = _x & 0xFF;				\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		_t <<= 8; _x >>= 8; _t |= _x & 0xFF;			\
+
		(X) = _t;						\
+
	} while (0)
+
#define	SWAP_ADDR64(X)	SWAP_WORD64(X)
+
#define	SWAP_LWORD(X)	SWAP_WORD64(X)
+
#define	SWAP_OFF64(X)	SWAP_WORD64(X)
+
#define	SWAP_SXWORD(X)	SWAP_WORD64(X)
+
#define	SWAP_XWORD(X)	SWAP_WORD64(X)
+

+
/*
+
 * Write out various integral values.  The destination pointer could
+
 * be unaligned.  Values are written out in native byte order.  The
+
 * destination pointer is incremented after the write.
+
 */
+
#define	WRITE_BYTE(P,X) do {						\
+
		char *const _p = (char *) (P);	\
+
		_p[0]		= (char) (X);			\
+
		(P)		= _p + 1;				\
+
	} while (0)
+
#define	WRITE_HALF(P,X)	do {						\
+
		uint16_t _t	= (X);					\
+
		char *const _p	= (char *) (P);	\
+
		const char *const _q = (char *) &_t;	\
+
		_p[0]		= _q[0];				\
+
		_p[1]		= _q[1];				\
+
		(P) 		= _p + 2;				\
+
	} while (0)
+
#define	WRITE_WORD(P,X)	do {						\
+
		uint32_t _t	= (X);					\
+
		char *const _p	= (char *) (P);	\
+
		const char *const _q = (char *) &_t;	\
+
		_p[0]		= _q[0];				\
+
		_p[1]		= _q[1];				\
+
		_p[2]		= _q[2];				\
+
		_p[3]		= _q[3];				\
+
		(P)		= _p + 4;				\
+
	} while (0)
+
#define	WRITE_ADDR32(P,X)	WRITE_WORD(P,X)
+
#define	WRITE_OFF32(P,X)	WRITE_WORD(P,X)
+
#define	WRITE_SWORD(P,X)	WRITE_WORD(P,X)
+
#define	WRITE_WORD64(P,X)	do {					\
+
		uint64_t _t	= (X);					\
+
		char *const _p	= (char *) (P);	\
+
		const char *const _q = (char *) &_t;	\
+
		_p[0]		= _q[0];				\
+
		_p[1]		= _q[1];				\
+
		_p[2]		= _q[2];				\
+
		_p[3]		= _q[3];				\
+
		_p[4]		= _q[4];				\
+
		_p[5]		= _q[5];				\
+
		_p[6]		= _q[6];				\
+
		_p[7]		= _q[7];				\
+
		(P)		= _p + 8;				\
+
	} while (0)
+
#define	WRITE_ADDR64(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_LWORD(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_OFF64(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_SXWORD(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_XWORD(P,X)	WRITE_WORD64(P,X)
+
#define	WRITE_IDENT(P,X)	do {					\
+
		(void) memcpy((P), (X), sizeof((X)));			\
+
		(P)		= (P) + EI_NIDENT;			\
+
	} while (0)
+

+
/*
+
 * Read in various integral values.  The source pointer could be
+
 * unaligned.  Values are read in native byte order.  The source
+
 * pointer is incremented appropriately.
+
 */
+

+
#define	READ_BYTE(P,X)	do {						\
+
		const char *const _p =				\
+
			(const char *) (P);			\
+
		(X)		= _p[0];				\
+
		(P)		= (P) + 1;				\
+
	} while (0)
+
#define	READ_HALF(P,X)	do {						\
+
		uint16_t _t;						\
+
		char *const _q = (char *) &_t;	\
+
		const char *const _p =				\
+
			(const char *) (P);			\
+
		_q[0]		= _p[0];				\
+
		_q[1]		= _p[1];				\
+
		(P)		= (P) + 2;				\
+
		(X)		= _t;					\
+
	} while (0)
+
#define	READ_WORD(P,X)	do {						\
+
		uint32_t _t;						\
+
		char *const _q = (char *) &_t;	\
+
		const char *const _p =				\
+
			(const char *) (P);			\
+
		_q[0]		= _p[0];				\
+
		_q[1]		= _p[1];				\
+
		_q[2]		= _p[2];				\
+
		_q[3]		= _p[3];				\
+
		(P)		= (P) + 4;				\
+
		(X)		= _t;					\
+
	} while (0)
+
#define	READ_ADDR32(P,X)	READ_WORD(P,X)
+
#define	READ_OFF32(P,X)		READ_WORD(P,X)
+
#define	READ_SWORD(P,X)		READ_WORD(P,X)
+
#define	READ_WORD64(P,X)	do {					\
+
		uint64_t _t;						\
+
		char *const _q = (char *) &_t;	\
+
		const char *const _p =				\
+
			(const char *) (P);			\
+
		_q[0]		= _p[0];				\
+
		_q[1]		= _p[1];				\
+
		_q[2]		= _p[2];				\
+
		_q[3]		= _p[3];				\
+
		_q[4]		= _p[4];				\
+
		_q[5]		= _p[5];				\
+
		_q[6]		= _p[6];				\
+
		_q[7]		= _p[7];				\
+
		(P)		= (P) + 8;				\
+
		(X)		= _t;					\
+
	} while (0)
+
#define	READ_ADDR64(P,X)	READ_WORD64(P,X)
+
#define	READ_LWORD(P,X)		READ_WORD64(P,X)
+
#define	READ_OFF64(P,X)		READ_WORD64(P,X)
+
#define	READ_SXWORD(P,X)	READ_WORD64(P,X)
+
#define	READ_XWORD(P,X)		READ_WORD64(P,X)
+
#define	READ_IDENT(P,X)		do {					\
+
		(void) memcpy((X), (P), sizeof((X)));			\
+
		(P)		= (P) + EI_NIDENT;			\
+
	} while (0)
+

+
#define	ROUNDUP2(V,N)	(V) = ((((V) + (N) - 1)) & ~((N) - 1))
+

+
divert(-1)
+

+
/*
+
 * Generate conversion routines for converting between in-memory and
+
 * file representations of Elf data structures.
+
 *
+
 * `In-memory' representations of an Elf data structure use natural
+
 * alignments and native byte ordering.  This allows arithmetic and
+
 * casting to work as expected.  On the other hand the `file'
+
 * representation of an ELF data structure could be packed tighter
+
 * than its `in-memory' representation, and could be of a differing
+
 * byte order.  An additional complication is that `ar' only pads data
+
 * to even addresses and so ELF archive member data being read from
+
 * inside an `ar' archive could end up at misaligned memory addresses.
+
 *
+
 * Consequently, casting the `char *' pointers that point to memory
+
 * representations (i.e., source pointers for the *_tof() functions
+
 * and the destination pointers for the *_tom() functions), is safe,
+
 * as these pointers should be correctly aligned for the memory type
+
 * already.  However, pointers to file representations have to be
+
 * treated as being potentially unaligned and no casting can be done.
+
 */
+

+
include(SRCDIR`/elf_types.m4')
+

+
/*
+
 * `IGNORE'_* flags turn off generation of template code.
+
 */
+

+
define(`IGNORE',
+
  `define(IGNORE_$1`'32,	1)
+
   define(IGNORE_$1`'64,	1)')
+

+
IGNORE(MOVEP)
+
IGNORE(NOTE)
+
IGNORE(GNUHASH)
+

+
define(IGNORE_BYTE,		1)	/* 'lator, leave 'em bytes alone */
+
define(IGNORE_GNUHASH,		1)
+
define(IGNORE_NOTE,		1)
+
define(IGNORE_SXWORD32,		1)
+
define(IGNORE_XWORD32,		1)
+

+
/*
+
 * `BASE'_XXX flags cause class agnostic template functions
+
 * to be generated.
+
 */
+

+
define(`BASE_BYTE',	1)
+
define(`BASE_HALF',	1)
+
define(`BASE_NOTE',	1)
+
define(`BASE_WORD',	1)
+
define(`BASE_LWORD',	1)
+
define(`BASE_SWORD',	1)
+
define(`BASE_XWORD',	1)
+
define(`BASE_SXWORD',	1)
+

+
/*
+
 * `SIZEDEP'_XXX flags cause 32/64 bit variants to be generated
+
 * for each primitive type.
+
 */
+

+
define(`SIZEDEP_ADDR',	1)
+
define(`SIZEDEP_OFF',	1)
+

+
/*
+
 * `Primitive' ELF types are those that are an alias for an integral
+
 * type.  They have no internal structure. These can be copied using
+
 * a `memcpy()', and byteswapped in straightforward way.
+
 *
+
 * Macro use:
+
 * `$1': Name of the ELF type.
+
 * `$2': C structure name suffix
+
 * `$3': ELF class specifier for symbols, one of [`', `32', `64']
+
 * `$4': ELF class specifier for types, one of [`32', `64']
+
 */
+
define(`MAKEPRIM_TO_F',`
+
static int
+
libelf_cvt_$1$3_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf$4_$2 t, *s = (Elf$4_$2 *) (uintptr_t) src;
+
	size_t c;
+

+
	(void) dsz;
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*s));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		SWAP_$1$3(t);
+
		WRITE_$1$3(dst,t);
+
	}
+

+
	return (1);
+
}
+
')
+

+
define(`MAKEPRIM_TO_M',`
+
static int
+
libelf_cvt_$1$3_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf$4_$2 t, *d = (Elf$4_$2 *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf$4_$2))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_$1$3(src,t);
+
		SWAP_$1$3(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+
')
+

+
define(`SWAP_FIELD',
+
  `ifdef(`IGNORE_'$2,`',
+
    `ifelse(BASE_$2,1,
+
      `SWAP_$2(t.$1);
+
			',
+
      `ifelse($2,BYTE,`',
+
        `ifelse($2,IDENT,`',
+
          `SWAP_$2'SZ()`(t.$1);
+
			')')')')')
+
define(`SWAP_MEMBERS',
+
  `ifelse($#,1,`/**/',
+
     `SWAP_FIELD($1)SWAP_MEMBERS(shift($@))')')
+

+
define(`SWAP_STRUCT',
+
  `pushdef(`SZ',$2)/* Swap an Elf$2_$1 */
+
			SWAP_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
+

+
define(`WRITE_FIELD',
+
  `ifelse(BASE_$2,1,
+
    `WRITE_$2(dst,t.$1);
+
		',
+
    `ifelse($2,IDENT,
+
      `WRITE_$2(dst,t.$1);
+
		',
+
      `WRITE_$2'SZ()`(dst,t.$1);
+
		')')')
+
define(`WRITE_MEMBERS',
+
  `ifelse($#,1,`/**/',
+
    `WRITE_FIELD($1)WRITE_MEMBERS(shift($@))')')
+

+
define(`WRITE_STRUCT',
+
  `pushdef(`SZ',$2)/* Write an Elf$2_$1 */
+
		WRITE_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
+

+
define(`READ_FIELD',
+
  `ifelse(BASE_$2,1,
+
    `READ_$2(s,t.$1);
+
		',
+
    `ifelse($2,IDENT,
+
      `READ_$2(s,t.$1);
+
		',
+
      `READ_$2'SZ()`(s,t.$1);
+
		')')')
+

+
define(`READ_MEMBERS',
+
  `ifelse($#,1,`/**/',
+
    `READ_FIELD($1)READ_MEMBERS(shift($@))')')
+

+
define(`READ_STRUCT',
+
  `pushdef(`SZ',$2)/* Read an Elf$2_$1 */
+
		READ_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
+

+
/*
+
 * Converters for non-integral ELF data structures.
+
 *
+
 * When converting data to file representation, the source pointer
+
 * will be naturally aligned for a data structure's in-memory
+
 * representation.  When converting data to memory, the destination
+
 * pointer will be similarly aligned.
+
 *
+
 * For in-place conversions, when converting to file representations,
+
 * the source buffer is large enough to hold `file' data.  When
+
 * converting from file to memory, we need to be careful to work
+
 * `backwards', to avoid overwriting unconverted data.
+
 *
+
 * Macro use:
+
 * `$1': Name of the ELF type.
+
 * `$2': C structure name suffix.
+
 * `$3': ELF class specifier, one of [`', `32', `64']
+
 */
+

+
define(`MAKE_TO_F',
+
  `ifdef(`IGNORE_'$1$3,`',`
+
static int
+
libelf_cvt$3_$1_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf$3_$2	t, *s;
+
	size_t c;
+

+
	(void) dsz;
+

+
	s = (Elf$3_$2 *) (uintptr_t) src;
+
	for (c = 0; c < count; c++) {
+
		t = *s++;
+
		if (byteswap) {
+
			SWAP_STRUCT($2,$3)
+
		}
+
		WRITE_STRUCT($2,$3)
+
	}
+

+
	return (1);
+
}
+
')')
+

+
define(`MAKE_TO_M',
+
  `ifdef(`IGNORE_'$1$3,`',`
+
static int
+
libelf_cvt$3_$1_tom(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	Elf$3_$2	 t, *d;
+
	char		*s,*s0;
+
	size_t		fsz;
+

+
	fsz = elf$3_fsize(ELF_T_$1, (size_t) 1, EV_CURRENT);
+
	d   = ((Elf$3_$2 *) (uintptr_t) dst) + (count - 1);
+
	s0  = (char *) src + (count - 1) * fsz;
+

+
	if (dsz < count * sizeof(Elf$3_$2))
+
		return (0);
+

+
	while (count--) {
+
		s = s0;
+
		READ_STRUCT($2,$3)
+
		if (byteswap) {
+
			SWAP_STRUCT($2,$3)
+
		}
+
		*d-- = t; s0 -= fsz;
+
	}
+

+
	return (1);
+
}
+
')')
+

+
/*
+
 * Make type convertor functions from the type definition
+
 * of the ELF type:
+
 * - if the type is a base (i.e., `primitive') type:
+
 *   - if it is marked as to be ignored (i.e., `IGNORE_'TYPE)
+
 *     is defined, we skip the code generation step.
+
 *   - if the type is declared as `SIZEDEP', then 32 and 64 bit
+
 *     variants of the conversion functions are generated.
+
 *   - otherwise a 32 bit variant is generated.
+
 * - if the type is a structure type, we generate 32 and 64 bit
+
 *   variants of the conversion functions.
+
 */
+

+
define(`MAKE_TYPE_CONVERTER',
+
  `#if	__FreeBSD_version >= $3 /* $1 */
+
ifdef(`BASE'_$1,
+
    `ifdef(`IGNORE_'$1,`',
+
      `MAKEPRIM_TO_F($1,$2,`',64)
+
       MAKEPRIM_TO_M($1,$2,`',64)')',
+
    `ifdef(`SIZEDEP_'$1,
+
      `MAKEPRIM_TO_F($1,$2,32,32)dnl
+
       MAKEPRIM_TO_M($1,$2,32,32)dnl
+
       MAKEPRIM_TO_F($1,$2,64,64)dnl
+
       MAKEPRIM_TO_M($1,$2,64,64)',
+
      `MAKE_TO_F($1,$2,32)dnl
+
       MAKE_TO_F($1,$2,64)dnl
+
       MAKE_TO_M($1,$2,32)dnl
+
       MAKE_TO_M($1,$2,64)')')
+
#endif /* $1 */
+
')
+

+
define(`MAKE_TYPE_CONVERTERS',
+
  `ifelse($#,1,`',
+
    `MAKE_TYPE_CONVERTER($1)MAKE_TYPE_CONVERTERS(shift($@))')')
+

+
divert(0)
+

+
/*
+
 * Sections of type ELF_T_BYTE are never byteswapped, consequently a
+
 * simple memcpy suffices for both directions of conversion.
+
 */
+

+
static int
+
libelf_cvt_BYTE_tox(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	(void) byteswap;
+
	if (dsz < count)
+
		return (0);
+
	if (dst != src)
+
		(void) memcpy(dst, src, count);
+
	return (1);
+
}
+

+
MAKE_TYPE_CONVERTERS(ELF_TYPE_LIST)
+

+
#if	__FreeBSD_version >= 800062
+
/*
+
 * Sections of type ELF_T_GNUHASH start with a header containing 4 32-bit
+
 * words.  Bloom filter data comes next, followed by hash buckets and the
+
 * hash chain.
+
 *
+
 * Bloom filter words are 64 bit wide on ELFCLASS64 objects and are 32 bit
+
 * wide on ELFCLASS32 objects.  The other objects in this section are 32
+
 * bits wide.
+
 *
+
 * Argument `srcsz' denotes the number of bytes to be converted.  In the
+
 * 32-bit case we need to translate `srcsz' to a count of 32-bit words.
+
 */
+

+
static int
+
libelf_cvt32_GNUHASH_tom(char *dst, size_t dsz, char *src, size_t srcsz,
+
    int byteswap)
+
{
+
	return (libelf_cvt_WORD_tom(dst, dsz, src, srcsz / sizeof(uint32_t),
+
	        byteswap));
+
}
+

+
static int
+
libelf_cvt32_GNUHASH_tof(char *dst, size_t dsz, char *src, size_t srcsz,
+
    int byteswap)
+
{
+
	return (libelf_cvt_WORD_tof(dst, dsz, src, srcsz / sizeof(uint32_t),
+
	        byteswap));
+
}
+

+
static int
+
libelf_cvt64_GNUHASH_tom(char *dst, size_t dsz, char *src, size_t srcsz,
+
    int byteswap)
+
{
+
	size_t sz;
+
	uint64_t t64, *bloom64;
+
	Elf_GNU_Hash_Header *gh;
+
	uint32_t n, nbuckets, nchains, maskwords, shift2, symndx, t32;
+
	uint32_t *buckets, *chains;
+

+
	sz = 4 * sizeof(uint32_t);	/* File header is 4 words long. */
+
	if (dsz < sizeof(Elf_GNU_Hash_Header) || srcsz < sz)
+
		return (0);
+

+
	/* Read in the section header and byteswap if needed. */
+
	READ_WORD(src, nbuckets);
+
	READ_WORD(src, symndx);
+
	READ_WORD(src, maskwords);
+
	READ_WORD(src, shift2);
+

+
	srcsz -= sz;
+

+
	if (byteswap) {
+
		SWAP_WORD(nbuckets);
+
		SWAP_WORD(symndx);
+
		SWAP_WORD(maskwords);
+
		SWAP_WORD(shift2);
+
	}
+

+
	/* Check source buffer and destination buffer sizes. */
+
	sz = nbuckets * sizeof(uint32_t) + maskwords * sizeof(uint64_t);
+
	if (srcsz < sz || dsz < sz + sizeof(Elf_GNU_Hash_Header))
+
		return (0);
+

+
	gh = (Elf_GNU_Hash_Header *) (uintptr_t) dst;
+
	gh->gh_nbuckets  = nbuckets;
+
	gh->gh_symndx    = symndx;
+
	gh->gh_maskwords = maskwords;
+
	gh->gh_shift2    = shift2;
+
	
+
	dsz -= sizeof(Elf_GNU_Hash_Header);
+
	dst += sizeof(Elf_GNU_Hash_Header);
+

+
	bloom64 = (uint64_t *) (uintptr_t) dst;
+

+
	/* Copy bloom filter data. */
+
	for (n = 0; n < maskwords; n++) {
+
		READ_XWORD(src, t64);
+
		if (byteswap)
+
			SWAP_XWORD(t64);
+
		bloom64[n] = t64;
+
	}
+

+
	/* The hash buckets follows the bloom filter. */
+
	dst += maskwords * sizeof(uint64_t);
+
	buckets = (uint32_t *) (uintptr_t) dst;
+

+
	for (n = 0; n < nbuckets; n++) {
+
		READ_WORD(src, t32);
+
		if (byteswap)
+
			SWAP_WORD(t32);
+
		buckets[n] = t32;
+
	}
+

+
	dst += nbuckets * sizeof(uint32_t);
+

+
	/* The hash chain follows the hash buckets. */
+
	dsz -= sz;
+
	srcsz -= sz;
+

+
	if (dsz < srcsz)	/* Destination lacks space. */
+
	        return (0);
+

+
	nchains = srcsz / sizeof(uint32_t);
+
	chains = (uint32_t *) (uintptr_t) dst;
+

+
	for (n = 0; n < nchains; n++) {
+
		READ_WORD(src, t32);
+
		if (byteswap)
+
			SWAP_WORD(t32);
+
		*chains++ = t32;
+
	}
+

+
	return (1);
+
}
+

+
static int
+
libelf_cvt64_GNUHASH_tof(char *dst, size_t dsz, char *src, size_t srcsz,
+
    int byteswap)
+
{
+
	uint32_t *s32;
+
	size_t sz, hdrsz;
+
	uint64_t *s64, t64;
+
	Elf_GNU_Hash_Header *gh;
+
	uint32_t maskwords, n, nbuckets, nchains, t0, t1, t2, t3, t32;
+

+
	hdrsz = 4 * sizeof(uint32_t);	/* Header is 4x32 bits. */
+
	if (dsz < hdrsz || srcsz < sizeof(Elf_GNU_Hash_Header))
+
		return (0);
+

+
	gh = (Elf_GNU_Hash_Header *) (uintptr_t) src;
+

+
	t0 = nbuckets = gh->gh_nbuckets;
+
	t1 = gh->gh_symndx;
+
	t2 = maskwords = gh->gh_maskwords;
+
	t3 = gh->gh_shift2;
+

+
	src   += sizeof(Elf_GNU_Hash_Header);
+
	srcsz -= sizeof(Elf_GNU_Hash_Header);
+
	dsz   -= hdrsz;
+

+
	sz = gh->gh_nbuckets * sizeof(uint32_t) + gh->gh_maskwords *
+
	    sizeof(uint64_t);
+

+
	if (srcsz < sz || dsz < sz)
+
		return (0);
+

+
 	/* Write out the header. */
+
	if (byteswap) {
+
		SWAP_WORD(t0);
+
		SWAP_WORD(t1);
+
		SWAP_WORD(t2);
+
		SWAP_WORD(t3);
+
	}
+

+
	WRITE_WORD(dst, t0);
+
	WRITE_WORD(dst, t1);
+
	WRITE_WORD(dst, t2);
+
	WRITE_WORD(dst, t3);
+

+
	/* Copy the bloom filter and the hash table. */
+
	s64 = (uint64_t *) (uintptr_t) src;
+
	for (n = 0; n < maskwords; n++) {
+
		t64 = *s64++;
+
		if (byteswap)
+
			SWAP_XWORD(t64);
+
		WRITE_WORD64(dst, t64);
+
	}
+

+
	s32 = (uint32_t *) s64;
+
	for (n = 0; n < nbuckets; n++) {
+
		t32 = *s32++;
+
		if (byteswap)
+
			SWAP_WORD(t32);
+
		WRITE_WORD(dst, t32);
+
	}
+

+
	srcsz -= sz;
+
	dsz   -= sz;
+

+
	/* Copy out the hash chains. */
+
	if (dsz < srcsz)
+
		return (0);
+

+
	nchains = srcsz / sizeof(uint32_t);
+
	for (n = 0; n < nchains; n++) {
+
		t32 = *s32++;
+
		if (byteswap)
+
			SWAP_WORD(t32);
+
		WRITE_WORD(dst, t32);
+
	}
+

+
	return (1);
+
}
+
#endif
+

+
/*
+
 * Elf_Note structures comprise a fixed size header followed by variable
+
 * length strings.  The fixed size header needs to be byte swapped, but
+
 * not the strings.
+
 *
+
 * Argument `count' denotes the total number of bytes to be converted.
+
 * The destination buffer needs to be at least `count' bytes in size.
+
 */
+
static int
+
libelf_cvt_NOTE_tom(char *dst, size_t dsz, char *src, size_t count, 
+
    int byteswap)
+
{
+
	uint32_t namesz, descsz, type;
+
	Elf_Note *en;
+
	size_t sz, hdrsz;
+

+
	if (dsz < count)	/* Destination buffer is too small. */
+
		return (0);
+

+
	hdrsz = 3 * sizeof(uint32_t);
+
	if (count < hdrsz)		/* Source too small. */
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count);
+
		return (1);
+
	}
+

+
	/* Process all notes in the section. */
+
	while (count > hdrsz) {
+
		/* Read the note header. */
+
		READ_WORD(src, namesz);
+
		READ_WORD(src, descsz);
+
		READ_WORD(src, type);
+

+
		/* Translate. */
+
		SWAP_WORD(namesz);
+
		SWAP_WORD(descsz);
+
		SWAP_WORD(type);
+

+
		/* Copy out the translated note header. */
+
		en = (Elf_Note *) (uintptr_t) dst;
+
		en->n_namesz = namesz;
+
		en->n_descsz = descsz;
+
		en->n_type = type;
+

+
		dsz -= sizeof(Elf_Note);
+
		dst += sizeof(Elf_Note);
+
		count -= hdrsz;
+

+
		ROUNDUP2(namesz, 4);
+
		ROUNDUP2(descsz, 4);
+

+
		sz = namesz + descsz;
+

+
		if (count < sz || dsz < sz)	/* Buffers are too small. */
+
			return (0);
+

+
		(void) memcpy(dst, src, sz);
+

+
		src += sz;
+
		dst += sz;
+

+
		count -= sz;
+
		dsz -= sz;
+
	}
+

+
	return (1);
+
}
+

+
static int
+
libelf_cvt_NOTE_tof(char *dst, size_t dsz, char *src, size_t count,
+
    int byteswap)
+
{
+
	uint32_t namesz, descsz, type;
+
	Elf_Note *en;
+
	size_t sz;
+

+
	if (dsz < count)
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count);
+
		return (1);
+
	}
+

+
	while (count > sizeof(Elf_Note)) {
+

+
		en = (Elf_Note *) (uintptr_t) src;
+
		namesz = en->n_namesz;
+
		descsz = en->n_descsz;
+
		type = en->n_type;
+

+
		SWAP_WORD(namesz);
+
		SWAP_WORD(descsz);
+
		SWAP_WORD(type);
+

+
		WRITE_WORD(dst, namesz);
+
		WRITE_WORD(dst, descsz);
+
		WRITE_WORD(dst, type);
+

+
		src += sizeof(Elf_Note);
+

+
		ROUNDUP2(namesz, 4);
+
		ROUNDUP2(descsz, 4);
+

+
		sz = namesz + descsz;
+

+
		if (count < sz)
+
			sz = count;
+

+
		(void) memcpy(dst, src, sz);
+

+
		src += sz;
+
		dst += sz;
+
		count -= sz;
+
	}
+

+
	return (1);
+
}
+

+
struct converters {
+
	int	(*tof32)(char *dst, size_t dsz, char *src, size_t cnt,
+
		    int byteswap);
+
	int	(*tom32)(char *dst, size_t dsz, char *src, size_t cnt,
+
		    int byteswap);
+
	int	(*tof64)(char *dst, size_t dsz, char *src, size_t cnt,
+
		    int byteswap);
+
	int	(*tom64)(char *dst, size_t dsz, char *src, size_t cnt,
+
		    int byteswap);
+
};
+

+
divert(-1)
+
define(`CONV',
+
  `ifdef(`IGNORE_'$1$2,
+
    `.$3$2 = NULL',
+
    `ifdef(`BASE_'$1,
+
      `.$3$2 = libelf_cvt_$1_$3',
+
      `ifdef(`SIZEDEP_'$1,
+
        `.$3$2 = libelf_cvt_$1$2_$3',
+
        `.$3$2 = libelf_cvt$2_$1_$3')')')')
+

+
define(`CONVERTER_NAME',
+
  `ifdef(`IGNORE_'$1,`',
+
    `#if	__FreeBSD_version >= $3
+
    [ELF_T_$1] = {
+
        CONV($1,32,tof), CONV($1,32,tom),
+
        CONV($1,64,tof), CONV($1,64,tom) },
+
#endif
+
')')
+

+
define(`CONVERTER_NAMES',
+
  `ifelse($#,1,`',
+
    `CONVERTER_NAME($1)CONVERTER_NAMES(shift($@))')')
+

+
undefine(`IGNORE_BYTE32', `IGNORE_BYTE64')
+
divert(0)
+

+
static struct converters cvt[ELF_T_NUM] = {
+
CONVERTER_NAMES(ELF_TYPE_LIST)
+

+
	/*
+
	 * Types that needs hand-coded converters follow.
+
	 */
+

+
	[ELF_T_BYTE] = {
+
		.tof32 = libelf_cvt_BYTE_tox,
+
		.tom32 = libelf_cvt_BYTE_tox,
+
		.tof64 = libelf_cvt_BYTE_tox,
+
		.tom64 = libelf_cvt_BYTE_tox
+
	},
+

+
#if	__FreeBSD_version >= 800062
+
	[ELF_T_GNUHASH] = {
+
		.tof32 = libelf_cvt32_GNUHASH_tof,
+
		.tom32 = libelf_cvt32_GNUHASH_tom,
+
		.tof64 = libelf_cvt64_GNUHASH_tof,
+
		.tom64 = libelf_cvt64_GNUHASH_tom
+
	},
+
#endif
+

+
	[ELF_T_NOTE] = {
+
		.tof32 = libelf_cvt_NOTE_tof,
+
		.tom32 = libelf_cvt_NOTE_tom,
+
		.tof64 = libelf_cvt_NOTE_tof,
+
		.tom64 = libelf_cvt_NOTE_tom
+
	}
+
};
+

+
int (*_libelf_get_translator(Elf_Type t, int direction, int elfclass))
+
 (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap)
+
{
+
	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
+
	assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
+

+
	if (t >= ELF_T_NUM ||
+
	    (elfclass != ELFCLASS32 && elfclass != ELFCLASS64) ||
+
	    (direction != ELF_TOFILE && direction != ELF_TOMEMORY))
+
		return (NULL);
+

+
	return ((elfclass == ELFCLASS32) ?
+
	    (direction == ELF_TOFILE ? cvt[t].tof32 : cvt[t].tom32) :
+
	    (direction == ELF_TOFILE ? cvt[t].tof64 : cvt[t].tom64));
+
}
added external/libelf/libelf_data.c
@@ -0,0 +1,98 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: head/lib/libelf/libelf_data.c 233524 2012-03-26 21:31:57Z gonzo $");
+

+
#include <libelf.h>
+
#include <osreldate.h>
+

+
#include "_libelf.h"
+

+
int
+
_libelf_xlate_shtype(uint32_t sht)
+
{
+
	switch (sht) {
+
	case SHT_DYNAMIC:
+
		return (ELF_T_DYN);
+
	case SHT_DYNSYM:
+
		return (ELF_T_SYM);
+
	case SHT_FINI_ARRAY:
+
		return (ELF_T_ADDR);
+
#if	__FreeBSD_version >= 800062
+
	case SHT_GNU_HASH:
+
		return (ELF_T_GNUHASH);
+
#endif
+
	case SHT_GROUP:
+
		return (ELF_T_WORD);
+
	case SHT_HASH:
+
		return (ELF_T_WORD);
+
	case SHT_INIT_ARRAY:
+
		return (ELF_T_ADDR);
+
	case SHT_NOBITS:
+
		return (ELF_T_BYTE);
+
	case SHT_NOTE:
+
		return (ELF_T_NOTE);
+
	case SHT_PREINIT_ARRAY:
+
		return (ELF_T_ADDR);
+
	case SHT_PROGBITS:
+
		return (ELF_T_BYTE);
+
	case SHT_REL:
+
		return (ELF_T_REL);
+
	case SHT_RELA:
+
		return (ELF_T_RELA);
+
	case SHT_STRTAB:
+
		return (ELF_T_BYTE);
+
	case SHT_SYMTAB:
+
		return (ELF_T_SYM);
+
	case SHT_SYMTAB_SHNDX:
+
		return (ELF_T_WORD);
+
#if	__FreeBSD_version >= 700025
+
	case SHT_GNU_verdef:	/* == SHT_SUNW_verdef */
+
		return (ELF_T_VDEF);
+
	case SHT_GNU_verneed:	/* == SHT_SUNW_verneed */
+
		return (ELF_T_VNEED);
+
	case SHT_GNU_versym:	/* == SHT_SUNW_versym */
+
		return (ELF_T_HALF);
+
	case SHT_SUNW_move:
+
		return (ELF_T_MOVE);
+
	case SHT_SUNW_syminfo:
+
		return (ELF_T_SYMINFO);
+
	case SHT_SUNW_dof:
+
		return (ELF_T_BYTE);
+
#endif
+
	case SHT_MIPS_DWARF:
+
		/* FALLTHROUGH */
+
	case SHT_MIPS_REGINFO:
+
		/* FALLTHROUGH */
+
	case SHT_MIPS_OPTIONS:
+
		/* FALLTHROUGH */
+
	case SHT_AMD64_UNWIND:	/* == SHT_IA_64_UNWIND */
+
		return (ELF_T_BYTE);
+
	default:
+
		return (-1);
+
	}
+
}
added external/libelf/libelf_ehdr.c
@@ -0,0 +1,205 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 *
+
 * $FreeBSD: releng/9.1/lib/libelf/libelf_ehdr.c 210338 2010-07-21 10:25:02Z kaiw $
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_ehdr.c 210338 2010-07-21 10:25:02Z kaiw $");
+

+
#include <assert.h>
+
#include <gelf.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Retrieve counts for sections, phdrs and the section string table index
+
 * from section header #0 of the ELF object.
+
 */
+
static int
+
_libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum,
+
    uint16_t strndx)
+
{
+
	Elf_Scn *scn;
+
	size_t fsz;
+
	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
+
	uint32_t shtype;
+

+
	assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn));
+

+
	fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1);
+
	assert(fsz > 0);
+

+
	if (e->e_rawsize < shoff + fsz) { /* raw file too small */
+
		LIBELF_SET_ERROR(HEADER, 0);
+
		return (0);
+
	}
+

+
	if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL)
+
		return (0);
+

+
	xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec);
+
	(*xlator)((char *) &scn->s_shdr, sizeof(scn->s_shdr),
+
	    e->e_rawfile + shoff, (size_t) 1,
+
	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
+

+
#define	GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \
+
		scn->s_shdr.s_shdr64.M)
+

+
	if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL) {
+
		LIBELF_SET_ERROR(SECTION, 0);
+
		return (0);
+
	}
+

+
	e->e_u.e_elf.e_nscn = GET_SHDR_MEMBER(sh_size);
+
	e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum :
+
	    GET_SHDR_MEMBER(sh_info);
+
	e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx :
+
	    GET_SHDR_MEMBER(sh_link);
+
#undef	GET_SHDR_MEMBER
+

+
	return (1);
+
}
+

+
#define	EHDR_INIT(E,SZ)	 do {						\
+
		Elf##SZ##_Ehdr *eh = (E);				\
+
		eh->e_ident[EI_MAG0] = ELFMAG0;				\
+
		eh->e_ident[EI_MAG1] = ELFMAG1;				\
+
		eh->e_ident[EI_MAG2] = ELFMAG2;				\
+
		eh->e_ident[EI_MAG3] = ELFMAG3;				\
+
		eh->e_ident[EI_CLASS] = ELFCLASS##SZ;			\
+
		eh->e_ident[EI_DATA]  = ELFDATANONE;			\
+
		eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version);	\
+
		eh->e_machine = EM_NONE;				\
+
		eh->e_type    = ELF_K_NONE;				\
+
		eh->e_version = LIBELF_PRIVATE(version);		\
+
	} while (0)
+

+
void *
+
_libelf_ehdr(Elf *e, int ec, int allocate)
+
{
+
	void *ehdr;
+
	size_t fsz, msz;
+
	uint16_t phnum, shnum, strndx;
+
	uint64_t shoff;
+
	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
+

+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (e == NULL || e->e_kind != ELF_K_ELF) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (e->e_class != ELFCLASSNONE && e->e_class != ec) {
+
		LIBELF_SET_ERROR(CLASS, 0);
+
		return (NULL);
+
	}
+

+
	if (e->e_version != EV_CURRENT) {
+
		LIBELF_SET_ERROR(VERSION, 0);
+
		return (NULL);
+
	}
+

+
	if (e->e_class == ELFCLASSNONE)
+
		e->e_class = ec;
+

+
	if (ec == ELFCLASS32)
+
		ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32;
+
	else
+
		ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64;
+

+
	if (ehdr != NULL)	/* already have a translated ehdr */
+
		return (ehdr);
+

+
	fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1);
+
	assert(fsz > 0);
+

+
	if (e->e_cmd != ELF_C_WRITE && e->e_rawsize < fsz) {
+
		LIBELF_SET_ERROR(HEADER, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT);
+

+
	assert(msz > 0);
+

+
	if ((ehdr = calloc((size_t) 1, msz)) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr;
+
		EHDR_INIT(ehdr,32);
+
	} else {
+
		e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr;
+
		EHDR_INIT(ehdr,64);
+
	}
+

+
	if (allocate)
+
		e->e_flags |= ELF_F_DIRTY;
+

+
	if (e->e_cmd == ELF_C_WRITE)
+
		return (ehdr);
+

+
	xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec);
+
	(*xlator)(ehdr, msz, e->e_rawfile, (size_t) 1,
+
	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
+

+
	/*
+
	 * If extended numbering is being used, read the correct
+
	 * number of sections and program header entries.
+
	 */
+
	if (ec == ELFCLASS32) {
+
		phnum = ((Elf32_Ehdr *) ehdr)->e_phnum;
+
		shnum = ((Elf32_Ehdr *) ehdr)->e_shnum;
+
		shoff = ((Elf32_Ehdr *) ehdr)->e_shoff;
+
		strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx;
+
	} else {
+
		phnum = ((Elf64_Ehdr *) ehdr)->e_phnum;
+
		shnum = ((Elf64_Ehdr *) ehdr)->e_shnum;
+
		shoff = ((Elf64_Ehdr *) ehdr)->e_shoff;
+
		strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx;
+
	}
+

+
	if (shnum >= SHN_LORESERVE ||
+
	    (shoff == 0LL && (shnum != 0 || phnum == PN_XNUM ||
+
		strndx == SHN_XINDEX))) {
+
		LIBELF_SET_ERROR(HEADER, 0);
+
		return (NULL);
+
	}
+

+
	if (shnum != 0 || shoff == 0LL) { /* not using extended numbering */
+
		e->e_u.e_elf.e_nphdr = phnum;
+
		e->e_u.e_elf.e_nscn = shnum;
+
		e->e_u.e_elf.e_strndx = strndx;
+
	} else if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0)
+
		return (NULL);
+

+
	return (ehdr);
+
}
added external/libelf/libelf_extended.c
@@ -0,0 +1,136 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_extended.c 165535 2006-12-25 02:22:22Z jkoshy $");
+

+
#include <assert.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Retrieve section #0, allocating a new section if needed.
+
 */
+
static Elf_Scn *
+
_libelf_getscn0(Elf *e)
+
{
+
	Elf_Scn *s;
+

+
	if ((s = STAILQ_FIRST(&e->e_u.e_elf.e_scn)) != NULL)
+
		return (s);
+

+
	return (_libelf_allocate_scn(e, (size_t) SHN_UNDEF));
+
}
+

+
int
+
_libelf_setshnum(Elf *e, void *eh, int ec, size_t shnum)
+
{
+
	Elf_Scn *scn;
+

+
	if (shnum >= SHN_LORESERVE) {
+
		if ((scn = _libelf_getscn0(e)) == NULL)
+
			return (0);
+

+
		assert(scn->s_ndx == SHN_UNDEF);
+

+
		if (ec == ELFCLASS32)
+
			scn->s_shdr.s_shdr32.sh_size = shnum;
+
		else
+
			scn->s_shdr.s_shdr64.sh_size = shnum;
+

+
		(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
+

+
		shnum = 0;
+
	}
+

+
	if (ec == ELFCLASS32)
+
		((Elf32_Ehdr *) eh)->e_shnum = shnum;
+
	else
+
		((Elf64_Ehdr *) eh)->e_shnum = shnum;
+

+

+
	return (1);
+
}
+

+
int
+
_libelf_setshstrndx(Elf *e, void *eh, int ec, size_t shstrndx)
+
{
+
	Elf_Scn *scn;
+

+
	if (shstrndx >= SHN_LORESERVE) {
+
		if ((scn = _libelf_getscn0(e)) == NULL)
+
			return (0);
+

+
		assert(scn->s_ndx == SHN_UNDEF);
+

+
		if (ec == ELFCLASS32)
+
			scn->s_shdr.s_shdr32.sh_link = shstrndx;
+
		else
+
			scn->s_shdr.s_shdr64.sh_link = shstrndx;
+

+
		(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
+

+
		shstrndx = SHN_XINDEX;
+
	}
+

+
	if (ec == ELFCLASS32)
+
		((Elf32_Ehdr *) eh)->e_shstrndx = shstrndx;
+
	else
+
		((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx;
+

+
	return (1);
+
}
+

+
int
+
_libelf_setphnum(Elf *e, void *eh, int ec, size_t phnum)
+
{
+
	Elf_Scn *scn;
+

+
	if (phnum >= PN_XNUM) {
+
		if ((scn = _libelf_getscn0(e)) == NULL)
+
			return (0);
+

+
		assert(scn->s_ndx == SHN_UNDEF);
+

+
		if (ec == ELFCLASS32)
+
			scn->s_shdr.s_shdr32.sh_info = phnum;
+
		else
+
			scn->s_shdr.s_shdr64.sh_info = phnum;
+

+
		(void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY);
+

+
		phnum = PN_XNUM;
+
	}
+

+
	if (ec == ELFCLASS32)
+
		((Elf32_Ehdr *) eh)->e_phnum = phnum;
+
	else
+
		((Elf64_Ehdr *) eh)->e_phnum = phnum;
+

+
	return (1);
+
}
+

added external/libelf/libelf_fsize.c
@@ -0,0 +1,139 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS `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 THE AUTHOR OR CONTRIBUTORS 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.
+
 *
+
 * $FreeBSD: releng/9.1/lib/libelf/libelf_fsize.m4 210340 2010-07-21 10:39:29Z kaiw $
+
 */
+

+
#include <libelf.h>
+
#include <osreldate.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Create an array of file sizes from the elf_type definitions
+
 */
+

+

+

+
struct fsize {
+
	size_t fsz32;
+
	size_t fsz64;
+
};
+

+
static struct fsize fsize[ELF_T_NUM] = {
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_ADDR] = { .fsz32 = sizeof(Elf32_Addr), .fsz64 = sizeof(Elf64_Addr) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_BYTE] = { .fsz32 = 1, .fsz64 = 1 },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_CAP] = { .fsz32 = sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_DYN] = { .fsz32 = sizeof(Elf32_Sword)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Sxword)+sizeof(Elf64_Xword)+0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_EHDR] = { .fsz32 = EI_NIDENT+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Word)+sizeof(Elf32_Addr)+sizeof(Elf32_Off)+sizeof(Elf32_Off)+sizeof(Elf32_Word)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+0, .fsz64 = EI_NIDENT+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Word)+sizeof(Elf64_Addr)+sizeof(Elf64_Off)+sizeof(Elf64_Off)+sizeof(Elf64_Word)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+0 },
+
#endif
+
#if	__FreeBSD_version >= 800062
+
    [ELF_T_GNUHASH] = { .fsz32 = 1, .fsz64 = 1 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_HALF] = { .fsz32 = sizeof(Elf32_Half), .fsz64 = sizeof(Elf64_Half) },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_LWORD] = { .fsz32 = sizeof(Elf32_Lword), .fsz64 = sizeof(Elf64_Lword) },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_MOVE] = { .fsz32 = sizeof(Elf32_Lword)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+0, .fsz64 = sizeof(Elf64_Lword)+sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+0 },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_MOVEP] = { .fsz32 = 0, .fsz64 = 0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_NOTE] = { .fsz32 = 1, .fsz64 = 1 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_OFF] = { .fsz32 = sizeof(Elf32_Off), .fsz64 = sizeof(Elf64_Off) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_PHDR] = { .fsz32 = sizeof(Elf32_Word)+sizeof(Elf32_Off)+sizeof(Elf32_Addr)+sizeof(Elf32_Addr)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Off)+sizeof(Elf64_Addr)+sizeof(Elf64_Addr)+sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_REL] = { .fsz32 = sizeof(Elf32_Addr)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Addr)+sizeof(Elf64_Xword)+0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_RELA] = { .fsz32 = sizeof(Elf32_Addr)+sizeof(Elf32_Word)+sizeof(Elf32_Sword)+0, .fsz64 = sizeof(Elf64_Addr)+sizeof(Elf64_Xword)+sizeof(Elf64_Sxword)+0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_SHDR] = { .fsz32 = sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Addr)+sizeof(Elf32_Off)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Xword)+sizeof(Elf64_Addr)+sizeof(Elf64_Off)+sizeof(Elf64_Xword)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_SWORD] = { .fsz32 = sizeof(Elf32_Sword), .fsz64 = sizeof(Elf64_Sword) },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_SXWORD] = { .fsz32 = 0, .fsz64 = sizeof(Elf64_Sxword) },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_SYMINFO] = { .fsz32 = sizeof(Elf32_Half)+sizeof(Elf32_Half)+0, .fsz64 = sizeof(Elf64_Half)+sizeof(Elf64_Half)+0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_SYM] = { .fsz32 = sizeof(Elf32_Word)+sizeof(Elf32_Addr)+sizeof(Elf32_Word)+1+1+sizeof(Elf32_Half)+0, .fsz64 = sizeof(Elf64_Word)+1+1+sizeof(Elf64_Half)+sizeof(Elf64_Addr)+sizeof(Elf64_Xword)+0 },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_VDEF] = { .fsz32 = sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+0 },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_VNEED] = { .fsz32 = sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_WORD] = { .fsz32 = sizeof(Elf32_Word), .fsz64 = sizeof(Elf64_Word) },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_XWORD] = { .fsz32 = 0, .fsz64 = sizeof(Elf64_Xword) },
+
#endif
+

+
};
+

+
size_t
+
_libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c)
+
{
+
	size_t sz;
+

+
	sz = 0;
+
	if (v != EV_CURRENT)
+
		LIBELF_SET_ERROR(VERSION, 0);
+
	else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST)
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
	else {
+
		sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32;
+
		if (sz == 0)
+
			LIBELF_SET_ERROR(UNIMPL, 0);
+
	}
+

+
	return (sz*c);
+
}
+

added external/libelf/libelf_fsize.m4
@@ -0,0 +1,158 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 *
+
 * $FreeBSD: releng/9.1/lib/libelf/libelf_fsize.m4 210340 2010-07-21 10:39:29Z kaiw $
+
 */
+

+
#include <libelf.h>
+
#include <osreldate.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Create an array of file sizes from the elf_type definitions
+
 */
+

+
divert(-1)
+
include(SRCDIR`/elf_types.m4')
+

+
/*
+
 * Translations from structure definitions to the size of their file
+
 * representations.
+
 */
+

+
/* `Basic' types. */
+
define(`BYTE_SIZE',	1)
+
define(`IDENT_SIZE',	`EI_NIDENT')
+

+
/* Types that have variable length. */
+
define(`GNUHASH_SIZE',	1)
+
define(`NOTE_SIZE',	1)
+

+
/* Currently unimplemented types. */
+
define(`MOVEP_SIZE',	0)
+

+
/* Overrides for 32 bit types that do not exist. */
+
define(`XWORD_SIZE32',	0)
+
define(`SXWORD_SIZE32',	0)
+

+
/*
+
 * FSZ{32,64} define the sizes of 32 and 64 bit file structures respectively.
+
 */
+

+
define(`FSZ32',`_FSZ32($1_DEF)')
+
define(`_FSZ32',
+
  `ifelse($#,1,0,
+
    `_BSZ32($1)+_FSZ32(shift($@))')')
+
define(`_BSZ32',`$2_SIZE32')
+

+
define(`FSZ64',`_FSZ64($1_DEF)')
+
define(`_FSZ64',
+
  `ifelse($#,1,0,
+
    `_BSZ64($1)+_FSZ64(shift($@))')')
+
define(`_BSZ64',`$2_SIZE64')
+

+
/*
+
 * DEFINE_ELF_FSIZES(TYPE,NAME)
+
 *
+
 * Shorthand for defining  for 32 and 64 versions
+
 * of elf type TYPE.
+
 *
+
 * If TYPE`'_SIZE is defined, use its value for both 32 bit and 64 bit
+
 * sizes.
+
 *
+
 * Otherwise, look for a explicit 32/64 bit size definition for TYPE,
+
 * TYPE`'_SIZE32 or TYPE`'_SIZE64. If this definition is present, there
+
 * is nothing further to do.
+
 *
+
 * Otherwise, if an Elf{32,64}_`'NAME structure definition is known,
+
 * compute an expression that adds up the sizes of the structure's
+
 * constituents.
+
 *
+
 * If such a structure definition is not known, treat TYPE as a primitive
+
 * (i.e., integral) type and use sizeof(Elf{32,64}_`'NAME) to get its
+
 * file representation size.
+
 */
+

+
define(`DEFINE_ELF_FSIZE',
+
  `ifdef($1`_SIZE',
+
    `define($1_SIZE32,$1_SIZE)
+
     define($1_SIZE64,$1_SIZE)',
+
    `ifdef($1`_SIZE32',`',
+
      `ifdef(`Elf32_'$2`_DEF',
+
        `define($1_SIZE32,FSZ32(Elf32_$2))',
+
        `define($1_SIZE32,`sizeof(Elf32_'$2`)')')')
+
     ifdef($1`_SIZE64',`',
+
      `ifdef(`Elf64_'$2`_DEF',
+
        `define($1_SIZE64,FSZ64(Elf64_$2))',
+
        `define($1_SIZE64,`sizeof(Elf64_'$2`)')')')')')
+

+
define(`DEFINE_ELF_FSIZES',
+
  `ifelse($#,1,`',
+
    `DEFINE_ELF_FSIZE($1)
+
     DEFINE_ELF_FSIZES(shift($@))')')
+

+
DEFINE_ELF_FSIZES(ELF_TYPE_LIST)
+
DEFINE_ELF_FSIZE(`IDENT',`')	# `IDENT' is a pseudo type
+

+
define(`FSIZE',
+
  `#if	__FreeBSD_version >= $3
+
    [ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 },
+
#endif')
+
define(`FSIZES',
+
  `ifelse($#,1,`',
+
    `FSIZE($1)
+
FSIZES(shift($@))')')
+

+
divert(0)
+

+
struct fsize {
+
	size_t fsz32;
+
	size_t fsz64;
+
};
+

+
static struct fsize fsize[ELF_T_NUM] = {
+
FSIZES(ELF_TYPE_LIST)
+
};
+

+
size_t
+
_libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c)
+
{
+
	size_t sz;
+

+
	sz = 0;
+
	if (v != EV_CURRENT)
+
		LIBELF_SET_ERROR(VERSION, 0);
+
	else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST)
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
	else {
+
		sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32;
+
		if (sz == 0)
+
			LIBELF_SET_ERROR(UNIMPL, 0);
+
	}
+

+
	return (sz*c);
+
}
+

added external/libelf/libelf_msize.c
@@ -0,0 +1,142 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS `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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_msize.m4 210332 2010-07-21 09:56:42Z kaiw $");
+

+
#include <sys/types.h>
+
#include <sys/elf32.h>
+
#include <sys/elf64.h>
+

+
#include <assert.h>
+
#include <libelf.h>
+
#include <osreldate.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
/* WARNING: GENERATED FROM libelf_msize.m4. */
+

+
struct msize {
+
	size_t	msz32;
+
	size_t	msz64;
+
};
+

+

+

+
static struct msize msize[ELF_T_NUM] = {
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_ADDR] = { .msz32 = sizeof(Elf32_Addr), .msz64 = sizeof(Elf64_Addr) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_BYTE] = { .msz32 = 1, .msz64 = 1 },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_CAP] = { .msz32 = sizeof(Elf32_Cap), .msz64 = sizeof(Elf64_Cap) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_DYN] = { .msz32 = sizeof(Elf32_Dyn), .msz64 = sizeof(Elf64_Dyn) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_EHDR] = { .msz32 = sizeof(Elf32_Ehdr), .msz64 = sizeof(Elf64_Ehdr) },
+
#endif
+
#if	__FreeBSD_version >= 800062
+
    [ELF_T_GNUHASH] = { .msz32 = 1, .msz64 = 1 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_HALF] = { .msz32 = sizeof(Elf32_Half), .msz64 = sizeof(Elf64_Half) },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_LWORD] = { .msz32 = sizeof(Elf32_Lword), .msz64 = sizeof(Elf64_Lword) },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_MOVE] = { .msz32 = sizeof(Elf32_Move), .msz64 = sizeof(Elf64_Move) },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_MOVEP] = { .msz32 = 0, .msz64 = 0 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_NOTE] = { .msz32 = 1, .msz64 = 1 },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_OFF] = { .msz32 = sizeof(Elf32_Off), .msz64 = sizeof(Elf64_Off) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_PHDR] = { .msz32 = sizeof(Elf32_Phdr), .msz64 = sizeof(Elf64_Phdr) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_REL] = { .msz32 = sizeof(Elf32_Rel), .msz64 = sizeof(Elf64_Rel) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_RELA] = { .msz32 = sizeof(Elf32_Rela), .msz64 = sizeof(Elf64_Rela) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_SHDR] = { .msz32 = sizeof(Elf32_Shdr), .msz64 = sizeof(Elf64_Shdr) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_SWORD] = { .msz32 = sizeof(Elf32_Sword), .msz64 = sizeof(Elf64_Sword) },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_SXWORD] = { .msz32 = 0, .msz64 = sizeof(Elf64_Sxword) },
+
#endif
+
#if	__FreeBSD_version >= 700025
+
    [ELF_T_SYMINFO] = { .msz32 = sizeof(Elf32_Syminfo), .msz64 = sizeof(Elf64_Syminfo) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_SYM] = { .msz32 = sizeof(Elf32_Sym), .msz64 = sizeof(Elf64_Sym) },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_VDEF] = { .msz32 = sizeof(Elf32_Verdef), .msz64 = sizeof(Elf64_Verdef) },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_VNEED] = { .msz32 = sizeof(Elf32_Verneed), .msz64 = sizeof(Elf64_Verneed) },
+
#endif
+
#if	__FreeBSD_version >= 600102
+
    [ELF_T_WORD] = { .msz32 = sizeof(Elf32_Word), .msz64 = sizeof(Elf64_Word) },
+
#endif
+
#if	__FreeBSD_version >= 700009
+
    [ELF_T_XWORD] = { .msz32 = 0, .msz64 = sizeof(Elf64_Xword) },
+
#endif
+

+
};
+

+
size_t
+
_libelf_msize(Elf_Type t, int elfclass, unsigned int version)
+
{
+
	size_t sz;
+

+
	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
+
	assert((signed) t >= ELF_T_FIRST && t <= ELF_T_LAST);
+

+
	if (version != EV_CURRENT) {
+
		LIBELF_SET_ERROR(VERSION, 0);
+
		return (0);
+
	}
+

+
	sz = (elfclass == ELFCLASS32) ? msize[t].msz32 : msize[t].msz64;
+

+
	return (sz);
+
}
added external/libelf/libelf_msize.m4
@@ -0,0 +1,108 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_msize.m4 210332 2010-07-21 09:56:42Z kaiw $");
+

+
#include <sys/types.h>
+
#include <sys/elf32.h>
+
#include <sys/elf64.h>
+

+
#include <assert.h>
+
#include <libelf.h>
+
#include <osreldate.h>
+
#include <string.h>
+

+
#include "_libelf.h"
+

+
/* WARNING: GENERATED FROM __file__. */
+

+
struct msize {
+
	size_t	msz32;
+
	size_t	msz64;
+
};
+

+
divert(-1)
+
include(SRCDIR`/elf_types.m4')
+

+
define(BYTE_SIZE,	1)
+
define(GNUHASH_SIZE,	1)
+
define(NOTE_SIZE,	1)
+

+
/*
+
 * Unimplemented types.
+
 */
+
define(MOVEP_SIZE,	0)
+
define(SXWORD_SIZE32,	0)
+
define(XWORD_SIZE32,	0)
+

+
define(`DEFINE_ELF_MSIZE',
+
  `ifdef($1`_SIZE',
+
    `define($1_SIZE32,$1_SIZE)
+
     define($1_SIZE64,$1_SIZE)',
+
    `ifdef($1`_SIZE32',`',
+
      `define($1_SIZE32,sizeof(Elf32_$2))')
+
     ifdef($1`_SIZE64',`',
+
      `define($1_SIZE64,sizeof(Elf64_$2))')')')
+
define(`DEFINE_ELF_MSIZES',
+
  `ifelse($#,1,`',
+
    `DEFINE_ELF_MSIZE($1)
+
     DEFINE_ELF_MSIZES(shift($@))')')
+

+
DEFINE_ELF_MSIZES(ELF_TYPE_LIST)
+

+
define(`MSIZE',
+
  `#if	__FreeBSD_version >= $3
+
    [ELF_T_$1] = { .msz32 = $1_SIZE32, .msz64 = $1_SIZE64 },
+
#endif')
+
define(`MSIZES',
+
  `ifelse($#,1,`',
+
    `MSIZE($1)
+
MSIZES(shift($@))')')
+

+
divert(0)
+

+
static struct msize msize[ELF_T_NUM] = {
+
MSIZES(ELF_TYPE_LIST)
+
};
+

+
size_t
+
_libelf_msize(Elf_Type t, int elfclass, unsigned int version)
+
{
+
	size_t sz;
+

+
	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
+
	assert((signed) t >= ELF_T_FIRST && t <= ELF_T_LAST);
+

+
	if (version != EV_CURRENT) {
+
		LIBELF_SET_ERROR(VERSION, 0);
+
		return (0);
+
	}
+

+
	sz = (elfclass == ELFCLASS32) ? msize[t].msz32 : msize[t].msz64;
+

+
	return (sz);
+
}
added external/libelf/libelf_phdr.c
@@ -0,0 +1,157 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 *
+
 * $FreeBSD: releng/9.1/lib/libelf/libelf_phdr.c 210338 2010-07-21 10:25:02Z kaiw $
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_phdr.c 210338 2010-07-21 10:25:02Z kaiw $");
+

+
#include <assert.h>
+
#include <gelf.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+

+
#include "_libelf.h"
+

+
void *
+
_libelf_getphdr(Elf *e, int ec)
+
{
+
	size_t phnum, phentsize;
+
	size_t fsz, msz;
+
	uint64_t phoff;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+
	void *ehdr, *phdr;
+
	int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap);
+

+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+

+
	if (e == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if ((phdr = (ec == ELFCLASS32 ?
+
		 (void *) e->e_u.e_elf.e_phdr.e_phdr32 :
+
		 (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL)
+
		return (phdr);
+

+
	/*
+
	 * Check the PHDR related fields in the EHDR for sanity.
+
	 */
+

+
	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL)
+
		return (NULL);
+

+
	phnum = e->e_u.e_elf.e_nphdr;
+

+
	if (ec == ELFCLASS32) {
+
		eh32      = (Elf32_Ehdr *) ehdr;
+
		phentsize = eh32->e_phentsize;
+
		phoff     = (uint64_t) eh32->e_phoff;
+
	} else {
+
		eh64      = (Elf64_Ehdr *) ehdr;
+
		phentsize = eh64->e_phentsize;
+
		phoff     = (uint64_t) eh64->e_phoff;
+
	}
+

+
	fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version);
+

+
	assert(fsz > 0);
+

+
	if ((uint64_t) e->e_rawsize < (phoff + fsz)) {
+
		LIBELF_SET_ERROR(HEADER, 0);
+
		return (NULL);
+
	}
+

+
	msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT);
+

+
	assert(msz > 0);
+

+
	if ((phdr = calloc(phnum, msz)) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32)
+
		e->e_u.e_elf.e_phdr.e_phdr32 = phdr;
+
	else
+
		e->e_u.e_elf.e_phdr.e_phdr64 = phdr;
+

+

+
	xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec);
+
	(*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum,
+
	    e->e_byteorder != LIBELF_PRIVATE(byteorder));
+

+
	return (phdr);
+
}
+

+
void *
+
_libelf_newphdr(Elf *e, int ec, size_t count)
+
{
+
	void *ehdr, *newphdr, *oldphdr;
+
	size_t msz;
+

+
	if (e == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) {
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		return (NULL);
+
	}
+

+
	assert(e->e_class == ec);
+
	assert(ec == ELFCLASS32 || ec == ELFCLASS64);
+
	assert(e->e_version == EV_CURRENT);
+

+
	msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version);
+

+
	assert(msz > 0);
+

+
	newphdr = NULL;
+
	if (count > 0 && (newphdr = calloc(count, msz)) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL)
+
			free(oldphdr);
+
		e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr;
+
	} else {
+
		if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL)
+
			free(oldphdr);
+
		e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr;
+
	}
+

+
	e->e_u.e_elf.e_nphdr = count;
+

+
	elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY);
+

+
	return (newphdr);
+
}
added external/libelf/libelf_shdr.c
@@ -0,0 +1,55 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_shdr.c 164190 2006-11-11 17:16:35Z jkoshy $");
+

+
#include <gelf.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
void *
+
_libelf_getshdr(Elf_Scn *s, int ec)
+
{
+
	Elf *e;
+

+
	if (s == NULL || (e = s->s_elf) == NULL ||
+
	    e->e_kind != ELF_K_ELF) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASSNONE)
+
		ec = e->e_class;
+

+
	if (ec != e->e_class) {
+
		LIBELF_SET_ERROR(CLASS, 0);
+
		return (NULL);
+
	}
+

+
	return ((void *) &s->s_shdr);
+
}
added external/libelf/libelf_xlate.c
@@ -0,0 +1,149 @@
+
/*-
+
 * Copyright (c) 2006 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
+
 */
+

+
#include <sys/cdefs.h>
+
__FBSDID("$FreeBSD: releng/9.1/lib/libelf/libelf_xlate.c 210338 2010-07-21 10:25:02Z kaiw $");
+

+
#include <assert.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
/*
+
 * Translate to/from the file representation of ELF objects.
+
 *
+
 * Translation could potentially involve the following
+
 * transformations:
+
 *
+
 * - an endianness conversion,
+
 * - a change of layout, as the file representation of ELF objects
+
 *   can differ from their in-memory representation.
+
 * - a change in representation due to a layout version change.
+
 */
+

+
Elf_Data *
+
_libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding,
+
    int elfclass, int direction)
+
{
+
	int byteswap;
+
	size_t cnt, dsz, fsz, msz;
+
	uintptr_t sb, se, db, de;
+

+
	if (encoding == ELFDATANONE)
+
		encoding = LIBELF_PRIVATE(byteorder);
+

+
	if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) ||
+
	    dst == NULL || src == NULL || dst == src)	{
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
+
	assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
+

+
	if (dst->d_version != src->d_version) {
+
		LIBELF_SET_ERROR(UNIMPL, 0);
+
		return (NULL);
+
	}
+

+
	if  (src->d_buf == NULL || dst->d_buf == NULL) {
+
		LIBELF_SET_ERROR(DATA, 0);
+
		return (NULL);
+
	}
+

+
	if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) {
+
		LIBELF_SET_ERROR(DATA, 0);
+
		return (NULL);
+
	}
+

+
	if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize)
+
	    (src->d_type, (size_t) 1, src->d_version)) == 0)
+
		return (NULL);
+

+
	msz = _libelf_msize(src->d_type, elfclass, src->d_version);
+

+
	assert(msz > 0);
+

+
	if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) {
+
		LIBELF_SET_ERROR(DATA, 0);
+
		return (NULL);
+
	}
+

+
	/*
+
	 * Determine the number of objects that need to be converted, and
+
	 * the space required for the converted objects in the destination
+
	 * buffer.
+
	 */
+
	if (direction == ELF_TOMEMORY) {
+
		cnt = src->d_size / fsz;
+
		dsz = cnt * msz;
+
	} else {
+
		cnt = src->d_size / msz;
+
		dsz = cnt * fsz;
+
	}
+

+
	if (dst->d_size  <  dsz) {
+
		LIBELF_SET_ERROR(DATA, 0);
+
		return (NULL);
+
	}
+

+
	sb = (uintptr_t) src->d_buf;
+
	se = sb + src->d_size;
+
	db = (uintptr_t) dst->d_buf;
+
	de = db + dst->d_size;
+

+
	/*
+
	 * Check for overlapping buffers.  Note that db == sb is
+
	 * allowed.
+
	 */
+
	if (db != sb && de > sb && se > db) {
+
		LIBELF_SET_ERROR(DATA, 0);
+
		return (NULL);
+
	}
+

+
	if ((direction == ELF_TOMEMORY ? db : sb) %
+
	    _libelf_malign(src->d_type, elfclass)) {
+
		LIBELF_SET_ERROR(DATA, 0);
+
		return (NULL);
+
	}
+

+
	dst->d_type = src->d_type;
+
	dst->d_size = dsz;
+

+
	byteswap = encoding != LIBELF_PRIVATE(byteorder);
+

+
	if (src->d_size == 0 ||
+
	    (db == sb && !byteswap && fsz == msz))
+
		return (dst);	/* nothing more to do */
+

+
	if (!(_libelf_get_translator(src->d_type, direction, elfclass))
+
	    (dst->d_buf, dsz, src->d_buf, cnt, byteswap)) {
+
		LIBELF_SET_ERROR(DATA, 0);
+
		return (NULL);
+
	}
+

+
	return (dst);
+
}
modified pkg-static/Makefile
@@ -5,8 +5,13 @@ NO_SHARED?= yes
NO_MAN=		yes
NOMAN=		yes
LDADD_STATIC=	-L${.OBJDIR}/../external/sqlite \
-
		-L${.OBJDIR}/../external/libyaml \
-
		-lsqlite3 \
+
		-L${.OBJDIR}/../external/libyaml
+

+
.if defined(WITH_BUNDLED_LIBELF)
+
LDADD_STATIC+=	-L${.OBJDIR}/../external/libelf
+
.endif
+

+
LDADD_STATIC+=	-lsqlite3 \
		-lyaml \
		-larchive \
		-lsbuf \