Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
build: cleanly separate build system from vendored source
Baptiste Daroussin committed 1 month ago
commit 7bba0f186e9319ceb2e58bcd846ce10afdd3e374
parent bd9cf035609b0f533645495daea418fe0088b4b2
295 files changed +27234 -27236
deleted Makefile.autosetup
@@ -1,82 +0,0 @@
-
include @builddir@/mk/defs.mk
-
DIRS=	external compat libpkg/repo libpkg src docs scripts
-
@if TESTS
-
DIRS+=	tests
-
@endif
-

-
include $(MK)/dir.mk
-
include $(MK)/common.mk
-

-
VPATH=	$(top_srcdir)
-

-
@if coverage
-
check: clean-profiles
-

-
clean-profiles:
-
	rm -f /tmp/pkg.*.profraw
-
@endif
-

-
configure_call: auto.def @CONF_GEN_FILES@
-
	sh configure_call
-
	${MAKE}
-

-
Makefile: configure_call
-

-
check: all
-

-
@if TESTS
-
all: Kyuafile Makefile
-

-
check: UndefinedBehaviour.suppress Leak.suppress
-
	export UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1:suppressions=$(top_builddir)/UndefinedBehaviour.suppress; \
-
	export LSAN_OPTIONS=print_suppressions=0:exitcode=0:suppressions=$(top_builddir)/Leak.suppress; \
-
	if [ "$$(uname)" != "FreeBSD" ]; then \
-
		export ASAN_OPTIONS=detect_leaks=1 ; \
-
	fi ; \
-
	export LLVM_PROFILE_FILE=/tmp/pkg.%p.profraw; \
-
	if [ "$(HTML)" != "" ]; then \
-
		args="-r $(top_builddir)/res.db" ; \
-
	fi ; \
-
	parallel=$$(getconf NPROCESSORS_ONLN 2>/dev/null || getconf _NPROCESSORS_ONLN ) ; \
-
	kyua --config=none \
-
		--variable parallelism=$${parallel} \
-
		test $$args \
-
		--kyuafile='$(top_builddir)/Kyuafile' \
-
		--build-root='$(top_builddir)' || FAILED=1 ; \
-
	if [ "$(HTML)" != "" ]; then \
-
		kyua report-html --force $$args --output=$(HTML) ; \
-
		rm -f $(top_builddir)/res.db ; \
-
	fi ; \
-
	exit $${FAILED}
-
@endif
-

-
@if coverage
-
check-coverage: check
-
	llvm-profdata merge --output=pkg.profdata /tmp/pkg.*.profraw
-

-
report-coverage:
-
	llvm-cov report ./src/pkg -instr-profile=pkg.profdata --use-color -ignore-filename-regex=external -ignore-filename-regex=compat
-
@endif
-

-
COCCI_ARGS=	-I ${top_srcdir} \
-
		-I /usr/include \
-
		-I /usr/local/include \
-
		-I ${top_srcdir}/compat \
-
		-I ${top_srcdir}/libpkg \
-
		-I ${top_srcdir}/src \
-
		-I ${top_srcdir}/external/yxml \
-
		-I ${top_srcdir}/external/libucl/include \
-
		-I ${top_srcdir}/external/uthash \
-
		-I ${top_srcdir}/external/sqlite  \
-
		-I ${top_srcdir}/external/libelf
-

-
1cocci:
-
	spatch ${COCCI_ARGS} -in_place -sp_file $(top_srcdir)/tests/cocci/${COCCITEST} -dir ${top_srcdir}/libpkg ; \
-
	spatch ${COCCI_ARGS} -in_place -sp_file $(top_srcdir)/tests/cocci/${COCCITEST} -dir ${top_srcdir}/src ;
-

-
cocci:
-
	for c in ${top_srcdir}/tests/cocci/*.cocci ; do \
-
		echo "Passing $$c" ; \
-
		spatch ${COCCI_ARGS} -in_place -sp_file $$c -dir ${top_srcdir}/libpkg ; \
-
		spatch ${COCCI_ARGS} -in_place -sp_file $$c -dir ${top_srcdir}/src ; \
-
	done
added Makefile.in
@@ -0,0 +1,82 @@
+
include @builddir@/mk/defs.mk
+
DIRS=	external compat libpkg/repo libpkg src docs scripts
+
@if TESTS
+
DIRS+=	tests
+
@endif
+

+
include $(MK)/dir.mk
+
include $(MK)/common.mk
+

+
VPATH=	$(top_srcdir)
+

+
@if coverage
+
check: clean-profiles
+

+
clean-profiles:
+
	rm -f /tmp/pkg.*.profraw
+
@endif
+

+
configure_call: auto.def @CONF_GEN_FILES@
+
	sh configure_call
+
	${MAKE}
+

+
Makefile: configure_call
+

+
check: all
+

+
@if TESTS
+
all: Kyuafile Makefile
+

+
check: UndefinedBehaviour.suppress Leak.suppress
+
	export UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1:suppressions=$(top_builddir)/UndefinedBehaviour.suppress; \
+
	export LSAN_OPTIONS=print_suppressions=0:exitcode=0:suppressions=$(top_builddir)/Leak.suppress; \
+
	if [ "$$(uname)" != "FreeBSD" ]; then \
+
		export ASAN_OPTIONS=detect_leaks=1 ; \
+
	fi ; \
+
	export LLVM_PROFILE_FILE=/tmp/pkg.%p.profraw; \
+
	if [ "$(HTML)" != "" ]; then \
+
		args="-r $(top_builddir)/res.db" ; \
+
	fi ; \
+
	parallel=$$(getconf NPROCESSORS_ONLN 2>/dev/null || getconf _NPROCESSORS_ONLN ) ; \
+
	kyua --config=none \
+
		--variable parallelism=$${parallel} \
+
		test $$args \
+
		--kyuafile='$(top_builddir)/Kyuafile' \
+
		--build-root='$(top_builddir)' || FAILED=1 ; \
+
	if [ "$(HTML)" != "" ]; then \
+
		kyua report-html --force $$args --output=$(HTML) ; \
+
		rm -f $(top_builddir)/res.db ; \
+
	fi ; \
+
	exit $${FAILED}
+
@endif
+

+
@if coverage
+
check-coverage: check
+
	llvm-profdata merge --output=pkg.profdata /tmp/pkg.*.profraw
+

+
report-coverage:
+
	llvm-cov report ./src/pkg -instr-profile=pkg.profdata --use-color -ignore-filename-regex=external -ignore-filename-regex=compat
+
@endif
+

+
COCCI_ARGS=	-I ${top_srcdir} \
+
		-I /usr/include \
+
		-I /usr/local/include \
+
		-I ${top_srcdir}/compat \
+
		-I ${top_srcdir}/libpkg \
+
		-I ${top_srcdir}/src \
+
		-I ${top_srcdir}/external/yxml \
+
		-I ${top_srcdir}/external/libucl/include \
+
		-I ${top_srcdir}/external/uthash \
+
		-I ${top_srcdir}/external/sqlite  \
+
		-I ${top_srcdir}/external/elf
+

+
1cocci:
+
	spatch ${COCCI_ARGS} -in_place -sp_file $(top_srcdir)/tests/cocci/${COCCITEST} -dir ${top_srcdir}/libpkg ; \
+
	spatch ${COCCI_ARGS} -in_place -sp_file $(top_srcdir)/tests/cocci/${COCCITEST} -dir ${top_srcdir}/src ;
+

+
cocci:
+
	for c in ${top_srcdir}/tests/cocci/*.cocci ; do \
+
		echo "Passing $$c" ; \
+
		spatch ${COCCI_ARGS} -in_place -sp_file $$c -dir ${top_srcdir}/libpkg ; \
+
		spatch ${COCCI_ARGS} -in_place -sp_file $$c -dir ${top_srcdir}/src ; \
+
	done
modified auto.def
@@ -337,23 +337,20 @@ if {[get-define CCACHE] ne {none}} {
}

make-config-header pkg_config.h -bare { DEFAULT_* }
-
make-config-header external/sqlite/sqlite_generated.h
+
make-config-header external/libsqlite/sqlite_generated.h
foreach in [list mk/defs.mk.in libpkg/pkg.h.in libpkg/pkg.pc.in \
	tests/frontend/test_environment.sh.in ] {
	make-template $in
	define-append CONF_GEN_FILES $in
}
-
make-template Makefile.autosetup Makefile
-
define-append CONF_GEN_FILES Makefile.autosetup
-

-
foreach dir [list external/blake2 external/picosat \
-
	external/linenoise external/sqlite external/libfetch \
+
foreach dir [list . external/libblake2 external/libpicosat \
+
	external/liblinenoise external/libsqlite external/libfetch \
	external compat libpkg libpkg/repo libpkg/repo/binary src \
	external/libucl external/libelf tests docs \
-
	external/liblua external/yxml scripts external/libder \
+
	external/liblua external/libyxml scripts external/libder \
	external/libecc] {
-
	make-template $dir/Makefile.autosetup $dir/Makefile
-
	define-append CONF_GEN_FILES $dir/Makefile.autosetup
+
	make-template $dir/Makefile.in
+
	define-append CONF_GEN_FILES $dir/Makefile.in
}

set out [open "configure_call" w+]
deleted compat/Makefile.autosetup
@@ -1,16 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	bsd_compat
-
SRCS=	closefrom.c \
-
	humanize_number.c \
-
	strtonum.c \
-
	funopen.c
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
-
		-I$(top_srcdir)/libpkg \
-
		-I$(top_srcdir) \
-
		-I$(top_builddir)
-

-
VPATH=	$(top_srcdir)/compat
-

-
include $(MK)/static-lib.mk
-

added compat/Makefile.in
@@ -0,0 +1,16 @@
+
include @builddir@/mk/defs.mk
+
LIB=	bsd_compat
+
SRCS=	closefrom.c \
+
	humanize_number.c \
+
	strtonum.c \
+
	funopen.c
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
+
		-I$(top_srcdir)/libpkg \
+
		-I$(top_srcdir) \
+
		-I$(top_builddir)
+

+
VPATH=	$(top_srcdir)/compat
+

+
include $(MK)/static-lib.mk
+

deleted docs/Makefile.autosetup
@@ -1,84 +0,0 @@
-
include @builddir@/mk/defs.mk
-

-
MAN3=	pkg_printf.3 \
-
	pkg_create.3 \
-
	pkg_repo_create.3 \
-
	pkg_repos.3
-
MAN5=	pkg-repository.5 \
-
	pkg-keywords.5 \
-
	pkg-script.5 \
-
	pkg-lua-script.5 \
-
	pkg-triggers.5 \
-
	pkg.conf.5
-
MAN8=	pkg-add.8 \
-
	pkg-alias.8 \
-
	pkg-annotate.8 \
-
	pkg-audit.8 \
-
	pkg-autoremove.8 \
-
	pkg-check.8 \
-
	pkg-clean.8 \
-
	pkg-config.8 \
-
	pkg-create.8 \
-
	pkg-delete.8 \
-
	pkg-fetch.8 \
-
	pkg-help.8 \
-
	pkg-info.8 \
-
	pkg-install.8 \
-
	pkg-key.8 \
-
	pkg-lock.8 \
-
	pkg-plugins.8 \
-
	pkg-query.8 \
-
	pkg-register.8 \
-
	pkg-repo.8 \
-
	pkg-repositories.8 \
-
	pkg-rquery.8 \
-
	pkg-search.8 \
-
	pkg-set.8 \
-
	pkg-shell.8 \
-
	pkg-shlib.8 \
-
	pkg-ssh.8 \
-
	pkg-stats.8 \
-
	pkg-triggers.8 \
-
	pkg-unregister.8 \
-
	pkg-update.8 \
-
	pkg-updating.8 \
-
	pkg-upgrade.8 \
-
	pkg-version.8 \
-
	pkg-which.8 \
-
	pkg.8
-

-
MAN3GZ=	$(MAN3:.3=.3.gz)
-
MAN5GZ=	$(MAN5:.5=.5.gz)
-
MAN8GZ=	$(MAN8:.8=.8.gz)
-

-
VPATH=	$(top_srcdir)/docs
-

-
.SUFFIXES: .3.gz .5.gz .8.gz .3 .5 .8
-

-
all: $(MAN3GZ) $(MAN5GZ) $(MAN8GZ)
-

-
.3.3.gz:
-
	gzip -knc $< > $@
-

-
.5.5.gz:
-
	gzip -knc $< > $@
-

-
.8.8.gz:
-
	gzip -knc $< > $@
-

-
clean:
-
	rm -f *.gz
-

-
run-fix-xrefs:
-
	./fix-xrefs $(MAN3) $(MAN5) $(MAN8)
-

-
install:
-
	install -d $(DESTDIR)$(mandir)/man3
-
	install -d $(DESTDIR)$(mandir)/man5
-
	install -d $(DESTDIR)$(mandir)/man8
-
	install -m 644 *.3.gz $(DESTDIR)$(mandir)/man3/
-
	install -m 644 *.5.gz $(DESTDIR)$(mandir)/man5/
-
	install -m 644 *.8.gz $(DESTDIR)$(mandir)/man8/
-
	ln -sf pkg-delete.8.gz $(DESTDIR)$(mandir)/man8/pkg-remove.8.gz
-
	ln -sf pkg.8.gz $(DESTDIR)$(mandir)/man8/pkg-static.8.gz
-
	ln -sf pkg-lock.8.gz $(DESTDIR)$(mandir)/man8/pkg-unlock.8.gz
added docs/Makefile.in
@@ -0,0 +1,84 @@
+
include @builddir@/mk/defs.mk
+

+
MAN3=	pkg_printf.3 \
+
	pkg_create.3 \
+
	pkg_repo_create.3 \
+
	pkg_repos.3
+
MAN5=	pkg-repository.5 \
+
	pkg-keywords.5 \
+
	pkg-script.5 \
+
	pkg-lua-script.5 \
+
	pkg-triggers.5 \
+
	pkg.conf.5
+
MAN8=	pkg-add.8 \
+
	pkg-alias.8 \
+
	pkg-annotate.8 \
+
	pkg-audit.8 \
+
	pkg-autoremove.8 \
+
	pkg-check.8 \
+
	pkg-clean.8 \
+
	pkg-config.8 \
+
	pkg-create.8 \
+
	pkg-delete.8 \
+
	pkg-fetch.8 \
+
	pkg-help.8 \
+
	pkg-info.8 \
+
	pkg-install.8 \
+
	pkg-key.8 \
+
	pkg-lock.8 \
+
	pkg-plugins.8 \
+
	pkg-query.8 \
+
	pkg-register.8 \
+
	pkg-repo.8 \
+
	pkg-repositories.8 \
+
	pkg-rquery.8 \
+
	pkg-search.8 \
+
	pkg-set.8 \
+
	pkg-shell.8 \
+
	pkg-shlib.8 \
+
	pkg-ssh.8 \
+
	pkg-stats.8 \
+
	pkg-triggers.8 \
+
	pkg-unregister.8 \
+
	pkg-update.8 \
+
	pkg-updating.8 \
+
	pkg-upgrade.8 \
+
	pkg-version.8 \
+
	pkg-which.8 \
+
	pkg.8
+

+
MAN3GZ=	$(MAN3:.3=.3.gz)
+
MAN5GZ=	$(MAN5:.5=.5.gz)
+
MAN8GZ=	$(MAN8:.8=.8.gz)
+

+
VPATH=	$(top_srcdir)/docs
+

+
.SUFFIXES: .3.gz .5.gz .8.gz .3 .5 .8
+

+
all: $(MAN3GZ) $(MAN5GZ) $(MAN8GZ)
+

+
.3.3.gz:
+
	gzip -knc $< > $@
+

+
.5.5.gz:
+
	gzip -knc $< > $@
+

+
.8.8.gz:
+
	gzip -knc $< > $@
+

+
clean:
+
	rm -f *.gz
+

+
run-fix-xrefs:
+
	./fix-xrefs $(MAN3) $(MAN5) $(MAN8)
+

+
install:
+
	install -d $(DESTDIR)$(mandir)/man3
+
	install -d $(DESTDIR)$(mandir)/man5
+
	install -d $(DESTDIR)$(mandir)/man8
+
	install -m 644 *.3.gz $(DESTDIR)$(mandir)/man3/
+
	install -m 644 *.5.gz $(DESTDIR)$(mandir)/man5/
+
	install -m 644 *.8.gz $(DESTDIR)$(mandir)/man8/
+
	ln -sf pkg-delete.8.gz $(DESTDIR)$(mandir)/man8/pkg-remove.8.gz
+
	ln -sf pkg.8.gz $(DESTDIR)$(mandir)/man8/pkg-static.8.gz
+
	ln -sf pkg-lock.8.gz $(DESTDIR)$(mandir)/man8/pkg-unlock.8.gz
modified external/.editorconfig
@@ -1,5 +1,5 @@
root = true

-
[Makefile.autosetup]
+
[Makefile.in]
tab_width = 8
indent_style = tab
deleted external/Makefile.autosetup
@@ -1,6 +0,0 @@
-
include @builddir@/mk/defs.mk
-
DIRS=	blake2 picosat linenoise libfetch sqlite libucl liblua yxml libder libecc
-
@if libelf-internal
-
DIRS+=	libelf
-
@endif
-
include $(MK)/dir.mk
added external/Makefile.in
@@ -0,0 +1,6 @@
+
include @builddir@/mk/defs.mk
+
DIRS=	libblake2 libpicosat liblinenoise libfetch libsqlite libucl liblua libyxml libder libecc
+
@if libelf-internal
+
DIRS+=	libelf
+
@endif
+
include $(MK)/dir.mk
deleted external/blake2/Makefile.autosetup
@@ -1,8 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	blake2
-
SRCS=	blake2b-ref.c blake2s-ref.c
-
LOCAL_CFLAGS=	-I$(top_srcdir)/compat -O3
-

-
VPATH=	$(top_srcdir)/external/blake2
-

-
include $(MK)/static-lib.mk
added external/elf/Version.map
@@ -0,0 +1,96 @@
+
/* $Id$ */
+

+
R1.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_flagarhdr;
+
	elf_flagdata;
+
	elf_flagehdr;
+
	elf_flagelf;
+
	elf_flagphdr;
+
	elf_flagscn;
+
	elf_flagshdr;
+
	elf_getarhdr;
+
	elf_getarsym;
+
	elf_getbase;
+
	elf_getdata;
+
	elf_getident;
+
	elf_getphdrnum;
+
	elf_getphnum;
+
	elf_getscn;
+
	elf_getshdrnum;
+
	elf_getshdrstrndx;
+
	elf_getshnum;
+
	elf_getshstrndx;
+
	elf_hash;
+
	elf_kind;
+
	elf_memory;
+
	elf_ndxscn;
+
	elf_newdata;
+
	elf_newscn;
+
	elf_next;
+
	elf_nextscn;
+
	elf_open;
+
	elf_openmemory;
+
	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:
+
	*;
+
};
added external/elf/_elftc.h
@@ -0,0 +1,395 @@
+
/*-
+
 * Copyright (c) 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.
+
 *
+
 * $Id: _elftc.h 2495 2012-04-23 05:31:56Z jkoshy $
+
 */
+

+
/**
+
 ** Miscellanous definitions needed by multiple components.
+
 **/
+

+
#ifndef	_ELFTC_H
+
#define	_ELFTC_H
+

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

+
#ifndef	offsetof
+
#define	offsetof(T, M)		((int) &((T*) 0) -> M)
+
#endif
+

+
/* --QUEUE-MACROS-- [[ */
+

+
/*
+
 * Supply macros missing from <sys/queue.h>
+
 */
+

+
/*
+
 * Copyright (c) 1991, 1993
+
 *	The Regents of the University of California.  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.
+
 * 3. Neither the name of the University nor the names of its contributors
+
 *    may be used to endorse or promote products derived from this software
+
 *    without specific prior written permission.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+
 */
+

+
#ifndef	STAILQ_CONCAT
+
#define	STAILQ_CONCAT(head1, head2) do {			\
+
	if (!STAILQ_EMPTY((head2))) {				\
+
		*(head1)->stqh_last = (head2)->stqh_first;	\
+
		(head1)->stqh_last = (head2)->stqh_last;	\
+
		STAILQ_INIT((head2));				\
+
	}							\
+
} while (/*CONSTCOND*/0)
+
#endif
+

+
#ifndef	STAILQ_EMPTY
+
#define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
+
#endif
+

+
#ifndef	STAILQ_ENTRY
+
#define	STAILQ_ENTRY(type)					\
+
struct {							\
+
	struct type *stqe_next;	/* next element */		\
+
}
+
#endif
+

+
#ifndef	STAILQ_FIRST
+
#define	STAILQ_FIRST(head)	((head)->stqh_first)
+
#endif
+

+
#ifndef	STAILQ_HEAD
+
#define	STAILQ_HEAD(name, type)					\
+
struct name {							\
+
	struct type *stqh_first; /* first element */		\
+
	struct type **stqh_last; /* addr of last next element */ \
+
}
+
#endif
+

+
#ifndef	STAILQ_HEAD_INITIALIZER
+
#define	STAILQ_HEAD_INITIALIZER(head)				\
+
	{ NULL, &(head).stqh_first }
+
#endif
+

+
#ifndef	STAILQ_FOREACH
+
#define	STAILQ_FOREACH(var, head, field)			\
+
	for ((var) = ((head)->stqh_first);			\
+
		(var);						\
+
		(var) = ((var)->field.stqe_next))
+
#endif
+

+
#ifndef	STAILQ_FOREACH_SAFE
+
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)		\
+
       for ((var) = STAILQ_FIRST((head));			\
+
	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);	\
+
	    (var) = (tvar))
+
#endif
+

+
#ifndef	STAILQ_INIT
+
#define	STAILQ_INIT(head) do {					\
+
	(head)->stqh_first = NULL;				\
+
	(head)->stqh_last = &(head)->stqh_first;		\
+
} while (/*CONSTCOND*/0)
+
#endif
+

+
#ifndef	STAILQ_INSERT_HEAD
+
#define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
+
	if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)	\
+
		(head)->stqh_last = &(elm)->field.stqe_next;		\
+
	(head)->stqh_first = (elm);					\
+
} while (/*CONSTCOND*/0)
+
#endif
+

+
#ifndef	STAILQ_INSERT_TAIL
+
#define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
+
	(elm)->field.stqe_next = NULL;					\
+
	*(head)->stqh_last = (elm);					\
+
	(head)->stqh_last = &(elm)->field.stqe_next;			\
+
} while (/*CONSTCOND*/0)
+
#endif
+

+
#ifndef	STAILQ_INSERT_AFTER
+
#define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
+
	if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
+
		(head)->stqh_last = &(elm)->field.stqe_next;		\
+
	(listelm)->field.stqe_next = (elm);				\
+
} while (/*CONSTCOND*/0)
+
#endif
+

+
#ifndef	STAILQ_LAST
+
#define STAILQ_LAST(head, type, field)					\
+
	(STAILQ_EMPTY((head)) ?					\
+
	    NULL : ((struct type *)(void *)				\
+
	    ((char *)((head)->stqh_last) - offsetof(struct type, field))))
+
#endif
+

+
#ifndef	STAILQ_NEXT
+
#define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
+
#endif
+

+
#ifndef	STAILQ_REMOVE
+
#define	STAILQ_REMOVE(head, elm, type, field) do {			\
+
	if ((head)->stqh_first == (elm)) {				\
+
		STAILQ_REMOVE_HEAD((head), field);			\
+
	} else {							\
+
		struct type *curelm = (head)->stqh_first;		\
+
		while (curelm->field.stqe_next != (elm))		\
+
			curelm = curelm->field.stqe_next;		\
+
		if ((curelm->field.stqe_next =				\
+
			curelm->field.stqe_next->field.stqe_next) == NULL) \
+
			    (head)->stqh_last = &(curelm)->field.stqe_next; \
+
	}								\
+
} while (/*CONSTCOND*/0)
+
#endif
+

+
#ifndef	STAILQ_REMOVE_HEAD
+
#define	STAILQ_REMOVE_HEAD(head, field) do {				\
+
	if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == \
+
	    NULL)							\
+
		(head)->stqh_last = &(head)->stqh_first;		\
+
} while (/*CONSTCOND*/0)
+
#endif
+

+
#ifndef	TAILQ_FOREACH_SAFE
+
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
+
	for ((var) = TAILQ_FIRST((head));                               \
+
	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
+
	    (var) = (tvar))
+
#endif
+

+
/* ]] --QUEUE-MACROS-- */
+

+
/*
+
 * VCS Ids.
+
 */
+

+
#ifndef	ELFTC_VCSID
+

+
#if defined(__DragonFly__)
+
#define	ELFTC_VCSID(ID)		__RCSID(ID)
+
#endif
+

+
#if defined(__FreeBSD__)
+
#define	ELFTC_VCSID(ID)		__FBSDID(ID)
+
#endif
+

+
#if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || defined(__APPLE__)
+
#if defined(__GNUC__)
+
#define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
+
#else
+
#define	ELFTC_VCSID(ID)		/**/
+
#endif
+
#endif
+

+
#if defined(__minix)
+
#if defined(__GNUC__)
+
#define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
+
#else
+
#define	ELFTC_VCSID(ID)		/**/
+
#endif	/* __GNU__ */
+
#endif
+

+
#if defined(__NetBSD__)
+
#define	ELFTC_VCSID(ID)		__RCSID(ID)
+
#endif
+

+
#if defined(__OpenBSD__)
+
#if defined(__GNUC__)
+
#define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
+
#else
+
#define	ELFTC_VCSID(ID)		/**/
+
#endif	/* __GNUC__ */
+
#endif
+

+
#endif	/* ELFTC_VCSID */
+

+
/*
+
 * Provide an equivalent for getprogname(3).
+
 */
+

+
#ifndef	ELFTC_GETPROGNAME
+

+
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__minix) || \
+
    defined(__NetBSD__) || defined(__APPLE__)
+

+
#include <stdlib.h>
+

+
#define	ELFTC_GETPROGNAME()	getprogname()
+

+
#endif	/* __DragonFly__ || __FreeBSD__ || __minix || __NetBSD__ */
+

+

+
#if defined(__GLIBC__)
+

+
/*
+
 * GLIBC based systems have a global 'char *' pointer referencing
+
 * the executable's name.
+
 */
+

+
/* XXX: defined in errno.h
+
extern const char *program_invocation_short_name;
+
*/
+

+
#define	ELFTC_GETPROGNAME()	program_invocation_short_name
+

+
#endif	/* __GLIBC__ */
+

+

+
#if defined(__OpenBSD__)
+

+
extern const char *__progname;
+

+
#define	ELFTC_GETPROGNAME()	__progname
+

+
#endif	/* __OpenBSD__ */
+

+
#endif	/* ELFTC_GETPROGNAME */
+

+

+
/**
+
 ** Per-OS configuration.
+
 **/
+

+
#if defined(__DragonFly__)
+

+
#include <osreldate.h>
+
#include <sys/endian.h>
+

+
#define	ELFTC_BYTE_ORDER			_BYTE_ORDER
+
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
+
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
+

+
#define	ELFTC_HAVE_MMAP				1
+

+
#endif
+

+
#if defined(__GLIBC__)
+

+
#include <endian.h>
+

+
#define	ELFTC_BYTE_ORDER			__BYTE_ORDER
+
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		__LITTLE_ENDIAN
+
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		__BIG_ENDIAN
+

+
#define	ELFTC_HAVE_MMAP				1
+

+
/*
+
 * Debian GNU/Linux and Debian GNU/kFreeBSD do not have strmode(3).
+
 */
+
#define	ELFTC_HAVE_STRMODE			0
+

+
/* Whether we need to supply {be,le}32dec. */
+
#define ELFTC_NEED_BYTEORDER_EXTENSIONS		1
+

+
#define	roundup2	roundup
+

+
#endif	/* __GLIBC__ */
+

+

+
#if defined(__FreeBSD__)
+

+
#include <osreldate.h>
+
#include <sys/endian.h>
+

+
#define	ELFTC_BYTE_ORDER			_BYTE_ORDER
+
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
+
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
+

+
#define	ELFTC_HAVE_MMAP				1
+
#define	ELFTC_HAVE_STRMODE			1
+
#if __FreeBSD_version <= 900000
+
#define	ELFTC_BROKEN_YY_NO_INPUT		1
+
#endif
+
#endif	/* __FreeBSD__ */
+

+

+
#if defined(__minix)
+
#define	ELFTC_HAVE_MMAP				0
+
#endif	/* __minix */
+

+

+
#if defined(__NetBSD__) || defined(__APPLE__)
+

+
#include <sys/param.h>
+
#ifndef __APPLE__
+
#include <sys/endian.h>
+
#else
+
#include <machine/endian.h>
+
#define	roundup2	roundup
+
#endif
+

+
#define	ELFTC_BYTE_ORDER			_BYTE_ORDER
+
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
+
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
+

+
#define	ELFTC_HAVE_MMAP				1
+
#define	ELFTC_HAVE_STRMODE			1
+
#if __NetBSD_Version__ <= 599002100
+
/* from src/doc/CHANGES: flex(1): Import flex-2.5.35 [christos 20091025] */
+
/* and 5.99.21 was from Wed Oct 21 21:28:36 2009 UTC */
+
#  define ELFTC_BROKEN_YY_NO_INPUT		1
+
#endif
+
#endif	/* __NetBSD __ */
+

+

+
#if defined(__OpenBSD__)
+

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

+
#define	ELFTC_BYTE_ORDER			_BYTE_ORDER
+
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
+
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
+

+
#define	ELFTC_HAVE_MMAP				1
+
#define	ELFTC_HAVE_STRMODE			1
+

+
#define	ELFTC_NEED_BYTEORDER_EXTENSIONS		1
+
#define	roundup2	roundup
+

+
#endif	/* __OpenBSD__ */
+

+
#endif	/* _ELFTC_H */
added external/elf/_libelf.h
@@ -0,0 +1,235 @@
+
/*-
+
 * Copyright (c) 2006,2008-2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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.
+
 *
+
 * $Id$
+
 */
+

+
#ifndef	__LIBELF_H_
+
#define	__LIBELF_H_
+

+
#include <sys/queue.h>
+

+
#include "_libelf_config.h"
+

+
#include "_elftc.h"
+

+
/*
+
 * 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;
+
	unsigned 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_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) |	\
+
	((O) << LIBELF_OS_ERROR_SHIFT))
+

+
#define	LIBELF_SET_ERROR(E, O) do {					\
+
		LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O));	\
+
	} while (0)
+

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

+
/*
+
 * Flags for library internal use.  These use the upper 16 bits of the
+
 * `e_flags' field.
+
 */
+
#define	LIBELF_F_API_MASK	0x00FFFFU  /* Flags defined by the API. */
+
#define	LIBELF_F_AR_HEADER	0x010000U  /* translated header available */
+
#define	LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */
+
#define	LIBELF_F_DATA_MALLOCED	0x040000U /* whether data was malloc'ed */
+
#define	LIBELF_F_RAWFILE_MALLOC	0x080000U /* whether e_rawfile was malloc'ed */
+
#define	LIBELF_F_RAWFILE_MMAP	0x100000U /* whether e_rawfile was mmap'ed */
+
#define	LIBELF_F_SHDRS_LOADED	0x200000U /* whether all shdrs were read in */
+
#define	LIBELF_F_SPECIAL_FILE	0x400000U /* non-regular file */
+

+
struct _Elf {
+
	int		e_activations;	/* activation count */
+
	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 */
+
	unsigned char	*e_rawfile;	/* uninterpreted bytes */
+
	size_t		e_rawsize;	/* size of uninterpreted bytes */
+
	unsigned int	e_version;	/* file version */
+

+
	/*
+
	 * Header information for archive members.  See the
+
	 * LIBELF_F_AR_HEADER flag.
+
	 */
+
	union {
+
		Elf_Arhdr	*e_arhdr;	/* translated header */
+
		unsigned char	*e_rawhdr;	/* untranslated header */
+
	} e_hdr;
+

+
	union {
+
		struct {		/* ar(1) archives */
+
			off_t	e_next;	/* set by elf_rand()/elf_next() */
+
			int	e_nchildren;
+
			unsigned char *e_rawstrtab; /* file name strings */
+
			size_t	e_rawstrtabsz;
+
			unsigned 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;
+
};
+

+
/*
+
 * The internal descriptor wrapping the "Elf_Data" type.
+
 */
+
struct _Libelf_Data {
+
	Elf_Data	d_data;		/* The exported descriptor. */
+
	Elf_Scn		*d_scn;		/* The containing section */
+
	unsigned int	d_flags;
+
	STAILQ_ENTRY(_Libelf_Data) d_next;
+
};
+

+
struct _Elf_Scn {
+
	union {
+
		Elf32_Shdr	s_shdr32;
+
		Elf64_Shdr	s_shdr64;
+
	} s_shdr;
+
	STAILQ_HEAD(, _Libelf_Data) s_data;	/* translated data */
+
	STAILQ_HEAD(, _Libelf_Data) s_rawdata;	/* raw data */
+
	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
+
};
+

+

+
/*
+
 * The LIBELF_COPY macros are used to copy fields from a GElf_*
+
 * structure to their 32-bit counterparts, while checking for out of
+
 * range values.
+
 *
+
 * - LIBELF_COPY_U32 :: copy an unsigned 32 bit field.
+
 * - LIBELF_COPY_S32 :: copy a signed 32 bit field.
+
 */
+

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

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

+

+
/*
+
 * Function Prototypes.
+
 */
+

+
__BEGIN_DECLS
+
struct _Libelf_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, int _reporterror);
+
Elf	*_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
+
Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);
+
Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);
+
long	 _libelf_checksum(Elf *_e, int _elfclass);
+
void	*_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
+
unsigned 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))
+
	    (unsigned char *_dst, size_t dsz, unsigned 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_section_headers(Elf *e, void *ehdr);
+
unsigned int _libelf_malign(Elf_Type _t, int _elfclass);
+
Elf	*_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror);
+
size_t	_libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
+
void	*_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
+
Elf	*_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);
+
struct _Libelf_Data *_libelf_release_data(struct _Libelf_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);
+
__END_DECLS
+

+
#endif	/* __LIBELF_H_ */
added external/elf/_libelf_ar.h
@@ -0,0 +1,57 @@
+
/*-
+
 * Copyright (c) 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 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.
+
 *
+
 * $Id$
+
 */
+

+
#ifndef	__LIBELF_AR_H_
+
#define	__LIBELF_AR_H_
+

+
/*
+
 * Prototypes and declarations needed by libelf's ar(1) archive
+
 * handling code.
+
 */
+

+
#include <ar.h>
+

+
#define	LIBELF_AR_BSD_EXTENDED_NAME_PREFIX	"#1/"
+
#define	LIBELF_AR_BSD_SYMTAB_NAME		"__.SYMDEF"
+
#define	LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE	\
+
	(sizeof(LIBELF_AR_BSD_EXTENDED_NAME_PREFIX) - 1)
+

+
#define	IS_EXTENDED_BSD_NAME(NAME)				\
+
	(strncmp((const char *) (NAME),				\
+
	 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX,			\
+
	 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE) == 0)
+

+

+
unsigned char *_libelf_ar_get_string(const char *_buf, size_t _sz,
+
    unsigned int _rawname, int _svr4names);
+
char	*_libelf_ar_get_raw_name(const struct ar_hdr *_arh);
+
char	*_libelf_ar_get_translated_name(const struct ar_hdr *_arh, Elf *_ar);
+
int	_libelf_ar_get_number(const char *_buf, size_t _sz,
+
    unsigned int _base, size_t *_ret);
+

+
#endif	/* __LIBELF_AR_H_ */
added external/elf/_libelf_config.h
@@ -0,0 +1,211 @@
+
/*-
+
 * Copyright (c) 2008-2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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.
+
 *
+
 * $Id$
+
 */
+

+
#if defined(__APPLE__) || defined(__DragonFly__)
+

+
#if	defined(__amd64__)
+
#define	LIBELF_ARCH		EM_X86_64
+
#define	LIBELF_BYTEORDER	ELFDATA2LSB
+
#define	LIBELF_CLASS		ELFCLASS64
+
#elif	defined(__i386__)
+
#define	LIBELF_ARCH		EM_386
+
#define	LIBELF_BYTEORDER	ELFDATA2LSB
+
#define	LIBELF_CLASS		ELFCLASS32
+
#elif   defined(__aarch64__)
+
#define LIBELF_ARCH             EM_AARCH64
+
#define LIBELF_BYTEORDER        ELFDATA2LSB
+
#define LIBELF_CLASS            ELFCLASS64
+
#endif
+

+
#endif	/* __DragonFly__ */
+

+
#ifdef __FreeBSD__
+

+
/*
+
 * Define LIBELF_{ARCH,BYTEORDER,CLASS} based on the machine architecture.
+
 * See also: <machine/elf.h>.
+
 */
+

+
#if	defined(__amd64__)
+

+
#define	LIBELF_ARCH		EM_X86_64
+
#define	LIBELF_BYTEORDER	ELFDATA2LSB
+
#define	LIBELF_CLASS		ELFCLASS64
+

+
#elif	defined(__aarch64__)
+

+
#define	LIBELF_ARCH		EM_AARCH64
+
#define	LIBELF_BYTEORDER	ELFDATA2LSB
+
#define	LIBELF_CLASS		ELFCLASS64
+

+
#elif	defined(__arm__)
+

+
#define	LIBELF_ARCH		EM_ARM
+
#if	defined(__ARMEB__)	/* Big-endian ARM. */
+
#define	LIBELF_BYTEORDER	ELFDATA2MSB
+
#else
+
#define	LIBELF_BYTEORDER	ELFDATA2LSB
+
#endif
+
#define	LIBELF_CLASS		ELFCLASS32
+

+
#elif	defined(__i386__)
+

+
#define	LIBELF_ARCH		EM_386
+
#define	LIBELF_BYTEORDER	ELFDATA2LSB
+
#define	LIBELF_CLASS		ELFCLASS32
+

+
#elif	defined(__ia64__)
+

+
#define	LIBELF_ARCH		EM_IA_64
+
#define	LIBELF_BYTEORDER	ELFDATA2LSB
+
#define	LIBELF_CLASS		ELFCLASS64
+

+
#elif	defined(__mips__)
+

+
#define	LIBELF_ARCH		EM_MIPS
+
#if	defined(__MIPSEB__)
+
#define	LIBELF_BYTEORDER	ELFDATA2MSB
+
#else
+
#define	LIBELF_BYTEORDER	ELFDATA2LSB
+
#endif
+
#define	LIBELF_CLASS		ELFCLASS32
+

+
#elif defined(__powerpc64__)
+

+
#define LIBELF_ARCH EM_PPC64
+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+
#define LIBELF_BYTEORDER ELFDATA2LSB
+
#else
+
#define LIBELF_BYTEORDER ELFDATA2MSB
+
#endif
+
#define LIBELF_CLASS ELFCLASS64
+

+
#elif	defined(__powerpc__)
+

+
#define	LIBELF_ARCH		EM_PPC
+
#define	LIBELF_BYTEORDER	ELFDATA2MSB
+
#define	LIBELF_CLASS		ELFCLASS32
+

+
#elif	defined(__sparc__)
+

+
#define	LIBELF_ARCH		EM_SPARCV9
+
#define	LIBELF_BYTEORDER	ELFDATA2MSB
+
#define	LIBELF_CLASS		ELFCLASS64
+

+
#else
+
#error	Unknown FreeBSD architecture.
+
#endif
+
#endif  /* __FreeBSD__ */
+

+
/*
+
 * Definitions for Minix3.
+
 */
+
#ifdef __minix
+

+
#define	LIBELF_ARCH		EM_386
+
#define	LIBELF_BYTEORDER	ELFDATA2LSB
+
#define	LIBELF_CLASS		ELFCLASS32
+

+
#endif	/* __minix */
+

+
#ifdef __NetBSD__
+

+
#include <machine/elf_machdep.h>
+

+
#if	!defined(ARCH_ELFSIZE)
+
#error	ARCH_ELFSIZE is not defined.
+
#endif
+

+
#if	ARCH_ELFSIZE == 32
+
#define	LIBELF_ARCH		ELF32_MACHDEP_ID
+
#define	LIBELF_BYTEORDER	ELF32_MACHDEP_ENDIANNESS
+
#define	LIBELF_CLASS		ELFCLASS32
+
#define	Elf_Note		Elf32_Nhdr
+
#else
+
#define	LIBELF_ARCH		ELF64_MACHDEP_ID
+
#define	LIBELF_BYTEORDER	ELF64_MACHDEP_ENDIANNESS
+
#define	LIBELF_CLASS		ELFCLASS64
+
#define	Elf_Note		Elf64_Nhdr
+
#endif
+

+
#endif	/* __NetBSD__ */
+

+
#if defined(__OpenBSD__)
+

+
#include <machine/exec.h>
+

+
#define	LIBELF_ARCH		ELF_TARG_MACH
+
#define	LIBELF_BYTEORDER	ELF_TARG_DATA
+
#define	LIBELF_CLASS		ELF_TARG_CLASS
+

+
#endif
+

+
/*
+
 * GNU & Linux compatibility.
+
 *
+
 * `__linux__' is defined in an environment runs the Linux kernel and glibc.
+
 * `__GNU__' is defined in an environment runs a GNU kernel (Hurd) and glibc.
+
 * `__GLIBC__' is defined for an environment that runs glibc over a non-GNU
+
 *     kernel such as GNU/kFreeBSD.
+
 */
+

+
#if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
+

+
#if defined(__linux__)
+

+
#if    defined(__amd64__)
+

+
#define LIBELF_ARCH             EM_X86_64
+
#define LIBELF_BYTEORDER        ELFDATA2LSB
+
#define LIBELF_CLASS            ELFCLASS64
+

+
#elif   defined(__aarch64__)
+

+
#define LIBELF_ARCH             EM_AARCH64
+
#define LIBELF_BYTEORDER        ELFDATA2LSB
+
#define LIBELF_CLASS            ELFCLASS64
+

+
#elif  defined(__i386__)
+

+
#define LIBELF_ARCH             EM_386
+
#define LIBELF_BYTEORDER        ELFDATA2LSB
+
#define LIBELF_CLASS            ELFCLASS32
+

+
#endif
+

+
#endif	/* defined(__linux__) */
+

+
#if	LIBELF_CLASS == ELFCLASS32
+
#define	Elf_Note		Elf32_Nhdr
+
#elif   LIBELF_CLASS == ELFCLASS64
+
#define	Elf_Note		Elf64_Nhdr
+
#else
+
#error  LIBELF_CLASS needs to be one of ELFCLASS32 or ELFCLASS64
+
#endif
+

+
#endif /* defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) */
added external/elf/elf.3
@@ -0,0 +1,612 @@
+
.\" Copyright (c) 2006-2008,2011 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd July 28, 2014
+
.Os
+
.Dt ELF 3
+
.Sh NAME
+
.Nm elf
+
.Nd API for manipulating ELF objects
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Sh DESCRIPTION
+
The
+
.Lb libelf
+
provides functions that allow an application to read and manipulate
+
ELF object files, and to read
+
.Xr ar 1
+
archives.
+
The library allows the manipulation of ELF objects in a byte ordering
+
and word-size independent way, allowing an application to read and
+
create ELF objects for 32 and 64 bit architectures and for little-
+
and big-endian machines.
+
The library is capable of processing ELF objects that use extended
+
section numbering.
+
.Pp
+
This manual page serves to provide an overview of the functionality in
+
the ELF library.
+
Further information may found in the manual pages for individual
+
.Xr ELF 3
+
functions that comprise the library.
+
.Ss ELF Concepts
+
As described in
+
.Xr elf 5 ,
+
ELF files contain several data structures that are laid out in a
+
specific way.
+
ELF files begin with an
+
.Dq Executable Header ,
+
and may contain an optional
+
.Dq Program Header Table ,
+
and optional data in the form of ELF
+
.Dq sections .
+
A
+
.Dq Section Header Table
+
describes the content of the data in these sections.
+
.Pp
+
ELF objects have an associated
+
.Dq "ELF class"
+
which denotes the natural machine word size for the architecture
+
the object is associated with.
+
Objects for 32 bit architectures have an ELF class of
+
.Dv ELFCLASS32 .
+
Objects for 64 bit architectures have an ELF class of
+
.Dv ELFCLASS64 .
+
.Pp
+
ELF objects also have an associated
+
.Dq endianness
+
which denotes the endianness of the machine architecture associated
+
with the object.
+
This may be
+
.Dv ELFDATA2LSB
+
for little-endian architectures and
+
.Dv ELFDATA2MSB
+
for big-endian architectures.
+
.Pp
+
ELF objects are also associated with an API version number.
+
This version number determines the layout of the individual components
+
of an ELF file and the semantics associated with these.
+
.Ss Data Representation And Translation
+
The
+
.Xr ELF 3
+
library distinguishes between
+
.Dq native
+
representations of ELF data structures and their
+
.Dq file
+
representations.
+
.Pp
+
An application would work with ELF data in its
+
.Dq native
+
representation, i.e., using the native byteorder and alignment mandated
+
by the processor the application is running on.
+
The
+
.Dq file
+
representation of the same data could use a different byte ordering
+
and follow different constraints on object alignment than these native
+
constraints.
+
.Pp
+
Accordingly, the
+
.Xr ELF 3
+
library offers translation facilities
+
.Xr ( elf32_xlatetof 3 ,
+
.Xr elf32_xlatetom 3 ,
+
.Xr elf64_xlatetof 3
+
and
+
.Xr elf64_xlatetom 3 )
+
to and from these
+
representations and also provides higher-level APIs that retrieve and store
+
data from the ELF object in a transparent manner.
+
.Ss Library Working Version
+
Conceptually, there are three version numbers associated with an
+
application using the ELF library to manipulate ELF objects:
+
.Bl -bullet -compact -offset indent
+
.It
+
The ELF version that the application was compiled against.
+
This version determines the ABI expected by the application.
+
.It
+
The ELF version of the ELF object being manipulated by the
+
application through the ELF library.
+
.It
+
The ELF version (or set of versions) supported by the ELF library itself.
+
.El
+
.Pp
+
In order to facilitate working with ELF objects of differing versions,
+
the ELF library requires the application to call the
+
.Fn elf_version
+
function before invoking many of its operations, in order to inform
+
the library of the application's desired working version.
+
.Pp
+
In the current implementation, all three versions have to be
+
.Dv EV_CURRENT .
+
.Ss Namespace use
+
The ELF library uses the following prefixes:
+
.Bl -tag -width "ELF_F_*"
+
.It Dv elf_
+
Used for class-independent functions.
+
.It Dv elf32_
+
Used for functions working with 32 bit ELF objects.
+
.It Dv elf64_
+
Used for functions working with 64 bit ELF objects.
+
.It Dv Elf_
+
Used for class-independent data types.
+
.It Dv ELF_C_
+
Used for command values used in a few functions.
+
These symbols are defined as members of the
+
.Vt Dv Elf_Cmd
+
enumeration.
+
.It Dv ELF_E_
+
Used for error numbers.
+
.It Dv ELF_F_
+
Used for flags.
+
.It Dv ELF_K_
+
These constants define the kind of file associated with an ELF
+
descriptor.
+
See
+
.Xr elf_kind 3 .
+
The symbols are defined by the
+
.Vt Elf_Kind
+
enumeration.
+
.It Dv ELF_T_
+
These values are defined by the
+
.Vt Elf_Type
+
enumeration, and denote the types of ELF data structures
+
that can be present in an ELF object.
+
.El
+
.Pp
+
In addition, the library uses symbols with prefixes
+
.Dv _ELF
+
and
+
.Dv _libelf
+
for its internal use.
+
.Ss Descriptors
+
Applications communicate with the library using descriptors.
+
These are:
+
.Bl -tag -width ".Vt Elf_Data"
+
.It Vt Elf
+
An
+
.Vt Elf
+
descriptor represents an ELF object or an
+
.Xr ar 1
+
archive.
+
It is allocated using one of the
+
.Fn elf_begin
+
or
+
.Fn elf_memory
+
functions.
+
An
+
.Vt Elf
+
descriptor can be used to read and write data to an ELF file.
+
An
+
.Vt Elf
+
descriptor can be associated with zero or more
+
.Vt Elf_Scn
+
section descriptors.
+
.Pp
+
Given an ELF descriptor, the application may retrieve the ELF
+
object's class-dependent
+
.Dq "Executable Header"
+
structures using the
+
.Fn elf32_getehdr
+
or
+
.Fn elf64_getehdr
+
functions.
+
A new Ehdr structure may be allocated using the
+
.Fn elf64_newehdr
+
or
+
.Fn elf64_newehdr
+
functions.
+
.Pp
+
The
+
.Dq "Program Header Table"
+
associated with an ELF descriptor may be allocated using the
+
.Fn elf32_getphdr
+
or
+
.Fn elf64_getphdr
+
functions.
+
A new program header table may be allocated or an existing table
+
resized using the
+
.Fn elf32_newphdr
+
or
+
.Fn elf64_newphdr
+
functions.
+
.Pp
+
The
+
.Vt Elf
+
structure is opaque and has no members visible to the
+
application.
+
.\" TODO describe the Elf_Arhdr and Elf_Arsym structures.
+
.It Vt Elf_Data
+
An
+
.Vt Elf_Data
+
data structure describes an individual chunk of a ELF file as
+
represented in memory.
+
It has the following application-visible members:
+
.Bl -tag -width ".Vt unsigned int d_version" -compact
+
.It Vt "uint64_t d_align"
+
The in-file alignment of the data buffer within its containing ELF section.
+
This value must be non-zero and a power of two.
+
.It Vt "void *d_buf"
+
A pointer to data in memory.
+
.It Vt "uint64_t d_off"
+
The offset within the containing section where this descriptor's data
+
would be placed.
+
This field will be computed by the library unless the application
+
requests full control of the ELF object's layout.
+
.It Vt "uint64_t d_size"
+
The number of bytes of data in this descriptor.
+
.It Vt "Elf_Type d_type"
+
The ELF type (see below) of the data in this descriptor.
+
.It Vt "unsigned int d_version"
+
The operating version for the data in this buffer.
+
.El
+
.Pp
+
.Vt Elf_Data
+
descriptors are usually associated with
+
.Vt Elf_Scn
+
descriptors.
+
Existing data descriptors associated with an ELF section may be
+
structures are retrieved using the
+
.Fn elf_getdata
+
and
+
.Fn elf_rawdata
+
functions.
+
The
+
.Fn elf_newdata
+
function may be used to attach new data descriptors to an ELF section.
+
.It Vt Elf_Scn
+
.Vt Elf_Scn
+
descriptors represent a section in an ELF object.
+
.Pp
+
They are retrieved using the
+
.Fn elf_getscn
+
function.
+
An application may iterate through the existing sections of an ELF
+
object using the
+
.Fn elf_nextscn
+
function.
+
New sections may be allocated using the
+
.Fn elf_newscn
+
function.
+
.Pp
+
The
+
.Vt Elf_Scn
+
descriptor is opaque and contains no application modifiable fields.
+
.El
+
.Ss Supported Elf Types
+
The following ELF datatypes are supported by the library.
+
.Pp
+
.Bl -tag -width ".Dv ELF_T_SYMINFO" -compact
+
.It Dv ELF_T_ADDR
+
Machine addresses.
+
.It Dv ELF_T_BYTE
+
Byte data.
+
The library will not attempt to translate byte data.
+
.It Dv ELF_T_CAP
+
Software and hardware capability records.
+
.It Dv ELF_T_DYN
+
Records used in a section of type
+
.Dv SHT_DYNAMIC .
+
.It Dv ELF_T_EHDR
+
ELF executable header.
+
.It Dv ELF_T_GNUHASH
+
GNU-style hash tables.
+
.It Dv ELF_T_HALF
+
16-bit unsigned words.
+
.It Dv ELF_T_LWORD
+
64 bit unsigned words.
+
.It Dv ELF_T_MOVE
+
ELF Move records.
+
.\".It Dv ELF_T_MOVEP
+
.\" As yet unsupported.
+
.It Dv ELF_T_NOTE
+
ELF Note structures.
+
.It Dv ELF_T_OFF
+
File offsets.
+
.It Dv ELF_T_PHDR
+
ELF program header table entries.
+
.It Dv ELF_T_REL
+
ELF relocation entries.
+
.It Dv ELF_T_RELA
+
ELF relocation entries with addends.
+
.It Dv ELF_T_SHDR
+
ELF section header entries.
+
.It Dv ELF_T_SWORD
+
Signed 32-bit words.
+
.It Dv ELF_T_SXWORD
+
Signed 64-bit words.
+
.It Dv ELF_T_SYMINFO
+
ELF symbol information.
+
.It Dv ELF_T_SYM
+
ELF symbol table entries.
+
.It Dv ELF_T_VDEF
+
Symbol version definition records.
+
.It Dv ELF_T_VNEED
+
Symbol version requirement records.
+
.It Dv ELF_T_WORD
+
Unsigned 32-bit words.
+
.It Dv ELF_T_XWORD
+
Unsigned 64-bit words.
+
.El
+
.Pp
+
The symbol
+
.Dv ELF_T_NUM
+
denotes the number of Elf types known to the library.
+
.Pp
+
The following table shows the mapping between ELF section types
+
defined in
+
.Xr elf 5
+
and the types supported by the library.
+
.Bl -column ".Dv SHT_PREINIT_ARRAY" ".Dv ELF_T_SYMINFO"
+
.It Em Section Type Ta Em "Library Type" Ta Em Description
+
.It Dv SHT_DYNAMIC Ta Dv ELF_T_DYN Ta Xo
+
.Sq .dynamic
+
section entries.
+
.Xc
+
.It Dv SHT_DYNSYM Ta Dv ELF_T_SYM Ta Symbols for dynamic linking.
+
.It Dv SHT_FINI_ARRAY Ta Dv ELF_T_ADDR Ta Termination function pointers.
+
.It Dv SHT_GNU_HASH Ta Dv ELF_T_GNUHASH Ta GNU hash sections.
+
.It Dv SHT_GNU_LIBLIST Ta Dv ELF_T_WORD Ta List of libraries to be pre-linked.
+
.It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions.
+
.It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements.
+
.It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols.
+
.It Dv SHT_GROUP Ta Dv ELF_T_WORD Ta Section group marker.
+
.It Dv SHT_HASH Ta Dv ELF_T_HASH Ta Symbol hashes.
+
.It Dv SHT_INIT_ARRAY Ta Dv ELF_T_ADDR Ta Initialization function pointers.
+
.It Dv SHT_NOBITS Ta Dv ELF_T_BYTE Ta Xo
+
Empty sections.
+
See
+
.Xr elf 5 .
+
.Xc
+
.It Dv SHT_NOTE Ta Dv ELF_T_NOTE Ta ELF note records.
+
.It Dv SHT_PREINIT_ARRAY Ta Dv ELF_T_ADDR Ta Pre-initialization function pointers.
+
.It Dv SHT_PROGBITS Ta Dv ELF_T_BYTE Ta Machine code.
+
.It Dv SHT_REL Ta Dv ELF_T_REL Ta ELF relocation records.
+
.It Dv SHT_RELA Ta Dv ELF_T_RELA Ta Relocation records with addends.
+
.It Dv SHT_STRTAB Ta Dv ELF_T_BYTE Ta String tables.
+
.It Dv SHT_SYMTAB Ta Dv ELF_T_SYM Ta Symbol tables.
+
.It Dv SHT_SYMTAB_SHNDX Ta Dv ELF_T_WORD Ta Used with extended section numbering.
+
.It Dv SHT_SUNW_dof Ta Dv ELF_T_BYTE Ta Xo
+
Used by
+
.Xr dtrace 1 .
+
.Xc
+
.It Dv SHT_SUNW_move Ta Dv ELF_T_MOVE Ta ELF move records.
+
.It Dv SHT_SUNW_syminfo Ta Dv ELF_T_SYMINFO Ta Additional symbol flags.
+
.It Dv SHT_SUNW_verdef Ta Dv ELF_T_VDEF Ta Xo
+
Same as
+
.Dv SHT_GNU_verdef .
+
.Xc
+
.It Dv SHT_SUNW_verneed Ta Dv ELF_T_VNEED Ta Xo
+
Same as
+
.Dv SHT_GNU_verneed .
+
.Xc
+
.It Dv SHT_SUNW_versym Ta Dv ELF_T_HALF Ta Xo
+
Same as
+
.Dv SHT_GNU_versym .
+
.Xc
+
.El
+
.Pp
+
Section types in the range
+
.Ns [ Dv SHT_LOOS ,
+
.Dv SHT_HIUSER ]
+
are otherwise considered to be of type
+
.Dv ELF_T_BYTE .
+
.Ss Functional Grouping
+
This section contains a brief overview of the available functionality
+
in the ELF library.
+
Each function listed here is described further in its own manual page.
+
.Bl -tag -width indent
+
.It "Archive Access"
+
.Bl -tag -compact
+
.It Fn elf_getarsym
+
Retrieve the archive symbol table.
+
.It Fn elf_getarhdr
+
Retrieve the archive header for an object.
+
.It Fn elf_getbase
+
Retrieve the offset of a member inside an archive.
+
.It Fn elf_next
+
Iterate through an
+
.Xr ar 1
+
archive.
+
.It Fn elf_rand
+
Random access inside an
+
.Xr ar 1
+
archive.
+
.El
+
.It "Data Structures"
+
.Bl -tag -compact
+
.It Fn elf_getdata
+
Retrieve translated data for an ELF section.
+
.It Fn elf_getscn
+
Retrieve the section descriptor for a named section.
+
.It Fn elf_ndxscn
+
Retrieve the index for a section.
+
.It Fn elf_newdata
+
Add a new
+
.Vt Elf_Data
+
descriptor to an ELF section.
+
.It Fn elf_newscn
+
Add a new section descriptor to an ELF descriptor.
+
.It Fn elf_nextscn
+
Iterate through the sections in an ELF object.
+
.It Fn elf_rawdata
+
Retrieve untranslated data for an ELF section.
+
.It Fn elf_rawfile
+
Return a pointer to the untranslated file contents for an ELF object.
+
.It Fn elf32_getehdr , Fn elf64_getehdr
+
Retrieve the Executable Header in an ELF object.
+
.It Fn elf32_getphdr , Fn elf64_getphdr
+
Retrieve the Program Header Table in an ELF object.
+
.It Fn elf32_getshdr , Fn elf64_getshdr
+
Retrieve the ELF section header associated with an
+
.Vt Elf_Scn
+
descriptor.
+
.It Fn elf32_newehdr , Fn elf64_newehdr
+
Allocate an Executable Header in an ELF object.
+
.It Fn elf32_newphdr , Fn elf64_newphdr
+
Allocate or resize the Program Header Table in an ELF object.
+
.El
+
.It "Data Translation"
+
.Bl -tag -compact
+
.It Fn elf32_xlatetof , Fn elf64_xlatetof
+
Translate an ELF data structure from its native representation to its
+
file representation.
+
.It Fn elf32_xlatetom , Fn elf64_xlatetom
+
Translate an ELF data structure from its file representation to a
+
native representation.
+
.El
+
.It "Error Reporting"
+
.Bl -tag -compact
+
.It Fn elf_errno
+
Retrieve the current error.
+
.It Fn elf_errmsg
+
Retrieve a human readable description of the current error.
+
.El
+
.It "Initialization"
+
.Bl -tag -compact
+
.It Fn elf_begin
+
Opens an
+
.Xr ar 1
+
archive or ELF object given a file descriptor.
+
.It Fn elf_end
+
Close an ELF descriptor and release all its resources.
+
.It Fn elf_memory
+
Opens an
+
.Xr ar 1
+
archive or ELF object present in a memory arena.
+
.It Fn elf_version
+
Sets the operating version.
+
.El
+
.It "IO Control"
+
.Bl -tag -width ".Fn elf_setshstrndx" -compact
+
.It Fn elf_cntl
+
Manage the association between and ELF descriptor and its underlying file.
+
.It Fn elf_flagdata
+
Mark an
+
.Vt Elf_Data
+
descriptor as dirty.
+
.It Fn elf_flagehdr
+
Mark the ELF Executable Header in an ELF descriptor as dirty.
+
.It Fn elf_flagphdr
+
Mark the ELF Program Header Table in an ELF descriptor as dirty.
+
.It Fn elf_flagscn
+
Mark an
+
.Vt Elf_Scn
+
descriptor as dirty.
+
.It Fn elf_flagshdr
+
Mark an ELF Section Header as dirty.
+
.It Fn elf_setshstrndx
+
Set the index of the section name string table for the ELF object.
+
.It Fn elf_update
+
Recompute ELF object layout and optionally write the modified object
+
back to the underlying file.
+
.El
+
.It "Queries"
+
.Bl -tag -width ".Fn elf_getshstrndx" -compact
+
.It Fn elf32_checksum , Fn elf64_checkum
+
Compute checksum of an ELF object.
+
.It Fn elf_getident
+
Retrieve the identification bytes for an ELF object.
+
.It Fn elf_getshnum
+
Retrieve the number of sections in an ELF object.
+
.It Fn elf_getshstrndx
+
Retrieve the section index of the section name string table in
+
an ELF object.
+
.It Fn elf_hash
+
Compute the ELF hash value of a string.
+
.It Fn elf_kind
+
Query the kind of object associated with an ELF descriptor.
+
.It Fn elf32_fsize , Fn elf64_fsize
+
Return the size of the file representation of an ELF type.
+
.El
+
.El
+
.Ss Controlling ELF Object Layout
+
In the usual mode of operation, library will compute section
+
offsets and alignments based on the contents of an ELF descriptor's
+
sections without need for further intervention by the
+
application.
+
.Pp
+
However, if the application wishes to take complete charge of the
+
layout of the ELF file, it may set the
+
.Dv ELF_F_LAYOUT
+
flag on an ELF descriptor using
+
.Xr elf_flagelf 3 ,
+
following which the library will use the data offsets and alignments
+
specified by the application when laying out the file.
+
Application control of file layout is described further in the 
+
.Xr elf_update 3
+
manual page.
+
.Pp
+
Gaps in between sections will be filled with the fill character
+
set by function
+
.Fn elf_fill .
+
.Ss Error Handling
+
In case an error is encountered, these library functions set an
+
internal error number and signal the presence of the error by
+
returning an special return value.
+
The application can check the
+
current error number by calling
+
.Xr elf_errno 3 .
+
A human readable description of the recorded error is available by
+
calling
+
.Xr elf_errmsg 3 .
+
.Ss Memory Management Rules
+
The library keeps track of all
+
.Vt Elf_Scn
+
and
+
.Vt Elf_Data
+
descriptors associated with an ELF descriptor and recovers them
+
when the descriptor is closed using
+
.Xr elf_end 3 .
+
Thus the application must not call
+
.Xr free 3
+
on data structures allocated by the ELF library.
+
.Pp
+
Conversely the library will not
+
free data that it has not allocated.
+
As an example, an application may call
+
.Xr elf_newdata 3
+
to allocate a new
+
.Vt Elf_Data
+
descriptor and can set the
+
.Va d_off
+
member of the descriptor to point to a region of memory allocated
+
using
+
.Xr malloc 3 .
+
It is the applications responsibility to free this arena, though the
+
library will reclaim the space used by the
+
.Vt Elf_Data
+
descriptor itself.
+
.Sh SEE ALSO
+
.Xr gelf 3 ,
+
.Xr elf 5
+
.Sh HISTORY
+
The original ELF(3) API was developed for Unix System V.
+
The current implementation of the ELF(3) API appeared in
+
.Fx 7.0 .
+
.Sh AUTHORS
+
The ELF library was written by
+
.An "Joseph Koshy"
+
.Aq jkoshy@FreeBSD.org .
added external/elf/elf.c
@@ -0,0 +1,46 @@
+
/*-
+
 * Copyright (c) 2006,2008,2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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 <libelf.h>
+

+
#include "_libelf.h"
+

+
#ifdef __OpenBSD__
+
 #ifndef EM_AMD64
+
  #define EM_AMD64    62
+
 #endif
+
#endif
+

+
ELFTC_VCSID("$Id$");
+

+
struct _libelf_globals _libelf = {
+
	.libelf_arch		= LIBELF_ARCH,
+
	.libelf_byteorder	= LIBELF_BYTEORDER,
+
	.libelf_class		= LIBELF_CLASS,
+
	.libelf_error		= 0,
+
	.libelf_fillchar	= 0,
+
	.libelf_version		= EV_NONE
+
};
added external/elf/elf_begin.3
@@ -0,0 +1,315 @@
+
.\" Copyright (c) 2006,2008-2011 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$ 
+
.\"
+
.Dd December 11, 2011
+
.Os
+
.Dt ELF_BEGIN 3
+
.Sh NAME
+
.Nm elf_begin
+
.Nd open an ELF file or ar(1) archive
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf *"
+
.Fn elf_begin "int fd" "Elf_Cmd cmd" "Elf *elf"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_begin
+
is used to open ELF files and
+
.Xr ar 1
+
archives for further processing by other APIs in the
+
.Xr elf 3
+
library.
+
It is also used to access individual ELF members of an
+
.Xr ar 1
+
archive in combination with the
+
.Xr elf_next 3
+
and
+
.Xr elf_rand 3
+
APIs.
+
.Pp
+
Argument
+
.Ar fd
+
is an open file descriptor returned from an
+
.Xr open 2
+
system call.
+
Function
+
.Fn elf_begin
+
uses argument
+
.Ar fd
+
for reading or writing depending on the value of argument
+
.Ar cmd .
+
Argument
+
.Ar elf
+
is primarily used for iterating through archives.
+
.Pp
+
The argument
+
.Ar cmd
+
can have the following values:
+
.Bl -tag -width "ELF_C_WRITE"
+
.It ELF_C_NULL
+
Causes
+
.Fn elf_begin
+
to return NULL.
+
Arguments
+
.Ar fd
+
and
+
.Ar elf
+
are ignored, and no additional error is signalled.
+
.It ELF_C_READ
+
This value is to be when the application wishes to examine (but not
+
modify) the contents of the file specified by the arguments
+
.Ar fd
+
and
+
.Ar elf .
+
It can be used for both
+
.Xr ar 1
+
archives and for ELF objects.
+
.Pp
+
If argument
+
.Ar elf
+
is NULL, the library will allocate a new ELF descriptor for the file
+
being processed.
+
The argument
+
.Ar fd
+
should have been opened for reading.
+
.Pp
+
If argument
+
.Ar elf
+
is not NULL, and references a regular ELF file previously opened with
+
.Fn elf_begin ,
+
then the activation count for the descriptor referenced by argument
+
.Ar elf
+
is incremented.
+
The value in argument
+
.Ar fd
+
should match that used to open the descriptor argument
+
.Ar elf .
+
.Pp
+
If argument
+
.Ar elf
+
is not NULL, and references a descriptor for an
+
.Xr ar 1
+
archive opened earlier with
+
.Fn elf_begin ,
+
a descriptor for an element in the archive is returned as
+
described in the section
+
.Sx "Processing ar(1) archives"
+
below.
+
The value for argument
+
.Ar fd
+
should match that used to open the archive earlier.
+
.Pp
+
If argument
+
.Ar elf
+
is not NULL, and references an
+
.Xr ar 1
+
archive opened earlier with
+
.Fn elf_memory ,
+
then the value of the argument
+
.Ar fd
+
is ignored.
+
.It Dv ELF_C_RDWR
+
This command is used to prepare an ELF file for reading and writing.
+
This command is not supported for
+
.Xr ar 1
+
archives.
+
.Pp
+
Argument
+
.Ar fd
+
should have been opened for reading and writing.
+
If argument
+
.Ar elf
+
is NULL, the library will allocate a new ELF descriptor for
+
the file being processed.
+
If the argument
+
.Ar elf
+
is non-null, it should point to a descriptor previously
+
allocated with
+
.Fn elf_begin
+
with the same values for arguments
+
.Ar fd
+
and
+
.Ar cmd ;
+
in this case the library will increment the activation count for descriptor
+
.Ar elf
+
and return the same descriptor.
+
.Pp
+
Changes to the in-memory image of the ELF file may be written back to
+
disk using the
+
.Xr elf_update 3
+
function.
+
.It Dv ELF_C_WRITE
+
This command is used when the application wishes to create a new ELF
+
file.
+
Argument
+
.Ar fd
+
should have been opened for writing.
+
Argument
+
.Ar elf
+
is ignored, and the previous contents of file referenced by argument
+
.Ar fd
+
are overwritten.
+
.El
+
.Ss Processing ar(1) archives
+
An
+
.Xr ar 1
+
archive may be opened in read mode (with argument
+
.Ar cmd
+
set to
+
.Dv ELF_C_READ )
+
using
+
.Fn elf_begin
+
or
+
.Fn elf_memory .
+
The returned ELF descriptor can be passed into to
+
subsequent calls to
+
.Fn elf_begin
+
to access individual members of the archive.
+
.Pp
+
Random access within an opened archive is possible using
+
the
+
.Xr elf_next 3
+
and
+
.Xr elf_rand 3
+
functions.
+
.Pp
+
The symbol table of the archive may be retrieved
+
using
+
.Xr elf_getarsym 3 .
+
.Sh RETURN VALUES
+
The function returns a pointer to a ELF descriptor if successful, or NULL
+
if an error occurred.
+
.Sh EXAMPLES
+
To iterate through the members of an
+
.Xr ar 1
+
archive, use:
+
.Bd -literal -offset indent
+
Elf_Cmd c;
+
Elf *ar_e, *elf_e;
+
\&...
+
c = ELF_C_READ;
+
if ((ar_e = elf_begin(fd, c, (Elf *) 0)) == 0) {
+
	\&... handle error in opening the archive ...
+
}
+
while ((elf_e = elf_begin(fd, c, ar_e)) != 0) {
+
	\&... process member referenced by elf_e here ...
+
	c = elf_next(elf_e);
+
	elf_end(elf_e);
+
}
+
.Ed
+
.Pp
+
To create a new ELF file, use:
+
.Bd -literal -offset indent
+
int fd;
+
Elf *e;
+
\&...
+
if ((fd = open("filename", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) {
+
	\&... handle the error from open(2) ...
+
}
+
if ((e = elf_begin(fd, ELF_C_WRITE, (Elf *) 0)) == 0) {
+
	\&... handle the error from elf_begin() ...
+
}
+
\&... create the ELF image using other elf(3) APIs ...
+
elf_update(e, ELF_C_WRITE);
+
elf_end(e);
+
.Ed
+
.Sh ERRORS
+
Function
+
.Fn elf_begin
+
can fail with the following errors:
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARCHIVE
+
The archive denoted by argument
+
.Ar elf
+
could not be parsed.
+
.It Bq Er ELF_E_ARGUMENT
+
The value in argument
+
.Ar cmd
+
was unrecognized.
+
.It Bq Er ELF_E_ARGUMENT
+
A non-null value for argument
+
.Ar elf
+
was specified when
+
.Ar cmd
+
was set to
+
.Dv ELF_C_RDWR .
+
.It Bq Er ELF_E_ARGUMENT
+
The value of argument
+
.Ar fd
+
differs from the one the ELF descriptor
+
.Ar elf
+
was created with.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar cmd
+
differs from the value specified when ELF descriptor
+
.Ar elf
+
was created.
+
.It Bq Er ELF_E_ARGUMENT
+
An
+
.Xr ar 1
+
archive was opened with with
+
.Ar cmd
+
set to
+
.Dv ELF_C_RDWR .
+
.It Bq Er ELF_E_ARGUMENT
+
The file referenced by argument
+
.Ar fd
+
was empty.
+
.It Bq Er ELF_E_ARGUMENT
+
The underlying file for argument
+
.Ar fd
+
was of an unsupported type.
+
.It Bq Er ELF_E_IO
+
The file descriptor in argument
+
.Ar fd
+
was invalid.
+
.It Bq Er ELF_E_IO
+
The file descriptor in argument
+
.Ar fd
+
could not be read or written to.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was encountered.
+
.It Bq Er ELF_E_SEQUENCE
+
Function
+
.Fn elf_begin
+
was called before a working version was established with
+
.Xr elf_version 3 .
+
.It Bq Er ELF_E_VERSION
+
The ELF object referenced by argument
+
.Ar fd
+
was of an unsupported ELF version.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_end 3 ,
+
.Xr elf_errno 3 ,
+
.Xr elf_memory 3 ,
+
.Xr elf_next 3 ,
+
.Xr elf_rand 3 ,
+
.Xr elf_update 3 ,
+
.Xr gelf 3
added external/elf/elf_begin.c
@@ -0,0 +1,91 @@
+
/*-
+
 * Copyright (c) 2006,2008-2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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 <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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:
+
		/*
+
		 * The ELF_C_WRITE command is required to ignore the
+
		 * descriptor passed in.
+
		 */
+
		a = NULL;
+
		break;
+

+
	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, 1);
+
	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/elf/elf_cntl.3
@@ -0,0 +1,111 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$ 
+
.\"
+
.Dd August 9, 2006
+
.Os
+
.Dt ELF_CNTL 3
+
.Sh NAME
+
.Nm elf_cntl
+
.Nd control an elf file descriptor
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft int
+
.Fn elf_cntl "Elf *elf" "Elf_Cmd cmd"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_cntl
+
controls the ELF library's subsequent use of the file descriptor
+
used to create ELF descriptor
+
.Ar elf .
+
.Pp
+
Argument
+
.Ar cmd
+
informs the library of the action to be taken:
+
.Bl -tag -width "ELF_C_FDDONE"
+
.It Dv ELF_C_FDDONE
+
This value instructs the ELF library not to perform any further
+
I/O on the file descriptor associated with argument
+
.Ar elf .
+
For ELF descriptors opened with mode
+
.Ar ELF_C_WRITE
+
or
+
.Ar ELF_C_RDWR
+
subsequent
+
.Fn elf_update
+
operations on the descriptor will fail.
+
.It Dv ELF_C_FDREAD
+
This value instructs the ELF library to read in all necessary
+
data associated with ELF descriptor
+
.Ar elf
+
into memory so that the underlying file descriptor can be
+
safely closed with command
+
.Dv ELF_C_FDDONE .
+
.El
+
.Pp
+
Argument
+
.Ar elf
+
must be an ELF descriptor associated with a file system object
+
(e.g., an
+
.Xr ar 1
+
archive, an ELF file, or other data file).
+
.Sh IMPLEMENTATION NOTES
+
Due to use of
+
.Xr mmap 2
+
internally, this function is a no-op for ELF objects opened in
+
.Dv ELF_C_READ
+
mode.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_cntl
+
returns 0 on success, or -1 if an error was detected.
+
.Sh ERRORS
+
.Bl -tag -width "[ELF_E_RESOURCE]"
+
.It Bq Er ELF_E_ARCHIVE
+
Argument
+
.Ar elf
+
is a descriptor for an archive member.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar elf
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
Argument
+
.Ar cmd
+
was not recognized.
+
.It Bq Er ELF_E_MODE
+
An
+
.Dv ELF_C_FDREAD
+
operation was requested on an ELF descriptor opened
+
for writing.
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_begin 3 ,
+
.Xr elf_end 3 ,
+
.Xr elf_next 3 ,
+
.Xr elf_update 3 ,
+
.Xr gelf 3
added external/elf/elf_cntl.c
@@ -0,0 +1,58 @@
+
/*-
+
 * 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 <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/elf_data.c
@@ -0,0 +1,269 @@
+
/*-
+
 * Copyright (c) 2006,2008,2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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 <assert.h>
+
#include <errno.h>
+
#include <libelf.h>
+
#include <stdlib.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	d = (struct _Libelf_Data *) ed;
+

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

+
	assert(e->e_kind == ELF_K_ELF);
+

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

+
	if (d != NULL)
+
		return (&STAILQ_NEXT(d, d_next)->d_data);
+

+
	if (e->e_rawfile == NULL) {
+
		/*
+
		 * In the ELF_C_WRITE case, there is no source that
+
		 * can provide data for the section.
+
		 */
+
		LIBELF_SET_ERROR(ARGUMENT, 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) {
+
		LIBELF_SET_ERROR(SECTION, 0);
+
		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);
+
	}
+

+
	if (sh_size / fsz > SIZE_MAX) {
+
		LIBELF_SET_ERROR(RANGE, 0);
+
		return (NULL);
+
	}
+

+
	count = (size_t) (sh_size / fsz);
+

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

+
	if (count > 0 && msz > SIZE_MAX / count) {
+
		LIBELF_SET_ERROR(RANGE, 0);
+
		return (NULL);
+
	}
+

+
	assert(msz > 0);
+
	assert(count <= SIZE_MAX);
+
	assert(msz * count <= SIZE_MAX);
+

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

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

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

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

+
	d->d_flags  |= LIBELF_F_DATA_MALLOCED;
+

+
	xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
+
	if (!(*xlate)(d->d_data.d_buf, (size_t) d->d_data.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->d_data);
+
}
+

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

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

+
	assert(e->e_kind == ELF_K_ELF);
+

+
	/*
+
	 * 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_data.d_align = 1;
+
	d->d_data.d_buf = NULL;
+
	d->d_data.d_off = (uint64_t) ~0;
+
	d->d_data.d_size = 0;
+
	d->d_data.d_type = ELF_T_BYTE;
+
	d->d_data.d_version = LIBELF_PRIVATE(version);
+

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

+
	return (&d->d_data);
+
}
+

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

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

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

+
	assert(e->e_kind == ELF_K_ELF);
+

+
	d = (struct _Libelf_Data *) ed;
+

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

+
	if (d != NULL)
+
		return (&STAILQ_NEXT(d, d_next)->d_data);
+

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

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

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

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

+
	return (&d->d_data);
+
}
added external/elf/elf_end.3
@@ -0,0 +1,76 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd June 29, 2006
+
.Os
+
.Dt ELF_END 3
+
.Sh NAME
+
.Nm elf_end
+
.Nd release an ELF descriptor
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft int
+
.Fn elf_end "Elf *elf"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_end
+
is used to release the resources associated with an ELF descriptor
+
pointed to by argument
+
.Ar elf .
+
This descriptor must have been allocated by a previous call to
+
.Xr elf_begin 3
+
or
+
.Xr elf_memory 3 .
+
For programming convenience, a NULL value is permitted for argument
+
.Ar elf .
+
.Pp
+
A call to
+
.Fn elf_end
+
decrements the activation count for descriptor
+
.Ar elf
+
by one.
+
The resources associated with the descriptor are only released
+
with its activation count goes to zero.
+
.Pp
+
Once function
+
.Fn elf_end
+
returns zero, the ELF descriptor
+
.Ar elf
+
will no longer be valid and should not be used further.
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_end
+
returns the current value of the ELF descriptor
+
.Ar elf Ap s
+
activation count, or zero if argument
+
.Ar elf
+
was NULL.
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_begin 3 ,
+
.Xr elf_memory 3 ,
+
.Xr gelf 3
added external/elf/elf_end.c
@@ -0,0 +1,97 @@
+
/*-
+
 * Copyright (c) 2006,2008-2009,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 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>
+

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

+
#include "_libelf.h"
+

+
#if	ELFTC_HAVE_MMAP
+
#include <sys/mman.h>
+
#endif
+

+
ELFTC_VCSID("$Id$");
+

+
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_rawfile) {
+
			if (e->e_flags & LIBELF_F_RAWFILE_MALLOC)
+
				free(e->e_rawfile);
+
#if	ELFTC_HAVE_MMAP
+
			else if (e->e_flags & LIBELF_F_RAWFILE_MMAP)
+
				(void) munmap(e->e_rawfile, e->e_rawsize);
+
#endif
+
		}
+

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

+
	return (0);
+
}
added external/elf/elf_errmsg.3
@@ -0,0 +1,107 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd June 11, 2006
+
.Os
+
.Dt ELF_ERRMSG 3
+
.Sh NAME
+
.Nm elf_errmsg ,
+
.Nm elf_errno
+
.Nd ELF library error message handling
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft int
+
.Fn elf_errno "void"
+
.Ft "const char *"
+
.Fn elf_errmsg "int error"
+
.Sh DESCRIPTION
+
When an error occurs during an ELF library API call, the library
+
encodes the error using an error number and stores the error number
+
internally for retrieval by the application at a later point of time.
+
Error numbers may contain an OS supplied error code in addition to
+
an ELF API specific error code.
+
An error number value of zero indicates no error.
+
.Pp
+
Function
+
.Fn elf_errno
+
is used to retrieve the last error recorded by the ELF library.
+
Invoking this function has the side-effect of resetting the
+
ELF library's recorded error number to zero.
+
.Pp
+
The function
+
.Fn elf_errmsg
+
returns a null-terminated string with a human readable
+
description of the error specified in argument
+
.Ar error .
+
A zero value for argument
+
.Ar error
+
retrieves the most recent error encountered by the ELF
+
library.
+
An argument value of -1 behaves identically, except that
+
it guarantees a non-NULL return from
+
.Fn elf_errmsg .
+
.Sh RETURN VALUES
+
Function
+
.Fn elf_errno
+
returns a non-zero value encoding the last error encountered
+
by the ELF library, or zero if no error was encountered.
+
.Pp
+
Function
+
.Fn elf_errmsg
+
returns a pointer to library local storage for non-zero values
+
of argument
+
.Ar error .
+
With a zero argument, the function will return a NULL pointer if no
+
error had been encountered by the library, or will return a pointer to
+
library local storage containing an appropriate message otherwise.
+
.Sh EXAMPLES
+
Clearing the ELF library's recorded error number can be accomplished
+
by invoking
+
.Fn elf_errno
+
and discarding its return value.
+
.Bd -literal -offset indent
+
/* clear error */
+
(void) elf_errno();
+
.Ed
+
.Pp
+
Retrieving a human-readable description of the current error number
+
can be done with the following snippet:
+
.Bd -literal -offset indent
+
int err;
+
const char *errmsg;
+
\&...
+
err = elf_errno();
+
if (err != 0)
+
	errmsg = elf_errmsg(err);
+
.Ed
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr gelf 3
+
.Sh BUGS
+
Function
+
.Fn elf_errmsg
+
is not localized.
added external/elf/elf_errmsg.c
@@ -0,0 +1,85 @@
+
/*-
+
 * Copyright (c) 2006,2008,2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * 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 == ELF_E_NONE &&
+
	    (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 < ELF_E_NONE || error >= ELF_E_NUM)
+
		return _libelf_errors[ELF_E_NUM];
+
	if (oserr) {
+
		(void) snprintf((char *) LIBELF_PRIVATE(msg),
+
		    sizeof(LIBELF_PRIVATE(msg)), "%s: %s",
+
		    _libelf_errors[error], strerror(oserr));
+
		return (const char *)&LIBELF_PRIVATE(msg);
+
	}
+
	return _libelf_errors[error];
+
}
added external/elf/elf_errno.c
@@ -0,0 +1,43 @@
+
/*-
+
 * Copyright (c) 2006,2008,2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	old = LIBELF_PRIVATE(error);
+
	LIBELF_PRIVATE(error) = 0;
+
	return (old & LIBELF_ELF_ERROR_MASK);
+
}
added external/elf/elf_fill.3
@@ -0,0 +1,52 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd June 11, 2006
+
.Os
+
.Dt ELF_FILL 3
+
.Sh NAME
+
.Nm elf_fill
+
.Nd set fill byte for inter-section padding
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft void
+
.Fn elf_fill "int fill"
+
.Sh DESCRIPTION
+
Function
+
.Fn elf_fill
+
allows an application to specify a fill value for the padding inserted
+
between two sections of an ELF file to meet section alignment
+
constraints.
+
By default the ELF library uses zero bytes for padding.
+
.Pp
+
The ELF library will only pad bytes if the
+
.Dv ELF_F_LAYOUT
+
flag is not set for the ELF file.
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_flagelf 3 ,
+
.Xr gelf 3
added external/elf/elf_fill.c
@@ -0,0 +1,39 @@
+
/*-
+
 * 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
void
+
elf_fill(int fill)
+
{
+
	LIBELF_PRIVATE(fillchar) = fill;
+
}
added external/elf/elf_flag.c
@@ -0,0 +1,198 @@
+
/*-
+
 * Copyright (c) 2006,2008-2009,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 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
unsigned int
+
elf_flagarhdr(Elf_Arhdr *a, Elf_Cmd c, unsigned int flags)
+
{
+
	unsigned int r;
+

+
	if (a == 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 = a->ar_flags |= flags;
+
	else
+
		r = a->ar_flags &= ~flags;
+

+
	return (r & LIBELF_F_API_MASK);
+
}
+

+
unsigned int
+
elf_flagdata(Elf_Data *d, Elf_Cmd c, unsigned int flags)
+
{
+
	unsigned int r;
+
	struct _Libelf_Data *ld;
+

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

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

+
	ld = (struct _Libelf_Data *) d;
+

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

+
	return (r & LIBELF_F_API_MASK);
+
}
+

+
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)
+
{
+
	unsigned 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_ARCHIVE | ELF_F_ARCHIVE_SYSV |
+
	    ELF_F_DIRTY | ELF_F_LAYOUT)) != 0) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if ((flags & ELF_F_ARCHIVE_SYSV) && (flags & ELF_F_ARCHIVE) == 0) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if ((flags & ELF_F_ARCHIVE) && e->e_cmd != ELF_C_WRITE) {
+
		LIBELF_SET_ERROR(MODE, 0);
+
		return (0);
+
	}
+

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

+
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)
+
{
+
	unsigned 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 & LIBELF_F_API_MASK);
+
}
+

+
unsigned int
+
elf_flagshdr(Elf_Scn *s, Elf_Cmd c, unsigned int flags)
+
{
+
	return (elf_flagscn(s, c, flags));
+
}
added external/elf/elf_flagdata.3
@@ -0,0 +1,226 @@
+
.\" Copyright (c) 2006-2008,2011 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd December 3, 2011
+
.Os
+
.Dt ELF_FLAGDATA 3
+
.Sh NAME
+
.Nm elf_flagarhdr ,
+
.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_flagarhdr "Elf_Arhdr *arhdr" "Elf_Cmd cmd" "unsigned int flags"
+
.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 arhdr ,
+
.Ar data ,
+
.Ar elf
+
and
+
.Ar scn
+
denote the data structures whose flags need to be changed.
+
These values should have been returned by prior calls to
+
functions in the ELF(3) API set:
+
.Bl -bullet -compact
+
.It
+
Argument
+
.Ar arhdr
+
should have been returned by a prior call to
+
.Xr elf_getarhdr 3 .
+
.It
+
Argument
+
.Ar data
+
should have been returned by a prior call to one of
+
.Xr elf_newdata 3 ,
+
.Xr elf_getdata 3
+
or
+
.Xr elf_rawdata 3 .
+
.It
+
Argument
+
.Ar elf
+
should have been allocated by a prior call to one of
+
.Xr elf_begin 3
+
or
+
.Xr elf_memory 3 .
+
.It
+
Argument
+
.Ar scn
+
should have been returned by a prior call to one of
+
.Xr elf_getscn 3 ,
+
.Xr elf_newscn 3
+
or
+
.Xr elf_nextscn 3 .
+
.El
+
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_ARCHIVE_SYSV
+
.It Dv ELF_F_ARCHIVE
+
This flag is only valid with the
+
.Fn elf_flagelf
+
API.
+
It informs the library that the application desires to create an
+
.Xr ar 1
+
archive.
+
Argument
+
.Ar elf
+
should have been opened for writing using the
+
.Dv ELF_C_WRITE
+
command to function
+
.Fn elf_begin .
+
.It Dv ELF_F_ARCHIVE_SYSV
+
This flag is used in conjunction with the
+
.Dv ELF_F_ARCHIVE
+
flag to indicate that library should create archives that conform
+
to System V layout rules.
+
The default is to create BSD style archives.
+
.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 if successful, or zero if
+
an error is detected.
+
.Sh COMPATIBILITY
+
The
+
.Fn elf_flagarhdr
+
function and the
+
.Dv ELF_F_ARCHIVE
+
and
+
.Dv ELF_F_ARCHIVE_SYSV
+
flags are an extension to the ELF(3) API.
+
.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_MODE
+
The
+
.Dv ELF_F_ARCHIVE
+
flag was used with an ELF descriptor that had not been opened for writing.
+
.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/elf/elf_getarhdr.3
@@ -0,0 +1,97 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 15, 2006
+
.Os
+
.Dt ELF_GETARHDR 3
+
.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/elf/elf_getarhdr.c
@@ -0,0 +1,47 @@
+
/*-
+
 * 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 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
Elf_Arhdr *
+
elf_getarhdr(Elf *e)
+
{
+
	if (e == NULL) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (e->e_flags & LIBELF_F_AR_HEADER)
+
		return (e->e_hdr.e_arhdr);
+

+
	return (_libelf_ar_gethdr(e));
+
}
added external/elf/elf_getarsym.3
@@ -0,0 +1,130 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 15, 2006
+
.Os
+
.Dt ELF_GETARSYM 3
+
.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/elf/elf_getarsym.c
@@ -0,0 +1,58 @@
+
/*-
+
 * 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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 = (ar->e_flags & LIBELF_F_AR_VARIANT_SVR4) ?
+
		    _libelf_ar_process_svr4_symtab(ar, &n) :
+
		    _libelf_ar_process_bsd_symtab(ar, &n);
+
	else
+
		LIBELF_SET_ERROR(ARCHIVE, 0);
+

+
	if (ptr)
+
		*ptr = n;
+
	return (symtab);
+
}
added external/elf/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.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd June 6, 2010
+
.Os
+
.Dt ELF_GETBASE 3
+
.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/elf/elf_getbase.c
@@ -0,0 +1,48 @@
+
/*-
+
 * 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/elf_getdata.3
@@ -0,0 +1,229 @@
+
.\" 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.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd January 26, 2011
+
.Os
+
.Dt ELF_GETDATA 3
+
.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
+
Either of the arguments
+
.Ar scn
+
or
+
.Ar data
+
was NULL.
+
.It Bq Er ELF_E_ARGUMENT
+
The data descriptor referenced by argument
+
.Ar data
+
is not associated with section descriptor
+
.Ar scn .
+
.It Bq Er ELF_E_ARGUMENT
+
The section denoted by argument
+
.Ar scn
+
had no data associated with it.
+
.It Bq Er ELF_E_DATA
+
Retrieval of data from the underlying object failed.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was detected.
+
.It Bq Er ELF_E_SECTION
+
Section
+
.Ar scn
+
had type
+
.Dv SHT_NULL .
+
.It Bq Er ELF_E_SECTION
+
The type of the section
+
.Ar scn
+
was not recognized by the library.
+
.It Bq Er ELF_E_SECTION
+
The size of the section
+
.Ar scn
+
is not a multiple of the file size for its section type.
+
.It Bq Er ELF_E_SECTION
+
The file offset for section
+
.Ar scn
+
is incorrect.
+
.It Bq Er ELF_E_UNIMPL
+
The section type associated with section
+
.Ar scn
+
is currently unsupported by the library.
+
.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/elf/elf_getident.3
@@ -0,0 +1,83 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd July 3, 2006
+
.Os
+
.Dt ELF_GETIDENT 3
+
.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/elf/elf_getident.c
@@ -0,0 +1,68 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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 ((char *) e->e_rawfile);
+

+
 error:
+
	if (sz)
+
		*sz = 0;
+
	return (NULL);
+
}
added external/elf/elf_getphdrnum.3
@@ -0,0 +1,86 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 5, 2009
+
.Os
+
.Dt ELF_GETPHDRNUM 3
+
.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.
+
.Pp
+
.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/elf/elf_getphnum.3
@@ -0,0 +1,93 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 5, 2009
+
.Os
+
.Dt ELF_GETPHNUM 3
+
.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.
+
.Pp
+
.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/elf/elf_getscn.3
@@ -0,0 +1,151 @@
+
.\" Copyright (c) 2006-2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd October 22, 2007
+
.Os
+
.Dt ELF_GETSCN 3
+
.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/elf/elf_getshdrnum.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.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 4, 2009
+
.Os
+
.Dt ELF_GETSHDRNUM 3
+
.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.
+
.Pp
+
.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/elf/elf_getshdrstrndx.3
@@ -0,0 +1,79 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 5, 2009
+
.Os
+
.Dt ELF_GETSHDRSTRNDX 3
+
.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.
+
.Pp
+
.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/elf/elf_getshnum.3
@@ -0,0 +1,84 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 5, 2009
+
.Os
+
.Dt ELF_GETSHNUM 3
+
.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.
+
.Pp
+
.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/elf/elf_getshstrndx.3
@@ -0,0 +1,94 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 5, 2009
+
.Os
+
.Dt ELF_GETSHSTRNDX 3
+
.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.
+
.Pp
+
.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/elf/elf_hash.3
@@ -0,0 +1,57 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 15, 2006
+
.Os
+
.Dt ELF_HASH 3
+
.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/elf/elf_hash.c
@@ -0,0 +1,56 @@
+
/*-
+
 * 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * This elf_hash function is defined by the System V ABI.
+
 */
+

+
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/elf/elf_kind.3
@@ -0,0 +1,71 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd June 1, 2006
+
.Os
+
.Dt ELF_KIND 3
+
.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/elf/elf_kind.c
@@ -0,0 +1,44 @@
+
/*-
+
 * 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/elf_memory.3
@@ -0,0 +1,122 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd June 28, 2006
+
.Os
+
.Dt ELF_MEMORY 3
+
.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 ELF object referenced by argument
+
.Ar image
+
was of an unsupported ELF 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/elf/elf_memory.c
@@ -0,0 +1,47 @@
+
/*-
+
 * 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 <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
Elf *
+
elf_memory(char *image, size_t sz)
+
{
+
	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);
+
	}
+

+
	return (_libelf_memory((unsigned char *) image, sz, 1));
+
}
added external/elf/elf_next.3
@@ -0,0 +1,96 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd June 17, 2006
+
.Os
+
.Dt ELF_NEXT 3
+
.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/elf/elf_next.c
@@ -0,0 +1,66 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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(e->e_rawfile > parent->e_rawfile);
+

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

+
	/*
+
	 * Setup the 'e_next' field of the archive descriptor for the
+
	 * next call to 'elf_begin()'.
+
	 */
+
	parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ?
+
	    (off_t) 0 : next;
+

+
	return (ELF_C_READ);
+
}
added external/elf/elf_open.3
@@ -0,0 +1,121 @@
+
.\" Copyright (c) 2012 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$ 
+
.\"
+
.Dd May 31, 2012
+
.Os
+
.Dt ELF_OPEN 3 
+
.Sh NAME
+
.Nm elf_open
+
.Nd open ELF objects and ar(1) archives
+
.Sh LIBRARY
+
.Lb libelf
+
.Sh SYNOPSIS
+
.In libelf.h
+
.Ft "Elf *"
+
.Fn elf_open "int fd"
+
.Ft "Elf *"
+
.Fn elf_openmemory "char *image" "size_t sz"
+
.Sh DESCRIPTION
+
.Em Important :
+
The functions
+
.Fn elf_open
+
and
+
.Fn elf_openmemory
+
are extensions to the ELF(3) API, for the internal use of the
+
Elftoolchain project.
+
Portable applications should not use these functions.
+
.Pp
+
The function
+
.Fn elf_open
+
returns an Elf descriptor opened with mode
+
.Dv ELF_C_READ
+
for the ELF object or
+
.Xr ar 1
+
archive referenced by the file descriptor in argument
+
.Ar fd .
+
.Pp
+
The function
+
.Fn elf_openmemory
+
returns an ELF descriptor opened with mode
+
.Dv ELF_C_READ
+
for the ELF object or
+
.Xr ar 1
+
archive contained in the memory area pointed to by the argument
+
.Ar image .
+
The argument
+
.Ar sz
+
specifies the size of the memory area in bytes.
+
.Sh COMPATIBILITY
+
These functions are non-standard extensions to the ELF(3) API set.
+
.Pp
+
The behavior of these functions differs from their counterparts
+
.Xr elf_begin 3
+
and
+
.Xr elf_memory 3
+
in that these functions will successfully open malformed ELF objects
+
and
+
.Xr ar 1
+
archives, returning an Elf descriptor of type
+
.Dv ELF_K_NONE .
+
.Sh RETURN VALUES
+
The function returns a pointer to a ELF descriptor if successful, or
+
NULL if an error occurred.
+
.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 fd
+
was of an unsupported file type.
+
.It Bq Er ELF_E_ARGUMENT
+
The argument
+
.Ar sz
+
was zero, or the argument
+
.Ar image
+
was NULL.
+
.It Bq Er ELF_E_IO
+
The file descriptor in argument
+
.Ar fd
+
was invalid.
+
.It Bq Er ELF_E_IO
+
The file descriptor in argument
+
.Ar fd
+
could not be read.
+
.It Bq Er ELF_E_RESOURCE
+
An out of memory condition was encountered.
+
.It Bq Er ELF_E_SEQUENCE
+
Functions
+
.Fn elf_open
+
or
+
.Fn elf_openmemory
+
was called before a working version was established with
+
.Xr elf_version 3 .
+
.El
+
.Sh SEE ALSO
+
.Xr elf 3 ,
+
.Xr elf_begin 3 ,
+
.Xr elf_errno 3 ,
+
.Xr elf_memory 3 ,
+
.Xr gelf 3
added external/elf/elf_open.c
@@ -0,0 +1,67 @@
+
/*-
+
 * Copyright (c) 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 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 <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * Extension API: open a file for reading, ignoring parse errors.
+
 */
+

+
Elf *
+
elf_open(int fd)
+
{
+
	if (LIBELF_PRIVATE(version) == EV_NONE) {
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		return (NULL);
+
	}
+

+
	return (_libelf_open_object(fd, ELF_C_READ, 0));
+
}
+

+
/*
+
 * Extension API: create an ELF descriptor for an in-memory object,
+
 * ignoring parse errors.
+
 */
+

+
Elf *
+
elf_openmemory(char *image, size_t sz)
+
{
+
	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);
+
	}
+

+
	return (_libelf_memory((unsigned char *) image, sz, 0));
+
}
added external/elf/elf_phnum.c
@@ -0,0 +1,67 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/elf_rand.3
@@ -0,0 +1,118 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd June 17, 2006
+
.Os
+
.Dt ELF_RAND 3
+
.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/elf/elf_rand.c
@@ -0,0 +1,59 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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 ||
+
	    (size_t) 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/elf/elf_rawfile.3
@@ -0,0 +1,76 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd July 3, 2006
+
.Os
+
.Dt ELF_RAWFILE 3
+
.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/elf/elf_rawfile.c
@@ -0,0 +1,53 @@
+
/*-
+
 * 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	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 ((char *) ptr);
+
}
added external/elf/elf_scn.c
@@ -0,0 +1,235 @@
+
/*-
+
 * 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 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>
+
#include <sys/queue.h>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * Load an ELF section table and create a list of Elf_Scn structures.
+
 */
+
int
+
_libelf_load_section_headers(Elf *e, void *ehdr)
+
{
+
	Elf_Scn *scn;
+
	uint64_t shoff;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+
	int ec, swapbytes;
+
	unsigned char *src;
+
	size_t fsz, i, shnum;
+
	int (*xlator)(unsigned char *_d, size_t _dsz, unsigned 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 (shoff > e->e_rawsize ||			\
+
		    fsz != (EH)->e_shentsize ||			\
+
		    shnum > SIZE_MAX / fsz ||			\
+
		    fsz * shnum > e->e_rawsize - shoff) {	\
+
			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)((unsigned 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_section_headers(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_section_headers(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/elf/elf_shnum.c
@@ -0,0 +1,67 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/elf_shstrndx.c
@@ -0,0 +1,82 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/elf_strptr.3
@@ -0,0 +1,116 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd December 16, 2006
+
.Os
+
.Dt ELF_STRPTR 3
+
.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/elf/elf_strptr.c
@@ -0,0 +1,136 @@
+
/*-
+
 * 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/param.h>
+

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

+
#include "_libelf.h"
+

+
#ifndef roundup2
+
#define	roundup2(x, y)	(((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
+
#endif
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * 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;
+
	GElf_Shdr shdr;
+
	uint64_t alignment, count;
+

+
	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 = (uint64_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/elf/elf_types.m4
@@ -0,0 +1,309 @@
+
/*-
+
 * 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.
+
 *
+
 * $Id$
+
 */
+

+
/*
+
 * ELF types, defined in the "enum Elf_Type" API.
+
 *
+
 * The members of the list form a 2-tuple: (name, C-type-suffix).
+
 * + `name' is an Elf_Type symbol without the `ELF_T_' prefix.
+
 * + `C-type-suffix' is the suffix for Elf32_ and Elf64_ type names.
+
 */
+

+
define(`ELF_TYPE_LIST',
+
	``ADDR,		Addr',
+
	`BYTE,		Byte',
+
	`CAP,		Cap',
+
	`DYN,		Dyn',
+
	`EHDR,		Ehdr',
+
	`GNUHASH,	-',
+
	`HALF,		Half',
+
	`LWORD,		Lword',
+
	`MOVE,		Move',
+
	`MOVEP,		MoveP',
+
	`NOTE,		Note',
+
	`OFF,		Off',
+
	`PHDR,		Phdr',
+
	`REL,		Rel',
+
	`RELA,		Rela',
+
	`SHDR,		Shdr',
+
	`SWORD,		Sword',
+
	`SXWORD,	Sxword',
+
	`SYMINFO,	Syminfo',
+
	`SYM,		Sym',
+
	`VDEF,		Verdef',
+
	`VNEED,		Verneed',
+
	`WORD,		Word',
+
	`XWORD,		Xword',
+
	`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/elf/elf_update.3
@@ -0,0 +1,378 @@
+
.\" Copyright (c) 2006-2011 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 14, 2011
+
.Os
+
.Dt ELF_UPDATE 3
+
.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
+
should reference a valid ELF descriptor.
+
.Pp
+
Argument
+
.Ar cmd
+
can be one of the following values:
+
.Bl -tag -width "Dv 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 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.
+
The ELF descriptor referenced by argument
+
.Ar elf
+
should permit the underlying ELF object to be written or updated
+
(see
+
.Xr elf_begin 3 ) .
+
.El
+
.Pp
+
All pointers to
+
.Vt Elf_Scn
+
and
+
.Vt Elf_Data
+
descriptors associated with descriptor
+
.Ar elf
+
should be considered invalid after a call to
+
.Fn elf_update .
+
.Ss Specifying Object Layout
+
The
+
.Lb libelf
+
supports two layout modes.
+
.Bl -tag -width indent
+
.It "Library Layout"
+
If the
+
.Dv ELF_F_LAYOUT
+
flag is not set on the ELF descriptor, the ELF library will lay out
+
the ELF object according to the following scheme:
+
.Bl -tag -compact -width "Section Data"
+
.It Em EHDR
+
The ELF executable header will be placed at the start of the object.
+
.It Em PHDR
+
If the ELF descriptor contains a program header table, it will be
+
placed after the Executable Header.
+
.It Em Section Data
+
ELF section data, if any, will be placed next, keeping each section's
+
alignment requirements in mind.
+
.It Em SHDR
+
The ELF section header table, if any, will be placed last.
+
.El
+
.It "Application Controlled Layout"
+
The application can take full control of the layout of the ELF object
+
by setting the
+
.Dv ELF_F_LAYOUT
+
flag on the ELF descriptor (see
+
.Xr elf_flagelf 3 ) .
+
In this case the library will lay out the ELF object using
+
application-supplied information as below:
+
.Pp
+
.Bl -tag -compact -width "Section Data"
+
.It Em EHDR
+
The ELF executable header will be placed at the start of the object.
+
.It Em PHDR
+
The ELF program header table, if any, it will be placed at the offset
+
specified in the
+
.Va e_phoff
+
field of the ELF executable header.
+
.It Em Section Data
+
The data for each ELF section will be placed at the offset specified
+
by the
+
.Va sh_offset
+
field of the section's header.
+
The size of the section will be taken from the
+
.Va sh_size
+
field of the section header.
+
.It Em SHDR
+
The ELF section header table, if any, will be placed at the offset
+
specified by the
+
.Va e_shoff
+
field of the executable header.
+
.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 .
+
.Ss Application Supplied Information
+
The application needs to set the following fields in the data
+
structures associated with the ELF descriptor prior to calling
+
.Fn elf_update .
+
.Bl -tag -width indent
+
.It "Executable Header"
+
The fields of the ELF executable header that need to be set by the
+
application are:
+
.Pp
+
.Bl -tag -width "e_ident[EI_OSABI]" -compact
+
.It Va e_entry
+
To be set to the desired entry address for executables.
+
.It Va e_flags
+
To be 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]"
+
To be set to the OS ABI desired.
+
For example, for
+
.Fx
+
executables, this field should be set to
+
.Dv ELFOSABI_FREEBSD .
+
.It Va e_machine
+
To be set to the desired machine architecture, one of the
+
.Dv EM_*
+
values in the header file
+
.In elfdefinitions.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
+
To be set to the index of the string table containing
+
section names.
+
.It Va e_type
+
To be set to the type of the ELF object, one of the
+
.Dv ET_*
+
values in the header file
+
.In elfdefinitions.h .
+
.It Va e_version
+
To be set to the desired version of the ELF object.
+
.El
+
.It "Program Header"
+
All fields of the entries in the program header table need to be
+
set by the application.
+
.It "Section Header"
+
The fields of ELF section headers that need to be set by the
+
application are:
+
.Pp
+
.Bl -tag -width "sh_addralign" -compact
+
.It Va sh_addr
+
To be set to the 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 and must be at least as large as the
+
largest alignment needed by any
+
.Vt Elf_Data
+
descriptor associated with the section.
+
.It Va sh_entsize
+
To be 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 are known to the library.
+
.It Va sh_flags
+
To be set to the desired section flags.
+
.It Va sh_info
+
To be set as described in
+
.Xr elf 5 .
+
.It Va sh_link
+
To be set as described in
+
.Xr elf 5 .
+
.It Va sh_name
+
To be 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
+
To be set to the type of the section.
+
.El
+
.It "Section Data"
+
The
+
.Vt Elf_Data
+
descriptors associated with each section specify its contents
+
(see
+
.Xr elf_getdata 3 ) .
+
While all the fields in these descriptors are under application
+
control, the following fields influence object layout:
+
.Bl -tag -width "Va d_align" -compact
+
.It Va d_align
+
To be set to the desired alignment, within the containing section, of
+
the descriptor's data.
+
.It Va d_off
+
If the application is managing object layout, it must set this field
+
to the file offset, within the section, at which the descriptor's data
+
should be placed.
+
.It Va d_size
+
To be set to the size in bytes of the memory representation of the
+
descriptor's data.
+
.El
+
.El
+
.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 an unsupported type.
+
.It Bq Er ELF_E_DATA
+
An
+
.Vt Elf_Data
+
descriptor specified an alignment that was zero or was not a power of
+
two.
+
.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 executable header overlapped
+
with the program header table.
+
.It Bq Er ELF_E_LAYOUT
+
The flag
+
.Dv ELF_F_LAYOUT
+
was set on the Elf descriptor and the program header table was placed
+
at a misaligned file offset.
+
.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 mapped by a section descriptor.
+
.It Bq Er ELF_E_LAYOUT
+
The
+
.Dv ELF_F_LAYOUT
+
flag was set on the Elf descriptor, and the
+
.Va d_offset
+
field in an
+
.Vt Elf_Data
+
descriptor contained a value that was not a multiple of the
+
descriptor's specified alignment.
+
.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_begin 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/elf/elf_update.c
@@ -0,0 +1,1217 @@
+
/*-
+
 * Copyright (c) 2006-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 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/param.h>
+
#include <sys/stat.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"
+

+
#if	ELFTC_HAVE_MMAP
+
#include <sys/mman.h>
+
#endif
+

+
#ifndef roundup2
+
#define	roundup2(x, y)	(((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
+
#endif
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * Layout strategy:
+
 *
+
 * - Case 1: ELF_F_LAYOUT is asserted
+
 *     In this case the application has full control over where the
+
 *     section header table, program header table, and section data
+
 *     will reside.   The library only perform error checks.
+
 *
+
 * - Case 2: ELF_F_LAYOUT is not asserted
+
 *
+
 *     The library will do the object layout using the following
+
 *     ordering:
+
 *     - The executable header is placed first, are required by the
+
 *     	 ELF specification.
+
 *     - The program header table is placed immediately following the
+
 *       executable header.
+
 *     - Section data, if any, is placed after the program header
+
 *       table, aligned appropriately.
+
 *     - The section header table, if needed, is placed last.
+
 *
+
 *     There are two sub-cases to be taken care of:
+
 *
+
 *     - Case 2a: e->e_cmd == ELF_C_READ or ELF_C_RDWR
+
 *
+
 *       In this sub-case, the underlying ELF object may already have
+
 *       content in it, which the application may have modified.  The
+
 *       library will retrieve content from the existing object as
+
 *       needed.
+
 *
+
 *     - Case 2b: e->e_cmd == ELF_C_WRITE
+
 *
+
 *       The ELF object is being created afresh in this sub-case;
+
 *       there is no pre-existing content in the underlying ELF
+
 *       object.
+
 */
+

+
/*
+
 * The types of extents in an ELF object.
+
 */
+
enum elf_extent {
+
	ELF_EXTENT_EHDR,
+
	ELF_EXTENT_PHDR,
+
	ELF_EXTENT_SECTION,
+
	ELF_EXTENT_SHDR
+
};
+

+
/*
+
 * A extent descriptor, used when laying out an ELF object.
+
 */
+
struct _Elf_Extent {
+
	SLIST_ENTRY(_Elf_Extent) ex_next;
+
	uint64_t	ex_start; /* Start of the region. */
+
	uint64_t	ex_size;  /* The size of the region. */
+
	enum elf_extent	ex_type;  /* Type of region. */
+
	void		*ex_desc; /* Associated descriptor. */
+
};
+

+
SLIST_HEAD(_Elf_Extent_List, _Elf_Extent);
+

+
/*
+
 * Compute the extents of a section, by looking at the data
+
 * descriptors associated with it.  The function returns 1
+
 * if successful, or zero if an error was detected.
+
 */
+
static int
+
_libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc)
+
{
+
	Elf_Data *d;
+
	size_t fsz, msz;
+
	int ec, elftype;
+
	uint32_t sh_type;
+
	uint64_t d_align;
+
	Elf32_Shdr *shdr32;
+
	Elf64_Shdr *shdr64;
+
	struct _Libelf_Data *ld;
+
	uint64_t scn_size, scn_alignment;
+
	uint64_t sh_align, sh_entsize, sh_offset, sh_size;
+

+
	ec = e->e_class;
+

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

+
	assert(sh_type != SHT_NULL && sh_type != SHT_NOBITS);
+

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

+
	if (sh_align == 0)
+
		sh_align = _libelf_falign(elftype, ec);
+

+
	/*
+
	 * Compute the section's size and alignment using the data
+
	 * descriptors associated with the section.
+
	 */
+
	if (STAILQ_EMPTY(&s->s_data)) {
+
		/*
+
		 * The section's content (if any) has not been read in
+
		 * yet.  If section is not dirty marked dirty, we can
+
		 * reuse the values in the 'sh_size' and 'sh_offset'
+
		 * fields of the section header.
+
		 */
+
		if ((s->s_flags & ELF_F_DIRTY) == 0) {
+
			/*
+
			 * If the library is doing the layout, then we
+
			 * compute the new start offset for the
+
			 * section based on the current offset and the
+
			 * section's alignment needs.
+
			 *
+
			 * If the application is doing the layout, we
+
			 * can use the value in the 'sh_offset' field
+
			 * in the section header directly.
+
			 */
+
			if (e->e_flags & ELF_F_LAYOUT)
+
				goto updatedescriptor;
+
			else
+
				goto computeoffset;
+
		}
+

+
		/*
+
		 * Otherwise, we need to bring in the section's data
+
		 * from the underlying ELF object.
+
		 */
+
		if (e->e_cmd != ELF_C_WRITE && elf_getdata(s, NULL) == NULL)
+
			return (0);
+
	}
+

+
	/*
+
	 * Loop through the section's data descriptors.
+
	 */
+
	scn_size = 0L;
+
	scn_alignment = 0;
+
	STAILQ_FOREACH(ld, &s->s_data, d_next)  {
+

+
		d = &ld->d_data;
+

+
		/*
+
		 * The data buffer's type is known.
+
		 */
+
		if (d->d_type >= ELF_T_NUM) {
+
			LIBELF_SET_ERROR(DATA, 0);
+
			return (0);
+
		}
+

+
		/*
+
		 * The data buffer's version is supported.
+
		 */
+
		if (d->d_version != e->e_version) {
+
			LIBELF_SET_ERROR(VERSION, 0);
+
			return (0);
+
		}
+

+
		/*
+
		 * The buffer's alignment is non-zero and a power of
+
		 * two.
+
		 */
+
		if ((d_align = d->d_align) == 0 ||
+
		    (d_align & (d_align - 1))) {
+
			LIBELF_SET_ERROR(DATA, 0);
+
			return (0);
+
		}
+

+
		/*
+
		 * The buffer's size should be a multiple of the
+
		 * memory size of the underlying type.
+
		 */
+
		msz = _libelf_msize(d->d_type, ec, e->e_version);
+
		if (d->d_size % msz) {
+
			LIBELF_SET_ERROR(DATA, 0);
+
			return (0);
+
		}
+

+
		/*
+
		 * If the application is controlling layout, then the
+
		 * d_offset field should be compatible with the
+
		 * buffer's specified alignment.
+
		 */
+
		if ((e->e_flags & ELF_F_LAYOUT) &&
+
		    (d->d_off & (d_align - 1))) {
+
			LIBELF_SET_ERROR(LAYOUT, 0);
+
			return (0);
+
		}
+

+
		/*
+
		 * Compute the section's size.
+
		 */
+
		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;
+
			fsz = _libelf_fsize(d->d_type, ec, d->d_version,
+
			    (size_t) d->d_size / msz);
+
			scn_size += fsz;
+
		}
+

+
		/*
+
		 * The section's alignment is the maximum alignment
+
		 * needed for its data buffers.
+
		 */
+
		if (d_align > scn_alignment)
+
			scn_alignment = d_align;
+
	}
+

+

+
	/*
+
	 * If the application is requesting full control over the
+
	 * layout of the section, check the section's specified size,
+
	 * offsets and alignment 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);
+
		}
+
		goto updatedescriptor;
+
	}
+

+
	/*
+
	 * Otherwise, compute the values in the section header.
+
	 *
+
	 * The section alignment is the maximum alignment for any of
+
	 * its contained data descriptors.
+
	 */
+
	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;
+

+
computeoffset:
+
	/*
+
	 * Compute the new offset for the section based on
+
	 * the section's alignment needs.
+
	 */
+
	sh_offset = roundup((uint64_t) rc, sh_align);
+

+
	/*
+
	 * Update the section header.
+
	 */
+
	if (ec == ELFCLASS32) {
+
		shdr32->sh_addralign = (uint32_t) sh_align;
+
		shdr32->sh_entsize   = (uint32_t) sh_entsize;
+
		shdr32->sh_offset    = (uint32_t) sh_offset;
+
		shdr32->sh_size      = (uint32_t) sh_size;
+
	} else {
+
		shdr64->sh_addralign = sh_align;
+
		shdr64->sh_entsize   = sh_entsize;
+
		shdr64->sh_offset    = sh_offset;
+
		shdr64->sh_size      = sh_size;
+
	}
+

+
updatedescriptor:
+
	/*
+
	 * Update the section descriptor.
+
	 */
+
	s->s_size = sh_size;
+
	s->s_offset = sh_offset;
+

+
	return (1);
+
}
+

+
/*
+
 * Free a list of extent descriptors.
+
 */
+

+
static void
+
_libelf_release_extents(struct _Elf_Extent_List *extents)
+
{
+
	struct _Elf_Extent *ex;
+

+
	while ((ex = SLIST_FIRST(extents)) != NULL) {
+
		SLIST_REMOVE_HEAD(extents, ex_next);
+
		free(ex);
+
	}
+
}
+

+
/*
+
 * Check if an extent 's' defined by [start..start+size) is free.
+
 * This routine assumes that the given extent list is sorted in order
+
 * of ascending extent offsets.
+
 */
+

+
static int
+
_libelf_extent_is_unused(struct _Elf_Extent_List *extents,
+
    const uint64_t start, const uint64_t size, struct _Elf_Extent **prevt)
+
{
+
	uint64_t tmax, tmin;
+
	struct _Elf_Extent *t, *pt;
+
	const uint64_t smax = start + size;
+

+
	/* First, look for overlaps with existing extents. */
+
	pt = NULL;
+
	SLIST_FOREACH(t, extents, ex_next) {
+
		tmin = t->ex_start;
+
		tmax = tmin + t->ex_size;
+

+
		if (tmax <= start) {
+
			/*
+
			 * 't' lies entirely before 's': ...| t |...| s |...
+
			 */
+
			pt = t;
+
			continue;
+
		} else if (smax <= tmin) {
+
			/*
+
			 * 's' lies entirely before 't', and after 'pt':
+
			 *      ...| pt |...| s |...| t |...
+
			 */
+
			assert(pt == NULL ||
+
			    pt->ex_start + pt->ex_size <= start);
+
			break;
+
		} else
+
			/* 's' and 't' overlap. */
+
			return (0);
+
	}
+

+
	if (prevt)
+
		*prevt = pt;
+
	return (1);
+
}
+

+
/*
+
 * Insert an extent into the list of extents.
+
 */
+

+
static int
+
_libelf_insert_extent(struct _Elf_Extent_List *extents, int type,
+
    uint64_t start, uint64_t size, void *desc)
+
{
+
	struct _Elf_Extent *ex, *prevt;
+

+
	assert(type >= ELF_EXTENT_EHDR && type <= ELF_EXTENT_SHDR);
+

+
	prevt = NULL;
+

+
	/*
+
	 * If the requested range overlaps with an existing extent,
+
	 * signal an error.
+
	 */
+
	if (!_libelf_extent_is_unused(extents, start, size, &prevt)) {
+
		LIBELF_SET_ERROR(LAYOUT, 0);
+
		return (0);
+
	}
+

+
	/* Allocate and fill in a new extent descriptor. */
+
	if ((ex = malloc(sizeof(struct _Elf_Extent))) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, errno);
+
		return (0);
+
	}
+
	ex->ex_start = start;
+
	ex->ex_size = size;
+
	ex->ex_desc = desc;
+
	ex->ex_type = type;
+

+
	/* Insert the region descriptor into the list. */
+
	if (prevt)
+
		SLIST_INSERT_AFTER(prevt, ex, ex_next);
+
	else
+
		SLIST_INSERT_HEAD(extents, ex, ex_next);
+
	return (1);
+
}
+

+
/*
+
 * Recompute section layout.
+
 */
+

+
static off_t
+
_libelf_resync_sections(Elf *e, off_t rc, struct _Elf_Extent_List *extents)
+
{
+
	int ec;
+
	Elf_Scn *s;
+
	size_t sh_type;
+

+
	ec = e->e_class;
+

+
	/*
+
	 * Make a pass through sections, computing the extent of each
+
	 * section.
+
	 */
+
	STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) {
+
		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 (_libelf_compute_section_extents(e, s, rc) == 0)
+
			return ((off_t) -1);
+

+
		if (s->s_size == 0)
+
			continue;
+

+
		if (!_libelf_insert_extent(extents, ELF_EXTENT_SECTION,
+
		    s->s_offset, s->s_size, s))
+
			return ((off_t) -1);
+

+
		if ((size_t) rc < s->s_offset + s->s_size)
+
			rc = (off_t) (s->s_offset + s->s_size);
+
	}
+

+
	return (rc);
+
}
+

+
/*
+
 * Recompute the layout of the ELF object and update the internal data
+
 * structures associated with the ELF descriptor.
+
 *
+
 * 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 extents in the ELF object are sorted in order of ascending
+
 *   addresses.  Sections have their section header table entries
+
 *   updated.  An error is signalled if an overlap was detected among
+
 *   extents.
+
 * - Data descriptors associated with sections are checked for valid
+
 *   types, offsets and alignment.
+
 *
+
 * After a resync_elf() successfully returns, the ELF descriptor is
+
 * ready for being handed over to _libelf_write_elf().
+
 */
+

+
static off_t
+
_libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents)
+
{
+
	int ec, eh_class;
+
	unsigned int eh_byteorder, eh_version;
+
	size_t align, fsz;
+
	size_t phnum, shnum;
+
	off_t rc, phoff, shoff;
+
	void *ehdr, *phdr;
+
	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        = (off_t) eh32->e_phoff;
+
		shoff        = (off_t) eh32->e_shoff;
+
		eh_version   = eh32->e_version;
+
	} else {
+
		eh_byteorder = eh64->e_ident[EI_DATA];
+
		eh_class     = eh64->e_ident[EI_CLASS];
+
		phoff        = (off_t) eh64->e_phoff;
+
		shoff        = (off_t) eh64->e_shoff;
+
		eh_version   = eh64->e_version;
+
	}
+

+
	if (phoff < 0 || shoff < 0) {
+
		LIBELF_SET_ERROR(HEADER, 0);
+
		return ((off_t) -1);
+
	}
+

+
	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 {					\
+
		unsigned int _version = (unsigned int) (V);		\
+
		(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] = (unsigned char) (EC);		\
+
		(E)->e_ident[EI_VERSION] = (_version & 0xFFU);		\
+
		(E)->e_ehsize = (uint16_t) _libelf_fsize(ELF_T_EHDR,	\
+
		    (EC), _version, (size_t) 1);			\
+
		(E)->e_phentsize = (uint16_t) ((phnum == 0) ? 0 :	\
+
		    _libelf_fsize(ELF_T_PHDR, (EC), _version,		\
+
			(size_t) 1));					\
+
		(E)->e_shentsize = (uint16_t) _libelf_fsize(ELF_T_SHDR,	\
+
		    (EC), _version, (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 += (off_t) _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1);
+

+
	if (!_libelf_insert_extent(extents, ELF_EXTENT_EHDR, 0, (uint64_t) rc,
+
		ehdr))
+
		return ((off_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(LAYOUT, 0);
+
				return ((off_t) -1);
+
			}
+

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

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

+
		rc = phoff + (off_t) fsz;
+

+
		phdr = _libelf_getphdr(e, ec);
+

+
		if (!_libelf_insert_extent(extents, ELF_EXTENT_PHDR,
+
			(uint64_t) phoff, fsz, phdr))
+
			return ((off_t) -1);
+
	} 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_section_headers(e, ehdr) == 0)
+
		return ((off_t) -1);
+

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

+
	/*
+
	 * Compute the space taken up by the section header table, if
+
	 * one is needed.
+
	 *
+
	 * If ELF_F_LAYOUT has been 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 the library is responsible for laying out the object,
+
	 * the section header table is placed after section data.
+
	 */
+
	if (shnum) {
+
		fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, shnum);
+
		align = _libelf_falign(ELF_T_SHDR, ec);
+

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

+
		if (shoff + (off_t) fsz > rc)
+
			rc = shoff + (off_t) fsz;
+

+
		if (!_libelf_insert_extent(extents, ELF_EXTENT_SHDR,
+
			(uint64_t) shoff, fsz, NULL))
+
			return ((off_t) -1);
+
	} 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 an ELF section.
+
 */
+

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

+
	assert(ex->ex_type == ELF_EXTENT_SECTION);
+

+
	s = ex->ex_desc;
+
	rc = (off_t) ex->ex_start;
+

+
	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(ld, &s->s_rawdata, d_next) {
+

+
			d = &ld->d_data;
+

+
			if ((uint64_t) rc < sh_off + d->d_off)
+
				(void) memset(nf + rc,
+
				    LIBELF_PRIVATE(fillchar),
+
				    (size_t) (sh_off + d->d_off -
+
					(uint64_t) rc));
+
			rc = (off_t) (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,
+
			    (size_t) d->d_size);
+

+
			rc += (off_t) 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(ld, &s->s_data, d_next) {
+

+
		d = &ld->d_data;
+

+
		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),
+
			    (size_t) (sh_off + d->d_off - (uint64_t) rc));
+

+
		rc = (off_t) (sh_off + d->d_off);
+

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

+
		nobjects = (size_t) (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 += (off_t) fsz;
+
	}
+

+
	return (rc);
+
}
+

+
/*
+
 * Write out an ELF Executable Header.
+
 */
+

+
static off_t
+
_libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
+
{
+
	int ec;
+
	void *ehdr;
+
	size_t fsz, msz;
+
	Elf_Data dst, src;
+

+
	assert(ex->ex_type == ELF_EXTENT_EHDR);
+
	assert(ex->ex_start == 0); /* Ehdr always comes first. */
+

+
	ec = e->e_class;
+

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

+
	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;
+

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

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

+
	return ((off_t) fsz);
+
}
+

+
/*
+
 * Write out an ELF program header table.
+
 */
+

+
static off_t
+
_libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
+
{
+
	int ec;
+
	void *ehdr;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+
	Elf_Data dst, src;
+
	size_t fsz, phnum;
+
	uint64_t phoff;
+

+
	assert(ex->ex_type == ELF_EXTENT_PHDR);
+

+
	ec = e->e_class;
+
	ehdr = _libelf_ehdr(e, ec, 0);
+
	phnum = e->e_u.e_elf.e_nphdr;
+

+
	assert(phnum > 0);
+

+
	if (ec == ELFCLASS32) {
+
		eh32 = (Elf32_Ehdr *) ehdr;
+
		phoff = (uint64_t) eh32->e_phoff;
+
	} else {
+
		eh64 = (Elf64_Ehdr *) ehdr;
+
		phoff = eh64->e_phoff;
+
	}
+

+
	assert(phoff > 0);
+
	assert(ex->ex_start == phoff);
+
	assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0);
+

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

+
	fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum);
+
	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;
+
	dst.d_buf = nf + ex->ex_start;
+

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

+
	return ((off_t) (phoff + fsz));
+
}
+

+
/*
+
 * Write out an ELF section header table.
+
 */
+

+
static off_t
+
_libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
+
{
+
	int ec;
+
	void *ehdr;
+
	Elf_Scn *scn;
+
	uint64_t shoff;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+
	size_t fsz, nscn;
+
	Elf_Data dst, src;
+

+
	assert(ex->ex_type == ELF_EXTENT_SHDR);
+

+
	ec = e->e_class;
+
	ehdr = _libelf_ehdr(e, ec, 0);
+
	nscn = e->e_u.e_elf.e_nscn;
+

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

+
	assert(nscn > 0);
+
	assert(shoff % _libelf_falign(ELF_T_SHDR, ec) == 0);
+
	assert(ex->ex_start == shoff);
+

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

+
	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 = nf + ex->ex_start + scn->s_ndx * fsz;
+

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

+
	return ((off_t) (ex->ex_start + nscn * fsz));
+
}
+

+
/*
+
 * 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, struct _Elf_Extent_List *extents)
+
{
+
	off_t nrc, rc;
+
	Elf_Scn *scn, *tscn;
+
	struct _Elf_Extent *ex;
+
	unsigned char *newfile;
+

+
	assert(e->e_kind == ELF_K_ELF);
+
	assert(e->e_cmd == ELF_C_RDWR || e->e_cmd == ELF_C_WRITE);
+
	assert(e->e_fd >= 0);
+

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

+
	nrc = rc = 0;
+
	SLIST_FOREACH(ex, extents, ex_next) {
+

+
		/* Fill inter-extent gaps. */
+
		if (ex->ex_start > (size_t) rc)
+
			(void) memset(newfile + rc, LIBELF_PRIVATE(fillchar),
+
			    (size_t) (ex->ex_start - (uint64_t) rc));
+

+
		switch (ex->ex_type) {
+
		case ELF_EXTENT_EHDR:
+
			if ((nrc = _libelf_write_ehdr(e, newfile, ex)) < 0)
+
				goto error;
+
			break;
+

+
		case ELF_EXTENT_PHDR:
+
			if ((nrc = _libelf_write_phdr(e, newfile, ex)) < 0)
+
				goto error;
+
			break;
+

+
		case ELF_EXTENT_SECTION:
+
			if ((nrc = _libelf_write_scn(e, newfile, ex)) < 0)
+
				goto error;
+
			break;
+

+
		case ELF_EXTENT_SHDR:
+
			if ((nrc = _libelf_write_shdr(e, newfile, ex)) < 0)
+
				goto error;
+
			break;
+

+
		default:
+
			assert(0);
+
			break;
+
		}
+

+
		assert(ex->ex_start + ex->ex_size == (size_t) nrc);
+
		assert(rc < nrc);
+

+
		rc = nrc;
+
	}
+

+
	assert(rc == newsize);
+

+
	/*
+
	 * For regular files, throw away existing file content and
+
	 * unmap any existing mappings.
+
	 */
+
	if ((e->e_flags & LIBELF_F_SPECIAL_FILE) == 0) {
+
		if (ftruncate(e->e_fd, (off_t) 0) < 0 ||
+
		    lseek(e->e_fd, (off_t) 0, SEEK_SET)) {
+
			LIBELF_SET_ERROR(IO, errno);
+
			goto error;
+
		}
+
#if	ELFTC_HAVE_MMAP
+
		if (e->e_flags & LIBELF_F_RAWFILE_MMAP) {
+
			assert(e->e_rawfile != NULL);
+
			assert(e->e_cmd == ELF_C_RDWR);
+
			if (munmap(e->e_rawfile, e->e_rawsize) < 0) {
+
				LIBELF_SET_ERROR(IO, errno);
+
				goto error;
+
			}
+
		}
+
#endif
+
	}
+

+
	/*
+
	 * Write out the new contents.
+
	 */
+
	if (write(e->e_fd, newfile, (size_t) newsize) != newsize) {
+
		LIBELF_SET_ERROR(IO, errno);
+
		goto error;
+
	}
+

+
	/*
+
	 * For files opened in ELF_C_RDWR mode, set up the new 'raw'
+
	 * contents.
+
	 */
+
	if (e->e_cmd == ELF_C_RDWR) {
+
		assert(e->e_rawfile != NULL);
+
		assert((e->e_flags & LIBELF_F_RAWFILE_MALLOC) ||
+
		    (e->e_flags & LIBELF_F_RAWFILE_MMAP));
+
		if (e->e_flags & LIBELF_F_RAWFILE_MALLOC) {
+
			free(e->e_rawfile);
+
			e->e_rawfile = newfile;
+
			newfile = NULL;
+
		}
+
#if	ELFTC_HAVE_MMAP
+
		else if (e->e_flags & LIBELF_F_RAWFILE_MMAP) {
+
			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;
+
			}
+
		}
+
#endif	/* ELFTC_HAVE_MMAP */
+

+
		/* Record the new size of the file. */
+
		e->e_rawsize = (size_t) newsize;
+
	} else {
+
		/* File opened in ELF_C_WRITE mode. */
+
		assert(e->e_rawfile == NULL);
+
	}
+

+
	/*
+
	 * 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 (e->e_class == 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 the temporary buffer. */
+
	if (newfile)
+
		free(newfile);
+

+
	return (rc);
+

+
 error:
+
	free(newfile);
+

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

+
/*
+
 * Update an ELF object.
+
 */
+

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

+
	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);
+
	}
+

+
	SLIST_INIT(&extents);
+

+
	if ((rc = _libelf_resync_elf(e, &extents)) < 0)
+
		goto done;
+

+
	if (c == ELF_C_NULL)
+
		goto done;
+

+
	if (e->e_fd < 0) {
+
		rc = (off_t) -1;
+
		LIBELF_SET_ERROR(SEQUENCE, 0);
+
		goto done;
+
	}
+

+
	rc = _libelf_write_elf(e, rc, &extents);
+

+
done:
+
	_libelf_release_extents(&extents);
+
	return (rc);
+
}
added external/elf/elf_version.3
@@ -0,0 +1,95 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd November 9, 2011
+
.Os
+
.Dt ELF_VERSION 3
+
.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(EXIT_FAILURE, "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/elf/elf_version.c
@@ -0,0 +1,52 @@
+
/*-
+
 * 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/elfdefinitions.h
@@ -0,0 +1,2700 @@
+
/*-
+
 * Copyright (c) 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 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.
+
 *
+
 * $Id: elfdefinitions.h 3403 2016-02-13 15:39:27Z jkoshy $
+
 */
+

+
/*
+
 * These definitions are based on:
+
 * - The public specification of the ELF format as defined in the
+
 *   October 2009 draft of System V ABI.
+
 *   See: http://www.sco.com/developers/gabi/latest/ch4.intro.html
+
 * - The May 1998 (version 1.5) draft of "The ELF-64 object format".
+
 * - Processor-specific ELF ABI definitions for sparc, i386, amd64, mips,
+
 *   ia64, and powerpc processors.
+
 * - The "Linkers and Libraries Guide", from Sun Microsystems.
+
 */
+

+
#ifndef _ELFDEFINITIONS_H_
+
#define _ELFDEFINITIONS_H_
+

+
#include <stdint.h>
+

+
/*
+
 * Types of capabilities.
+
 */
+

+
#define	_ELF_DEFINE_CAPABILITIES()				\
+
_ELF_DEFINE_CA(CA_SUNW_NULL,	0,	"ignored")		\
+
_ELF_DEFINE_CA(CA_SUNW_HW_1,	1,	"hardware capability")	\
+
_ELF_DEFINE_CA(CA_SUNW_SW_1,	2,	"software capability")
+

+
#undef	_ELF_DEFINE_CA
+
#define	_ELF_DEFINE_CA(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_CAPABILITIES()
+
	CA__LAST__
+
};
+

+
/*
+
 * Flags used with dynamic linking entries.
+
 */
+

+
#define	_ELF_DEFINE_DYN_FLAGS()					\
+
_ELF_DEFINE_DF(DF_ORIGIN,           0x1,			\
+
	"object being loaded may refer to $ORIGIN")		\
+
_ELF_DEFINE_DF(DF_SYMBOLIC,         0x2,			\
+
	"search library for references before executable")	\
+
_ELF_DEFINE_DF(DF_TEXTREL,          0x4,			\
+
	"relocation entries may modify text segment")		\
+
_ELF_DEFINE_DF(DF_BIND_NOW,         0x8,			\
+
	"process relocation entries at load time")		\
+
_ELF_DEFINE_DF(DF_STATIC_TLS,       0x10,			\
+
	"uses static thread-local storage")
+
#undef	_ELF_DEFINE_DF
+
#define	_ELF_DEFINE_DF(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_DYN_FLAGS()
+
	DF__LAST__
+
};
+

+

+
/*
+
 * Dynamic linking entry types.
+
 */
+

+
#define	_ELF_DEFINE_DYN_TYPES()						\
+
_ELF_DEFINE_DT(DT_NULL,             0, "end of array")			\
+
_ELF_DEFINE_DT(DT_NEEDED,           1, "names a needed library")	\
+
_ELF_DEFINE_DT(DT_PLTRELSZ,         2,					\
+
	"size in bytes of associated relocation entries")		\
+
_ELF_DEFINE_DT(DT_PLTGOT,           3,					\
+
	"address associated with the procedure linkage table")		\
+
_ELF_DEFINE_DT(DT_HASH,             4,					\
+
	"address of the symbol hash table")				\
+
_ELF_DEFINE_DT(DT_STRTAB,           5,					\
+
	"address of the string table")					\
+
_ELF_DEFINE_DT(DT_SYMTAB,           6,					\
+
	"address of the symbol table")					\
+
_ELF_DEFINE_DT(DT_RELA,             7,					\
+
	"address of the relocation table")				\
+
_ELF_DEFINE_DT(DT_RELASZ,           8, "size of the DT_RELA table")	\
+
_ELF_DEFINE_DT(DT_RELAENT,          9, "size of each DT_RELA entry")	\
+
_ELF_DEFINE_DT(DT_STRSZ,            10, "size of the string table")	\
+
_ELF_DEFINE_DT(DT_SYMENT,           11,					\
+
	"size of a symbol table entry")					\
+
_ELF_DEFINE_DT(DT_INIT,             12,					\
+
	"address of the initialization function")			\
+
_ELF_DEFINE_DT(DT_FINI,             13,					\
+
	"address of the finalization function")				\
+
_ELF_DEFINE_DT(DT_SONAME,           14, "names the shared object")	\
+
_ELF_DEFINE_DT(DT_RPATH,            15,					\
+
	"runtime library search path")					\
+
_ELF_DEFINE_DT(DT_SYMBOLIC,         16,					\
+
	"alter symbol resolution algorithm")				\
+
_ELF_DEFINE_DT(DT_REL,              17,					\
+
	"address of the DT_REL table")					\
+
_ELF_DEFINE_DT(DT_RELSZ,            18, "size of the DT_REL table")	\
+
_ELF_DEFINE_DT(DT_RELENT,           19, "size of each DT_REL entry")	\
+
_ELF_DEFINE_DT(DT_PLTREL,           20,					\
+
	"type of relocation entry in the procedure linkage table")	\
+
_ELF_DEFINE_DT(DT_DEBUG,            21, "used for debugging")		\
+
_ELF_DEFINE_DT(DT_TEXTREL,          22,					\
+
	"text segment may be written to during relocation")		\
+
_ELF_DEFINE_DT(DT_JMPREL,           23,					\
+
	"address of relocation entries associated with the procedure linkage table") \
+
_ELF_DEFINE_DT(DT_BIND_NOW,         24,					\
+
	"bind symbols at loading time")					\
+
_ELF_DEFINE_DT(DT_INIT_ARRAY,       25,					\
+
	"pointers to initialization functions")				\
+
_ELF_DEFINE_DT(DT_FINI_ARRAY,       26,					\
+
	"pointers to termination functions")				\
+
_ELF_DEFINE_DT(DT_INIT_ARRAYSZ,     27, "size of the DT_INIT_ARRAY")	\
+
_ELF_DEFINE_DT(DT_FINI_ARRAYSZ,     28, "size of the DT_FINI_ARRAY")	\
+
_ELF_DEFINE_DT(DT_RUNPATH,          29,					\
+
	"index of library search path string")				\
+
_ELF_DEFINE_DT(DT_FLAGS,            30,					\
+
	"flags specific to the object being loaded")			\
+
_ELF_DEFINE_DT(DT_ENCODING,         32, "standard semantics")		\
+
_ELF_DEFINE_DT(DT_PREINIT_ARRAY,    32,					\
+
	"pointers to pre-initialization functions")			\
+
_ELF_DEFINE_DT(DT_PREINIT_ARRAYSZ,  33,					\
+
	"size of pre-initialization array")				\
+
_ELF_DEFINE_DT(DT_MAXPOSTAGS,	    34,					\
+
	"the number of positive tags")					\
+
_ELF_DEFINE_DT(DT_LOOS,             0x6000000DUL,			\
+
	"start of OS-specific types")					\
+
_ELF_DEFINE_DT(DT_SUNW_AUXILIARY,   0x6000000DUL,			\
+
	"offset of string naming auxiliary filtees")			\
+
_ELF_DEFINE_DT(DT_SUNW_RTLDINF,     0x6000000EUL, "rtld internal use")	\
+
_ELF_DEFINE_DT(DT_SUNW_FILTER,      0x6000000FUL,			\
+
	"offset of string naming standard filtees")			\
+
_ELF_DEFINE_DT(DT_SUNW_CAP,         0x60000010UL,			\
+
	"address of hardware capabilities section")			\
+
_ELF_DEFINE_DT(DT_HIOS,             0x6FFFF000UL,			\
+
	"end of OS-specific types")					\
+
_ELF_DEFINE_DT(DT_VALRNGLO,         0x6FFFFD00UL,			\
+
	"start of range using the d_val field")				\
+
_ELF_DEFINE_DT(DT_GNU_PRELINKED,    0x6FFFFDF5UL,			\
+
	"prelinking timestamp")						\
+
_ELF_DEFINE_DT(DT_GNU_CONFLICTSZ,   0x6FFFFDF6UL,			\
+
	"size of conflict section")					\
+
_ELF_DEFINE_DT(DT_GNU_LIBLISTSZ,    0x6FFFFDF7UL,			\
+
	"size of library list")						\
+
_ELF_DEFINE_DT(DT_CHECKSUM,         0x6FFFFDF8UL,			\
+
	"checksum for the object")					\
+
_ELF_DEFINE_DT(DT_PLTPADSZ,         0x6FFFFDF9UL,			\
+
	"size of PLT padding")						\
+
_ELF_DEFINE_DT(DT_MOVEENT,          0x6FFFFDFAUL,			\
+
	"size of DT_MOVETAB entries")					\
+
_ELF_DEFINE_DT(DT_MOVESZ,           0x6FFFFDFBUL,			\
+
	"total size of the MOVETAB table")				\
+
_ELF_DEFINE_DT(DT_FEATURE,          0x6FFFFDFCUL, "feature values")	\
+
_ELF_DEFINE_DT(DT_POSFLAG_1,        0x6FFFFDFDUL,			\
+
	"dynamic position flags")					\
+
_ELF_DEFINE_DT(DT_SYMINSZ,          0x6FFFFDFEUL,			\
+
	"size of the DT_SYMINFO table")					\
+
_ELF_DEFINE_DT(DT_SYMINENT,         0x6FFFFDFFUL,			\
+
	"size of a DT_SYMINFO entry")					\
+
_ELF_DEFINE_DT(DT_VALRNGHI,         0x6FFFFDFFUL,			\
+
	"end of range using the d_val field")				\
+
_ELF_DEFINE_DT(DT_ADDRRNGLO,        0x6FFFFE00UL,			\
+
	"start of range using the d_ptr field")				\
+
_ELF_DEFINE_DT(DT_GNU_HASH,	    0x6FFFFEF5UL,			\
+
	"GNU style hash tables")					\
+
_ELF_DEFINE_DT(DT_GNU_CONFLICT,     0x6FFFFEF8UL,			\
+
	"address of conflict section")					\
+
_ELF_DEFINE_DT(DT_GNU_LIBLIST,      0x6FFFFEF9UL,			\
+
	"address of conflict section")					\
+
_ELF_DEFINE_DT(DT_CONFIG,           0x6FFFFEFAUL,			\
+
	"configuration file")						\
+
_ELF_DEFINE_DT(DT_DEPAUDIT,         0x6FFFFEFBUL,			\
+
	"string defining audit libraries")				\
+
_ELF_DEFINE_DT(DT_AUDIT,            0x6FFFFEFCUL,			\
+
	"string defining audit libraries")				\
+
_ELF_DEFINE_DT(DT_PLTPAD,           0x6FFFFEFDUL, "PLT padding")	\
+
_ELF_DEFINE_DT(DT_MOVETAB,          0x6FFFFEFEUL,			\
+
	"address of a move table")					\
+
_ELF_DEFINE_DT(DT_SYMINFO,          0x6FFFFEFFUL,			\
+
	"address of the symbol information table")			\
+
_ELF_DEFINE_DT(DT_ADDRRNGHI,        0x6FFFFEFFUL,			\
+
	"end of range using the d_ptr field")				\
+
_ELF_DEFINE_DT(DT_VERSYM,	    0x6FFFFFF0UL,			\
+
	"address of the version section")				\
+
_ELF_DEFINE_DT(DT_RELACOUNT,        0x6FFFFFF9UL,			\
+
	"count of RELA relocations")					\
+
_ELF_DEFINE_DT(DT_RELCOUNT,         0x6FFFFFFAUL,			\
+
	"count of REL relocations")					\
+
_ELF_DEFINE_DT(DT_FLAGS_1,          0x6FFFFFFBUL, "flag values")	\
+
_ELF_DEFINE_DT(DT_VERDEF,	    0x6FFFFFFCUL,			\
+
	"address of the version definition segment")			\
+
_ELF_DEFINE_DT(DT_VERDEFNUM,	    0x6FFFFFFDUL,			\
+
	"the number of version definition entries")			\
+
_ELF_DEFINE_DT(DT_VERNEED,	    0x6FFFFFFEUL,			\
+
	"address of section with needed versions")			\
+
_ELF_DEFINE_DT(DT_VERNEEDNUM,       0x6FFFFFFFUL,			\
+
	"the number of version needed entries")				\
+
_ELF_DEFINE_DT(DT_LOPROC,           0x70000000UL,			\
+
	"start of processor-specific types")				\
+
_ELF_DEFINE_DT(DT_ARM_SYMTABSZ,	    0x70000001UL,			\
+
	"number of entries in the dynamic symbol table")		\
+
_ELF_DEFINE_DT(DT_SPARC_REGISTER,   0x70000001UL,			\
+
	"index of an STT_SPARC_REGISTER symbol")			\
+
_ELF_DEFINE_DT(DT_ARM_PREEMPTMAP,   0x70000002UL,			\
+
	"address of the preemption map")				\
+
_ELF_DEFINE_DT(DT_MIPS_RLD_VERSION, 0x70000001UL,			\
+
	"version ID for runtime linker interface")			\
+
_ELF_DEFINE_DT(DT_MIPS_TIME_STAMP,  0x70000002UL,			\
+
	"timestamp")							\
+
_ELF_DEFINE_DT(DT_MIPS_ICHECKSUM,   0x70000003UL,			\
+
	"checksum of all external strings and common sizes")		\
+
_ELF_DEFINE_DT(DT_MIPS_IVERSION,    0x70000004UL,			\
+
	"string table index of a version string")			\
+
_ELF_DEFINE_DT(DT_MIPS_FLAGS,       0x70000005UL,			\
+
	"MIPS-specific flags")						\
+
_ELF_DEFINE_DT(DT_MIPS_BASE_ADDRESS, 0x70000006UL,			\
+
	"base address for the executable/DSO")				\
+
_ELF_DEFINE_DT(DT_MIPS_CONFLICT,    0x70000008UL,			\
+
	"address of .conflict section")					\
+
_ELF_DEFINE_DT(DT_MIPS_LIBLIST,     0x70000009UL,			\
+
	"address of .liblist section")					\
+
_ELF_DEFINE_DT(DT_MIPS_LOCAL_GOTNO, 0x7000000AUL,			\
+
	"number of local GOT entries")					\
+
_ELF_DEFINE_DT(DT_MIPS_CONFLICTNO,  0x7000000BUL,			\
+
	"number of entries in the .conflict section")			\
+
_ELF_DEFINE_DT(DT_MIPS_LIBLISTNO,   0x70000010UL,			\
+
	"number of entries in the .liblist section")			\
+
_ELF_DEFINE_DT(DT_MIPS_SYMTABNO,    0x70000011UL,			\
+
	"number of entries in the .dynsym section")			\
+
_ELF_DEFINE_DT(DT_MIPS_UNREFEXTNO,  0x70000012UL,			\
+
	"index of first external dynamic symbol not ref'ed locally")	\
+
_ELF_DEFINE_DT(DT_MIPS_GOTSYM,      0x70000013UL,			\
+
	"index of first dynamic symbol corresponds to a GOT entry")	\
+
_ELF_DEFINE_DT(DT_MIPS_HIPAGENO,    0x70000014UL,			\
+
	"number of page table entries in GOT")				\
+
_ELF_DEFINE_DT(DT_MIPS_RLD_MAP,     0x70000016UL,			\
+
	"address of runtime linker map")				\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASS, 0x70000017UL,			\
+
	"Delta C++ class definition")					\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASS_NO, 0x70000018UL,			\
+
	"number of entries in DT_MIPS_DELTA_CLASS")			\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_INSTANCE, 0x70000019UL,			\
+
	"Delta C++ class instances")					\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_INSTANCE_NO, 0x7000001AUL,			\
+
	"number of entries in DT_MIPS_DELTA_INSTANCE")			\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_RELOC, 0x7000001BUL,			\
+
	"Delta relocations")						\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_RELOC_NO, 0x7000001CUL,			\
+
	"number of entries in DT_MIPS_DELTA_RELOC")			\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_SYM,   0x7000001DUL,			\
+
	"Delta symbols refered by Delta relocations")			\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_SYM_NO, 0x7000001EUL,			\
+
	"number of entries in DT_MIPS_DELTA_SYM")			\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASSSYM, 0x70000020UL,			\
+
	"Delta symbols for class declarations")				\
+
_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASSSYM_NO, 0x70000021UL,			\
+
	"number of entries in DT_MIPS_DELTA_CLASSSYM")			\
+
_ELF_DEFINE_DT(DT_MIPS_CXX_FLAGS,   0x70000022UL,			\
+
	"C++ flavor flags")						\
+
_ELF_DEFINE_DT(DT_MIPS_PIXIE_INIT,  0x70000023UL,			\
+
	"address of an initialization routine created by pixie")	\
+
_ELF_DEFINE_DT(DT_MIPS_SYMBOL_LIB,  0x70000024UL,			\
+
	"address of .MIPS.symlib section")				\
+
_ELF_DEFINE_DT(DT_MIPS_LOCALPAGE_GOTIDX, 0x70000025UL,			\
+
	"GOT index of first page table entry for a segment")		\
+
_ELF_DEFINE_DT(DT_MIPS_LOCAL_GOTIDX, 0x70000026UL,			\
+
	"GOT index of first page table entry for a local symbol")	\
+
_ELF_DEFINE_DT(DT_MIPS_HIDDEN_GOTIDX, 0x70000027UL,			\
+
	"GOT index of first page table entry for a hidden symbol")	\
+
_ELF_DEFINE_DT(DT_MIPS_PROTECTED_GOTIDX, 0x70000028UL,			\
+
	"GOT index of first page table entry for a protected symbol")	\
+
_ELF_DEFINE_DT(DT_MIPS_OPTIONS,     0x70000029UL,			\
+
	"address of .MIPS.options section")				\
+
_ELF_DEFINE_DT(DT_MIPS_INTERFACE,   0x7000002AUL,			\
+
	"address of .MIPS.interface section")				\
+
_ELF_DEFINE_DT(DT_MIPS_DYNSTR_ALIGN, 0x7000002BUL, "???")		\
+
_ELF_DEFINE_DT(DT_MIPS_INTERFACE_SIZE, 0x7000002CUL,			\
+
	"size of .MIPS.interface section")				\
+
_ELF_DEFINE_DT(DT_MIPS_RLD_TEXT_RESOLVE_ADDR, 0x7000002DUL,		\
+
	"address of _rld_text_resolve in GOT")				\
+
_ELF_DEFINE_DT(DT_MIPS_PERF_SUFFIX, 0x7000002EUL,			\
+
	"default suffix of DSO to be appended by dlopen")		\
+
_ELF_DEFINE_DT(DT_MIPS_COMPACT_SIZE, 0x7000002FUL,			\
+
	"size of a ucode compact relocation record (o32)")		\
+
_ELF_DEFINE_DT(DT_MIPS_GP_VALUE,    0x70000030UL,			\
+
	"GP value of a specified GP relative range")			\
+
_ELF_DEFINE_DT(DT_MIPS_AUX_DYNAMIC, 0x70000031UL,			\
+
	"address of an auxiliary dynamic table")			\
+
_ELF_DEFINE_DT(DT_MIPS_PLTGOT,      0x70000032UL,			\
+
	"address of the PLTGOT")					\
+
_ELF_DEFINE_DT(DT_MIPS_RLD_OBJ_UPDATE, 0x70000033UL,			\
+
	"object list update callback")					\
+
_ELF_DEFINE_DT(DT_MIPS_RWPLT,       0x70000034UL,			\
+
	"address of a writable PLT")					\
+
_ELF_DEFINE_DT(DT_PPC_GOT,          0x70000000UL,			\
+
	"value of _GLOBAL_OFFSET_TABLE_")				\
+
_ELF_DEFINE_DT(DT_PPC_TLSOPT,       0x70000001UL,			\
+
	"TLS descriptor should be optimized")				\
+
_ELF_DEFINE_DT(DT_PPC64_GLINK,      0x70000000UL,			\
+
	"address of .glink section")					\
+
_ELF_DEFINE_DT(DT_PPC64_OPD,        0x70000001UL,			\
+
	"address of .opd section")					\
+
_ELF_DEFINE_DT(DT_PPC64_OPDSZ,      0x70000002UL,			\
+
	"size of .opd section")						\
+
_ELF_DEFINE_DT(DT_PPC64_TLSOPT,     0x70000003UL,			\
+
	"TLS descriptor should be optimized")				\
+
_ELF_DEFINE_DT(DT_AUXILIARY,        0x7FFFFFFDUL,			\
+
	"offset of string naming auxiliary filtees")			\
+
_ELF_DEFINE_DT(DT_USED,             0x7FFFFFFEUL, "ignored")		\
+
_ELF_DEFINE_DT(DT_FILTER,           0x7FFFFFFFUL,			\
+
	"index of string naming filtees")				\
+
_ELF_DEFINE_DT(DT_HIPROC,           0x7FFFFFFFUL,			\
+
	"end of processor-specific types")
+

+
#undef	_ELF_DEFINE_DT
+
#define	_ELF_DEFINE_DT(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_DYN_TYPES()
+
	DT__LAST__ = DT_HIPROC
+
};
+

+
#define	DT_DEPRECATED_SPARC_REGISTER	DT_SPARC_REGISTER
+

+
/*
+
 * Flags used in the executable header (field: e_flags).
+
 */
+
#define	_ELF_DEFINE_EHDR_FLAGS()					\
+
_ELF_DEFINE_EF(EF_ARM_RELEXEC,      0x00000001UL,			\
+
	"dynamic segment describes only how to relocate segments")	\
+
_ELF_DEFINE_EF(EF_ARM_HASENTRY,     0x00000002UL,			\
+
	"e_entry contains a program entry point")			\
+
_ELF_DEFINE_EF(EF_ARM_SYMSARESORTED, 0x00000004UL,			\
+
	"subsection of symbol table is sorted by symbol value")		\
+
_ELF_DEFINE_EF(EF_ARM_DYNSYMSUSESEGIDX, 0x00000008UL,			\
+
	"dynamic symbol st_shndx = containing segment index + 1")	\
+
_ELF_DEFINE_EF(EF_ARM_MAPSYMSFIRST, 0x00000010UL,			\
+
	"mapping symbols precede other local symbols in symtab")	\
+
_ELF_DEFINE_EF(EF_ARM_BE8,          0x00800000UL,			\
+
	"file contains BE-8 code")					\
+
_ELF_DEFINE_EF(EF_ARM_LE8,          0x00400000UL,			\
+
	"file contains LE-8 code")					\
+
_ELF_DEFINE_EF(EF_ARM_EABIMASK,     0xFF000000UL,			\
+
	"mask for ARM EABI version number (0 denotes GNU or unknown)")	\
+
_ELF_DEFINE_EF(EF_ARM_EABI_UNKNOWN, 0x00000000UL,			\
+
	"Unknown or GNU ARM EABI version number")			\
+
_ELF_DEFINE_EF(EF_ARM_EABI_VER1,    0x01000000UL,			\
+
	"ARM EABI version 1")						\
+
_ELF_DEFINE_EF(EF_ARM_EABI_VER2,    0x02000000UL,			\
+
	"ARM EABI version 2")						\
+
_ELF_DEFINE_EF(EF_ARM_EABI_VER3,    0x03000000UL,			\
+
	"ARM EABI version 3")						\
+
_ELF_DEFINE_EF(EF_ARM_EABI_VER4,    0x04000000UL,			\
+
	"ARM EABI version 4")						\
+
_ELF_DEFINE_EF(EF_ARM_EABI_VER5,    0x05000000UL,			\
+
	"ARM EABI version 5")						\
+
_ELF_DEFINE_EF(EF_ARM_INTERWORK,    0x00000004UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_ARM_APCS_26,      0x00000008UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_ARM_APCS_FLOAT,   0x00000010UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_ARM_PIC,          0x00000020UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_ARM_ALIGN8,       0x00000040UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_ARM_NEW_ABI,      0x00000080UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_ARM_OLD_ABI,      0x00000100UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_ARM_SOFT_FLOAT,   0x00000200UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_ARM_VFP_FLOAT,    0x00000400UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_ARM_MAVERICK_FLOAT, 0x00000800UL,			\
+
	"GNU EABI extension")						\
+
_ELF_DEFINE_EF(EF_MIPS_NOREORDER,   0x00000001UL,			\
+
	"at least one .noreorder directive appeared in the source")	\
+
_ELF_DEFINE_EF(EF_MIPS_PIC,         0x00000002UL,			\
+
	"file contains position independent code")			\
+
_ELF_DEFINE_EF(EF_MIPS_CPIC,        0x00000004UL,			\
+
	"file's code uses standard conventions for calling PIC")	\
+
_ELF_DEFINE_EF(EF_MIPS_UCODE,       0x00000010UL,			\
+
	"file contains UCODE (obsolete)")				\
+
_ELF_DEFINE_EF(EF_MIPS_ABI2,        0x00000020UL,			\
+
	"file follows MIPS III 32-bit ABI")				\
+
_ELF_DEFINE_EF(EF_MIPS_OPTIONS_FIRST, 0x00000080UL,			\
+
	"ld(1) should process .MIPS.options section first")		\
+
_ELF_DEFINE_EF(EF_MIPS_ARCH_ASE,    0x0F000000UL,			\
+
	"file uses application-specific architectural extensions")	\
+
_ELF_DEFINE_EF(EF_MIPS_ARCH_ASE_MDMX, 0x08000000UL,			\
+
	"file uses MDMX multimedia extensions")				\
+
_ELF_DEFINE_EF(EF_MIPS_ARCH_ASE_M16, 0x04000000UL,			\
+
	"file uses MIPS-16 ISA extensions")				\
+
_ELF_DEFINE_EF(EF_MIPS_ARCH,         0xF0000000UL,			\
+
	"4-bit MIPS architecture field")				\
+
_ELF_DEFINE_EF(EF_PPC_EMB,          0x80000000UL,			\
+
	"Embedded PowerPC flag")					\
+
_ELF_DEFINE_EF(EF_PPC_RELOCATABLE,  0x00010000UL,			\
+
	"-mrelocatable flag")						\
+
_ELF_DEFINE_EF(EF_PPC_RELOCATABLE_LIB, 0x00008000UL,			\
+
	"-mrelocatable-lib flag")					\
+
_ELF_DEFINE_EF(EF_SPARC_EXT_MASK,   0x00ffff00UL,			\
+
	"Vendor Extension mask")					\
+
_ELF_DEFINE_EF(EF_SPARC_32PLUS,     0x00000100UL,			\
+
	"Generic V8+ features")						\
+
_ELF_DEFINE_EF(EF_SPARC_SUN_US1,    0x00000200UL,			\
+
	"Sun UltraSPARCTM 1 Extensions")				\
+
_ELF_DEFINE_EF(EF_SPARC_HAL_R1,     0x00000400UL, "HAL R1 Extensions")	\
+
_ELF_DEFINE_EF(EF_SPARC_SUN_US3,    0x00000800UL,			\
+
	"Sun UltraSPARC 3 Extensions")					\
+
_ELF_DEFINE_EF(EF_SPARCV9_MM,       0x00000003UL,			\
+
	"Mask for Memory Model")					\
+
_ELF_DEFINE_EF(EF_SPARCV9_TSO,      0x00000000UL,			\
+
	"Total Store Ordering")						\
+
_ELF_DEFINE_EF(EF_SPARCV9_PSO,      0x00000001UL,			\
+
	"Partial Store Ordering")					\
+
_ELF_DEFINE_EF(EF_SPARCV9_RMO,      0x00000002UL,			\
+
	"Relaxed Memory Ordering")
+

+
#undef	_ELF_DEFINE_EF
+
#define	_ELF_DEFINE_EF(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_EHDR_FLAGS()
+
	EF__LAST__
+
};
+

+
/*
+
 * Offsets in the `ei_ident[]` field of an ELF executable header.
+
 */
+
#define	_ELF_DEFINE_EI_OFFSETS()			\
+
_ELF_DEFINE_EI(EI_MAG0,     0, "magic number")		\
+
_ELF_DEFINE_EI(EI_MAG1,     1, "magic number")		\
+
_ELF_DEFINE_EI(EI_MAG2,     2, "magic number")		\
+
_ELF_DEFINE_EI(EI_MAG3,     3, "magic number")		\
+
_ELF_DEFINE_EI(EI_CLASS,    4, "file class")		\
+
_ELF_DEFINE_EI(EI_DATA,     5, "data encoding")		\
+
_ELF_DEFINE_EI(EI_VERSION,  6, "file version")		\
+
_ELF_DEFINE_EI(EI_OSABI,    7, "OS ABI kind")		\
+
_ELF_DEFINE_EI(EI_ABIVERSION, 8, "OS ABI version")	\
+
_ELF_DEFINE_EI(EI_PAD,	    9, "padding start")		\
+
_ELF_DEFINE_EI(EI_NIDENT,  16, "total size")
+

+
#undef	_ELF_DEFINE_EI
+
#define	_ELF_DEFINE_EI(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_EI_OFFSETS()
+
	EI__LAST__
+
};
+

+
/*
+
 * The ELF class of an object.
+
 */
+
#define	_ELF_DEFINE_ELFCLASS()				\
+
_ELF_DEFINE_EC(ELFCLASSNONE, 0, "Unknown ELF class")	\
+
_ELF_DEFINE_EC(ELFCLASS32,   1, "32 bit objects")	\
+
_ELF_DEFINE_EC(ELFCLASS64,   2, "64 bit objects")
+

+
#undef	_ELF_DEFINE_EC
+
#define	_ELF_DEFINE_EC(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_ELFCLASS()
+
	EC__LAST__
+
};
+

+
/*
+
 * Endianness of data in an ELF object.
+
 */
+

+
#define	_ELF_DEFINE_ELF_DATA_ENDIANNESS()			\
+
_ELF_DEFINE_ED(ELFDATANONE, 0, "Unknown data endianness")	\
+
_ELF_DEFINE_ED(ELFDATA2LSB, 1, "little endian")			\
+
_ELF_DEFINE_ED(ELFDATA2MSB, 2, "big endian")
+

+
#undef	_ELF_DEFINE_ED
+
#define	_ELF_DEFINE_ED(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_ELF_DATA_ENDIANNESS()
+
	ED__LAST__
+
};
+

+
/*
+
 * Values of the magic numbers used in identification array.
+
 */
+
#define	_ELF_DEFINE_ELF_MAGIC()			\
+
_ELF_DEFINE_EMAG(ELFMAG0, 0x7FU)		\
+
_ELF_DEFINE_EMAG(ELFMAG1, 'E')			\
+
_ELF_DEFINE_EMAG(ELFMAG2, 'L')			\
+
_ELF_DEFINE_EMAG(ELFMAG3, 'F')
+

+
#undef	_ELF_DEFINE_EMAG
+
#define	_ELF_DEFINE_EMAG(N, V)		N = V ,
+
enum {
+
	_ELF_DEFINE_ELF_MAGIC()
+
	ELFMAG__LAST__
+
};
+

+
/*
+
 * ELF OS ABI field.
+
 */
+
#define	_ELF_DEFINE_ELF_OSABI()						\
+
_ELF_DEFINE_EABI(ELFOSABI_NONE,       0,				\
+
	"No extensions or unspecified")					\
+
_ELF_DEFINE_EABI(ELFOSABI_SYSV,       0, "SYSV")			\
+
_ELF_DEFINE_EABI(ELFOSABI_HPUX,       1, "Hewlett-Packard HP-UX")	\
+
_ELF_DEFINE_EABI(ELFOSABI_NETBSD,     2, "NetBSD")			\
+
_ELF_DEFINE_EABI(ELFOSABI_GNU,        3, "GNU")				\
+
_ELF_DEFINE_EABI(ELFOSABI_HURD,       4, "GNU/HURD")			\
+
_ELF_DEFINE_EABI(ELFOSABI_86OPEN,     5, "86Open Common ABI")		\
+
_ELF_DEFINE_EABI(ELFOSABI_SOLARIS,    6, "Sun Solaris")			\
+
_ELF_DEFINE_EABI(ELFOSABI_AIX,        7, "AIX")				\
+
_ELF_DEFINE_EABI(ELFOSABI_IRIX,       8, "IRIX")			\
+
_ELF_DEFINE_EABI(ELFOSABI_FREEBSD,    9, "FreeBSD")			\
+
_ELF_DEFINE_EABI(ELFOSABI_TRU64,      10, "Compaq TRU64 UNIX")		\
+
_ELF_DEFINE_EABI(ELFOSABI_MODESTO,    11, "Novell Modesto")		\
+
_ELF_DEFINE_EABI(ELFOSABI_OPENBSD,    12, "Open BSD")			\
+
_ELF_DEFINE_EABI(ELFOSABI_OPENVMS,    13, "Open VMS")			\
+
_ELF_DEFINE_EABI(ELFOSABI_NSK,        14,				\
+
	"Hewlett-Packard Non-Stop Kernel")				\
+
_ELF_DEFINE_EABI(ELFOSABI_AROS,       15, "Amiga Research OS")		\
+
_ELF_DEFINE_EABI(ELFOSABI_FENIXOS,    16,				\
+
	"The FenixOS highly scalable multi-core OS")			\
+
_ELF_DEFINE_EABI(ELFOSABI_ARM_AEABI,  64,				\
+
	"ARM specific symbol versioning extensions")			\
+
_ELF_DEFINE_EABI(ELFOSABI_ARM,        97, "ARM ABI")			\
+
_ELF_DEFINE_EABI(ELFOSABI_STANDALONE, 255,				\
+
	"Standalone (embedded) application")
+

+
#undef	_ELF_DEFINE_EABI
+
#define	_ELF_DEFINE_EABI(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_ELF_OSABI()
+
	ELFOSABI__LAST__
+
};
+

+
#define	ELFOSABI_LINUX			ELFOSABI_GNU
+

+
/*
+
 * ELF Machine types: (EM_*).
+
 */
+
#define	_ELF_DEFINE_ELF_MACHINES()					\
+
_ELF_DEFINE_EM(EM_NONE,             0, "No machine")			\
+
_ELF_DEFINE_EM(EM_M32,              1, "AT&T WE 32100")			\
+
_ELF_DEFINE_EM(EM_SPARC,            2, "SPARC")				\
+
_ELF_DEFINE_EM(EM_386,              3, "Intel 80386")			\
+
_ELF_DEFINE_EM(EM_68K,              4, "Motorola 68000")		\
+
_ELF_DEFINE_EM(EM_88K,              5, "Motorola 88000")		\
+
_ELF_DEFINE_EM(EM_IAMCU,            6, "Intel MCU")			\
+
_ELF_DEFINE_EM(EM_860,              7, "Intel 80860")			\
+
_ELF_DEFINE_EM(EM_MIPS,             8, "MIPS I Architecture")		\
+
_ELF_DEFINE_EM(EM_S370,             9, "IBM System/370 Processor")	\
+
_ELF_DEFINE_EM(EM_MIPS_RS3_LE,      10, "MIPS RS3000 Little-endian")	\
+
_ELF_DEFINE_EM(EM_PARISC,           15, "Hewlett-Packard PA-RISC")	\
+
_ELF_DEFINE_EM(EM_VPP500,           17, "Fujitsu VPP500")		\
+
_ELF_DEFINE_EM(EM_SPARC32PLUS,      18,					\
+
	"Enhanced instruction set SPARC")				\
+
_ELF_DEFINE_EM(EM_960,              19, "Intel 80960")			\
+
_ELF_DEFINE_EM(EM_PPC,              20, "PowerPC")			\
+
_ELF_DEFINE_EM(EM_PPC64,            21, "64-bit PowerPC")		\
+
_ELF_DEFINE_EM(EM_S390,             22, "IBM System/390 Processor")	\
+
_ELF_DEFINE_EM(EM_SPU,              23, "IBM SPU/SPC")			\
+
_ELF_DEFINE_EM(EM_V800,             36, "NEC V800")			\
+
_ELF_DEFINE_EM(EM_FR20,             37, "Fujitsu FR20")			\
+
_ELF_DEFINE_EM(EM_RH32,             38, "TRW RH-32")			\
+
_ELF_DEFINE_EM(EM_RCE,              39, "Motorola RCE")			\
+
_ELF_DEFINE_EM(EM_ARM,              40, "Advanced RISC Machines ARM")	\
+
_ELF_DEFINE_EM(EM_ALPHA,            41, "Digital Alpha")		\
+
_ELF_DEFINE_EM(EM_SH,               42, "Hitachi SH")			\
+
_ELF_DEFINE_EM(EM_SPARCV9,          43, "SPARC Version 9")		\
+
_ELF_DEFINE_EM(EM_TRICORE,          44,					\
+
	"Siemens TriCore embedded processor")				\
+
_ELF_DEFINE_EM(EM_ARC,              45,					\
+
	"Argonaut RISC Core, Argonaut Technologies Inc.")		\
+
_ELF_DEFINE_EM(EM_H8_300,           46, "Hitachi H8/300")		\
+
_ELF_DEFINE_EM(EM_H8_300H,          47, "Hitachi H8/300H")		\
+
_ELF_DEFINE_EM(EM_H8S,              48, "Hitachi H8S")			\
+
_ELF_DEFINE_EM(EM_H8_500,           49, "Hitachi H8/500")		\
+
_ELF_DEFINE_EM(EM_IA_64,            50,					\
+
	"Intel IA-64 processor architecture")				\
+
_ELF_DEFINE_EM(EM_MIPS_X,           51, "Stanford MIPS-X")		\
+
_ELF_DEFINE_EM(EM_COLDFIRE,         52, "Motorola ColdFire")		\
+
_ELF_DEFINE_EM(EM_68HC12,           53, "Motorola M68HC12")		\
+
_ELF_DEFINE_EM(EM_MMA,              54,					\
+
	"Fujitsu MMA Multimedia Accelerator")				\
+
_ELF_DEFINE_EM(EM_PCP,              55, "Siemens PCP")			\
+
_ELF_DEFINE_EM(EM_NCPU,             56,					\
+
	"Sony nCPU embedded RISC processor")				\
+
_ELF_DEFINE_EM(EM_NDR1,             57, "Denso NDR1 microprocessor")	\
+
_ELF_DEFINE_EM(EM_STARCORE,         58, "Motorola Star*Core processor")	\
+
_ELF_DEFINE_EM(EM_ME16,             59, "Toyota ME16 processor")	\
+
_ELF_DEFINE_EM(EM_ST100,            60,					\
+
	"STMicroelectronics ST100 processor")				\
+
_ELF_DEFINE_EM(EM_TINYJ,            61,					\
+
	"Advanced Logic Corp. TinyJ embedded processor family")		\
+
_ELF_DEFINE_EM(EM_X86_64,           62, "AMD x86-64 architecture")	\
+
_ELF_DEFINE_EM(EM_PDSP,             63, "Sony DSP Processor")		\
+
_ELF_DEFINE_EM(EM_PDP10,            64,					\
+
	"Digital Equipment Corp. PDP-10")				\
+
_ELF_DEFINE_EM(EM_PDP11,            65,					\
+
	"Digital Equipment Corp. PDP-11")				\
+
_ELF_DEFINE_EM(EM_FX66,             66, "Siemens FX66 microcontroller")	\
+
_ELF_DEFINE_EM(EM_ST9PLUS,          67,					\
+
	"STMicroelectronics ST9+ 8/16 bit microcontroller")		\
+
_ELF_DEFINE_EM(EM_ST7,              68,					\
+
	"STMicroelectronics ST7 8-bit microcontroller")			\
+
_ELF_DEFINE_EM(EM_68HC16,           69,					\
+
	"Motorola MC68HC16 Microcontroller")				\
+
_ELF_DEFINE_EM(EM_68HC11,           70,					\
+
	"Motorola MC68HC11 Microcontroller")				\
+
_ELF_DEFINE_EM(EM_68HC08,           71,					\
+
	"Motorola MC68HC08 Microcontroller")				\
+
_ELF_DEFINE_EM(EM_68HC05,           72,					\
+
	"Motorola MC68HC05 Microcontroller")				\
+
_ELF_DEFINE_EM(EM_SVX,              73, "Silicon Graphics SVx")		\
+
_ELF_DEFINE_EM(EM_ST19,             74,					\
+
	"STMicroelectronics ST19 8-bit microcontroller")		\
+
_ELF_DEFINE_EM(EM_VAX,              75, "Digital VAX")			\
+
_ELF_DEFINE_EM(EM_CRIS,             76,					\
+
	"Axis Communications 32-bit embedded processor")		\
+
_ELF_DEFINE_EM(EM_JAVELIN,          77,					\
+
	"Infineon Technologies 32-bit embedded processor")		\
+
_ELF_DEFINE_EM(EM_FIREPATH,         78,					\
+
	"Element 14 64-bit DSP Processor")				\
+
_ELF_DEFINE_EM(EM_ZSP,              79,					\
+
	"LSI Logic 16-bit DSP Processor")				\
+
_ELF_DEFINE_EM(EM_MMIX,             80,					\
+
	"Donald Knuth's educational 64-bit processor")			\
+
_ELF_DEFINE_EM(EM_HUANY,            81,					\
+
	"Harvard University machine-independent object files")		\
+
_ELF_DEFINE_EM(EM_PRISM,            82, "SiTera Prism")			\
+
_ELF_DEFINE_EM(EM_AVR,              83,					\
+
	"Atmel AVR 8-bit microcontroller")				\
+
_ELF_DEFINE_EM(EM_FR30,             84, "Fujitsu FR30")			\
+
_ELF_DEFINE_EM(EM_D10V,             85, "Mitsubishi D10V")		\
+
_ELF_DEFINE_EM(EM_D30V,             86, "Mitsubishi D30V")		\
+
_ELF_DEFINE_EM(EM_V850,             87, "NEC v850")			\
+
_ELF_DEFINE_EM(EM_M32R,             88, "Mitsubishi M32R")		\
+
_ELF_DEFINE_EM(EM_MN10300,          89, "Matsushita MN10300")		\
+
_ELF_DEFINE_EM(EM_MN10200,          90, "Matsushita MN10200")		\
+
_ELF_DEFINE_EM(EM_PJ,               91, "picoJava")			\
+
_ELF_DEFINE_EM(EM_OPENRISC,         92,					\
+
	"OpenRISC 32-bit embedded processor")				\
+
_ELF_DEFINE_EM(EM_ARC_COMPACT,      93,					\
+
	"ARC International ARCompact processor")			\
+
_ELF_DEFINE_EM(EM_XTENSA,           94,					\
+
	"Tensilica Xtensa Architecture")				\
+
_ELF_DEFINE_EM(EM_VIDEOCORE,        95,					\
+
	"Alphamosaic VideoCore processor")				\
+
_ELF_DEFINE_EM(EM_TMM_GPP,          96,					\
+
	"Thompson Multimedia General Purpose Processor")		\
+
_ELF_DEFINE_EM(EM_NS32K,            97,					\
+
	"National Semiconductor 32000 series")				\
+
_ELF_DEFINE_EM(EM_TPC,              98, "Tenor Network TPC processor")	\
+
_ELF_DEFINE_EM(EM_SNP1K,            99, "Trebia SNP 1000 processor")	\
+
_ELF_DEFINE_EM(EM_ST200,            100,				\
+
	"STMicroelectronics (www.st.com) ST200 microcontroller")	\
+
_ELF_DEFINE_EM(EM_IP2K,             101,				\
+
	"Ubicom IP2xxx microcontroller family")				\
+
_ELF_DEFINE_EM(EM_MAX,              102, "MAX Processor")		\
+
_ELF_DEFINE_EM(EM_CR,               103,				\
+
	"National Semiconductor CompactRISC microprocessor")		\
+
_ELF_DEFINE_EM(EM_F2MC16,           104, "Fujitsu F2MC16")		\
+
_ELF_DEFINE_EM(EM_MSP430,           105,				\
+
	"Texas Instruments embedded microcontroller msp430")		\
+
_ELF_DEFINE_EM(EM_BLACKFIN,         106,				\
+
	"Analog Devices Blackfin (DSP) processor")			\
+
_ELF_DEFINE_EM(EM_SE_C33,           107,				\
+
	"S1C33 Family of Seiko Epson processors")			\
+
_ELF_DEFINE_EM(EM_SEP,              108,				\
+
	"Sharp embedded microprocessor")				\
+
_ELF_DEFINE_EM(EM_ARCA,             109, "Arca RISC Microprocessor")	\
+
_ELF_DEFINE_EM(EM_UNICORE,          110,				\
+
	"Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University") \
+
_ELF_DEFINE_EM(EM_EXCESS,           111,				\
+
	"eXcess: 16/32/64-bit configurable embedded CPU")		\
+
_ELF_DEFINE_EM(EM_DXP,              112,				\
+
	"Icera Semiconductor Inc. Deep Execution Processor")		\
+
_ELF_DEFINE_EM(EM_ALTERA_NIOS2,     113,				\
+
	"Altera Nios II soft-core processor")				\
+
_ELF_DEFINE_EM(EM_CRX,              114,				\
+
	"National Semiconductor CompactRISC CRX microprocessor")	\
+
_ELF_DEFINE_EM(EM_XGATE,            115,				\
+
	"Motorola XGATE embedded processor")				\
+
_ELF_DEFINE_EM(EM_C166,             116,				\
+
	"Infineon C16x/XC16x processor")				\
+
_ELF_DEFINE_EM(EM_M16C,             117,				\
+
	"Renesas M16C series microprocessors")				\
+
_ELF_DEFINE_EM(EM_DSPIC30F,         118,				\
+
	"Microchip Technology dsPIC30F Digital Signal Controller")	\
+
_ELF_DEFINE_EM(EM_CE,               119,				\
+
	"Freescale Communication Engine RISC core")			\
+
_ELF_DEFINE_EM(EM_M32C,             120,				\
+
	"Renesas M32C series microprocessors")				\
+
_ELF_DEFINE_EM(EM_TSK3000,          131, "Altium TSK3000 core")		\
+
_ELF_DEFINE_EM(EM_RS08,             132,				\
+
	"Freescale RS08 embedded processor")				\
+
_ELF_DEFINE_EM(EM_SHARC,            133,				\
+
	"Analog Devices SHARC family of 32-bit DSP processors")		\
+
_ELF_DEFINE_EM(EM_ECOG2,            134,				\
+
	"Cyan Technology eCOG2 microprocessor")				\
+
_ELF_DEFINE_EM(EM_SCORE7,           135,				\
+
	"Sunplus S+core7 RISC processor")				\
+
_ELF_DEFINE_EM(EM_DSP24,            136,				\
+
	"New Japan Radio (NJR) 24-bit DSP Processor")			\
+
_ELF_DEFINE_EM(EM_VIDEOCORE3,       137,				\
+
	"Broadcom VideoCore III processor")				\
+
_ELF_DEFINE_EM(EM_LATTICEMICO32,    138,				\
+
	"RISC processor for Lattice FPGA architecture")			\
+
_ELF_DEFINE_EM(EM_SE_C17,           139, "Seiko Epson C17 family")	\
+
_ELF_DEFINE_EM(EM_TI_C6000,         140,				\
+
	"The Texas Instruments TMS320C6000 DSP family")			\
+
_ELF_DEFINE_EM(EM_TI_C2000,         141,				\
+
	"The Texas Instruments TMS320C2000 DSP family")			\
+
_ELF_DEFINE_EM(EM_TI_C5500,         142,				\
+
	"The Texas Instruments TMS320C55x DSP family")			\
+
_ELF_DEFINE_EM(EM_MMDSP_PLUS,       160,				\
+
	"STMicroelectronics 64bit VLIW Data Signal Processor")		\
+
_ELF_DEFINE_EM(EM_CYPRESS_M8C,      161, "Cypress M8C microprocessor")	\
+
_ELF_DEFINE_EM(EM_R32C,             162,				\
+
	"Renesas R32C series microprocessors")				\
+
_ELF_DEFINE_EM(EM_TRIMEDIA,         163,				\
+
	"NXP Semiconductors TriMedia architecture family")		\
+
_ELF_DEFINE_EM(EM_QDSP6,            164, "QUALCOMM DSP6 Processor")	\
+
_ELF_DEFINE_EM(EM_8051,             165, "Intel 8051 and variants")	\
+
_ELF_DEFINE_EM(EM_STXP7X,           166,				\
+
	"STMicroelectronics STxP7x family of configurable and extensible RISC processors") \
+
_ELF_DEFINE_EM(EM_NDS32,            167,				\
+
	"Andes Technology compact code size embedded RISC processor family") \
+
_ELF_DEFINE_EM(EM_ECOG1,            168,				\
+
	"Cyan Technology eCOG1X family")				\
+
_ELF_DEFINE_EM(EM_ECOG1X,           168,				\
+
	"Cyan Technology eCOG1X family")				\
+
_ELF_DEFINE_EM(EM_MAXQ30,           169,				\
+
	"Dallas Semiconductor MAXQ30 Core Micro-controllers")		\
+
_ELF_DEFINE_EM(EM_XIMO16,           170,				\
+
	"New Japan Radio (NJR) 16-bit DSP Processor")			\
+
_ELF_DEFINE_EM(EM_MANIK,            171,				\
+
	"M2000 Reconfigurable RISC Microprocessor")			\
+
_ELF_DEFINE_EM(EM_CRAYNV2,          172,				\
+
	"Cray Inc. NV2 vector architecture")				\
+
_ELF_DEFINE_EM(EM_RX,               173, "Renesas RX family")		\
+
_ELF_DEFINE_EM(EM_METAG,            174,				\
+
	"Imagination Technologies META processor architecture")		\
+
_ELF_DEFINE_EM(EM_MCST_ELBRUS,      175,				\
+
	"MCST Elbrus general purpose hardware architecture")		\
+
_ELF_DEFINE_EM(EM_ECOG16,           176,				\
+
	"Cyan Technology eCOG16 family")				\
+
_ELF_DEFINE_EM(EM_CR16,             177,				\
+
	"National Semiconductor CompactRISC CR16 16-bit microprocessor") \
+
_ELF_DEFINE_EM(EM_ETPU,             178,				\
+
	"Freescale Extended Time Processing Unit")			\
+
_ELF_DEFINE_EM(EM_SLE9X,            179,				\
+
	"Infineon Technologies SLE9X core")				\
+
_ELF_DEFINE_EM(EM_AARCH64,          183,				\
+
	"AArch64 (64-bit ARM)")						\
+
_ELF_DEFINE_EM(EM_AVR32,            185,				\
+
	"Atmel Corporation 32-bit microprocessor family")		\
+
_ELF_DEFINE_EM(EM_STM8,             186,				\
+
	"STMicroeletronics STM8 8-bit microcontroller")			\
+
_ELF_DEFINE_EM(EM_TILE64,           187,				\
+
	"Tilera TILE64 multicore architecture family")			\
+
_ELF_DEFINE_EM(EM_TILEPRO,          188,				\
+
	"Tilera TILEPro multicore architecture family")			\
+
_ELF_DEFINE_EM(EM_MICROBLAZE,       189,				\
+
	"Xilinx MicroBlaze 32-bit RISC soft processor core")		\
+
_ELF_DEFINE_EM(EM_CUDA,             190, "NVIDIA CUDA architecture")	\
+
_ELF_DEFINE_EM(EM_TILEGX,           191,				\
+
	"Tilera TILE-Gx multicore architecture family")			\
+
_ELF_DEFINE_EM(EM_CLOUDSHIELD,      192,				\
+
	"CloudShield architecture family")				\
+
_ELF_DEFINE_EM(EM_COREA_1ST,        193,				\
+
	"KIPO-KAIST Core-A 1st generation processor family")		\
+
_ELF_DEFINE_EM(EM_COREA_2ND,        194,				\
+
	"KIPO-KAIST Core-A 2nd generation processor family")		\
+
_ELF_DEFINE_EM(EM_ARC_COMPACT2,     195, "Synopsys ARCompact V2")	\
+
_ELF_DEFINE_EM(EM_OPEN8,            196,				\
+
	"Open8 8-bit RISC soft processor core")				\
+
_ELF_DEFINE_EM(EM_RL78,             197, "Renesas RL78 family")		\
+
_ELF_DEFINE_EM(EM_VIDEOCORE5,       198, "Broadcom VideoCore V processor") \
+
_ELF_DEFINE_EM(EM_78KOR,            199, "Renesas 78KOR family")	\
+
_ELF_DEFINE_EM(EM_56800EX,          200,				\
+
	"Freescale 56800EX Digital Signal Controller")			\
+
_ELF_DEFINE_EM(EM_BA1,              201, "Beyond BA1 CPU architecture")	\
+
_ELF_DEFINE_EM(EM_BA2,              202, "Beyond BA2 CPU architecture")	\
+
_ELF_DEFINE_EM(EM_XCORE,            203, "XMOS xCORE processor family") \
+
_ELF_DEFINE_EM(EM_MCHP_PIC,         204, "Microchip 8-bit PIC(r) family") \
+
_ELF_DEFINE_EM(EM_INTEL205,         205, "Reserved by Intel")           \
+
_ELF_DEFINE_EM(EM_INTEL206,         206, "Reserved by Intel")           \
+
_ELF_DEFINE_EM(EM_INTEL207,         207, "Reserved by Intel")           \
+
_ELF_DEFINE_EM(EM_INTEL208,         208, "Reserved by Intel")           \
+
_ELF_DEFINE_EM(EM_INTEL209,         209, "Reserved by Intel")           \
+
_ELF_DEFINE_EM(EM_KM32,             210, "KM211 KM32 32-bit processor") \
+
_ELF_DEFINE_EM(EM_KMX32,            211, "KM211 KMX32 32-bit processor") \
+
_ELF_DEFINE_EM(EM_KMX16,            212, "KM211 KMX16 16-bit processor") \
+
_ELF_DEFINE_EM(EM_KMX8,             213, "KM211 KMX8 8-bit processor")  \
+
_ELF_DEFINE_EM(EM_KVARC,            214, "KM211 KMX32 KVARC processor") \
+
_ELF_DEFINE_EM(EM_RISCV,            243, "RISC-V")
+

+
#undef	_ELF_DEFINE_EM
+
#define	_ELF_DEFINE_EM(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_ELF_MACHINES()
+
	EM__LAST__
+
};
+

+
/* Other synonyms. */
+
#define	EM_AMD64		EM_X86_64
+
#define	EM_ARC_A5		EM_ARC_COMPACT
+

+
/*
+
 * ELF file types: (ET_*).
+
 */
+
#define	_ELF_DEFINE_ELF_TYPES()						\
+
_ELF_DEFINE_ET(ET_NONE,   0,	    "No file type")			\
+
_ELF_DEFINE_ET(ET_REL,    1, 	    "Relocatable object")		\
+
_ELF_DEFINE_ET(ET_EXEC,   2, 	    "Executable")			\
+
_ELF_DEFINE_ET(ET_DYN,    3, 	    "Shared object")			\
+
_ELF_DEFINE_ET(ET_CORE,   4, 	    "Core file")			\
+
_ELF_DEFINE_ET(ET_LOOS,   0xFE00U,  "Begin OS-specific range")		\
+
_ELF_DEFINE_ET(ET_HIOS,   0xFEFFU,  "End OS-specific range")		\
+
_ELF_DEFINE_ET(ET_LOPROC, 0xFF00U,  "Begin processor-specific range")	\
+
_ELF_DEFINE_ET(ET_HIPROC, 0xFFFFU,  "End processor-specific range")
+

+
#undef	_ELF_DEFINE_ET
+
#define	_ELF_DEFINE_ET(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_ELF_TYPES()
+
	ET__LAST__
+
};
+

+
/* ELF file format version numbers. */
+
#define	EV_NONE		0
+
#define	EV_CURRENT	1
+

+
/*
+
 * Flags for section groups.
+
 */
+
#define	GRP_COMDAT 	0x1		/* COMDAT semantics */
+
#define	GRP_MASKOS 	0x0ff00000	/* OS-specific flags */
+
#define	GRP_MASKPROC 	0xf0000000	/* processor-specific flags */
+

+
/*
+
 * Flags / mask for .gnu.versym sections.
+
 */
+
#define	VERSYM_VERSION	0x7fff
+
#define	VERSYM_HIDDEN	0x8000
+

+
/*
+
 * Flags used by program header table entries.
+
 */
+

+
#define	_ELF_DEFINE_PHDR_FLAGS()					\
+
_ELF_DEFINE_PF(PF_X,                0x1, "Execute")			\
+
_ELF_DEFINE_PF(PF_W,                0x2, "Write")			\
+
_ELF_DEFINE_PF(PF_R,                0x4, "Read")			\
+
_ELF_DEFINE_PF(PF_MASKOS,           0x0ff00000, "OS-specific flags")	\
+
_ELF_DEFINE_PF(PF_MASKPROC,         0xf0000000, "Processor-specific flags") \
+
_ELF_DEFINE_PF(PF_ARM_SB,           0x10000000,				\
+
	"segment contains the location addressed by the static base")	\
+
_ELF_DEFINE_PF(PF_ARM_PI,           0x20000000,				\
+
	"segment is position-independent")				\
+
_ELF_DEFINE_PF(PF_ARM_ABS,          0x40000000,				\
+
	"segment must be loaded at its base address")
+

+
#undef	_ELF_DEFINE_PF
+
#define	_ELF_DEFINE_PF(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_PHDR_FLAGS()
+
	PF__LAST__
+
};
+

+
/*
+
 * Types of program header table entries.
+
 */
+

+
#define	_ELF_DEFINE_PHDR_TYPES()				\
+
_ELF_DEFINE_PT(PT_NULL,             0, "ignored entry")		\
+
_ELF_DEFINE_PT(PT_LOAD,             1, "loadable segment")	\
+
_ELF_DEFINE_PT(PT_DYNAMIC,          2,				\
+
	"contains dynamic linking information")			\
+
_ELF_DEFINE_PT(PT_INTERP,           3, "names an interpreter")	\
+
_ELF_DEFINE_PT(PT_NOTE,             4, "auxiliary information")	\
+
_ELF_DEFINE_PT(PT_SHLIB,            5, "reserved")		\
+
_ELF_DEFINE_PT(PT_PHDR,             6,				\
+
	"describes the program header itself")			\
+
_ELF_DEFINE_PT(PT_TLS,              7, "thread local storage")	\
+
_ELF_DEFINE_PT(PT_LOOS,             0x60000000UL,		\
+
	"start of OS-specific range")				\
+
_ELF_DEFINE_PT(PT_SUNW_UNWIND,      0x6464E550UL,		\
+
	"Solaris/amd64 stack unwind tables")			\
+
_ELF_DEFINE_PT(PT_GNU_EH_FRAME,     0x6474E550UL,		\
+
	"GCC generated .eh_frame_hdr segment")			\
+
_ELF_DEFINE_PT(PT_GNU_STACK,	    0x6474E551UL,		\
+
	"Stack flags")						\
+
_ELF_DEFINE_PT(PT_GNU_RELRO,	    0x6474E552UL,		\
+
	"Segment becomes read-only after relocation")		\
+
_ELF_DEFINE_PT(PT_SUNWBSS,          0x6FFFFFFAUL,		\
+
	"A Solaris .SUNW_bss section")				\
+
_ELF_DEFINE_PT(PT_SUNWSTACK,        0x6FFFFFFBUL,		\
+
	"A Solaris process stack")				\
+
_ELF_DEFINE_PT(PT_SUNWDTRACE,       0x6FFFFFFCUL,		\
+
	"Used by dtrace(1)")					\
+
_ELF_DEFINE_PT(PT_SUNWCAP,          0x6FFFFFFDUL,		\
+
	"Special hardware capability requirements")		\
+
_ELF_DEFINE_PT(PT_HIOS,             0x6FFFFFFFUL,		\
+
	"end of OS-specific range")				\
+
_ELF_DEFINE_PT(PT_LOPROC,           0x70000000UL,		\
+
	"start of processor-specific range")			\
+
_ELF_DEFINE_PT(PT_ARM_ARCHEXT,      0x70000000UL,		\
+
	"platform architecture compatibility information")	\
+
_ELF_DEFINE_PT(PT_ARM_EXIDX,        0x70000001UL,		\
+
	"exception unwind tables")				\
+
_ELF_DEFINE_PT(PT_MIPS_REGINFO,     0x70000000UL,		\
+
	"register usage information")				\
+
_ELF_DEFINE_PT(PT_MIPS_RTPROC,      0x70000001UL,		\
+
	"runtime procedure table")				\
+
_ELF_DEFINE_PT(PT_MIPS_OPTIONS,     0x70000002UL,		\
+
	"options segment")					\
+
_ELF_DEFINE_PT(PT_HIPROC,           0x7FFFFFFFUL,		\
+
	"end of processor-specific range")
+

+
#undef	_ELF_DEFINE_PT
+
#define	_ELF_DEFINE_PT(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_PHDR_TYPES()
+
	PT__LAST__ = PT_HIPROC
+
};
+

+
/* synonyms. */
+
#define	PT_ARM_UNWIND	PT_ARM_EXIDX
+
#define	PT_HISUNW	PT_HIOS
+
#define	PT_LOSUNW	PT_SUNWBSS
+

+
/*
+
 * Section flags.
+
 */
+

+
#define	_ELF_DEFINE_SECTION_FLAGS()					\
+
_ELF_DEFINE_SHF(SHF_WRITE,           0x1,				\
+
	"writable during program execution")				\
+
_ELF_DEFINE_SHF(SHF_ALLOC,           0x2,				\
+
	"occupies memory during program execution")			\
+
_ELF_DEFINE_SHF(SHF_EXECINSTR,       0x4, "executable instructions")	\
+
_ELF_DEFINE_SHF(SHF_MERGE,           0x10,				\
+
	"may be merged to prevent duplication")				\
+
_ELF_DEFINE_SHF(SHF_STRINGS,         0x20,				\
+
	"NUL-terminated character strings")				\
+
_ELF_DEFINE_SHF(SHF_INFO_LINK,       0x40,				\
+
	"the sh_info field holds a link")				\
+
_ELF_DEFINE_SHF(SHF_LINK_ORDER,      0x80,				\
+
	"special ordering requirements during linking")			\
+
_ELF_DEFINE_SHF(SHF_OS_NONCONFORMING, 0x100,				\
+
	"requires OS-specific processing during linking")		\
+
_ELF_DEFINE_SHF(SHF_GROUP,           0x200,				\
+
	"member of a section group")					\
+
_ELF_DEFINE_SHF(SHF_TLS,             0x400,				\
+
	"holds thread-local storage")					\
+
_ELF_DEFINE_SHF(SHF_COMPRESSED,      0x800,				\
+
	"holds compressed data")					\
+
_ELF_DEFINE_SHF(SHF_MASKOS,          0x0FF00000UL,			\
+
	"bits reserved for OS-specific semantics")			\
+
_ELF_DEFINE_SHF(SHF_AMD64_LARGE,     0x10000000UL,			\
+
	"section uses large code model")				\
+
_ELF_DEFINE_SHF(SHF_ENTRYSECT,       0x10000000UL,			\
+
	"section contains an entry point (ARM)")			\
+
_ELF_DEFINE_SHF(SHF_COMDEF,          0x80000000UL,			\
+
	"section may be multiply defined in input to link step (ARM)")	\
+
_ELF_DEFINE_SHF(SHF_MIPS_GPREL,      0x10000000UL,			\
+
	"section must be part of global data area")			\
+
_ELF_DEFINE_SHF(SHF_MIPS_MERGE,      0x20000000UL,			\
+
	"section data should be merged to eliminate duplication")	\
+
_ELF_DEFINE_SHF(SHF_MIPS_ADDR,       0x40000000UL,			\
+
	"section data is addressed by default")				\
+
_ELF_DEFINE_SHF(SHF_MIPS_STRING,     0x80000000UL,			\
+
	"section data is string data by default")			\
+
_ELF_DEFINE_SHF(SHF_MIPS_NOSTRIP,    0x08000000UL,			\
+
	"section data may not be stripped")				\
+
_ELF_DEFINE_SHF(SHF_MIPS_LOCAL,      0x04000000UL,			\
+
	"section data local to process")				\
+
_ELF_DEFINE_SHF(SHF_MIPS_NAMES,      0x02000000UL,			\
+
	"linker must generate implicit hidden weak names")		\
+
_ELF_DEFINE_SHF(SHF_MIPS_NODUPE,     0x01000000UL,			\
+
	"linker must retain only one copy")				\
+
_ELF_DEFINE_SHF(SHF_ORDERED,         0x40000000UL,			\
+
	"section is ordered with respect to other sections")		\
+
_ELF_DEFINE_SHF(SHF_EXCLUDE,	     0x80000000UL,			\
+
	"section is excluded from executables and shared objects")	\
+
_ELF_DEFINE_SHF(SHF_MASKPROC,        0xF0000000UL,			\
+
	"bits reserved for processor-specific semantics")
+

+
#undef	_ELF_DEFINE_SHF
+
#define	_ELF_DEFINE_SHF(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_SECTION_FLAGS()
+
	SHF__LAST__
+
};
+

+
/*
+
 * Special section indices.
+
 */
+
#define _ELF_DEFINE_SECTION_INDICES()					\
+
_ELF_DEFINE_SHN(SHN_UNDEF, 	0, 	 "undefined section")		\
+
_ELF_DEFINE_SHN(SHN_LORESERVE, 	0xFF00U, "start of reserved area")	\
+
_ELF_DEFINE_SHN(SHN_LOPROC, 	0xFF00U,				\
+
	"start of processor-specific range")				\
+
_ELF_DEFINE_SHN(SHN_BEFORE,	0xFF00U, "used for section ordering")	\
+
_ELF_DEFINE_SHN(SHN_AFTER,	0xFF01U, "used for section ordering")	\
+
_ELF_DEFINE_SHN(SHN_AMD64_LCOMMON, 0xFF02U, "large common block label") \
+
_ELF_DEFINE_SHN(SHN_MIPS_ACOMMON, 0xFF00U,				\
+
	"allocated common symbols in a DSO")				\
+
_ELF_DEFINE_SHN(SHN_MIPS_TEXT,	0xFF01U, "Reserved (obsolete)")		\
+
_ELF_DEFINE_SHN(SHN_MIPS_DATA,	0xFF02U, "Reserved (obsolete)")		\
+
_ELF_DEFINE_SHN(SHN_MIPS_SCOMMON, 0xFF03U,				\
+
	"gp-addressable common symbols")				\
+
_ELF_DEFINE_SHN(SHN_MIPS_SUNDEFINED, 0xFF04U,				\
+
	"gp-addressable undefined symbols")				\
+
_ELF_DEFINE_SHN(SHN_MIPS_LCOMMON, 0xFF05U, "local common symbols")	\
+
_ELF_DEFINE_SHN(SHN_MIPS_LUNDEFINED, 0xFF06U,				\
+
	"local undefined symbols")					\
+
_ELF_DEFINE_SHN(SHN_HIPROC, 	0xFF1FU,				\
+
	"end of processor-specific range")				\
+
_ELF_DEFINE_SHN(SHN_LOOS, 	0xFF20U,				\
+
	"start of OS-specific range")					\
+
_ELF_DEFINE_SHN(SHN_SUNW_IGNORE, 0xFF3FU, "used by dtrace")		\
+
_ELF_DEFINE_SHN(SHN_HIOS, 	0xFF3FU,				\
+
	"end of OS-specific range")					\
+
_ELF_DEFINE_SHN(SHN_ABS, 	0xFFF1U, "absolute references")		\
+
_ELF_DEFINE_SHN(SHN_COMMON, 	0xFFF2U, "references to COMMON areas")	\
+
_ELF_DEFINE_SHN(SHN_XINDEX, 	0xFFFFU, "extended index")		\
+
_ELF_DEFINE_SHN(SHN_HIRESERVE, 	0xFFFFU, "end of reserved area")
+

+
#undef	_ELF_DEFINE_SHN
+
#define	_ELF_DEFINE_SHN(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_SECTION_INDICES()
+
	SHN__LAST__
+
};
+

+
/*
+
 * Section types.
+
 */
+

+
#define	_ELF_DEFINE_SECTION_TYPES()					\
+
_ELF_DEFINE_SHT(SHT_NULL,            0, "inactive header")		\
+
_ELF_DEFINE_SHT(SHT_PROGBITS,        1, "program defined information")	\
+
_ELF_DEFINE_SHT(SHT_SYMTAB,          2, "symbol table")			\
+
_ELF_DEFINE_SHT(SHT_STRTAB,          3, "string table")			\
+
_ELF_DEFINE_SHT(SHT_RELA,            4,					\
+
	"relocation entries with addends")				\
+
_ELF_DEFINE_SHT(SHT_HASH,            5, "symbol hash table")		\
+
_ELF_DEFINE_SHT(SHT_DYNAMIC,         6,					\
+
	"information for dynamic linking")				\
+
_ELF_DEFINE_SHT(SHT_NOTE,            7, "additional notes")		\
+
_ELF_DEFINE_SHT(SHT_NOBITS,          8, "section occupying no space")	\
+
_ELF_DEFINE_SHT(SHT_REL,             9,					\
+
	"relocation entries without addends")				\
+
_ELF_DEFINE_SHT(SHT_SHLIB,           10, "reserved")			\
+
_ELF_DEFINE_SHT(SHT_DYNSYM,          11, "symbol table")		\
+
_ELF_DEFINE_SHT(SHT_INIT_ARRAY,      14,				\
+
	"pointers to initialization functions")				\
+
_ELF_DEFINE_SHT(SHT_FINI_ARRAY,      15,				\
+
	"pointers to termination functions")				\
+
_ELF_DEFINE_SHT(SHT_PREINIT_ARRAY,   16,				\
+
	"pointers to functions called before initialization")		\
+
_ELF_DEFINE_SHT(SHT_GROUP,           17, "defines a section group")	\
+
_ELF_DEFINE_SHT(SHT_SYMTAB_SHNDX,    18,				\
+
	"used for extended section numbering")				\
+
_ELF_DEFINE_SHT(SHT_LOOS,            0x60000000UL,			\
+
	"start of OS-specific range")					\
+
_ELF_DEFINE_SHT(SHT_SUNW_dof,	     0x6FFFFFF4UL,			\
+
	"used by dtrace")						\
+
_ELF_DEFINE_SHT(SHT_SUNW_cap,	     0x6FFFFFF5UL,			\
+
	"capability requirements")					\
+
_ELF_DEFINE_SHT(SHT_GNU_ATTRIBUTES,  0x6FFFFFF5UL,			\
+
	"object attributes")						\
+
_ELF_DEFINE_SHT(SHT_SUNW_SIGNATURE,  0x6FFFFFF6UL,			\
+
	"module verification signature")				\
+
_ELF_DEFINE_SHT(SHT_GNU_HASH,	     0x6FFFFFF6UL,			\
+
	"GNU Hash sections")						\
+
_ELF_DEFINE_SHT(SHT_GNU_LIBLIST,     0x6FFFFFF7UL,			\
+
	"List of libraries to be prelinked")				\
+
_ELF_DEFINE_SHT(SHT_SUNW_ANNOTATE,   0x6FFFFFF7UL,			\
+
	"special section where unresolved references are allowed")	\
+
_ELF_DEFINE_SHT(SHT_SUNW_DEBUGSTR,   0x6FFFFFF8UL,			\
+
	"debugging information")					\
+
_ELF_DEFINE_SHT(SHT_CHECKSUM, 	     0x6FFFFFF8UL,			\
+
	"checksum for dynamic shared objects")				\
+
_ELF_DEFINE_SHT(SHT_SUNW_DEBUG,      0x6FFFFFF9UL,			\
+
	"debugging information")					\
+
_ELF_DEFINE_SHT(SHT_SUNW_move,       0x6FFFFFFAUL,			\
+
	"information to handle partially initialized symbols")		\
+
_ELF_DEFINE_SHT(SHT_SUNW_COMDAT,     0x6FFFFFFBUL,			\
+
	"section supporting merging of multiple copies of data")	\
+
_ELF_DEFINE_SHT(SHT_SUNW_syminfo,    0x6FFFFFFCUL,			\
+
	"additional symbol information")				\
+
_ELF_DEFINE_SHT(SHT_SUNW_verdef,     0x6FFFFFFDUL,			\
+
	"symbol versioning information")				\
+
_ELF_DEFINE_SHT(SHT_SUNW_verneed,    0x6FFFFFFEUL,			\
+
	"symbol versioning requirements")				\
+
_ELF_DEFINE_SHT(SHT_SUNW_versym,     0x6FFFFFFFUL,			\
+
	"symbol versioning table")					\
+
_ELF_DEFINE_SHT(SHT_HIOS,            0x6FFFFFFFUL,			\
+
	"end of OS-specific range")					\
+
_ELF_DEFINE_SHT(SHT_LOPROC,          0x70000000UL,			\
+
	"start of processor-specific range")				\
+
_ELF_DEFINE_SHT(SHT_ARM_EXIDX,       0x70000001UL,			\
+
	"exception index table")					\
+
_ELF_DEFINE_SHT(SHT_ARM_PREEMPTMAP,  0x70000002UL,			\
+
	"BPABI DLL dynamic linking preemption map")			\
+
_ELF_DEFINE_SHT(SHT_ARM_ATTRIBUTES,  0x70000003UL,			\
+
	"object file compatibility attributes")				\
+
_ELF_DEFINE_SHT(SHT_ARM_DEBUGOVERLAY, 0x70000004UL,			\
+
	"overlay debug information")					\
+
_ELF_DEFINE_SHT(SHT_ARM_OVERLAYSECTION, 0x70000005UL,			\
+
	"overlay debug information")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_LIBLIST,    0x70000000UL,			\
+
	"DSO library information used in link")				\
+
_ELF_DEFINE_SHT(SHT_MIPS_MSYM,       0x70000001UL,			\
+
	"MIPS symbol table extension")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_CONFLICT,   0x70000002UL,			\
+
	"symbol conflicting with DSO-defined symbols ")			\
+
_ELF_DEFINE_SHT(SHT_MIPS_GPTAB,      0x70000003UL,			\
+
	"global pointer table")						\
+
_ELF_DEFINE_SHT(SHT_MIPS_UCODE,      0x70000004UL,			\
+
	"reserved")							\
+
_ELF_DEFINE_SHT(SHT_MIPS_DEBUG,      0x70000005UL,			\
+
	"reserved (obsolete debug information)")			\
+
_ELF_DEFINE_SHT(SHT_MIPS_REGINFO,    0x70000006UL,			\
+
	"register usage information")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_PACKAGE,    0x70000007UL,			\
+
	"OSF reserved")							\
+
_ELF_DEFINE_SHT(SHT_MIPS_PACKSYM,    0x70000008UL,			\
+
	"OSF reserved")							\
+
_ELF_DEFINE_SHT(SHT_MIPS_RELD,       0x70000009UL,			\
+
	"dynamic relocation")						\
+
_ELF_DEFINE_SHT(SHT_MIPS_IFACE,      0x7000000BUL,			\
+
	"subprogram interface information")				\
+
_ELF_DEFINE_SHT(SHT_MIPS_CONTENT,    0x7000000CUL,			\
+
	"section content classification")				\
+
_ELF_DEFINE_SHT(SHT_MIPS_OPTIONS,     0x7000000DUL,			\
+
	"general options")						\
+
_ELF_DEFINE_SHT(SHT_MIPS_DELTASYM,   0x7000001BUL,			\
+
	"Delta C++: symbol table")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_DELTAINST,  0x7000001CUL,			\
+
	"Delta C++: instance table")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_DELTACLASS, 0x7000001DUL,			\
+
	"Delta C++: class table")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_DWARF,      0x7000001EUL,			\
+
	"DWARF debug information")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_DELTADECL,  0x7000001FUL,			\
+
	"Delta C++: declarations")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_SYMBOL_LIB, 0x70000020UL,			\
+
	"symbol-to-library mapping")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_EVENTS,     0x70000021UL,			\
+
	"event locations")						\
+
_ELF_DEFINE_SHT(SHT_MIPS_TRANSLATE,  0x70000022UL,			\
+
	"???")								\
+
_ELF_DEFINE_SHT(SHT_MIPS_PIXIE,      0x70000023UL,			\
+
	"special pixie sections")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_XLATE,      0x70000024UL,			\
+
	"address translation table")					\
+
_ELF_DEFINE_SHT(SHT_MIPS_XLATE_DEBUG, 0x70000025UL,			\
+
	"SGI internal address translation table")			\
+
_ELF_DEFINE_SHT(SHT_MIPS_WHIRL,      0x70000026UL,			\
+
	"intermediate code")						\
+
_ELF_DEFINE_SHT(SHT_MIPS_EH_REGION,  0x70000027UL,			\
+
	"C++ exception handling region info")				\
+
_ELF_DEFINE_SHT(SHT_MIPS_XLATE_OLD,  0x70000028UL,			\
+
	"obsolete")							\
+
_ELF_DEFINE_SHT(SHT_MIPS_PDR_EXCEPTION, 0x70000029UL,			\
+
	"runtime procedure descriptor table exception information")	\
+
_ELF_DEFINE_SHT(SHT_MIPS_ABIFLAGS,   0x7000002AUL,			\
+
	"ABI flags")							\
+
_ELF_DEFINE_SHT(SHT_SPARC_GOTDATA,   0x70000000UL,			\
+
	"SPARC-specific data")						\
+
_ELF_DEFINE_SHT(SHT_AMD64_UNWIND,    0x70000001UL,			\
+
	"unwind tables for the AMD64")					\
+
_ELF_DEFINE_SHT(SHT_ORDERED,         0x7FFFFFFFUL,			\
+
	"sort entries in the section")					\
+
_ELF_DEFINE_SHT(SHT_HIPROC,          0x7FFFFFFFUL,			\
+
	"end of processor-specific range")				\
+
_ELF_DEFINE_SHT(SHT_LOUSER,          0x80000000UL,			\
+
	"start of application-specific range")				\
+
_ELF_DEFINE_SHT(SHT_HIUSER,          0xFFFFFFFFUL,			\
+
	"end of application-specific range")
+

+
#undef	_ELF_DEFINE_SHT
+
#define	_ELF_DEFINE_SHT(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_SECTION_TYPES()
+
	SHT__LAST__ = SHT_HIUSER
+
};
+

+
/* Aliases for section types. */
+
#define	SHT_GNU_verdef		SHT_SUNW_verdef
+
#define	SHT_GNU_verneed		SHT_SUNW_verneed
+
#define	SHT_GNU_versym		SHT_SUNW_versym
+

+
/*
+
 * Symbol binding information.
+
 */
+

+
#define	_ELF_DEFINE_SYMBOL_BINDING()					\
+
_ELF_DEFINE_STB(STB_LOCAL,           0,					\
+
	"not visible outside defining object file")			\
+
_ELF_DEFINE_STB(STB_GLOBAL,          1,					\
+
	"visible across all object files being combined")		\
+
_ELF_DEFINE_STB(STB_WEAK,            2,					\
+
	"visible across all object files but with low precedence")	\
+
_ELF_DEFINE_STB(STB_LOOS,            10, "start of OS-specific range")	\
+
_ELF_DEFINE_STB(STB_GNU_UNIQUE,      10, "unique symbol (GNU)")		\
+
_ELF_DEFINE_STB(STB_HIOS,            12, "end of OS-specific range")	\
+
_ELF_DEFINE_STB(STB_LOPROC,          13,				\
+
	"start of processor-specific range")				\
+
_ELF_DEFINE_STB(STB_HIPROC,          15,				\
+
	"end of processor-specific range")
+

+
#undef	_ELF_DEFINE_STB
+
#define	_ELF_DEFINE_STB(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_SYMBOL_BINDING()
+
	STB__LAST__
+
};
+

+
/*
+
 * Symbol types
+
 */
+

+
#define	_ELF_DEFINE_SYMBOL_TYPES()					\
+
_ELF_DEFINE_STT(STT_NOTYPE,          0, "unspecified type")		\
+
_ELF_DEFINE_STT(STT_OBJECT,          1, "data object")			\
+
_ELF_DEFINE_STT(STT_FUNC,            2, "executable code")		\
+
_ELF_DEFINE_STT(STT_SECTION,         3, "section")			\
+
_ELF_DEFINE_STT(STT_FILE,            4, "source file")			\
+
_ELF_DEFINE_STT(STT_COMMON,          5, "uninitialized common block")	\
+
_ELF_DEFINE_STT(STT_TLS,             6, "thread local storage")		\
+
_ELF_DEFINE_STT(STT_LOOS,            10, "start of OS-specific types")	\
+
_ELF_DEFINE_STT(STT_HIOS,            12, "end of OS-specific types")	\
+
_ELF_DEFINE_STT(STT_LOPROC,          13,				\
+
	"start of processor-specific types")				\
+
_ELF_DEFINE_STT(STT_ARM_TFUNC,       13, "Thumb function (GNU)")	\
+
_ELF_DEFINE_STT(STT_ARM_16BIT,       15, "Thumb label (GNU)")		\
+
_ELF_DEFINE_STT(STT_SPARC_REGISTER,  13, "SPARC register information")	\
+
_ELF_DEFINE_STT(STT_HIPROC,          15,				\
+
	"end of processor-specific types")
+

+
#undef	_ELF_DEFINE_STT
+
#define	_ELF_DEFINE_STT(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_SYMBOL_TYPES()
+
	STT__LAST__
+
};
+

+
/*
+
 * Symbol binding.
+
 */
+

+
#define	_ELF_DEFINE_SYMBOL_BINDING_KINDS()		\
+
_ELF_DEFINE_SYB(SYMINFO_BT_SELF,	0xFFFFU,	\
+
	"bound to self")				\
+
_ELF_DEFINE_SYB(SYMINFO_BT_PARENT,	0xFFFEU,	\
+
	"bound to parent")				\
+
_ELF_DEFINE_SYB(SYMINFO_BT_NONE,	0xFFFDU,	\
+
	"no special binding")
+

+
#undef	_ELF_DEFINE_SYB
+
#define	_ELF_DEFINE_SYB(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_SYMBOL_BINDING_KINDS()
+
	SYMINFO__LAST__
+
};
+

+
/*
+
 * Symbol visibility.
+
 */
+

+
#define	_ELF_DEFINE_SYMBOL_VISIBILITY()		\
+
_ELF_DEFINE_STV(STV_DEFAULT,         0,		\
+
	"as specified by symbol type")		\
+
_ELF_DEFINE_STV(STV_INTERNAL,        1,		\
+
	"as defined by processor semantics")	\
+
_ELF_DEFINE_STV(STV_HIDDEN,          2,		\
+
	"hidden from other components")		\
+
_ELF_DEFINE_STV(STV_PROTECTED,       3,		\
+
	"local references are not preemptable")
+

+
#undef	_ELF_DEFINE_STV
+
#define	_ELF_DEFINE_STV(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_SYMBOL_VISIBILITY()
+
	STV__LAST__
+
};
+

+
/*
+
 * Symbol flags.
+
 */
+
#define	_ELF_DEFINE_SYMBOL_FLAGS()		\
+
_ELF_DEFINE_SYF(SYMINFO_FLG_DIRECT,	0x01,	\
+
	"directly assocated reference")		\
+
_ELF_DEFINE_SYF(SYMINFO_FLG_COPY,	0x04,	\
+
	"definition by copy-relocation")	\
+
_ELF_DEFINE_SYF(SYMINFO_FLG_LAZYLOAD,	0x08,	\
+
	"object should be lazily loaded")	\
+
_ELF_DEFINE_SYF(SYMINFO_FLG_DIRECTBIND,	0x10,	\
+
	"reference should be directly bound")	\
+
_ELF_DEFINE_SYF(SYMINFO_FLG_NOEXTDIRECT, 0x20,	\
+
	"external references not allowed to bind to definition")
+

+
#undef	_ELF_DEFINE_SYF
+
#define	_ELF_DEFINE_SYF(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_SYMBOL_FLAGS()
+
	SYMINFO_FLG__LAST__
+
};
+

+
/*
+
 * Version dependencies.
+
 */
+
#define	_ELF_DEFINE_VERSIONING_DEPENDENCIES()			\
+
_ELF_DEFINE_VERD(VER_NDX_LOCAL,		0,	"local scope")	\
+
_ELF_DEFINE_VERD(VER_NDX_GLOBAL,	1,	"global scope")
+
#undef	_ELF_DEFINE_VERD
+
#define	_ELF_DEFINE_VERD(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_VERSIONING_DEPENDENCIES()
+
	VER_NDX__LAST__
+
};
+

+
/*
+
 * Version flags.
+
 */
+
#define	_ELF_DEFINE_VERSIONING_FLAGS()				\
+
_ELF_DEFINE_VERF(VER_FLG_BASE,		0x1,	"file version") \
+
_ELF_DEFINE_VERF(VER_FLG_WEAK,		0x2,	"weak version")
+
#undef	_ELF_DEFINE_VERF
+
#define	_ELF_DEFINE_VERF(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_VERSIONING_FLAGS()
+
	VER_FLG__LAST__
+
};
+

+
/*
+
 * Version needs
+
 */
+
#define	_ELF_DEFINE_VERSIONING_NEEDS()					\
+
_ELF_DEFINE_VRN(VER_NEED_NONE,		0,	"invalid version")	\
+
_ELF_DEFINE_VRN(VER_NEED_CURRENT,	1,	"current version")
+
#undef	_ELF_DEFINE_VRN
+
#define	_ELF_DEFINE_VRN(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_VERSIONING_NEEDS()
+
	VER_NEED__LAST__
+
};
+

+
/*
+
 * Version numbers.
+
 */
+
#define	_ELF_DEFINE_VERSIONING_NUMBERS()				\
+
_ELF_DEFINE_VRNU(VER_DEF_NONE,		0,	"invalid version")	\
+
_ELF_DEFINE_VRNU(VER_DEF_CURRENT,	1, 	"current version")
+
#undef	_ELF_DEFINE_VRNU
+
#define	_ELF_DEFINE_VRNU(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_VERSIONING_NUMBERS()
+
	VER_DEF__LAST__
+
};
+

+
/**
+
 ** Relocation types.
+
 **/
+

+
#define	_ELF_DEFINE_386_RELOCATIONS()		\
+
_ELF_DEFINE_RELOC(R_386_NONE,		0)	\
+
_ELF_DEFINE_RELOC(R_386_32,		1)	\
+
_ELF_DEFINE_RELOC(R_386_PC32,		2)	\
+
_ELF_DEFINE_RELOC(R_386_GOT32,		3)	\
+
_ELF_DEFINE_RELOC(R_386_PLT32,		4)	\
+
_ELF_DEFINE_RELOC(R_386_COPY,		5)	\
+
_ELF_DEFINE_RELOC(R_386_GLOB_DAT,	6)	\
+
_ELF_DEFINE_RELOC(R_386_JUMP_SLOT,	7)	\
+
_ELF_DEFINE_RELOC(R_386_RELATIVE,	8)	\
+
_ELF_DEFINE_RELOC(R_386_GOTOFF,		9)	\
+
_ELF_DEFINE_RELOC(R_386_GOTPC,		10)	\
+
_ELF_DEFINE_RELOC(R_386_32PLT,		11)	\
+
_ELF_DEFINE_RELOC(R_386_16,		20)	\
+
_ELF_DEFINE_RELOC(R_386_PC16,		21)	\
+
_ELF_DEFINE_RELOC(R_386_8,		22)	\
+
_ELF_DEFINE_RELOC(R_386_PC8,		23)
+

+
/*
+
 */
+
#define	_ELF_DEFINE_AARCH64_RELOCATIONS()		\
+
_ELF_DEFINE_RELOC(R_AARCH64_ABS64,		257)	\
+
_ELF_DEFINE_RELOC(R_AARCH64_ABS32,		258)	\
+

+
/*
+
 * These are the symbols used in the Sun ``Linkers and Loaders
+
 * Guide'', Document No: 817-1984-17.  See the X86_64 relocations list
+
 * below for the spellings used in the ELF specification.
+
 */
+
#define	_ELF_DEFINE_AMD64_RELOCATIONS()		\
+
_ELF_DEFINE_RELOC(R_AMD64_NONE,		0)	\
+
_ELF_DEFINE_RELOC(R_AMD64_64,		1)	\
+
_ELF_DEFINE_RELOC(R_AMD64_PC32,		2)	\
+
_ELF_DEFINE_RELOC(R_AMD64_GOT32,	3)	\
+
_ELF_DEFINE_RELOC(R_AMD64_PLT32,	4)	\
+
_ELF_DEFINE_RELOC(R_AMD64_COPY,		5)	\
+
_ELF_DEFINE_RELOC(R_AMD64_GLOB_DAT,	6)	\
+
_ELF_DEFINE_RELOC(R_AMD64_JUMP_SLOT,	7)	\
+
_ELF_DEFINE_RELOC(R_AMD64_RELATIVE,	8)	\
+
_ELF_DEFINE_RELOC(R_AMD64_GOTPCREL,	9)	\
+
_ELF_DEFINE_RELOC(R_AMD64_32,		10)	\
+
_ELF_DEFINE_RELOC(R_AMD64_32S,		11)	\
+
_ELF_DEFINE_RELOC(R_AMD64_16,		12)	\
+
_ELF_DEFINE_RELOC(R_AMD64_PC16,		13)	\
+
_ELF_DEFINE_RELOC(R_AMD64_8,		14)	\
+
_ELF_DEFINE_RELOC(R_AMD64_PC8,		15)	\
+
_ELF_DEFINE_RELOC(R_AMD64_PC64,		24)	\
+
_ELF_DEFINE_RELOC(R_AMD64_GOTOFF64,	25)	\
+
_ELF_DEFINE_RELOC(R_AMD64_GOTPC32,	26)
+

+
/*
+
 * Relocation definitions from the ARM ELF ABI, version "ARM IHI
+
 * 0044E" released on 30th November 2012.
+
 */
+
#define	_ELF_DEFINE_ARM_RELOCATIONS()			\
+
_ELF_DEFINE_RELOC(R_ARM_NONE,			0)	\
+
_ELF_DEFINE_RELOC(R_ARM_PC24,			1)	\
+
_ELF_DEFINE_RELOC(R_ARM_ABS32,			2)	\
+
_ELF_DEFINE_RELOC(R_ARM_REL32,			3)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDR_PC_G0,		4)	\
+
_ELF_DEFINE_RELOC(R_ARM_ABS16,			5)	\
+
_ELF_DEFINE_RELOC(R_ARM_ABS12,			6)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_ABS5,		7)	\
+
_ELF_DEFINE_RELOC(R_ARM_ABS8,			8)	\
+
_ELF_DEFINE_RELOC(R_ARM_SBREL32,		9)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_CALL,		10)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_PC8,		11)	\
+
_ELF_DEFINE_RELOC(R_ARM_BREL_ADJ,		12)	\
+
_ELF_DEFINE_RELOC(R_ARM_SWI24,			13)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_DESC,		13)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_SWI8,		14)	\
+
_ELF_DEFINE_RELOC(R_ARM_XPC25,			15)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_XPC22,		16)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_DTPMOD32,		17)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_DTPOFF32,		18)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_TPOFF32,		19)	\
+
_ELF_DEFINE_RELOC(R_ARM_COPY,			20)	\
+
_ELF_DEFINE_RELOC(R_ARM_GLOB_DAT,		21)	\
+
_ELF_DEFINE_RELOC(R_ARM_JUMP_SLOT,		22)	\
+
_ELF_DEFINE_RELOC(R_ARM_RELATIVE,		23)	\
+
_ELF_DEFINE_RELOC(R_ARM_GOTOFF32,		24)	\
+
_ELF_DEFINE_RELOC(R_ARM_BASE_PREL,		25)	\
+
_ELF_DEFINE_RELOC(R_ARM_GOT_BREL,		26)	\
+
_ELF_DEFINE_RELOC(R_ARM_PLT32,			27)	\
+
_ELF_DEFINE_RELOC(R_ARM_CALL,			28)	\
+
_ELF_DEFINE_RELOC(R_ARM_JUMP24,			29)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP24,		30)	\
+
_ELF_DEFINE_RELOC(R_ARM_BASE_ABS,		31)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_PCREL_7_0,		32)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_PCREL_15_8,		33)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_PCREL_23_15,	34)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDR_SBREL_11_0_NC,	35)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_SBREL_19_12_NC,	36)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_SBREL_27_20_CK,	37)	\
+
_ELF_DEFINE_RELOC(R_ARM_TARGET1,		38)	\
+
_ELF_DEFINE_RELOC(R_ARM_SBREL31,		39)	\
+
_ELF_DEFINE_RELOC(R_ARM_V4BX,			40)	\
+
_ELF_DEFINE_RELOC(R_ARM_TARGET2,		41)	\
+
_ELF_DEFINE_RELOC(R_ARM_PREL31,			42)	\
+
_ELF_DEFINE_RELOC(R_ARM_MOVW_ABS_NC,		43)	\
+
_ELF_DEFINE_RELOC(R_ARM_MOVT_ABS,		44)	\
+
_ELF_DEFINE_RELOC(R_ARM_MOVW_PREL_NC,		45)	\
+
_ELF_DEFINE_RELOC(R_ARM_MOVT_PREL,		46)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_ABS_NC,	47)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_MOVT_ABS,		48)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_PREL_NC,	49)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_MOVT_PREL,		50)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP19,		51)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP6,		52)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_ALU_PREL_11_0,	53)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_PC12,		54)	\
+
_ELF_DEFINE_RELOC(R_ARM_ABS32_NOI,		55)	\
+
_ELF_DEFINE_RELOC(R_ARM_REL32_NOI,		56)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G0_NC,		57)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G0,		58)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G1_NC,		59)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G1,		60)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G2,		61)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDR_PC_G1,		62)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDR_PC_G2,		63)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDRS_PC_G0,		64)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDRS_PC_G1,		65)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDRS_PC_G2,		66)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDC_PC_G0,		67)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDC_PC_G1,		68)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDC_PC_G2,		69)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G0_NC,		70)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G0,		71)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G1_NC,		72)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G1,		73)	\
+
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G2,		74)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDR_SB_G0,		75)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDR_SB_G1,		76)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDR_SB_G2,		77)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDRS_SB_G0,		78)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDRS_SB_G1,		79)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDRS_SB_G2,		80)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDC_SB_G0,		81)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDC_SB_G1,		82)	\
+
_ELF_DEFINE_RELOC(R_ARM_LDC_SB_G2,		83)	\
+
_ELF_DEFINE_RELOC(R_ARM_MOVW_BREL_NC,		84)	\
+
_ELF_DEFINE_RELOC(R_ARM_MOVT_BREL,		85)	\
+
_ELF_DEFINE_RELOC(R_ARM_MOVW_BREL,		86)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_BREL_NC,	87)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_MOVT_BREL,		88)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_BREL,		89)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_GOTDESC,		90)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_CALL,		91)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_DESCSEQ,		92)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_TLS_CALL,		93)	\
+
_ELF_DEFINE_RELOC(R_ARM_PLT32_ABS,		94)	\
+
_ELF_DEFINE_RELOC(R_ARM_GOT_ABS,		95)	\
+
_ELF_DEFINE_RELOC(R_ARM_GOT_PREL,		96)	\
+
_ELF_DEFINE_RELOC(R_ARM_GOT_BREL12,		97)	\
+
_ELF_DEFINE_RELOC(R_ARM_GOTOFF12,		98)	\
+
_ELF_DEFINE_RELOC(R_ARM_GOTRELAX,		99)	\
+
_ELF_DEFINE_RELOC(R_ARM_GNU_VTENTRY,		100)	\
+
_ELF_DEFINE_RELOC(R_ARM_GNU_VTINHERIT,		101)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP11,		102)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP8,		103)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_GD32,		104)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_LDM32,		105)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_LDO32,		106)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_IE32,		107)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_LE32,		108)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_LDO12,		109)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_LE12,		110)	\
+
_ELF_DEFINE_RELOC(R_ARM_TLS_IE12GP,		111)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_0,		112)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_1,		113)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_2,		114)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_3,		115)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_4,		116)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_5,		117)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_6,		118)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_7,		119)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_8,		120)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_9,		121)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_10,		122)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_11,		123)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_12,		124)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_13,		125)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_14,		126)	\
+
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_15,		127)	\
+
_ELF_DEFINE_RELOC(R_ARM_ME_TOO,			128)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_TLS_DESCSEQ16,	129)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_TLS_DESCSEQ32,	130)	\
+
_ELF_DEFINE_RELOC(R_ARM_THM_GOT_BREL12,		131)	\
+
_ELF_DEFINE_RELOC(R_ARM_IRELATIVE,		140)
+

+
#define	_ELF_DEFINE_IA64_RELOCATIONS()			\
+
_ELF_DEFINE_RELOC(R_IA_64_NONE,			0)	\
+
_ELF_DEFINE_RELOC(R_IA_64_IMM14,		0x21)	\
+
_ELF_DEFINE_RELOC(R_IA_64_IMM22,		0x22)	\
+
_ELF_DEFINE_RELOC(R_IA_64_IMM64,		0x23)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DIR32MSB,		0x24)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DIR32LSB,		0x25)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DIR64MSB,		0x26)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DIR64LSB,		0x27)	\
+
_ELF_DEFINE_RELOC(R_IA_64_GPREL22,		0x2a)	\
+
_ELF_DEFINE_RELOC(R_IA_64_GPREL64I,		0x2b)	\
+
_ELF_DEFINE_RELOC(R_IA_64_GPREL32MSB,		0x2c)	\
+
_ELF_DEFINE_RELOC(R_IA_64_GPREL32LSB,		0x2d)	\
+
_ELF_DEFINE_RELOC(R_IA_64_GPREL64MSB,		0x2e)	\
+
_ELF_DEFINE_RELOC(R_IA_64_GPREL64LSB,		0x2f)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF22,		0x32)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF64I,		0x33)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PLTOFF22,		0x3a)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PLTOFF64I,		0x3b)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PLTOFF64MSB,		0x3e)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PLTOFF64LSB,		0x3f)	\
+
_ELF_DEFINE_RELOC(R_IA_64_FPTR64I,		0x43)	\
+
_ELF_DEFINE_RELOC(R_IA_64_FPTR32MSB,		0x44)	\
+
_ELF_DEFINE_RELOC(R_IA_64_FPTR32LSB,		0x45)	\
+
_ELF_DEFINE_RELOC(R_IA_64_FPTR64MSB,		0x46)	\
+
_ELF_DEFINE_RELOC(R_IA_64_FPTR64LSB,		0x47)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL60B,		0x48)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL21B,		0x49)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL21M,		0x4a)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL21F,		0x4b)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL32MSB,		0x4c)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL32LSB,		0x4d)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL64MSB,		0x4e)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL64LSB,		0x4f)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR22,		0x52)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR64I,	0x53)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR32MSB,	0x54)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR32LSB,	0x55)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR64MSB,	0x56)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR64LSB,	0x57)	\
+
_ELF_DEFINE_RELOC(R_IA_64_SEGREL32MSB,		0x5c)	\
+
_ELF_DEFINE_RELOC(R_IA_64_SEGREL32LSB,		0x5d)	\
+
_ELF_DEFINE_RELOC(R_IA_64_SEGREL64MSB,		0x5e)	\
+
_ELF_DEFINE_RELOC(R_IA_64_SEGREL64LSB,		0x5f)	\
+
_ELF_DEFINE_RELOC(R_IA_64_SECREL32MSB,		0x64)	\
+
_ELF_DEFINE_RELOC(R_IA_64_SECREL32LSB,		0x65)	\
+
_ELF_DEFINE_RELOC(R_IA_64_SECREL64MSB,		0x66)	\
+
_ELF_DEFINE_RELOC(R_IA_64_SECREL64LSB,		0x67)	\
+
_ELF_DEFINE_RELOC(R_IA_64_REL32MSB,		0x6c)	\
+
_ELF_DEFINE_RELOC(R_IA_64_REL32LSB,		0x6d)	\
+
_ELF_DEFINE_RELOC(R_IA_64_REL64MSB,		0x6e)	\
+
_ELF_DEFINE_RELOC(R_IA_64_REL64LSB,		0x6f)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTV32MSB,		0x74)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTV32LSB,		0x75)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTV64MSB,		0x76)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTV64LSB,		0x77)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL21BI,		0x79)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL22,		0x7A)	\
+
_ELF_DEFINE_RELOC(R_IA_64_PCREL64I,		0x7B)	\
+
_ELF_DEFINE_RELOC(R_IA_64_IPLTMSB,		0x80)	\
+
_ELF_DEFINE_RELOC(R_IA_64_IPLTLSB,		0x81)	\
+
_ELF_DEFINE_RELOC(R_IA_64_SUB,			0x85)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF22X,		0x86)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LDXMOV,		0x87)	\
+
_ELF_DEFINE_RELOC(R_IA_64_TPREL14,		0x91)	\
+
_ELF_DEFINE_RELOC(R_IA_64_TPREL22,		0x92)	\
+
_ELF_DEFINE_RELOC(R_IA_64_TPREL64I,		0x93)	\
+
_ELF_DEFINE_RELOC(R_IA_64_TPREL64MSB,		0x96)	\
+
_ELF_DEFINE_RELOC(R_IA_64_TPREL64LSB,		0x97)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_TPREL22,	0x9A)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DTPMOD64MSB,		0xA6)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DTPMOD64LSB,		0xA7)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_DTPMOD22,	0xAA)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DTPREL14,		0xB1)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DTPREL22,		0xB2)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DTPREL64I,		0xB3)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DTPREL32MSB,		0xB4)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DTPREL32LSB,		0xB5)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DTPREL64MSB,		0xB6)	\
+
_ELF_DEFINE_RELOC(R_IA_64_DTPREL64LSB,		0xB7)	\
+
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_DTPREL22,	0xBA)
+

+
#define	_ELF_DEFINE_MIPS_RELOCATIONS()			\
+
_ELF_DEFINE_RELOC(R_MIPS_NONE,			0)	\
+
_ELF_DEFINE_RELOC(R_MIPS_16,			1)	\
+
_ELF_DEFINE_RELOC(R_MIPS_32,			2)	\
+
_ELF_DEFINE_RELOC(R_MIPS_REL32,			3)	\
+
_ELF_DEFINE_RELOC(R_MIPS_26,			4)	\
+
_ELF_DEFINE_RELOC(R_MIPS_HI16,			5)	\
+
_ELF_DEFINE_RELOC(R_MIPS_LO16,			6)	\
+
_ELF_DEFINE_RELOC(R_MIPS_GPREL16,		7)	\
+
_ELF_DEFINE_RELOC(R_MIPS_LITERAL, 		8)	\
+
_ELF_DEFINE_RELOC(R_MIPS_GOT16,			9)	\
+
_ELF_DEFINE_RELOC(R_MIPS_PC16,			10)	\
+
_ELF_DEFINE_RELOC(R_MIPS_CALL16,		11)	\
+
_ELF_DEFINE_RELOC(R_MIPS_GPREL32,		12)	\
+
_ELF_DEFINE_RELOC(R_MIPS_64,			18)	\
+
_ELF_DEFINE_RELOC(R_MIPS_GOTHI16,		21)	\
+
_ELF_DEFINE_RELOC(R_MIPS_GOTLO16,		22)	\
+
_ELF_DEFINE_RELOC(R_MIPS_CALLHI16,		30)	\
+
_ELF_DEFINE_RELOC(R_MIPS_CALLLO16,		31)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPMOD32,		38)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL32,		39)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPMOD64,		40)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL64,		41)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_GD,		42)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_LDM,		43)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL_HI16,	44)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL_LO16,	45)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_GOTTPREL,		46)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL32,		47)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL64,		48)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL_HI16,	49)	\
+
_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL_LO16,	50)
+

+
#define	_ELF_DEFINE_PPC32_RELOCATIONS()		\
+
_ELF_DEFINE_RELOC(R_PPC_NONE,		0)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR32,		1)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR24,		2)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR16,		3)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR16_LO,	4)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR16_HI,	5)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR16_HA,	6)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR14,		7)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR14_BRTAKEN,	8)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR14_BRNTAKEN, 9)	\
+
_ELF_DEFINE_RELOC(R_PPC_REL24,		10)	\
+
_ELF_DEFINE_RELOC(R_PPC_REL14,		11)	\
+
_ELF_DEFINE_RELOC(R_PPC_REL14_BRTAKEN,	12)	\
+
_ELF_DEFINE_RELOC(R_PPC_REL14_BRNTAKEN,	13)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT16,		14)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT16_LO,	15)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT16_HI,	16)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT16_HA,	17)	\
+
_ELF_DEFINE_RELOC(R_PPC_PLTREL24,	18)	\
+
_ELF_DEFINE_RELOC(R_PPC_COPY,		19)	\
+
_ELF_DEFINE_RELOC(R_PPC_GLOB_DAT,	20)	\
+
_ELF_DEFINE_RELOC(R_PPC_JMP_SLOT,	21)	\
+
_ELF_DEFINE_RELOC(R_PPC_RELATIVE,	22)	\
+
_ELF_DEFINE_RELOC(R_PPC_LOCAL24PC,	23)	\
+
_ELF_DEFINE_RELOC(R_PPC_UADDR32,	24)	\
+
_ELF_DEFINE_RELOC(R_PPC_UADDR16,	25)	\
+
_ELF_DEFINE_RELOC(R_PPC_REL32,		26)	\
+
_ELF_DEFINE_RELOC(R_PPC_PLT32,		27)	\
+
_ELF_DEFINE_RELOC(R_PPC_PLTREL32,	28)	\
+
_ELF_DEFINE_RELOC(R_PPC_PLT16_LO,	29)	\
+
_ELF_DEFINE_RELOC(R_PPC_PLT16_HI,	30)	\
+
_ELF_DEFINE_RELOC(R_PPC_PLT16_HA,	31)	\
+
_ELF_DEFINE_RELOC(R_PPC_SDAREL16,	32)	\
+
_ELF_DEFINE_RELOC(R_PPC_SECTOFF,	33)	\
+
_ELF_DEFINE_RELOC(R_PPC_SECTOFF_LO,	34)	\
+
_ELF_DEFINE_RELOC(R_PPC_SECTOFF_HI,	35)	\
+
_ELF_DEFINE_RELOC(R_PPC_SECTOFF_HA,	36)	\
+
_ELF_DEFINE_RELOC(R_PPC_ADDR30,		37)	\
+
_ELF_DEFINE_RELOC(R_PPC_TLS,		67)	\
+
_ELF_DEFINE_RELOC(R_PPC_DTPMOD32,	68)	\
+
_ELF_DEFINE_RELOC(R_PPC_TPREL16,	69)	\
+
_ELF_DEFINE_RELOC(R_PPC_TPREL16_LO,	70)	\
+
_ELF_DEFINE_RELOC(R_PPC_TPREL16_HI,	71)	\
+
_ELF_DEFINE_RELOC(R_PPC_TPREL16_HA,	72)	\
+
_ELF_DEFINE_RELOC(R_PPC_TPREL32,	73)	\
+
_ELF_DEFINE_RELOC(R_PPC_DTPREL16,	74)	\
+
_ELF_DEFINE_RELOC(R_PPC_DTPREL16_LO,	75)	\
+
_ELF_DEFINE_RELOC(R_PPC_DTPREL16_HI,	76)	\
+
_ELF_DEFINE_RELOC(R_PPC_DTPREL16_HA,	77)	\
+
_ELF_DEFINE_RELOC(R_PPC_DTPREL32,	78)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16,	79)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16_LO,	80)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16_HI,	81)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16_HA,	82)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16,	83)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16_LO,	84)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16_HI,	85)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16_HA,	86)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16,	87)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16_LO,	88)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16_HI,	89)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16_HA,	90)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16,	91)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16_LO, 92)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16_HI, 93)	\
+
_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16_HA, 94)	\
+
_ELF_DEFINE_RELOC(R_PPC_TLSGD,		95)	\
+
_ELF_DEFINE_RELOC(R_PPC_TLSLD,		96)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR32,	101)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16,	102)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16_LO,	103)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16_HI,	104)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16_HA,	105)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_SDAI16,	106)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_SDA2I16,	107)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_SDA2REL,	108)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_SDA21,	109)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_MRKREF,	110)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_RELSEC16,	111)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_RELST_LO,	112)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_RELST_HI,	113)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_RELST_HA,	114)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_BIT_FLD,	115)	\
+
_ELF_DEFINE_RELOC(R_PPC_EMB_RELSDA,	116)	\
+

+
#define	_ELF_DEFINE_PPC64_RELOCATIONS()			\
+
_ELF_DEFINE_RELOC(R_PPC64_NONE,			0)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR32,		1)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR24,		2)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16,		3)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_LO,		4)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HI,		5)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HA,		6)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR14,		7)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR14_BRTAKEN,	8)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR14_BRNTAKEN,	9)	\
+
_ELF_DEFINE_RELOC(R_PPC64_REL24,		10)	\
+
_ELF_DEFINE_RELOC(R_PPC64_REL14,		11)	\
+
_ELF_DEFINE_RELOC(R_PPC64_REL14_BRTAKEN,	12)	\
+
_ELF_DEFINE_RELOC(R_PPC64_REL14_BRNTAKEN,	13)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT16,		14)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT16_LO,		15)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT16_HI,		16)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT16_HA,		17)	\
+
_ELF_DEFINE_RELOC(R_PPC64_COPY,			19)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GLOB_DAT,		20)	\
+
_ELF_DEFINE_RELOC(R_PPC64_JMP_SLOT,		21)	\
+
_ELF_DEFINE_RELOC(R_PPC64_RELATIVE,		22)	\
+
_ELF_DEFINE_RELOC(R_PPC64_UADDR32,		24)	\
+
_ELF_DEFINE_RELOC(R_PPC64_UADDR16,		25)	\
+
_ELF_DEFINE_RELOC(R_PPC64_REL32,		26)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLT32,		27)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLTREL32,		28)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLT16_LO,		29)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLT16_HI,		30)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLT16_HA,		31)	\
+
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF,		33)	\
+
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_LO,		34)	\
+
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_HI,		35)	\
+
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_HA,		36)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR30,		37)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR64,		38)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHER,	39)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHERA,	40)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHEST,	41)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHESTA,	42)	\
+
_ELF_DEFINE_RELOC(R_PPC64_UADDR64,		43)	\
+
_ELF_DEFINE_RELOC(R_PPC64_REL64,		44)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLT64,		45)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLTREL64,		46)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TOC16,		47)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TOC16_LO,		48)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TOC16_HI,		49)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TOC16_HA,		50)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TOC,			51)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16,		52)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_LO,		53)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_HI,		54)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_HA,		55)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_DS,		56)	\
+
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_LO_DS,		57)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT16_DS,		58)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT16_LO_DS,		59)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLT16_LO_DS,		60)	\
+
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_DS,		61)	\
+
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_LO_DS,	62)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TOC16_DS,		63)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TOC16_LO_DS,		64)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_DS,		65)	\
+
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_LO_DS,	66)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TLS,			67)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPMOD64,		68)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16,		69)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_LO,		60)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HI,		71)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HA,		72)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL64,		73)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16,		74)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_LO,		75)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HI,		76)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HA,		77)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL64,		78)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16,		79)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16_LO,	80)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16_HI,	81)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16_HA,	82)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16,		83)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16_LO,	84)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16_HI,	85)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16_HA,	86)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_DS,	87)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_LO_DS,	88)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_HI,	89)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_HA,	90)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_DS,	91)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_LO_DS,	92)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_HI,	93)	\
+
_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_HA,	94)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_DS,		95)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_LO_DS,	96)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHER,	97)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHERA,	98)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHEST,	99)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHESTA,	100)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_DS,		101)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_LO_DS,	102)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHER,	103)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHERA,	104)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHEST,	105)	\
+
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHESTA,	106)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TLSGD,		107)	\
+
_ELF_DEFINE_RELOC(R_PPC64_TLSLD,		108)
+

+
#define	_ELF_DEFINE_RISCV_RELOCATIONS()			\
+
_ELF_DEFINE_RELOC(R_RISCV_NONE,			0)	\
+
_ELF_DEFINE_RELOC(R_RISCV_32,			1)	\
+
_ELF_DEFINE_RELOC(R_RISCV_64,			2)	\
+
_ELF_DEFINE_RELOC(R_RISCV_RELATIVE,		3)	\
+
_ELF_DEFINE_RELOC(R_RISCV_COPY,			4)	\
+
_ELF_DEFINE_RELOC(R_RISCV_JUMP_SLOT,		5)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TLS_DTPMOD32,		6)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TLS_DTPMOD64,		7)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TLS_DTPREL32,		8)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TLS_DTPREL64,		9)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TLS_TPREL32,		10)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TLS_TPREL64,		11)	\
+
_ELF_DEFINE_RELOC(R_RISCV_BRANCH,		16)	\
+
_ELF_DEFINE_RELOC(R_RISCV_JAL,			17)	\
+
_ELF_DEFINE_RELOC(R_RISCV_CALL,			18)	\
+
_ELF_DEFINE_RELOC(R_RISCV_CALL_PLT,		19)	\
+
_ELF_DEFINE_RELOC(R_RISCV_GOT_HI20,		20)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TLS_GOT_HI20,		21)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TLS_GD_HI20,		22)	\
+
_ELF_DEFINE_RELOC(R_RISCV_PCREL_HI20,		23)	\
+
_ELF_DEFINE_RELOC(R_RISCV_PCREL_LO12_I,		24)	\
+
_ELF_DEFINE_RELOC(R_RISCV_PCREL_LO12_S,		25)	\
+
_ELF_DEFINE_RELOC(R_RISCV_HI20,			26)	\
+
_ELF_DEFINE_RELOC(R_RISCV_LO12_I,		27)	\
+
_ELF_DEFINE_RELOC(R_RISCV_LO12_S,		28)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TPREL_HI20,		29)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TPREL_LO12_I,		30)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TPREL_LO12_S,		31)	\
+
_ELF_DEFINE_RELOC(R_RISCV_TPREL_ADD,		32)	\
+
_ELF_DEFINE_RELOC(R_RISCV_ADD8,			33)	\
+
_ELF_DEFINE_RELOC(R_RISCV_ADD16,		34)	\
+
_ELF_DEFINE_RELOC(R_RISCV_ADD32,		35)	\
+
_ELF_DEFINE_RELOC(R_RISCV_ADD64,		36)	\
+
_ELF_DEFINE_RELOC(R_RISCV_SUB8,			37)	\
+
_ELF_DEFINE_RELOC(R_RISCV_SUB16,		38)	\
+
_ELF_DEFINE_RELOC(R_RISCV_SUB32,		39)	\
+
_ELF_DEFINE_RELOC(R_RISCV_SUB64,		40)	\
+
_ELF_DEFINE_RELOC(R_RISCV_GNU_VTINHERIT,	41)	\
+
_ELF_DEFINE_RELOC(R_RISCV_GNU_VTENTRY,		42)	\
+
_ELF_DEFINE_RELOC(R_RISCV_ALIGN,		43)	\
+
_ELF_DEFINE_RELOC(R_RISCV_RVC_BRANCH,		44)	\
+
_ELF_DEFINE_RELOC(R_RISCV_RVC_JUMP,		45)
+

+
#define	_ELF_DEFINE_SPARC_RELOCATIONS()		\
+
_ELF_DEFINE_RELOC(R_SPARC_NONE,		0)	\
+
_ELF_DEFINE_RELOC(R_SPARC_8,		1)	\
+
_ELF_DEFINE_RELOC(R_SPARC_16,		2)	\
+
_ELF_DEFINE_RELOC(R_SPARC_32, 		3)	\
+
_ELF_DEFINE_RELOC(R_SPARC_DISP8,	4)	\
+
_ELF_DEFINE_RELOC(R_SPARC_DISP16,	5)	\
+
_ELF_DEFINE_RELOC(R_SPARC_DISP32,	6)	\
+
_ELF_DEFINE_RELOC(R_SPARC_WDISP30,	7)	\
+
_ELF_DEFINE_RELOC(R_SPARC_WDISP22,	8)	\
+
_ELF_DEFINE_RELOC(R_SPARC_HI22,		9)	\
+
_ELF_DEFINE_RELOC(R_SPARC_22,		10)	\
+
_ELF_DEFINE_RELOC(R_SPARC_13,		11)	\
+
_ELF_DEFINE_RELOC(R_SPARC_LO10,		12)	\
+
_ELF_DEFINE_RELOC(R_SPARC_GOT10,	13)	\
+
_ELF_DEFINE_RELOC(R_SPARC_GOT13,	14)	\
+
_ELF_DEFINE_RELOC(R_SPARC_GOT22,	15)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PC10,		16)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PC22,		17)	\
+
_ELF_DEFINE_RELOC(R_SPARC_WPLT30,	18)	\
+
_ELF_DEFINE_RELOC(R_SPARC_COPY,		19)	\
+
_ELF_DEFINE_RELOC(R_SPARC_GLOB_DAT,	20)	\
+
_ELF_DEFINE_RELOC(R_SPARC_JMP_SLOT,	21)	\
+
_ELF_DEFINE_RELOC(R_SPARC_RELATIVE,	22)	\
+
_ELF_DEFINE_RELOC(R_SPARC_UA32,		23)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PLT32,	24)	\
+
_ELF_DEFINE_RELOC(R_SPARC_HIPLT22,	25)	\
+
_ELF_DEFINE_RELOC(R_SPARC_LOPLT10,	26)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PCPLT32,	27)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PCPLT22,	28)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PCPLT10,	29)	\
+
_ELF_DEFINE_RELOC(R_SPARC_10,		30)	\
+
_ELF_DEFINE_RELOC(R_SPARC_11,		31)	\
+
_ELF_DEFINE_RELOC(R_SPARC_64,		32)	\
+
_ELF_DEFINE_RELOC(R_SPARC_OLO10,	33)	\
+
_ELF_DEFINE_RELOC(R_SPARC_HH22,		34)	\
+
_ELF_DEFINE_RELOC(R_SPARC_HM10,		35)	\
+
_ELF_DEFINE_RELOC(R_SPARC_LM22,		36)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PC_HH22,	37)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PC_HM10,	38)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PC_LM22,	39)	\
+
_ELF_DEFINE_RELOC(R_SPARC_WDISP16,	40)	\
+
_ELF_DEFINE_RELOC(R_SPARC_WDISP19,	41)	\
+
_ELF_DEFINE_RELOC(R_SPARC_7,		43)	\
+
_ELF_DEFINE_RELOC(R_SPARC_5,		44)	\
+
_ELF_DEFINE_RELOC(R_SPARC_6,		45)	\
+
_ELF_DEFINE_RELOC(R_SPARC_DISP64,	46)	\
+
_ELF_DEFINE_RELOC(R_SPARC_PLT64,	47)	\
+
_ELF_DEFINE_RELOC(R_SPARC_HIX22,	48)	\
+
_ELF_DEFINE_RELOC(R_SPARC_LOX10,	49)	\
+
_ELF_DEFINE_RELOC(R_SPARC_H44,		50)	\
+
_ELF_DEFINE_RELOC(R_SPARC_M44,		51)	\
+
_ELF_DEFINE_RELOC(R_SPARC_L44,		52)	\
+
_ELF_DEFINE_RELOC(R_SPARC_REGISTER,	53)	\
+
_ELF_DEFINE_RELOC(R_SPARC_UA64,		54)	\
+
_ELF_DEFINE_RELOC(R_SPARC_UA16,		55)	\
+
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_HIX22, 80)	\
+
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_LOX10, 81)	\
+
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_OP_HIX22, 82)	\
+
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_OP_LOX10, 83)	\
+
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_OP,	84)	\
+
_ELF_DEFINE_RELOC(R_SPARC_H34,		85)
+

+
#define	_ELF_DEFINE_X86_64_RELOCATIONS()	\
+
_ELF_DEFINE_RELOC(R_X86_64_NONE,	0)	\
+
_ELF_DEFINE_RELOC(R_X86_64_64,		1)	\
+
_ELF_DEFINE_RELOC(R_X86_64_PC32,	2)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOT32,	3)	\
+
_ELF_DEFINE_RELOC(R_X86_64_PLT32,	4)	\
+
_ELF_DEFINE_RELOC(R_X86_64_COPY,	5)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GLOB_DAT,	6)	\
+
_ELF_DEFINE_RELOC(R_X86_64_JUMP_SLOT,	7)	\
+
_ELF_DEFINE_RELOC(R_X86_64_RELATIVE,	8)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOTPCREL,	9)	\
+
_ELF_DEFINE_RELOC(R_X86_64_32,		10)	\
+
_ELF_DEFINE_RELOC(R_X86_64_32S,		11)	\
+
_ELF_DEFINE_RELOC(R_X86_64_16,		12)	\
+
_ELF_DEFINE_RELOC(R_X86_64_PC16,	13)	\
+
_ELF_DEFINE_RELOC(R_X86_64_8,		14)	\
+
_ELF_DEFINE_RELOC(R_X86_64_PC8,		15)	\
+
_ELF_DEFINE_RELOC(R_X86_64_DTPMOD64,	16)	\
+
_ELF_DEFINE_RELOC(R_X86_64_DTPOFF64,	17)	\
+
_ELF_DEFINE_RELOC(R_X86_64_TPOFF64,	18)	\
+
_ELF_DEFINE_RELOC(R_X86_64_TLSGD,	19)	\
+
_ELF_DEFINE_RELOC(R_X86_64_TLSLD,	20)	\
+
_ELF_DEFINE_RELOC(R_X86_64_DTPOFF32,	21)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOTTPOFF,	22)	\
+
_ELF_DEFINE_RELOC(R_X86_64_TPOFF32,	23)	\
+
_ELF_DEFINE_RELOC(R_X86_64_PC64,	24)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOTOFF64,	25)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOTPC32,	26)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOT64,	27)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOTPCREL64,	28)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOTPC64,	29)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOTPLT64,	30)	\
+
_ELF_DEFINE_RELOC(R_X86_64_PLTOFF64,	31)	\
+
_ELF_DEFINE_RELOC(R_X86_64_SIZE32,	32)	\
+
_ELF_DEFINE_RELOC(R_X86_64_SIZE64,	33)	\
+
_ELF_DEFINE_RELOC(R_X86_64_GOTPC32_TLSDESC, 34)	\
+
_ELF_DEFINE_RELOC(R_X86_64_TLSDESC_CALL, 35)	\
+
_ELF_DEFINE_RELOC(R_X86_64_TLSDESC,	36)	\
+
_ELF_DEFINE_RELOC(R_X86_64_IRELATIVE,	37)
+

+
#define	_ELF_DEFINE_RELOCATIONS()		\
+
_ELF_DEFINE_386_RELOCATIONS()			\
+
_ELF_DEFINE_AARCH64_RELOCATIONS()		\
+
_ELF_DEFINE_AMD64_RELOCATIONS()			\
+
_ELF_DEFINE_ARM_RELOCATIONS()			\
+
_ELF_DEFINE_IA64_RELOCATIONS()			\
+
_ELF_DEFINE_MIPS_RELOCATIONS()			\
+
_ELF_DEFINE_PPC32_RELOCATIONS()			\
+
_ELF_DEFINE_PPC64_RELOCATIONS()			\
+
_ELF_DEFINE_RISCV_RELOCATIONS()			\
+
_ELF_DEFINE_SPARC_RELOCATIONS()			\
+
_ELF_DEFINE_X86_64_RELOCATIONS()
+

+
#undef	_ELF_DEFINE_RELOC
+
#define	_ELF_DEFINE_RELOC(N, V)		N = V ,
+
enum {
+
	_ELF_DEFINE_RELOCATIONS()
+
	R__LAST__
+
};
+

+
#define	PN_XNUM			0xFFFFU /* Use extended section numbering. */
+

+
/**
+
 ** ELF Types.
+
 **/
+

+
typedef uint32_t	Elf32_Addr;	/* Program address. */
+
typedef uint8_t		Elf32_Byte;	/* Unsigned tiny integer. */
+
typedef uint16_t	Elf32_Half;	/* Unsigned medium integer. */
+
typedef uint32_t	Elf32_Off;	/* File offset. */
+
typedef uint16_t	Elf32_Section;	/* Section index. */
+
typedef int32_t		Elf32_Sword;	/* Signed integer. */
+
typedef uint32_t	Elf32_Word;	/* Unsigned integer. */
+
typedef uint64_t	Elf32_Lword;	/* Unsigned long integer. */
+

+
typedef uint64_t	Elf64_Addr;	/* Program address. */
+
typedef uint8_t		Elf64_Byte;	/* Unsigned tiny integer. */
+
typedef uint16_t	Elf64_Half;	/* Unsigned medium integer. */
+
typedef uint64_t	Elf64_Off;	/* File offset. */
+
typedef uint16_t	Elf64_Section;	/* Section index. */
+
typedef int32_t		Elf64_Sword;	/* Signed integer. */
+
typedef uint32_t	Elf64_Word;	/* Unsigned integer. */
+
typedef uint64_t	Elf64_Lword;	/* Unsigned long integer. */
+
typedef uint64_t	Elf64_Xword;	/* Unsigned long integer. */
+
typedef int64_t		Elf64_Sxword;	/* Signed long integer. */
+

+

+
/*
+
 * Capability descriptors.
+
 */
+

+
/* 32-bit capability descriptor. */
+
typedef struct {
+
	Elf32_Word	c_tag;	     /* Type of entry. */
+
	union {
+
		Elf32_Word	c_val; /* Integer value. */
+
		Elf32_Addr	c_ptr; /* Pointer value. */
+
	} c_un;
+
} Elf32_Cap;
+

+
/* 64-bit capability descriptor. */
+
typedef struct {
+
	Elf64_Xword	c_tag;	     /* Type of entry. */
+
	union {
+
		Elf64_Xword	c_val; /* Integer value. */
+
		Elf64_Addr	c_ptr; /* Pointer value. */
+
	} c_un;
+
} Elf64_Cap;
+

+
/*
+
 * MIPS .conflict section entries.
+
 */
+

+
/* 32-bit entry. */
+
typedef struct {
+
	Elf32_Addr	c_index;
+
} Elf32_Conflict;
+

+
/* 64-bit entry. */
+
typedef struct {
+
	Elf64_Addr	c_index;
+
} Elf64_Conflict;
+

+
/*
+
 * Dynamic section entries.
+
 */
+

+
/* 32-bit entry. */
+
typedef struct {
+
	Elf32_Sword	d_tag;	     /* Type of entry. */
+
	union {
+
		Elf32_Word	d_val; /* Integer value. */
+
		Elf32_Addr	d_ptr; /* Pointer value. */
+
	} d_un;
+
} Elf32_Dyn;
+

+
/* 64-bit entry. */
+
typedef struct {
+
	Elf64_Sxword	d_tag;	     /* Type of entry. */
+
	union {
+
		Elf64_Xword	d_val; /* Integer value. */
+
		Elf64_Addr	d_ptr; /* Pointer value; */
+
	} d_un;
+
} Elf64_Dyn;
+

+

+
/*
+
 * The executable header (EHDR).
+
 */
+

+
/* 32 bit EHDR. */
+
typedef struct {
+
	unsigned char   e_ident[EI_NIDENT]; /* ELF identification. */
+
	Elf32_Half      e_type;	     /* Object file type (ET_*). */
+
	Elf32_Half      e_machine;   /* Machine type (EM_*). */
+
	Elf32_Word      e_version;   /* File format version (EV_*). */
+
	Elf32_Addr      e_entry;     /* Start address. */
+
	Elf32_Off       e_phoff;     /* File offset to the PHDR table. */
+
	Elf32_Off       e_shoff;     /* File offset to the SHDRheader. */
+
	Elf32_Word      e_flags;     /* Flags (EF_*). */
+
	Elf32_Half      e_ehsize;    /* Elf header size in bytes. */
+
	Elf32_Half      e_phentsize; /* PHDR table entry size in bytes. */
+
	Elf32_Half      e_phnum;     /* Number of PHDR entries. */
+
	Elf32_Half      e_shentsize; /* SHDR table entry size in bytes. */
+
	Elf32_Half      e_shnum;     /* Number of SHDR entries. */
+
	Elf32_Half      e_shstrndx;  /* Index of section name string table. */
+
} Elf32_Ehdr;
+

+

+
/* 64 bit EHDR. */
+
typedef struct {
+
	unsigned char   e_ident[EI_NIDENT]; /* ELF identification. */
+
	Elf64_Half      e_type;	     /* Object file type (ET_*). */
+
	Elf64_Half      e_machine;   /* Machine type (EM_*). */
+
	Elf64_Word      e_version;   /* File format version (EV_*). */
+
	Elf64_Addr      e_entry;     /* Start address. */
+
	Elf64_Off       e_phoff;     /* File offset to the PHDR table. */
+
	Elf64_Off       e_shoff;     /* File offset to the SHDRheader. */
+
	Elf64_Word      e_flags;     /* Flags (EF_*). */
+
	Elf64_Half      e_ehsize;    /* Elf header size in bytes. */
+
	Elf64_Half      e_phentsize; /* PHDR table entry size in bytes. */
+
	Elf64_Half      e_phnum;     /* Number of PHDR entries. */
+
	Elf64_Half      e_shentsize; /* SHDR table entry size in bytes. */
+
	Elf64_Half      e_shnum;     /* Number of SHDR entries. */
+
	Elf64_Half      e_shstrndx;  /* Index of section name string table. */
+
} Elf64_Ehdr;
+

+

+
/*
+
 * Shared object information.
+
 */
+

+
/* 32-bit entry. */
+
typedef struct {
+
	Elf32_Word l_name;	     /* The name of a shared object. */
+
	Elf32_Word l_time_stamp;     /* 32-bit timestamp. */
+
	Elf32_Word l_checksum;	     /* Checksum of visible symbols, sizes. */
+
	Elf32_Word l_version;	     /* Interface version string index. */
+
	Elf32_Word l_flags;	     /* Flags (LL_*). */
+
} Elf32_Lib;
+

+
/* 64-bit entry. */
+
typedef struct {
+
	Elf64_Word l_name;	     /* The name of a shared object. */
+
	Elf64_Word l_time_stamp;     /* 32-bit timestamp. */
+
	Elf64_Word l_checksum;	     /* Checksum of visible symbols, sizes. */
+
	Elf64_Word l_version;	     /* Interface version string index. */
+
	Elf64_Word l_flags;	     /* Flags (LL_*). */
+
} Elf64_Lib;
+

+
#define	_ELF_DEFINE_LL_FLAGS()			\
+
_ELF_DEFINE_LL(LL_NONE,			0,	\
+
	"no flags")				\
+
_ELF_DEFINE_LL(LL_EXACT_MATCH,		0x1,	\
+
	"require an exact match")		\
+
_ELF_DEFINE_LL(LL_IGNORE_INT_VER,	0x2,	\
+
	"ignore version incompatibilities")	\
+
_ELF_DEFINE_LL(LL_REQUIRE_MINOR,	0x4,	\
+
	"")					\
+
_ELF_DEFINE_LL(LL_EXPORTS,		0x8,	\
+
	"")					\
+
_ELF_DEFINE_LL(LL_DELAY_LOAD,		0x10,	\
+
	"")					\
+
_ELF_DEFINE_LL(LL_DELTA,		0x20,	\
+
	"")
+

+
#undef	_ELF_DEFINE_LL
+
#define	_ELF_DEFINE_LL(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_LL_FLAGS()
+
	LL__LAST__
+
};
+

+
/*
+
 * Note tags
+
 */
+

+
#define	_ELF_DEFINE_NOTE_ENTRY_TYPES()					\
+
_ELF_DEFINE_NT(NT_ABI_TAG,	1,	"Tag indicating the ABI")	\
+
_ELF_DEFINE_NT(NT_GNU_HWCAP,	2,	"Hardware capabilities")	\
+
_ELF_DEFINE_NT(NT_GNU_BUILD_ID,	3,	"Build id, set by ld(1)")	\
+
_ELF_DEFINE_NT(NT_GNU_GOLD_VERSION, 4,					\
+
	"Version number of the GNU gold linker")			\
+
_ELF_DEFINE_NT(NT_PRSTATUS,	1,	"Process status")		\
+
_ELF_DEFINE_NT(NT_FPREGSET,	2,	"Floating point information")	\
+
_ELF_DEFINE_NT(NT_PRPSINFO,	3,	"Process information")		\
+
_ELF_DEFINE_NT(NT_AUXV,		6,	"Auxiliary vector")		\
+
_ELF_DEFINE_NT(NT_PRXFPREG,	0x46E62B7FUL,				\
+
	"Linux user_xfpregs structure")					\
+
_ELF_DEFINE_NT(NT_PSTATUS,	10,	"Linux process status")		\
+
_ELF_DEFINE_NT(NT_FPREGS,	12,	"Linux floating point regset")	\
+
_ELF_DEFINE_NT(NT_PSINFO,	13,	"Linux process information")	\
+
_ELF_DEFINE_NT(NT_LWPSTATUS,	16,	"Linux lwpstatus_t type")	\
+
_ELF_DEFINE_NT(NT_LWPSINFO,	17,	"Linux lwpinfo_t type")
+

+
#undef	_ELF_DEFINE_NT
+
#define	_ELF_DEFINE_NT(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_NOTE_ENTRY_TYPES()
+
	NT__LAST__
+
};
+

+
/* Aliases for the ABI tag. */
+
#define	NT_FREEBSD_ABI_TAG	NT_ABI_TAG
+
#define	NT_GNU_ABI_TAG		NT_ABI_TAG
+
#define	NT_NETBSD_IDENT		NT_ABI_TAG
+
#define	NT_OPENBSD_IDENT	NT_ABI_TAG
+

+
/*
+
 * Note descriptors.
+
 */
+

+
typedef	struct {
+
	uint32_t	n_namesz;    /* Length of note's name. */
+
	uint32_t	n_descsz;    /* Length of note's value. */
+
	uint32_t	n_type;	     /* Type of note. */
+
} Elf_Note;
+

+
typedef Elf_Note Elf32_Nhdr;	     /* 32-bit note header. */
+
typedef Elf_Note Elf64_Nhdr;	     /* 64-bit note header. */
+

+
/*
+
 * MIPS ELF options descriptor header.
+
 */
+

+
typedef struct {
+
	Elf64_Byte	kind;        /* Type of options. */
+
	Elf64_Byte     	size;	     /* Size of option descriptor. */
+
	Elf64_Half	section;     /* Index of section affected. */
+
	Elf64_Word	info;        /* Kind-specific information. */
+
} Elf_Options;
+

+
/*
+
 * Option kinds.
+
 */
+

+
#define	_ELF_DEFINE_OPTION_KINDS()					\
+
_ELF_DEFINE_ODK(ODK_NULL,       0,      "undefined")			\
+
_ELF_DEFINE_ODK(ODK_REGINFO,    1,      "register usage info")		\
+
_ELF_DEFINE_ODK(ODK_EXCEPTIONS, 2,      "exception processing info")	\
+
_ELF_DEFINE_ODK(ODK_PAD,        3,      "section padding")		\
+
_ELF_DEFINE_ODK(ODK_HWPATCH,    4,      "hardware patch applied")	\
+
_ELF_DEFINE_ODK(ODK_FILL,       5,      "fill value used by linker")	\
+
_ELF_DEFINE_ODK(ODK_TAGS,       6,      "reserved space for tools")	\
+
_ELF_DEFINE_ODK(ODK_HWAND,      7,      "hardware AND patch applied")	\
+
_ELF_DEFINE_ODK(ODK_HWOR,       8,      "hardware OR patch applied")	\
+
_ELF_DEFINE_ODK(ODK_GP_GROUP,   9,					\
+
	"GP group to use for text/data sections")			\
+
_ELF_DEFINE_ODK(ODK_IDENT,      10,     "ID information")		\
+
_ELF_DEFINE_ODK(ODK_PAGESIZE,   11,     "page size infomation")
+

+
#undef	_ELF_DEFINE_ODK
+
#define	_ELF_DEFINE_ODK(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_OPTION_KINDS()
+
	ODK__LAST__
+
};
+

+
/*
+
 * ODK_EXCEPTIONS info field masks.
+
 */
+

+
#define	_ELF_DEFINE_ODK_EXCEPTIONS_MASK()				\
+
_ELF_DEFINE_OEX(OEX_FPU_MIN,    0x0000001FUL,				\
+
	"minimum FPU exception which must be enabled")			\
+
_ELF_DEFINE_OEX(OEX_FPU_MAX,    0x00001F00UL,				\
+
	"maximum FPU exception which can be enabled")			\
+
_ELF_DEFINE_OEX(OEX_PAGE0,      0x00010000UL,				\
+
	"page zero must be mapped")					\
+
_ELF_DEFINE_OEX(OEX_SMM,        0x00020000UL,				\
+
	"run in sequential memory mode")				\
+
_ELF_DEFINE_OEX(OEX_PRECISEFP,  0x00040000UL,				\
+
	"run in precise FP exception mode")				\
+
_ELF_DEFINE_OEX(OEX_DISMISS,    0x00080000UL,				\
+
	"dismiss invalid address traps")
+

+
#undef	_ELF_DEFINE_OEX
+
#define	_ELF_DEFINE_OEX(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_ODK_EXCEPTIONS_MASK()
+
	OEX__LAST__
+
};
+

+
/*
+
 * ODK_PAD info field masks.
+
 */
+

+
#define	_ELF_DEFINE_ODK_PAD_MASK()					\
+
_ELF_DEFINE_OPAD(OPAD_PREFIX,   0x0001)					\
+
_ELF_DEFINE_OPAD(OPAD_POSTFIX,  0x0002)					\
+
_ELF_DEFINE_OPAD(OPAD_SYMBOL,   0x0004)
+

+
#undef	_ELF_DEFINE_OPAD
+
#define	_ELF_DEFINE_OPAD(N, V)		N = V ,
+
enum {
+
	_ELF_DEFINE_ODK_PAD_MASK()
+
	OPAD__LAST__
+
};
+

+
/*
+
 * ODK_HWPATCH info field masks.
+
 */
+

+
#define	_ELF_DEFINE_ODK_HWPATCH_MASK()					\
+
_ELF_DEFINE_OHW(OHW_R4KEOP,     0x00000001UL,				\
+
	"patch for R4000 branch at end-of-page bug")			\
+
_ELF_DEFINE_OHW(OHW_R8KPFETCH,  0x00000002UL,				\
+
	"R8000 prefetch bug may occur")					\
+
_ELF_DEFINE_OHW(OHW_R5KEOP,     0x00000004UL,				\
+
	"patch for R5000 branch at end-of-page bug")			\
+
_ELF_DEFINE_OHW(OHW_R5KCVTL,    0x00000008UL,				\
+
	"R5000 cvt.[ds].l bug: clean == 1")				\
+
_ELF_DEFINE_OHW(OHW_R10KLDL,    0x00000010UL,				\
+
	"needd patch for R10000 misaligned load")
+

+
#undef	_ELF_DEFINE_OHW
+
#define	_ELF_DEFINE_OHW(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_ODK_HWPATCH_MASK()
+
	OHW__LAST__
+
};
+

+
/*
+
 * ODK_HWAND/ODK_HWOR info field and hwp_flags[12] masks.
+
 */
+

+
#define	_ELF_DEFINE_ODK_HWP_MASK()					\
+
_ELF_DEFINE_HWP(OHWA0_R4KEOP_CHECKED, 0x00000001UL,			\
+
	"object checked for R4000 end-of-page bug")			\
+
_ELF_DEFINE_HWP(OHWA0_R4KEOP_CLEAN, 0x00000002UL,			\
+
	"object verified clean for R4000 end-of-page bug")		\
+
_ELF_DEFINE_HWP(OHWO0_FIXADE,   0x00000001UL,				\
+
	"object requires call to fixade")
+

+
#undef	_ELF_DEFINE_HWP
+
#define	_ELF_DEFINE_HWP(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_ODK_HWP_MASK()
+
	OHWX0__LAST__
+
};
+

+
/*
+
 * ODK_IDENT/ODK_GP_GROUP info field masks.
+
 */
+

+
#define	_ELF_DEFINE_ODK_GP_MASK()					\
+
_ELF_DEFINE_OGP(OGP_GROUP,      0x0000FFFFUL, "GP group number")	\
+
_ELF_DEFINE_OGP(OGP_SELF,       0x00010000UL,				\
+
	"GP group is self-contained")
+

+
#undef	_ELF_DEFINE_OGP
+
#define	_ELF_DEFINE_OGP(N, V, DESCR)	N = V ,
+
enum {
+
	_ELF_DEFINE_ODK_GP_MASK()
+
	OGP__LAST__
+
};
+

+
/*
+
 * MIPS ELF register info descriptor.
+
 */
+

+
/* 32 bit RegInfo entry. */
+
typedef struct {
+
	Elf32_Word	ri_gprmask;  /* Mask of general register used. */
+
	Elf32_Word	ri_cprmask[4]; /* Mask of coprocessor register used. */
+
	Elf32_Addr	ri_gp_value; /* GP register value. */
+
} Elf32_RegInfo;
+

+
/* 64 bit RegInfo entry. */
+
typedef struct {
+
	Elf64_Word	ri_gprmask;  /* Mask of general register used. */
+
	Elf64_Word	ri_pad;	     /* Padding. */
+
	Elf64_Word	ri_cprmask[4]; /* Mask of coprocessor register used. */
+
	Elf64_Addr	ri_gp_value; /* GP register value. */
+
} Elf64_RegInfo;
+

+
/*
+
 * Program Header Table (PHDR) entries.
+
 */
+

+
/* 32 bit PHDR entry. */
+
typedef struct {
+
	Elf32_Word	p_type;	     /* Type of segment. */
+
	Elf32_Off	p_offset;    /* File offset to segment. */
+
	Elf32_Addr	p_vaddr;     /* Virtual address in memory. */
+
	Elf32_Addr	p_paddr;     /* Physical address (if relevant). */
+
	Elf32_Word	p_filesz;    /* Size of segment in file. */
+
	Elf32_Word	p_memsz;     /* Size of segment in memory. */
+
	Elf32_Word	p_flags;     /* Segment flags. */
+
	Elf32_Word	p_align;     /* Alignment constraints. */
+
} Elf32_Phdr;
+

+
/* 64 bit PHDR entry. */
+
typedef struct {
+
	Elf64_Word	p_type;	     /* Type of segment. */
+
	Elf64_Word	p_flags;     /* Segment flags. */
+
	Elf64_Off	p_offset;    /* File offset to segment. */
+
	Elf64_Addr	p_vaddr;     /* Virtual address in memory. */
+
	Elf64_Addr	p_paddr;     /* Physical address (if relevant). */
+
	Elf64_Xword	p_filesz;    /* Size of segment in file. */
+
	Elf64_Xword	p_memsz;     /* Size of segment in memory. */
+
	Elf64_Xword	p_align;     /* Alignment constraints. */
+
} Elf64_Phdr;
+

+

+
/*
+
 * Move entries, for describing data in COMMON blocks in a compact
+
 * manner.
+
 */
+

+
/* 32-bit move entry. */
+
typedef struct {
+
	Elf32_Lword	m_value;     /* Initialization value. */
+
	Elf32_Word 	m_info;	     /* Encoded size and index. */
+
	Elf32_Word	m_poffset;   /* Offset relative to symbol. */
+
	Elf32_Half	m_repeat;    /* Repeat count. */
+
	Elf32_Half	m_stride;    /* Number of units to skip. */
+
} Elf32_Move;
+

+
/* 64-bit move entry. */
+
typedef struct {
+
	Elf64_Lword	m_value;     /* Initialization value. */
+
	Elf64_Xword 	m_info;	     /* Encoded size and index. */
+
	Elf64_Xword	m_poffset;   /* Offset relative to symbol. */
+
	Elf64_Half	m_repeat;    /* Repeat count. */
+
	Elf64_Half	m_stride;    /* Number of units to skip. */
+
} Elf64_Move;
+

+
#define ELF32_M_SYM(I)		((I) >> 8)
+
#define ELF32_M_SIZE(I)		((unsigned char) (I))
+
#define ELF32_M_INFO(M, S)	(((M) << 8) + (unsigned char) (S))
+

+
#define ELF64_M_SYM(I)		((I) >> 8)
+
#define ELF64_M_SIZE(I)		((unsigned char) (I))
+
#define ELF64_M_INFO(M, S)	(((M) << 8) + (unsigned char) (S))
+

+
/*
+
 * Section Header Table (SHDR) entries.
+
 */
+

+
/* 32 bit SHDR */
+
typedef struct {
+
	Elf32_Word	sh_name;     /* index of section name */
+
	Elf32_Word	sh_type;     /* section type */
+
	Elf32_Word	sh_flags;    /* section flags */
+
	Elf32_Addr	sh_addr;     /* in-memory address of section */
+
	Elf32_Off	sh_offset;   /* file offset of section */
+
	Elf32_Word	sh_size;     /* section size in bytes */
+
	Elf32_Word	sh_link;     /* section header table link */
+
	Elf32_Word	sh_info;     /* extra information */
+
	Elf32_Word	sh_addralign; /* alignment constraint */
+
	Elf32_Word	sh_entsize;   /* size for fixed-size entries */
+
} Elf32_Shdr;
+

+
/* 64 bit SHDR */
+
typedef struct {
+
	Elf64_Word	sh_name;     /* index of section name */
+
	Elf64_Word	sh_type;     /* section type */
+
	Elf64_Xword	sh_flags;    /* section flags */
+
	Elf64_Addr	sh_addr;     /* in-memory address of section */
+
	Elf64_Off	sh_offset;   /* file offset of section */
+
	Elf64_Xword	sh_size;     /* section size in bytes */
+
	Elf64_Word	sh_link;     /* section header table link */
+
	Elf64_Word	sh_info;     /* extra information */
+
	Elf64_Xword	sh_addralign; /* alignment constraint */
+
	Elf64_Xword	sh_entsize;  /* size for fixed-size entries */
+
} Elf64_Shdr;
+

+

+
/*
+
 * Symbol table entries.
+
 */
+

+
typedef struct {
+
	Elf32_Word	st_name;     /* index of symbol's name */
+
	Elf32_Addr	st_value;    /* value for the symbol */
+
	Elf32_Word	st_size;     /* size of associated data */
+
	unsigned char	st_info;     /* type and binding attributes */
+
	unsigned char	st_other;    /* visibility */
+
	Elf32_Half	st_shndx;    /* index of related section */
+
} Elf32_Sym;
+

+
typedef struct {
+
	Elf64_Word	st_name;     /* index of symbol's name */
+
	unsigned char	st_info;     /* type and binding attributes */
+
	unsigned char	st_other;    /* visibility */
+
	Elf64_Half	st_shndx;    /* index of related section */
+
	Elf64_Addr	st_value;    /* value for the symbol */
+
	Elf64_Xword	st_size;     /* size of associated data */
+
} Elf64_Sym;
+

+
#define ELF32_ST_BIND(I)	((I) >> 4)
+
#define ELF32_ST_TYPE(I)	((I) & 0xFU)
+
#define ELF32_ST_INFO(B,T)	(((B) << 4) + ((T) & 0xF))
+

+
#define ELF64_ST_BIND(I)	((I) >> 4)
+
#define ELF64_ST_TYPE(I)	((I) & 0xFU)
+
#define ELF64_ST_INFO(B,T)	(((B) << 4) + ((T) & 0xF))
+

+
#define ELF32_ST_VISIBILITY(O)	((O) & 0x3)
+
#define ELF64_ST_VISIBILITY(O)	((O) & 0x3)
+

+
/*
+
 * Syminfo descriptors, containing additional symbol information.
+
 */
+

+
/* 32-bit entry. */
+
typedef struct {
+
	Elf32_Half	si_boundto;  /* Entry index with additional flags. */
+
	Elf32_Half	si_flags;    /* Flags. */
+
} Elf32_Syminfo;
+

+
/* 64-bit entry. */
+
typedef struct {
+
	Elf64_Half	si_boundto;  /* Entry index with additional flags. */
+
	Elf64_Half	si_flags;    /* Flags. */
+
} Elf64_Syminfo;
+

+
/*
+
 * Relocation descriptors.
+
 */
+

+
typedef struct {
+
	Elf32_Addr	r_offset;    /* location to apply relocation to */
+
	Elf32_Word	r_info;	     /* type+section for relocation */
+
} Elf32_Rel;
+

+
typedef struct {
+
	Elf32_Addr	r_offset;    /* location to apply relocation to */
+
	Elf32_Word	r_info;      /* type+section for relocation */
+
	Elf32_Sword	r_addend;    /* constant addend */
+
} Elf32_Rela;
+

+
typedef struct {
+
	Elf64_Addr	r_offset;    /* location to apply relocation to */
+
	Elf64_Xword	r_info;      /* type+section for relocation */
+
} Elf64_Rel;
+

+
typedef struct {
+
	Elf64_Addr	r_offset;    /* location to apply relocation to */
+
	Elf64_Xword	r_info;      /* type+section for relocation */
+
	Elf64_Sxword	r_addend;    /* constant addend */
+
} Elf64_Rela;
+

+

+
#define ELF32_R_SYM(I)		((I) >> 8)
+
#define ELF32_R_TYPE(I)		((unsigned char) (I))
+
#define ELF32_R_INFO(S,T)	(((S) << 8) + (unsigned char) (T))
+

+
#define ELF64_R_SYM(I)		((I) >> 32)
+
#define ELF64_R_TYPE(I)		((I) & 0xFFFFFFFFUL)
+
#define ELF64_R_INFO(S,T)	(((S) << 32) + ((T) & 0xFFFFFFFFUL))
+

+
/*
+
 * Symbol versioning structures.
+
 */
+

+
/* 32-bit structures. */
+
typedef struct
+
{
+
	Elf32_Word	vda_name;    /* Index to name. */
+
	Elf32_Word	vda_next;    /* Offset to next entry. */
+
} Elf32_Verdaux;
+

+
typedef struct
+
{
+
	Elf32_Word	vna_hash;    /* Hash value of dependency name. */
+
	Elf32_Half	vna_flags;   /* Flags. */
+
	Elf32_Half	vna_other;   /* Unused. */
+
	Elf32_Word	vna_name;    /* Offset to dependency name. */
+
	Elf32_Word	vna_next;    /* Offset to next vernaux entry. */
+
} Elf32_Vernaux;
+

+
typedef struct
+
{
+
	Elf32_Half	vd_version;  /* Version information. */
+
	Elf32_Half	vd_flags;    /* Flags. */
+
	Elf32_Half	vd_ndx;	     /* Index into the versym section. */
+
	Elf32_Half	vd_cnt;	     /* Number of aux entries. */
+
	Elf32_Word	vd_hash;     /* Hash value of name. */
+
	Elf32_Word	vd_aux;	     /* Offset to aux entries. */
+
	Elf32_Word	vd_next;     /* Offset to next version definition. */
+
} Elf32_Verdef;
+

+
typedef struct
+
{
+
	Elf32_Half	vn_version;  /* Version number. */
+
	Elf32_Half	vn_cnt;	     /* Number of aux entries. */
+
	Elf32_Word	vn_file;     /* Offset of associated file name. */
+
	Elf32_Word	vn_aux;	     /* Offset of vernaux array. */
+
	Elf32_Word	vn_next;     /* Offset of next verneed entry. */
+
} Elf32_Verneed;
+

+
typedef Elf32_Half	Elf32_Versym;
+

+
/* 64-bit structures. */
+

+
typedef struct {
+
	Elf64_Word	vda_name;    /* Index to name. */
+
	Elf64_Word	vda_next;    /* Offset to next entry. */
+
} Elf64_Verdaux;
+

+
typedef struct {
+
	Elf64_Word	vna_hash;    /* Hash value of dependency name. */
+
	Elf64_Half	vna_flags;   /* Flags. */
+
	Elf64_Half	vna_other;   /* Unused. */
+
	Elf64_Word	vna_name;    /* Offset to dependency name. */
+
	Elf64_Word	vna_next;    /* Offset to next vernaux entry. */
+
} Elf64_Vernaux;
+

+
typedef struct {
+
	Elf64_Half	vd_version;  /* Version information. */
+
	Elf64_Half	vd_flags;    /* Flags. */
+
	Elf64_Half	vd_ndx;	     /* Index into the versym section. */
+
	Elf64_Half	vd_cnt;	     /* Number of aux entries. */
+
	Elf64_Word	vd_hash;     /* Hash value of name. */
+
	Elf64_Word	vd_aux;	     /* Offset to aux entries. */
+
	Elf64_Word	vd_next;     /* Offset to next version definition. */
+
} Elf64_Verdef;
+

+
typedef struct {
+
	Elf64_Half	vn_version;  /* Version number. */
+
	Elf64_Half	vn_cnt;	     /* Number of aux entries. */
+
	Elf64_Word	vn_file;     /* Offset of associated file name. */
+
	Elf64_Word	vn_aux;	     /* Offset of vernaux array. */
+
	Elf64_Word	vn_next;     /* Offset of next verneed entry. */
+
} Elf64_Verneed;
+

+
typedef Elf64_Half	Elf64_Versym;
+

+

+
/*
+
 * The header for GNU-style hash sections.
+
 */
+

+
typedef struct {
+
	uint32_t	gh_nbuckets;	/* Number of hash buckets. */
+
	uint32_t	gh_symndx;	/* First visible symbol in .dynsym. */
+
	uint32_t	gh_maskwords;	/* #maskwords used in bloom filter. */
+
	uint32_t	gh_shift2;	/* Bloom filter shift count. */
+
} Elf_GNU_Hash_Header;
+

+
#endif	/* _ELFDEFINITIONS_H_ */
added external/elf/gelf.3
@@ -0,0 +1,201 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd September 1, 2006
+
.Os
+
.Dt GELF 3
+
.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
+
.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
+
.It "Allocating ELF Data Structures"
+
.Bl -tag -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 -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 -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 -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 -compact -width ".Fn gelf_update_shdr"
+
.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/elf/gelf.h
@@ -0,0 +1,108 @@
+
/*-
+
 * 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.
+
 *
+
 * $Id$
+
 */
+

+
#ifndef	_GELF_H_
+
#define	_GELF_H_
+

+
#include <sys/cdefs.h>
+

+
#include <libelf.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 */
+

+
typedef	Elf64_Cap	GElf_Cap;	/* SW/HW capabilities */
+
typedef Elf64_Move	GElf_Move;	/* Move entries */
+
typedef Elf64_Syminfo	GElf_Syminfo;	/* Symbol information */
+

+
#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);
+

+
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);
+
__END_DECLS
+

+
#endif	/* _GELF_H_ */
added external/elf/gelf_cap.c
@@ -0,0 +1,151 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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 * (size_t) ndx >= d->d_data.d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	if (ec == ELFCLASS32) {
+

+
		cap32 = (Elf32_Cap *) d->d_data.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_data.d_buf + ndx;
+

+
		*dst = *cap64;
+
	}
+

+
	return (dst);
+
}
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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 * (size_t) ndx >= d->d_data.d_size) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (0);
+
	}
+

+
	if (ec == ELFCLASS32) {
+
		cap32 = (Elf32_Cap *) d->d_data.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_data.d_buf + ndx;
+

+
		*cap64 = *gc;
+
	}
+

+
	return (1);
+
}
added external/elf/gelf_checksum.3
@@ -0,0 +1,115 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 29, 2006
+
.Os
+
.Dt GELF_CHECKSUM 3
+
.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/elf/gelf_checksum.c
@@ -0,0 +1,58 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/gelf_dyn.c
@@ -0,0 +1,153 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+
		dyn32 = (Elf32_Dyn *) d->d_data.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_data.d_buf + ndx;
+

+
		*dst = *dyn64;
+
	}
+

+
	return (dst);
+
}
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+
		dyn32 = (Elf32_Dyn *) d->d_data.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_data.d_buf + ndx;
+

+
		*dyn64 = *ds;
+
	}
+

+
	return (1);
+
}
added external/elf/gelf_ehdr.c
@@ -0,0 +1,168 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/gelf_fsize.3
@@ -0,0 +1,96 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd February 5, 2008
+
.Os
+
.Dt GELF_FSIZE 3
+
.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/elf/gelf_fsize.c
@@ -0,0 +1,62 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/gelf_getcap.3
@@ -0,0 +1,121 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 29, 2006
+
.Os
+
.Dt GELF_GETCAP 3
+
.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/elf/gelf_getclass.3
@@ -0,0 +1,61 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd July 3, 2006
+
.Os
+
.Dt GELF_GETCLASS 3
+
.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/elf/gelf_getclass.c
@@ -0,0 +1,39 @@
+
/*-
+
 * 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>
+

+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
int
+
gelf_getclass(Elf *e)
+
{
+
	return (e != NULL ? e->e_class : ELFCLASSNONE);
+
}
added external/elf/gelf_getdyn.3
@@ -0,0 +1,123 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 29, 2006
+
.Os
+
.Dt GELF_GETDYN 3
+
.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/elf/gelf_getehdr.3
@@ -0,0 +1,123 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd December 16, 2006
+
.Os
+
.Dt GELF_GETEHDR 3
+
.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/elf/gelf_getmove.3
@@ -0,0 +1,120 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 29, 2006
+
.Os
+
.Dt GELF_GETMOVE 3
+
.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/elf/gelf_getphdr.3
@@ -0,0 +1,141 @@
+
.\" Copyright (c) 2006-2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd October 21, 2007
+
.Os
+
.Dt GELF_GETPHDR 3
+
.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/elf/gelf_getrel.3
@@ -0,0 +1,121 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 29, 2006
+
.Os
+
.Dt GELF_GETREL 3
+
.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/elf/gelf_getrela.3
@@ -0,0 +1,121 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 29, 2006
+
.Os
+
.Dt GELF_GETRELA 3
+
.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/elf/gelf_getshdr.3
@@ -0,0 +1,115 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 27, 2006
+
.Os
+
.Dt GELF_GETSHDR 3
+
.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/elf/gelf_getsym.3
@@ -0,0 +1,125 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 29, 2006
+
.Os
+
.Dt GELF_GETSYM 3
+
.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/elf/gelf_getsyminfo.3
@@ -0,0 +1,115 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 29, 2006
+
.Os
+
.Dt GELF_GETSYMINFO 3
+
.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/elf/gelf_getsymshndx.3
@@ -0,0 +1,162 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd November 5, 2006
+
.Os
+
.Dt GELF_GETSYMSHNDX 3
+
.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/elf/gelf_move.c
@@ -0,0 +1,160 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+

+
		move32 = (Elf32_Move *) d->d_data.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_data.d_buf + ndx;
+

+
		*dst = *move64;
+
	}
+

+
	return (dst);
+
}
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+
		move32 = (Elf32_Move *) d->d_data.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_data.d_buf + ndx;
+

+
		*move64 = *gm;
+
	}
+

+
	return (1);
+
}
added external/elf/gelf_newehdr.3
@@ -0,0 +1,185 @@
+
.\" Copyright (c) 2006-2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd October 22, 2007
+
.Os
+
.Dt GELF_NEWEHDR 3
+
.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/elf/gelf_newphdr.3
@@ -0,0 +1,133 @@
+
.\" Copyright (c) 2006-2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd October 22, 2007
+
.Os
+
.Dt GELF_NEWPHDR 3
+
.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/elf/gelf_phdr.c
@@ -0,0 +1,178 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/gelf_rel.c
@@ -0,0 +1,163 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+
		rel32 = (Elf32_Rel *) d->d_data.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_data.d_buf + ndx;
+

+
		*dst = *rel64;
+
	}
+

+
	return (dst);
+
}
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+
		rel32 = (Elf32_Rel *) d->d_data.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(
+
			(Elf32_Word) ELF64_R_SYM(dr->r_info),
+
			(Elf32_Word) ELF64_R_TYPE(dr->r_info));
+
	} else {
+
		rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx;
+

+
		*rel64 = *dr;
+
	}
+

+
	return (1);
+
}
added external/elf/gelf_rela.c
@@ -0,0 +1,166 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+
		rela32 = (Elf32_Rela *) d->d_data.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_data.d_buf + ndx;
+

+
		*dst = *rela64;
+
	}
+

+
	return (dst);
+
}
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+
		rela32 = (Elf32_Rela *) d->d_data.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(
+
			(Elf32_Word) ELF64_R_SYM(dr->r_info),
+
			(Elf32_Word) ELF64_R_TYPE(dr->r_info));
+

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

+
		*rela64 = *dr;
+
	}
+

+
	return (1);
+
}
added external/elf/gelf_shdr.c
@@ -0,0 +1,131 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/gelf_sym.c
@@ -0,0 +1,160 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+
		sym32 = (Elf32_Sym *) d->d_data.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  = sym32->st_info;
+
		dst->st_other = sym32->st_other;
+
		dst->st_shndx = sym32->st_shndx;
+
	} else {
+
		sym64 = (Elf64_Sym *) d->d_data.d_buf + ndx;
+

+
		*dst = *sym64;
+
	}
+

+
	return (dst);
+
}
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+
		sym32 = (Elf32_Sym *) d->d_data.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_data.d_buf + ndx;
+

+
		*sym64 = *gs;
+
	}
+

+
	return (1);
+
}
added external/elf/gelf_syminfo.c
@@ -0,0 +1,154 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

+
	if (ec == ELFCLASS32) {
+

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

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

+
	} else {
+

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

+
		*dst = *syminfo64;
+
	}
+

+
	return (dst);
+
}
+

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

+
	d = (struct _Libelf_Data *) ed;
+

+
	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);
+
	assert(ndx >= 0);
+

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

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

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

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

+
		*syminfo64 = *gs;
+
	}
+

+
	return (1);
+
}
added external/elf/gelf_symshndx.c
@@ -0,0 +1,139 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

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

+
	ld = (struct _Libelf_Data *) d;
+
	lid = (struct _Libelf_Data *) id;
+

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

+
	if (lid == NULL || (scn = lid->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL || (e != ld->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);
+
	assert(ndx >= 0);
+

+
	if (msz * (size_t) 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;
+
	size_t msz;
+
	Elf_Scn *scn;
+
	uint32_t sh_type;
+
	struct _Libelf_Data *ld, *lid;
+

+
	ld = (struct _Libelf_Data *) d;
+
	lid = (struct _Libelf_Data *) id;
+

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

+
	if (lid == NULL || (scn = lid->d_scn) == NULL ||
+
	    (e = scn->s_elf) == NULL || (e != ld->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);
+
	assert(ndx >= 0);
+

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

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

+
	return (1);
+
}
added external/elf/gelf_update_ehdr.3
@@ -0,0 +1,123 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd August 27, 2006
+
.Os
+
.Dt GELF_UPDATE_EHDR 3
+
.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/elf/gelf_xlate.c
@@ -0,0 +1,81 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/gelf_xlatetof.3
@@ -0,0 +1,247 @@
+
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
+
.\"
+
.\" Redistribution and use in source and binary forms, with or without
+
.\" modification, are permitted provided that the following conditions
+
.\" are met:
+
.\" 1. Redistributions of source code must retain the above copyright
+
.\"    notice, this list of conditions and the following disclaimer.
+
.\" 2. Redistributions in binary form must reproduce the above copyright
+
.\"    notice, this list of conditions and the following disclaimer in the
+
.\"    documentation and/or other materials provided with the distribution.
+
.\"
+
.\" This software is provided by Joseph Koshy ``as is'' and
+
.\" any express or implied warranties, including, but not limited to, the
+
.\" implied warranties of merchantability and fitness for a particular purpose
+
.\" are disclaimed.  in no event shall Joseph Koshy be liable
+
.\" for any direct, indirect, incidental, special, exemplary, or consequential
+
.\" damages (including, but not limited to, procurement of substitute goods
+
.\" or services; loss of use, data, or profits; or business interruption)
+
.\" however caused and on any theory of liability, whether in contract, strict
+
.\" liability, or tort (including negligence or otherwise) arising in any way
+
.\" out of the use of this software, even if advised of the possibility of
+
.\" such damage.
+
.\"
+
.\" $Id$
+
.\"
+
.Dd July 24, 2006
+
.Os
+
.Dt GELF_XLATETOF 3
+
.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/elf/libelf.h
@@ -0,0 +1,253 @@
+
/*-
+
 * 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 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.
+
 *
+
 * $Id$
+
 */
+

+
#ifndef	_LIBELF_H_
+
#define	_LIBELF_H_
+

+
#include <sys/types.h>
+

+
#include <elfdefinitions.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;
+
} 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;
+

+
	/*
+
	 * Members that are not part of the public API.
+
	 */
+
	unsigned int	ar_flags;
+
} 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 */
+

+
/* ELF(3) API extensions. */
+
#define	ELF_F_ARCHIVE	   0x100U /* archive creation */
+
#define	ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */
+

+
#include <sys/cdefs.h>
+
__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_flagarhdr(Elf_Arhdr *_arh, Elf_Cmd _cmd,
+
			unsigned int _flags);
+
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);
+
Elf		*elf_open(int _fd);
+
Elf		*elf_openmemory(char *_image, size_t _size);
+
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);
+
__END_DECLS
+

+
#endif	/* _LIBELF_H_ */
added external/elf/libelf_align.c
@@ -0,0 +1,137 @@
+
/*-
+
 * 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>
+

+
#include <sys/types.h>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
struct align {
+
	unsigned int a32;
+
	unsigned 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 },
+
	[ELF_T_CAP]	= MALIGN(Cap),
+
	[ELF_T_DYN]	= MALIGN(Dyn),
+
	[ELF_T_EHDR]	= MALIGN(Ehdr),
+
	[ELF_T_HALF]	= MALIGN(Half),
+
	[ELF_T_LWORD]	= MALIGN(Lword),
+
	[ELF_T_MOVE]	= MALIGN(Move),
+
	[ELF_T_MOVEP] 	= UNSUPPORTED(),
+
	[ELF_T_NOTE]	= MALIGN(Nhdr),
+
	[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),
+
	[ELF_T_SYMINFO]	= MALIGN(Syminfo),
+
	[ELF_T_VDEF]	= MALIGN(Verdef),
+
	[ELF_T_VNEED]	= MALIGN(Verneed),
+
	[ELF_T_WORD]	= MALIGN(Word),
+
	[ELF_T_XWORD]	= MALIGN64(Xword),
+
	[ELF_T_GNUHASH] = MALIGN_WORD()
+
};
+

+
unsigned 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),
+
	[ELF_T_CAP]	= FALIGN(4,8),
+
	[ELF_T_DYN]	= FALIGN(4,8),
+
	[ELF_T_EHDR]	= FALIGN(4,8),
+
	[ELF_T_HALF]	= FALIGN(2,2),
+
	[ELF_T_LWORD]	= FALIGN(8,8),
+
	[ELF_T_MOVE]	= FALIGN(8,8),
+
	[ELF_T_MOVEP] 	= UNSUPPORTED(),
+
	[ELF_T_NOTE]	= FALIGN(4,4),
+
	[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),
+
	[ELF_T_SYMINFO]	= FALIGN(2,2),
+
	[ELF_T_VDEF]	= FALIGN(4,4),
+
	[ELF_T_VNEED]	= FALIGN(4,4),
+
	[ELF_T_WORD]	= FALIGN(4,4),
+
	[ELF_T_XWORD]	= FALIGN(0,8),
+
	[ELF_T_GNUHASH] = FALIGN(4,8)
+
};
+

+
unsigned 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/elf/libelf_allocate.c
@@ -0,0 +1,212 @@
+
/*-
+
 * 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 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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_hdr.e_rawhdr = 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)
+
{
+
	Elf_Arhdr *arh;
+

+
	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_flags & LIBELF_F_AR_HEADER) {
+
			arh = e->e_hdr.e_arhdr;
+
			FREE(arh->ar_name);
+
			FREE(arh->ar_rawname);
+
			free(arh);
+
		}
+

+
		break;
+

+
	default:
+
		break;
+
	}
+

+
	free(e);
+

+
	return (NULL);
+
}
+

+
struct _Libelf_Data *
+
_libelf_allocate_data(Elf_Scn *s)
+
{
+
	struct _Libelf_Data *d;
+

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

+
	d->d_scn = s;
+

+
	return (d);
+
}
+

+
struct _Libelf_Data *
+
_libelf_release_data(struct _Libelf_Data *d)
+
{
+

+
	if (d->d_flags & LIBELF_F_DATA_MALLOCED)
+
		free(d->d_data.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;
+
	struct _Libelf_Data *d, *td;
+

+
	assert(s != NULL);
+

+
	STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
+
		STAILQ_REMOVE(&s->s_data, d, _Libelf_Data, d_next);
+
		d = _libelf_release_data(d);
+
	}
+

+
	STAILQ_FOREACH_SAFE(d, &s->s_rawdata, d_next, td) {
+
		assert((d->d_flags & LIBELF_F_DATA_MALLOCED) == 0);
+
		STAILQ_REMOVE(&s->s_rawdata, d, _Libelf_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/elf/libelf_ar.c
@@ -0,0 +1,468 @@
+
/*-
+
 * 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 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>
+

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

+
#include "_libelf.h"
+
#include "_libelf_ar.h"
+

+
ELFTC_VCSID("$Id$");
+

+
#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:
+
 *
+
 * There are two variants of the `ar' archive format: traditional BSD
+
 * and SVR4.  These differ in the way long file names are treated, and
+
 * in the layout of the archive symbol table.
+
 *
+
 * The `ar' header only has space for a 16 character file name.
+
 *
+
 * In the SVR4 format, file names are terminated with a '/', so this
+
 * effectively leaves 15 characters for the actual file name.  Longer
+
 * file names stored in a separate 'string table' and referenced
+
 * indirectly from the name field.  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.
+
 *
+
 * In the BSD format, file names can be upto 16 characters.  File
+
 * names shorter than 16 characters are padded to 16 characters using
+
 * (ASCII) space characters.  File names with embedded spaces and file
+
 * names longer than 16 characters are stored immediately after the
+
 * archive header and the name field set to a special indirect name
+
 * matching the pattern "#1/[0-9]+".  The digits form a decimal number
+
 * that corresponds to the actual length of the file name following
+
 * the archive header.  The content of the archive member immediately
+
 * follows the file name, and the size field of the archive member
+
 * holds the sum of the sizes of the member and of the appended file
+
 * name.
+
 *
+
 * Archives may also have a symbol table (see ranlib(1)), mapping
+
 * program symbols to object files inside the archive.
+
 *
+
 * In the SVR4 format, 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.
+
 *
+
 * In the BSD format, the symbol table uses a file name of "__.SYMDEF".
+
 * It is structured as two parts:
+
 *  - The first part is an array of "ranlib" structures preceded by
+
 *    the size of the array in bytes.  Each "ranlib" structure
+
 *    describes one symbol.  Each structure contains an offset into
+
 *    the string table for the symbol name, and a file offset into the
+
 *    archive for the member defining the symbol.
+
 *  - The second part is a string table containing NUL-terminated
+
 *    strings, preceded by the size of the string table in bytes.
+
 *
+
 * If the symbol table and string table are is present in an archive
+
 * they must be the very first objects and in that order.
+
 */
+

+

+
/*
+
 * Retrieve an archive header descriptor.
+
 */
+

+
Elf_Arhdr *
+
_libelf_ar_gethdr(Elf *e)
+
{
+
	Elf *parent;
+
	Elf_Arhdr *eh;
+
	char *namelen;
+
	size_t n, nlen;
+
	struct ar_hdr *arh;
+

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

+
	assert((e->e_flags & LIBELF_F_AR_HEADER) == 0);
+

+
	arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr;
+

+
	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_hdr.e_arhdr = eh;
+
	e->e_flags |= LIBELF_F_AR_HEADER;
+

+
	eh->ar_name = eh->ar_rawname = NULL;
+

+
	if ((eh->ar_name = _libelf_ar_get_translated_name(arh, 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;
+

+
	/*
+
	 * Get the true size of the member if extended naming is being used.
+
	 */
+
	if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
+
		namelen = arh->ar_name +
+
		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
+
		if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
+
		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nlen) == 0)
+
			goto error;
+
		n -= nlen;
+
	}
+

+
	eh->ar_size = n;
+

+
	if ((eh->ar_rawname = _libelf_ar_get_raw_name(arh)) == NULL)
+
		goto error;
+

+
	eh->ar_flags = 0;
+

+
	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_flags &= ~LIBELF_F_AR_HEADER;
+
	e->e_hdr.e_rawhdr = (unsigned char *) arh;
+

+
	return (NULL);
+
}
+

+
Elf *
+
_libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
+
{
+
	Elf *e;
+
	off_t next;
+
	size_t nsz, sz;
+
	struct ar_hdr *arh;
+
	char *member, *namelen;
+

+
	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);
+

+
	/*
+
	 * Retrieve the size of the member.
+
	 */
+
	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
+
	    &sz) == 0) {
+
		LIBELF_SET_ERROR(ARCHIVE, 0);
+
		return (NULL);
+
	}
+

+
	/*
+
	 * Adjust the size field for members in BSD archives using
+
	 * extended naming.
+
	 */
+
	if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
+
		namelen = arh->ar_name +
+
		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
+
		if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
+
		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nsz) == 0) {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			return (NULL);
+
		}
+

+
		member = (char *) (arh + 1) + nsz;
+
		sz -= nsz;
+
	} else
+
		member = (char *) (arh + 1);
+

+

+
	if ((e = elf_memory(member, sz)) == NULL)
+
		return (NULL);
+

+
	e->e_fd = fd;
+
	e->e_cmd = c;
+
	e->e_hdr.e_rawhdr = (unsigned char *) arh;
+

+
	elf->e_u.e_ar.e_nchildren++;
+
	e->e_parent = elf;
+

+
	return (e);
+
}
+

+
/*
+
 * A BSD-style ar(1) symbol table has the following layout:
+
 *
+
 * - A count of bytes used by the following array of 'ranlib'
+
 *   structures, stored as a 'long'.
+
 * - An array of 'ranlib' structures.  Each array element is
+
 *   two 'long's in size.
+
 * - A count of bytes used for the following symbol table.
+
 * - The symbol table itself.
+
 */
+

+
/*
+
 * A helper macro to read in a 'long' value from the archive.
+
 *
+
 * We use memcpy() since the source pointer may be misaligned with
+
 * respect to the natural alignment for a C 'long'.
+
 */
+
#define	GET_LONG(P, V)do {				\
+
		memcpy(&(V), (P), sizeof(long));	\
+
		(P) += sizeof(long);			\
+
	} while (0)
+

+
Elf_Arsym *
+
_libelf_ar_process_bsd_symtab(Elf *e, size_t *count)
+
{
+
	Elf_Arsym *symtab, *sym;
+
	unsigned int n, nentries;
+
	unsigned char *end, *p, *p0, *s, *s0;
+
	const size_t entrysize = 2 * sizeof(long);
+
	long arraysize, fileoffset, stroffset, strtabsize;
+

+
	assert(e != NULL);
+
	assert(count != NULL);
+
	assert(e->e_u.e_ar.e_symtab == NULL);
+

+
	symtab = NULL;
+

+
	/*
+
	 * The BSD symbol table always contains the count fields even
+
	 * if there are no entries in it.
+
	 */
+
	if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long))
+
		goto symtaberror;
+

+
	p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
+
	end = p0 + e->e_u.e_ar.e_rawsymtabsz;
+

+
	/*
+
	 * Retrieve the size of the array of ranlib descriptors and
+
	 * check it for validity.
+
	 */
+
	GET_LONG(p, arraysize);
+

+
	if (arraysize < 0 || p0 + arraysize >= end ||
+
	    ((size_t) arraysize % entrysize != 0))
+
		goto symtaberror;
+

+
	/*
+
	 * Check the value of the string table size.
+
	 */
+
	s = p + arraysize;
+
	GET_LONG(s, strtabsize);
+

+
	s0 = s;			/* Start of string table. */
+
	if (strtabsize < 0 || s0 + strtabsize > end)
+
		goto symtaberror;
+

+
	nentries = (size_t) arraysize / entrysize;
+

+
	/*
+
	 * Allocate space for the returned Elf_Arsym array.
+
	 */
+
	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	/* Read in symbol table entries. */
+
	for (n = 0, sym = symtab; n < nentries; n++, sym++) {
+
		GET_LONG(p, stroffset);
+
		GET_LONG(p, fileoffset);
+

+
		if (stroffset < 0 || fileoffset <  0 ||
+
		    (size_t) fileoffset >= e->e_rawsize)
+
			goto symtaberror;
+

+
		s = s0 + stroffset;
+

+
		if (s >= end)
+
			goto symtaberror;
+

+
		sym->as_off = (off_t) fileoffset;
+
		sym->as_hash = elf_hash((char *) s);
+
		sym->as_name = (char *) s;
+
	}
+

+
	/* Fill up the sentinel entry. */
+
	sym->as_name = NULL;
+
	sym->as_hash = ~0UL;
+
	sym->as_off = (off_t) 0;
+

+
	/* Remember the processed symbol table. */
+
	e->e_u.e_ar.e_symtab = symtab;
+

+
	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
+

+
	return (symtab);
+

+
symtaberror:
+
	if (symtab)
+
		free(symtab);
+
	LIBELF_SET_ERROR(ARCHIVE, 0);
+
	return (NULL);
+
}
+

+
/*
+
 * An SVR4-style 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_svr4_symtab(Elf *e, size_t *count)
+
{
+
	uint32_t off;
+
	size_t n, nentries;
+
	Elf_Arsym *symtab, *sym;
+
	unsigned char *p, *s, *end;
+

+
	assert(e != NULL);
+
	assert(count != NULL);
+
	assert(e->e_u.e_ar.e_symtab == NULL);
+

+
	symtab = NULL;
+

+
	if (e->e_u.e_ar.e_rawsymtabsz < INTSZ)
+
		goto symtaberror;
+

+
	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)
+
		goto symtaberror;
+

+
	/* 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--) {
+
		if (s >= end)
+
			goto symtaberror;
+

+
		GET_WORD(p, off);
+
		if (off >= e->e_rawsize)
+
			goto symtaberror;
+

+
		sym->as_off = (off_t) off;
+
		sym->as_hash = elf_hash((char *) s);
+
		sym->as_name = (char *) s;
+

+
		p += INTSZ;
+
		sym++;
+

+
		for (; s < end && *s++ != '\0';) /* skip to next string */
+
			;
+
	}
+

+
	/* 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);
+

+
symtaberror:
+
	if (symtab)
+
		free(symtab);
+
	LIBELF_SET_ERROR(ARCHIVE, 0);
+
	return (NULL);
+
}
added external/elf/libelf_ar_util.c
@@ -0,0 +1,362 @@
+
/*-
+
 * Copyright (c) 2006,2009,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 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>
+

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

+
#include "_libelf.h"
+
#include "_libelf_ar.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
+
 * number in the specified base.
+
 */
+
int
+
_libelf_ar_get_number(const char *src, size_t sz, unsigned int base,
+
    size_t *ret)
+
{
+
	size_t r;
+
	unsigned int c, v;
+
	const unsigned char *e, *s;
+

+
	assert(base <= 10);
+

+
	s = (const unsigned char *) src;
+
	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);
+
}
+

+
/*
+
 * Return the translated name for an archive member.
+
 */
+
char *
+
_libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
+
{
+
	char *s;
+
	unsigned char c;
+
	size_t len, offset;
+
	const unsigned char *buf, *p, *q, *r;
+
	const size_t bufsize = sizeof(arh->ar_name);
+

+
	assert(arh != NULL);
+
	assert(ar->e_kind == ELF_K_AR);
+
	assert((const unsigned char *) arh >= ar->e_rawfile &&
+
	    (const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize);
+

+
	buf = (const unsigned char *) arh->ar_name;
+

+
	/*
+
	 * Check for extended naming.
+
	 *
+
	 * If the name matches the pattern "^/[0-9]+", it is an
+
	 * SVR4-style extended name.  If the name matches the pattern
+
	 * "#1/[0-9]+", the entry uses BSD style extended naming.
+
	 */
+
	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((const char *) (buf + 1),
+
			bufsize - 1, 10, &offset) == 0) {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			return (NULL);
+
		}
+

+
		if (offset > ar->e_u.e_ar.e_rawstrtabsz) {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			return (NULL);
+
		}
+

+
		p = q = ar->e_u.e_ar.e_rawstrtab + offset;
+
		r = ar->e_u.e_ar.e_rawstrtab + ar->e_u.e_ar.e_rawstrtabsz;
+

+
		for (; p < r && *p != '/'; p++)
+
			;
+
		len = (size_t) (p - q + 1); /* space for the trailing NUL */
+

+
		if ((s = malloc(len)) == NULL) {
+
			LIBELF_SET_ERROR(RESOURCE, 0);
+
			return (NULL);
+
		}
+

+
		(void) strncpy(s, (const char *) q, len - 1);
+
		s[len - 1] = '\0';
+

+
		return (s);
+
	} else if (IS_EXTENDED_BSD_NAME(buf)) {
+
		r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
+

+
		if (_libelf_ar_get_number((const char *) r, bufsize -
+
			LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10,
+
			&len) == 0) {
+
			LIBELF_SET_ERROR(ARCHIVE, 0);
+
			return (NULL);
+
		}
+

+
		/*
+
		 * Allocate space for the file name plus a
+
		 * trailing NUL.
+
		 */
+
		if ((s = malloc(len + 1)) == NULL) {
+
			LIBELF_SET_ERROR(RESOURCE, 0);
+
			return (NULL);
+
		}
+

+
		/*
+
		 * The file name follows the archive header.
+
		 */
+
		q = (const unsigned char *) (arh + 1);
+

+
		(void) strncpy(s, (const char *) q, len);
+
		s[len] = '\0';
+

+
		return (s);
+
	}
+

+
	/*
+
	 * A 'normal' name.
+
	 *
+
	 * Skip back over trailing blanks from the end of the field.
+
	 * In the SVR4 format, a '/' is used as a terminator for
+
	 * non-special names.
+
	 */
+
	for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
+
		;
+

+
	if (q >= buf) {
+
		if (*q == '/') {
+
			/*
+
			 * SVR4 style names: ignore the trailing
+
			 * character '/', but only if the name is not
+
			 * one of the special names "/" and "//".
+
			 */
+
			if (q > buf + 1 ||
+
			    (q == (buf + 1) && *buf != '/'))
+
				q--;
+
		}
+

+
		len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */
+
	} else {
+
		/* The buffer only had blanks. */
+
		buf = (const unsigned char *) "";
+
		len = 1;
+
	}
+

+
	if ((s = malloc(len)) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	(void) strncpy(s, (const char *) buf, len - 1);
+
	s[len - 1] = '\0';
+

+
	return (s);
+
}
+

+
/*
+
 * Return the raw name for an archive member, inclusive of any
+
 * formatting characters.
+
 */
+
char *
+
_libelf_ar_get_raw_name(const struct ar_hdr *arh)
+
{
+
	char *rawname;
+
	const size_t namesz = sizeof(arh->ar_name);
+

+
	if ((rawname = malloc(namesz + 1)) == NULL) {
+
		LIBELF_SET_ERROR(RESOURCE, 0);
+
		return (NULL);
+
	}
+

+
	(void) strncpy(rawname, arh->ar_name, namesz);
+
	rawname[namesz] = '\0';
+
	return (rawname);
+
}
+

+
/*
+
 * Open an 'ar' archive.
+
 */
+
Elf *
+
_libelf_ar_open(Elf *e, int reporterror)
+
{
+
	size_t sz;
+
	int scanahead;
+
	struct ar_hdr arh;
+
	unsigned char *s, *end;
+

+
	_libelf_init_elf(e, 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);
+

+
	/*
+
	 * We use heuristics to determine the flavor of the archive we
+
	 * are examining.
+
	 *
+
	 * SVR4 flavor archives use the name "/ " and "// " for
+
	 * special members.
+
	 *
+
	 * In BSD flavor archives the symbol table, if present, is the
+
	 * first archive with name "__.SYMDEF".
+
	 */
+

+
#define	READ_AR_HEADER(S, ARH, SZ, END)					\
+
	do {								\
+
		if ((S) + sizeof((ARH)) > (END))			\
+
		        goto error;					\
+
		(void) memcpy(&(ARH), (S), sizeof((ARH)));		\
+
		if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
+
			goto error;					\
+
		if (_libelf_ar_get_number((char *) (ARH).ar_size,	\
+
		    sizeof((ARH).ar_size), 10, &(SZ)) == 0)		\
+
			goto error;					\
+
	} while (0)
+

+
	READ_AR_HEADER(s, arh, sz, end);
+

+
	/*
+
	 * Handle special archive members for the SVR4 format.
+
	 */
+
	if (arh.ar_name[0] == '/') {
+
		if (sz == 0)
+
			goto error;
+

+
		e->e_flags |= LIBELF_F_AR_VARIANT_SVR4;
+

+
		scanahead = 0;
+

+
		/*
+
		 * The symbol table (file name "/ ") always comes before the
+
		 * string table (file name "// ").
+
		 */
+
		if (arh.ar_name[1] == ' ') {
+
			/* "/ " => symbol table. */
+
			scanahead = 1;	/* The string table to follow. */
+

+
			s += sizeof(arh);
+
			e->e_u.e_ar.e_rawsymtab = s;
+
			e->e_u.e_ar.e_rawsymtabsz = sz;
+

+
			sz = LIBELF_ADJUST_AR_SIZE(sz);
+
			s += sz;
+

+
		} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
+
			/* "// " => string table for long file names. */
+
			s += sizeof(arh);
+
			e->e_u.e_ar.e_rawstrtab = s;
+
			e->e_u.e_ar.e_rawstrtabsz = sz;
+

+
			sz = LIBELF_ADJUST_AR_SIZE(sz);
+
			s += sz;
+
		}
+

+
		/*
+
		 * If the string table hasn't been seen yet, look for
+
		 * it in the next member.
+
		 */
+
		if (scanahead) {
+
			READ_AR_HEADER(s, arh, sz, end);
+

+
			/* "// " => string table for long file names. */
+
			if (arh.ar_name[0] == '/' && arh.ar_name[1] == '/' &&
+
			    arh.ar_name[2] == ' ') {
+

+
				s += sizeof(arh);
+

+
				e->e_u.e_ar.e_rawstrtab = s;
+
				e->e_u.e_ar.e_rawstrtabsz = sz;
+

+
				sz = LIBELF_ADJUST_AR_SIZE(sz);
+
				s += sz;
+
			}
+
		}
+
	} else if (strncmp(arh.ar_name, LIBELF_AR_BSD_SYMTAB_NAME,
+
		sizeof(LIBELF_AR_BSD_SYMTAB_NAME) - 1) == 0) {
+
		/*
+
		 * BSD style archive symbol table.
+
		 */
+
		s += sizeof(arh);
+
		e->e_u.e_ar.e_rawsymtab = s;
+
		e->e_u.e_ar.e_rawsymtabsz = sz;
+

+
		sz = LIBELF_ADJUST_AR_SIZE(sz);
+
		s += sz;
+
	}
+

+
	/*
+
	 * Update the 'next' offset, so that a subsequent elf_begin()
+
	 * works as expected.
+
	 */
+
	e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
+

+
	return (e);
+

+
error:
+
	if (!reporterror) {
+
		e->e_kind = ELF_K_NONE;
+
		return (e);
+
	}
+

+
	LIBELF_SET_ERROR(ARCHIVE, 0);
+
	return (NULL);
+
}
added external/elf/libelf_checksum.c
@@ -0,0 +1,100 @@
+
/*-
+
 * 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>
+

+
#include <gelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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);
+
}
+

+
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, (size_t) d->d_size);
+
	}
+

+
	/*
+
	 * Return a 16-bit checksum compatible with Solaris.
+
	 */
+
	return (long) (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL));
+
}
added external/elf/libelf_convert.m4
@@ -0,0 +1,1089 @@
+
/*-
+
 * Copyright (c) 2006-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 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/* WARNING: GENERATED FROM __file__. */
+

+
divert(-1)
+

+
# Generate conversion routines for converting between in-memory and
+
# file representations of Elf data structures.
+
#
+
# These conversions use the type information defined in `elf_types.m4'.
+

+
include(SRCDIR`/elf_types.m4')
+

+
# For the purposes of generating conversion code, ELF types may be
+
# classified according to the following characteristics:
+
#
+
# 1. Whether the ELF type can be directly mapped to an integral C
+
#    language type.  For example, the ELF_T_WORD type maps directly to
+
#    a 'uint32_t', but ELF_T_GNUHASH lacks a matching C type.
+
#
+
# 2. Whether the type has word size dependent variants.  For example,
+
#    ELT_T_EHDR is represented using C types Elf32_Ehdr and El64_Ehdr,
+
#    and the ELF_T_ADDR and ELF_T_OFF types have integral C types that
+
#    can be 32- or 64- bit wide.
+
#
+
# 3. Whether the ELF types has a fixed representation or not.  For
+
#    example, the ELF_T_SYM type has a fixed size file representation,
+
#    some types like ELF_T_NOTE and ELF_T_GNUHASH use a variable size
+
#    representation.
+
#
+
# We use m4 macros to generate conversion code for ELF types that have
+
# a fixed size representation.  Conversion functions for the remaining
+
# types are coded by hand.
+
#
+
#* Handling File and Memory Representations
+
#
+
# `In-memory' representations of an Elf data structure use natural
+
# alignments and native byte ordering.  This allows pointer arithmetic
+
# and casting to work as expected.  On the other hand, the `file'
+
# representation of an ELF data structure could possibly be packed
+
# tighter than its `in-memory' representation, and could be of a
+
# differing byte order.  Reading ELF objects that are members of `ar'
+
# archives present an additional complication: `ar' pads file data to
+
# even addresses, so file data structures in an archive member
+
# residing inside an `ar' archive could be at misaligned memory
+
# addresses when brought into memory.
+
#
+
# In summary, 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.
+

+
# NOCVT(TYPE) -- Do not generate the cvt[] structure entry for TYPE
+
define(`NOCVT',`define(`NOCVT_'$1,1)')
+

+
# NOFUNC(TYPE) -- Do not generate a conversion function for TYPE
+
define(`NOFUNC',`define(`NOFUNC_'$1,1)')
+

+
# IGNORE(TYPE) -- Completely ignore the type.
+
define(`IGNORE',`NOCVT($1)NOFUNC($1)')
+

+
# Mark ELF types that should not be processed by the M4 macros below.
+

+
# Types for which we use functions with non-standard names.
+
IGNORE(`BYTE')			# Uses a wrapper around memcpy().
+
IGNORE(`NOTE')			# Not a fixed size type.
+

+
# Types for which we supply hand-coded functions.
+
NOFUNC(`GNUHASH')		# A type with complex internal structure.
+
NOFUNC(`VDEF')			# See MAKE_VERSION_CONVERTERS below.
+
NOFUNC(`VNEED')			# ..
+

+
# Unimplemented types.
+
IGNORE(`MOVEP')
+

+
# ELF types that don't exist in a 32-bit world.
+
NOFUNC(`XWORD32')
+
NOFUNC(`SXWORD32')
+

+
# `Primitive' ELF types are those that are an alias for an integral
+
# type.  As they have no internal structure, they can be copied using
+
# a `memcpy()', and byteswapped in straightforward way.
+
#
+
# Mark all ELF types that directly map to integral C types.
+
define(`PRIM_ADDR',	1)
+
define(`PRIM_BYTE',	1)
+
define(`PRIM_HALF',	1)
+
define(`PRIM_LWORD',	1)
+
define(`PRIM_OFF',	1)
+
define(`PRIM_SWORD',	1)
+
define(`PRIM_SXWORD',	1)
+
define(`PRIM_WORD',	1)
+
define(`PRIM_XWORD',	1)
+

+
# Note the primitive types that are size-dependent.
+
define(`SIZEDEP_ADDR',	1)
+
define(`SIZEDEP_OFF',	1)
+

+
# Generate conversion functions for primitive types.
+
#
+
# Macro use: MAKEPRIMFUNCS(ELFTYPE,CTYPE,TYPESIZE,SYMSIZE)
+
# `$1': Name of the ELF type.
+
# `$2': C structure name suffix.
+
# `$3': ELF class specifier for types, one of [`32', `64'].
+
# `$4': Additional ELF class specifier, one of [`', `32', `64'].
+
#
+
# Generates a pair of conversion functions.
+
define(`MAKEPRIMFUNCS',`
+
static int
+
_libelf_cvt_$1$4_tof(unsigned char *dst, size_t dsz, unsigned char *src,
+
    size_t count, int byteswap)
+
{
+
	Elf$3_$2 t, *s = (Elf$3_$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$4(t);
+
		WRITE_$1$4(dst,t);
+
	}
+

+
	return (1);
+
}
+

+
static int
+
_libelf_cvt_$1$4_tom(unsigned char *dst, size_t dsz, unsigned char *src,
+
    size_t count, int byteswap)
+
{
+
	Elf$3_$2 t, *d = (Elf$3_$2 *) (uintptr_t) dst;
+
	size_t c;
+

+
	if (dsz < count * sizeof(Elf$3_$2))
+
		return (0);
+

+
	if (!byteswap) {
+
		(void) memcpy(dst, src, count * sizeof(*d));
+
		return (1);
+
	}
+

+
	for (c = 0; c < count; c++) {
+
		READ_$1$4(src,t);
+
		SWAP_$1$4(t);
+
		*d++ = t;
+
	}
+

+
	return (1);
+
}
+
')
+

+
#
+
# Handling composite ELF types
+
#
+

+
# SWAP_FIELD(FIELDNAME,ELFTYPE) -- Generate code to swap one field.
+
define(`SWAP_FIELD',
+
  `ifdef(`SIZEDEP_'$2,
+
    `SWAP_$2'SZ()`(t.$1);
+
			',
+
    `SWAP_$2(t.$1);
+
			')')
+

+
# SWAP_MEMBERS(STRUCT) -- Iterate over a structure definition.
+
define(`SWAP_MEMBERS',
+
  `ifelse($#,1,`/**/',
+
     `SWAP_FIELD($1)SWAP_MEMBERS(shift($@))')')
+

+
# SWAP_STRUCT(CTYPE,SIZE) -- Generate code to swap an ELF structure.
+
define(`SWAP_STRUCT',
+
  `pushdef(`SZ',$2)/* Swap an Elf$2_$1 */
+
			SWAP_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
+

+
# WRITE_FIELD(ELFTYPE,FIELDNAME) -- Generate code to write one field.
+
define(`WRITE_FIELD',
+
  `ifdef(`SIZEDEP_'$2,
+
    `WRITE_$2'SZ()`(dst,t.$1);
+
		',
+
    `WRITE_$2(dst,t.$1);
+
		')')
+

+
# WRITE_MEMBERS(ELFTYPELIST) -- Iterate over a structure definition.
+
define(`WRITE_MEMBERS',
+
  `ifelse($#,1,`/**/',
+
    `WRITE_FIELD($1)WRITE_MEMBERS(shift($@))')')
+

+
# WRITE_STRUCT(CTYPE,SIZE) -- Generate code to write out an ELF structure.
+
define(`WRITE_STRUCT',
+
  `pushdef(`SZ',$2)/* Write an Elf$2_$1 */
+
		WRITE_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
+

+
# READ_FIELD(ELFTYPE,CTYPE) -- Generate code to read one field.
+
define(`READ_FIELD',
+
  `ifdef(`SIZEDEP_'$2,
+
    `READ_$2'SZ()`(s,t.$1);
+
		',
+
    `READ_$2(s,t.$1);
+
		')')
+

+
# READ_MEMBERS(ELFTYPELIST) -- Iterate over a structure definition.
+
define(`READ_MEMBERS',
+
  `ifelse($#,1,`/**/',
+
    `READ_FIELD($1)READ_MEMBERS(shift($@))')')
+

+
# READ_STRUCT(CTYPE,SIZE) -- Generate code to read an ELF structure.
+
define(`READ_STRUCT',
+
  `pushdef(`SZ',$2)/* Read an Elf$2_$1 */
+
		READ_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
+

+

+
# MAKECOMPFUNCS -- Generate converters for composite ELF 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(`MAKECOMPFUNCS', `ifdef(`NOFUNC_'$1$3,`',`
+
static int
+
_libelf_cvt_$1$3_tof(unsigned char *dst, size_t dsz, unsigned 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);
+
}
+

+
static int
+
_libelf_cvt_$1$3_tom(unsigned char *dst, size_t dsz, unsigned char *src,
+
    size_t count, int byteswap)
+
{
+
	Elf$3_$2	t, *d;
+
	unsigned 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  = 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_CONVERTER(ELFTYPE,CTYPE)
+
#
+
# Make type convertor functions from the type definition
+
# of the ELF type:
+
# - Skip convertors marked as `NOFUNC'.
+
# - Invoke `MAKEPRIMFUNCS' or `MAKECOMPFUNCS' as appropriate.
+
define(`MAKE_TYPE_CONVERTER',
+
  `ifdef(`NOFUNC_'$1,`',
+
    `ifdef(`PRIM_'$1,
+
      `ifdef(`SIZEDEP_'$1,
+
	`MAKEPRIMFUNCS($1,$2,32,32)dnl
+
	 MAKEPRIMFUNCS($1,$2,64,64)',
+
	`MAKEPRIMFUNCS($1,$2,64)')',
+
      `MAKECOMPFUNCS($1,$2,32)dnl
+
       MAKECOMPFUNCS($1,$2,64)')')')
+

+
# MAKE_TYPE_CONVERTERS(ELFTYPELIST) -- Generate conversion functions.
+
define(`MAKE_TYPE_CONVERTERS',
+
  `ifelse($#,1,`',
+
    `MAKE_TYPE_CONVERTER($1)MAKE_TYPE_CONVERTERS(shift($@))')')
+

+

+
#
+
# Macros to generate entries for the table of convertors.
+
#
+

+
# CONV(ELFTYPE,SIZE,DIRECTION)
+
#
+
# Generate the name of a convertor function.
+
define(`CONV',
+
  `ifdef(`NOFUNC_'$1$2,
+
    `.$3$2 = NULL',
+
    `ifdef(`PRIM_'$1,
+
      `ifdef(`SIZEDEP_'$1,
+
	`.$3$2 = _libelf_cvt_$1$2_$3',
+
	`.$3$2 = _libelf_cvt_$1_$3')',
+
      `.$3$2 = _libelf_cvt_$1$2_$3')')')
+

+
# CONVERTER_NAME(ELFTYPE)
+
#
+
# Generate the contents of one `struct cvt' instance.
+
define(`CONVERTER_NAME',
+
  `ifdef(`NOCVT_'$1,`',
+
    `	[ELF_T_$1] = {
+
		CONV($1,32,tof),
+
		CONV($1,32,tom),
+
		CONV($1,64,tof),
+
		CONV($1,64,tom)
+
	},
+

+
')')
+

+
# CONVERTER_NAMES(ELFTYPELIST)
+
#
+
# Generate the `struct cvt[]' array.
+
define(`CONVERTER_NAMES',
+
  `ifelse($#,1,`',
+
    `CONVERTER_NAME($1)CONVERTER_NAMES(shift($@))')')
+

+
#
+
# Handling ELF version sections.
+
#
+

+
# _FSZ(FIELD,BASETYPE) - return the file size for a field.
+
define(`_FSZ',
+
  `ifelse($2,`HALF',2,
+
     $2,`WORD',4)')
+

+
# FSZ(STRUCT) - determine the file size of a structure.
+
define(`FSZ',
+
  `ifelse($#,1,0,
+
    `eval(_FSZ($1) + FSZ(shift($@)))')')
+

+
# MAKE_VERSION_CONVERTERS(TYPE,BASE,AUX,PFX) -- Generate conversion
+
# functions for versioning structures.
+
define(`MAKE_VERSION_CONVERTERS',
+
  `MAKE_VERSION_CONVERTER($1,$2,$3,$4,32)
+
   MAKE_VERSION_CONVERTER($1,$2,$3,$4,64)')
+

+
# MAKE_VERSION_CONVERTOR(TYPE,CBASE,CAUX,PFX,SIZE) -- Generate a
+
# conversion function.
+
define(`MAKE_VERSION_CONVERTER',`
+
static int
+
_libelf_cvt_$1$5_tof(unsigned char *dst, size_t dsz, unsigned char *src,
+
    size_t count, int byteswap)
+
{
+
	Elf$5_$2	t;
+
	Elf$5_$3	a;
+
	const size_t	verfsz = FSZ(Elf$5_$2_DEF);
+
	const size_t	auxfsz = FSZ(Elf$5_$3_DEF);
+
	const size_t	vermsz = sizeof(Elf$5_$2);
+
	const size_t	auxmsz = sizeof(Elf$5_$3);
+
	unsigned char * const dstend = dst + dsz;
+
	unsigned char * const srcend = src + count;
+
	unsigned char	*dtmp, *dstaux, *srcaux;
+
	Elf$5_Word	aux, anext, cnt, vnext;
+

+
	for (dtmp = dst, vnext = ~0U;
+
	     vnext != 0 && dtmp + verfsz <= dstend && src + vermsz <= srcend;
+
	     dtmp += vnext, src += vnext) {
+

+
		/* Read in an Elf$5_$2 structure. */
+
		t = *((Elf$5_$2 *) (uintptr_t) src);
+

+
		aux = t.$4_aux;
+
		cnt = t.$4_cnt;
+
		vnext = t.$4_next;
+

+
		if (byteswap) {
+
			SWAP_STRUCT($2, $5)
+
		}
+

+
		dst = dtmp;
+
		WRITE_STRUCT($2, $5)
+

+
		if (aux < verfsz)
+
			return (0);
+

+
		/* Process AUX entries. */
+
		for (anext = ~0U, dstaux = dtmp + aux, srcaux = src + aux;
+
		     cnt != 0 && anext != 0 && dstaux + auxfsz <= dstend &&
+
			srcaux + auxmsz <= srcend;
+
		     dstaux += anext, srcaux += anext, cnt--) {
+

+
			/* Read in an Elf$5_$3 structure. */
+
			a = *((Elf$5_$3 *) (uintptr_t) srcaux);
+
			anext = a.$4a_next;
+

+
			if (byteswap) {
+
				pushdef(`t',`a')SWAP_STRUCT($3, $5)popdef(`t')
+
			}
+

+
			dst = dstaux;
+
			pushdef(`t',`a')WRITE_STRUCT($3, $5)popdef(`t')
+
		}
+

+
		if (anext || cnt)
+
			return (0);
+
	}
+

+
	if (vnext)
+
		return (0);
+

+
	return (1);
+
}
+

+
static int
+
_libelf_cvt_$1$5_tom(unsigned char *dst, size_t dsz, unsigned char *src,
+
    size_t count, int byteswap)
+
{
+
	Elf$5_$2	t, *dp;
+
	Elf$5_$3	a, *ap;
+
	const size_t	verfsz = FSZ(Elf$5_$2_DEF);
+
	const size_t	auxfsz = FSZ(Elf$5_$3_DEF);
+
	const size_t	vermsz = sizeof(Elf$5_$2);
+
	const size_t	auxmsz = sizeof(Elf$5_$3);
+
	unsigned char * const dstend = dst + dsz;
+
	unsigned char * const srcend = src + count;
+
	unsigned char	*dstaux, *s, *srcaux, *stmp;
+
	Elf$5_Word	aux, anext, cnt, vnext;
+

+
	for (stmp = src, vnext = ~0U;
+
	     vnext != 0 && stmp + verfsz <= srcend && dst + vermsz <= dstend;
+
	     stmp += vnext, dst += vnext) {
+

+
		/* Read in a $1 structure. */
+
		s = stmp;
+
		READ_STRUCT($2, $5)
+
		if (byteswap) {
+
			SWAP_STRUCT($2, $5)
+
		}
+

+
		dp = (Elf$5_$2 *) (uintptr_t) dst;
+
		*dp = t;
+

+
		aux = t.$4_aux;
+
		cnt = t.$4_cnt;
+
		vnext = t.$4_next;
+

+
		if (aux < vermsz)
+
			return (0);
+

+
		/* Process AUX entries. */
+
		for (anext = ~0U, dstaux = dst + aux, srcaux = stmp + aux;
+
		     cnt != 0 && anext != 0 && dstaux + auxmsz <= dstend &&
+
			srcaux + auxfsz <= srcend;
+
		     dstaux += anext, srcaux += anext, cnt--) {
+

+
			s = srcaux;
+
			pushdef(`t',`a')READ_STRUCT($3, $5)popdef(`t')
+

+
			if (byteswap) {
+
				pushdef(`t',`a')SWAP_STRUCT($3, $5)popdef(`t')
+
			}
+

+
			anext = a.$4a_next;
+

+
			ap = ((Elf$5_$3 *) (uintptr_t) dstaux);
+
			*ap = a;
+
		}
+

+
		if (anext || cnt)
+
			return (0);
+
	}
+

+
	if (vnext)
+
		return (0);
+

+
	return (1);
+
}')
+

+
divert(0)
+

+
/*
+
 * C macros to byte swap integral quantities.
+
 */
+

+
#define	SWAP_BYTE(X)	do { (void) (X); } while (0)
+
#define	SWAP_IDENT(X)	do { (void) (X); } while (0)
+
#define	SWAP_HALF(X)	do {						\
+
		uint16_t _x = (uint16_t) (X);				\
+
		uint32_t _t = _x & 0xFFU;				\
+
		_t <<= 8U; _x >>= 8U; _t |= _x & 0xFFU;			\
+
		(X) = (uint16_t) _t;					\
+
	} while (0)
+
#define	_SWAP_WORD(X, T) 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) _t;						\
+
	} while (0)
+
#define	SWAP_ADDR32(X)	_SWAP_WORD(X, Elf32_Addr)
+
#define	SWAP_OFF32(X)	_SWAP_WORD(X, Elf32_Off)
+
#define	SWAP_SWORD(X)	_SWAP_WORD(X, Elf32_Sword)
+
#define	SWAP_WORD(X)	_SWAP_WORD(X, Elf32_Word)
+
#define	_SWAP_WORD64(X, T) 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) _t;						\
+
	} while (0)
+
#define	SWAP_ADDR64(X)	_SWAP_WORD64(X, Elf64_Addr)
+
#define	SWAP_LWORD(X)	_SWAP_WORD64(X, Elf64_Lword)
+
#define	SWAP_OFF64(X)	_SWAP_WORD64(X, Elf64_Off)
+
#define	SWAP_SXWORD(X)	_SWAP_WORD64(X, Elf64_Sxword)
+
#define	SWAP_XWORD(X)	_SWAP_WORD64(X, Elf64_Xword)
+

+
/*
+
 * C macros to write out various integral values.
+
 *
+
 * Note:
+
 * - 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 {						\
+
		unsigned char *const _p = (unsigned char *) (P);	\
+
		_p[0]		= (unsigned char) (X);			\
+
		(P)		= _p + 1;				\
+
	} while (0)
+
#define	WRITE_HALF(P,X)	do {						\
+
		uint16_t _t	= (X);					\
+
		unsigned char *const _p	= (unsigned char *) (P);	\
+
		const unsigned char *const _q = (unsigned char *) &_t;	\
+
		_p[0]		= _q[0];				\
+
		_p[1]		= _q[1];				\
+
		(P)		= _p + 2;				\
+
	} while (0)
+
#define	WRITE_WORD(P,X) do {						\
+
		uint32_t _t	= (uint32_t) (X);			\
+
		unsigned char *const _p	= (unsigned char *) (P);	\
+
		const unsigned char *const _q = (unsigned 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	= (uint64_t) (X);			\
+
		unsigned char *const _p	= (unsigned char *) (P);	\
+
		const unsigned char *const _q = (unsigned 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)
+

+
/*
+
 * C macros to read in various integral values.
+
 *
+
 * Note:
+
 * - 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 unsigned char *const _p =				\
+
			(const unsigned char *) (P);			\
+
		(X)		= _p[0];				\
+
		(P)		= (P) + 1;				\
+
	} while (0)
+
#define	READ_HALF(P,X)	do {						\
+
		uint16_t _t;						\
+
		unsigned char *const _q = (unsigned char *) &_t;	\
+
		const unsigned char *const _p =				\
+
			(const unsigned char *) (P);			\
+
		_q[0]		= _p[0];				\
+
		_q[1]		= _p[1];				\
+
		(P)		= (P) + 2;				\
+
		(X)		= _t;					\
+
	} while (0)
+
#define	_READ_WORD(P,X,T) do {						\
+
		uint32_t _t;						\
+
		unsigned char *const _q = (unsigned char *) &_t;	\
+
		const unsigned char *const _p =				\
+
			(const unsigned char *) (P);			\
+
		_q[0]		= _p[0];				\
+
		_q[1]		= _p[1];				\
+
		_q[2]		= _p[2];				\
+
		_q[3]		= _p[3];				\
+
		(P)		= (P) + 4;				\
+
		(X)		= (T) _t;				\
+
	} while (0)
+
#define	READ_ADDR32(P,X)	_READ_WORD(P, X, Elf32_Addr)
+
#define	READ_OFF32(P,X)		_READ_WORD(P, X, Elf32_Off)
+
#define	READ_SWORD(P,X)		_READ_WORD(P, X, Elf32_Sword)
+
#define	READ_WORD(P,X)		_READ_WORD(P, X, Elf32_Word)
+
#define	_READ_WORD64(P,X,T)	do {					\
+
		uint64_t _t;						\
+
		unsigned char *const _q = (unsigned char *) &_t;	\
+
		const unsigned char *const _p =				\
+
			(const unsigned 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) _t;				\
+
	} while (0)
+
#define	READ_ADDR64(P,X)	_READ_WORD64(P, X, Elf64_Addr)
+
#define	READ_LWORD(P,X)		_READ_WORD64(P, X, Elf64_Lword)
+
#define	READ_OFF64(P,X)		_READ_WORD64(P, X, Elf64_Off)
+
#define	READ_SXWORD(P,X)	_READ_WORD64(P, X, Elf64_Sxword)
+
#define	READ_XWORD(P,X)		_READ_WORD64(P, X, Elf64_Xword)
+
#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))
+

+
/*[*/
+
MAKE_TYPE_CONVERTERS(ELF_TYPE_LIST)
+
MAKE_VERSION_CONVERTERS(VDEF,Verdef,Verdaux,vd)
+
MAKE_VERSION_CONVERTERS(VNEED,Verneed,Vernaux,vn)
+
/*]*/
+

+
/*
+
 * 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(unsigned char *dst, size_t dsz, unsigned char *src,
+
    size_t count, int byteswap)
+
{
+
	(void) byteswap;
+
	if (dsz < count)
+
		return (0);
+
	if (dst != src)
+
		(void) memcpy(dst, src, count);
+
	return (1);
+
}
+

+
/*
+
 * 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_cvt_GNUHASH32_tom(unsigned char *dst, size_t dsz, unsigned char *src,
+
    size_t srcsz, int byteswap)
+
{
+
	return (_libelf_cvt_WORD_tom(dst, dsz, src, srcsz / sizeof(uint32_t),
+
		byteswap));
+
}
+

+
static int
+
_libelf_cvt_GNUHASH32_tof(unsigned char *dst, size_t dsz, unsigned char *src,
+
    size_t srcsz, int byteswap)
+
{
+
	return (_libelf_cvt_WORD_tof(dst, dsz, src, srcsz / sizeof(uint32_t),
+
		byteswap));
+
}
+

+
static int
+
_libelf_cvt_GNUHASH64_tom(unsigned char *dst, size_t dsz, unsigned 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_cvt_GNUHASH64_tof(unsigned char *dst, size_t dsz, unsigned 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);
+
}
+

+
/*
+
 * 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(unsigned char *dst, size_t dsz, unsigned 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, 4U);
+
		ROUNDUP2(descsz, 4U);
+

+
		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(unsigned char *dst, size_t dsz, unsigned 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;
+

+
		sz = namesz;
+
		ROUNDUP2(sz, 4U);
+
		sz += descsz;
+
		ROUNDUP2(sz, 4U);
+

+
		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);
+

+
		if (count < sz)
+
			sz = count;
+

+
		(void) memcpy(dst, src, sz);
+

+
		src += sz;
+
		dst += sz;
+
		count -= sz;
+
	}
+

+
	return (1);
+
}
+

+
struct converters {
+
	int	(*tof32)(unsigned char *dst, size_t dsz, unsigned char *src,
+
		    size_t cnt, int byteswap);
+
	int	(*tom32)(unsigned char *dst, size_t dsz, unsigned char *src,
+
		    size_t cnt, int byteswap);
+
	int	(*tof64)(unsigned char *dst, size_t dsz, unsigned char *src,
+
		    size_t cnt, int byteswap);
+
	int	(*tom64)(unsigned char *dst, size_t dsz, unsigned char *src,
+
		    size_t cnt, int byteswap);
+
};
+

+

+
static struct converters cvt[ELF_T_NUM] = {
+
	/*[*/
+
CONVERTER_NAMES(ELF_TYPE_LIST)
+
	/*]*/
+

+
	/*
+
	 * Types that need 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
+
	},
+

+
	[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))
+
 (unsigned char *_dst, size_t dsz, unsigned 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/elf/libelf_data.c
@@ -0,0 +1,103 @@
+
/*-
+
 * 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>
+

+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
int
+
_libelf_xlate_shtype(uint32_t sht)
+
{
+
	/*
+
	 * Look for known section types.
+
	 */
+
	switch (sht) {
+
	case SHT_DYNAMIC:
+
		return (ELF_T_DYN);
+
	case SHT_DYNSYM:
+
		return (ELF_T_SYM);
+
	case SHT_FINI_ARRAY:
+
		return (ELF_T_ADDR);
+
	case SHT_GNU_HASH:
+
		return (ELF_T_GNUHASH);
+
	case SHT_GNU_LIBLIST:
+
		return (ELF_T_WORD);
+
	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);
+
	case SHT_SUNW_dof:
+
		return (ELF_T_BYTE);
+
	case SHT_SUNW_move:
+
		return (ELF_T_MOVE);
+
	case SHT_SUNW_syminfo:
+
		return (ELF_T_SYMINFO);
+
	case SHT_SUNW_verdef:	/* == SHT_GNU_verdef */
+
		return (ELF_T_VDEF);
+
	case SHT_SUNW_verneed:	/* == SHT_GNU_verneed */
+
		return (ELF_T_VNEED);
+
	case SHT_SUNW_versym:	/* == SHT_GNU_versym */
+
		return (ELF_T_HALF);
+
	default:
+
		/*
+
		 * Values in the range [SHT_LOOS..SHT_HIUSER] (i.e.,
+
		 * OS, processor and user-defined section types) are
+
		 * legal, but since we do not know anything more about
+
		 * their semantics, we return a type of ELF_T_BYTE.
+
		 */
+
		if (sht >= SHT_LOOS && sht <= SHT_HIUSER)
+
			return (ELF_T_BYTE);
+

+
		/*
+
		 * Other values are unsupported.
+
		 */
+
		return (-1);
+
	}
+
}
added external/elf/libelf_ehdr.c
@@ -0,0 +1,206 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * 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)(unsigned char *_d, size_t _dsz, unsigned 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)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr),
+
	    (unsigned char *) 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 = (size_t) 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) & 0xFFU; \
+
		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)(unsigned char *_d, size_t _dsz, unsigned 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)((unsigned char*) 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/elf/libelf_extended.c
@@ -0,0 +1,136 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * 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 & 0xFFFFU;
+
	else
+
		((Elf64_Ehdr *) eh)->e_shnum = shnum & 0xFFFFU;
+

+

+
	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 & 0xFFFFU;
+
	else
+
		((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx & 0xFFFFU;
+

+
	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 & 0xFFFFU;
+
	else
+
		((Elf64_Ehdr *) eh)->e_phnum = phnum & 0xFFFFU;
+

+
	return (1);
+
}
added external/elf/libelf_fsize.m4
@@ -0,0 +1,159 @@
+
/*-
+
 * Copyright (c) 2006,2008-2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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 <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/* WARNING: GENERATED FROM __file__. */
+

+
/*
+
 * 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)
+
define(`VDEF_SIZE',	1)
+
define(`VNEED_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',
+
  `[ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 },
+
')
+
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/elf/libelf_memory.c
@@ -0,0 +1,96 @@
+
/*-
+
 * Copyright (c) 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 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 <ar.h>
+
#include <assert.h>
+
#include <string.h>
+
#include <libelf.h>
+

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * Create an ELF descriptor for a memory image, optionally reporting
+
 * parse errors.
+
 */
+

+
Elf *
+
_libelf_memory(unsigned char *image, size_t sz, int reporterror)
+
{
+
	Elf *e;
+
	int e_class;
+
	enum Elf_Error error;
+
	unsigned int e_byteorder, e_version;
+

+
	assert(image != NULL);
+
	assert(sz > 0);
+

+
	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)) {
+
		e_byteorder = image[EI_DATA];
+
		e_class     = image[EI_CLASS];
+
		e_version   = image[EI_VERSION];
+

+
		error = ELF_E_NONE;
+

+
		if (e_version > EV_CURRENT)
+
			error = ELF_E_VERSION;
+
		else if ((e_byteorder != ELFDATA2LSB && e_byteorder !=
+
 		    ELFDATA2MSB) || (e_class != ELFCLASS32 && e_class !=
+
		    ELFCLASS64))
+
			error = ELF_E_HEADER;
+

+
		if (error != ELF_E_NONE) {
+
			if (reporterror) {
+
				LIBELF_PRIVATE(error) = LIBELF_ERROR(error, 0);
+
				(void) _libelf_release_elf(e);
+
				return (NULL);
+
			}
+
		} else {
+
			_libelf_init_elf(e, ELF_K_ELF);
+

+
			e->e_byteorder = e_byteorder;
+
			e->e_class = e_class;
+
			e->e_version = e_version;
+
		}
+
	} else if (sz >= SARMAG &&
+
	    strncmp((const char *) image, ARMAG, (size_t) SARMAG) == 0)
+
		return (_libelf_ar_open(e, reporterror));
+

+
	return (e);
+
}
added external/elf/libelf_msize.m4
@@ -0,0 +1,108 @@
+
/*-
+
 * Copyright (c) 2006,2008-2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/* WARNING: GENERATED FROM __file__. */
+

+
struct msize {
+
	size_t	msz32;
+
	size_t	msz64;
+
};
+

+
divert(-1)
+
include(SRCDIR`/elf_types.m4')
+

+
/*
+
 * ELF types whose memory representations have a variable size.
+
 */
+
define(BYTE_SIZE,	1)
+
define(GNUHASH_SIZE,	1)
+
define(NOTE_SIZE,	1)
+
define(VDEF_SIZE,	1)
+
define(VNEED_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',
+
  `[ELF_T_$1] = { .msz32 = $1_SIZE32, .msz64 = $1_SIZE64 },
+
')
+
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/elf/libelf_open.c
@@ -0,0 +1,249 @@
+
/*-
+
 * Copyright (c) 2006,2008-2011 Joseph Koshy
+
 * All rights reserved.
+
 *
+
 * Redistribution and use in source and binary forms, with or without
+
 * modification, are permitted provided that the following conditions
+
 * are met:
+
 * 1. Redistributions of source code must retain the above copyright
+
 *    notice, this list of conditions and the following disclaimer.
+
 * 2. Redistributions in binary form must reproduce the above copyright
+
 *    notice, this list of conditions and the following disclaimer in the
+
 *    documentation and/or other materials provided with the distribution.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY 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/types.h>
+
#include <sys/stat.h>
+

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

+
#include "_libelf.h"
+

+
#if	ELFTC_HAVE_MMAP
+
#include <sys/mman.h>
+
#endif
+

+
ELFTC_VCSID("$Id$");
+

+
#define	_LIBELF_INITSIZE	(64*1024)
+

+
/*
+
 * Read from a device file, pipe or socket.
+
 */
+
static void *
+
_libelf_read_special_file(int fd, size_t *fsz)
+
{
+
	ssize_t readsz;
+
	size_t bufsz, datasz;
+
	unsigned char *buf, *t;
+

+
	datasz = 0;
+
	readsz = 0;
+
	bufsz = _LIBELF_INITSIZE;
+
	if ((buf = malloc(bufsz)) == NULL)
+
		goto resourceerror;
+

+
	/*
+
	 * Read data from the file descriptor till we reach EOF, or
+
	 * till an error is encountered.
+
	 */
+
	do {
+
		/* Check if we need to expand the data buffer. */
+
		if (datasz == bufsz) {
+
			bufsz *= 2;
+
			if ((t = realloc(buf, bufsz)) == NULL)
+
				goto resourceerror;
+
			buf = t;
+
		}
+

+
		do {
+
			assert(bufsz - datasz > 0);
+
			t = buf + datasz;
+
			if ((readsz = read(fd, t, bufsz - datasz)) <= 0)
+
				break;
+
			datasz += (size_t) readsz;
+
		} while (datasz < bufsz);
+

+
	} while (readsz > 0);
+

+
	if (readsz < 0) {
+
		LIBELF_SET_ERROR(IO, errno);
+
		goto error;
+
	}
+

+
	assert(readsz == 0);
+

+
	/*
+
	 * Free up extra buffer space.
+
	 */
+
	if (bufsz > datasz) {
+
		if (datasz > 0) {
+
			if ((t = realloc(buf, datasz)) == NULL)
+
				goto resourceerror;
+
			buf = t;
+
		} else {	/* Zero bytes read. */
+
			LIBELF_SET_ERROR(ARGUMENT, 0);
+
			free(buf);
+
			buf = NULL;
+
		}
+
	}
+

+
	*fsz = datasz;
+
	return (buf);
+

+
resourceerror:
+
	LIBELF_SET_ERROR(RESOURCE, 0);
+
error:
+
	if (buf != NULL)
+
		free(buf);
+
	return (NULL);
+
}
+

+
/*
+
 * Read the contents of the file referenced by the file descriptor
+
 * 'fd'.
+
 */
+

+
Elf *
+
_libelf_open_object(int fd, Elf_Cmd c, int reporterror)
+
{
+
	Elf *e;
+
	void *m;
+
	mode_t mode;
+
	size_t fsize;
+
	struct stat sb;
+
	unsigned int flags;
+

+
	assert(c == ELF_C_READ || c == ELF_C_RDWR || c == ELF_C_WRITE);
+

+
	if (fstat(fd, &sb) < 0) {
+
		LIBELF_SET_ERROR(IO, errno);
+
		return (NULL);
+
	}
+

+
	mode = sb.st_mode;
+
	fsize = (size_t) sb.st_size;
+

+
	/*
+
	 * Reject unsupported file types.
+
	 */
+
	if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&
+
	    !S_ISSOCK(mode)) {
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

+
	/*
+
	 * For ELF_C_WRITE mode, allocate and return a descriptor.
+
	 */
+
	if (c == ELF_C_WRITE) {
+
		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;
+
			if (!S_ISREG(mode))
+
				e->e_flags |= LIBELF_F_SPECIAL_FILE;
+
		}
+

+
		return (e);
+
	}
+

+

+
	/*
+
	 * ELF_C_READ and ELF_C_RDWR mode.
+
	 */
+
	m = NULL;
+
	flags = 0;
+
	if (S_ISREG(mode)) {
+

+
		/*
+
		 * Reject zero length files.
+
		 */
+
		if (fsize == 0) {
+
			LIBELF_SET_ERROR(ARGUMENT, 0);
+
			return (NULL);
+
		}
+

+
#if	ELFTC_HAVE_MMAP
+
		/*
+
		 * Always map regular files in with 'PROT_READ'
+
		 * permissions.
+
		 *
+
		 * For objects opened in ELF_C_RDWR mode, when
+
		 * elf_update(3) is called, we remove this mapping,
+
		 * write file data out using write(2), and map the new
+
		 * contents back.
+
		 */
+
		m = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
+

+
		if (m == MAP_FAILED)
+
			m = NULL;
+
		else
+
			flags = LIBELF_F_RAWFILE_MMAP;
+
#endif
+

+
		/*
+
		 * Fallback to a read() if the call to mmap() failed,
+
		 * or if mmap() is not available.
+
		 */
+
		if (m == NULL) {
+
			if ((m = malloc(fsize)) == NULL) {
+
				LIBELF_SET_ERROR(RESOURCE, 0);
+
				return (NULL);
+
			}
+

+
			if (read(fd, m, fsize) != (ssize_t) fsize) {
+
				LIBELF_SET_ERROR(IO, errno);
+
				free(m);
+
				return (NULL);
+
			}
+

+
			flags = LIBELF_F_RAWFILE_MALLOC;
+
		}
+
	} else if ((m = _libelf_read_special_file(fd, &fsize)) != NULL)
+
		flags = LIBELF_F_RAWFILE_MALLOC | LIBELF_F_SPECIAL_FILE;
+
	else
+
		return (NULL);
+

+
	if ((e = _libelf_memory(m, fsize, reporterror)) == NULL) {
+
		assert((flags & LIBELF_F_RAWFILE_MALLOC) ||
+
		    (flags & LIBELF_F_RAWFILE_MMAP));
+
		if (flags & LIBELF_F_RAWFILE_MALLOC)
+
			free(m);
+
#if	ELFTC_HAVE_MMAP
+
		else
+
			(void) munmap(m, fsize);
+
#endif
+
		return (NULL);
+
	}
+

+
	/* ar(1) archives aren't supported in RDWR mode. */
+
	if (c == ELF_C_RDWR && e->e_kind == ELF_K_AR) {
+
		(void) elf_end(e);
+
		LIBELF_SET_ERROR(ARGUMENT, 0);
+
		return (NULL);
+
	}
+

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

+
	return (e);
+
}
added external/elf/libelf_phdr.c
@@ -0,0 +1,155 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
void *
+
_libelf_getphdr(Elf *e, int ec)
+
{
+
	size_t phnum;
+
	size_t fsz, msz;
+
	uint64_t phoff;
+
	Elf32_Ehdr *eh32;
+
	Elf64_Ehdr *eh64;
+
	void *ehdr, *phdr;
+
	int (*xlator)(unsigned char *_d, size_t _dsz, unsigned 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;
+
		phoff     = (uint64_t) eh32->e_phoff;
+
	} else {
+
		eh64      = (Elf64_Ehdr *) ehdr;
+
		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/elf/libelf_shdr.c
@@ -0,0 +1,56 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
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/elf/libelf_xlate.c
@@ -0,0 +1,150 @@
+
/*-
+
 * 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>
+

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

+
#include "_libelf.h"
+

+
ELFTC_VCSID("$Id$");
+

+
/*
+
 * 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 = (size_t) src->d_size / fsz;
+
		dsz = cnt * msz;
+
	} else {
+
		cnt = (size_t) 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 + (size_t) src->d_size;
+
	db = (uintptr_t) dst->d_buf;
+
	de = db + (size_t) 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);
+
}
added external/elf/os.FreeBSD.mk
@@ -0,0 +1,7 @@
+
#
+
# Building for a FreeBSD target.
+
#
+
# $Id$
+

+
# Symbol versioning support [FreeBSD 7.X and later]
+
VERSION_MAP=		${.CURDIR}/Version.map
added external/elf/os.NetBSD.mk
@@ -0,0 +1,7 @@
+
#
+
# Build recipes for NetBSD.
+
#
+
# $Id$
+
#
+

+
MKLINT=		no		# lint dies with a sigbus
added external/fetch/common.c
@@ -0,0 +1,1782 @@
+
/*-
+
 * SPDX-License-Identifier: BSD-3-Clause
+
 *
+
 * Copyright (c) 1998-2016 Dag-Erling Smørgrav
+
 * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
+
 * 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
+
 *    in this position and unchanged.
+
 * 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.
+
 * 3. The name of the author may not be used to endorse or promote products
+
 *    derived from this software without specific prior written permission
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "bsd_compat.h"
+

+
#include <sys/param.h>
+
#include <sys/socket.h>
+
#include <sys/time.h>
+
#include <sys/uio.h>
+

+
#include <netinet/in.h>
+

+
#include <ctype.h>
+
#include <errno.h>
+
#include <fcntl.h>
+
#include <inttypes.h>
+
#include <netdb.h>
+
#include <paths.h>
+
#include <poll.h>
+
#include <pwd.h>
+
#include <stdarg.h>
+
#include <stdlib.h>
+
#include <stdio.h>
+
#include <string.h>
+
#include <unistd.h>
+

+
#ifdef WITH_SSL
+
#include <openssl/x509v3.h>
+
#endif
+

+
#include "fetch.h"
+
#include "common.h"
+

+
#ifndef INFTIM
+
#define INFTIM (-1)
+
#endif
+

+
/*** Local data **************************************************************/
+

+
/*
+
 * Error messages for resolver errors
+
 */
+
static struct fetcherr netdb_errlist[] = {
+
#ifdef EAI_ADDRFAMILY
+
	{ EAI_ADDRFAMILY, FETCH_RESOLV, "Address family for host not supported" },
+
#endif
+
#ifdef EAI_NODATA
+
	{ EAI_NODATA,	FETCH_RESOLV,	"No address for host" },
+
#endif
+
	{ EAI_AGAIN,	FETCH_TEMP,	"Transient resolver failure" },
+
	{ EAI_FAIL,	FETCH_RESOLV,	"Non-recoverable resolver failure" },
+
	{ EAI_NONAME,	FETCH_RESOLV,	"Host does not resolve" },
+
	{ -1,		FETCH_UNKNOWN,	"Unknown resolver error" }
+
};
+

+
/*
+
 * SOCKS5 error enumerations
+
 */
+
enum SOCKS5_ERR {
+
/* Protocol errors */
+
	SOCKS5_ERR_SELECTION,
+
	SOCKS5_ERR_READ_METHOD,
+
	SOCKS5_ERR_VER5_ONLY,
+
	SOCKS5_ERR_NOMETHODS,
+
	SOCKS5_ERR_NOTIMPLEMENTED,
+
	SOCKS5_ERR_HOSTNAME_SIZE,
+
	SOCKS5_ERR_REQUEST,
+
	SOCKS5_ERR_REPLY,
+
	SOCKS5_ERR_NON_VER5_RESP,
+
	SOCKS5_ERR_GENERAL,
+
	SOCKS5_ERR_NOT_ALLOWED,
+
	SOCKS5_ERR_NET_UNREACHABLE,
+
	SOCKS5_ERR_HOST_UNREACHABLE,
+
	SOCKS5_ERR_CONN_REFUSED,
+
	SOCKS5_ERR_TTL_EXPIRED,
+
	SOCKS5_ERR_COM_UNSUPPORTED,
+
	SOCKS5_ERR_ADDR_UNSUPPORTED,
+
	SOCKS5_ERR_UNSPECIFIED,
+
/* Configuration errors */
+
	SOCKS5_ERR_BAD_HOST,
+
	SOCKS5_ERR_BAD_PROXY_FORMAT,
+
	SOCKS5_ERR_BAD_PORT
+
};
+

+
/*
+
 * Error messages for SOCKS5 errors
+
 */
+
static struct fetcherr socks5_errlist[] = {
+
/* SOCKS5 protocol errors */
+
	{ SOCKS5_ERR_SELECTION,		FETCH_ABORT,	"SOCKS5: Failed to send selection method" },
+
	{ SOCKS5_ERR_READ_METHOD,	FETCH_ABORT,	"SOCKS5: Failed to read method" },
+
	{ SOCKS5_ERR_VER5_ONLY,		FETCH_PROTO,	"SOCKS5: Only version 5 is implemented" },
+
	{ SOCKS5_ERR_NOMETHODS,		FETCH_PROTO,	"SOCKS5: No acceptable methods" },
+
	{ SOCKS5_ERR_NOTIMPLEMENTED,	FETCH_PROTO,	"SOCKS5: Method currently not implemented" },
+
	{ SOCKS5_ERR_HOSTNAME_SIZE,	FETCH_PROTO,	"SOCKS5: Hostname size is above 256 bytes" },
+
	{ SOCKS5_ERR_REQUEST,		FETCH_PROTO,	"SOCKS5: Failed to request" },
+
	{ SOCKS5_ERR_REPLY,		FETCH_PROTO,	"SOCKS5: Failed to receive reply" },
+
	{ SOCKS5_ERR_NON_VER5_RESP,	FETCH_PROTO,	"SOCKS5: Server responded with a non-version 5 response" },
+
	{ SOCKS5_ERR_GENERAL,		FETCH_ABORT,	"SOCKS5: General server failure" },
+
	{ SOCKS5_ERR_NOT_ALLOWED,	FETCH_AUTH,	"SOCKS5: Connection not allowed by ruleset" },
+
	{ SOCKS5_ERR_NET_UNREACHABLE,	FETCH_NETWORK,	"SOCKS5: Network unreachable" },
+
	{ SOCKS5_ERR_HOST_UNREACHABLE,	FETCH_ABORT,	"SOCKS5: Host unreachable" },
+
	{ SOCKS5_ERR_CONN_REFUSED,	FETCH_ABORT,	"SOCKS5: Connection refused" },
+
	{ SOCKS5_ERR_TTL_EXPIRED,	FETCH_TIMEOUT,	"SOCKS5: TTL expired" },
+
	{ SOCKS5_ERR_COM_UNSUPPORTED,	FETCH_PROTO,	"SOCKS5: Command not supported" },
+
	{ SOCKS5_ERR_ADDR_UNSUPPORTED,	FETCH_ABORT,	"SOCKS5: Address type not supported" },
+
	{ SOCKS5_ERR_UNSPECIFIED,	FETCH_UNKNOWN,	"SOCKS5: Unspecified error" },
+
/* Configuration error */
+
	{ SOCKS5_ERR_BAD_HOST,		FETCH_ABORT,	"SOCKS5: Bad proxy host" },
+
	{ SOCKS5_ERR_BAD_PROXY_FORMAT,	FETCH_ABORT,	"SOCKS5: Bad proxy format" },
+
	{ SOCKS5_ERR_BAD_PORT,		FETCH_ABORT,	"SOCKS5: Bad port" }
+
};
+

+
/* End-of-Line */
+
static const char ENDL[2] = { '\r', '\n' };
+

+

+
/*** Error-reporting functions ***********************************************/
+

+
/*
+
 * Map error code to string
+
 */
+
static struct fetcherr *
+
fetch_finderr(struct fetcherr *p, int e)
+
{
+
	while (p->num != -1 && p->num != e)
+
		p++;
+
	return (p);
+
}
+

+
/*
+
 * Set error code
+
 */
+
void
+
fetch_seterr(struct fetcherr *p, int e)
+
{
+
	p = fetch_finderr(p, e);
+
	fetchLastErrCode = p->cat;
+
	snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
+
}
+

+
/*
+
 * Set error code according to errno
+
 */
+
void
+
fetch_syserr(void)
+
{
+
	switch (errno) {
+
	case 0:
+
		fetchLastErrCode = FETCH_OK;
+
		break;
+
	case EPERM:
+
	case EACCES:
+
	case EROFS:
+
	case EAUTH:
+
	case ENEEDAUTH:
+
		fetchLastErrCode = FETCH_AUTH;
+
		break;
+
	case ENOENT:
+
	case EISDIR: /* XXX */
+
		fetchLastErrCode = FETCH_UNAVAIL;
+
		break;
+
	case ENOMEM:
+
		fetchLastErrCode = FETCH_MEMORY;
+
		break;
+
	case EBUSY:
+
	case EAGAIN:
+
		fetchLastErrCode = FETCH_TEMP;
+
		break;
+
	case EEXIST:
+
		fetchLastErrCode = FETCH_EXISTS;
+
		break;
+
	case ENOSPC:
+
		fetchLastErrCode = FETCH_FULL;
+
		break;
+
	case EADDRINUSE:
+
	case EADDRNOTAVAIL:
+
	case ENETDOWN:
+
	case ENETUNREACH:
+
	case ENETRESET:
+
	case EHOSTUNREACH:
+
		fetchLastErrCode = FETCH_NETWORK;
+
		break;
+
	case ECONNABORTED:
+
	case ECONNRESET:
+
		fetchLastErrCode = FETCH_ABORT;
+
		break;
+
	case ETIMEDOUT:
+
		fetchLastErrCode = FETCH_TIMEOUT;
+
		break;
+
	case ECONNREFUSED:
+
	case EHOSTDOWN:
+
		fetchLastErrCode = FETCH_DOWN;
+
		break;
+
	default:
+
		fetchLastErrCode = FETCH_UNKNOWN;
+
	}
+
	snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
+
}
+

+

+
/*
+
 * Emit status message
+
 */
+
void
+
fetch_info(const char *fmt, ...)
+
{
+
	va_list ap;
+
	int serrno = errno;
+

+
	va_start(ap, fmt);
+
	vfprintf(stderr, fmt, ap);
+
	va_end(ap);
+
	fputc('\n', stderr);
+
	errno = serrno;
+
}
+
#define fetch_verbose(...)						\
+
	do { if (verbose) fetch_info(__VA_ARGS__); } while (0)
+

+

+
/*** Network-related utility functions ***************************************/
+

+
/*
+
 * Return the default port for a scheme
+
 */
+
int
+
fetch_default_port(const char *scheme)
+
{
+
	struct servent *se;
+

+
	if ((se = getservbyname(scheme, "tcp")) != NULL)
+
		return (ntohs(se->s_port));
+
	if (strcmp(scheme, SCHEME_FTP) == 0)
+
		return (FTP_DEFAULT_PORT);
+
	if (strcmp(scheme, SCHEME_HTTP) == 0)
+
		return (HTTP_DEFAULT_PORT);
+
	return (0);
+
}
+

+
/*
+
 * Return the default proxy port for a scheme
+
 */
+
int
+
fetch_default_proxy_port(const char *scheme)
+
{
+
	if (strcmp(scheme, SCHEME_FTP) == 0)
+
		return (FTP_DEFAULT_PROXY_PORT);
+
	if (strcmp(scheme, SCHEME_HTTP) == 0)
+
		return (HTTP_DEFAULT_PROXY_PORT);
+
	return (0);
+
}
+

+

+
/*
+
 * Create a connection for an existing descriptor.
+
 */
+
conn_t *
+
fetch_reopen(int sd)
+
{
+
	conn_t *conn;
+
	int flags;
+
#ifdef SO_NOSIGPIPE
+
	int opt = 1;
+
#endif
+

+
	/* allocate and fill connection structure */
+
	if ((conn = calloc(1, sizeof(*conn))) == NULL)
+
		return (NULL);
+
	flags = fcntl(sd, F_GETFD);
+
	if (flags != -1 && (flags & FD_CLOEXEC) == 0)
+
		(void)fcntl(sd, F_SETFD, flags | FD_CLOEXEC);
+
	flags = fcntl(sd, F_GETFL);
+
	if (flags != -1 && (flags & O_NONBLOCK) == 0)
+
		(void)fcntl(sd, F_SETFL, flags | O_NONBLOCK);
+
#ifdef SO_NOSIGPIPE
+
	(void)setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
+
#endif
+
	conn->sd = sd;
+
	++conn->ref;
+
	return (conn);
+
}
+

+

+
/*
+
 * Bump a connection's reference count.
+
 */
+
conn_t *
+
fetch_ref(conn_t *conn)
+
{
+
	++conn->ref;
+
	return (conn);
+
}
+

+

+
/*
+
 * Resolve an address
+
 */
+
struct addrinfo *
+
fetch_resolve(const char *addr, int port, int af)
+
{
+
	char hbuf[256], sbuf[8];
+
	struct addrinfo hints, *res;
+
	const char *hb, *he, *sep;
+
	const char *host, *service;
+
	int err, len;
+

+
	/* first, check for a bracketed IPv6 address */
+
	if (*addr == '[') {
+
		hb = addr + 1;
+
		if ((sep = strchr(hb, ']')) == NULL) {
+
			errno = EINVAL;
+
			goto syserr;
+
		}
+
		he = sep++;
+
	} else {
+
		hb = addr;
+
		sep = strchrnul(hb, ':');
+
		he = sep;
+
	}
+

+
	/* see if we need to copy the host name */
+
	if (*he != '\0') {
+
		len = snprintf(hbuf, sizeof(hbuf),
+
		    "%.*s", (int)(he - hb), hb);
+
		if (len < 0)
+
			goto syserr;
+
		if (len >= (int)sizeof(hbuf)) {
+
			errno = ENAMETOOLONG;
+
			goto syserr;
+
		}
+
		host = hbuf;
+
	} else {
+
		host = hb;
+
	}
+

+
	/* was it followed by a service name? */
+
	if (*sep == '\0' && port != 0) {
+
		if (port < 1 || port > 65535) {
+
			errno = EINVAL;
+
			goto syserr;
+
		}
+
		if (snprintf(sbuf, sizeof(sbuf), "%d", port) < 0)
+
			goto syserr;
+
		service = sbuf;
+
	} else if (*sep != '\0') {
+
		service = sep + 1;
+
	} else {
+
		service = NULL;
+
	}
+

+
	/* resolve */
+
	memset(&hints, 0, sizeof(hints));
+
	hints.ai_family = af;
+
	hints.ai_socktype = SOCK_STREAM;
+
	hints.ai_flags = AI_ADDRCONFIG;
+
	if ((err = getaddrinfo(host, service, &hints, &res)) != 0) {
+
		netdb_seterr(err);
+
		return (NULL);
+
	}
+
	return (res);
+
syserr:
+
	fetch_syserr();
+
	return (NULL);
+
}
+

+

+
/*
+
 * Bind a socket to a specific local address
+
 */
+
int
+
fetch_bind(int sd, int af, const char *addr)
+
{
+
	struct addrinfo *cliai, *ai;
+
	int err;
+

+
	if ((cliai = fetch_resolve(addr, 0, af)) == NULL)
+
		return (-1);
+
	for (ai = cliai; ai != NULL; ai = ai->ai_next)
+
		if ((err = bind(sd, ai->ai_addr, ai->ai_addrlen)) == 0)
+
			break;
+
	if (err != 0)
+
		fetch_syserr();
+
	freeaddrinfo(cliai);
+
	return (err == 0 ? 0 : -1);
+
}
+

+

+
/*
+
 * SOCKS5 connection initiation, based on RFC 1928
+
 * Default DNS resolution over SOCKS5
+
 */
+
int
+
fetch_socks5_init(conn_t *conn, const char *host, int port, int verbose)
+
{
+
	/*
+
	 * Size is based on largest packet prefix (4 bytes) +
+
	 * Largest FQDN (256) + one byte size (1) +
+
	 * Port (2)
+
	 */
+
	unsigned char buf[BUFF_SIZE];
+
	unsigned char *ptr;
+
	int ret = 1;
+

+
	fetch_verbose("Initializing SOCKS5 connection: %s:%d", host, port);
+

+
	/* Connection initialization */
+
	ptr = buf;
+
	*ptr++ = SOCKS_VERSION_5;
+
	*ptr++ = SOCKS_CONNECTION;
+
	*ptr++ = SOCKS_RSV;
+

+
	if (fetch_write(conn, buf, 3) != 3) {
+
		ret = SOCKS5_ERR_SELECTION;
+
		goto fail;
+
	}
+

+
	/* Verify response from SOCKS5 server */
+
	if (fetch_read(conn, buf, 2) != 2) {
+
		ret = SOCKS5_ERR_READ_METHOD;
+
		goto fail;
+
	}
+

+
	ptr = buf;
+
	if (ptr[0] != SOCKS_VERSION_5) {
+
		ret = SOCKS5_ERR_VER5_ONLY;
+
		goto fail;
+
	}
+
	if (ptr[1] == SOCKS_NOMETHODS) {
+
		ret = SOCKS5_ERR_NOMETHODS;
+
		goto fail;
+
	}
+
	else if (ptr[1] != SOCKS5_NOTIMPLEMENTED) {
+
		ret = SOCKS5_ERR_NOTIMPLEMENTED;
+
		goto fail;
+
	}
+

+
	/* Send Request */
+
	*ptr++ = SOCKS_VERSION_5;
+
	*ptr++ = SOCKS_CONNECTION;
+
	*ptr++ = SOCKS_RSV;
+
	/* Encode all targets as a hostname to avoid DNS leaks */
+
	*ptr++ = SOCKS_ATYP_DOMAINNAME;
+
	if (strlen(host) > FQDN_SIZE) {
+
		ret = SOCKS5_ERR_HOSTNAME_SIZE;
+
		goto fail;
+
	}
+
	*ptr++ = strlen(host);
+
	memcpy(ptr, host, strlen(host));
+
	ptr = ptr + strlen(host);
+

+
	port = htons(port);
+
	*ptr++ = port & 0x00ff;
+
	*ptr++ = (port & 0xff00) >> 8;
+

+
	if (fetch_write(conn, buf, ptr - buf) != ptr - buf) {
+
		ret = SOCKS5_ERR_REQUEST;
+
		goto fail;
+
	}
+

+
	/* BND.ADDR is variable length, read the largest on non-blocking socket */
+
	if (!fetch_read(conn, buf, BUFF_SIZE)) {
+
		ret = SOCKS5_ERR_REPLY;
+
		goto fail;
+
	}
+

+
	ptr = buf;
+
	if (*ptr++ != SOCKS_VERSION_5) {
+
		ret = SOCKS5_ERR_NON_VER5_RESP;
+
		goto fail;
+
	}
+

+
	switch (*ptr++) {
+
	case SOCKS_SUCCESS:
+
		break;
+
	case SOCKS_GENERAL_FAILURE:
+
		ret = SOCKS5_ERR_GENERAL;
+
		goto fail;
+
	case SOCKS_CONNECTION_NOT_ALLOWED:
+
		ret = SOCKS5_ERR_NOT_ALLOWED;
+
		goto fail;
+
	case SOCKS_NETWORK_UNREACHABLE:
+
		ret = SOCKS5_ERR_NET_UNREACHABLE;
+
		goto fail;
+
	case SOCKS_HOST_UNREACHABLE:
+
		ret = SOCKS5_ERR_HOST_UNREACHABLE;
+
		goto fail;
+
	case SOCKS_CONNECTION_REFUSED:
+
		ret = SOCKS5_ERR_CONN_REFUSED;
+
		goto fail;
+
	case SOCKS_TTL_EXPIRED:
+
		ret = SOCKS5_ERR_TTL_EXPIRED;
+
		goto fail;
+
	case SOCKS_COMMAND_NOT_SUPPORTED:
+
		ret = SOCKS5_ERR_COM_UNSUPPORTED;
+
		goto fail;
+
	case SOCKS_ADDRESS_NOT_SUPPORTED:
+
		ret = SOCKS5_ERR_ADDR_UNSUPPORTED;
+
		goto fail;
+
	default:
+
		ret = SOCKS5_ERR_UNSPECIFIED;
+
		goto fail;
+
	}
+

+
	return (ret);
+

+
fail:
+
	socks5_seterr(ret);
+
	return (0);
+
}
+

+
/*
+
 * Perform SOCKS5 initialization
+
 */
+
int
+
fetch_socks5_getenv(char **host, int *port)
+
{
+
	char *socks5env, *endptr, *ext;
+
	const char *portDelim;
+
	size_t slen;
+

+
	portDelim = ":";
+
	if ((socks5env = getenv("SOCKS5_PROXY")) == NULL || *socks5env == '\0') {
+
		*host = NULL;
+
		*port = -1;
+
		return (-1);
+
	}
+

+
	/*
+
	 * IPv6 addresses begin and end in brackets.  Set the port delimiter
+
	 * accordingly and search for it so we can do appropriate validation.
+
	 */
+
	if (socks5env[0] == '[')
+
		portDelim = "]:";
+

+
	slen = strlen(socks5env);
+
	ext = strstr(socks5env, portDelim);
+
	if (socks5env[0] == '[') {
+
		if (socks5env[slen - 1] == ']') {
+
			*host = strndup(socks5env, slen);
+
		} else if (ext != NULL) {
+
			*host = strndup(socks5env, ext - socks5env + 1);
+
		} else {
+
			socks5_seterr(SOCKS5_ERR_BAD_PROXY_FORMAT);
+
			return (0);
+
		}
+
	} else {
+
		*host = strndup(socks5env, ext - socks5env);
+
	}
+

+
	if (*host == NULL)
+
		return (-1);
+
	if (ext == NULL) {
+
		*port = 1080; /* Default port as defined in RFC1928 */
+
	} else {
+
		ext += strlen(portDelim);
+
		errno = 0;
+
		*port = strtoimax(ext, (char **)&endptr, 10);
+
		if (*endptr != '\0' || errno != 0 || *port < 0 ||
+
		    *port > 65535) {
+
			free(*host);
+
			*host = NULL;
+
			socks5_seterr(SOCKS5_ERR_BAD_PORT);
+
			return (0);
+
		}
+
	}
+

+
	return (2);
+
}
+

+

+
/*
+
 * Establish a TCP connection to the specified port on the specified host.
+
 */
+
conn_t *
+
fetch_connect(const char *host, int port, int af, int verbose)
+
{
+
	struct addrinfo *cais = NULL, *sais = NULL, *cai, *sai;
+
	const char *bindaddr;
+
	conn_t *conn = NULL;
+
	int err = 0, sd = -1;
+
	char *sockshost;
+
	int socksport;
+

+
	DEBUGF("---> %s:%d\n", host, port);
+

+
	/*
+
	 * Check if SOCKS5_PROXY env variable is set.  fetch_socks5_getenv
+
	 * will either set sockshost = NULL or allocate memory in all cases.
+
	 */
+
	sockshost = NULL;
+
	if (!fetch_socks5_getenv(&sockshost, &socksport))
+
		goto fail;
+

+
	/* Not using SOCKS5 proxy */
+
	if (sockshost == NULL) {
+
		/* resolve server address */
+
		fetch_verbose("resolving server address: %s:%d", host, port);
+
		if ((sais = fetch_resolve(host, port, af)) == NULL)
+
			goto fail;
+

+
		/* resolve client address */
+
		bindaddr = getenv("FETCH_BIND_ADDRESS");
+
		if (bindaddr != NULL && *bindaddr != '\0') {
+
			fetch_verbose("resolving client address: %s", bindaddr);
+
			if ((cais = fetch_resolve(bindaddr, 0, af)) == NULL)
+
				goto fail;
+
		}
+
	} else {
+
		/* resolve socks5 proxy address */
+
		fetch_verbose("resolving SOCKS5 server address: %s:%d",
+
		    sockshost, socksport);
+
		if ((sais = fetch_resolve(sockshost, socksport, af)) == NULL) {
+
			socks5_seterr(SOCKS5_ERR_BAD_HOST);
+
			goto fail;
+
		}
+
	}
+

+
	/* try each server address in turn */
+
	for (err = 0, sai = sais; sai != NULL; sai = sai->ai_next) {
+
		/* open socket */
+
		if ((sd = socket(sai->ai_family, SOCK_STREAM, 0)) < 0) {
+
			err = -1;
+
			if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
+
				continue;
+
			goto syserr;
+
		}
+
		/* attempt to bind to client address */
+
		for (err = 0, cai = cais; cai != NULL; cai = cai->ai_next) {
+
			if (cai->ai_family != sai->ai_family)
+
				continue;
+
			if ((err = bind(sd, cai->ai_addr, cai->ai_addrlen)) == 0)
+
				break;
+
		}
+
		if (err != 0) {
+
			fetch_verbose("failed to bind to %s", bindaddr);
+
			goto syserr;
+
		}
+
		/* attempt to connect to server address */
+
		while ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) < 0) {
+
			if (errno == EINTR && fetchRestartCalls)
+
				continue;
+
			break;
+
		}
+
		/* success? */
+
		if (err == 0)
+
			break;
+
		/* clean up before next attempt */
+
		close(sd);
+
		sd = -1;
+
	}
+
	if (err != 0) {
+
		if (verbose && sockshost == NULL) {
+
			fetch_info("failed to connect to %s:%d", host, port);
+
			goto syserr;
+
		} else if (sockshost != NULL) {
+
			fetch_verbose("failed to connect to SOCKS5 server %s:%d",
+
			    sockshost, socksport);
+
			socks5_seterr(SOCKS5_ERR_CONN_REFUSED);
+
			goto fail;
+
		}
+
		goto syserr;
+
	}
+

+
	if ((conn = fetch_reopen(sd)) == NULL)
+
		goto syserr;
+

+
	if (sockshost)
+
		if (!fetch_socks5_init(conn, host, port, verbose))
+
			goto fail;
+
	free(sockshost);
+
	if (cais != NULL)
+
		freeaddrinfo(cais);
+
	if (sais != NULL)
+
		freeaddrinfo(sais);
+
	return (conn);
+
syserr:
+
	fetch_syserr();
+
fail:
+
	free(sockshost);
+
	/* Fully close if it was opened; otherwise just don't leak the fd. */
+
	if (conn != NULL)
+
		fetch_close(conn);
+
	else if (sd >= 0)
+
		close(sd);
+
	if (cais != NULL)
+
		freeaddrinfo(cais);
+
	if (sais != NULL)
+
		freeaddrinfo(sais);
+
	return (NULL);
+
}
+

+
#ifdef WITH_SSL
+
/*
+
 * Convert characters A-Z to lowercase (intentionally avoid any locale
+
 * specific conversions).
+
 */
+
static char
+
fetch_ssl_tolower(char in)
+
{
+
	if (in >= 'A' && in <= 'Z')
+
		return (in + 32);
+
	else
+
		return (in);
+
}
+

+
/*
+
 * isalpha implementation that intentionally avoids any locale specific
+
 * conversions.
+
 */
+
static int
+
fetch_ssl_isalpha(char in)
+
{
+
	return ((in >= 'A' && in <= 'Z') || (in >= 'a' && in <= 'z'));
+
}
+

+
/*
+
 * Check if passed hostnames a and b are equal.
+
 */
+
static int
+
fetch_ssl_hname_equal(const char *a, size_t alen, const char *b,
+
    size_t blen)
+
{
+
	size_t i;
+

+
	if (alen != blen)
+
		return (0);
+
	for (i = 0; i < alen; ++i) {
+
		if (fetch_ssl_tolower(a[i]) != fetch_ssl_tolower(b[i]))
+
			return (0);
+
	}
+
	return (1);
+
}
+

+
/*
+
 * Check if domain label is traditional, meaning that only A-Z, a-z, 0-9
+
 * and '-' (hyphen) are allowed. Hyphens have to be surrounded by alpha-
+
 * numeric characters. Double hyphens (like they're found in IDN a-labels
+
 * 'xn--') are not allowed. Empty labels are invalid.
+
 */
+
static int
+
fetch_ssl_is_trad_domain_label(const char *l, size_t len, int wcok)
+
{
+
	size_t i;
+

+
	if (!len || l[0] == '-' || l[len-1] == '-')
+
		return (0);
+
	for (i = 0; i < len; ++i) {
+
		if (!isdigit(l[i]) &&
+
		    !fetch_ssl_isalpha(l[i]) &&
+
		    !(l[i] == '*' && wcok) &&
+
		    !(l[i] == '-' && l[i - 1] != '-'))
+
			return (0);
+
	}
+
	return (1);
+
}
+

+
/*
+
 * Check if host name consists only of numbers. This might indicate an IP
+
 * address, which is not a good idea for CN wildcard comparison.
+
 */
+
static int
+
fetch_ssl_hname_is_only_numbers(const char *hostname, size_t len)
+
{
+
	size_t i;
+

+
	for (i = 0; i < len; ++i) {
+
		if (!((hostname[i] >= '0' && hostname[i] <= '9') ||
+
		    hostname[i] == '.'))
+
			return (0);
+
	}
+
	return (1);
+
}
+

+
/*
+
 * Check if the host name h passed matches the pattern passed in m which
+
 * is usually part of subjectAltName or CN of a certificate presented to
+
 * the client. This includes wildcard matching. The algorithm is based on
+
 * RFC6125, sections 6.4.3 and 7.2, which clarifies RFC2818 and RFC3280.
+
 */
+
static int
+
fetch_ssl_hname_match(const char *h, size_t hlen, const char *m,
+
    size_t mlen)
+
{
+
	int delta, hdotidx, mdot1idx, wcidx;
+
	const char *hdot, *mdot1, *mdot2;
+
	const char *wc; /* wildcard */
+

+
	if (!(h && *h && m && *m))
+
		return (0);
+
	if ((wc = strnstr(m, "*", mlen)) == NULL)
+
		return (fetch_ssl_hname_equal(h, hlen, m, mlen));
+
	wcidx = wc - m;
+
	/* hostname should not be just dots and numbers */
+
	if (fetch_ssl_hname_is_only_numbers(h, hlen))
+
		return (0);
+
	/* only one wildcard allowed in pattern */
+
	if (strnstr(wc + 1, "*", mlen - wcidx - 1) != NULL)
+
		return (0);
+
	/*
+
	 * there must be at least two more domain labels and
+
	 * wildcard has to be in the leftmost label (RFC6125)
+
	 */
+
	mdot1 = strnstr(m, ".", mlen);
+
	if (mdot1 == NULL || mdot1 < wc || (mlen - (mdot1 - m)) < 4)
+
		return (0);
+
	mdot1idx = mdot1 - m;
+
	mdot2 = strnstr(mdot1 + 1, ".", mlen - mdot1idx - 1);
+
	if (mdot2 == NULL || (mlen - (mdot2 - m)) < 2)
+
		return (0);
+
	/* hostname must contain a dot and not be the 1st char */
+
	hdot = strnstr(h, ".", hlen);
+
	if (hdot == NULL || hdot == h)
+
		return (0);
+
	hdotidx = hdot - h;
+
	/*
+
	 * host part of hostname must be at least as long as
+
	 * pattern it's supposed to match
+
	 */
+
	if (hdotidx < mdot1idx)
+
		return (0);
+
	/*
+
	 * don't allow wildcards in non-traditional domain names
+
	 * (IDN, A-label, U-label...)
+
	 */
+
	if (!fetch_ssl_is_trad_domain_label(h, hdotidx, 0) ||
+
	    !fetch_ssl_is_trad_domain_label(m, mdot1idx, 1))
+
		return (0);
+
	/* match domain part (part after first dot) */
+
	if (!fetch_ssl_hname_equal(hdot, hlen - hdotidx, mdot1,
+
	    mlen - mdot1idx))
+
		return (0);
+
	/* match part left of wildcard */
+
	if (!fetch_ssl_hname_equal(h, wcidx, m, wcidx))
+
		return (0);
+
	/* match part right of wildcard */
+
	delta = mdot1idx - wcidx - 1;
+
	if (!fetch_ssl_hname_equal(hdot - delta, delta,
+
	    mdot1 - delta, delta))
+
		return (0);
+
	/* all tests succeeded, it's a match */
+
	return (1);
+
}
+

+
/*
+
 * Get numeric host address info - returns NULL if host was not an IP
+
 * address. The caller is responsible for deallocation using
+
 * freeaddrinfo(3).
+
 */
+
static struct addrinfo *
+
fetch_ssl_get_numeric_addrinfo(const char *hostname, size_t len)
+
{
+
	struct addrinfo hints, *res;
+
	char *host;
+

+
	host = (char *)malloc(len + 1);
+
	memcpy(host, hostname, len);
+
	host[len] = '\0';
+
	memset(&hints, 0, sizeof(hints));
+
	hints.ai_family = PF_UNSPEC;
+
	hints.ai_socktype = SOCK_STREAM;
+
	hints.ai_protocol = 0;
+
	hints.ai_flags = AI_NUMERICHOST;
+
	/* port is not relevant for this purpose */
+
	if (getaddrinfo(host, "443", &hints, &res) != 0)
+
		res = NULL;
+
	free(host);
+
	return res;
+
}
+

+
/*
+
 * Compare ip address in addrinfo with address passes.
+
 */
+
static int
+
fetch_ssl_ipaddr_match_bin(const struct addrinfo *lhost, const char *rhost,
+
    size_t rhostlen)
+
{
+
	const void *left;
+

+
	if (lhost->ai_family == AF_INET && rhostlen == 4) {
+
		left = (void *)&((struct sockaddr_in*)(void *)
+
		    lhost->ai_addr)->sin_addr.s_addr;
+
#ifdef INET6
+
	} else if (lhost->ai_family == AF_INET6 && rhostlen == 16) {
+
		left = (void *)&((struct sockaddr_in6 *)(void *)
+
		    lhost->ai_addr)->sin6_addr;
+
#endif
+
	} else
+
		return (0);
+
	return (!memcmp(left, (const void *)rhost, rhostlen) ? 1 : 0);
+
}
+

+
/*
+
 * Compare ip address in addrinfo with host passed. If host is not an IP
+
 * address, comparison will fail.
+
 */
+
static int
+
fetch_ssl_ipaddr_match(const struct addrinfo *laddr, const char *r,
+
    size_t rlen)
+
{
+
	struct addrinfo *raddr;
+
	int ret;
+
	char *rip;
+

+
	ret = 0;
+
	if ((raddr = fetch_ssl_get_numeric_addrinfo(r, rlen)) == NULL)
+
		return 0; /* not a numeric host */
+

+
	if (laddr->ai_family == raddr->ai_family) {
+
		if (laddr->ai_family == AF_INET) {
+
			rip = (char *)&((struct sockaddr_in *)(void *)
+
			    raddr->ai_addr)->sin_addr.s_addr;
+
			ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 4);
+
#ifdef INET6
+
		} else if (laddr->ai_family == AF_INET6) {
+
			rip = (char *)&((struct sockaddr_in6 *)(void *)
+
			    raddr->ai_addr)->sin6_addr;
+
			ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 16);
+
#endif
+
		}
+

+
	}
+
	freeaddrinfo(raddr);
+
	return (ret);
+
}
+

+
/*
+
 * Verify server certificate by subjectAltName.
+
 */
+
static int
+
fetch_ssl_verify_altname(STACK_OF(GENERAL_NAME) *altnames,
+
    const char *host, struct addrinfo *ip)
+
{
+
	const GENERAL_NAME *name;
+
	size_t nslen;
+
	int i;
+
	const char *ns;
+

+
	for (i = 0; i < sk_GENERAL_NAME_num(altnames); ++i) {
+
		name = sk_GENERAL_NAME_value(altnames, i);
+
		ns = (const char *)ASN1_STRING_get0_data(name->d.ia5);
+
		nslen = (size_t)ASN1_STRING_length(name->d.ia5);
+

+
		if (name->type == GEN_DNS && ip == NULL &&
+
		    fetch_ssl_hname_match(host, strlen(host), ns, nslen))
+
			return (1);
+
		else if (name->type == GEN_IPADD && ip != NULL &&
+
		    fetch_ssl_ipaddr_match_bin(ip, ns, nslen))
+
			return (1);
+
	}
+
	return (0);
+
}
+

+
/*
+
 * Verify server certificate by CN.
+
 */
+
static int
+
fetch_ssl_verify_cn(X509_NAME *subject, const char *host,
+
    struct addrinfo *ip)
+
{
+
	ASN1_STRING *namedata;
+
	X509_NAME_ENTRY *nameentry;
+
	int cnlen, lastpos, loc, ret;
+
	unsigned char *cn;
+

+
	ret = 0;
+
	lastpos = -1;
+
	loc = -1;
+
	cn = NULL;
+
	/* get most specific CN (last entry in list) and compare */
+
	while ((lastpos = X509_NAME_get_index_by_NID(subject,
+
	    NID_commonName, lastpos)) != -1)
+
		loc = lastpos;
+

+
	if (loc > -1) {
+
		nameentry = X509_NAME_get_entry(subject, loc);
+
		namedata = X509_NAME_ENTRY_get_data(nameentry);
+
		cnlen = ASN1_STRING_to_UTF8(&cn, namedata);
+
		if (ip == NULL &&
+
		    fetch_ssl_hname_match(host, strlen(host), cn, cnlen))
+
			ret = 1;
+
		else if (ip != NULL && fetch_ssl_ipaddr_match(ip, cn, cnlen))
+
			ret = 1;
+
		OPENSSL_free(cn);
+
	}
+
	return (ret);
+
}
+

+
/*
+
 * Verify that server certificate subjectAltName/CN matches
+
 * hostname. First check, if there are alternative subject names. If yes,
+
 * those have to match. Only if those don't exist it falls back to
+
 * checking the subject's CN.
+
 */
+
static int
+
fetch_ssl_verify_hname(X509 *cert, const char *host)
+
{
+
	struct addrinfo *ip;
+
	STACK_OF(GENERAL_NAME) *altnames;
+
	X509_NAME *subject;
+
	int ret;
+

+
	ret = 0;
+
	ip = fetch_ssl_get_numeric_addrinfo(host, strlen(host));
+
	altnames = X509_get_ext_d2i(cert, NID_subject_alt_name,
+
	    NULL, NULL);
+

+
	if (altnames != NULL) {
+
		ret = fetch_ssl_verify_altname(altnames, host, ip);
+
	} else {
+
		subject = X509_get_subject_name(cert);
+
		if (subject != NULL)
+
			ret = fetch_ssl_verify_cn(subject, host, ip);
+
	}
+

+
	if (ip != NULL)
+
		freeaddrinfo(ip);
+
	if (altnames != NULL)
+
		GENERAL_NAMES_free(altnames);
+
	return (ret);
+
}
+

+
/*
+
 * Configure transport security layer based on environment.
+
 */
+
static void
+
fetch_ssl_setup_transport_layer(SSL_CTX *ctx, int verbose)
+
{
+
	long ssl_ctx_options;
+

+
	ssl_ctx_options = SSL_OP_ALL | SSL_OP_NO_SSLv3 | SSL_OP_NO_TICKET;
+
	if (getenv("SSL_NO_TLS1") != NULL)
+
		ssl_ctx_options |= SSL_OP_NO_TLSv1;
+
	if (getenv("SSL_NO_TLS1_1") != NULL)
+
		ssl_ctx_options |= SSL_OP_NO_TLSv1_1;
+
	if (getenv("SSL_NO_TLS1_2") != NULL)
+
		ssl_ctx_options |= SSL_OP_NO_TLSv1_2;
+
	if (getenv("SSL_NO_TLS1_3") != NULL)
+
		ssl_ctx_options |= SSL_OP_NO_TLSv1_3;
+
	fetch_verbose("SSL options: %lx", ssl_ctx_options);
+
	SSL_CTX_set_options(ctx, ssl_ctx_options);
+
}
+

+

+
/*
+
 * Configure peer verification based on environment.
+
 */
+
static int
+
fetch_ssl_setup_peer_verification(SSL_CTX *ctx, int verbose)
+
{
+
	X509_LOOKUP *crl_lookup;
+
	X509_STORE *crl_store;
+
	const char *ca_cert_file, *ca_cert_path, *crl_file;
+

+
	if (getenv("SSL_NO_VERIFY_PEER") == NULL) {
+
		ca_cert_file = getenv("SSL_CA_CERT_FILE");
+
		ca_cert_path = getenv("SSL_CA_CERT_PATH");
+
		if (verbose) {
+
			fetch_info("Peer verification enabled");
+
			if (ca_cert_file != NULL)
+
				fetch_info("Using CA cert file: %s",
+
				    ca_cert_file);
+
			if (ca_cert_path != NULL)
+
				fetch_info("Using CA cert path: %s",
+
				    ca_cert_path);
+
			if (ca_cert_file == NULL && ca_cert_path == NULL)
+
				fetch_info("Using OpenSSL default "
+
				    "CA cert file and path");
+
		}
+
		SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER,
+
		    fetch_ssl_cb_verify_crt);
+
		if (ca_cert_file != NULL || ca_cert_path != NULL)
+
			SSL_CTX_load_verify_locations(ctx, ca_cert_file,
+
			    ca_cert_path);
+
		else
+
			SSL_CTX_set_default_verify_paths(ctx);
+
		if ((crl_file = getenv("SSL_CRL_FILE")) != NULL) {
+
			fetch_verbose("Using CRL file: %s", crl_file);
+
			crl_store = SSL_CTX_get_cert_store(ctx);
+
			crl_lookup = X509_STORE_add_lookup(crl_store,
+
			    X509_LOOKUP_file());
+
			if (crl_lookup == NULL ||
+
			    !X509_load_crl_file(crl_lookup, crl_file,
+
				X509_FILETYPE_PEM)) {
+
				fetch_info("Could not load CRL file %s",
+
				    crl_file);
+
				return (0);
+
			}
+
			X509_STORE_set_flags(crl_store,
+
			    X509_V_FLAG_CRL_CHECK |
+
			    X509_V_FLAG_CRL_CHECK_ALL);
+
		}
+
	}
+
	return (1);
+
}
+

+
/*
+
 * Configure client certificate based on environment.
+
 */
+
static int
+
fetch_ssl_setup_client_certificate(SSL_CTX *ctx, int verbose)
+
{
+
	const char *client_cert_file, *client_key_file;
+

+
	if ((client_cert_file = getenv("SSL_CLIENT_CERT_FILE")) != NULL) {
+
		client_key_file = getenv("SSL_CLIENT_KEY_FILE") != NULL ?
+
		    getenv("SSL_CLIENT_KEY_FILE") : client_cert_file;
+
		fetch_verbose("Using client cert file: %s", client_cert_file);
+
		fetch_verbose("Using client key file: %s", client_key_file);
+
		if (SSL_CTX_use_certificate_chain_file(ctx,
+
			client_cert_file) != 1) {
+
			fetch_info("Could not load client certificate %s",
+
			    client_cert_file);
+
			return (0);
+
		}
+
		if (SSL_CTX_use_PrivateKey_file(ctx, client_key_file,
+
			SSL_FILETYPE_PEM) != 1) {
+
			fetch_info("Could not load client key %s",
+
			    client_key_file);
+
			return (0);
+
		}
+
	}
+
	return (1);
+
}
+

+
/*
+
 * Callback for SSL certificate verification, this is called on server
+
 * cert verification. It takes no decision, but informs the user in case
+
 * verification failed.
+
 */
+
int
+
fetch_ssl_cb_verify_crt(int verified, X509_STORE_CTX *ctx)
+
{
+
	X509 *crt;
+
	X509_NAME *name;
+
	char *str;
+

+
	str = NULL;
+
	if (!verified) {
+
		if ((crt = X509_STORE_CTX_get_current_cert(ctx)) != NULL &&
+
		    (name = X509_get_subject_name(crt)) != NULL)
+
			str = X509_NAME_oneline(name, 0, 0);
+
		fetch_info("Certificate verification failed for %s",
+
		    str != NULL ? str : "no relevant certificate");
+
		OPENSSL_free(str);
+
	}
+
	return (verified);
+
}
+

+
#endif
+

+
/*
+
 * Enable SSL on a connection.
+
 */
+
int
+
fetch_ssl(conn_t *conn, const struct url *URL, int verbose)
+
{
+
#ifdef WITH_SSL
+
	int ret, ssl_err;
+
	X509_NAME *name;
+
	char *str;
+

+
	if ((conn->ssl_ctx = SSL_CTX_new(TLS_client_method())) == NULL) {
+
		fetch_info("SSL context creation failed");
+
		ERR_print_errors_fp(stderr);
+
		return (-1);
+
	}
+
	SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
+

+
	fetch_ssl_setup_transport_layer(conn->ssl_ctx, verbose);
+
	if (!fetch_ssl_setup_peer_verification(conn->ssl_ctx, verbose))
+
		return (-1);
+
	if (!fetch_ssl_setup_client_certificate(conn->ssl_ctx, verbose))
+
		return (-1);
+

+
	conn->ssl = SSL_new(conn->ssl_ctx);
+
	if (conn->ssl == NULL) {
+
		fetch_info("SSL connection creation failed");
+
		ERR_print_errors_fp(stderr);
+
		return (-1);
+
	}
+
	SSL_set_fd(conn->ssl, conn->sd);
+

+
#if !defined(OPENSSL_NO_TLSEXT)
+
	if (!SSL_set_tlsext_host_name(conn->ssl, __DECONST(char *, URL->host))) {
+
		fetch_info("Failed to set TLS server name indication for host %s",
+
		    URL->host);
+
		return (-1);
+
	}
+
#endif
+
	while ((ret = SSL_connect(conn->ssl)) == -1) {
+
		ssl_err = SSL_get_error(conn->ssl, ret);
+
		if (ssl_err != SSL_ERROR_WANT_READ &&
+
		    ssl_err != SSL_ERROR_WANT_WRITE) {
+
			ERR_print_errors_fp(stderr);
+
			return (-1);
+
		}
+
	}
+
	conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
+

+
	if (conn->ssl_cert == NULL) {
+
		fetch_info("No server SSL certificate");
+
		return (-1);
+
	}
+

+
	if (getenv("SSL_NO_VERIFY_HOSTNAME") == NULL) {
+
		fetch_verbose("Verify hostname");
+
		if (!fetch_ssl_verify_hname(conn->ssl_cert, URL->host)) {
+
			fetch_info("SSL certificate subject does not match host %s",
+
			    URL->host);
+
			return (-1);
+
		}
+
	}
+

+
	if (verbose) {
+
		fetch_info("%s connection established using %s",
+
		    SSL_get_version(conn->ssl), SSL_get_cipher(conn->ssl));
+
		name = X509_get_subject_name(conn->ssl_cert);
+
		str = X509_NAME_oneline(name, 0, 0);
+
		fetch_info("Certificate subject: %s", str);
+
		OPENSSL_free(str);
+
		name = X509_get_issuer_name(conn->ssl_cert);
+
		str = X509_NAME_oneline(name, 0, 0);
+
		fetch_info("Certificate issuer: %s", str);
+
		OPENSSL_free(str);
+
	}
+

+
	return (0);
+
#else
+
	(void)conn;
+
	(void)verbose;
+
	(void)URL;
+
	fetch_info("SSL support disabled");
+
	return (-1);
+
#endif
+
}
+

+
#define FETCH_READ_WAIT		-2
+
#define FETCH_READ_ERROR	-1
+
#define FETCH_READ_DONE		 0
+

+
#ifdef WITH_SSL
+
static ssize_t
+
fetch_ssl_read(SSL *ssl, char *buf, size_t len)
+
{
+
	ssize_t rlen;
+
	int ssl_err;
+

+
	rlen = SSL_read(ssl, buf, len);
+
	if (rlen < 0) {
+
		ssl_err = SSL_get_error(ssl, rlen);
+
		if (ssl_err == SSL_ERROR_WANT_READ ||
+
		    ssl_err == SSL_ERROR_WANT_WRITE) {
+
			return (FETCH_READ_WAIT);
+
		} else {
+
			ERR_print_errors_fp(stderr);
+
			return (FETCH_READ_ERROR);
+
		}
+
	}
+
	return (rlen);
+
}
+
#endif
+

+
static ssize_t
+
fetch_socket_read(int sd, char *buf, size_t len)
+
{
+
	ssize_t rlen;
+

+
	rlen = read(sd, buf, len);
+
	if (rlen < 0) {
+
		if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls)) {
+
			return (FETCH_READ_WAIT);
+
		} else {
+
			return (FETCH_READ_ERROR);
+
		}
+
	}
+
	return (rlen);
+
}
+

+
/*
+
 * Read a character from a connection w/ timeout
+
 */
+
ssize_t
+
fetch_read(conn_t *conn, char *buf, size_t len)
+
{
+
	struct timeval now, timeout, delta;
+
	struct pollfd pfd;
+
	ssize_t rlen;
+
	int deltams;
+

+
	if (fetchTimeout > 0) {
+
		gettimeofday(&timeout, NULL);
+
		timeout.tv_sec += fetchTimeout;
+
	}
+

+
	deltams = INFTIM;
+
	memset(&pfd, 0, sizeof pfd);
+
	pfd.fd = conn->sd;
+
	pfd.events = POLLIN | POLLERR;
+

+
	for (;;) {
+
		/*
+
		 * The socket is non-blocking.  Instead of the canonical
+
		 * poll() -> read(), we do the following:
+
		 *
+
		 * 1) call read() or SSL_read().
+
		 * 2) if we received some data, return it.
+
		 * 3) if an error occurred, return -1.
+
		 * 4) if read() or SSL_read() signaled EOF, return.
+
		 * 5) if we did not receive any data but we're not at EOF,
+
		 *    call poll().
+
		 *
+
		 * In the SSL case, this is necessary because if we
+
		 * receive a close notification, we have to call
+
		 * SSL_read() one additional time after we've read
+
		 * everything we received.
+
		 *
+
		 * In the non-SSL case, it may improve performance (very
+
		 * slightly) when reading small amounts of data.
+
		 */
+
#ifdef WITH_SSL
+
		if (conn->ssl != NULL)
+
			rlen = fetch_ssl_read(conn->ssl, buf, len);
+
		else
+
#endif
+
			rlen = fetch_socket_read(conn->sd, buf, len);
+
		if (rlen >= 0) {
+
			break;
+
		} else if (rlen == FETCH_READ_ERROR) {
+
			fetch_syserr();
+
			return (-1);
+
		}
+
		// assert(rlen == FETCH_READ_WAIT);
+
		if (fetchTimeout > 0) {
+
			gettimeofday(&now, NULL);
+
			if (!timercmp(&timeout, &now, >)) {
+
				errno = ETIMEDOUT;
+
				fetch_syserr();
+
				return (-1);
+
			}
+
			timersub(&timeout, &now, &delta);
+
			deltams = delta.tv_sec * 1000 +
+
			    delta.tv_usec / 1000;
+
		}
+
		errno = 0;
+
		pfd.revents = 0;
+
		if (poll(&pfd, 1, deltams) < 0) {
+
			if (errno == EINTR && fetchRestartCalls)
+
				continue;
+
			fetch_syserr();
+
			return (-1);
+
		}
+
	}
+
	return (rlen);
+
}
+

+

+
/*
+
 * Read a line of text from a connection w/ timeout
+
 */
+
#define MIN_BUF_SIZE 1024
+

+
int
+
fetch_getln(conn_t *conn)
+
{
+
	char *tmp;
+
	size_t tmpsize;
+
	ssize_t len;
+
	char c;
+

+
	if (conn->buf == NULL) {
+
		if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
+
			errno = ENOMEM;
+
			return (-1);
+
		}
+
		conn->bufsize = MIN_BUF_SIZE;
+
	}
+

+
	conn->buf[0] = '\0';
+
	conn->buflen = 0;
+

+
	do {
+
		len = fetch_read(conn, &c, 1);
+
		if (len == -1)
+
			return (-1);
+
		if (len == 0)
+
			break;
+
		conn->buf[conn->buflen++] = c;
+
		if (conn->buflen == conn->bufsize) {
+
			tmp = conn->buf;
+
			tmpsize = conn->bufsize * 2 + 1;
+
			if ((tmp = realloc(tmp, tmpsize)) == NULL) {
+
				errno = ENOMEM;
+
				return (-1);
+
			}
+
			conn->buf = tmp;
+
			conn->bufsize = tmpsize;
+
		}
+
	} while (c != '\n');
+

+
	conn->buf[conn->buflen] = '\0';
+
	DEBUGF("<<< %s", conn->buf);
+
	return (0);
+
}
+

+

+
/*
+
 * Write to a connection w/ timeout
+
 */
+
ssize_t
+
fetch_write(conn_t *conn, const char *buf, size_t len)
+
{
+
	struct iovec iov;
+

+
	iov.iov_base = __DECONST(char *, buf);
+
	iov.iov_len = len;
+
	return (fetch_writev(conn, &iov, 1));
+
}
+

+
/*
+
 * Write a vector to a connection w/ timeout
+
 * Note: can modify the iovec.
+
 */
+
ssize_t
+
fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
+
{
+
	struct timeval now, timeout, delta;
+
	struct pollfd pfd;
+
	ssize_t wlen, total;
+
	int deltams;
+

+
	memset(&pfd, 0, sizeof pfd);
+
	if (fetchTimeout) {
+
		pfd.fd = conn->sd;
+
		pfd.events = POLLOUT | POLLERR;
+
		gettimeofday(&timeout, NULL);
+
		timeout.tv_sec += fetchTimeout;
+
	}
+

+
	total = 0;
+
	while (iovcnt > 0) {
+
		while (fetchTimeout && pfd.revents == 0) {
+
			gettimeofday(&now, NULL);
+
			if (!timercmp(&timeout, &now, >)) {
+
				errno = ETIMEDOUT;
+
				fetch_syserr();
+
				return (-1);
+
			}
+
			timersub(&timeout, &now, &delta);
+
			deltams = delta.tv_sec * 1000 +
+
			    delta.tv_usec / 1000;
+
			errno = 0;
+
			pfd.revents = 0;
+
			if (poll(&pfd, 1, deltams) < 0) {
+
				/* POSIX compliance */
+
				if (errno == EAGAIN)
+
					continue;
+
				if (errno == EINTR && fetchRestartCalls)
+
					continue;
+
				return (-1);
+
			}
+
		}
+
		errno = 0;
+
#ifdef WITH_SSL
+
		if (conn->ssl != NULL)
+
			wlen = SSL_write(conn->ssl,
+
			    iov->iov_base, iov->iov_len);
+
		else
+
#endif
+
			wlen = writev(conn->sd, iov, iovcnt);
+
		if (wlen == 0) {
+
			/* we consider a short write a failure */
+
			/* XXX perhaps we shouldn't in the SSL case */
+
			errno = EPIPE;
+
			fetch_syserr();
+
			return (-1);
+
		}
+
		if (wlen < 0) {
+
			if (errno == EINTR && fetchRestartCalls)
+
				continue;
+
			return (-1);
+
		}
+
		total += wlen;
+
		while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
+
			wlen -= iov->iov_len;
+
			iov++;
+
			iovcnt--;
+
		}
+
		if (iovcnt > 0) {
+
			iov->iov_len -= wlen;
+
			iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
+
		}
+
	}
+
	return (total);
+
}
+

+

+
/*
+
 * Write a line of text to a connection w/ timeout
+
 */
+
int
+
fetch_putln(conn_t *conn, const char *str, size_t len)
+
{
+
	struct iovec iov[2];
+
	int ret;
+

+
	DEBUGF(">>> %s\n", str);
+
	iov[0].iov_base = __DECONST(char *, str);
+
	iov[0].iov_len = len;
+
	iov[1].iov_base = __DECONST(char *, ENDL);
+
	iov[1].iov_len = sizeof(ENDL);
+
	if (len == 0)
+
		ret = fetch_writev(conn, &iov[1], 1);
+
	else
+
		ret = fetch_writev(conn, iov, 2);
+
	if (ret == -1)
+
		return (-1);
+
	return (0);
+
}
+

+

+
/*
+
 * Close connection
+
 */
+
int
+
fetch_close(conn_t *conn)
+
{
+
	int ret;
+

+
	if (--conn->ref > 0)
+
		return (0);
+
#ifdef WITH_SSL
+
	if (conn->ssl) {
+
		SSL_shutdown(conn->ssl);
+
		SSL_set_connect_state(conn->ssl);
+
		SSL_free(conn->ssl);
+
		conn->ssl = NULL;
+
	}
+
	if (conn->ssl_ctx) {
+
		SSL_CTX_free(conn->ssl_ctx);
+
		conn->ssl_ctx = NULL;
+
	}
+
	if (conn->ssl_cert) {
+
		X509_free(conn->ssl_cert);
+
		conn->ssl_cert = NULL;
+
	}
+
#endif
+
	ret = close(conn->sd);
+
	free(conn->buf);
+
	free(conn);
+
	return (ret);
+
}
+

+

+
/*** Directory-related utility functions *************************************/
+

+
int
+
fetch_add_entry(struct url_ent **p, int *size, int *len,
+
    const char *name, struct url_stat *us)
+
{
+
	struct url_ent *tmp;
+

+
	if (*p == NULL) {
+
		*size = 0;
+
		*len = 0;
+
	}
+

+
	if (*len >= *size - 1) {
+
		tmp = realloc(*p, (*size * 2 + 1) * sizeof(**p));
+
		if (tmp == NULL) {
+
			errno = ENOMEM;
+
			fetch_syserr();
+
			return (-1);
+
		}
+
		*size = (*size * 2 + 1);
+
		*p = tmp;
+
	}
+

+
	tmp = *p + *len;
+
	snprintf(tmp->name, PATH_MAX, "%s", name);
+
	memcpy(&tmp->stat, us, sizeof(*us));
+

+
	(*len)++;
+
	(++tmp)->name[0] = 0;
+

+
	return (0);
+
}
+

+

+
/*** Authentication-related utility functions ********************************/
+

+
static const char *
+
fetch_read_word(FILE *f)
+
{
+
	static char word[1024];
+

+
	if (fscanf(f, " %1023s ", word) != 1)
+
		return (NULL);
+
	return (word);
+
}
+

+
static int
+
fetch_netrc_open(void)
+
{
+
	struct passwd *pwd;
+
	char fn[PATH_MAX];
+
	const char *p;
+
	int fd, serrno;
+

+
	if ((p = getenv("NETRC")) != NULL) {
+
		DEBUGF("NETRC=%s\n", p);
+
		if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
+
			fetch_info("$NETRC specifies a file name "
+
			    "longer than PATH_MAX");
+
			return (-1);
+
		}
+
	} else {
+
		if ((p = getenv("HOME")) == NULL) {
+
			if ((pwd = getpwuid(getuid())) == NULL ||
+
			    (p = pwd->pw_dir) == NULL)
+
				return (-1);
+
		}
+
		if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
+
			return (-1);
+
	}
+

+
	if ((fd = open(fn, O_RDONLY)) < 0) {
+
		serrno = errno;
+
		DEBUGF("%s: %s\n", fn, strerror(serrno));
+
		errno = serrno;
+
	}
+
	return (fd);
+
}
+

+
/*
+
 * Get authentication data for a URL from .netrc
+
 */
+
int
+
fetch_netrc_auth(struct url *url)
+
{
+
	const char *word;
+
	int serrno;
+
	FILE *f;
+

+
	if (url->netrcfd < 0)
+
		url->netrcfd = fetch_netrc_open();
+
	if (url->netrcfd < 0)
+
		return (-1);
+
	if ((f = fdopen(url->netrcfd, "r")) == NULL) {
+
		serrno = errno;
+
		DEBUGF("fdopen(netrcfd): %s", strerror(errno));
+
		close(url->netrcfd);
+
		url->netrcfd = -1;
+
		errno = serrno;
+
		return (-1);
+
	}
+
	rewind(f);
+
	DEBUGF("searching netrc for %s\n", url->host);
+
	while ((word = fetch_read_word(f)) != NULL) {
+
		if (strcmp(word, "default") == 0) {
+
			DEBUGF("using default netrc settings\n");
+
			break;
+
		}
+
		if (strcmp(word, "machine") == 0 &&
+
		    (word = fetch_read_word(f)) != NULL &&
+
		    strcasecmp(word, url->host) == 0) {
+
			DEBUGF("using netrc settings for %s\n", word);
+
			break;
+
		}
+
	}
+
	if (word == NULL)
+
		goto ferr;
+
	while ((word = fetch_read_word(f)) != NULL) {
+
		if (strcmp(word, "login") == 0) {
+
			if ((word = fetch_read_word(f)) == NULL)
+
				goto ferr;
+
			if (snprintf(url->user, sizeof(url->user),
+
				"%s", word) > (int)sizeof(url->user)) {
+
				fetch_info("login name in .netrc is too long");
+
				url->user[0] = '\0';
+
			}
+
		} else if (strcmp(word, "password") == 0) {
+
			if ((word = fetch_read_word(f)) == NULL)
+
				goto ferr;
+
			if (snprintf(url->pwd, sizeof(url->pwd),
+
				"%s", word) > (int)sizeof(url->pwd)) {
+
				fetch_info("password in .netrc is too long");
+
				url->pwd[0] = '\0';
+
			}
+
		} else if (strcmp(word, "account") == 0) {
+
			if ((word = fetch_read_word(f)) == NULL)
+
				goto ferr;
+
			/* XXX not supported! */
+
		} else {
+
			break;
+
		}
+
	}
+
	fclose(f);
+
	url->netrcfd = -1;
+
	return (0);
+
ferr:
+
	serrno = errno;
+
	fclose(f);
+
	url->netrcfd = -1;
+
	errno = serrno;
+
	return (-1);
+
}
+

+
/*
+
 * The no_proxy environment variable specifies a set of domains for
+
 * which the proxy should not be consulted; the contents is a comma-,
+
 * or space-separated list of domain names.  A single asterisk will
+
 * override all proxy variables and no transactions will be proxied
+
 * (for compatibility with lynx and curl, see the discussion at
+
 * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
+
 */
+
int
+
fetch_no_proxy_match(const char *host)
+
{
+
	const char *no_proxy, *p, *q;
+
	size_t h_len, d_len;
+

+
	if ((no_proxy = getenv("NO_PROXY")) == NULL &&
+
	    (no_proxy = getenv("no_proxy")) == NULL)
+
		return (0);
+

+
	/* asterisk matches any hostname */
+
	if (strcmp(no_proxy, "*") == 0)
+
		return (1);
+

+
	h_len = strlen(host);
+
	p = no_proxy;
+
	do {
+
		/* position p at the beginning of a domain suffix */
+
		while (*p == ',' || isspace((unsigned char)*p))
+
			p++;
+

+
		/* position q at the first separator character */
+
		for (q = p; *q; ++q)
+
			if (*q == ',' || isspace((unsigned char)*q))
+
				break;
+

+
		d_len = q - p;
+
		if (d_len > 0 && h_len >= d_len &&
+
		    strncasecmp(host + h_len - d_len,
+
			p, d_len) == 0) {
+
			/* domain name matches */
+
			return (1);
+
		}
+

+
		p = q + 1;
+
	} while (*q);
+

+
	return (0);
+
}
added external/fetch/common.h
@@ -0,0 +1,175 @@
+
/*-
+
 * SPDX-License-Identifier: BSD-3-Clause
+
 *
+
 * Copyright (c) 1998-2014 Dag-Erling Smørgrav
+
 * 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
+
 *    in this position and unchanged.
+
 * 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.
+
 * 3. The name of the author may not be used to endorse or promote products
+
 *    derived from this software without specific prior written permission
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
+
 */
+

+
#ifndef _COMMON_H_INCLUDED
+
#define _COMMON_H_INCLUDED
+

+
#define FTP_DEFAULT_PORT	21
+
#define HTTP_DEFAULT_PORT	80
+
#define FTP_DEFAULT_PROXY_PORT	21
+
#define HTTP_DEFAULT_PROXY_PORT	3128
+

+
#ifdef WITH_SSL
+
#include <openssl/crypto.h>
+
#include <openssl/x509.h>
+
#include <openssl/pem.h>
+
#include <openssl/ssl.h>
+
#include <openssl/err.h>
+
#endif
+

+
/* Connection */
+
typedef struct fetchconn conn_t;
+
struct fetchconn {
+
	int		 sd;		/* socket descriptor */
+
	char		*buf;		/* buffer */
+
	size_t		 bufsize;	/* buffer size */
+
	size_t		 buflen;	/* length of buffer contents */
+
	int		 err;		/* last protocol reply code */
+
#ifdef WITH_SSL
+
	SSL		*ssl;		/* SSL handle */
+
	SSL_CTX		*ssl_ctx;	/* SSL context */
+
	X509		*ssl_cert;	/* server certificate */
+
#endif
+
	int		 ref;		/* reference count */
+
	char		 cache_host[MAXHOSTNAMELEN + 1];
+
	int		 cache_port;
+
	int		 cache_ssl;	/* 1 if HTTPS */
+
};
+

+
/* Structure used for error message lists */
+
struct fetcherr {
+
	const int	 num;
+
	const int	 cat;
+
	const char	*string;
+
};
+

+
/* For SOCKS header size */
+
#define HEAD_SIZE	4
+
#define FQDN_SIZE	256
+
#define PACK_SIZE	1
+
#define PORT_SIZE	2
+
#define BUFF_SIZE	HEAD_SIZE + FQDN_SIZE + PACK_SIZE + PORT_SIZE
+

+
/* SOCKS5 Request Header */
+
#define SOCKS_VERSION_5		0x05
+
/* SOCKS5 CMD */
+
#define SOCKS_CONNECTION	0x01
+
#define SOCKS_BIND		0x02
+
#define SOCKS_UDP		0x03
+
#define SOCKS_NOMETHODS		0xFF
+
#define SOCKS5_NOTIMPLEMENTED	0x00
+
/* SOCKS5 Reserved */
+
#define SOCKS_RSV		0x00
+
/* SOCKS5 Address Type */
+
#define SOCKS_ATYP_IPV4		0x01
+
#define SOCKS_ATYP_DOMAINNAME	0x03
+
#define SOCKS_ATYP_IPV6		0x04
+
/* SOCKS5 Reply Field */
+
#define SOCKS_SUCCESS			0x00
+
#define SOCKS_GENERAL_FAILURE		0x01
+
#define SOCKS_CONNECTION_NOT_ALLOWED	0x02
+
#define SOCKS_NETWORK_UNREACHABLE	0x03
+
#define SOCKS_HOST_UNREACHABLE		0x04
+
#define SOCKS_CONNECTION_REFUSED	0x05
+
#define SOCKS_TTL_EXPIRED		0x06
+
#define SOCKS_COMMAND_NOT_SUPPORTED	0x07
+
#define SOCKS_ADDRESS_NOT_SUPPORTED	0x08
+

+
/* for fetch_writev */
+
struct iovec;
+

+
void		 fetch_seterr(struct fetcherr *, int);
+
void		 fetch_syserr(void);
+
void		 fetch_info(const char *, ...) __printflike(1, 2);
+
int		 fetch_socks5_getenv(char **host, int *port);
+
int		 fetch_socks5_init(conn_t *conn, const char *host,
+
		     int port, int verbose);
+
int		 fetch_default_port(const char *);
+
int		 fetch_default_proxy_port(const char *);
+
struct addrinfo *fetch_resolve(const char *, int, int);
+
int		 fetch_bind(int, int, const char *);
+
conn_t		*fetch_connect(const char *, int, int, int);
+
conn_t		*fetch_reopen(int);
+
conn_t		*fetch_ref(conn_t *);
+
#ifdef WITH_SSL
+
int		 fetch_ssl_cb_verify_crt(int, X509_STORE_CTX*);
+
#endif
+
int		 fetch_ssl(conn_t *, const struct url *, int);
+
ssize_t		 fetch_read(conn_t *, char *, size_t);
+
int		 fetch_getln(conn_t *);
+
ssize_t		 fetch_write(conn_t *, const char *, size_t);
+
ssize_t		 fetch_writev(conn_t *, struct iovec *, int);
+
int		 fetch_putln(conn_t *, const char *, size_t);
+
int		 fetch_close(conn_t *);
+
int		 fetch_add_entry(struct url_ent **, int *, int *,
+
		     const char *, struct url_stat *);
+
int		 fetch_netrc_auth(struct url *url);
+
int		 fetch_no_proxy_match(const char *);
+

+
#define ftp_seterr(n)	 fetch_seterr(ftp_errlist, n)
+
#define http_seterr(n)	 fetch_seterr(http_errlist, n)
+
#define netdb_seterr(n)	 fetch_seterr(netdb_errlist, n)
+
#define url_seterr(n)	 fetch_seterr(url_errlist, n)
+
#define socks5_seterr(n) fetch_seterr(socks5_errlist, n)
+

+
#ifndef NDEBUG
+
#define DEBUGF(...)							\
+
	do {								\
+
		if (fetchDebug)						\
+
			fprintf(stderr, __VA_ARGS__);			\
+
	} while (0)
+
#else
+
#define DEBUGF(...)							\
+
	do {								\
+
		/* nothing */						\
+
	} while (0)
+
#endif
+

+
/*
+
 * I don't really like exporting http_request(),
+
 * but the HTTP and FTP code occasionally needs to cross-call
+
 * eachother, and this saves me from adding a lot of special-case code
+
 * to handle those cases.
+
 *
+
 * Note that _*_request() free purl, which is way ugly but saves us a
+
 * whole lot of trouble.
+
 */
+
FILE		*http_request(struct url *, const char *,
+
		     struct url_stat *, struct url *, const char *);
+
FILE		*http_request_body(struct url *, const char *,
+
		     struct url_stat *, struct url *, const char *,
+
		     const char *, const char *);
+

+
/*
+
 * Check whether a particular flag is set
+
 */
+
#define CHECK_FLAG(x)	(flags && strchr(flags, (x)))
+

+
#endif
added external/fetch/fetch.3
@@ -0,0 +1,841 @@
+
.\"-
+
.\" Copyright (c) 1998-2013 Dag-Erling Smørgrav
+
.\" Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
+
.\" 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: head/lib/libfetch/fetch.3 273124 2014-10-15 07:35:50Z des $
+
.\"
+
.Dd October 15, 2014
+
.Dt FETCH 3
+
.Os
+
.Sh NAME
+
.Nm fetchMakeURL ,
+
.Nm fetchParseURL ,
+
.Nm fetchFreeURL ,
+
.Nm fetchXGetURL ,
+
.Nm fetchGetURL ,
+
.Nm fetchPutURL ,
+
.Nm fetchStatURL ,
+
.Nm fetchListURL ,
+
.Nm fetchXGet ,
+
.Nm fetchGet ,
+
.Nm fetchPut ,
+
.Nm fetchStat ,
+
.Nm fetchList ,
+
.Nm fetchXGetFile ,
+
.Nm fetchGetFile ,
+
.Nm fetchPutFile ,
+
.Nm fetchStatFile ,
+
.Nm fetchListFile ,
+
.Nm fetchXGetHTTP ,
+
.Nm fetchGetHTTP ,
+
.Nm fetchPutHTTP ,
+
.Nm fetchStatHTTP ,
+
.Nm fetchListHTTP ,
+
.Nm fetchXGetFTP ,
+
.Nm fetchGetFTP ,
+
.Nm fetchPutFTP ,
+
.Nm fetchStatFTP ,
+
.Nm fetchListFTP
+
.Nd file transfer functions
+
.Sh LIBRARY
+
.Lb libfetch
+
.Sh SYNOPSIS
+
.In sys/param.h
+
.In stdio.h
+
.In fetch.h
+
.Ft struct url *
+
.Fn fetchMakeURL "const char *scheme" "const char *host" "int port" "const char *doc" "const char *user" "const char *pwd"
+
.Ft struct url *
+
.Fn fetchParseURL "const char *URL"
+
.Ft void
+
.Fn fetchFreeURL "struct url *u"
+
.Ft FILE *
+
.Fn fetchXGetURL "const char *URL" "struct url_stat *us" "const char *flags"
+
.Ft FILE *
+
.Fn fetchGetURL "const char *URL" "const char *flags"
+
.Ft FILE *
+
.Fn fetchPutURL "const char *URL" "const char *flags"
+
.Ft int
+
.Fn fetchStatURL "const char *URL" "struct url_stat *us" "const char *flags"
+
.Ft struct url_ent *
+
.Fn fetchListURL "const char *URL" "const char *flags"
+
.Ft FILE *
+
.Fn fetchXGet "struct url *u" "struct url_stat *us" "const char *flags"
+
.Ft FILE *
+
.Fn fetchGet "struct url *u" "const char *flags"
+
.Ft FILE *
+
.Fn fetchPut "struct url *u" "const char *flags"
+
.Ft int
+
.Fn fetchStat "struct url *u" "struct url_stat *us" "const char *flags"
+
.Ft struct url_ent *
+
.Fn fetchList "struct url *u" "const char *flags"
+
.Ft FILE *
+
.Fn fetchXGetFile "struct url *u" "struct url_stat *us" "const char *flags"
+
.Ft FILE *
+
.Fn fetchGetFile "struct url *u" "const char *flags"
+
.Ft FILE *
+
.Fn fetchPutFile "struct url *u" "const char *flags"
+
.Ft int
+
.Fn fetchStatFile "struct url *u" "struct url_stat *us" "const char *flags"
+
.Ft struct url_ent *
+
.Fn fetchListFile "struct url *u" "const char *flags"
+
.Ft FILE *
+
.Fn fetchXGetHTTP "struct url *u" "struct url_stat *us" "const char *flags"
+
.Ft FILE *
+
.Fn fetchGetHTTP "struct url *u" "const char *flags"
+
.Ft FILE *
+
.Fn fetchPutHTTP "struct url *u" "const char *flags"
+
.Ft int
+
.Fn fetchStatHTTP "struct url *u" "struct url_stat *us" "const char *flags"
+
.Ft struct url_ent *
+
.Fn fetchListHTTP "struct url *u" "const char *flags"
+
.Ft FILE *
+
.Fn fetchXGetFTP "struct url *u" "struct url_stat *us" "const char *flags"
+
.Ft FILE *
+
.Fn fetchGetFTP "struct url *u" "const char *flags"
+
.Ft FILE *
+
.Fn fetchPutFTP "struct url *u" "const char *flags"
+
.Ft int
+
.Fn fetchStatFTP "struct url *u" "struct url_stat *us" "const char *flags"
+
.Ft struct url_ent *
+
.Fn fetchListFTP "struct url *u" "const char *flags"
+
.Sh DESCRIPTION
+
These functions implement a high-level library for retrieving and
+
uploading files using Uniform Resource Locators (URLs).
+
.Pp
+
.Fn fetchParseURL
+
takes a URL in the form of a null-terminated string and splits it into
+
its components function according to the Common Internet Scheme Syntax
+
detailed in RFC1738.
+
A regular expression which produces this syntax is:
+
.Bd -literal
+
    <scheme>:(//(<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
+
.Ed
+
.Pp
+
If the URL does not seem to begin with a scheme name, the following
+
syntax is assumed:
+
.Bd -literal
+
    ((<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
+
.Ed
+
.Pp
+
Note that some components of the URL are not necessarily relevant to
+
all URL schemes.
+
For instance, the file scheme only needs the <scheme> and <document>
+
components.
+
.Pp
+
.Fn fetchMakeURL
+
and
+
.Fn fetchParseURL
+
return a pointer to a
+
.Vt url
+
structure, which is defined as follows in
+
.In fetch.h :
+
.Bd -literal
+
#define URL_SCHEMELEN 16
+
#define URL_USERLEN 256
+
#define URL_PWDLEN 256
+

+
struct url {
+
    char	 scheme[URL_SCHEMELEN+1];
+
    char	 user[URL_USERLEN+1];
+
    char	 pwd[URL_PWDLEN+1];
+
    char	 host[MAXHOSTNAMELEN+1];
+
    int		 port;
+
    char	*doc;
+
    off_t	 offset;
+
    size_t	 length;
+
    time_t	 ims_time;
+
};
+
.Ed
+
.Pp
+
The
+
.Va ims_time
+
field stores the time value for
+
.Li If-Modified-Since
+
HTTP requests.
+
.Pp
+
The pointer returned by
+
.Fn fetchMakeURL
+
or
+
.Fn fetchParseURL
+
should be freed using
+
.Fn fetchFreeURL .
+
.Pp
+
.Fn fetchXGetURL ,
+
.Fn fetchGetURL ,
+
and
+
.Fn fetchPutURL
+
constitute the recommended interface to the
+
.Nm fetch
+
library.
+
They examine the URL passed to them to determine the transfer
+
method, and call the appropriate lower-level functions to perform the
+
actual transfer.
+
.Fn fetchXGetURL
+
also returns the remote document's metadata in the
+
.Vt url_stat
+
structure pointed to by the
+
.Fa us
+
argument.
+
.Pp
+
The
+
.Fa flags
+
argument is a string of characters which specify transfer options.
+
The
+
meaning of the individual flags is scheme-dependent, and is detailed
+
in the appropriate section below.
+
.Pp
+
.Fn fetchStatURL
+
attempts to obtain the requested document's metadata and fill in the
+
structure pointed to by its second argument.
+
The
+
.Vt url_stat
+
structure is defined as follows in
+
.In fetch.h :
+
.Bd -literal
+
struct url_stat {
+
    off_t	 size;
+
    time_t	 atime;
+
    time_t	 mtime;
+
};
+
.Ed
+
.Pp
+
If the size could not be obtained from the server, the
+
.Fa size
+
field is set to -1.
+
If the modification time could not be obtained from the server, the
+
.Fa mtime
+
field is set to the epoch.
+
If the access time could not be obtained from the server, the
+
.Fa atime
+
field is set to the modification time.
+
.Pp
+
.Fn fetchListURL
+
attempts to list the contents of the directory pointed to by the URL
+
provided.
+
If successful, it returns a malloced array of
+
.Vt url_ent
+
structures.
+
The
+
.Vt url_ent
+
structure is defined as follows in
+
.In fetch.h :
+
.Bd -literal
+
struct url_ent {
+
    char         name[PATH_MAX];
+
    struct url_stat stat;
+
};
+
.Ed
+
.Pp
+
The list is terminated by an entry with an empty name.
+
.Pp
+
The pointer returned by
+
.Fn fetchListURL
+
should be freed using
+
.Fn free .
+
.Pp
+
.Fn fetchXGet ,
+
.Fn fetchGet ,
+
.Fn fetchPut
+
and
+
.Fn fetchStat
+
are similar to
+
.Fn fetchXGetURL ,
+
.Fn fetchGetURL ,
+
.Fn fetchPutURL
+
and
+
.Fn fetchStatURL ,
+
except that they expect a pre-parsed URL in the form of a pointer to
+
a
+
.Vt struct url
+
rather than a string.
+
.Pp
+
All of the
+
.Fn fetchXGetXXX ,
+
.Fn fetchGetXXX
+
and
+
.Fn fetchPutXXX
+
functions return a pointer to a stream which can be used to read or
+
write data from or to the requested document, respectively.
+
Note that
+
although the implementation details of the individual access methods
+
vary, it can generally be assumed that a stream returned by one of the
+
.Fn fetchXGetXXX
+
or
+
.Fn fetchGetXXX
+
functions is read-only, and that a stream returned by one of the
+
.Fn fetchPutXXX
+
functions is write-only.
+
.Sh FILE SCHEME
+
.Fn fetchXGetFile ,
+
.Fn fetchGetFile
+
and
+
.Fn fetchPutFile
+
provide access to documents which are files in a locally mounted file
+
system.
+
Only the <document> component of the URL is used.
+
.Pp
+
.Fn fetchXGetFile
+
and
+
.Fn fetchGetFile
+
do not accept any flags.
+
.Pp
+
.Fn fetchPutFile
+
accepts the
+
.Ql a
+
(append to file) flag.
+
If that flag is specified, the data written to
+
the stream returned by
+
.Fn fetchPutFile
+
will be appended to the previous contents of the file, instead of
+
replacing them.
+
.Sh FTP SCHEME
+
.Fn fetchXGetFTP ,
+
.Fn fetchGetFTP
+
and
+
.Fn fetchPutFTP
+
implement the FTP protocol as described in RFC959.
+
.Pp
+
If the
+
.Ql P
+
(not passive) flag is specified, an active (rather than passive)
+
connection will be attempted.
+
.Pp
+
The
+
.Ql p
+
flag is supported for compatibility with earlier versions where active
+
connections were the default.
+
It has precedence over the
+
.Ql P
+
flag, so if both are specified,
+
.Nm
+
will use a passive connection.
+
.Pp
+
If the
+
.Ql l
+
(low) flag is specified, data sockets will be allocated in the low (or
+
default) port range instead of the high port range (see
+
.Xr ip 4 ) .
+
.Pp
+
If the
+
.Ql d
+
(direct) flag is specified,
+
.Fn fetchXGetFTP ,
+
.Fn fetchGetFTP
+
and
+
.Fn fetchPutFTP
+
will use a direct connection even if a proxy server is defined.
+
.Pp
+
If no user name or password is given, the
+
.Nm fetch
+
library will attempt an anonymous login, with user name "anonymous"
+
and password "anonymous@<hostname>".
+
.Sh HTTP SCHEME
+
The
+
.Fn fetchXGetHTTP ,
+
.Fn fetchGetHTTP
+
and
+
.Fn fetchPutHTTP
+
functions implement the HTTP/1.1 protocol.
+
With a little luck, there is
+
even a chance that they comply with RFC2616 and RFC2617.
+
.Pp
+
If the
+
.Ql d
+
(direct) flag is specified,
+
.Fn fetchXGetHTTP ,
+
.Fn fetchGetHTTP
+
and
+
.Fn fetchPutHTTP
+
will use a direct connection even if a proxy server is defined.
+
.Pp
+
If the
+
.Ql i
+
(if-modified-since) flag is specified, and
+
the
+
.Va ims_time
+
field is set in
+
.Vt "struct url" ,
+
then
+
.Fn fetchXGetHTTP
+
and
+
.Fn fetchGetHTTP
+
will send a conditional
+
.Li If-Modified-Since
+
HTTP header to only fetch the content if it is newer than
+
.Va ims_time .
+
.Pp
+
Since there seems to be no good way of implementing the HTTP PUT
+
method in a manner consistent with the rest of the
+
.Nm fetch
+
library,
+
.Fn fetchPutHTTP
+
is currently unimplemented.
+
.Sh HTTPS SCHEME
+
Based on HTTP SCHEME.
+
By default the peer is verified using the CA bundle located in
+
.Pa /etc/ssl/cert.pem .
+
The file may contain multiple CA certificates.
+
A common source of a current CA bundle is
+
.Pa \%security/ca_root_nss .
+
.Pp
+
The CA bundle used for peer verification can be changed by setting the
+
environment variables
+
.Ev SSL_CA_CERT_FILE
+
to point to a concatenated bundle of trusted certificates and
+
.Ev SSL_CA_CERT_PATH
+
to point to a directory containing hashes of trusted CAs (see
+
.Xr verify 1 ) .
+
.Pp
+
A certificate revocation list (CRL) can be used by setting the
+
environment variable
+
.Ev SSL_CRL_FILE
+
(see
+
.Xr crl 1 ) .
+
.Pp
+
Peer verification can be disabled by setting the environment variable
+
.Ev SSL_NO_VERIFY_PEER .
+
Note that this also disables CRL checking.
+
.Pp
+
By default the service identity is verified according to the rules
+
detailed in RFC6125 (also known as hostname verification).
+
This feature can be disabled by setting the environment variable
+
.Ev SSL_NO_VERIFY_HOSTNAME .
+
.Pp
+
Client certificate based authentication is supported.
+
The environment variable
+
.Ev SSL_CLIENT_CERT_FILE
+
should be set to point to a file containing key and client certificate
+
to be used in PEM format. In case the key is stored in a separate
+
file, the environment variable
+
.Ev SSL_CLIENT_KEY_FILE
+
can be set to point to the key in PEM format.
+
In case the key uses a password, the user will be prompted on standard
+
input (see
+
.Xr PEM 3 ) .
+
.Pp
+
By default
+
.Nm libfetch
+
allows TLSv1 and newer when negotiating the connecting with the remote
+
peer.
+
You can change this behavior by setting the
+
.Ev SSL_ALLOW_SSL2
+
and
+
.Ev SSL_ALLOW_SSL3
+
environment variables to allow SSLv2 and SSLv3, respectively, and
+
.Ev SSL_NO_TLS1 ,
+
.Ev SSL_NO_TLS1_1 and
+
.Ev SSL_NO_TLS1_2
+
to disable TLS 1.0, 1.1 and 1.2 respectively.
+
.Sh AUTHENTICATION
+
Apart from setting the appropriate environment variables and
+
specifying the user name and password in the URL or the
+
.Vt struct url ,
+
the calling program has the option of defining an authentication
+
function with the following prototype:
+
.Pp
+
.Ft int
+
.Fn myAuthMethod "struct url *u"
+
.Pp
+
The callback function should fill in the
+
.Fa user
+
and
+
.Fa pwd
+
fields in the provided
+
.Vt struct url
+
and return 0 on success, or any other value to indicate failure.
+
.Pp
+
To register the authentication callback, simply set
+
.Va fetchAuthMethod
+
to point at it.
+
The callback will be used whenever a site requires authentication and
+
the appropriate environment variables are not set.
+
.Pp
+
This interface is experimental and may be subject to change.
+
.Sh RETURN VALUES
+
.Fn fetchParseURL
+
returns a pointer to a
+
.Vt struct url
+
containing the individual components of the URL.
+
If it is
+
unable to allocate memory, or the URL is syntactically incorrect,
+
.Fn fetchParseURL
+
returns a NULL pointer.
+
.Pp
+
The
+
.Fn fetchStat
+
functions return 0 on success and -1 on failure.
+
.Pp
+
All other functions return a stream pointer which may be used to
+
access the requested document, or NULL if an error occurred.
+
.Pp
+
The following error codes are defined in
+
.In fetch.h :
+
.Bl -tag -width 18n
+
.It Bq Er FETCH_ABORT
+
Operation aborted
+
.It Bq Er FETCH_AUTH
+
Authentication failed
+
.It Bq Er FETCH_DOWN
+
Service unavailable
+
.It Bq Er FETCH_EXISTS
+
File exists
+
.It Bq Er FETCH_FULL
+
File system full
+
.It Bq Er FETCH_INFO
+
Informational response
+
.It Bq Er FETCH_MEMORY
+
Insufficient memory
+
.It Bq Er FETCH_MOVED
+
File has moved
+
.It Bq Er FETCH_NETWORK
+
Network error
+
.It Bq Er FETCH_OK
+
No error
+
.It Bq Er FETCH_PROTO
+
Protocol error
+
.It Bq Er FETCH_RESOLV
+
Resolver error
+
.It Bq Er FETCH_SERVER
+
Server error
+
.It Bq Er FETCH_TEMP
+
Temporary error
+
.It Bq Er FETCH_TIMEOUT
+
Operation timed out
+
.It Bq Er FETCH_UNAVAIL
+
File is not available
+
.It Bq Er FETCH_UNKNOWN
+
Unknown error
+
.It Bq Er FETCH_URL
+
Invalid URL
+
.El
+
.Pp
+
The accompanying error message includes a protocol-specific error code
+
and message, e.g.\& "File is not available (404 Not Found)"
+
.Sh ENVIRONMENT
+
.Bl -tag -width ".Ev FETCH_BIND_ADDRESS"
+
.It Ev FETCH_BIND_ADDRESS
+
Specifies a hostname or IP address to which sockets used for outgoing
+
connections will be bound.
+
.It Ev FTP_LOGIN
+
Default FTP login if none was provided in the URL.
+
.It Ev FTP_PASSIVE_MODE
+
If set to
+
.Ql no ,
+
forces the FTP code to use active mode.
+
If set to any other value, forces passive mode even if the application
+
requested active mode.
+
.It Ev FTP_PASSWORD
+
Default FTP password if the remote server requests one and none was
+
provided in the URL.
+
.It Ev FTP_PROXY
+
URL of the proxy to use for FTP requests.
+
The document part is ignored.
+
FTP and HTTP proxies are supported; if no scheme is specified, FTP is
+
assumed.
+
If the proxy is an FTP proxy,
+
.Nm libfetch
+
will send
+
.Ql user@host
+
as user name to the proxy, where
+
.Ql user
+
is the real user name, and
+
.Ql host
+
is the name of the FTP server.
+
.Pp
+
If this variable is set to an empty string, no proxy will be used for
+
FTP requests, even if the
+
.Ev HTTP_PROXY
+
variable is set.
+
.It Ev ftp_proxy
+
Same as
+
.Ev FTP_PROXY ,
+
for compatibility.
+
.It Ev HTTP_ACCEPT
+
Specifies the value of the
+
.Va Accept
+
header for HTTP requests.
+
If empty, no
+
.Va Accept
+
header is sent.
+
The default is
+
.Dq */* .
+
.It Ev HTTP_AUTH
+
Specifies HTTP authorization parameters as a colon-separated list of
+
items.
+
The first and second item are the authorization scheme and realm
+
respectively; further items are scheme-dependent.
+
Currently, the
+
.Dq basic
+
and
+
.Dq digest
+
authorization methods are supported.
+
.Pp
+
Both methods require two parameters: the user name and
+
password, in that order.
+
.Pp
+
This variable is only used if the server requires authorization and
+
no user name or password was specified in the URL.
+
.It Ev HTTP_PROXY
+
URL of the proxy to use for HTTP requests.
+
The document part is ignored.
+
Only HTTP proxies are supported for HTTP requests.
+
If no port number is specified, the default is 3128.
+
.Pp
+
Note that this proxy will also be used for FTP documents, unless the
+
.Ev FTP_PROXY
+
variable is set.
+
.It Ev http_proxy
+
Same as
+
.Ev HTTP_PROXY ,
+
for compatibility.
+
.It Ev HTTP_PROXY_AUTH
+
Specifies authorization parameters for the HTTP proxy in the same
+
format as the
+
.Ev HTTP_AUTH
+
variable.
+
.Pp
+
This variable is used if and only if connected to an HTTP proxy, and
+
is ignored if a user and/or a password were specified in the proxy
+
URL.
+
.It Ev HTTP_REFERER
+
Specifies the referrer URL to use for HTTP requests.
+
If set to
+
.Dq auto ,
+
the document URL will be used as referrer URL.
+
.It Ev HTTP_USER_AGENT
+
Specifies the User-Agent string to use for HTTP requests.
+
This can be useful when working with HTTP origin or proxy servers that
+
differentiate between user agents.
+
If defined but empty, no User-Agent header is sent.
+
.It Ev NETRC
+
Specifies a file to use instead of
+
.Pa ~/.netrc
+
to look up login names and passwords for FTP sites.
+
See
+
.Xr ftp 1
+
for a description of the file format.
+
This feature is experimental.
+
.It Ev NO_PROXY
+
Either a single asterisk, which disables the use of proxies
+
altogether, or a comma- or whitespace-separated list of hosts for
+
which proxies should not be used.
+
.It Ev no_proxy
+
Same as
+
.Ev NO_PROXY ,
+
for compatibility.
+
.It Ev SSL_ALLOW_SSL2
+
Allow SSL version 2 when negotiating the connection (not recommended).
+
.It Ev SSL_ALLOW_SSL3
+
Allow SSL version 3 when negotiating the connection (not recommended).
+
.It Ev SSL_CA_CERT_FILE
+
CA certificate bundle containing trusted CA certificates.
+
Default value:
+
.Pa /etc/ssl/cert.pem .
+
.It Ev SSL_CA_CERT_PATH
+
Path containing trusted CA hashes.
+
.It Ev SSL_CLIENT_CERT_FILE
+
PEM encoded client certificate/key which will be used in
+
client certificate authentication.
+
.It Ev SSL_CLIENT_KEY_FILE
+
PEM encoded client key in case key and client certificate
+
are stored separately.
+
.It Ev SSL_CRL_FILE
+
File containing certificate revocation list.
+
.It Ev SSL_NO_TLS1
+
Do not allow TLS version 1.0 when negotiating the connection.
+
.It Ev SSL_NO_TLS1_1
+
Do not allow TLS version 1.1 when negotiating the connection.
+
.It Ev SSL_NO_TLS1_2
+
Do not allow TLS version 1.2 when negotiating the connection.
+
.It Ev SSL_NO_VERIFY_HOSTNAME
+
If set, do not verify that the hostname matches the subject of the
+
certificate presented by the server.
+
.It Ev SSL_NO_VERIFY_PEER
+
If set, do not verify the peer certificate against trusted CAs.
+
.El
+
.Sh EXAMPLES
+
To access a proxy server on
+
.Pa proxy.example.com
+
port 8080, set the
+
.Ev HTTP_PROXY
+
environment variable in a manner similar to this:
+
.Pp
+
.Dl HTTP_PROXY=http://proxy.example.com:8080
+
.Pp
+
If the proxy server requires authentication, there are
+
two options available for passing the authentication data.
+
The first method is by using the proxy URL:
+
.Pp
+
.Dl HTTP_PROXY=http://<user>:<pwd>@proxy.example.com:8080
+
.Pp
+
The second method is by using the
+
.Ev HTTP_PROXY_AUTH
+
environment variable:
+
.Bd -literal -offset indent
+
HTTP_PROXY=http://proxy.example.com:8080
+
HTTP_PROXY_AUTH=basic:*:<user>:<pwd>
+
.Ed
+
.Pp
+
To disable the use of a proxy for an HTTP server running on the local
+
host, define
+
.Ev NO_PROXY
+
as follows:
+
.Bd -literal -offset indent
+
NO_PROXY=localhost,127.0.0.1
+
.Ed
+
.Pp
+
Access HTTPS website without any certificate verification whatsoever:
+
.Bd -literal -offset indent
+
SSL_NO_VERIFY_PEER=1
+
SSL_NO_VERIFY_HOSTNAME=1
+
.Ed
+
.Pp
+
Access HTTPS website using client certificate based authentication
+
and a private CA:
+
.Bd -literal -offset indent
+
SSL_CLIENT_CERT_FILE=/path/to/client.pem
+
SSL_CA_CERT_FILE=/path/to/myca.pem
+
.Ed
+
.Sh SEE ALSO
+
.Xr fetch 1 ,
+
.Xr ftpio 3 ,
+
.Xr ip 4
+
.Rs
+
.%A J. Postel
+
.%A J. K. Reynolds
+
.%D October 1985
+
.%B File Transfer Protocol
+
.%O RFC959
+
.Re
+
.Rs
+
.%A P. Deutsch
+
.%A A. Emtage
+
.%A A. Marine.
+
.%D May 1994
+
.%T How to Use Anonymous FTP
+
.%O RFC1635
+
.Re
+
.Rs
+
.%A T. Berners-Lee
+
.%A L. Masinter
+
.%A M. McCahill
+
.%D December 1994
+
.%T Uniform Resource Locators (URL)
+
.%O RFC1738
+
.Re
+
.Rs
+
.%A R. Fielding
+
.%A J. Gettys
+
.%A J. Mogul
+
.%A H. Frystyk
+
.%A L. Masinter
+
.%A P. Leach
+
.%A T. Berners-Lee
+
.%D January 1999
+
.%B Hypertext Transfer Protocol -- HTTP/1.1
+
.%O RFC2616
+
.Re
+
.Rs
+
.%A J. Franks
+
.%A P. Hallam-Baker
+
.%A J. Hostetler
+
.%A S. Lawrence
+
.%A P. Leach
+
.%A A. Luotonen
+
.%A L. Stewart
+
.%D June 1999
+
.%B HTTP Authentication: Basic and Digest Access Authentication
+
.%O RFC2617
+
.Re
+
.Sh HISTORY
+
The
+
.Nm fetch
+
library first appeared in
+
.Fx 3.0 .
+
.Sh AUTHORS
+
.An -nosplit
+
The
+
.Nm fetch
+
library was mostly written by
+
.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org
+
with numerous suggestions and contributions from
+
.An Jordan K. Hubbard Aq Mt jkh@FreeBSD.org ,
+
.An Eugene Skepner Aq Mt eu@qub.com ,
+
.An Hajimu Umemoto Aq Mt ume@FreeBSD.org ,
+
.An Henry Whincup Aq Mt henry@techiebod.com ,
+
.An Jukka A. Ukkonen Aq Mt jau@iki.fi ,
+
.An Jean-Fran\(,cois Dockes Aq Mt jf@dockes.org ,
+
.An Michael Gmelin Aq Mt freebsd@grem.de
+
and others.
+
It replaces the older
+
.Nm ftpio
+
library written by
+
.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org
+
and
+
.An Jordan K. Hubbard Aq Mt jkh@FreeBSD.org .
+
.Pp
+
This manual page was written by
+
.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org
+
and
+
.An Michael Gmelin Aq Mt freebsd@grem.de .
+
.Sh BUGS
+
Some parts of the library are not yet implemented.
+
The most notable
+
examples of this are
+
.Fn fetchPutHTTP ,
+
.Fn fetchListHTTP ,
+
.Fn fetchListFTP
+
and FTP proxy support.
+
.Pp
+
There is no way to select a proxy at run-time other than setting the
+
.Ev HTTP_PROXY
+
or
+
.Ev FTP_PROXY
+
environment variables as appropriate.
+
.Pp
+
.Nm libfetch
+
does not understand or obey 305 (Use Proxy) replies.
+
.Pp
+
Error numbers are unique only within a certain context; the error
+
codes used for FTP and HTTP overlap, as do those used for resolver and
+
system errors.
+
For instance, error code 202 means "Command not
+
implemented, superfluous at this site" in an FTP context and
+
"Accepted" in an HTTP context.
+
.Pp
+
.Fn fetchStatFTP
+
does not check that the result of an MDTM command is a valid date.
+
.Pp
+
In case password protected keys are used for client certificate based
+
authentication the user is prompted for the password on each and every
+
fetch operation.
+
.Pp
+
The man page is incomplete, poorly written and produces badly
+
formatted text.
+
.Pp
+
The error reporting mechanism is unsatisfactory.
+
.Pp
+
Some parts of the code are not fully reentrant.
added external/fetch/fetch.c
@@ -0,0 +1,485 @@
+
/*-
+
 * SPDX-License-Identifier: BSD-3-Clause
+
 *
+
 * Copyright (c) 1998-2004 Dag-Erling Smørgrav
+
 * 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
+
 *    in this position and unchanged.
+
 * 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.
+
 * 3. The name of the author may not be used to endorse or promote products
+
 *    derived from this software without specific prior written permission
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "bsd_compat.h"
+

+
#include <sys/param.h>
+

+
#include <netinet/in.h>
+

+
#include <errno.h>
+
#include <ctype.h>
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <string.h>
+

+
#include "fetch.h"
+
#include "common.h"
+

+
auth_t	 fetchAuthMethod;
+
int	 fetchLastErrCode;
+
char	 fetchLastErrString[MAXERRSTRING];
+
int	 fetchTimeout;
+
int	 fetchRestartCalls = 1;
+
int	 fetchDebug;
+
const char	*fetchCustomHTTPHeaders;
+
int	 fetchSpeedLimit;
+
int	 fetchSpeedTime;
+

+

+
/*** Local data **************************************************************/
+

+
/*
+
 * Error messages for parser errors
+
 */
+
#define URL_MALFORMED		1
+
#define URL_BAD_SCHEME		2
+
#define URL_BAD_PORT		3
+
static struct fetcherr url_errlist[] = {
+
	{ URL_MALFORMED,	FETCH_URL,	"Malformed URL" },
+
	{ URL_BAD_SCHEME,	FETCH_URL,	"Invalid URL scheme" },
+
	{ URL_BAD_PORT,		FETCH_URL,	"Invalid server port" },
+
	{ -1,			FETCH_UNKNOWN,	"Unknown parser error" }
+
};
+

+

+
/*** Public API **************************************************************/
+

+
/*
+
 * Select the appropriate protocol for the URL scheme, and return a
+
 * read-only stream connected to the document referenced by the URL.
+
 * Also fill out the struct url_stat.
+
 */
+
FILE *
+
fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
+
{
+

+
	if (us != NULL) {
+
		us->size = -1;
+
		us->atime = us->mtime = 0;
+
	}
+
	if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
+
		return (fetchXGetHTTP(URL, us, flags));
+
	else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
+
		return (fetchXGetHTTP(URL, us, flags));
+
	url_seterr(URL_BAD_SCHEME);
+
	return (NULL);
+
}
+

+
/*
+
 * Select the appropriate protocol for the URL scheme, and return a
+
 * read-only stream connected to the document referenced by the URL.
+
 */
+
FILE *
+
fetchGet(struct url *URL, const char *flags)
+
{
+
	return (fetchXGet(URL, NULL, flags));
+
}
+

+
/*
+
 * Select the appropriate protocol for the URL scheme, and return a
+
 * write-only stream connected to the document referenced by the URL.
+
 */
+
FILE *
+
fetchPut(struct url *URL, const char *flags)
+
{
+

+
	if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
+
		return (fetchPutHTTP(URL, flags));
+
	else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
+
		return (fetchPutHTTP(URL, flags));
+
	url_seterr(URL_BAD_SCHEME);
+
	return (NULL);
+
}
+

+
/*
+
 * Select the appropriate protocol for the URL scheme, and return the
+
 * size of the document referenced by the URL if it exists.
+
 */
+
int
+
fetchStat(struct url *URL, struct url_stat *us, const char *flags)
+
{
+

+
	if (us != NULL) {
+
		us->size = -1;
+
		us->atime = us->mtime = 0;
+
	}
+
	if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
+
		return (fetchStatHTTP(URL, us, flags));
+
	else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
+
		return (fetchStatHTTP(URL, us, flags));
+
	url_seterr(URL_BAD_SCHEME);
+
	return (-1);
+
}
+

+
/*
+
 * Select the appropriate protocol for the URL scheme, and return a
+
 * list of files in the directory pointed to by the URL.
+
 */
+
struct url_ent *
+
fetchList(struct url *URL, const char *flags)
+
{
+

+
	if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
+
		return (fetchListHTTP(URL, flags));
+
	else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
+
		return (fetchListHTTP(URL, flags));
+
	url_seterr(URL_BAD_SCHEME);
+
	return (NULL);
+
}
+

+
/*
+
 * Attempt to parse the given URL; if successful, call fetchXGet().
+
 */
+
FILE *
+
fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
+
{
+
	struct url *u;
+
	FILE *f;
+

+
	if ((u = fetchParseURL(URL)) == NULL)
+
		return (NULL);
+

+
	f = fetchXGet(u, us, flags);
+

+
	fetchFreeURL(u);
+
	return (f);
+
}
+

+
/*
+
 * Attempt to parse the given URL; if successful, call fetchGet().
+
 */
+
FILE *
+
fetchGetURL(const char *URL, const char *flags)
+
{
+
	return (fetchXGetURL(URL, NULL, flags));
+
}
+

+
/*
+
 * Attempt to parse the given URL; if successful, call fetchPut().
+
 */
+
FILE *
+
fetchPutURL(const char *URL, const char *flags)
+
{
+
	struct url *u;
+
	FILE *f;
+

+
	if ((u = fetchParseURL(URL)) == NULL)
+
		return (NULL);
+

+
	f = fetchPut(u, flags);
+

+
	fetchFreeURL(u);
+
	return (f);
+
}
+

+
/*
+
 * Attempt to parse the given URL; if successful, call fetchStat().
+
 */
+
int
+
fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
+
{
+
	struct url *u;
+
	int s;
+

+
	if ((u = fetchParseURL(URL)) == NULL)
+
		return (-1);
+

+
	s = fetchStat(u, us, flags);
+

+
	fetchFreeURL(u);
+
	return (s);
+
}
+

+
/*
+
 * Attempt to parse the given URL; if successful, call fetchList().
+
 */
+
struct url_ent *
+
fetchListURL(const char *URL, const char *flags)
+
{
+
	struct url *u;
+
	struct url_ent *ue;
+

+
	if ((u = fetchParseURL(URL)) == NULL)
+
		return (NULL);
+

+
	ue = fetchList(u, flags);
+

+
	fetchFreeURL(u);
+
	return (ue);
+
}
+

+
/*
+
 * Make a URL
+
 */
+
struct url *
+
fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
+
    const char *user, const char *pwd)
+
{
+
	struct url *u;
+

+
	if (!scheme || (!host && !doc)) {
+
		url_seterr(URL_MALFORMED);
+
		return (NULL);
+
	}
+

+
	if (port < 0 || port > 65535) {
+
		url_seterr(URL_BAD_PORT);
+
		return (NULL);
+
	}
+

+
	/* allocate struct url */
+
	if ((u = calloc(1, sizeof(*u))) == NULL) {
+
		fetch_syserr();
+
		return (NULL);
+
	}
+
	u->netrcfd = -1;
+

+
	if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
+
		fetch_syserr();
+
		free(u);
+
		return (NULL);
+
	}
+

+
#define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
+
	seturl(scheme);
+
	seturl(host);
+
	seturl(user);
+
	seturl(pwd);
+
#undef seturl
+
	u->port = port;
+

+
	return (u);
+
}
+

+
/*
+
 * Return value of the given hex digit.
+
 */
+
static int
+
fetch_hexval(char ch)
+
{
+

+
	if (ch >= '0' && ch <= '9')
+
		return (ch - '0');
+
	else if (ch >= 'a' && ch <= 'f')
+
		return (ch - 'a' + 10);
+
	else if (ch >= 'A' && ch <= 'F')
+
		return (ch - 'A' + 10);
+
	return (-1);
+
}
+

+
/*
+
 * Decode percent-encoded URL component from src into dst, stopping at end
+
 * of string, or at @ or : separators.  Returns a pointer to the unhandled
+
 * part of the input string (null terminator, @, or :).  No terminator is
+
 * written to dst (it is the caller's responsibility).
+
 */
+
static const char *
+
fetch_pctdecode(char *dst, const char *src, size_t dlen)
+
{
+
	int d1, d2;
+
	char c;
+
	const char *s;
+

+
	for (s = src; *s != '\0' && *s != '@' && *s != ':'; s++) {
+
		if (s[0] == '%' && (d1 = fetch_hexval(s[1])) >= 0 &&
+
		    (d2 = fetch_hexval(s[2])) >= 0 && (d1 > 0 || d2 > 0)) {
+
			c = d1 << 4 | d2;
+
			s += 2;
+
		} else if (s[0] == '%') {
+
			/* Invalid escape sequence. */
+
			return (NULL);
+
		} else {
+
			c = *s;
+
		}
+
		if (dlen-- > 0)
+
			*dst++ = c;
+
		else
+
			return (NULL);
+
	}
+
	return (s);
+
}
+

+
/*
+
 * Split an URL into components. URL syntax is:
+
 * [method:/][/[user[:pwd]@]host[:port]/][document]
+
 * This almost, but not quite, RFC1738 URL syntax.
+
 */
+
struct url *
+
fetchParseURL(const char *URL)
+
{
+
	char *doc;
+
	const char *p, *q;
+
	struct url *u;
+
	int i, n;
+

+
	/* allocate struct url */
+
	if ((u = calloc(1, sizeof(*u))) == NULL) {
+
		fetch_syserr();
+
		return (NULL);
+
	}
+
	u->netrcfd = -1;
+

+
	/* scheme name */
+
	if ((p = strstr(URL, ":/"))) {
+
                if (p - URL > URL_SCHEMELEN)
+
                        goto ouch;
+
                for (i = 0; URL + i < p; i++)
+
                        u->scheme[i] = tolower((unsigned char)URL[i]);
+
		URL = ++p;
+
		/*
+
		 * Only one slash: no host, leave slash as part of document
+
		 * Two slashes: host follows, strip slashes
+
		 */
+
		if (URL[1] == '/')
+
			URL = (p += 2);
+
	} else {
+
		p = URL;
+
	}
+
	if (!*URL || *URL == '/' || *URL == '.' ||
+
	    (u->scheme[0] == '\0' &&
+
		strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
+
		goto nohost;
+

+
	p = strpbrk(URL, "/@");
+
	if (p && *p == '@') {
+
		/* username */
+
		q = fetch_pctdecode(u->user, URL, URL_USERLEN);
+
		if (q == NULL)
+
			goto ouch;
+

+
		/* password */
+
		if (*q == ':') {
+
			q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
+
			if (q == NULL)
+
				goto ouch;
+
		}
+
		p++;
+
	} else {
+
		p = URL;
+
	}
+

+
	/* hostname */
+
	if (*p == '[') {
+
		q = p + 1 + strspn(p + 1, ":0123456789ABCDEFabcdef.");
+
		if (*q++ != ']')
+
			goto ouch;
+
	} else {
+
		/* valid characters in a DNS name */
+
		q = p + strspn(p, "-." "0123456789"
+
		    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "_"
+
		    "abcdefghijklmnopqrstuvwxyz");
+
	}
+
	if ((*q != '\0' && *q != '/' && *q != ':') || q - p > MAXHOSTNAMELEN)
+
		goto ouch;
+
	for (i = 0; p + i < q; i++)
+
		u->host[i] = tolower((unsigned char)p[i]);
+
	u->host[i] = '\0';
+
	p = q;
+

+
	/* port */
+
	if (*p == ':') {
+
		for (n = 0, q = ++p; *q && (*q != '/'); q++) {
+
			if (*q >= '0' && *q <= '9' && n < INT_MAX / 10) {
+
				n = n * 10 + (*q - '0');
+
			} else {
+
				/* invalid port */
+
				url_seterr(URL_BAD_PORT);
+
				goto ouch;
+
			}
+
		}
+
#ifndef IPPORT_MAX
+
#define IPPORT_MAX 65535
+
#endif
+
		if (p != q && (n < 1 || n > IPPORT_MAX))
+
			goto ouch;
+
		u->port = n;
+
		p = q;
+
	}
+

+
nohost:
+
	/* document */
+
	if (!*p)
+
		p = "/";
+

+
	if (strcmp(u->scheme, SCHEME_HTTP) == 0 ||
+
	    strcmp(u->scheme, SCHEME_HTTPS) == 0) {
+
		const char hexnums[] = "0123456789abcdef";
+

+
		/* percent-escape whitespace. */
+
		if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
+
			fetch_syserr();
+
			goto ouch;
+
		}
+
		u->doc = doc;
+
		/* fragments are reserved for client-side processing, see
+
		 * https://www.rfc-editor.org/rfc/rfc9110.html#section-7.1
+
		 */
+
		while (*p != '\0' && *p != '#') {
+
			if (!isspace((unsigned char)*p)) {
+
				*doc++ = *p++;
+
			} else {
+
				*doc++ = '%';
+
				*doc++ = hexnums[((unsigned int)*p) >> 4];
+
				*doc++ = hexnums[((unsigned int)*p) & 0xf];
+
				p++;
+
			}
+
		}
+
		*doc = '\0';
+
	} else if ((u->doc = strdup(p)) == NULL) {
+
		fetch_syserr();
+
		goto ouch;
+
	}
+

+
	DEBUGF("scheme:   \"%s\"\n"
+
	    "user:     \"%s\"\n"
+
	    "password: \"%s\"\n"
+
	    "host:     \"%s\"\n"
+
	    "port:     \"%d\"\n"
+
	    "document: \"%s\"\n",
+
	    u->scheme, u->user, u->pwd,
+
	    u->host, u->port, u->doc);
+

+
	return (u);
+

+
ouch:
+
	free(u);
+
	return (NULL);
+
}
+

+
/*
+
 * Free a URL
+
 */
+
void
+
fetchFreeURL(struct url *u)
+
{
+
	free(u->doc);
+
	free(u);
+
}
added external/fetch/fetch.h
@@ -0,0 +1,159 @@
+
/*-
+
 * SPDX-License-Identifier: BSD-3-Clause
+
 *
+
 * Copyright (c) 1998-2004 Dag-Erling Smørgrav
+
 * 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
+
 *    in this position and unchanged.
+
 * 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.
+
 * 3. The name of the author may not be used to endorse or promote products
+
 *    derived from this software without specific prior written permission
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
+
 */
+

+
#ifndef _FETCH_H_INCLUDED
+
#define _FETCH_H_INCLUDED
+

+
#define _LIBFETCH_VER "libfetch/2.0"
+

+
#define URL_SCHEMELEN 16
+
#define URL_USERLEN 256
+
#define URL_PWDLEN 256
+

+
struct url {
+
	char		 scheme[URL_SCHEMELEN+1];
+
	char		 user[URL_USERLEN+1];
+
	char		 pwd[URL_PWDLEN+1];
+
	char		 host[MAXHOSTNAMELEN+1];
+
	int		 port;
+
	char		*doc;
+
	off_t		 offset;
+
	size_t		 length;
+
	time_t		 ims_time;
+
	int		 netrcfd;
+
};
+

+
struct url_stat {
+
	off_t		 size;
+
	time_t		 atime;
+
	time_t		 mtime;
+
};
+

+
struct url_ent {
+
	char		 name[PATH_MAX];
+
	struct url_stat	 stat;
+
};
+

+
/* Recognized schemes */
+
#define SCHEME_FTP	"ftp"
+
#define SCHEME_HTTP	"http"
+
#define SCHEME_HTTPS	"https"
+
#define SCHEME_FILE	"file"
+

+
/* Error codes */
+
#define	FETCH_ABORT	 1
+
#define	FETCH_AUTH	 2
+
#define	FETCH_DOWN	 3
+
#define	FETCH_EXISTS	 4
+
#define	FETCH_FULL	 5
+
#define	FETCH_INFO	 6
+
#define	FETCH_MEMORY	 7
+
#define	FETCH_MOVED	 8
+
#define	FETCH_NETWORK	 9
+
#define	FETCH_OK	10
+
#define	FETCH_PROTO	11
+
#define	FETCH_RESOLV	12
+
#define	FETCH_SERVER	13
+
#define	FETCH_TEMP	14
+
#define	FETCH_TIMEOUT	15
+
#define	FETCH_UNAVAIL	16
+
#define	FETCH_UNKNOWN	17
+
#define	FETCH_URL	18
+
#define	FETCH_VERBOSE	19
+

+
#ifdef __cplusplus
+
extern "C" {
+
#endif
+

+
/* FILE-specific functions */
+
FILE		*fetchXGetFile(struct url *, struct url_stat *, const char *);
+
FILE		*fetchGetFile(struct url *, const char *);
+
FILE		*fetchPutFile(struct url *, const char *);
+
int		 fetchStatFile(struct url *, struct url_stat *, const char *);
+
struct url_ent	*fetchListFile(struct url *, const char *);
+

+
/* HTTP-specific functions */
+
FILE		*fetchXGetHTTP(struct url *, struct url_stat *, const char *);
+
FILE		*fetchGetHTTP(struct url *, const char *);
+
FILE		*fetchPutHTTP(struct url *, const char *);
+
int		 fetchStatHTTP(struct url *, struct url_stat *, const char *);
+
struct url_ent	*fetchListHTTP(struct url *, const char *);
+
FILE		*fetchReqHTTP(struct url *, const char *, const char *,
+
		    const char *, const char *);
+

+
/* Generic functions */
+
FILE		*fetchXGetURL(const char *, struct url_stat *, const char *);
+
FILE		*fetchGetURL(const char *, const char *);
+
FILE		*fetchPutURL(const char *, const char *);
+
int		 fetchStatURL(const char *, struct url_stat *, const char *);
+
struct url_ent	*fetchListURL(const char *, const char *);
+
FILE		*fetchXGet(struct url *, struct url_stat *, const char *);
+
FILE		*fetchGet(struct url *, const char *);
+
FILE		*fetchPut(struct url *, const char *);
+
int		 fetchStat(struct url *, struct url_stat *, const char *);
+
struct url_ent	*fetchList(struct url *, const char *);
+

+
/* URL parsing */
+
struct url	*fetchMakeURL(const char *, const char *, int,
+
		     const char *, const char *, const char *);
+
struct url	*fetchParseURL(const char *);
+
void		 fetchFreeURL(struct url *);
+

+
#ifdef __cplusplus
+
}
+
#endif
+

+
/* Authentication */
+
typedef int (*auth_t)(struct url *);
+
extern auth_t		 fetchAuthMethod;
+

+
/* Last error code */
+
extern int		 fetchLastErrCode;
+
#define MAXERRSTRING 256
+
extern char		 fetchLastErrString[MAXERRSTRING];
+

+
/* I/O timeout */
+
extern int		 fetchTimeout;
+

+
/* Restart interrupted syscalls */
+
extern int		 fetchRestartCalls;
+

+
/* Extra verbosity */
+
extern int		 fetchDebug;
+

+
/* Custom HTTP headers (newline-separated) */
+
extern const char	*fetchCustomHTTPHeaders;
+

+
/* Low speed limit: abort if speed drops below fetchSpeedLimit bytes/sec
+
 * for fetchSpeedTime consecutive seconds.  Both must be set to enable. */
+
extern int		 fetchSpeedLimit;
+
extern int		 fetchSpeedTime;
+

+
#endif
added external/fetch/http.c
@@ -0,0 +1,2328 @@
+
/*-
+
 * SPDX-License-Identifier: BSD-3-Clause
+
 *
+
 * Copyright (c) 2000-2014 Dag-Erling Smørgrav
+
 * 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
+
 *    in this position and unchanged.
+
 * 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.
+
 * 3. The name of the author may not be used to endorse or promote products
+
 *    derived from this software without specific prior written permission.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "bsd_compat.h"
+

+
#if !defined(TCP_NOPUSH) && defined(TCP_CORK)
+
#define TCP_NOPUSH TCP_CORK
+
#endif
+

+
/*
+
 * The following copyright applies to the base64 code:
+
 *
+
 *-
+
 * Copyright 1997 Massachusetts Institute of Technology
+
 *
+
 * Permission to use, copy, modify, and distribute this software and
+
 * its documentation for any purpose and without fee is hereby
+
 * granted, provided that both the above copyright notice and this
+
 * permission notice appear in all copies, that both the above
+
 * copyright notice and this permission notice appear in all
+
 * supporting documentation, and that the name of M.I.T. not be used
+
 * in advertising or publicity pertaining to distribution of the
+
 * software without specific, written prior permission.  M.I.T. makes
+
 * no representations about the suitability of this software for any
+
 * purpose.  It is provided "as is" without express or implied
+
 * warranty.
+
 *
+
 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
+
 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
+
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
+
 * SHALL M.I.T. 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/param.h>
+
#include <sys/socket.h>
+
#include <sys/time.h>
+

+
#include <ctype.h>
+
#include <err.h>
+
#include <errno.h>
+
#include <locale.h>
+
#include <netdb.h>
+
#include <stdarg.h>
+
#include <stdbool.h>
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <string.h>
+
#include <time.h>
+
#include <unistd.h>
+

+
#ifdef WITH_SSL
+
#include <openssl/md5.h>
+
#define MD5Init(c) MD5_Init(c)
+
#define MD5Update(c, data, len) MD5_Update(c, data, len)
+
#define MD5Final(md, c) MD5_Final(md, c)
+
#else
+
#include <md5.h>
+
#endif
+

+
#include <netinet/in.h>
+
#include <netinet/tcp.h>
+

+
#include "fetch.h"
+
#include "common.h"
+
#include "httperr.h"
+

+
/* Maximum number of redirects to follow */
+
#define MAX_REDIRECT 20
+

+
/* Persistent connection cache */
+
static conn_t *http_cached_conn;
+

+
static void
+
http_cache_flush(void)
+
{
+
	if (http_cached_conn != NULL) {
+
		fetch_close(http_cached_conn);
+
		http_cached_conn = NULL;
+
	}
+
}
+

+
/* Symbolic names for reply codes we care about */
+
#define HTTP_OK			200
+
#define HTTP_PARTIAL		206
+
#define HTTP_MOVED_PERM		301
+
#define HTTP_MOVED_TEMP		302
+
#define HTTP_SEE_OTHER		303
+
#define HTTP_NOT_MODIFIED	304
+
#define HTTP_USE_PROXY		305
+
#define HTTP_TEMP_REDIRECT	307
+
#define HTTP_PERM_REDIRECT	308
+
#define HTTP_NEED_AUTH		401
+
#define HTTP_NEED_PROXY_AUTH	407
+
#define HTTP_BAD_RANGE		416
+
#define HTTP_PROTOCOL_ERROR	999
+

+
#define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
+
			    || (xyz) == HTTP_MOVED_TEMP \
+
			    || (xyz) == HTTP_TEMP_REDIRECT \
+
			    || (xyz) == HTTP_PERM_REDIRECT \
+
			    || (xyz) == HTTP_USE_PROXY \
+
			    || (xyz) == HTTP_SEE_OTHER)
+

+
#define HTTP_ERROR(xyz) ((xyz) >= 400 && (xyz) <= 599)
+

+

+
/*****************************************************************************
+
 * I/O functions for decoding chunked streams
+
 */
+

+
struct httpio
+
{
+
	conn_t		*conn;		/* connection */
+
	int		 chunked;	/* chunked mode */
+
	int		 keep_alive;	/* eligible for connection reuse */
+
	char		*buf;		/* chunk buffer */
+
	size_t		 bufsize;	/* size of chunk buffer */
+
	size_t		 buflen;	/* amount of data currently in buffer */
+
	size_t		 bufpos;	/* current read offset in buffer */
+
	int		 eof;		/* end-of-file flag */
+
	int		 error;		/* error flag */
+
	size_t		 chunksize;	/* remaining size of current chunk */
+
	off_t		 contentlength;	/* Content-Length or -1 if unknown */
+
	off_t		 bytes_read;	/* total bytes read from body */
+
#ifndef NDEBUG
+
	size_t		 total;
+
#endif
+
	/* low speed limit tracking */
+
	time_t		 lowspeed_start;  /* when slow period began (0=none) */
+
	size_t		 lowspeed_bytes;  /* bytes received since lowspeed_start */
+
};
+

+
/*
+
 * Get next chunk header
+
 */
+
static int
+
http_new_chunk(struct httpio *io)
+
{
+
	char *p;
+

+
	if (fetch_getln(io->conn) == -1)
+
		return (-1);
+

+
	if (io->conn->buflen < 2 || !isxdigit((unsigned char)*io->conn->buf))
+
		return (-1);
+

+
	for (p = io->conn->buf; *p && !isspace((unsigned char)*p); ++p) {
+
		if (*p == ';')
+
			break;
+
		if (!isxdigit((unsigned char)*p))
+
			return (-1);
+
		if (isdigit((unsigned char)*p)) {
+
			io->chunksize = io->chunksize * 16 +
+
			    *p - '0';
+
		} else {
+
			io->chunksize = io->chunksize * 16 +
+
			    10 + tolower((unsigned char)*p) - 'a';
+
		}
+
	}
+

+
#ifndef NDEBUG
+
	if (fetchDebug) {
+
		io->total += io->chunksize;
+
		if (io->chunksize == 0)
+
			fprintf(stderr, "%s(): end of last chunk\n", __func__);
+
		else
+
			fprintf(stderr, "%s(): new chunk: %lu (%lu)\n",
+
			    __func__, (unsigned long)io->chunksize,
+
			    (unsigned long)io->total);
+
	}
+
#endif
+

+
	return (io->chunksize);
+
}
+

+
/*
+
 * Grow the input buffer to at least len bytes
+
 */
+
static inline int
+
http_growbuf(struct httpio *io, size_t len)
+
{
+
	char *tmp;
+

+
	if (io->bufsize >= len)
+
		return (0);
+

+
	if ((tmp = realloc(io->buf, len)) == NULL)
+
		return (-1);
+
	io->buf = tmp;
+
	io->bufsize = len;
+
	return (0);
+
}
+

+
/*
+
 * Fill the input buffer, do chunk decoding on the fly
+
 */
+
static ssize_t
+
http_fillbuf(struct httpio *io, size_t len)
+
{
+
	ssize_t nbytes;
+
	char ch;
+

+
	if (io->error)
+
		return (-1);
+
	if (io->eof)
+
		return (0);
+

+
	/* not chunked: just fetch the requested amount */
+
	if (io->chunked == 0) {
+
		/* limit read to remaining content if Content-Length is known */
+
		if (io->contentlength >= 0) {
+
			off_t remaining = io->contentlength - io->bytes_read;
+
			if (remaining <= 0) {
+
				io->eof = 1;
+
				return (0);
+
			}
+
			if ((off_t)len > remaining)
+
				len = (size_t)remaining;
+
		}
+
		if (http_growbuf(io, len) == -1)
+
			return (-1);
+
		if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
+
			io->error = errno;
+
			return (-1);
+
		}
+
		if (nbytes == 0) {
+
			io->eof = 1;
+
			return (0);
+
		}
+
		io->bytes_read += nbytes;
+
		io->buflen = nbytes;
+
		io->bufpos = 0;
+
		return (io->buflen);
+
	}
+

+
	/* chunked, but we ran out: get the next chunk header */
+
	if (io->chunksize == 0) {
+
		switch (http_new_chunk(io)) {
+
		case -1:
+
			io->error = EPROTO;
+
			return (-1);
+
		case 0:
+
			io->eof = 1;
+
			return (0);
+
		}
+
	}
+

+
	/* fetch the requested amount, but no more than the current chunk */
+
	if (len > io->chunksize)
+
		len = io->chunksize;
+
	if (http_growbuf(io, len) == -1)
+
		return (-1);
+
	if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
+
		io->error = errno;
+
		return (-1);
+
	}
+
	io->bufpos = 0;
+
	io->buflen = nbytes;
+
	io->chunksize -= nbytes;
+

+
	if (io->chunksize == 0) {
+
		if (fetch_read(io->conn, &ch, 1) != 1 || ch != '\r' ||
+
		    fetch_read(io->conn, &ch, 1) != 1 || ch != '\n')
+
			return (-1);
+
	}
+

+
	return (io->buflen);
+
}
+

+
/*
+
 * Read function
+
 */
+
static int
+
http_readfn(void *v, char *buf, int len)
+
{
+
	struct httpio *io = (struct httpio *)v;
+
	int rlen;
+

+
	if (io->error)
+
		return (-1);
+
	if (io->eof)
+
		return (0);
+

+
	/* empty buffer */
+
	if (!io->buf || io->bufpos == io->buflen) {
+
		if ((rlen = http_fillbuf(io, len)) < 0) {
+
			if ((errno = io->error) == EINTR)
+
				io->error = 0;
+
			return (-1);
+
		} else if (rlen == 0) {
+
			return (0);
+
		}
+
	}
+

+
	rlen = io->buflen - io->bufpos;
+
	if (len < rlen)
+
		rlen = len;
+
	memcpy(buf, io->buf + io->bufpos, rlen);
+
	io->bufpos += rlen;
+

+
	/* low speed limit check */
+
	if (fetchSpeedLimit > 0 && fetchSpeedTime > 0) {
+
		time_t now = time(NULL);
+
		if (io->lowspeed_start == 0) {
+
			io->lowspeed_start = now;
+
			io->lowspeed_bytes = rlen;
+
		} else {
+
			io->lowspeed_bytes += rlen;
+
			time_t elapsed = now - io->lowspeed_start;
+
			if (elapsed >= fetchSpeedTime) {
+
				if ((off_t)io->lowspeed_bytes / elapsed <
+
				    fetchSpeedLimit) {
+
					io->error = ETIMEDOUT;
+
					errno = ETIMEDOUT;
+
					fetch_syserr();
+
					return (-1);
+
				}
+
				/* reset window */
+
				io->lowspeed_start = now;
+
				io->lowspeed_bytes = 0;
+
			}
+
		}
+
	}
+

+
	return (rlen);
+
}
+

+
/*
+
 * Write function
+
 */
+
static int
+
http_writefn(void *v, const char *buf, int len)
+
{
+
	struct httpio *io = (struct httpio *)v;
+

+
	return (fetch_write(io->conn, buf, len));
+
}
+

+
/*
+
 * Drain any unread response body data
+
 */
+
static void
+
http_drain(struct httpio *io)
+
{
+
	char buf[4096];
+

+
	while (!io->eof && !io->error) {
+
		if (http_fillbuf(io, sizeof(buf)) <= 0)
+
			break;
+
		io->bufpos = io->buflen;
+
	}
+
}
+

+
/*
+
 * Close function — caches the connection for reuse when possible
+
 */
+
static int
+
http_closefn(void *v)
+
{
+
	struct httpio *io = (struct httpio *)v;
+
	int r;
+

+
	if (io->keep_alive && !io->error) {
+
		/* drain unread body so the connection is at a clean state */
+
		http_drain(io);
+
		if (!io->error) {
+
			/* connection is reusable — cache it */
+
			if (http_cached_conn != NULL)
+
				fetch_close(http_cached_conn);
+
			http_cached_conn = io->conn;
+
			/* clear leftover line buffer */
+
			io->conn->buflen = 0;
+
			DEBUGF("cached connection to %s:%d\n",
+
			    io->conn->cache_host, io->conn->cache_port);
+
			if (io->buf)
+
				free(io->buf);
+
			free(io);
+
			return (0);
+
		}
+
	}
+
	r = fetch_close(io->conn);
+
	if (io->buf)
+
		free(io->buf);
+
	free(io);
+
	return (r);
+
}
+

+
/*
+
 * Wrap a file descriptor up
+
 */
+
static FILE *
+
http_funopen(conn_t *conn, int chunked, int keep_alive, off_t clength)
+
{
+
	struct httpio *io;
+
	FILE *f;
+

+
	if ((io = calloc(1, sizeof(*io))) == NULL) {
+
		fetch_syserr();
+
		return (NULL);
+
	}
+
	io->conn = conn;
+
	io->chunked = chunked;
+
	io->keep_alive = keep_alive;
+
	io->contentlength = clength;
+
	io->bytes_read = 0;
+
	f = funopen(io, http_readfn, http_writefn, NULL, http_closefn);
+
	if (f == NULL) {
+
		fetch_syserr();
+
		free(io);
+
		return (NULL);
+
	}
+
	return (f);
+
}
+

+

+
/*****************************************************************************
+
 * Helper functions for talking to the server and parsing its replies
+
 */
+

+
/* Header types */
+
typedef enum {
+
	hdr_syserror = -2,
+
	hdr_error = -1,
+
	hdr_end = 0,
+
	hdr_unknown = 1,
+
	hdr_content_length,
+
	hdr_content_range,
+
	hdr_last_modified,
+
	hdr_location,
+
	hdr_transfer_encoding,
+
	hdr_www_authenticate,
+
	hdr_proxy_authenticate,
+
	hdr_connection,
+
} hdr_t;
+

+
/* Names of interesting headers */
+
static struct {
+
	hdr_t		 num;
+
	const char	*name;
+
} hdr_names[] = {
+
	{ hdr_content_length,		"Content-Length" },
+
	{ hdr_content_range,		"Content-Range" },
+
	{ hdr_last_modified,		"Last-Modified" },
+
	{ hdr_location,			"Location" },
+
	{ hdr_transfer_encoding,	"Transfer-Encoding" },
+
	{ hdr_www_authenticate,		"WWW-Authenticate" },
+
	{ hdr_proxy_authenticate,	"Proxy-Authenticate" },
+
	{ hdr_connection,		"Connection" },
+
	{ hdr_unknown,			NULL },
+
};
+

+
/*
+
 * Send a formatted line; optionally echo to terminal
+
 */
+
static int
+
http_cmd(conn_t *conn, const char *fmt, ...)
+
{
+
	va_list ap;
+
	size_t len;
+
	char *msg;
+
	int r;
+

+
	va_start(ap, fmt);
+
	len = vasprintf(&msg, fmt, ap);
+
	va_end(ap);
+

+
	if (msg == NULL) {
+
		errno = ENOMEM;
+
		fetch_syserr();
+
		return (-1);
+
	}
+

+
	r = fetch_putln(conn, msg, len);
+
	free(msg);
+

+
	if (r == -1) {
+
		fetch_syserr();
+
		return (-1);
+
	}
+

+
	return (0);
+
}
+

+
/*
+
 * Get and parse status line
+
 */
+
static int
+
http_get_reply(conn_t *conn)
+
{
+
	char *p;
+

+
	if (fetch_getln(conn) == -1)
+
		return (-1);
+
	/*
+
	 * A valid status line looks like "HTTP/m.n xyz reason" where m
+
	 * and n are the major and minor protocol version numbers and xyz
+
	 * is the reply code.
+
	 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
+
	 * just one) that do not send a version number, so we can't rely
+
	 * on finding one, but if we do, insist on it being 1.0 or 1.1.
+
	 * We don't care about the reason phrase.
+
	 */
+
	if (strncmp(conn->buf, "HTTP", 4) != 0)
+
		return (HTTP_PROTOCOL_ERROR);
+
	p = conn->buf + 4;
+
	if (*p == '/') {
+
		if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
+
			return (HTTP_PROTOCOL_ERROR);
+
		p += 4;
+
	}
+
	if (*p != ' ' ||
+
	    !isdigit((unsigned char)p[1]) ||
+
	    !isdigit((unsigned char)p[2]) ||
+
	    !isdigit((unsigned char)p[3]))
+
		return (HTTP_PROTOCOL_ERROR);
+

+
	conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
+
	return (conn->err);
+
}
+

+
/*
+
 * Check a header; if the type matches the given string, return a pointer
+
 * to the beginning of the value.
+
 */
+
static const char *
+
http_match(const char *str, const char *hdr)
+
{
+
	while (*str && *hdr &&
+
	    tolower((unsigned char)*str++) == tolower((unsigned char)*hdr++))
+
		/* nothing */;
+
	if (*str || *hdr != ':')
+
		return (NULL);
+
	while (*hdr && isspace((unsigned char)*++hdr))
+
		/* nothing */;
+
	return (hdr);
+
}
+

+

+
/*
+
 * Get the next header and return the appropriate symbolic code.  We
+
 * need to read one line ahead for checking for a continuation line
+
 * belonging to the current header (continuation lines start with
+
 * white space).
+
 *
+
 * We get called with a fresh line already in the conn buffer, either
+
 * from the previous http_next_header() invocation, or, the first
+
 * time, from a fetch_getln() performed by our caller.
+
 *
+
 * This stops when we encounter an empty line (we dont read beyond the header
+
 * area).
+
 *
+
 * Note that the "headerbuf" is just a place to return the result. Its
+
 * contents are not used for the next call. This means that no cleanup
+
 * is needed when ie doing another connection, just call the cleanup when
+
 * fully done to deallocate memory.
+
 */
+

+
/* Limit the max number of continuation lines to some reasonable value */
+
#define HTTP_MAX_CONT_LINES 10
+

+
/* Place into which to build a header from one or several lines */
+
typedef struct {
+
	char	*buf;		/* buffer */
+
	size_t	 bufsize;	/* buffer size */
+
	size_t	 buflen;	/* length of buffer contents */
+
} http_headerbuf_t;
+

+
static void
+
init_http_headerbuf(http_headerbuf_t *buf)
+
{
+
	buf->buf = NULL;
+
	buf->bufsize = 0;
+
	buf->buflen = 0;
+
}
+

+
static void
+
clean_http_headerbuf(http_headerbuf_t *buf)
+
{
+
	if (buf->buf)
+
		free(buf->buf);
+
	init_http_headerbuf(buf);
+
}
+

+
/* Remove whitespace at the end of the buffer */
+
static void
+
http_conn_trimright(conn_t *conn)
+
{
+
	while (conn->buflen &&
+
	       isspace((unsigned char)conn->buf[conn->buflen - 1]))
+
		conn->buflen--;
+
	conn->buf[conn->buflen] = '\0';
+
}
+

+
static hdr_t
+
http_next_header(conn_t *conn, http_headerbuf_t *hbuf, const char **p)
+
{
+
	unsigned int i, len;
+

+
	/*
+
	 * Have to do the stripping here because of the first line. So
+
	 * it's done twice for the subsequent lines. No big deal
+
	 */
+
	http_conn_trimright(conn);
+
	if (conn->buflen == 0)
+
		return (hdr_end);
+

+
	/* Copy the line to the headerbuf */
+
	if (hbuf->bufsize < conn->buflen + 1) {
+
		if ((hbuf->buf = realloc(hbuf->buf, conn->buflen + 1)) == NULL)
+
			return (hdr_syserror);
+
		hbuf->bufsize = conn->buflen + 1;
+
	}
+
	strcpy(hbuf->buf, conn->buf);
+
	hbuf->buflen = conn->buflen;
+

+
	/*
+
	 * Fetch possible continuation lines. Stop at 1st non-continuation
+
	 * and leave it in the conn buffer
+
	 */
+
	for (i = 0; i < HTTP_MAX_CONT_LINES; i++) {
+
		if (fetch_getln(conn) == -1)
+
			return (hdr_syserror);
+

+
		/*
+
		 * Note: we carry on the idea from the previous version
+
		 * that a pure whitespace line is equivalent to an empty
+
		 * one (so it's not continuation and will be handled when
+
		 * we are called next)
+
		 */
+
		http_conn_trimright(conn);
+
		if (conn->buf[0] != ' ' && conn->buf[0] != "\t"[0])
+
			break;
+

+
		/* Got a continuation line. Concatenate to previous */
+
		len = hbuf->buflen + conn->buflen;
+
		if (hbuf->bufsize < len + 1) {
+
			len *= 2;
+
			if ((hbuf->buf = realloc(hbuf->buf, len + 1)) == NULL)
+
				return (hdr_syserror);
+
			hbuf->bufsize = len + 1;
+
		}
+
		strcpy(hbuf->buf + hbuf->buflen, conn->buf);
+
		hbuf->buflen += conn->buflen;
+
	}
+

+
	/*
+
	 * We could check for malformed headers but we don't really care.
+
	 * A valid header starts with a token immediately followed by a
+
	 * colon; a token is any sequence of non-control, non-whitespace
+
	 * characters except "()<>@,;:\\\"{}".
+
	 */
+
	for (i = 0; hdr_names[i].num != hdr_unknown; i++)
+
		if ((*p = http_match(hdr_names[i].name, hbuf->buf)) != NULL)
+
			return (hdr_names[i].num);
+

+
	return (hdr_unknown);
+
}
+

+
/**************************
+
 * [Proxy-]Authenticate header parsing
+
 */
+

+
/*
+
 * Read doublequote-delimited string into output buffer obuf (allocated
+
 * by caller, whose responsibility it is to ensure that it's big enough)
+
 * cp points to the first char after the initial '"'
+
 * Handles \ quoting
+
 * Returns pointer to the first char after the terminating double quote, or
+
 * NULL for error.
+
 */
+
static const char *
+
http_parse_headerstring(const char *cp, char *obuf)
+
{
+
	for (;;) {
+
		switch (*cp) {
+
		case 0: /* Unterminated string */
+
			*obuf = 0;
+
			return (NULL);
+
		case '"': /* Ending quote */
+
			*obuf = 0;
+
			return (++cp);
+
		case '\\':
+
			if (*++cp == 0) {
+
				*obuf = 0;
+
				return (NULL);
+
			}
+
			/* FALLTHROUGH */
+
		default:
+
			*obuf++ = *cp++;
+
		}
+
	}
+
}
+

+
/* Http auth challenge schemes */
+
typedef enum {HTTPAS_UNKNOWN, HTTPAS_BASIC,HTTPAS_DIGEST} http_auth_schemes_t;
+

+
/* Data holder for a Basic or Digest challenge. */
+
typedef struct {
+
	http_auth_schemes_t scheme;
+
	char	*realm;
+
	char	*qop;
+
	char	*nonce;
+
	char	*opaque;
+
	char	*algo;
+
	int	 stale;
+
	int	 nc; /* Nonce count */
+
} http_auth_challenge_t;
+

+
static void
+
init_http_auth_challenge(http_auth_challenge_t *b)
+
{
+
	b->scheme = HTTPAS_UNKNOWN;
+
	b->realm = b->qop = b->nonce = b->opaque = b->algo = NULL;
+
	b->stale = b->nc = 0;
+
}
+

+
static void
+
clean_http_auth_challenge(http_auth_challenge_t *b)
+
{
+
	if (b->realm)
+
		free(b->realm);
+
	if (b->qop)
+
		free(b->qop);
+
	if (b->nonce)
+
		free(b->nonce);
+
	if (b->opaque)
+
		free(b->opaque);
+
	if (b->algo)
+
		free(b->algo);
+
	init_http_auth_challenge(b);
+
}
+

+
/* Data holder for an array of challenges offered in an http response. */
+
#define MAX_CHALLENGES 10
+
typedef struct {
+
	http_auth_challenge_t *challenges[MAX_CHALLENGES];
+
	int	count; /* Number of parsed challenges in the array */
+
	int	valid; /* We did parse an authenticate header */
+
} http_auth_challenges_t;
+

+
static void
+
init_http_auth_challenges(http_auth_challenges_t *cs)
+
{
+
	int i;
+
	for (i = 0; i < MAX_CHALLENGES; i++)
+
		cs->challenges[i] = NULL;
+
	cs->count = cs->valid = 0;
+
}
+

+
static void
+
clean_http_auth_challenges(http_auth_challenges_t *cs)
+
{
+
	int i;
+
	/* We rely on non-zero pointers being allocated, not on the count */
+
	for (i = 0; i < MAX_CHALLENGES; i++) {
+
		if (cs->challenges[i] != NULL) {
+
			clean_http_auth_challenge(cs->challenges[i]);
+
			free(cs->challenges[i]);
+
		}
+
	}
+
	init_http_auth_challenges(cs);
+
}
+

+
/*
+
 * Enumeration for lexical elements. Separators will be returned as their own
+
 * ascii value
+
 */
+
typedef enum {HTTPHL_WORD=256, HTTPHL_STRING=257, HTTPHL_END=258,
+
	      HTTPHL_ERROR = 259} http_header_lex_t;
+

+
/*
+
 * Determine what kind of token comes next and return possible value
+
 * in buf, which is supposed to have been allocated big enough by
+
 * caller. Advance input pointer and return element type.
+
 */
+
static int
+
http_header_lex(const char **cpp, char *buf)
+
{
+
	size_t l;
+
	/* Eat initial whitespace */
+
	*cpp += strspn(*cpp, " \t");
+
	if (**cpp == 0)
+
		return (HTTPHL_END);
+

+
	/* Separator ? */
+
	if (**cpp == ',' || **cpp == '=')
+
		return (*((*cpp)++));
+

+
	/* String ? */
+
	if (**cpp == '"') {
+
		*cpp = http_parse_headerstring(++*cpp, buf);
+
		if (*cpp == NULL)
+
			return (HTTPHL_ERROR);
+
		return (HTTPHL_STRING);
+
	}
+

+
	/* Read other token, until separator or whitespace */
+
	l = strcspn(*cpp, " \t,=");
+
	memcpy(buf, *cpp, l);
+
	buf[l] = 0;
+
	*cpp += l;
+
	return (HTTPHL_WORD);
+
}
+

+
/*
+
 * Read challenges from http xxx-authenticate header and accumulate them
+
 * in the challenges list structure.
+
 *
+
 * Headers with multiple challenges are specified by rfc2617, but
+
 * servers (ie: squid) often send them in separate headers instead,
+
 * which in turn is forbidden by the http spec (multiple headers with
+
 * the same name are only allowed for pure comma-separated lists, see
+
 * rfc2616 sec 4.2).
+
 *
+
 * We support both approaches anyway
+
 */
+
static int
+
http_parse_authenticate(const char *cp, http_auth_challenges_t *cs)
+
{
+
	int ret = -1;
+
	http_header_lex_t lex;
+
	char *key = malloc(strlen(cp) + 1);
+
	char *value = malloc(strlen(cp) + 1);
+
	char *buf = malloc(strlen(cp) + 1);
+

+
	if (key == NULL || value == NULL || buf == NULL) {
+
		fetch_syserr();
+
		goto out;
+
	}
+

+
	/* In any case we've seen the header and we set the valid bit */
+
	cs->valid = 1;
+

+
	/* Need word first */
+
	lex = http_header_lex(&cp, key);
+
	if (lex != HTTPHL_WORD)
+
		goto out;
+

+
	/* Loop on challenges */
+
	for (; cs->count < MAX_CHALLENGES; cs->count++) {
+
		cs->challenges[cs->count] =
+
			malloc(sizeof(http_auth_challenge_t));
+
		if (cs->challenges[cs->count] == NULL) {
+
			fetch_syserr();
+
			goto out;
+
		}
+
		init_http_auth_challenge(cs->challenges[cs->count]);
+
		if (strcasecmp(key, "basic") == 0) {
+
			cs->challenges[cs->count]->scheme = HTTPAS_BASIC;
+
		} else if (strcasecmp(key, "digest") == 0) {
+
			cs->challenges[cs->count]->scheme = HTTPAS_DIGEST;
+
		} else {
+
			cs->challenges[cs->count]->scheme = HTTPAS_UNKNOWN;
+
			/*
+
			 * Continue parsing as basic or digest may
+
			 * follow, and the syntax is the same for
+
			 * all. We'll just ignore this one when
+
			 * looking at the list
+
			 */
+
		}
+

+
		/* Loop on attributes */
+
		for (;;) {
+
			/* Key */
+
			lex = http_header_lex(&cp, key);
+
			if (lex != HTTPHL_WORD)
+
				goto out;
+

+
			/* Equal sign */
+
			lex = http_header_lex(&cp, buf);
+
			if (lex != '=')
+
				goto out;
+

+
			/* Value */
+
			lex = http_header_lex(&cp, value);
+
			if (lex != HTTPHL_WORD && lex != HTTPHL_STRING)
+
				goto out;
+

+
			if (strcasecmp(key, "realm") == 0) {
+
				cs->challenges[cs->count]->realm =
+
				    strdup(value);
+
			} else if (strcasecmp(key, "qop") == 0) {
+
				cs->challenges[cs->count]->qop =
+
				    strdup(value);
+
			} else if (strcasecmp(key, "nonce") == 0) {
+
				cs->challenges[cs->count]->nonce =
+
				    strdup(value);
+
			} else if (strcasecmp(key, "opaque") == 0) {
+
				cs->challenges[cs->count]->opaque =
+
				    strdup(value);
+
			} else if (strcasecmp(key, "algorithm") == 0) {
+
				cs->challenges[cs->count]->algo =
+
				    strdup(value);
+
			} else if (strcasecmp(key, "stale") == 0) {
+
				cs->challenges[cs->count]->stale =
+
				    strcasecmp(value, "no");
+
			} else {
+
				/* ignore unknown attributes */
+
			}
+

+
			/* Comma or Next challenge or End */
+
			lex = http_header_lex(&cp, key);
+
			/*
+
			 * If we get a word here, this is the beginning of the
+
			 * next challenge. Break the attributes loop
+
			 */
+
			if (lex == HTTPHL_WORD)
+
				break;
+

+
			if (lex == HTTPHL_END) {
+
				/* End while looking for ',' is normal exit */
+
				cs->count++;
+
				ret = 0;
+
				goto out;
+
			}
+
			/* Anything else is an error */
+
			if (lex != ',')
+
				goto out;
+

+
		} /* End attributes loop */
+
	} /* End challenge loop */
+

+
	/*
+
	 * Challenges max count exceeded. This really can't happen
+
	 * with normal data, something's fishy -> error
+
	 */
+

+
out:
+
	if (key)
+
		free(key);
+
	if (value)
+
		free(value);
+
	if (buf)
+
		free(buf);
+
	return (ret);
+
}
+

+

+
/*
+
 * Parse a last-modified header
+
 */
+
static int
+
http_parse_mtime(const char *p, time_t *mtime)
+
{
+
	char locale[64], *r;
+
	struct tm tm;
+

+
	memset(&tm, 0, sizeof(tm));
+
	strlcpy(locale, setlocale(LC_TIME, NULL), sizeof(locale));
+
	setlocale(LC_TIME, "C");
+
	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
+
	/*
+
	 * Some proxies use UTC in response, but it should still be
+
	 * parsed. RFC2616 states GMT and UTC are exactly equal for HTTP.
+
	 */
+
	if (r == NULL)
+
		r = strptime(p, "%a, %d %b %Y %H:%M:%S UTC", &tm);
+
	/* XXX should add support for date-2 and date-3 */
+
	setlocale(LC_TIME, locale);
+
	if (r == NULL)
+
		return (-1);
+
	DEBUGF("last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
+
	    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
+
	    tm.tm_hour, tm.tm_min, tm.tm_sec);
+
	*mtime = timegm(&tm);
+
	return (0);
+
}
+

+
/*
+
 * Parse a content-length header
+
 */
+
static int
+
http_parse_length(const char *p, off_t *length)
+
{
+
	off_t len;
+

+
	for (len = 0; *p && isdigit((unsigned char)*p); ++p)
+
		len = len * 10 + (*p - '0');
+
	if (*p)
+
		return (-1);
+
	DEBUGF("content length: [%lld]\n", (long long)len);
+
	*length = len;
+
	return (0);
+
}
+

+
/*
+
 * Parse a content-range header
+
 */
+
static int
+
http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
+
{
+
	off_t first, last, len;
+

+
	if (strncasecmp(p, "bytes ", 6) != 0)
+
		return (-1);
+
	p += 6;
+
	if (*p == '*') {
+
		first = last = -1;
+
		++p;
+
	} else {
+
		for (first = 0; *p && isdigit((unsigned char)*p); ++p)
+
			first = first * 10 + *p - '0';
+
		if (*p != '-')
+
			return (-1);
+
		for (last = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
+
			last = last * 10 + *p - '0';
+
	}
+
	if (first > last || *p != '/')
+
		return (-1);
+
	for (len = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
+
		len = len * 10 + *p - '0';
+
	if (*p || len < last - first + 1)
+
		return (-1);
+
	if (first == -1) {
+
		DEBUGF("content range: [*/%lld]\n", (long long)len);
+
		*length = 0;
+
	} else {
+
		DEBUGF("content range: [%lld-%lld/%lld]\n",
+
		    (long long)first, (long long)last, (long long)len);
+
		*length = last - first + 1;
+
	}
+
	*offset = first;
+
	*size = len;
+
	return (0);
+
}
+

+

+
/*****************************************************************************
+
 * Helper functions for authorization
+
 */
+

+
/*
+
 * Base64 encoding
+
 */
+
static char *
+
http_base64(const char *src)
+
{
+
	static const char base64[] =
+
	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+
	    "abcdefghijklmnopqrstuvwxyz"
+
	    "0123456789+/";
+
	char *str, *dst;
+
	size_t l;
+
	int t;
+

+
	l = strlen(src);
+
	if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
+
		return (NULL);
+
	dst = str;
+

+
	while (l >= 3) {
+
		t = (src[0] << 16) | (src[1] << 8) | src[2];
+
		dst[0] = base64[(t >> 18) & 0x3f];
+
		dst[1] = base64[(t >> 12) & 0x3f];
+
		dst[2] = base64[(t >> 6) & 0x3f];
+
		dst[3] = base64[(t >> 0) & 0x3f];
+
		src += 3; l -= 3;
+
		dst += 4;
+
	}
+

+
	switch (l) {
+
	case 2:
+
		t = (src[0] << 16) | (src[1] << 8);
+
		dst[0] = base64[(t >> 18) & 0x3f];
+
		dst[1] = base64[(t >> 12) & 0x3f];
+
		dst[2] = base64[(t >> 6) & 0x3f];
+
		dst[3] = '=';
+
		dst += 4;
+
		break;
+
	case 1:
+
		t = src[0] << 16;
+
		dst[0] = base64[(t >> 18) & 0x3f];
+
		dst[1] = base64[(t >> 12) & 0x3f];
+
		dst[2] = dst[3] = '=';
+
		dst += 4;
+
		break;
+
	case 0:
+
		break;
+
	}
+

+
	*dst = 0;
+
	return (str);
+
}
+

+

+
/*
+
 * Extract authorization parameters from environment value.
+
 * The value is like scheme:realm:user:pass
+
 */
+
typedef struct {
+
	char	*scheme;
+
	char	*realm;
+
	char	*user;
+
	char	*password;
+
} http_auth_params_t;
+

+
static void
+
init_http_auth_params(http_auth_params_t *s)
+
{
+
	s->scheme = s->realm = s->user = s->password = NULL;
+
}
+

+
static void
+
clean_http_auth_params(http_auth_params_t *s)
+
{
+
	if (s->scheme)
+
		free(s->scheme);
+
	if (s->realm)
+
		free(s->realm);
+
	if (s->user)
+
		free(s->user);
+
	if (s->password)
+
		free(s->password);
+
	init_http_auth_params(s);
+
}
+

+
static int
+
http_authfromenv(const char *p, http_auth_params_t *parms)
+
{
+
	int ret = -1;
+
	char *v, *ve;
+
	char *str = strdup(p);
+

+
	if (str == NULL) {
+
		fetch_syserr();
+
		return (-1);
+
	}
+
	v = str;
+

+
	if ((ve = strchr(v, ':')) == NULL)
+
		goto out;
+

+
	*ve = 0;
+
	if ((parms->scheme = strdup(v)) == NULL) {
+
		fetch_syserr();
+
		goto out;
+
	}
+
	v = ve + 1;
+

+
	if ((ve = strchr(v, ':')) == NULL)
+
		goto out;
+

+
	*ve = 0;
+
	if ((parms->realm = strdup(v)) == NULL) {
+
		fetch_syserr();
+
		goto out;
+
	}
+
	v = ve + 1;
+

+
	if ((ve = strchr(v, ':')) == NULL)
+
		goto out;
+

+
	*ve = 0;
+
	if ((parms->user = strdup(v)) == NULL) {
+
		fetch_syserr();
+
		goto out;
+
	}
+
	v = ve + 1;
+

+

+
	if ((parms->password = strdup(v)) == NULL) {
+
		fetch_syserr();
+
		goto out;
+
	}
+
	ret = 0;
+
out:
+
	if (ret == -1)
+
		clean_http_auth_params(parms);
+
	if (str)
+
		free(str);
+
	return (ret);
+
}
+

+

+
/*
+
 * Digest response: the code to compute the digest is taken from the
+
 * sample implementation in RFC2616
+
 */
+
#define IN const
+
#define OUT
+

+
#define HASHLEN 16
+
typedef char HASH[HASHLEN];
+
#define HASHHEXLEN 32
+
typedef char HASHHEX[HASHHEXLEN+1];
+

+
static const char *hexchars = "0123456789abcdef";
+
static void
+
CvtHex(IN HASH Bin, OUT HASHHEX Hex)
+
{
+
	unsigned short i;
+
	unsigned char j;
+

+
	for (i = 0; i < HASHLEN; i++) {
+
		j = (Bin[i] >> 4) & 0xf;
+
		Hex[i*2] = hexchars[j];
+
		j = Bin[i] & 0xf;
+
		Hex[i*2+1] = hexchars[j];
+
	}
+
	Hex[HASHHEXLEN] = '\0';
+
};
+

+
/* calculate H(A1) as per spec */
+
static void
+
DigestCalcHA1(
+
	IN char * pszAlg,
+
	IN char * pszUserName,
+
	IN char * pszRealm,
+
	IN char * pszPassword,
+
	IN char * pszNonce,
+
	IN char * pszCNonce,
+
	OUT HASHHEX SessionKey
+
	)
+
{
+
	MD5_CTX Md5Ctx;
+
	HASH HA1;
+

+
	MD5Init(&Md5Ctx);
+
	MD5Update(&Md5Ctx, pszUserName, strlen(pszUserName));
+
	MD5Update(&Md5Ctx, ":", 1);
+
	MD5Update(&Md5Ctx, pszRealm, strlen(pszRealm));
+
	MD5Update(&Md5Ctx, ":", 1);
+
	MD5Update(&Md5Ctx, pszPassword, strlen(pszPassword));
+
	MD5Final(HA1, &Md5Ctx);
+
	if (strcasecmp(pszAlg, "md5-sess") == 0) {
+

+
		MD5Init(&Md5Ctx);
+
		MD5Update(&Md5Ctx, HA1, HASHLEN);
+
		MD5Update(&Md5Ctx, ":", 1);
+
		MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
+
		MD5Update(&Md5Ctx, ":", 1);
+
		MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
+
		MD5Final(HA1, &Md5Ctx);
+
	}
+
	CvtHex(HA1, SessionKey);
+
}
+

+
/* calculate request-digest/response-digest as per HTTP Digest spec */
+
static void
+
DigestCalcResponse(
+
	IN HASHHEX HA1,           /* H(A1) */
+
	IN char * pszNonce,       /* nonce from server */
+
	IN char * pszNonceCount,  /* 8 hex digits */
+
	IN char * pszCNonce,      /* client nonce */
+
	IN char * pszQop,         /* qop-value: "", "auth", "auth-int" */
+
	IN char * pszMethod,      /* method from the request */
+
	IN char * pszDigestUri,   /* requested URL */
+
	IN HASHHEX HEntity,       /* H(entity body) if qop="auth-int" */
+
	OUT HASHHEX Response      /* request-digest or response-digest */
+
	)
+
{
+
#if 0
+
	DEBUGF("Calc: HA1[%s] Nonce[%s] qop[%s] method[%s] URI[%s]\n",
+
	    HA1, pszNonce, pszQop, pszMethod, pszDigestUri);
+
#endif
+
	MD5_CTX Md5Ctx;
+
	HASH HA2;
+
	HASH RespHash;
+
	HASHHEX HA2Hex;
+

+
	// calculate H(A2)
+
	MD5Init(&Md5Ctx);
+
	MD5Update(&Md5Ctx, pszMethod, strlen(pszMethod));
+
	MD5Update(&Md5Ctx, ":", 1);
+
	MD5Update(&Md5Ctx, pszDigestUri, strlen(pszDigestUri));
+
	if (strcasecmp(pszQop, "auth-int") == 0) {
+
		MD5Update(&Md5Ctx, ":", 1);
+
		MD5Update(&Md5Ctx, HEntity, HASHHEXLEN);
+
	}
+
	MD5Final(HA2, &Md5Ctx);
+
	CvtHex(HA2, HA2Hex);
+

+
	// calculate response
+
	MD5Init(&Md5Ctx);
+
	MD5Update(&Md5Ctx, HA1, HASHHEXLEN);
+
	MD5Update(&Md5Ctx, ":", 1);
+
	MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
+
	MD5Update(&Md5Ctx, ":", 1);
+
	if (*pszQop) {
+
		MD5Update(&Md5Ctx, pszNonceCount, strlen(pszNonceCount));
+
		MD5Update(&Md5Ctx, ":", 1);
+
		MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
+
		MD5Update(&Md5Ctx, ":", 1);
+
		MD5Update(&Md5Ctx, pszQop, strlen(pszQop));
+
		MD5Update(&Md5Ctx, ":", 1);
+
	}
+
	MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
+
	MD5Final(RespHash, &Md5Ctx);
+
	CvtHex(RespHash, Response);
+
}
+

+
/*
+
 * Generate/Send a Digest authorization header
+
 * This looks like: [Proxy-]Authorization: credentials
+
 *
+
 *  credentials      = "Digest" digest-response
+
 *  digest-response  = 1#( username | realm | nonce | digest-uri
+
 *                      | response | [ algorithm ] | [cnonce] |
+
 *                      [opaque] | [message-qop] |
+
 *                          [nonce-count]  | [auth-param] )
+
 *  username         = "username" "=" username-value
+
 *  username-value   = quoted-string
+
 *  digest-uri       = "uri" "=" digest-uri-value
+
 *  digest-uri-value = request-uri   ; As specified by HTTP/1.1
+
 *  message-qop      = "qop" "=" qop-value
+
 *  cnonce           = "cnonce" "=" cnonce-value
+
 *  cnonce-value     = nonce-value
+
 *  nonce-count      = "nc" "=" nc-value
+
 *  nc-value         = 8LHEX
+
 *  response         = "response" "=" request-digest
+
 *  request-digest = <"> 32LHEX <">
+
 */
+
static int
+
http_digest_auth(conn_t *conn, const char *hdr, http_auth_challenge_t *c,
+
		 http_auth_params_t *parms, struct url *url)
+
{
+
	int r;
+
	char noncecount[10];
+
	char cnonce[40];
+
	char *options = NULL;
+

+
	if (!c->realm || !c->nonce) {
+
		DEBUGF("realm/nonce not set in challenge\n");
+
		return(-1);
+
	}
+
	if (!c->algo)
+
		c->algo = strdup("");
+

+
	if (asprintf(&options, "%s%s%s%s",
+
	    *c->algo? ",algorithm=" : "", c->algo,
+
	    c->opaque? ",opaque=" : "", c->opaque?c->opaque:"") < 0)
+
		return (-1);
+

+
	if (!c->qop) {
+
		c->qop = strdup("");
+
		*noncecount = 0;
+
		*cnonce = 0;
+
	} else {
+
		c->nc++;
+
		sprintf(noncecount, "%08x", c->nc);
+
		/* We don't try very hard with the cnonce ... */
+
		sprintf(cnonce, "%x%lx", getpid(), (unsigned long)time(0));
+
	}
+

+
	HASHHEX HA1;
+
	DigestCalcHA1(c->algo, parms->user, c->realm,
+
		      parms->password, c->nonce, cnonce, HA1);
+
	DEBUGF("HA1: [%s]\n", HA1);
+
	HASHHEX digest, null;
+
	memset(null, 0, sizeof(null));
+
	DigestCalcResponse(HA1, c->nonce, noncecount, cnonce, c->qop,
+
			   "GET", url->doc, null, digest);
+

+
	if (c->qop[0]) {
+
		r = http_cmd(conn, "%s: Digest username=\"%s\",realm=\"%s\","
+
			     "nonce=\"%s\",uri=\"%s\",response=\"%s\","
+
			     "qop=\"auth\", cnonce=\"%s\", nc=%s%s",
+
			     hdr, parms->user, c->realm,
+
			     c->nonce, url->doc, digest,
+
			     cnonce, noncecount, options);
+
	} else {
+
		r = http_cmd(conn, "%s: Digest username=\"%s\",realm=\"%s\","
+
			     "nonce=\"%s\",uri=\"%s\",response=\"%s\"%s",
+
			     hdr, parms->user, c->realm,
+
			     c->nonce, url->doc, digest, options);
+
	}
+
	if (options)
+
		free(options);
+
	return (r);
+
}
+

+
/*
+
 * Encode username and password
+
 */
+
static int
+
http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
+
{
+
	char *upw, *auth;
+
	int r;
+

+
	DEBUGF("basic: usr: [%s]\n", usr);
+
	DEBUGF("basic: pwd: [%s]\n", pwd);
+
	if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
+
		return (-1);
+
	auth = http_base64(upw);
+
	free(upw);
+
	if (auth == NULL)
+
		return (-1);
+
	r = http_cmd(conn, "%s: Basic %s", hdr, auth);
+
	free(auth);
+
	return (r);
+
}
+

+
/*
+
 * Chose the challenge to answer and call the appropriate routine to
+
 * produce the header.
+
 */
+
static int
+
http_authorize(conn_t *conn, const char *hdr, http_auth_challenges_t *cs,
+
	       http_auth_params_t *parms, struct url *url)
+
{
+
	http_auth_challenge_t *digest = NULL;
+
	int i;
+

+
	/* If user or pass are null we're not happy */
+
	if (!parms->user || !parms->password) {
+
		DEBUGF("NULL usr or pass\n");
+
		return (-1);
+
	}
+

+
	/* Look for a Digest */
+
	for (i = 0; i < cs->count; i++) {
+
		if (cs->challenges[i]->scheme == HTTPAS_DIGEST)
+
			digest = cs->challenges[i];
+
	}
+

+
	/* Error if "Digest" was specified and there is no Digest challenge */
+
	if (!digest &&
+
	    (parms->scheme && strcasecmp(parms->scheme, "digest") == 0)) {
+
		DEBUGF("Digest auth in env, not supported by peer\n");
+
		return (-1);
+
	}
+
	/*
+
	 * If "basic" was specified in the environment, or there is no Digest
+
	 * challenge, do the basic thing. Don't need a challenge for this,
+
	 * so no need to check basic!=NULL
+
	 */
+
	if (!digest ||
+
	    (parms->scheme && strcasecmp(parms->scheme, "basic") == 0))
+
		return (http_basic_auth(conn,hdr,parms->user,parms->password));
+

+
	/* Else, prefer digest. We just checked that it's not NULL */
+
	return (http_digest_auth(conn, hdr, digest, parms, url));
+
}
+

+
/*
+
 * Send custom headers from a newline-separated string.
+
 * Each line should be a complete "Header-Name: value" pair.
+
 * Empty lines and lines without a colon are silently skipped.
+
 */
+
static void
+
http_custom_headers(conn_t *conn, const char *hdrs)
+
{
+
	const char *p, *eol;
+
	size_t len;
+

+
	for (p = hdrs; *p != '\0'; p = eol) {
+
		eol = strpbrk(p, "\r\n");
+
		if (eol == NULL)
+
			eol = p + strlen(p);
+
		len = eol - p;
+
		/* skip empty lines and lines without ':' */
+
		if (len > 0 && memchr(p, ':', len) != NULL)
+
			http_cmd(conn, "%.*s", (int)len, p);
+
		/* skip past \r\n or \n */
+
		if (*eol == '\r')
+
			eol++;
+
		if (*eol == '\n')
+
			eol++;
+
	}
+
}
+

+

+
/*****************************************************************************
+
 * Helper functions for connecting to a server or proxy
+
 */
+

+
/*
+
 * Connect to the correct HTTP server or proxy.
+
 */
+
static conn_t *
+
http_connect(struct url *URL, struct url *purl, const char *flags)
+
{
+
	struct url *curl;
+
	conn_t *conn;
+
	hdr_t h;
+
	http_headerbuf_t headerbuf;
+
	const char *p;
+
	int verbose;
+
	int af, val;
+
	int serrno;
+
	bool isproxyauth = false;
+
	http_auth_challenges_t proxy_challenges;
+

+
#ifdef INET6
+
	af = AF_UNSPEC;
+
#else
+
	af = AF_INET;
+
#endif
+

+
	verbose = CHECK_FLAG('v');
+
	if (CHECK_FLAG('4'))
+
		af = AF_INET;
+
#ifdef INET6
+
	else if (CHECK_FLAG('6'))
+
		af = AF_INET6;
+
#endif
+

+
	curl = (purl != NULL) ? purl : URL;
+

+
	/*
+
	 * Try to reuse a cached connection.  Only for direct (non-proxy)
+
	 * connections where host, port, and scheme match.
+
	 */
+
	if (purl == NULL && http_cached_conn != NULL) {
+
		int is_ssl = (strcmp(URL->scheme, SCHEME_HTTPS) == 0);
+
		if (strcmp(http_cached_conn->cache_host, URL->host) == 0 &&
+
		    http_cached_conn->cache_port == URL->port &&
+
		    http_cached_conn->cache_ssl == is_ssl) {
+
			conn = http_cached_conn;
+
			http_cached_conn = NULL;
+
			DEBUGF("reusing cached connection to %s:%d\n",
+
			    URL->host, URL->port);
+
			val = 1;
+
#ifdef TCP_NOPUSH
+
			setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH,
+
			    &val, sizeof(val));
+
#endif
+
			return (conn);
+
		}
+
		/* stale cache — different host */
+
		http_cache_flush();
+
	}
+

+
retry:
+
	if ((conn = fetch_connect(curl->host, curl->port, af, verbose)) == NULL)
+
		/* fetch_connect() has already set an error code */
+
		return (NULL);
+

+
	/* record connection identity for later cache matching */
+
	strlcpy(conn->cache_host, URL->host, sizeof(conn->cache_host));
+
	conn->cache_port = URL->port;
+
	conn->cache_ssl = (strcmp(URL->scheme, SCHEME_HTTPS) == 0);
+
	init_http_headerbuf(&headerbuf);
+
	if (strcmp(URL->scheme, SCHEME_HTTPS) == 0 && purl) {
+
		init_http_auth_challenges(&proxy_challenges);
+
		http_cmd(conn, "CONNECT %s:%d HTTP/1.1", URL->host, URL->port);
+
		http_cmd(conn, "Host: %s:%d", URL->host, URL->port);
+
		if (isproxyauth) {
+
			http_auth_params_t aparams;
+
			init_http_auth_params(&aparams);
+
			if (*purl->user || *purl->pwd) {
+
				aparams.user = strdup(purl->user);
+
				aparams.password = strdup(purl->pwd);
+
			} else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL &&
+
				    *p != '\0') {
+
				if (http_authfromenv(p, &aparams) < 0) {
+
					http_seterr(HTTP_NEED_PROXY_AUTH);
+
					fetch_syserr();
+
					goto ouch;
+
				}
+
			} else if (fetch_netrc_auth(purl) == 0) {
+
				aparams.user = strdup(purl->user);
+
				aparams.password = strdup(purl->pwd);
+
			} else {
+
				/*
+
				 * No auth information found in system - exiting
+
				 * with warning.
+
				 */
+
				warnx("Missing username and/or password set");
+
				fetch_syserr();
+
				goto ouch;
+
			}
+
			http_authorize(conn, "Proxy-Authorization",
+
			    &proxy_challenges, &aparams, purl);
+
			clean_http_auth_params(&aparams);
+
		}
+
		http_cmd(conn, "");
+
		/* Get reply from CONNECT Tunnel attempt */
+
		int httpreply = http_get_reply(conn);
+
		if (httpreply != HTTP_OK) {
+
			http_seterr(httpreply);
+
			/* If the error is a 407/HTTP_NEED_PROXY_AUTH */
+
			if (httpreply == HTTP_NEED_PROXY_AUTH &&
+
			    ! isproxyauth) {
+
				/* Try again with authentication. */
+
				clean_http_headerbuf(&headerbuf);
+
				fetch_close(conn);
+
				isproxyauth = true;
+
				goto retry;
+
			}
+
			goto ouch;
+
		}
+
		/* Read and discard the rest of the proxy response */
+
		if (fetch_getln(conn) < 0) {
+
			fetch_syserr();
+
			goto ouch;
+
		}
+
		do {
+
			switch ((h = http_next_header(conn, &headerbuf, &p))) {
+
			case hdr_syserror:
+
				fetch_syserr();
+
				goto ouch;
+
			case hdr_error:
+
				http_seterr(HTTP_PROTOCOL_ERROR);
+
				goto ouch;
+
			default:
+
				/* ignore */ ;
+
			}
+
		} while (h > hdr_end);
+
	}
+
	if (strcmp(URL->scheme, SCHEME_HTTPS) == 0 &&
+
	    fetch_ssl(conn, URL, verbose) == -1) {
+
		/* grrr */
+
		errno = EAUTH;
+
		fetch_syserr();
+
		goto ouch;
+
	}
+

+
	val = 1;
+
#ifdef TCP_NOPUSH
+
	setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
+
#endif
+

+
	clean_http_headerbuf(&headerbuf);
+
	return (conn);
+
ouch:
+
	serrno = errno;
+
	clean_http_headerbuf(&headerbuf);
+
	fetch_close(conn);
+
	errno = serrno;
+
	return (NULL);
+
}
+

+
static struct url *
+
http_get_proxy(struct url * url, const char *flags)
+
{
+
	struct url *purl;
+
	char *p;
+

+
	if (flags != NULL && strchr(flags, 'd') != NULL)
+
		return (NULL);
+
	if (fetch_no_proxy_match(url->host))
+
		return (NULL);
+
	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
+
	    *p && (purl = fetchParseURL(p))) {
+
		if (!*purl->scheme)
+
			strcpy(purl->scheme, SCHEME_HTTP);
+
		if (!purl->port)
+
			purl->port = fetch_default_proxy_port(purl->scheme);
+
		if (strcmp(purl->scheme, SCHEME_HTTP) == 0)
+
			return (purl);
+
		fetchFreeURL(purl);
+
	}
+
	return (NULL);
+
}
+

+
static void
+
http_print_html(FILE *out, FILE *in)
+
{
+
	ssize_t len = 0;
+
	size_t cap;
+
	char *line = NULL, *p, *q;
+
	int comment, tag;
+

+
	comment = tag = 0;
+
	while ((len = getline(&line, &cap, in)) >= 0) {
+
		while (len && isspace((unsigned char)line[len - 1]))
+
			--len;
+
		for (p = q = line; q < line + len; ++q) {
+
			if (comment && *q == '-') {
+
				if (q + 2 < line + len &&
+
				    strcmp(q, "-->") == 0) {
+
					tag = comment = 0;
+
					q += 2;
+
				}
+
			} else if (tag && !comment && *q == '>') {
+
				p = q + 1;
+
				tag = 0;
+
			} else if (!tag && *q == '<') {
+
				if (q > p)
+
					fwrite(p, q - p, 1, out);
+
				tag = 1;
+
				if (q + 3 < line + len &&
+
				    strcmp(q, "<!--") == 0) {
+
					comment = 1;
+
					q += 3;
+
				}
+
			}
+
		}
+
		if (!tag && q > p)
+
			fwrite(p, q - p, 1, out);
+
		fputc('\n', out);
+
	}
+

+
	free(line);
+
}
+

+

+
/*****************************************************************************
+
 * Core
+
 */
+

+
FILE *
+
http_request(struct url *URL, const char *op, struct url_stat *us,
+
	struct url *purl, const char *flags)
+
{
+

+
	return (http_request_body(URL, op, us, purl, flags, NULL, NULL));
+
}
+

+
/*
+
 * Send a request and process the reply
+
 *
+
 * XXX This function is way too long, the do..while loop should be split
+
 * XXX off into a separate function.
+
 */
+
FILE *
+
http_request_body(struct url *URL, const char *op, struct url_stat *us,
+
	struct url *purl, const char *flags, const char *content_type,
+
	const char *body)
+
{
+
	char timebuf[80];
+
	char hbuf[MAXHOSTNAMELEN + 7], *host;
+
	conn_t *conn;
+
	struct url *url, *new;
+
	int chunked, conn_close, direct, from_cache, ims, noredirect, verbose;
+
	int e, i, n, val;
+
	off_t offset, clength, length, size;
+
	time_t mtime;
+
	const char *p;
+
	FILE *f;
+
	hdr_t h;
+
	struct tm *timestruct;
+
	http_headerbuf_t headerbuf;
+
	http_auth_challenges_t server_challenges;
+
	http_auth_challenges_t proxy_challenges;
+
	size_t body_len;
+

+
	/* The following calls don't allocate anything */
+
	init_http_headerbuf(&headerbuf);
+
	init_http_auth_challenges(&server_challenges);
+
	init_http_auth_challenges(&proxy_challenges);
+

+
	direct = CHECK_FLAG('d');
+
	noredirect = CHECK_FLAG('A');
+
	verbose = CHECK_FLAG('v');
+
	ims = CHECK_FLAG('i');
+

+
	if (direct && purl) {
+
		fetchFreeURL(purl);
+
		purl = NULL;
+
	}
+

+
	/* try the provided URL first */
+
	url = URL;
+

+
	n = MAX_REDIRECT;
+
	i = 0;
+

+
	e = HTTP_PROTOCOL_ERROR;
+
	do {
+
		new = NULL;
+
		chunked = 0;
+
		conn_close = 0;
+
		offset = 0;
+
		clength = -1;
+
		length = -1;
+
		size = -1;
+
		mtime = 0;
+

+
		/* check port */
+
		if (!url->port)
+
			url->port = fetch_default_port(url->scheme);
+

+
		/* connect to server or proxy */
+
		from_cache = (http_cached_conn != NULL);
+
		if ((conn = http_connect(url, purl, flags)) == NULL)
+
			goto ouch;
+

+
		/* append port number only if necessary */
+
		host = url->host;
+
		if (url->port != fetch_default_port(url->scheme)) {
+
			snprintf(hbuf, sizeof(hbuf), "%s:%d", host, url->port);
+
			host = hbuf;
+
		}
+

+
		/* send request */
+
		if (verbose)
+
			fetch_info("requesting %s://%s%s",
+
			    url->scheme, host, url->doc);
+
		if (purl && strcmp(url->scheme, SCHEME_HTTPS) != 0) {
+
			http_cmd(conn, "%s %s://%s%s HTTP/1.1",
+
			    op, url->scheme, host, url->doc);
+
		} else {
+
			http_cmd(conn, "%s %s HTTP/1.1",
+
			    op, url->doc);
+
		}
+

+
		if (ims && url->ims_time) {
+
			timestruct = gmtime((time_t *)&url->ims_time);
+
			(void)strftime(timebuf, 80, "%a, %d %b %Y %T GMT",
+
			    timestruct);
+
			if (verbose)
+
				fetch_info("If-Modified-Since: %s", timebuf);
+
			http_cmd(conn, "If-Modified-Since: %s", timebuf);
+
		}
+
		/* virtual host */
+
		http_cmd(conn, "Host: %s", host);
+

+
		/*
+
		 * Proxy authorization: we only send auth after we received
+
		 * a 407 error. We do not first try basic anyway (changed
+
		 * when support was added for digest-auth)
+
		 */
+
		if (purl && proxy_challenges.valid) {
+
			http_auth_params_t aparams;
+
			init_http_auth_params(&aparams);
+
			if (*purl->user || *purl->pwd) {
+
				aparams.user = strdup(purl->user);
+
				aparams.password = strdup(purl->pwd);
+
			} else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL &&
+
				   *p != '\0') {
+
				if (http_authfromenv(p, &aparams) < 0) {
+
					http_seterr(HTTP_NEED_PROXY_AUTH);
+
					goto ouch;
+
				}
+
			} else if (fetch_netrc_auth(purl) == 0) {
+
				aparams.user = strdup(purl->user);
+
				aparams.password = strdup(purl->pwd);
+
			}
+
			http_authorize(conn, "Proxy-Authorization",
+
				       &proxy_challenges, &aparams, url);
+
			clean_http_auth_params(&aparams);
+
		}
+

+
		/*
+
		 * Server authorization: we never send "a priori"
+
		 * Basic auth, which used to be done if user/pass were
+
		 * set in the url. This would be weird because we'd send the
+
		 * password in the clear even if Digest is finally to be
+
		 * used (it would have made more sense for the
+
		 * pre-digest version to do this when Basic was specified
+
		 * in the environment)
+
		 */
+
		if (server_challenges.valid) {
+
			http_auth_params_t aparams;
+
			init_http_auth_params(&aparams);
+
			if (*url->user || *url->pwd) {
+
				aparams.user = strdup(url->user);
+
				aparams.password = strdup(url->pwd);
+
			} else if ((p = getenv("HTTP_AUTH")) != NULL &&
+
				   *p != '\0') {
+
				if (http_authfromenv(p, &aparams) < 0) {
+
					http_seterr(HTTP_NEED_AUTH);
+
					goto ouch;
+
				}
+
			} else if (fetch_netrc_auth(url) == 0) {
+
				aparams.user = strdup(url->user);
+
				aparams.password = strdup(url->pwd);
+
			} else if (fetchAuthMethod &&
+
				   fetchAuthMethod(url) == 0) {
+
				aparams.user = strdup(url->user);
+
				aparams.password = strdup(url->pwd);
+
			} else {
+
				http_seterr(HTTP_NEED_AUTH);
+
				goto ouch;
+
			}
+
			http_authorize(conn, "Authorization",
+
				       &server_challenges, &aparams, url);
+
			clean_http_auth_params(&aparams);
+
		}
+

+
		/* other headers */
+
		if ((p = getenv("HTTP_ACCEPT")) != NULL) {
+
			if (*p != '\0')
+
				http_cmd(conn, "Accept: %s", p);
+
		} else {
+
			http_cmd(conn, "Accept: */*");
+
		}
+
		if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') {
+
			if (strcasecmp(p, "auto") == 0)
+
				http_cmd(conn, "Referer: %s://%s%s",
+
				    url->scheme, host, url->doc);
+
			else
+
				http_cmd(conn, "Referer: %s", p);
+
		}
+
		if ((p = getenv("HTTP_USER_AGENT")) != NULL) {
+
			/* no User-Agent if defined but empty */
+
			if  (*p != '\0')
+
				http_cmd(conn, "User-Agent: %s", p);
+
		} else {
+
			/* default User-Agent */
+
			http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER,
+
			    getprogname());
+
		}
+
		if (url->offset > 0)
+
			http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
+

+
		/* custom headers from the API and environment */
+
		if (fetchCustomHTTPHeaders != NULL)
+
			http_custom_headers(conn, fetchCustomHTTPHeaders);
+
		if ((p = getenv("HTTP_HEADERS")) != NULL && *p != '\0')
+
			http_custom_headers(conn, p);
+

+
		if (body) {
+
			body_len = strlen(body);
+
			http_cmd(conn, "Content-Length: %zu", body_len);
+
			if (content_type != NULL)
+
				http_cmd(conn, "Content-Type: %s", content_type);
+
		}
+

+
		http_cmd(conn, "");
+

+
		if (body)
+
			fetch_write(conn, body, body_len);
+

+
		/*
+
		 * Force the queued request to be dispatched.  Normally, one
+
		 * would do this with shutdown(2) but squid proxies can be
+
		 * configured to disallow such half-closed connections.  To
+
		 * be compatible with such configurations, fiddle with socket
+
		 * options to force the pending data to be written.
+
		 */
+
		val = 0;
+
#ifdef TCP_NOPUSH
+
		setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
+
			   sizeof(val));
+
#endif
+
		val = 1;
+
		setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
+
			   sizeof(val));
+

+
		/* get reply */
+
		switch (http_get_reply(conn)) {
+
		case HTTP_OK:
+
		case HTTP_PARTIAL:
+
		case HTTP_NOT_MODIFIED:
+
			/* fine */
+
			break;
+
		case HTTP_MOVED_PERM:
+
		case HTTP_MOVED_TEMP:
+
		case HTTP_TEMP_REDIRECT:
+
		case HTTP_PERM_REDIRECT:
+
		case HTTP_SEE_OTHER:
+
		case HTTP_USE_PROXY:
+
			/*
+
			 * Not so fine, but we still have to read the
+
			 * headers to get the new location.
+
			 */
+
			break;
+
		case HTTP_NEED_AUTH:
+
			if (server_challenges.valid) {
+
				/*
+
				 * We already sent out authorization code,
+
				 * so there's nothing more we can do.
+
				 */
+
				http_seterr(conn->err);
+
				goto ouch;
+
			}
+
			/* try again, but send the password this time */
+
			if (verbose)
+
				fetch_info("server requires authorization");
+
			break;
+
		case HTTP_NEED_PROXY_AUTH:
+
			if (proxy_challenges.valid) {
+
				/*
+
				 * We already sent our proxy
+
				 * authorization code, so there's
+
				 * nothing more we can do. */
+
				http_seterr(conn->err);
+
				goto ouch;
+
			}
+
			/* try again, but send the password this time */
+
			if (verbose)
+
				fetch_info("proxy requires authorization");
+
			break;
+
		case HTTP_BAD_RANGE:
+
			/*
+
			 * This can happen if we ask for 0 bytes because
+
			 * we already have the whole file.  Consider this
+
			 * a success for now, and check sizes later.
+
			 */
+
			break;
+
		case HTTP_PROTOCOL_ERROR:
+
			/* fall through */
+
		case -1:
+
			/*
+
			 * If we got this connection from the cache, the
+
			 * server may have closed it while idle.  Retry
+
			 * once with a fresh connection.
+
			 */
+
			if (from_cache) {
+
				if (verbose)
+
					fetch_info("stale cached connection, "
+
					    "retrying");
+
				fetch_close(conn);
+
				conn = NULL;
+
				from_cache = 0;
+
				continue;
+
			}
+
			fetch_syserr();
+
			goto ouch;
+
		default:
+
			http_seterr(conn->err);
+
			if (!verbose)
+
				goto ouch;
+
			/* fall through so we can get the full error message */
+
		}
+

+
		/* get headers. http_next_header expects one line readahead */
+
		if (fetch_getln(conn) == -1) {
+
			fetch_syserr();
+
			goto ouch;
+
		}
+
		do {
+
			switch ((h = http_next_header(conn, &headerbuf, &p))) {
+
			case hdr_syserror:
+
				fetch_syserr();
+
				goto ouch;
+
			case hdr_error:
+
				http_seterr(HTTP_PROTOCOL_ERROR);
+
				goto ouch;
+
			case hdr_content_length:
+
				http_parse_length(p, &clength);
+
				break;
+
			case hdr_content_range:
+
				http_parse_range(p, &offset, &length, &size);
+
				break;
+
			case hdr_last_modified:
+
				http_parse_mtime(p, &mtime);
+
				break;
+
			case hdr_location:
+
				if (!HTTP_REDIRECT(conn->err))
+
					break;
+
				/*
+
				 * if the A flag is set, we don't follow
+
				 * temporary redirects.
+
				 */
+
				if (noredirect &&
+
				    conn->err != HTTP_MOVED_PERM &&
+
				    conn->err != HTTP_PERM_REDIRECT &&
+
				    conn->err != HTTP_USE_PROXY) {
+
					n = 1;
+
					break;
+
				}
+
				if (new)
+
					free(new);
+
				if (verbose)
+
					fetch_info("%d redirect to %s",
+
					    conn->err, p);
+
				if (*p == '/')
+
					/* absolute path */
+
					new = fetchMakeURL(url->scheme, url->host,
+
					    url->port, p, url->user, url->pwd);
+
				else
+
					new = fetchParseURL(p);
+
				if (new == NULL) {
+
					/* XXX should set an error code */
+
					DEBUGF("failed to parse new URL\n");
+
					goto ouch;
+
				}
+

+
				/* Only copy credentials if the host matches */
+
				if (strcmp(new->host, url->host) == 0 &&
+
				    !*new->user && !*new->pwd) {
+
					strcpy(new->user, url->user);
+
					strcpy(new->pwd, url->pwd);
+
				}
+
				new->offset = url->offset;
+
				new->length = url->length;
+
				new->ims_time = url->ims_time;
+
				break;
+
			case hdr_transfer_encoding:
+
				/* XXX weak test*/
+
				chunked = (strcasecmp(p, "chunked") == 0);
+
				break;
+
			case hdr_www_authenticate:
+
				if (conn->err != HTTP_NEED_AUTH)
+
					break;
+
				if (http_parse_authenticate(p, &server_challenges) == 0)
+
					++n;
+
				break;
+
			case hdr_proxy_authenticate:
+
				if (conn->err != HTTP_NEED_PROXY_AUTH)
+
					break;
+
				if (http_parse_authenticate(p, &proxy_challenges) == 0)
+
					++n;
+
				break;
+
			case hdr_connection:
+
				if (strcasecmp(p, "close") == 0)
+
					conn_close = 1;
+
				break;
+
			case hdr_end:
+
				/* fall through */
+
			case hdr_unknown:
+
				/* ignore */
+
				break;
+
			}
+
		} while (h > hdr_end);
+

+
		/* we need to provide authentication */
+
		if (conn->err == HTTP_NEED_AUTH ||
+
		    conn->err == HTTP_NEED_PROXY_AUTH) {
+
			e = conn->err;
+
			if ((conn->err == HTTP_NEED_AUTH &&
+
			     !server_challenges.valid) ||
+
			    (conn->err == HTTP_NEED_PROXY_AUTH &&
+
			     !proxy_challenges.valid)) {
+
				/* 401/7 but no www/proxy-authenticate ?? */
+
				DEBUGF("%03d without auth header\n", conn->err);
+
				goto ouch;
+
			}
+
			fetch_close(conn);
+
			conn = NULL;
+
			continue;
+
		}
+

+
		/* requested range not satisfiable */
+
		if (conn->err == HTTP_BAD_RANGE) {
+
			if (url->offset > 0 && url->length == 0) {
+
				/* asked for 0 bytes; fake it */
+
				offset = url->offset;
+
				clength = -1;
+
				conn->err = HTTP_OK;
+
				break;
+
			} else {
+
				http_seterr(conn->err);
+
				goto ouch;
+
			}
+
		}
+

+
		/* we have a hit or an error */
+
		if (conn->err == HTTP_OK
+
		    || conn->err == HTTP_NOT_MODIFIED
+
		    || conn->err == HTTP_PARTIAL
+
		    || HTTP_ERROR(conn->err))
+
			break;
+

+
		/* all other cases: we got a redirect */
+
		e = conn->err;
+
		clean_http_auth_challenges(&server_challenges);
+
		fetch_close(conn);
+
		conn = NULL;
+
		if (!new) {
+
			DEBUGF("redirect with no new location\n");
+
			break;
+
		}
+
		if (url != URL)
+
			fetchFreeURL(url);
+
		url = new;
+
	} while (++i < n);
+

+
	/* we failed, or ran out of retries */
+
	if (conn == NULL) {
+
		http_seterr(e);
+
		goto ouch;
+
	}
+

+
	DEBUGF("offset %lld, length %lld, size %lld, clength %lld\n",
+
	    (long long)offset, (long long)length,
+
	    (long long)size, (long long)clength);
+

+
	if (conn->err == HTTP_NOT_MODIFIED) {
+
		http_seterr(HTTP_NOT_MODIFIED);
+
		return (NULL);
+
	}
+

+
	/* check for inconsistencies */
+
	if (clength != -1 && length != -1 && clength != length) {
+
		http_seterr(HTTP_PROTOCOL_ERROR);
+
		goto ouch;
+
	}
+
	if (clength == -1)
+
		clength = length;
+
	if (clength != -1)
+
		length = offset + clength;
+
	if (length != -1 && size != -1 && length != size) {
+
		http_seterr(HTTP_PROTOCOL_ERROR);
+
		goto ouch;
+
	}
+
	if (size == -1)
+
		size = length;
+

+
	/* fill in stats */
+
	if (us) {
+
		us->size = size;
+
		us->atime = us->mtime = mtime;
+
	}
+

+
	/* too far? */
+
	if (URL->offset > 0 && offset > URL->offset) {
+
		http_seterr(HTTP_PROTOCOL_ERROR);
+
		goto ouch;
+
	}
+

+
	/* report back real offset and size */
+
	URL->offset = offset;
+
	URL->length = clength;
+

+
	/* wrap it up in a FILE */
+
	if ((f = http_funopen(conn, chunked, !conn_close && purl == NULL, clength)) == NULL) {
+
		fetch_syserr();
+
		goto ouch;
+
	}
+

+
	if (url != URL)
+
		fetchFreeURL(url);
+
	if (purl)
+
		fetchFreeURL(purl);
+

+
	if (HTTP_ERROR(conn->err)) {
+
		http_print_html(stderr, f);
+
		fclose(f);
+
		f = NULL;
+
	}
+
	clean_http_headerbuf(&headerbuf);
+
	clean_http_auth_challenges(&server_challenges);
+
	clean_http_auth_challenges(&proxy_challenges);
+
	return (f);
+

+
ouch:
+
	if (url != URL)
+
		fetchFreeURL(url);
+
	if (purl)
+
		fetchFreeURL(purl);
+
	if (conn != NULL)
+
		fetch_close(conn);
+
	clean_http_headerbuf(&headerbuf);
+
	clean_http_auth_challenges(&server_challenges);
+
	clean_http_auth_challenges(&proxy_challenges);
+
	return (NULL);
+
}
+

+

+
/*****************************************************************************
+
 * Entry points
+
 */
+

+
/*
+
 * Retrieve and stat a file by HTTP
+
 */
+
FILE *
+
fetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
+
{
+
	return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags));
+
}
+

+
/*
+
 * Retrieve a file by HTTP
+
 */
+
FILE *
+
fetchGetHTTP(struct url *URL, const char *flags)
+
{
+
	return (fetchXGetHTTP(URL, NULL, flags));
+
}
+

+
/*
+
 * Store a file by HTTP
+
 */
+
FILE *
+
fetchPutHTTP(struct url *URL __unused, const char *flags __unused)
+
{
+
	warnx("fetchPutHTTP(): not implemented");
+
	return (NULL);
+
}
+

+
/*
+
 * Get an HTTP document's metadata
+
 */
+
int
+
fetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
+
{
+
	FILE *f;
+

+
	f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags);
+
	if (f == NULL)
+
		return (-1);
+
	fclose(f);
+
	return (0);
+
}
+

+
/*
+
 * List a directory
+
 */
+
struct url_ent *
+
fetchListHTTP(struct url *url __unused, const char *flags __unused)
+
{
+
	warnx("fetchListHTTP(): not implemented");
+
	return (NULL);
+
}
+

+
/*
+
 * Arbitrary HTTP verb and content requests
+
 */
+
FILE *
+
fetchReqHTTP(struct url *URL, const char *method, const char *flags,
+
	const char *content_type, const char *body)
+
{
+

+
	return (http_request_body(URL, method, NULL, http_get_proxy(URL, flags),
+
	    flags, content_type, body));
+
}
added external/fetch/http.errors
@@ -0,0 +1,45 @@
+
#
+
# This list is taken from RFC 2068.
+
#
+
100 OK		Continue
+
101 OK		Switching Protocols
+
200 OK		OK
+
201 OK		Created
+
202 OK		Accepted
+
203 INFO	Non-Authoritative Information
+
204 OK		No Content
+
205 OK		Reset Content
+
206 OK		Partial Content
+
300 MOVED	Multiple Choices
+
301 MOVED	Moved Permanently
+
302 MOVED	Moved Temporarily
+
303 MOVED	See Other
+
304 OK		Not Modified
+
305 INFO	Use Proxy
+
307 MOVED	Temporary Redirect
+
308 MOVED	Permanent Redirect
+
400 PROTO	Bad Request
+
401 AUTH	Unauthorized
+
402 AUTH	Payment Required
+
403 AUTH	Forbidden
+
404 UNAVAIL	Not Found
+
405 PROTO	Method Not Allowed
+
406 PROTO	Not Acceptable
+
407 AUTH	Proxy Authentication Required
+
408 TIMEOUT	Request Time-out
+
409 EXISTS	Conflict
+
410 UNAVAIL	Gone
+
411 PROTO	Length Required
+
412 SERVER	Precondition Failed
+
413 PROTO	Request Entity Too Large
+
414 PROTO	Request-URI Too Large
+
415 PROTO	Unsupported Media Type
+
416 UNAVAIL	Requested Range Not Satisfiable
+
417 SERVER	Expectation Failed
+
500 SERVER	Internal Server Error
+
501 PROTO	Not Implemented
+
502 SERVER	Bad Gateway
+
503 TEMP	Service Unavailable
+
504 TIMEOUT	Gateway Time-out
+
505 PROTO	HTTP Version not supported
+
999 PROTO	Protocol error
added external/fetch/httperr.h
@@ -0,0 +1,45 @@
+
static struct fetcherr http_errlist[] = {
+
    { 100, FETCH_OK, "Continue" },
+
    { 101, FETCH_OK, "Switching Protocols" },
+
    { 200, FETCH_OK, "OK" },
+
    { 201, FETCH_OK, "Created" },
+
    { 202, FETCH_OK, "Accepted" },
+
    { 203, FETCH_INFO, "Non-Authoritative Information" },
+
    { 204, FETCH_OK, "No Content" },
+
    { 205, FETCH_OK, "Reset Content" },
+
    { 206, FETCH_OK, "Partial Content" },
+
    { 300, FETCH_MOVED, "Multiple Choices" },
+
    { 301, FETCH_MOVED, "Moved Permanently" },
+
    { 302, FETCH_MOVED, "Moved Temporarily" },
+
    { 303, FETCH_MOVED, "See Other" },
+
    { 304, FETCH_OK, "Not Modified" },
+
    { 305, FETCH_INFO, "Use Proxy" },
+
    { 307, FETCH_MOVED, "Temporary Redirect" },
+
    { 308, FETCH_MOVED, "Permanent Redirect" },
+
    { 400, FETCH_PROTO, "Bad Request" },
+
    { 401, FETCH_AUTH, "Unauthorized" },
+
    { 402, FETCH_AUTH, "Payment Required" },
+
    { 403, FETCH_AUTH, "Forbidden" },
+
    { 404, FETCH_UNAVAIL, "Not Found" },
+
    { 405, FETCH_PROTO, "Method Not Allowed" },
+
    { 406, FETCH_PROTO, "Not Acceptable" },
+
    { 407, FETCH_AUTH, "Proxy Authentication Required" },
+
    { 408, FETCH_TIMEOUT, "Request Time-out" },
+
    { 409, FETCH_EXISTS, "Conflict" },
+
    { 410, FETCH_UNAVAIL, "Gone" },
+
    { 411, FETCH_PROTO, "Length Required" },
+
    { 412, FETCH_SERVER, "Precondition Failed" },
+
    { 413, FETCH_PROTO, "Request Entity Too Large" },
+
    { 414, FETCH_PROTO, "Request-URI Too Large" },
+
    { 415, FETCH_PROTO, "Unsupported Media Type" },
+
    { 416, FETCH_UNAVAIL, "Requested Range Not Satisfiable" },
+
    { 417, FETCH_SERVER, "Expectation Failed" },
+
    { 500, FETCH_SERVER, "Internal Server Error" },
+
    { 501, FETCH_PROTO, "Not Implemented" },
+
    { 502, FETCH_SERVER, "Bad Gateway" },
+
    { 503, FETCH_TEMP, "Service Unavailable" },
+
    { 504, FETCH_TIMEOUT, "Gateway Time-out" },
+
    { 505, FETCH_PROTO, "HTTP Version not supported" },
+
    { 999, FETCH_PROTO, "Protocol error" },
+
    { -1, FETCH_UNKNOWN, "Unknown HTTP error" }
+
};
added external/libblake2/Makefile.in
@@ -0,0 +1,8 @@
+
include @builddir@/mk/defs.mk
+
LIB=	blake2
+
SRCS=	blake2b-ref.c blake2s-ref.c
+
LOCAL_CFLAGS=	-I$(top_srcdir)/compat -O3
+

+
VPATH=	$(top_srcdir)/external/blake2
+

+
include $(MK)/static-lib.mk
deleted external/libder/Makefile.autosetup
@@ -1,15 +0,0 @@
-
include @builddir@/mk/defs.mk
-

-
LIB=	der
-

-
VPATH=	$(top_srcdir)/external/libder/libder
-
.PATH:	$(top_srcdir)/external/libder/libder
-

-
SRCS=	libder.c \
-
	libder_error.c \
-
	libder_obj.c \
-
	libder_read.c \
-
	libder_type.c \
-
	libder_write.c
-

-
include $(MK)/static-lib.mk
added external/libder/Makefile.in
@@ -0,0 +1,15 @@
+
include @builddir@/mk/defs.mk
+

+
LIB=	der
+

+
VPATH=	$(top_srcdir)/external/libder/libder
+
.PATH:	$(top_srcdir)/external/libder/libder
+

+
SRCS=	libder.c \
+
	libder_error.c \
+
	libder_obj.c \
+
	libder_read.c \
+
	libder_type.c \
+
	libder_write.c
+

+
include $(MK)/static-lib.mk
deleted external/libecc/Makefile.autosetup
@@ -1,147 +0,0 @@
-
include @builddir@/mk/defs.mk
-

-
LIB=	ecc
-

-
.PATH: $(top_srcdir)/external/libecc
-
VPATH+= $(top_srcdir)/external/libecc
-
SRCS+=	pkg_libecc_rand.c
-

-
# curves_mod_src
-
.PATH:	$(top_srcdir)/external/libecc/src/curves
-
VPATH+=	$(top_srcdir)/external/libecc/src/curves
-
SRCS+=	aff_pt.c \
-
	aff_pt_montgomery.c \
-
	ec_edwards.c \
-
	ec_montgomery.c \
-
	ec_params.c \
-
	ec_shortw.c \
-
	aff_pt_edwards.c \
-
	curves.c \
-
	prj_pt.c
-

-
# utils_ec_src
-
.PATH:	$(top_srcdir)/external/libecc/src/utils
-
VPATH+=	$(top_srcdir)/external/libecc/src/utils
-
SRCS+=	print_curves.c
-

-
# fp_mod_src
-
.PATH:	$(top_srcdir)/external/libecc/src/fp
-
VPATH+=	$(top_srcdir)/external/libecc/src/fp
-
SRCS+=	fp_add.c \
-
	fp.c \
-
	fp_montgomery.c \
-
	fp_mul.c \
-
	fp_mul_redc1.c \
-
	fp_pow.c \
-
	fp_rand.c \
-
	fp_sqrt.c
-

-
# nn_mod_src
-
.PATH:	$(top_srcdir)/external/libecc/src/nn
-
VPATH+=	$(top_srcdir)/external/libecc/src/nn
-
SRCS+=	nn_add.c \
-
	nn.c \
-
	nn_div.c \
-
	nn_logical.c \
-
	nn_modinv.c \
-
	nn_mod_pow.c \
-
	nn_mul.c \
-
	nn_mul_redc1.c \
-
	nn_rand.c
-

-
# utils_arith_src
-
SRCS+=	utils.c \
-
	utils_rand.c \
-
	print_buf.c \
-
	print_fp.c \
-
	print_nn.c
-

-
## libsign bits
-
# hash_mod_src
-
.PATH:	$(top_srcdir)/external/libecc/src/hash
-
VPATH+=	$(top_srcdir)/external/libecc/src/hash
-
SRCS+=	hash_algs.c \
-
	sm3.c \
-
	streebog.c \
-
	ripemd160.c \
-
	belt-hash.c \
-
	hmac.c \
-
	bash224.c \
-
	bash256.c \
-
	bash384.c \
-
	bash512.c \
-
	bash.c \
-
	sha224.c \
-
	sha256.c \
-
	sha3-224.c \
-
	sha3-256.c \
-
	sha3-384.c \
-
	sha3-512.c \
-
	sha384.c \
-
	sha3.c \
-
	sha512-224.c \
-
	sha512-256.c \
-
	sha512.c \
-
	sha512_core.c \
-
	shake256.c \
-
	shake.c
-

-
# sig_mod_src
-
.PATH:	$(top_srcdir)/external/libecc/src/sig
-
VPATH+=	$(top_srcdir)/external/libecc/src/sig
-
SRCS+=	decdsa.c \
-
	ecdsa.c \
-
	ecfsdsa.c \
-
	ecgdsa.c \
-
	eckcdsa.c \
-
	ecosdsa.c \
-
	ecrdsa.c \
-
	ecsdsa.c \
-
	eddsa.c \
-
	fuzzing_ecdsa.c \
-
	fuzzing_ecgdsa.c \
-
	fuzzing_ecrdsa.c \
-
	ecdsa_common.c \
-
	ecsdsa_common.c \
-
	sig_algs.c \
-
	sm2.c \
-
	bign_common.c \
-
	bign.c \
-
	dbign.c \
-
	bip0340.c
-

-
# key_mod_src
-
SRCS+=	ec_key.c
-

-
# utils_sign_src
-
.PATH:	$(top_srcdir)/external/libecc/src/sig
-
VPATH+=	$(top_srcdir)/external/libecc/src/sig
-
SRCS+=	print_keys.c
-

-
# ecdh_mod_src
-
.PATH:	$(top_srcdir)/external/libecc/src/ecdh
-
VPATH+=	$(top_srcdir)/external/libecc/src/ecdh
-
SRCS+=	ecccdh.c \
-
	x25519_448.c
-

-
# external_deps
-
.PATH:	$(top_srcdir)/external/libecc/src/external_deps
-
VPATH+=	$(top_srcdir)/external/libecc/src/external_deps
-
SRCS+=	print.c
-

-
CONFLICTS=	-Dsha256_init=_libecc_sha256_init \
-
		-Dsha256_update=_libecc_sha256_update \
-
		-Dsha256_final=_libecc_sha256_final \
-
		-Dsha512_224_init=_libecc_sha512_224_init \
-
		-Dsha512_256_init=_libecc_sha512_256_init
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/external/libecc/include \
-
		-I$(top_srcdir)/compat \
-
		-I$(top_builddir)/ \
-
		-fno-builtin \
-
		-fstack-protector-strong \
-
		-DUSE_WARN_UNUSED_RET \
-
		-DWITH_STDLIB \
-
		$(CONFLICTS)
-

-
include $(MK)/static-lib.mk
added external/libecc/Makefile.in
@@ -0,0 +1,147 @@
+
include @builddir@/mk/defs.mk
+

+
LIB=	ecc
+

+
.PATH: $(top_srcdir)/external/libecc
+
VPATH+= $(top_srcdir)/external/libecc
+
SRCS+=	pkg_libecc_rand.c
+

+
# curves_mod_src
+
.PATH:	$(top_srcdir)/external/libecc/src/curves
+
VPATH+=	$(top_srcdir)/external/libecc/src/curves
+
SRCS+=	aff_pt.c \
+
	aff_pt_montgomery.c \
+
	ec_edwards.c \
+
	ec_montgomery.c \
+
	ec_params.c \
+
	ec_shortw.c \
+
	aff_pt_edwards.c \
+
	curves.c \
+
	prj_pt.c
+

+
# utils_ec_src
+
.PATH:	$(top_srcdir)/external/libecc/src/utils
+
VPATH+=	$(top_srcdir)/external/libecc/src/utils
+
SRCS+=	print_curves.c
+

+
# fp_mod_src
+
.PATH:	$(top_srcdir)/external/libecc/src/fp
+
VPATH+=	$(top_srcdir)/external/libecc/src/fp
+
SRCS+=	fp_add.c \
+
	fp.c \
+
	fp_montgomery.c \
+
	fp_mul.c \
+
	fp_mul_redc1.c \
+
	fp_pow.c \
+
	fp_rand.c \
+
	fp_sqrt.c
+

+
# nn_mod_src
+
.PATH:	$(top_srcdir)/external/libecc/src/nn
+
VPATH+=	$(top_srcdir)/external/libecc/src/nn
+
SRCS+=	nn_add.c \
+
	nn.c \
+
	nn_div.c \
+
	nn_logical.c \
+
	nn_modinv.c \
+
	nn_mod_pow.c \
+
	nn_mul.c \
+
	nn_mul_redc1.c \
+
	nn_rand.c
+

+
# utils_arith_src
+
SRCS+=	utils.c \
+
	utils_rand.c \
+
	print_buf.c \
+
	print_fp.c \
+
	print_nn.c
+

+
## libsign bits
+
# hash_mod_src
+
.PATH:	$(top_srcdir)/external/libecc/src/hash
+
VPATH+=	$(top_srcdir)/external/libecc/src/hash
+
SRCS+=	hash_algs.c \
+
	sm3.c \
+
	streebog.c \
+
	ripemd160.c \
+
	belt-hash.c \
+
	hmac.c \
+
	bash224.c \
+
	bash256.c \
+
	bash384.c \
+
	bash512.c \
+
	bash.c \
+
	sha224.c \
+
	sha256.c \
+
	sha3-224.c \
+
	sha3-256.c \
+
	sha3-384.c \
+
	sha3-512.c \
+
	sha384.c \
+
	sha3.c \
+
	sha512-224.c \
+
	sha512-256.c \
+
	sha512.c \
+
	sha512_core.c \
+
	shake256.c \
+
	shake.c
+

+
# sig_mod_src
+
.PATH:	$(top_srcdir)/external/libecc/src/sig
+
VPATH+=	$(top_srcdir)/external/libecc/src/sig
+
SRCS+=	decdsa.c \
+
	ecdsa.c \
+
	ecfsdsa.c \
+
	ecgdsa.c \
+
	eckcdsa.c \
+
	ecosdsa.c \
+
	ecrdsa.c \
+
	ecsdsa.c \
+
	eddsa.c \
+
	fuzzing_ecdsa.c \
+
	fuzzing_ecgdsa.c \
+
	fuzzing_ecrdsa.c \
+
	ecdsa_common.c \
+
	ecsdsa_common.c \
+
	sig_algs.c \
+
	sm2.c \
+
	bign_common.c \
+
	bign.c \
+
	dbign.c \
+
	bip0340.c
+

+
# key_mod_src
+
SRCS+=	ec_key.c
+

+
# utils_sign_src
+
.PATH:	$(top_srcdir)/external/libecc/src/sig
+
VPATH+=	$(top_srcdir)/external/libecc/src/sig
+
SRCS+=	print_keys.c
+

+
# ecdh_mod_src
+
.PATH:	$(top_srcdir)/external/libecc/src/ecdh
+
VPATH+=	$(top_srcdir)/external/libecc/src/ecdh
+
SRCS+=	ecccdh.c \
+
	x25519_448.c
+

+
# external_deps
+
.PATH:	$(top_srcdir)/external/libecc/src/external_deps
+
VPATH+=	$(top_srcdir)/external/libecc/src/external_deps
+
SRCS+=	print.c
+

+
CONFLICTS=	-Dsha256_init=_libecc_sha256_init \
+
		-Dsha256_update=_libecc_sha256_update \
+
		-Dsha256_final=_libecc_sha256_final \
+
		-Dsha512_224_init=_libecc_sha512_224_init \
+
		-Dsha512_256_init=_libecc_sha512_256_init
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/external/libecc/include \
+
		-I$(top_srcdir)/compat \
+
		-I$(top_builddir)/ \
+
		-fno-builtin \
+
		-fstack-protector-strong \
+
		-DUSE_WARN_UNUSED_RET \
+
		-DWITH_STDLIB \
+
		$(CONFLICTS)
+

+
include $(MK)/static-lib.mk
deleted external/libelf/Makefile.autosetup
@@ -1,76 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	elf
-
SRCS=	elf_begin.c \
-
	elf_cntl.c \
-
	elf_data.c \
-
	elf_end.c \
-
	elf_errmsg.c \
-
	elf_errno.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_open.c \
-
	elf_phnum.c \
-
	elf_rand.c \
-
	elf_rawfile.c \
-
	elf_scn.c \
-
	elf_shnum.c \
-
	elf_shstrndx.c \
-
	elf_strptr.c \
-
	elf_update.c \
-
	elf_version.c \
-
	elf.c \
-
	gelf_cap.c \
-
	gelf_checksum.c \
-
	gelf_dyn.c \
-
	gelf_ehdr.c \
-
	gelf_fsize.c \
-
	gelf_getclass.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_align.c \
-
	libelf_allocate.c \
-
	libelf_ar_util.c \
-
	libelf_ar.c \
-
	libelf_checksum.c \
-
	libelf_convert.c \
-
	libelf_data.c \
-
	libelf_ehdr.c \
-
	libelf_extended.c \
-
	libelf_fsize.c \
-
	libelf_memory.c \
-
	libelf_msize.c \
-
	libelf_open.c \
-
	libelf_phdr.c \
-
	libelf_shdr.c \
-
	libelf_xlate.c
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/external/libelf -I$(top_builddir)/external/libelf
-

-
VPATH=	$(top_srcdir)/external/libelf
-

-
libelf_fsize.c: libelf_fsize.m4 elf_types.m4
-
	m4 -D SRCDIR=$(top_srcdir)/external/libelf $(top_srcdir)/external/libelf/elf_types.m4 $(top_srcdir)/external/libelf/libelf_fsize.m4 > $@
-

-
libelf_convert.c: libelf_convert.m4 elf_types.m4
-
	m4 -D SRCDIR=$(top_srcdir)/external/libelf $(top_srcdir)/external/libelf/elf_types.m4 $(top_srcdir)/external/libelf/libelf_convert.m4 > $@
-

-
libelf_msize.c: libelf_msize.m4 elf_types.m4
-
	@m4 -D SRCDIR=$(top_srcdir)/external/libelf $(top_srcdir)/external/libelf/elf_types.m4 $(top_srcdir)/external/libelf/libelf_msize.m4 > $@
-

-

-
include $(MK)/static-lib.mk
added external/libelf/Makefile.in
@@ -0,0 +1,76 @@
+
include @builddir@/mk/defs.mk
+
LIB=	elf
+
SRCS=	elf_begin.c \
+
	elf_cntl.c \
+
	elf_data.c \
+
	elf_end.c \
+
	elf_errmsg.c \
+
	elf_errno.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_open.c \
+
	elf_phnum.c \
+
	elf_rand.c \
+
	elf_rawfile.c \
+
	elf_scn.c \
+
	elf_shnum.c \
+
	elf_shstrndx.c \
+
	elf_strptr.c \
+
	elf_update.c \
+
	elf_version.c \
+
	elf.c \
+
	gelf_cap.c \
+
	gelf_checksum.c \
+
	gelf_dyn.c \
+
	gelf_ehdr.c \
+
	gelf_fsize.c \
+
	gelf_getclass.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_align.c \
+
	libelf_allocate.c \
+
	libelf_ar_util.c \
+
	libelf_ar.c \
+
	libelf_checksum.c \
+
	libelf_convert.c \
+
	libelf_data.c \
+
	libelf_ehdr.c \
+
	libelf_extended.c \
+
	libelf_fsize.c \
+
	libelf_memory.c \
+
	libelf_msize.c \
+
	libelf_open.c \
+
	libelf_phdr.c \
+
	libelf_shdr.c \
+
	libelf_xlate.c
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/external/elf -I$(top_builddir)/external/elf
+

+
VPATH=	$(top_srcdir)/external/elf
+

+
libelf_fsize.c: libelf_fsize.m4 elf_types.m4
+
	m4 -D SRCDIR=$(top_srcdir)/external/elf $(top_srcdir)/external/elf/elf_types.m4 $(top_srcdir)/external/elf/libelf_fsize.m4 > $@
+

+
libelf_convert.c: libelf_convert.m4 elf_types.m4
+
	m4 -D SRCDIR=$(top_srcdir)/external/elf $(top_srcdir)/external/elf/elf_types.m4 $(top_srcdir)/external/elf/libelf_convert.m4 > $@
+

+
libelf_msize.c: libelf_msize.m4 elf_types.m4
+
	@m4 -D SRCDIR=$(top_srcdir)/external/elf $(top_srcdir)/external/elf/elf_types.m4 $(top_srcdir)/external/elf/libelf_msize.m4 > $@
+

+

+
include $(MK)/static-lib.mk
deleted external/libelf/Version.map
@@ -1,96 +0,0 @@
-
/* $Id$ */
-

-
R1.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_flagarhdr;
-
	elf_flagdata;
-
	elf_flagehdr;
-
	elf_flagelf;
-
	elf_flagphdr;
-
	elf_flagscn;
-
	elf_flagshdr;
-
	elf_getarhdr;
-
	elf_getarsym;
-
	elf_getbase;
-
	elf_getdata;
-
	elf_getident;
-
	elf_getphdrnum;
-
	elf_getphnum;
-
	elf_getscn;
-
	elf_getshdrnum;
-
	elf_getshdrstrndx;
-
	elf_getshnum;
-
	elf_getshstrndx;
-
	elf_hash;
-
	elf_kind;
-
	elf_memory;
-
	elf_ndxscn;
-
	elf_newdata;
-
	elf_newscn;
-
	elf_next;
-
	elf_nextscn;
-
	elf_open;
-
	elf_openmemory;
-
	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:
-
	*;
-
};
deleted external/libelf/_elftc.h
@@ -1,395 +0,0 @@
-
/*-
-
 * Copyright (c) 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.
-
 *
-
 * $Id: _elftc.h 2495 2012-04-23 05:31:56Z jkoshy $
-
 */
-

-
/**
-
 ** Miscellanous definitions needed by multiple components.
-
 **/
-

-
#ifndef	_ELFTC_H
-
#define	_ELFTC_H
-

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

-
#ifndef	offsetof
-
#define	offsetof(T, M)		((int) &((T*) 0) -> M)
-
#endif
-

-
/* --QUEUE-MACROS-- [[ */
-

-
/*
-
 * Supply macros missing from <sys/queue.h>
-
 */
-

-
/*
-
 * Copyright (c) 1991, 1993
-
 *	The Regents of the University of California.  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.
-
 * 3. Neither the name of the University nor the names of its contributors
-
 *    may be used to endorse or promote products derived from this software
-
 *    without specific prior written permission.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
-
 */
-

-
#ifndef	STAILQ_CONCAT
-
#define	STAILQ_CONCAT(head1, head2) do {			\
-
	if (!STAILQ_EMPTY((head2))) {				\
-
		*(head1)->stqh_last = (head2)->stqh_first;	\
-
		(head1)->stqh_last = (head2)->stqh_last;	\
-
		STAILQ_INIT((head2));				\
-
	}							\
-
} while (/*CONSTCOND*/0)
-
#endif
-

-
#ifndef	STAILQ_EMPTY
-
#define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
-
#endif
-

-
#ifndef	STAILQ_ENTRY
-
#define	STAILQ_ENTRY(type)					\
-
struct {							\
-
	struct type *stqe_next;	/* next element */		\
-
}
-
#endif
-

-
#ifndef	STAILQ_FIRST
-
#define	STAILQ_FIRST(head)	((head)->stqh_first)
-
#endif
-

-
#ifndef	STAILQ_HEAD
-
#define	STAILQ_HEAD(name, type)					\
-
struct name {							\
-
	struct type *stqh_first; /* first element */		\
-
	struct type **stqh_last; /* addr of last next element */ \
-
}
-
#endif
-

-
#ifndef	STAILQ_HEAD_INITIALIZER
-
#define	STAILQ_HEAD_INITIALIZER(head)				\
-
	{ NULL, &(head).stqh_first }
-
#endif
-

-
#ifndef	STAILQ_FOREACH
-
#define	STAILQ_FOREACH(var, head, field)			\
-
	for ((var) = ((head)->stqh_first);			\
-
		(var);						\
-
		(var) = ((var)->field.stqe_next))
-
#endif
-

-
#ifndef	STAILQ_FOREACH_SAFE
-
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)		\
-
       for ((var) = STAILQ_FIRST((head));			\
-
	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);	\
-
	    (var) = (tvar))
-
#endif
-

-
#ifndef	STAILQ_INIT
-
#define	STAILQ_INIT(head) do {					\
-
	(head)->stqh_first = NULL;				\
-
	(head)->stqh_last = &(head)->stqh_first;		\
-
} while (/*CONSTCOND*/0)
-
#endif
-

-
#ifndef	STAILQ_INSERT_HEAD
-
#define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
-
	if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)	\
-
		(head)->stqh_last = &(elm)->field.stqe_next;		\
-
	(head)->stqh_first = (elm);					\
-
} while (/*CONSTCOND*/0)
-
#endif
-

-
#ifndef	STAILQ_INSERT_TAIL
-
#define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
-
	(elm)->field.stqe_next = NULL;					\
-
	*(head)->stqh_last = (elm);					\
-
	(head)->stqh_last = &(elm)->field.stqe_next;			\
-
} while (/*CONSTCOND*/0)
-
#endif
-

-
#ifndef	STAILQ_INSERT_AFTER
-
#define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
-
	if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
-
		(head)->stqh_last = &(elm)->field.stqe_next;		\
-
	(listelm)->field.stqe_next = (elm);				\
-
} while (/*CONSTCOND*/0)
-
#endif
-

-
#ifndef	STAILQ_LAST
-
#define STAILQ_LAST(head, type, field)					\
-
	(STAILQ_EMPTY((head)) ?					\
-
	    NULL : ((struct type *)(void *)				\
-
	    ((char *)((head)->stqh_last) - offsetof(struct type, field))))
-
#endif
-

-
#ifndef	STAILQ_NEXT
-
#define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
-
#endif
-

-
#ifndef	STAILQ_REMOVE
-
#define	STAILQ_REMOVE(head, elm, type, field) do {			\
-
	if ((head)->stqh_first == (elm)) {				\
-
		STAILQ_REMOVE_HEAD((head), field);			\
-
	} else {							\
-
		struct type *curelm = (head)->stqh_first;		\
-
		while (curelm->field.stqe_next != (elm))		\
-
			curelm = curelm->field.stqe_next;		\
-
		if ((curelm->field.stqe_next =				\
-
			curelm->field.stqe_next->field.stqe_next) == NULL) \
-
			    (head)->stqh_last = &(curelm)->field.stqe_next; \
-
	}								\
-
} while (/*CONSTCOND*/0)
-
#endif
-

-
#ifndef	STAILQ_REMOVE_HEAD
-
#define	STAILQ_REMOVE_HEAD(head, field) do {				\
-
	if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == \
-
	    NULL)							\
-
		(head)->stqh_last = &(head)->stqh_first;		\
-
} while (/*CONSTCOND*/0)
-
#endif
-

-
#ifndef	TAILQ_FOREACH_SAFE
-
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
-
	for ((var) = TAILQ_FIRST((head));                               \
-
	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
-
	    (var) = (tvar))
-
#endif
-

-
/* ]] --QUEUE-MACROS-- */
-

-
/*
-
 * VCS Ids.
-
 */
-

-
#ifndef	ELFTC_VCSID
-

-
#if defined(__DragonFly__)
-
#define	ELFTC_VCSID(ID)		__RCSID(ID)
-
#endif
-

-
#if defined(__FreeBSD__)
-
#define	ELFTC_VCSID(ID)		__FBSDID(ID)
-
#endif
-

-
#if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || defined(__APPLE__)
-
#if defined(__GNUC__)
-
#define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
-
#else
-
#define	ELFTC_VCSID(ID)		/**/
-
#endif
-
#endif
-

-
#if defined(__minix)
-
#if defined(__GNUC__)
-
#define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
-
#else
-
#define	ELFTC_VCSID(ID)		/**/
-
#endif	/* __GNU__ */
-
#endif
-

-
#if defined(__NetBSD__)
-
#define	ELFTC_VCSID(ID)		__RCSID(ID)
-
#endif
-

-
#if defined(__OpenBSD__)
-
#if defined(__GNUC__)
-
#define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
-
#else
-
#define	ELFTC_VCSID(ID)		/**/
-
#endif	/* __GNUC__ */
-
#endif
-

-
#endif	/* ELFTC_VCSID */
-

-
/*
-
 * Provide an equivalent for getprogname(3).
-
 */
-

-
#ifndef	ELFTC_GETPROGNAME
-

-
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__minix) || \
-
    defined(__NetBSD__) || defined(__APPLE__)
-

-
#include <stdlib.h>
-

-
#define	ELFTC_GETPROGNAME()	getprogname()
-

-
#endif	/* __DragonFly__ || __FreeBSD__ || __minix || __NetBSD__ */
-

-

-
#if defined(__GLIBC__)
-

-
/*
-
 * GLIBC based systems have a global 'char *' pointer referencing
-
 * the executable's name.
-
 */
-

-
/* XXX: defined in errno.h
-
extern const char *program_invocation_short_name;
-
*/
-

-
#define	ELFTC_GETPROGNAME()	program_invocation_short_name
-

-
#endif	/* __GLIBC__ */
-

-

-
#if defined(__OpenBSD__)
-

-
extern const char *__progname;
-

-
#define	ELFTC_GETPROGNAME()	__progname
-

-
#endif	/* __OpenBSD__ */
-

-
#endif	/* ELFTC_GETPROGNAME */
-

-

-
/**
-
 ** Per-OS configuration.
-
 **/
-

-
#if defined(__DragonFly__)
-

-
#include <osreldate.h>
-
#include <sys/endian.h>
-

-
#define	ELFTC_BYTE_ORDER			_BYTE_ORDER
-
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
-
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
-

-
#define	ELFTC_HAVE_MMAP				1
-

-
#endif
-

-
#if defined(__GLIBC__)
-

-
#include <endian.h>
-

-
#define	ELFTC_BYTE_ORDER			__BYTE_ORDER
-
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		__LITTLE_ENDIAN
-
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		__BIG_ENDIAN
-

-
#define	ELFTC_HAVE_MMAP				1
-

-
/*
-
 * Debian GNU/Linux and Debian GNU/kFreeBSD do not have strmode(3).
-
 */
-
#define	ELFTC_HAVE_STRMODE			0
-

-
/* Whether we need to supply {be,le}32dec. */
-
#define ELFTC_NEED_BYTEORDER_EXTENSIONS		1
-

-
#define	roundup2	roundup
-

-
#endif	/* __GLIBC__ */
-

-

-
#if defined(__FreeBSD__)
-

-
#include <osreldate.h>
-
#include <sys/endian.h>
-

-
#define	ELFTC_BYTE_ORDER			_BYTE_ORDER
-
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
-
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
-

-
#define	ELFTC_HAVE_MMAP				1
-
#define	ELFTC_HAVE_STRMODE			1
-
#if __FreeBSD_version <= 900000
-
#define	ELFTC_BROKEN_YY_NO_INPUT		1
-
#endif
-
#endif	/* __FreeBSD__ */
-

-

-
#if defined(__minix)
-
#define	ELFTC_HAVE_MMAP				0
-
#endif	/* __minix */
-

-

-
#if defined(__NetBSD__) || defined(__APPLE__)
-

-
#include <sys/param.h>
-
#ifndef __APPLE__
-
#include <sys/endian.h>
-
#else
-
#include <machine/endian.h>
-
#define	roundup2	roundup
-
#endif
-

-
#define	ELFTC_BYTE_ORDER			_BYTE_ORDER
-
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
-
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
-

-
#define	ELFTC_HAVE_MMAP				1
-
#define	ELFTC_HAVE_STRMODE			1
-
#if __NetBSD_Version__ <= 599002100
-
/* from src/doc/CHANGES: flex(1): Import flex-2.5.35 [christos 20091025] */
-
/* and 5.99.21 was from Wed Oct 21 21:28:36 2009 UTC */
-
#  define ELFTC_BROKEN_YY_NO_INPUT		1
-
#endif
-
#endif	/* __NetBSD __ */
-

-

-
#if defined(__OpenBSD__)
-

-
#include <sys/param.h>
-
#include <sys/endian.h>
-

-
#define	ELFTC_BYTE_ORDER			_BYTE_ORDER
-
#define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
-
#define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
-

-
#define	ELFTC_HAVE_MMAP				1
-
#define	ELFTC_HAVE_STRMODE			1
-

-
#define	ELFTC_NEED_BYTEORDER_EXTENSIONS		1
-
#define	roundup2	roundup
-

-
#endif	/* __OpenBSD__ */
-

-
#endif	/* _ELFTC_H */
deleted external/libelf/_libelf.h
@@ -1,235 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008-2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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.
-
 *
-
 * $Id$
-
 */
-

-
#ifndef	__LIBELF_H_
-
#define	__LIBELF_H_
-

-
#include <sys/queue.h>
-

-
#include "_libelf_config.h"
-

-
#include "_elftc.h"
-

-
/*
-
 * 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;
-
	unsigned 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_ERROR(E, O) (((E) & LIBELF_ELF_ERROR_MASK) |	\
-
	((O) << LIBELF_OS_ERROR_SHIFT))
-

-
#define	LIBELF_SET_ERROR(E, O) do {					\
-
		LIBELF_PRIVATE(error) = LIBELF_ERROR(ELF_E_##E, (O));	\
-
	} while (0)
-

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

-
/*
-
 * Flags for library internal use.  These use the upper 16 bits of the
-
 * `e_flags' field.
-
 */
-
#define	LIBELF_F_API_MASK	0x00FFFFU  /* Flags defined by the API. */
-
#define	LIBELF_F_AR_HEADER	0x010000U  /* translated header available */
-
#define	LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */
-
#define	LIBELF_F_DATA_MALLOCED	0x040000U /* whether data was malloc'ed */
-
#define	LIBELF_F_RAWFILE_MALLOC	0x080000U /* whether e_rawfile was malloc'ed */
-
#define	LIBELF_F_RAWFILE_MMAP	0x100000U /* whether e_rawfile was mmap'ed */
-
#define	LIBELF_F_SHDRS_LOADED	0x200000U /* whether all shdrs were read in */
-
#define	LIBELF_F_SPECIAL_FILE	0x400000U /* non-regular file */
-

-
struct _Elf {
-
	int		e_activations;	/* activation count */
-
	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 */
-
	unsigned char	*e_rawfile;	/* uninterpreted bytes */
-
	size_t		e_rawsize;	/* size of uninterpreted bytes */
-
	unsigned int	e_version;	/* file version */
-

-
	/*
-
	 * Header information for archive members.  See the
-
	 * LIBELF_F_AR_HEADER flag.
-
	 */
-
	union {
-
		Elf_Arhdr	*e_arhdr;	/* translated header */
-
		unsigned char	*e_rawhdr;	/* untranslated header */
-
	} e_hdr;
-

-
	union {
-
		struct {		/* ar(1) archives */
-
			off_t	e_next;	/* set by elf_rand()/elf_next() */
-
			int	e_nchildren;
-
			unsigned char *e_rawstrtab; /* file name strings */
-
			size_t	e_rawstrtabsz;
-
			unsigned 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;
-
};
-

-
/*
-
 * The internal descriptor wrapping the "Elf_Data" type.
-
 */
-
struct _Libelf_Data {
-
	Elf_Data	d_data;		/* The exported descriptor. */
-
	Elf_Scn		*d_scn;		/* The containing section */
-
	unsigned int	d_flags;
-
	STAILQ_ENTRY(_Libelf_Data) d_next;
-
};
-

-
struct _Elf_Scn {
-
	union {
-
		Elf32_Shdr	s_shdr32;
-
		Elf64_Shdr	s_shdr64;
-
	} s_shdr;
-
	STAILQ_HEAD(, _Libelf_Data) s_data;	/* translated data */
-
	STAILQ_HEAD(, _Libelf_Data) s_rawdata;	/* raw data */
-
	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
-
};
-

-

-
/*
-
 * The LIBELF_COPY macros are used to copy fields from a GElf_*
-
 * structure to their 32-bit counterparts, while checking for out of
-
 * range values.
-
 *
-
 * - LIBELF_COPY_U32 :: copy an unsigned 32 bit field.
-
 * - LIBELF_COPY_S32 :: copy a signed 32 bit field.
-
 */
-

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

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

-

-
/*
-
 * Function Prototypes.
-
 */
-

-
__BEGIN_DECLS
-
struct _Libelf_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, int _reporterror);
-
Elf	*_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar);
-
Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst);
-
Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst);
-
long	 _libelf_checksum(Elf *_e, int _elfclass);
-
void	*_libelf_ehdr(Elf *_e, int _elfclass, int _allocate);
-
unsigned 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))
-
	    (unsigned char *_dst, size_t dsz, unsigned 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_section_headers(Elf *e, void *ehdr);
-
unsigned int _libelf_malign(Elf_Type _t, int _elfclass);
-
Elf	*_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror);
-
size_t	_libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version);
-
void	*_libelf_newphdr(Elf *_e, int _elfclass, size_t _count);
-
Elf	*_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror);
-
struct _Libelf_Data *_libelf_release_data(struct _Libelf_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);
-
__END_DECLS
-

-
#endif	/* __LIBELF_H_ */
deleted external/libelf/_libelf_ar.h
@@ -1,57 +0,0 @@
-
/*-
-
 * Copyright (c) 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 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.
-
 *
-
 * $Id$
-
 */
-

-
#ifndef	__LIBELF_AR_H_
-
#define	__LIBELF_AR_H_
-

-
/*
-
 * Prototypes and declarations needed by libelf's ar(1) archive
-
 * handling code.
-
 */
-

-
#include <ar.h>
-

-
#define	LIBELF_AR_BSD_EXTENDED_NAME_PREFIX	"#1/"
-
#define	LIBELF_AR_BSD_SYMTAB_NAME		"__.SYMDEF"
-
#define	LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE	\
-
	(sizeof(LIBELF_AR_BSD_EXTENDED_NAME_PREFIX) - 1)
-

-
#define	IS_EXTENDED_BSD_NAME(NAME)				\
-
	(strncmp((const char *) (NAME),				\
-
	 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX,			\
-
	 LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE) == 0)
-

-

-
unsigned char *_libelf_ar_get_string(const char *_buf, size_t _sz,
-
    unsigned int _rawname, int _svr4names);
-
char	*_libelf_ar_get_raw_name(const struct ar_hdr *_arh);
-
char	*_libelf_ar_get_translated_name(const struct ar_hdr *_arh, Elf *_ar);
-
int	_libelf_ar_get_number(const char *_buf, size_t _sz,
-
    unsigned int _base, size_t *_ret);
-

-
#endif	/* __LIBELF_AR_H_ */
deleted external/libelf/_libelf_config.h
@@ -1,211 +0,0 @@
-
/*-
-
 * Copyright (c) 2008-2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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.
-
 *
-
 * $Id$
-
 */
-

-
#if defined(__APPLE__) || defined(__DragonFly__)
-

-
#if	defined(__amd64__)
-
#define	LIBELF_ARCH		EM_X86_64
-
#define	LIBELF_BYTEORDER	ELFDATA2LSB
-
#define	LIBELF_CLASS		ELFCLASS64
-
#elif	defined(__i386__)
-
#define	LIBELF_ARCH		EM_386
-
#define	LIBELF_BYTEORDER	ELFDATA2LSB
-
#define	LIBELF_CLASS		ELFCLASS32
-
#elif   defined(__aarch64__)
-
#define LIBELF_ARCH             EM_AARCH64
-
#define LIBELF_BYTEORDER        ELFDATA2LSB
-
#define LIBELF_CLASS            ELFCLASS64
-
#endif
-

-
#endif	/* __DragonFly__ */
-

-
#ifdef __FreeBSD__
-

-
/*
-
 * Define LIBELF_{ARCH,BYTEORDER,CLASS} based on the machine architecture.
-
 * See also: <machine/elf.h>.
-
 */
-

-
#if	defined(__amd64__)
-

-
#define	LIBELF_ARCH		EM_X86_64
-
#define	LIBELF_BYTEORDER	ELFDATA2LSB
-
#define	LIBELF_CLASS		ELFCLASS64
-

-
#elif	defined(__aarch64__)
-

-
#define	LIBELF_ARCH		EM_AARCH64
-
#define	LIBELF_BYTEORDER	ELFDATA2LSB
-
#define	LIBELF_CLASS		ELFCLASS64
-

-
#elif	defined(__arm__)
-

-
#define	LIBELF_ARCH		EM_ARM
-
#if	defined(__ARMEB__)	/* Big-endian ARM. */
-
#define	LIBELF_BYTEORDER	ELFDATA2MSB
-
#else
-
#define	LIBELF_BYTEORDER	ELFDATA2LSB
-
#endif
-
#define	LIBELF_CLASS		ELFCLASS32
-

-
#elif	defined(__i386__)
-

-
#define	LIBELF_ARCH		EM_386
-
#define	LIBELF_BYTEORDER	ELFDATA2LSB
-
#define	LIBELF_CLASS		ELFCLASS32
-

-
#elif	defined(__ia64__)
-

-
#define	LIBELF_ARCH		EM_IA_64
-
#define	LIBELF_BYTEORDER	ELFDATA2LSB
-
#define	LIBELF_CLASS		ELFCLASS64
-

-
#elif	defined(__mips__)
-

-
#define	LIBELF_ARCH		EM_MIPS
-
#if	defined(__MIPSEB__)
-
#define	LIBELF_BYTEORDER	ELFDATA2MSB
-
#else
-
#define	LIBELF_BYTEORDER	ELFDATA2LSB
-
#endif
-
#define	LIBELF_CLASS		ELFCLASS32
-

-
#elif defined(__powerpc64__)
-

-
#define LIBELF_ARCH EM_PPC64
-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-
#define LIBELF_BYTEORDER ELFDATA2LSB
-
#else
-
#define LIBELF_BYTEORDER ELFDATA2MSB
-
#endif
-
#define LIBELF_CLASS ELFCLASS64
-

-
#elif	defined(__powerpc__)
-

-
#define	LIBELF_ARCH		EM_PPC
-
#define	LIBELF_BYTEORDER	ELFDATA2MSB
-
#define	LIBELF_CLASS		ELFCLASS32
-

-
#elif	defined(__sparc__)
-

-
#define	LIBELF_ARCH		EM_SPARCV9
-
#define	LIBELF_BYTEORDER	ELFDATA2MSB
-
#define	LIBELF_CLASS		ELFCLASS64
-

-
#else
-
#error	Unknown FreeBSD architecture.
-
#endif
-
#endif  /* __FreeBSD__ */
-

-
/*
-
 * Definitions for Minix3.
-
 */
-
#ifdef __minix
-

-
#define	LIBELF_ARCH		EM_386
-
#define	LIBELF_BYTEORDER	ELFDATA2LSB
-
#define	LIBELF_CLASS		ELFCLASS32
-

-
#endif	/* __minix */
-

-
#ifdef __NetBSD__
-

-
#include <machine/elf_machdep.h>
-

-
#if	!defined(ARCH_ELFSIZE)
-
#error	ARCH_ELFSIZE is not defined.
-
#endif
-

-
#if	ARCH_ELFSIZE == 32
-
#define	LIBELF_ARCH		ELF32_MACHDEP_ID
-
#define	LIBELF_BYTEORDER	ELF32_MACHDEP_ENDIANNESS
-
#define	LIBELF_CLASS		ELFCLASS32
-
#define	Elf_Note		Elf32_Nhdr
-
#else
-
#define	LIBELF_ARCH		ELF64_MACHDEP_ID
-
#define	LIBELF_BYTEORDER	ELF64_MACHDEP_ENDIANNESS
-
#define	LIBELF_CLASS		ELFCLASS64
-
#define	Elf_Note		Elf64_Nhdr
-
#endif
-

-
#endif	/* __NetBSD__ */
-

-
#if defined(__OpenBSD__)
-

-
#include <machine/exec.h>
-

-
#define	LIBELF_ARCH		ELF_TARG_MACH
-
#define	LIBELF_BYTEORDER	ELF_TARG_DATA
-
#define	LIBELF_CLASS		ELF_TARG_CLASS
-

-
#endif
-

-
/*
-
 * GNU & Linux compatibility.
-
 *
-
 * `__linux__' is defined in an environment runs the Linux kernel and glibc.
-
 * `__GNU__' is defined in an environment runs a GNU kernel (Hurd) and glibc.
-
 * `__GLIBC__' is defined for an environment that runs glibc over a non-GNU
-
 *     kernel such as GNU/kFreeBSD.
-
 */
-

-
#if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
-

-
#if defined(__linux__)
-

-
#if    defined(__amd64__)
-

-
#define LIBELF_ARCH             EM_X86_64
-
#define LIBELF_BYTEORDER        ELFDATA2LSB
-
#define LIBELF_CLASS            ELFCLASS64
-

-
#elif   defined(__aarch64__)
-

-
#define LIBELF_ARCH             EM_AARCH64
-
#define LIBELF_BYTEORDER        ELFDATA2LSB
-
#define LIBELF_CLASS            ELFCLASS64
-

-
#elif  defined(__i386__)
-

-
#define LIBELF_ARCH             EM_386
-
#define LIBELF_BYTEORDER        ELFDATA2LSB
-
#define LIBELF_CLASS            ELFCLASS32
-

-
#endif
-

-
#endif	/* defined(__linux__) */
-

-
#if	LIBELF_CLASS == ELFCLASS32
-
#define	Elf_Note		Elf32_Nhdr
-
#elif   LIBELF_CLASS == ELFCLASS64
-
#define	Elf_Note		Elf64_Nhdr
-
#else
-
#error  LIBELF_CLASS needs to be one of ELFCLASS32 or ELFCLASS64
-
#endif
-

-
#endif /* defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) */
deleted external/libelf/elf.3
@@ -1,612 +0,0 @@
-
.\" Copyright (c) 2006-2008,2011 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd July 28, 2014
-
.Os
-
.Dt ELF 3
-
.Sh NAME
-
.Nm elf
-
.Nd API for manipulating ELF objects
-
.Sh LIBRARY
-
.Lb libelf
-
.Sh SYNOPSIS
-
.In libelf.h
-
.Sh DESCRIPTION
-
The
-
.Lb libelf
-
provides functions that allow an application to read and manipulate
-
ELF object files, and to read
-
.Xr ar 1
-
archives.
-
The library allows the manipulation of ELF objects in a byte ordering
-
and word-size independent way, allowing an application to read and
-
create ELF objects for 32 and 64 bit architectures and for little-
-
and big-endian machines.
-
The library is capable of processing ELF objects that use extended
-
section numbering.
-
.Pp
-
This manual page serves to provide an overview of the functionality in
-
the ELF library.
-
Further information may found in the manual pages for individual
-
.Xr ELF 3
-
functions that comprise the library.
-
.Ss ELF Concepts
-
As described in
-
.Xr elf 5 ,
-
ELF files contain several data structures that are laid out in a
-
specific way.
-
ELF files begin with an
-
.Dq Executable Header ,
-
and may contain an optional
-
.Dq Program Header Table ,
-
and optional data in the form of ELF
-
.Dq sections .
-
A
-
.Dq Section Header Table
-
describes the content of the data in these sections.
-
.Pp
-
ELF objects have an associated
-
.Dq "ELF class"
-
which denotes the natural machine word size for the architecture
-
the object is associated with.
-
Objects for 32 bit architectures have an ELF class of
-
.Dv ELFCLASS32 .
-
Objects for 64 bit architectures have an ELF class of
-
.Dv ELFCLASS64 .
-
.Pp
-
ELF objects also have an associated
-
.Dq endianness
-
which denotes the endianness of the machine architecture associated
-
with the object.
-
This may be
-
.Dv ELFDATA2LSB
-
for little-endian architectures and
-
.Dv ELFDATA2MSB
-
for big-endian architectures.
-
.Pp
-
ELF objects are also associated with an API version number.
-
This version number determines the layout of the individual components
-
of an ELF file and the semantics associated with these.
-
.Ss Data Representation And Translation
-
The
-
.Xr ELF 3
-
library distinguishes between
-
.Dq native
-
representations of ELF data structures and their
-
.Dq file
-
representations.
-
.Pp
-
An application would work with ELF data in its
-
.Dq native
-
representation, i.e., using the native byteorder and alignment mandated
-
by the processor the application is running on.
-
The
-
.Dq file
-
representation of the same data could use a different byte ordering
-
and follow different constraints on object alignment than these native
-
constraints.
-
.Pp
-
Accordingly, the
-
.Xr ELF 3
-
library offers translation facilities
-
.Xr ( elf32_xlatetof 3 ,
-
.Xr elf32_xlatetom 3 ,
-
.Xr elf64_xlatetof 3
-
and
-
.Xr elf64_xlatetom 3 )
-
to and from these
-
representations and also provides higher-level APIs that retrieve and store
-
data from the ELF object in a transparent manner.
-
.Ss Library Working Version
-
Conceptually, there are three version numbers associated with an
-
application using the ELF library to manipulate ELF objects:
-
.Bl -bullet -compact -offset indent
-
.It
-
The ELF version that the application was compiled against.
-
This version determines the ABI expected by the application.
-
.It
-
The ELF version of the ELF object being manipulated by the
-
application through the ELF library.
-
.It
-
The ELF version (or set of versions) supported by the ELF library itself.
-
.El
-
.Pp
-
In order to facilitate working with ELF objects of differing versions,
-
the ELF library requires the application to call the
-
.Fn elf_version
-
function before invoking many of its operations, in order to inform
-
the library of the application's desired working version.
-
.Pp
-
In the current implementation, all three versions have to be
-
.Dv EV_CURRENT .
-
.Ss Namespace use
-
The ELF library uses the following prefixes:
-
.Bl -tag -width "ELF_F_*"
-
.It Dv elf_
-
Used for class-independent functions.
-
.It Dv elf32_
-
Used for functions working with 32 bit ELF objects.
-
.It Dv elf64_
-
Used for functions working with 64 bit ELF objects.
-
.It Dv Elf_
-
Used for class-independent data types.
-
.It Dv ELF_C_
-
Used for command values used in a few functions.
-
These symbols are defined as members of the
-
.Vt Dv Elf_Cmd
-
enumeration.
-
.It Dv ELF_E_
-
Used for error numbers.
-
.It Dv ELF_F_
-
Used for flags.
-
.It Dv ELF_K_
-
These constants define the kind of file associated with an ELF
-
descriptor.
-
See
-
.Xr elf_kind 3 .
-
The symbols are defined by the
-
.Vt Elf_Kind
-
enumeration.
-
.It Dv ELF_T_
-
These values are defined by the
-
.Vt Elf_Type
-
enumeration, and denote the types of ELF data structures
-
that can be present in an ELF object.
-
.El
-
.Pp
-
In addition, the library uses symbols with prefixes
-
.Dv _ELF
-
and
-
.Dv _libelf
-
for its internal use.
-
.Ss Descriptors
-
Applications communicate with the library using descriptors.
-
These are:
-
.Bl -tag -width ".Vt Elf_Data"
-
.It Vt Elf
-
An
-
.Vt Elf
-
descriptor represents an ELF object or an
-
.Xr ar 1
-
archive.
-
It is allocated using one of the
-
.Fn elf_begin
-
or
-
.Fn elf_memory
-
functions.
-
An
-
.Vt Elf
-
descriptor can be used to read and write data to an ELF file.
-
An
-
.Vt Elf
-
descriptor can be associated with zero or more
-
.Vt Elf_Scn
-
section descriptors.
-
.Pp
-
Given an ELF descriptor, the application may retrieve the ELF
-
object's class-dependent
-
.Dq "Executable Header"
-
structures using the
-
.Fn elf32_getehdr
-
or
-
.Fn elf64_getehdr
-
functions.
-
A new Ehdr structure may be allocated using the
-
.Fn elf64_newehdr
-
or
-
.Fn elf64_newehdr
-
functions.
-
.Pp
-
The
-
.Dq "Program Header Table"
-
associated with an ELF descriptor may be allocated using the
-
.Fn elf32_getphdr
-
or
-
.Fn elf64_getphdr
-
functions.
-
A new program header table may be allocated or an existing table
-
resized using the
-
.Fn elf32_newphdr
-
or
-
.Fn elf64_newphdr
-
functions.
-
.Pp
-
The
-
.Vt Elf
-
structure is opaque and has no members visible to the
-
application.
-
.\" TODO describe the Elf_Arhdr and Elf_Arsym structures.
-
.It Vt Elf_Data
-
An
-
.Vt Elf_Data
-
data structure describes an individual chunk of a ELF file as
-
represented in memory.
-
It has the following application-visible members:
-
.Bl -tag -width ".Vt unsigned int d_version" -compact
-
.It Vt "uint64_t d_align"
-
The in-file alignment of the data buffer within its containing ELF section.
-
This value must be non-zero and a power of two.
-
.It Vt "void *d_buf"
-
A pointer to data in memory.
-
.It Vt "uint64_t d_off"
-
The offset within the containing section where this descriptor's data
-
would be placed.
-
This field will be computed by the library unless the application
-
requests full control of the ELF object's layout.
-
.It Vt "uint64_t d_size"
-
The number of bytes of data in this descriptor.
-
.It Vt "Elf_Type d_type"
-
The ELF type (see below) of the data in this descriptor.
-
.It Vt "unsigned int d_version"
-
The operating version for the data in this buffer.
-
.El
-
.Pp
-
.Vt Elf_Data
-
descriptors are usually associated with
-
.Vt Elf_Scn
-
descriptors.
-
Existing data descriptors associated with an ELF section may be
-
structures are retrieved using the
-
.Fn elf_getdata
-
and
-
.Fn elf_rawdata
-
functions.
-
The
-
.Fn elf_newdata
-
function may be used to attach new data descriptors to an ELF section.
-
.It Vt Elf_Scn
-
.Vt Elf_Scn
-
descriptors represent a section in an ELF object.
-
.Pp
-
They are retrieved using the
-
.Fn elf_getscn
-
function.
-
An application may iterate through the existing sections of an ELF
-
object using the
-
.Fn elf_nextscn
-
function.
-
New sections may be allocated using the
-
.Fn elf_newscn
-
function.
-
.Pp
-
The
-
.Vt Elf_Scn
-
descriptor is opaque and contains no application modifiable fields.
-
.El
-
.Ss Supported Elf Types
-
The following ELF datatypes are supported by the library.
-
.Pp
-
.Bl -tag -width ".Dv ELF_T_SYMINFO" -compact
-
.It Dv ELF_T_ADDR
-
Machine addresses.
-
.It Dv ELF_T_BYTE
-
Byte data.
-
The library will not attempt to translate byte data.
-
.It Dv ELF_T_CAP
-
Software and hardware capability records.
-
.It Dv ELF_T_DYN
-
Records used in a section of type
-
.Dv SHT_DYNAMIC .
-
.It Dv ELF_T_EHDR
-
ELF executable header.
-
.It Dv ELF_T_GNUHASH
-
GNU-style hash tables.
-
.It Dv ELF_T_HALF
-
16-bit unsigned words.
-
.It Dv ELF_T_LWORD
-
64 bit unsigned words.
-
.It Dv ELF_T_MOVE
-
ELF Move records.
-
.\".It Dv ELF_T_MOVEP
-
.\" As yet unsupported.
-
.It Dv ELF_T_NOTE
-
ELF Note structures.
-
.It Dv ELF_T_OFF
-
File offsets.
-
.It Dv ELF_T_PHDR
-
ELF program header table entries.
-
.It Dv ELF_T_REL
-
ELF relocation entries.
-
.It Dv ELF_T_RELA
-
ELF relocation entries with addends.
-
.It Dv ELF_T_SHDR
-
ELF section header entries.
-
.It Dv ELF_T_SWORD
-
Signed 32-bit words.
-
.It Dv ELF_T_SXWORD
-
Signed 64-bit words.
-
.It Dv ELF_T_SYMINFO
-
ELF symbol information.
-
.It Dv ELF_T_SYM
-
ELF symbol table entries.
-
.It Dv ELF_T_VDEF
-
Symbol version definition records.
-
.It Dv ELF_T_VNEED
-
Symbol version requirement records.
-
.It Dv ELF_T_WORD
-
Unsigned 32-bit words.
-
.It Dv ELF_T_XWORD
-
Unsigned 64-bit words.
-
.El
-
.Pp
-
The symbol
-
.Dv ELF_T_NUM
-
denotes the number of Elf types known to the library.
-
.Pp
-
The following table shows the mapping between ELF section types
-
defined in
-
.Xr elf 5
-
and the types supported by the library.
-
.Bl -column ".Dv SHT_PREINIT_ARRAY" ".Dv ELF_T_SYMINFO"
-
.It Em Section Type Ta Em "Library Type" Ta Em Description
-
.It Dv SHT_DYNAMIC Ta Dv ELF_T_DYN Ta Xo
-
.Sq .dynamic
-
section entries.
-
.Xc
-
.It Dv SHT_DYNSYM Ta Dv ELF_T_SYM Ta Symbols for dynamic linking.
-
.It Dv SHT_FINI_ARRAY Ta Dv ELF_T_ADDR Ta Termination function pointers.
-
.It Dv SHT_GNU_HASH Ta Dv ELF_T_GNUHASH Ta GNU hash sections.
-
.It Dv SHT_GNU_LIBLIST Ta Dv ELF_T_WORD Ta List of libraries to be pre-linked.
-
.It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions.
-
.It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements.
-
.It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols.
-
.It Dv SHT_GROUP Ta Dv ELF_T_WORD Ta Section group marker.
-
.It Dv SHT_HASH Ta Dv ELF_T_HASH Ta Symbol hashes.
-
.It Dv SHT_INIT_ARRAY Ta Dv ELF_T_ADDR Ta Initialization function pointers.
-
.It Dv SHT_NOBITS Ta Dv ELF_T_BYTE Ta Xo
-
Empty sections.
-
See
-
.Xr elf 5 .
-
.Xc
-
.It Dv SHT_NOTE Ta Dv ELF_T_NOTE Ta ELF note records.
-
.It Dv SHT_PREINIT_ARRAY Ta Dv ELF_T_ADDR Ta Pre-initialization function pointers.
-
.It Dv SHT_PROGBITS Ta Dv ELF_T_BYTE Ta Machine code.
-
.It Dv SHT_REL Ta Dv ELF_T_REL Ta ELF relocation records.
-
.It Dv SHT_RELA Ta Dv ELF_T_RELA Ta Relocation records with addends.
-
.It Dv SHT_STRTAB Ta Dv ELF_T_BYTE Ta String tables.
-
.It Dv SHT_SYMTAB Ta Dv ELF_T_SYM Ta Symbol tables.
-
.It Dv SHT_SYMTAB_SHNDX Ta Dv ELF_T_WORD Ta Used with extended section numbering.
-
.It Dv SHT_SUNW_dof Ta Dv ELF_T_BYTE Ta Xo
-
Used by
-
.Xr dtrace 1 .
-
.Xc
-
.It Dv SHT_SUNW_move Ta Dv ELF_T_MOVE Ta ELF move records.
-
.It Dv SHT_SUNW_syminfo Ta Dv ELF_T_SYMINFO Ta Additional symbol flags.
-
.It Dv SHT_SUNW_verdef Ta Dv ELF_T_VDEF Ta Xo
-
Same as
-
.Dv SHT_GNU_verdef .
-
.Xc
-
.It Dv SHT_SUNW_verneed Ta Dv ELF_T_VNEED Ta Xo
-
Same as
-
.Dv SHT_GNU_verneed .
-
.Xc
-
.It Dv SHT_SUNW_versym Ta Dv ELF_T_HALF Ta Xo
-
Same as
-
.Dv SHT_GNU_versym .
-
.Xc
-
.El
-
.Pp
-
Section types in the range
-
.Ns [ Dv SHT_LOOS ,
-
.Dv SHT_HIUSER ]
-
are otherwise considered to be of type
-
.Dv ELF_T_BYTE .
-
.Ss Functional Grouping
-
This section contains a brief overview of the available functionality
-
in the ELF library.
-
Each function listed here is described further in its own manual page.
-
.Bl -tag -width indent
-
.It "Archive Access"
-
.Bl -tag -compact
-
.It Fn elf_getarsym
-
Retrieve the archive symbol table.
-
.It Fn elf_getarhdr
-
Retrieve the archive header for an object.
-
.It Fn elf_getbase
-
Retrieve the offset of a member inside an archive.
-
.It Fn elf_next
-
Iterate through an
-
.Xr ar 1
-
archive.
-
.It Fn elf_rand
-
Random access inside an
-
.Xr ar 1
-
archive.
-
.El
-
.It "Data Structures"
-
.Bl -tag -compact
-
.It Fn elf_getdata
-
Retrieve translated data for an ELF section.
-
.It Fn elf_getscn
-
Retrieve the section descriptor for a named section.
-
.It Fn elf_ndxscn
-
Retrieve the index for a section.
-
.It Fn elf_newdata
-
Add a new
-
.Vt Elf_Data
-
descriptor to an ELF section.
-
.It Fn elf_newscn
-
Add a new section descriptor to an ELF descriptor.
-
.It Fn elf_nextscn
-
Iterate through the sections in an ELF object.
-
.It Fn elf_rawdata
-
Retrieve untranslated data for an ELF section.
-
.It Fn elf_rawfile
-
Return a pointer to the untranslated file contents for an ELF object.
-
.It Fn elf32_getehdr , Fn elf64_getehdr
-
Retrieve the Executable Header in an ELF object.
-
.It Fn elf32_getphdr , Fn elf64_getphdr
-
Retrieve the Program Header Table in an ELF object.
-
.It Fn elf32_getshdr , Fn elf64_getshdr
-
Retrieve the ELF section header associated with an
-
.Vt Elf_Scn
-
descriptor.
-
.It Fn elf32_newehdr , Fn elf64_newehdr
-
Allocate an Executable Header in an ELF object.
-
.It Fn elf32_newphdr , Fn elf64_newphdr
-
Allocate or resize the Program Header Table in an ELF object.
-
.El
-
.It "Data Translation"
-
.Bl -tag -compact
-
.It Fn elf32_xlatetof , Fn elf64_xlatetof
-
Translate an ELF data structure from its native representation to its
-
file representation.
-
.It Fn elf32_xlatetom , Fn elf64_xlatetom
-
Translate an ELF data structure from its file representation to a
-
native representation.
-
.El
-
.It "Error Reporting"
-
.Bl -tag -compact
-
.It Fn elf_errno
-
Retrieve the current error.
-
.It Fn elf_errmsg
-
Retrieve a human readable description of the current error.
-
.El
-
.It "Initialization"
-
.Bl -tag -compact
-
.It Fn elf_begin
-
Opens an
-
.Xr ar 1
-
archive or ELF object given a file descriptor.
-
.It Fn elf_end
-
Close an ELF descriptor and release all its resources.
-
.It Fn elf_memory
-
Opens an
-
.Xr ar 1
-
archive or ELF object present in a memory arena.
-
.It Fn elf_version
-
Sets the operating version.
-
.El
-
.It "IO Control"
-
.Bl -tag -width ".Fn elf_setshstrndx" -compact
-
.It Fn elf_cntl
-
Manage the association between and ELF descriptor and its underlying file.
-
.It Fn elf_flagdata
-
Mark an
-
.Vt Elf_Data
-
descriptor as dirty.
-
.It Fn elf_flagehdr
-
Mark the ELF Executable Header in an ELF descriptor as dirty.
-
.It Fn elf_flagphdr
-
Mark the ELF Program Header Table in an ELF descriptor as dirty.
-
.It Fn elf_flagscn
-
Mark an
-
.Vt Elf_Scn
-
descriptor as dirty.
-
.It Fn elf_flagshdr
-
Mark an ELF Section Header as dirty.
-
.It Fn elf_setshstrndx
-
Set the index of the section name string table for the ELF object.
-
.It Fn elf_update
-
Recompute ELF object layout and optionally write the modified object
-
back to the underlying file.
-
.El
-
.It "Queries"
-
.Bl -tag -width ".Fn elf_getshstrndx" -compact
-
.It Fn elf32_checksum , Fn elf64_checkum
-
Compute checksum of an ELF object.
-
.It Fn elf_getident
-
Retrieve the identification bytes for an ELF object.
-
.It Fn elf_getshnum
-
Retrieve the number of sections in an ELF object.
-
.It Fn elf_getshstrndx
-
Retrieve the section index of the section name string table in
-
an ELF object.
-
.It Fn elf_hash
-
Compute the ELF hash value of a string.
-
.It Fn elf_kind
-
Query the kind of object associated with an ELF descriptor.
-
.It Fn elf32_fsize , Fn elf64_fsize
-
Return the size of the file representation of an ELF type.
-
.El
-
.El
-
.Ss Controlling ELF Object Layout
-
In the usual mode of operation, library will compute section
-
offsets and alignments based on the contents of an ELF descriptor's
-
sections without need for further intervention by the
-
application.
-
.Pp
-
However, if the application wishes to take complete charge of the
-
layout of the ELF file, it may set the
-
.Dv ELF_F_LAYOUT
-
flag on an ELF descriptor using
-
.Xr elf_flagelf 3 ,
-
following which the library will use the data offsets and alignments
-
specified by the application when laying out the file.
-
Application control of file layout is described further in the 
-
.Xr elf_update 3
-
manual page.
-
.Pp
-
Gaps in between sections will be filled with the fill character
-
set by function
-
.Fn elf_fill .
-
.Ss Error Handling
-
In case an error is encountered, these library functions set an
-
internal error number and signal the presence of the error by
-
returning an special return value.
-
The application can check the
-
current error number by calling
-
.Xr elf_errno 3 .
-
A human readable description of the recorded error is available by
-
calling
-
.Xr elf_errmsg 3 .
-
.Ss Memory Management Rules
-
The library keeps track of all
-
.Vt Elf_Scn
-
and
-
.Vt Elf_Data
-
descriptors associated with an ELF descriptor and recovers them
-
when the descriptor is closed using
-
.Xr elf_end 3 .
-
Thus the application must not call
-
.Xr free 3
-
on data structures allocated by the ELF library.
-
.Pp
-
Conversely the library will not
-
free data that it has not allocated.
-
As an example, an application may call
-
.Xr elf_newdata 3
-
to allocate a new
-
.Vt Elf_Data
-
descriptor and can set the
-
.Va d_off
-
member of the descriptor to point to a region of memory allocated
-
using
-
.Xr malloc 3 .
-
It is the applications responsibility to free this arena, though the
-
library will reclaim the space used by the
-
.Vt Elf_Data
-
descriptor itself.
-
.Sh SEE ALSO
-
.Xr gelf 3 ,
-
.Xr elf 5
-
.Sh HISTORY
-
The original ELF(3) API was developed for Unix System V.
-
The current implementation of the ELF(3) API appeared in
-
.Fx 7.0 .
-
.Sh AUTHORS
-
The ELF library was written by
-
.An "Joseph Koshy"
-
.Aq jkoshy@FreeBSD.org .
deleted external/libelf/elf.c
@@ -1,46 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008,2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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 <libelf.h>
-

-
#include "_libelf.h"
-

-
#ifdef __OpenBSD__
-
 #ifndef EM_AMD64
-
  #define EM_AMD64    62
-
 #endif
-
#endif
-

-
ELFTC_VCSID("$Id$");
-

-
struct _libelf_globals _libelf = {
-
	.libelf_arch		= LIBELF_ARCH,
-
	.libelf_byteorder	= LIBELF_BYTEORDER,
-
	.libelf_class		= LIBELF_CLASS,
-
	.libelf_error		= 0,
-
	.libelf_fillchar	= 0,
-
	.libelf_version		= EV_NONE
-
};
deleted external/libelf/elf_begin.3
@@ -1,315 +0,0 @@
-
.\" Copyright (c) 2006,2008-2011 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$ 
-
.\"
-
.Dd December 11, 2011
-
.Os
-
.Dt ELF_BEGIN 3
-
.Sh NAME
-
.Nm elf_begin
-
.Nd open an ELF file or ar(1) archive
-
.Sh LIBRARY
-
.Lb libelf
-
.Sh SYNOPSIS
-
.In libelf.h
-
.Ft "Elf *"
-
.Fn elf_begin "int fd" "Elf_Cmd cmd" "Elf *elf"
-
.Sh DESCRIPTION
-
Function
-
.Fn elf_begin
-
is used to open ELF files and
-
.Xr ar 1
-
archives for further processing by other APIs in the
-
.Xr elf 3
-
library.
-
It is also used to access individual ELF members of an
-
.Xr ar 1
-
archive in combination with the
-
.Xr elf_next 3
-
and
-
.Xr elf_rand 3
-
APIs.
-
.Pp
-
Argument
-
.Ar fd
-
is an open file descriptor returned from an
-
.Xr open 2
-
system call.
-
Function
-
.Fn elf_begin
-
uses argument
-
.Ar fd
-
for reading or writing depending on the value of argument
-
.Ar cmd .
-
Argument
-
.Ar elf
-
is primarily used for iterating through archives.
-
.Pp
-
The argument
-
.Ar cmd
-
can have the following values:
-
.Bl -tag -width "ELF_C_WRITE"
-
.It ELF_C_NULL
-
Causes
-
.Fn elf_begin
-
to return NULL.
-
Arguments
-
.Ar fd
-
and
-
.Ar elf
-
are ignored, and no additional error is signalled.
-
.It ELF_C_READ
-
This value is to be when the application wishes to examine (but not
-
modify) the contents of the file specified by the arguments
-
.Ar fd
-
and
-
.Ar elf .
-
It can be used for both
-
.Xr ar 1
-
archives and for ELF objects.
-
.Pp
-
If argument
-
.Ar elf
-
is NULL, the library will allocate a new ELF descriptor for the file
-
being processed.
-
The argument
-
.Ar fd
-
should have been opened for reading.
-
.Pp
-
If argument
-
.Ar elf
-
is not NULL, and references a regular ELF file previously opened with
-
.Fn elf_begin ,
-
then the activation count for the descriptor referenced by argument
-
.Ar elf
-
is incremented.
-
The value in argument
-
.Ar fd
-
should match that used to open the descriptor argument
-
.Ar elf .
-
.Pp
-
If argument
-
.Ar elf
-
is not NULL, and references a descriptor for an
-
.Xr ar 1
-
archive opened earlier with
-
.Fn elf_begin ,
-
a descriptor for an element in the archive is returned as
-
described in the section
-
.Sx "Processing ar(1) archives"
-
below.
-
The value for argument
-
.Ar fd
-
should match that used to open the archive earlier.
-
.Pp
-
If argument
-
.Ar elf
-
is not NULL, and references an
-
.Xr ar 1
-
archive opened earlier with
-
.Fn elf_memory ,
-
then the value of the argument
-
.Ar fd
-
is ignored.
-
.It Dv ELF_C_RDWR
-
This command is used to prepare an ELF file for reading and writing.
-
This command is not supported for
-
.Xr ar 1
-
archives.
-
.Pp
-
Argument
-
.Ar fd
-
should have been opened for reading and writing.
-
If argument
-
.Ar elf
-
is NULL, the library will allocate a new ELF descriptor for
-
the file being processed.
-
If the argument
-
.Ar elf
-
is non-null, it should point to a descriptor previously
-
allocated with
-
.Fn elf_begin
-
with the same values for arguments
-
.Ar fd
-
and
-
.Ar cmd ;
-
in this case the library will increment the activation count for descriptor
-
.Ar elf
-
and return the same descriptor.
-
.Pp
-
Changes to the in-memory image of the ELF file may be written back to
-
disk using the
-
.Xr elf_update 3
-
function.
-
.It Dv ELF_C_WRITE
-
This command is used when the application wishes to create a new ELF
-
file.
-
Argument
-
.Ar fd
-
should have been opened for writing.
-
Argument
-
.Ar elf
-
is ignored, and the previous contents of file referenced by argument
-
.Ar fd
-
are overwritten.
-
.El
-
.Ss Processing ar(1) archives
-
An
-
.Xr ar 1
-
archive may be opened in read mode (with argument
-
.Ar cmd
-
set to
-
.Dv ELF_C_READ )
-
using
-
.Fn elf_begin
-
or
-
.Fn elf_memory .
-
The returned ELF descriptor can be passed into to
-
subsequent calls to
-
.Fn elf_begin
-
to access individual members of the archive.
-
.Pp
-
Random access within an opened archive is possible using
-
the
-
.Xr elf_next 3
-
and
-
.Xr elf_rand 3
-
functions.
-
.Pp
-
The symbol table of the archive may be retrieved
-
using
-
.Xr elf_getarsym 3 .
-
.Sh RETURN VALUES
-
The function returns a pointer to a ELF descriptor if successful, or NULL
-
if an error occurred.
-
.Sh EXAMPLES
-
To iterate through the members of an
-
.Xr ar 1
-
archive, use:
-
.Bd -literal -offset indent
-
Elf_Cmd c;
-
Elf *ar_e, *elf_e;
-
\&...
-
c = ELF_C_READ;
-
if ((ar_e = elf_begin(fd, c, (Elf *) 0)) == 0) {
-
	\&... handle error in opening the archive ...
-
}
-
while ((elf_e = elf_begin(fd, c, ar_e)) != 0) {
-
	\&... process member referenced by elf_e here ...
-
	c = elf_next(elf_e);
-
	elf_end(elf_e);
-
}
-
.Ed
-
.Pp
-
To create a new ELF file, use:
-
.Bd -literal -offset indent
-
int fd;
-
Elf *e;
-
\&...
-
if ((fd = open("filename", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) {
-
	\&... handle the error from open(2) ...
-
}
-
if ((e = elf_begin(fd, ELF_C_WRITE, (Elf *) 0)) == 0) {
-
	\&... handle the error from elf_begin() ...
-
}
-
\&... create the ELF image using other elf(3) APIs ...
-
elf_update(e, ELF_C_WRITE);
-
elf_end(e);
-
.Ed
-
.Sh ERRORS
-
Function
-
.Fn elf_begin
-
can fail with the following errors:
-
.Bl -tag -width "[ELF_E_RESOURCE]"
-
.It Bq Er ELF_E_ARCHIVE
-
The archive denoted by argument
-
.Ar elf
-
could not be parsed.
-
.It Bq Er ELF_E_ARGUMENT
-
The value in argument
-
.Ar cmd
-
was unrecognized.
-
.It Bq Er ELF_E_ARGUMENT
-
A non-null value for argument
-
.Ar elf
-
was specified when
-
.Ar cmd
-
was set to
-
.Dv ELF_C_RDWR .
-
.It Bq Er ELF_E_ARGUMENT
-
The value of argument
-
.Ar fd
-
differs from the one the ELF descriptor
-
.Ar elf
-
was created with.
-
.It Bq Er ELF_E_ARGUMENT
-
Argument
-
.Ar cmd
-
differs from the value specified when ELF descriptor
-
.Ar elf
-
was created.
-
.It Bq Er ELF_E_ARGUMENT
-
An
-
.Xr ar 1
-
archive was opened with with
-
.Ar cmd
-
set to
-
.Dv ELF_C_RDWR .
-
.It Bq Er ELF_E_ARGUMENT
-
The file referenced by argument
-
.Ar fd
-
was empty.
-
.It Bq Er ELF_E_ARGUMENT
-
The underlying file for argument
-
.Ar fd
-
was of an unsupported type.
-
.It Bq Er ELF_E_IO
-
The file descriptor in argument
-
.Ar fd
-
was invalid.
-
.It Bq Er ELF_E_IO
-
The file descriptor in argument
-
.Ar fd
-
could not be read or written to.
-
.It Bq Er ELF_E_RESOURCE
-
An out of memory condition was encountered.
-
.It Bq Er ELF_E_SEQUENCE
-
Function
-
.Fn elf_begin
-
was called before a working version was established with
-
.Xr elf_version 3 .
-
.It Bq Er ELF_E_VERSION
-
The ELF object referenced by argument
-
.Ar fd
-
was of an unsupported ELF version.
-
.El
-
.Sh SEE ALSO
-
.Xr elf 3 ,
-
.Xr elf_end 3 ,
-
.Xr elf_errno 3 ,
-
.Xr elf_memory 3 ,
-
.Xr elf_next 3 ,
-
.Xr elf_rand 3 ,
-
.Xr elf_update 3 ,
-
.Xr gelf 3
deleted external/libelf/elf_begin.c
@@ -1,91 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008-2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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 <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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:
-
		/*
-
		 * The ELF_C_WRITE command is required to ignore the
-
		 * descriptor passed in.
-
		 */
-
		a = NULL;
-
		break;
-

-
	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, 1);
-
	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);
-
}
deleted external/libelf/elf_cntl.3
@@ -1,111 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$ 
-
.\"
-
.Dd August 9, 2006
-
.Os
-
.Dt ELF_CNTL 3
-
.Sh NAME
-
.Nm elf_cntl
-
.Nd control an elf file descriptor
-
.Sh LIBRARY
-
.Lb libelf
-
.Sh SYNOPSIS
-
.In libelf.h
-
.Ft int
-
.Fn elf_cntl "Elf *elf" "Elf_Cmd cmd"
-
.Sh DESCRIPTION
-
Function
-
.Fn elf_cntl
-
controls the ELF library's subsequent use of the file descriptor
-
used to create ELF descriptor
-
.Ar elf .
-
.Pp
-
Argument
-
.Ar cmd
-
informs the library of the action to be taken:
-
.Bl -tag -width "ELF_C_FDDONE"
-
.It Dv ELF_C_FDDONE
-
This value instructs the ELF library not to perform any further
-
I/O on the file descriptor associated with argument
-
.Ar elf .
-
For ELF descriptors opened with mode
-
.Ar ELF_C_WRITE
-
or
-
.Ar ELF_C_RDWR
-
subsequent
-
.Fn elf_update
-
operations on the descriptor will fail.
-
.It Dv ELF_C_FDREAD
-
This value instructs the ELF library to read in all necessary
-
data associated with ELF descriptor
-
.Ar elf
-
into memory so that the underlying file descriptor can be
-
safely closed with command
-
.Dv ELF_C_FDDONE .
-
.El
-
.Pp
-
Argument
-
.Ar elf
-
must be an ELF descriptor associated with a file system object
-
(e.g., an
-
.Xr ar 1
-
archive, an ELF file, or other data file).
-
.Sh IMPLEMENTATION NOTES
-
Due to use of
-
.Xr mmap 2
-
internally, this function is a no-op for ELF objects opened in
-
.Dv ELF_C_READ
-
mode.
-
.Sh RETURN VALUES
-
Function
-
.Fn elf_cntl
-
returns 0 on success, or -1 if an error was detected.
-
.Sh ERRORS
-
.Bl -tag -width "[ELF_E_RESOURCE]"
-
.It Bq Er ELF_E_ARCHIVE
-
Argument
-
.Ar elf
-
is a descriptor for an archive member.
-
.It Bq Er ELF_E_ARGUMENT
-
Argument
-
.Ar elf
-
was NULL.
-
.It Bq Er ELF_E_ARGUMENT
-
Argument
-
.Ar cmd
-
was not recognized.
-
.It Bq Er ELF_E_MODE
-
An
-
.Dv ELF_C_FDREAD
-
operation was requested on an ELF descriptor opened
-
for writing.
-
.El
-
.Sh SEE ALSO
-
.Xr elf 3 ,
-
.Xr elf_begin 3 ,
-
.Xr elf_end 3 ,
-
.Xr elf_next 3 ,
-
.Xr elf_update 3 ,
-
.Xr gelf 3
deleted external/libelf/elf_cntl.c
@@ -1,58 +0,0 @@
-
/*-
-
 * 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 <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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;
-
}
deleted external/libelf/elf_data.c
@@ -1,269 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008,2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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 <assert.h>
-
#include <errno.h>
-
#include <libelf.h>
-
#include <stdlib.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	d = (struct _Libelf_Data *) ed;
-

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

-
	assert(e->e_kind == ELF_K_ELF);
-

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

-
	if (d != NULL)
-
		return (&STAILQ_NEXT(d, d_next)->d_data);
-

-
	if (e->e_rawfile == NULL) {
-
		/*
-
		 * In the ELF_C_WRITE case, there is no source that
-
		 * can provide data for the section.
-
		 */
-
		LIBELF_SET_ERROR(ARGUMENT, 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) {
-
		LIBELF_SET_ERROR(SECTION, 0);
-
		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);
-
	}
-

-
	if (sh_size / fsz > SIZE_MAX) {
-
		LIBELF_SET_ERROR(RANGE, 0);
-
		return (NULL);
-
	}
-

-
	count = (size_t) (sh_size / fsz);
-

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

-
	if (count > 0 && msz > SIZE_MAX / count) {
-
		LIBELF_SET_ERROR(RANGE, 0);
-
		return (NULL);
-
	}
-

-
	assert(msz > 0);
-
	assert(count <= SIZE_MAX);
-
	assert(msz * count <= SIZE_MAX);
-

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

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

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

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

-
	d->d_flags  |= LIBELF_F_DATA_MALLOCED;
-

-
	xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass);
-
	if (!(*xlate)(d->d_data.d_buf, (size_t) d->d_data.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->d_data);
-
}
-

-
Elf_Data *
-
elf_newdata(Elf_Scn *s)
-
{
-
	Elf *e;
-
	struct _Libelf_Data *d;
-

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

-
	assert(e->e_kind == ELF_K_ELF);
-

-
	/*
-
	 * 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_data.d_align = 1;
-
	d->d_data.d_buf = NULL;
-
	d->d_data.d_off = (uint64_t) ~0;
-
	d->d_data.d_size = 0;
-
	d->d_data.d_type = ELF_T_BYTE;
-
	d->d_data.d_version = LIBELF_PRIVATE(version);
-

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

-
	return (&d->d_data);
-
}
-

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

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

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

-
	assert(e->e_kind == ELF_K_ELF);
-

-
	d = (struct _Libelf_Data *) ed;
-

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

-
	if (d != NULL)
-
		return (&STAILQ_NEXT(d, d_next)->d_data);
-

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

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

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

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

-
	return (&d->d_data);
-
}
deleted external/libelf/elf_end.3
@@ -1,76 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd June 29, 2006
-
.Os
-
.Dt ELF_END 3
-
.Sh NAME
-
.Nm elf_end
-
.Nd release an ELF descriptor
-
.Sh LIBRARY
-
.Lb libelf
-
.Sh SYNOPSIS
-
.In libelf.h
-
.Ft int
-
.Fn elf_end "Elf *elf"
-
.Sh DESCRIPTION
-
Function
-
.Fn elf_end
-
is used to release the resources associated with an ELF descriptor
-
pointed to by argument
-
.Ar elf .
-
This descriptor must have been allocated by a previous call to
-
.Xr elf_begin 3
-
or
-
.Xr elf_memory 3 .
-
For programming convenience, a NULL value is permitted for argument
-
.Ar elf .
-
.Pp
-
A call to
-
.Fn elf_end
-
decrements the activation count for descriptor
-
.Ar elf
-
by one.
-
The resources associated with the descriptor are only released
-
with its activation count goes to zero.
-
.Pp
-
Once function
-
.Fn elf_end
-
returns zero, the ELF descriptor
-
.Ar elf
-
will no longer be valid and should not be used further.
-
.Sh RETURN VALUES
-
Function
-
.Fn elf_end
-
returns the current value of the ELF descriptor
-
.Ar elf Ap s
-
activation count, or zero if argument
-
.Ar elf
-
was NULL.
-
.Sh SEE ALSO
-
.Xr elf 3 ,
-
.Xr elf_begin 3 ,
-
.Xr elf_memory 3 ,
-
.Xr gelf 3
deleted external/libelf/elf_end.c
@@ -1,97 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008-2009,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 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>
-

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

-
#include "_libelf.h"
-

-
#if	ELFTC_HAVE_MMAP
-
#include <sys/mman.h>
-
#endif
-

-
ELFTC_VCSID("$Id$");
-

-
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_rawfile) {
-
			if (e->e_flags & LIBELF_F_RAWFILE_MALLOC)
-
				free(e->e_rawfile);
-
#if	ELFTC_HAVE_MMAP
-
			else if (e->e_flags & LIBELF_F_RAWFILE_MMAP)
-
				(void) munmap(e->e_rawfile, e->e_rawsize);
-
#endif
-
		}
-

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

-
	return (0);
-
}
deleted external/libelf/elf_errmsg.3
@@ -1,107 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd June 11, 2006
-
.Os
-
.Dt ELF_ERRMSG 3
-
.Sh NAME
-
.Nm elf_errmsg ,
-
.Nm elf_errno
-
.Nd ELF library error message handling
-
.Sh LIBRARY
-
.Lb libelf
-
.Sh SYNOPSIS
-
.In libelf.h
-
.Ft int
-
.Fn elf_errno "void"
-
.Ft "const char *"
-
.Fn elf_errmsg "int error"
-
.Sh DESCRIPTION
-
When an error occurs during an ELF library API call, the library
-
encodes the error using an error number and stores the error number
-
internally for retrieval by the application at a later point of time.
-
Error numbers may contain an OS supplied error code in addition to
-
an ELF API specific error code.
-
An error number value of zero indicates no error.
-
.Pp
-
Function
-
.Fn elf_errno
-
is used to retrieve the last error recorded by the ELF library.
-
Invoking this function has the side-effect of resetting the
-
ELF library's recorded error number to zero.
-
.Pp
-
The function
-
.Fn elf_errmsg
-
returns a null-terminated string with a human readable
-
description of the error specified in argument
-
.Ar error .
-
A zero value for argument
-
.Ar error
-
retrieves the most recent error encountered by the ELF
-
library.
-
An argument value of -1 behaves identically, except that
-
it guarantees a non-NULL return from
-
.Fn elf_errmsg .
-
.Sh RETURN VALUES
-
Function
-
.Fn elf_errno
-
returns a non-zero value encoding the last error encountered
-
by the ELF library, or zero if no error was encountered.
-
.Pp
-
Function
-
.Fn elf_errmsg
-
returns a pointer to library local storage for non-zero values
-
of argument
-
.Ar error .
-
With a zero argument, the function will return a NULL pointer if no
-
error had been encountered by the library, or will return a pointer to
-
library local storage containing an appropriate message otherwise.
-
.Sh EXAMPLES
-
Clearing the ELF library's recorded error number can be accomplished
-
by invoking
-
.Fn elf_errno
-
and discarding its return value.
-
.Bd -literal -offset indent
-
/* clear error */
-
(void) elf_errno();
-
.Ed
-
.Pp
-
Retrieving a human-readable description of the current error number
-
can be done with the following snippet:
-
.Bd -literal -offset indent
-
int err;
-
const char *errmsg;
-
\&...
-
err = elf_errno();
-
if (err != 0)
-
	errmsg = elf_errmsg(err);
-
.Ed
-
.Sh SEE ALSO
-
.Xr elf 3 ,
-
.Xr gelf 3
-
.Sh BUGS
-
Function
-
.Fn elf_errmsg
-
is not localized.
deleted external/libelf/elf_errmsg.c
@@ -1,85 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008,2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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>
-

-
#include <libelf.h>
-
#include <stdio.h>
-
#include <string.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * 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 == ELF_E_NONE &&
-
	    (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 < ELF_E_NONE || error >= ELF_E_NUM)
-
		return _libelf_errors[ELF_E_NUM];
-
	if (oserr) {
-
		(void) snprintf((char *) LIBELF_PRIVATE(msg),
-
		    sizeof(LIBELF_PRIVATE(msg)), "%s: %s",
-
		    _libelf_errors[error], strerror(oserr));
-
		return (const char *)&LIBELF_PRIVATE(msg);
-
	}
-
	return _libelf_errors[error];
-
}
deleted external/libelf/elf_errno.c
@@ -1,43 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008,2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	old = LIBELF_PRIVATE(error);
-
	LIBELF_PRIVATE(error) = 0;
-
	return (old & LIBELF_ELF_ERROR_MASK);
-
}
deleted external/libelf/elf_fill.3
@@ -1,52 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd June 11, 2006
-
.Os
-
.Dt ELF_FILL 3
-
.Sh NAME
-
.Nm elf_fill
-
.Nd set fill byte for inter-section padding
-
.Sh LIBRARY
-
.Lb libelf
-
.Sh SYNOPSIS
-
.In libelf.h
-
.Ft void
-
.Fn elf_fill "int fill"
-
.Sh DESCRIPTION
-
Function
-
.Fn elf_fill
-
allows an application to specify a fill value for the padding inserted
-
between two sections of an ELF file to meet section alignment
-
constraints.
-
By default the ELF library uses zero bytes for padding.
-
.Pp
-
The ELF library will only pad bytes if the
-
.Dv ELF_F_LAYOUT
-
flag is not set for the ELF file.
-
.Sh SEE ALSO
-
.Xr elf 3 ,
-
.Xr elf_flagelf 3 ,
-
.Xr gelf 3
deleted external/libelf/elf_fill.c
@@ -1,39 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
void
-
elf_fill(int fill)
-
{
-
	LIBELF_PRIVATE(fillchar) = fill;
-
}
deleted external/libelf/elf_flag.c
@@ -1,198 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008-2009,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 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
unsigned int
-
elf_flagarhdr(Elf_Arhdr *a, Elf_Cmd c, unsigned int flags)
-
{
-
	unsigned int r;
-

-
	if (a == 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 = a->ar_flags |= flags;
-
	else
-
		r = a->ar_flags &= ~flags;
-

-
	return (r & LIBELF_F_API_MASK);
-
}
-

-
unsigned int
-
elf_flagdata(Elf_Data *d, Elf_Cmd c, unsigned int flags)
-
{
-
	unsigned int r;
-
	struct _Libelf_Data *ld;
-

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

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

-
	ld = (struct _Libelf_Data *) d;
-

-
	if (c == ELF_C_SET)
-
		r = ld->d_flags |= flags;
-
	else
-
		r = ld->d_flags &= ~flags;
-

-
	return (r & LIBELF_F_API_MASK);
-
}
-

-
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)
-
{
-
	unsigned 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_ARCHIVE | ELF_F_ARCHIVE_SYSV |
-
	    ELF_F_DIRTY | ELF_F_LAYOUT)) != 0) {
-
		LIBELF_SET_ERROR(ARGUMENT, 0);
-
		return (0);
-
	}
-

-
	if ((flags & ELF_F_ARCHIVE_SYSV) && (flags & ELF_F_ARCHIVE) == 0) {
-
		LIBELF_SET_ERROR(ARGUMENT, 0);
-
		return (0);
-
	}
-

-
	if ((flags & ELF_F_ARCHIVE) && e->e_cmd != ELF_C_WRITE) {
-
		LIBELF_SET_ERROR(MODE, 0);
-
		return (0);
-
	}
-

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

-
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)
-
{
-
	unsigned 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 & LIBELF_F_API_MASK);
-
}
-

-
unsigned int
-
elf_flagshdr(Elf_Scn *s, Elf_Cmd c, unsigned int flags)
-
{
-
	return (elf_flagscn(s, c, flags));
-
}
deleted external/libelf/elf_flagdata.3
@@ -1,226 +0,0 @@
-
.\" Copyright (c) 2006-2008,2011 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd December 3, 2011
-
.Os
-
.Dt ELF_FLAGDATA 3
-
.Sh NAME
-
.Nm elf_flagarhdr ,
-
.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_flagarhdr "Elf_Arhdr *arhdr" "Elf_Cmd cmd" "unsigned int flags"
-
.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 arhdr ,
-
.Ar data ,
-
.Ar elf
-
and
-
.Ar scn
-
denote the data structures whose flags need to be changed.
-
These values should have been returned by prior calls to
-
functions in the ELF(3) API set:
-
.Bl -bullet -compact
-
.It
-
Argument
-
.Ar arhdr
-
should have been returned by a prior call to
-
.Xr elf_getarhdr 3 .
-
.It
-
Argument
-
.Ar data
-
should have been returned by a prior call to one of
-
.Xr elf_newdata 3 ,
-
.Xr elf_getdata 3
-
or
-
.Xr elf_rawdata 3 .
-
.It
-
Argument
-
.Ar elf
-
should have been allocated by a prior call to one of
-
.Xr elf_begin 3
-
or
-
.Xr elf_memory 3 .
-
.It
-
Argument
-
.Ar scn
-
should have been returned by a prior call to one of
-
.Xr elf_getscn 3 ,
-
.Xr elf_newscn 3
-
or
-
.Xr elf_nextscn 3 .
-
.El
-
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_ARCHIVE_SYSV
-
.It Dv ELF_F_ARCHIVE
-
This flag is only valid with the
-
.Fn elf_flagelf
-
API.
-
It informs the library that the application desires to create an
-
.Xr ar 1
-
archive.
-
Argument
-
.Ar elf
-
should have been opened for writing using the
-
.Dv ELF_C_WRITE
-
command to function
-
.Fn elf_begin .
-
.It Dv ELF_F_ARCHIVE_SYSV
-
This flag is used in conjunction with the
-
.Dv ELF_F_ARCHIVE
-
flag to indicate that library should create archives that conform
-
to System V layout rules.
-
The default is to create BSD style archives.
-
.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 if successful, or zero if
-
an error is detected.
-
.Sh COMPATIBILITY
-
The
-
.Fn elf_flagarhdr
-
function and the
-
.Dv ELF_F_ARCHIVE
-
and
-
.Dv ELF_F_ARCHIVE_SYSV
-
flags are an extension to the ELF(3) API.
-
.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_MODE
-
The
-
.Dv ELF_F_ARCHIVE
-
flag was used with an ELF descriptor that had not been opened for writing.
-
.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
deleted external/libelf/elf_getarhdr.3
@@ -1,97 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 15, 2006
-
.Os
-
.Dt ELF_GETARHDR 3
-
.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
deleted external/libelf/elf_getarhdr.c
@@ -1,47 +0,0 @@
-
/*-
-
 * 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 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
Elf_Arhdr *
-
elf_getarhdr(Elf *e)
-
{
-
	if (e == NULL) {
-
		LIBELF_SET_ERROR(ARGUMENT, 0);
-
		return (NULL);
-
	}
-

-
	if (e->e_flags & LIBELF_F_AR_HEADER)
-
		return (e->e_hdr.e_arhdr);
-

-
	return (_libelf_ar_gethdr(e));
-
}
deleted external/libelf/elf_getarsym.3
@@ -1,130 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 15, 2006
-
.Os
-
.Dt ELF_GETARSYM 3
-
.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
deleted external/libelf/elf_getarsym.c
@@ -1,58 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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 = (ar->e_flags & LIBELF_F_AR_VARIANT_SVR4) ?
-
		    _libelf_ar_process_svr4_symtab(ar, &n) :
-
		    _libelf_ar_process_bsd_symtab(ar, &n);
-
	else
-
		LIBELF_SET_ERROR(ARCHIVE, 0);
-

-
	if (ptr)
-
		*ptr = n;
-
	return (symtab);
-
}
deleted external/libelf/elf_getbase.3
@@ -1,71 +0,0 @@
-
.\" 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.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd June 6, 2010
-
.Os
-
.Dt ELF_GETBASE 3
-
.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
deleted external/libelf/elf_getbase.c
@@ -1,48 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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));
-
}
deleted external/libelf/elf_getdata.3
@@ -1,229 +0,0 @@
-
.\" 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.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd January 26, 2011
-
.Os
-
.Dt ELF_GETDATA 3
-
.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
-
Either of the arguments
-
.Ar scn
-
or
-
.Ar data
-
was NULL.
-
.It Bq Er ELF_E_ARGUMENT
-
The data descriptor referenced by argument
-
.Ar data
-
is not associated with section descriptor
-
.Ar scn .
-
.It Bq Er ELF_E_ARGUMENT
-
The section denoted by argument
-
.Ar scn
-
had no data associated with it.
-
.It Bq Er ELF_E_DATA
-
Retrieval of data from the underlying object failed.
-
.It Bq Er ELF_E_RESOURCE
-
An out of memory condition was detected.
-
.It Bq Er ELF_E_SECTION
-
Section
-
.Ar scn
-
had type
-
.Dv SHT_NULL .
-
.It Bq Er ELF_E_SECTION
-
The type of the section
-
.Ar scn
-
was not recognized by the library.
-
.It Bq Er ELF_E_SECTION
-
The size of the section
-
.Ar scn
-
is not a multiple of the file size for its section type.
-
.It Bq Er ELF_E_SECTION
-
The file offset for section
-
.Ar scn
-
is incorrect.
-
.It Bq Er ELF_E_UNIMPL
-
The section type associated with section
-
.Ar scn
-
is currently unsupported by the library.
-
.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
deleted external/libelf/elf_getident.3
@@ -1,83 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd July 3, 2006
-
.Os
-
.Dt ELF_GETIDENT 3
-
.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
deleted external/libelf/elf_getident.c
@@ -1,68 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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 ((char *) e->e_rawfile);
-

-
 error:
-
	if (sz)
-
		*sz = 0;
-
	return (NULL);
-
}
deleted external/libelf/elf_getphdrnum.3
@@ -1,86 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 5, 2009
-
.Os
-
.Dt ELF_GETPHDRNUM 3
-
.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.
-
.Pp
-
.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
deleted external/libelf/elf_getphnum.3
@@ -1,93 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 5, 2009
-
.Os
-
.Dt ELF_GETPHNUM 3
-
.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.
-
.Pp
-
.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
deleted external/libelf/elf_getscn.3
@@ -1,151 +0,0 @@
-
.\" Copyright (c) 2006-2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd October 22, 2007
-
.Os
-
.Dt ELF_GETSCN 3
-
.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
deleted external/libelf/elf_getshdrnum.3
@@ -1,78 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 4, 2009
-
.Os
-
.Dt ELF_GETSHDRNUM 3
-
.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.
-
.Pp
-
.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
deleted external/libelf/elf_getshdrstrndx.3
@@ -1,79 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 5, 2009
-
.Os
-
.Dt ELF_GETSHDRSTRNDX 3
-
.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.
-
.Pp
-
.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
deleted external/libelf/elf_getshnum.3
@@ -1,84 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 5, 2009
-
.Os
-
.Dt ELF_GETSHNUM 3
-
.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.
-
.Pp
-
.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
deleted external/libelf/elf_getshstrndx.3
@@ -1,94 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 5, 2009
-
.Os
-
.Dt ELF_GETSHSTRNDX 3
-
.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.
-
.Pp
-
.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
deleted external/libelf/elf_hash.3
@@ -1,57 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 15, 2006
-
.Os
-
.Dt ELF_HASH 3
-
.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
deleted external/libelf/elf_hash.c
@@ -1,56 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * This elf_hash function is defined by the System V ABI.
-
 */
-

-
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);
-
}
deleted external/libelf/elf_kind.3
@@ -1,71 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd June 1, 2006
-
.Os
-
.Dt ELF_KIND 3
-
.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
deleted external/libelf/elf_kind.c
@@ -1,44 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/elf_memory.3
@@ -1,122 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd June 28, 2006
-
.Os
-
.Dt ELF_MEMORY 3
-
.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 ELF object referenced by argument
-
.Ar image
-
was of an unsupported ELF 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
deleted external/libelf/elf_memory.c
@@ -1,47 +0,0 @@
-
/*-
-
 * 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 <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
Elf *
-
elf_memory(char *image, size_t sz)
-
{
-
	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);
-
	}
-

-
	return (_libelf_memory((unsigned char *) image, sz, 1));
-
}
deleted external/libelf/elf_next.3
@@ -1,96 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd June 17, 2006
-
.Os
-
.Dt ELF_NEXT 3
-
.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
deleted external/libelf/elf_next.c
@@ -1,66 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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(e->e_rawfile > parent->e_rawfile);
-

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

-
	/*
-
	 * Setup the 'e_next' field of the archive descriptor for the
-
	 * next call to 'elf_begin()'.
-
	 */
-
	parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ?
-
	    (off_t) 0 : next;
-

-
	return (ELF_C_READ);
-
}
deleted external/libelf/elf_open.3
@@ -1,121 +0,0 @@
-
.\" Copyright (c) 2012 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$ 
-
.\"
-
.Dd May 31, 2012
-
.Os
-
.Dt ELF_OPEN 3 
-
.Sh NAME
-
.Nm elf_open
-
.Nd open ELF objects and ar(1) archives
-
.Sh LIBRARY
-
.Lb libelf
-
.Sh SYNOPSIS
-
.In libelf.h
-
.Ft "Elf *"
-
.Fn elf_open "int fd"
-
.Ft "Elf *"
-
.Fn elf_openmemory "char *image" "size_t sz"
-
.Sh DESCRIPTION
-
.Em Important :
-
The functions
-
.Fn elf_open
-
and
-
.Fn elf_openmemory
-
are extensions to the ELF(3) API, for the internal use of the
-
Elftoolchain project.
-
Portable applications should not use these functions.
-
.Pp
-
The function
-
.Fn elf_open
-
returns an Elf descriptor opened with mode
-
.Dv ELF_C_READ
-
for the ELF object or
-
.Xr ar 1
-
archive referenced by the file descriptor in argument
-
.Ar fd .
-
.Pp
-
The function
-
.Fn elf_openmemory
-
returns an ELF descriptor opened with mode
-
.Dv ELF_C_READ
-
for the ELF object or
-
.Xr ar 1
-
archive contained in the memory area pointed to by the argument
-
.Ar image .
-
The argument
-
.Ar sz
-
specifies the size of the memory area in bytes.
-
.Sh COMPATIBILITY
-
These functions are non-standard extensions to the ELF(3) API set.
-
.Pp
-
The behavior of these functions differs from their counterparts
-
.Xr elf_begin 3
-
and
-
.Xr elf_memory 3
-
in that these functions will successfully open malformed ELF objects
-
and
-
.Xr ar 1
-
archives, returning an Elf descriptor of type
-
.Dv ELF_K_NONE .
-
.Sh RETURN VALUES
-
The function returns a pointer to a ELF descriptor if successful, or
-
NULL if an error occurred.
-
.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 fd
-
was of an unsupported file type.
-
.It Bq Er ELF_E_ARGUMENT
-
The argument
-
.Ar sz
-
was zero, or the argument
-
.Ar image
-
was NULL.
-
.It Bq Er ELF_E_IO
-
The file descriptor in argument
-
.Ar fd
-
was invalid.
-
.It Bq Er ELF_E_IO
-
The file descriptor in argument
-
.Ar fd
-
could not be read.
-
.It Bq Er ELF_E_RESOURCE
-
An out of memory condition was encountered.
-
.It Bq Er ELF_E_SEQUENCE
-
Functions
-
.Fn elf_open
-
or
-
.Fn elf_openmemory
-
was called before a working version was established with
-
.Xr elf_version 3 .
-
.El
-
.Sh SEE ALSO
-
.Xr elf 3 ,
-
.Xr elf_begin 3 ,
-
.Xr elf_errno 3 ,
-
.Xr elf_memory 3 ,
-
.Xr gelf 3
deleted external/libelf/elf_open.c
@@ -1,67 +0,0 @@
-
/*-
-
 * Copyright (c) 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 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 <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * Extension API: open a file for reading, ignoring parse errors.
-
 */
-

-
Elf *
-
elf_open(int fd)
-
{
-
	if (LIBELF_PRIVATE(version) == EV_NONE) {
-
		LIBELF_SET_ERROR(SEQUENCE, 0);
-
		return (NULL);
-
	}
-

-
	return (_libelf_open_object(fd, ELF_C_READ, 0));
-
}
-

-
/*
-
 * Extension API: create an ELF descriptor for an in-memory object,
-
 * ignoring parse errors.
-
 */
-

-
Elf *
-
elf_openmemory(char *image, size_t sz)
-
{
-
	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);
-
	}
-

-
	return (_libelf_memory((unsigned char *) image, sz, 0));
-
}
deleted external/libelf/elf_phnum.c
@@ -1,67 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/elf_rand.3
@@ -1,118 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd June 17, 2006
-
.Os
-
.Dt ELF_RAND 3
-
.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
deleted external/libelf/elf_rand.c
@@ -1,59 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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 ||
-
	    (size_t) 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);
-
}
deleted external/libelf/elf_rawfile.3
@@ -1,76 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd July 3, 2006
-
.Os
-
.Dt ELF_RAWFILE 3
-
.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
deleted external/libelf/elf_rawfile.c
@@ -1,53 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	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 ((char *) ptr);
-
}
deleted external/libelf/elf_scn.c
@@ -1,235 +0,0 @@
-
/*-
-
 * 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 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>
-
#include <sys/queue.h>
-

-
#include <assert.h>
-
#include <errno.h>
-
#include <gelf.h>
-
#include <libelf.h>
-
#include <stddef.h>
-
#include <stdlib.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * Load an ELF section table and create a list of Elf_Scn structures.
-
 */
-
int
-
_libelf_load_section_headers(Elf *e, void *ehdr)
-
{
-
	Elf_Scn *scn;
-
	uint64_t shoff;
-
	Elf32_Ehdr *eh32;
-
	Elf64_Ehdr *eh64;
-
	int ec, swapbytes;
-
	unsigned char *src;
-
	size_t fsz, i, shnum;
-
	int (*xlator)(unsigned char *_d, size_t _dsz, unsigned 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 (shoff > e->e_rawsize ||			\
-
		    fsz != (EH)->e_shentsize ||			\
-
		    shnum > SIZE_MAX / fsz ||			\
-
		    fsz * shnum > e->e_rawsize - shoff) {	\
-
			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)((unsigned 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_section_headers(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_section_headers(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));
-
}
deleted external/libelf/elf_shnum.c
@@ -1,67 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/elf_shstrndx.c
@@ -1,82 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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));
-
}
deleted external/libelf/elf_strptr.3
@@ -1,116 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd December 16, 2006
-
.Os
-
.Dt ELF_STRPTR 3
-
.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
deleted external/libelf/elf_strptr.c
@@ -1,136 +0,0 @@
-
/*-
-
 * 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/param.h>
-

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

-
#include "_libelf.h"
-

-
#ifndef roundup2
-
#define	roundup2(x, y)	(((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
-
#endif
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * 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;
-
	GElf_Shdr shdr;
-
	uint64_t alignment, count;
-

-
	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 = (uint64_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);
-
}
deleted external/libelf/elf_types.m4
@@ -1,309 +0,0 @@
-
/*-
-
 * 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.
-
 *
-
 * $Id$
-
 */
-

-
/*
-
 * ELF types, defined in the "enum Elf_Type" API.
-
 *
-
 * The members of the list form a 2-tuple: (name, C-type-suffix).
-
 * + `name' is an Elf_Type symbol without the `ELF_T_' prefix.
-
 * + `C-type-suffix' is the suffix for Elf32_ and Elf64_ type names.
-
 */
-

-
define(`ELF_TYPE_LIST',
-
	``ADDR,		Addr',
-
	`BYTE,		Byte',
-
	`CAP,		Cap',
-
	`DYN,		Dyn',
-
	`EHDR,		Ehdr',
-
	`GNUHASH,	-',
-
	`HALF,		Half',
-
	`LWORD,		Lword',
-
	`MOVE,		Move',
-
	`MOVEP,		MoveP',
-
	`NOTE,		Note',
-
	`OFF,		Off',
-
	`PHDR,		Phdr',
-
	`REL,		Rel',
-
	`RELA,		Rela',
-
	`SHDR,		Shdr',
-
	`SWORD,		Sword',
-
	`SXWORD,	Sxword',
-
	`SYMINFO,	Syminfo',
-
	`SYM,		Sym',
-
	`VDEF,		Verdef',
-
	`VNEED,		Verneed',
-
	`WORD,		Word',
-
	`XWORD,		Xword',
-
	`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',
-
	`_,_'')
deleted external/libelf/elf_update.3
@@ -1,378 +0,0 @@
-
.\" Copyright (c) 2006-2011 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 14, 2011
-
.Os
-
.Dt ELF_UPDATE 3
-
.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
-
should reference a valid ELF descriptor.
-
.Pp
-
Argument
-
.Ar cmd
-
can be one of the following values:
-
.Bl -tag -width "Dv 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 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.
-
The ELF descriptor referenced by argument
-
.Ar elf
-
should permit the underlying ELF object to be written or updated
-
(see
-
.Xr elf_begin 3 ) .
-
.El
-
.Pp
-
All pointers to
-
.Vt Elf_Scn
-
and
-
.Vt Elf_Data
-
descriptors associated with descriptor
-
.Ar elf
-
should be considered invalid after a call to
-
.Fn elf_update .
-
.Ss Specifying Object Layout
-
The
-
.Lb libelf
-
supports two layout modes.
-
.Bl -tag -width indent
-
.It "Library Layout"
-
If the
-
.Dv ELF_F_LAYOUT
-
flag is not set on the ELF descriptor, the ELF library will lay out
-
the ELF object according to the following scheme:
-
.Bl -tag -compact -width "Section Data"
-
.It Em EHDR
-
The ELF executable header will be placed at the start of the object.
-
.It Em PHDR
-
If the ELF descriptor contains a program header table, it will be
-
placed after the Executable Header.
-
.It Em Section Data
-
ELF section data, if any, will be placed next, keeping each section's
-
alignment requirements in mind.
-
.It Em SHDR
-
The ELF section header table, if any, will be placed last.
-
.El
-
.It "Application Controlled Layout"
-
The application can take full control of the layout of the ELF object
-
by setting the
-
.Dv ELF_F_LAYOUT
-
flag on the ELF descriptor (see
-
.Xr elf_flagelf 3 ) .
-
In this case the library will lay out the ELF object using
-
application-supplied information as below:
-
.Pp
-
.Bl -tag -compact -width "Section Data"
-
.It Em EHDR
-
The ELF executable header will be placed at the start of the object.
-
.It Em PHDR
-
The ELF program header table, if any, it will be placed at the offset
-
specified in the
-
.Va e_phoff
-
field of the ELF executable header.
-
.It Em Section Data
-
The data for each ELF section will be placed at the offset specified
-
by the
-
.Va sh_offset
-
field of the section's header.
-
The size of the section will be taken from the
-
.Va sh_size
-
field of the section header.
-
.It Em SHDR
-
The ELF section header table, if any, will be placed at the offset
-
specified by the
-
.Va e_shoff
-
field of the executable header.
-
.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 .
-
.Ss Application Supplied Information
-
The application needs to set the following fields in the data
-
structures associated with the ELF descriptor prior to calling
-
.Fn elf_update .
-
.Bl -tag -width indent
-
.It "Executable Header"
-
The fields of the ELF executable header that need to be set by the
-
application are:
-
.Pp
-
.Bl -tag -width "e_ident[EI_OSABI]" -compact
-
.It Va e_entry
-
To be set to the desired entry address for executables.
-
.It Va e_flags
-
To be 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]"
-
To be set to the OS ABI desired.
-
For example, for
-
.Fx
-
executables, this field should be set to
-
.Dv ELFOSABI_FREEBSD .
-
.It Va e_machine
-
To be set to the desired machine architecture, one of the
-
.Dv EM_*
-
values in the header file
-
.In elfdefinitions.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
-
To be set to the index of the string table containing
-
section names.
-
.It Va e_type
-
To be set to the type of the ELF object, one of the
-
.Dv ET_*
-
values in the header file
-
.In elfdefinitions.h .
-
.It Va e_version
-
To be set to the desired version of the ELF object.
-
.El
-
.It "Program Header"
-
All fields of the entries in the program header table need to be
-
set by the application.
-
.It "Section Header"
-
The fields of ELF section headers that need to be set by the
-
application are:
-
.Pp
-
.Bl -tag -width "sh_addralign" -compact
-
.It Va sh_addr
-
To be set to the 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 and must be at least as large as the
-
largest alignment needed by any
-
.Vt Elf_Data
-
descriptor associated with the section.
-
.It Va sh_entsize
-
To be 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 are known to the library.
-
.It Va sh_flags
-
To be set to the desired section flags.
-
.It Va sh_info
-
To be set as described in
-
.Xr elf 5 .
-
.It Va sh_link
-
To be set as described in
-
.Xr elf 5 .
-
.It Va sh_name
-
To be 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
-
To be set to the type of the section.
-
.El
-
.It "Section Data"
-
The
-
.Vt Elf_Data
-
descriptors associated with each section specify its contents
-
(see
-
.Xr elf_getdata 3 ) .
-
While all the fields in these descriptors are under application
-
control, the following fields influence object layout:
-
.Bl -tag -width "Va d_align" -compact
-
.It Va d_align
-
To be set to the desired alignment, within the containing section, of
-
the descriptor's data.
-
.It Va d_off
-
If the application is managing object layout, it must set this field
-
to the file offset, within the section, at which the descriptor's data
-
should be placed.
-
.It Va d_size
-
To be set to the size in bytes of the memory representation of the
-
descriptor's data.
-
.El
-
.El
-
.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 an unsupported type.
-
.It Bq Er ELF_E_DATA
-
An
-
.Vt Elf_Data
-
descriptor specified an alignment that was zero or was not a power of
-
two.
-
.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 executable header overlapped
-
with the program header table.
-
.It Bq Er ELF_E_LAYOUT
-
The flag
-
.Dv ELF_F_LAYOUT
-
was set on the Elf descriptor and the program header table was placed
-
at a misaligned file offset.
-
.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 mapped by a section descriptor.
-
.It Bq Er ELF_E_LAYOUT
-
The
-
.Dv ELF_F_LAYOUT
-
flag was set on the Elf descriptor, and the
-
.Va d_offset
-
field in an
-
.Vt Elf_Data
-
descriptor contained a value that was not a multiple of the
-
descriptor's specified alignment.
-
.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_begin 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
deleted external/libelf/elf_update.c
@@ -1,1217 +0,0 @@
-
/*-
-
 * Copyright (c) 2006-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 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/param.h>
-
#include <sys/stat.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"
-

-
#if	ELFTC_HAVE_MMAP
-
#include <sys/mman.h>
-
#endif
-

-
#ifndef roundup2
-
#define	roundup2(x, y)	(((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
-
#endif
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * Layout strategy:
-
 *
-
 * - Case 1: ELF_F_LAYOUT is asserted
-
 *     In this case the application has full control over where the
-
 *     section header table, program header table, and section data
-
 *     will reside.   The library only perform error checks.
-
 *
-
 * - Case 2: ELF_F_LAYOUT is not asserted
-
 *
-
 *     The library will do the object layout using the following
-
 *     ordering:
-
 *     - The executable header is placed first, are required by the
-
 *     	 ELF specification.
-
 *     - The program header table is placed immediately following the
-
 *       executable header.
-
 *     - Section data, if any, is placed after the program header
-
 *       table, aligned appropriately.
-
 *     - The section header table, if needed, is placed last.
-
 *
-
 *     There are two sub-cases to be taken care of:
-
 *
-
 *     - Case 2a: e->e_cmd == ELF_C_READ or ELF_C_RDWR
-
 *
-
 *       In this sub-case, the underlying ELF object may already have
-
 *       content in it, which the application may have modified.  The
-
 *       library will retrieve content from the existing object as
-
 *       needed.
-
 *
-
 *     - Case 2b: e->e_cmd == ELF_C_WRITE
-
 *
-
 *       The ELF object is being created afresh in this sub-case;
-
 *       there is no pre-existing content in the underlying ELF
-
 *       object.
-
 */
-

-
/*
-
 * The types of extents in an ELF object.
-
 */
-
enum elf_extent {
-
	ELF_EXTENT_EHDR,
-
	ELF_EXTENT_PHDR,
-
	ELF_EXTENT_SECTION,
-
	ELF_EXTENT_SHDR
-
};
-

-
/*
-
 * A extent descriptor, used when laying out an ELF object.
-
 */
-
struct _Elf_Extent {
-
	SLIST_ENTRY(_Elf_Extent) ex_next;
-
	uint64_t	ex_start; /* Start of the region. */
-
	uint64_t	ex_size;  /* The size of the region. */
-
	enum elf_extent	ex_type;  /* Type of region. */
-
	void		*ex_desc; /* Associated descriptor. */
-
};
-

-
SLIST_HEAD(_Elf_Extent_List, _Elf_Extent);
-

-
/*
-
 * Compute the extents of a section, by looking at the data
-
 * descriptors associated with it.  The function returns 1
-
 * if successful, or zero if an error was detected.
-
 */
-
static int
-
_libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc)
-
{
-
	Elf_Data *d;
-
	size_t fsz, msz;
-
	int ec, elftype;
-
	uint32_t sh_type;
-
	uint64_t d_align;
-
	Elf32_Shdr *shdr32;
-
	Elf64_Shdr *shdr64;
-
	struct _Libelf_Data *ld;
-
	uint64_t scn_size, scn_alignment;
-
	uint64_t sh_align, sh_entsize, sh_offset, sh_size;
-

-
	ec = e->e_class;
-

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

-
	assert(sh_type != SHT_NULL && sh_type != SHT_NOBITS);
-

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

-
	if (sh_align == 0)
-
		sh_align = _libelf_falign(elftype, ec);
-

-
	/*
-
	 * Compute the section's size and alignment using the data
-
	 * descriptors associated with the section.
-
	 */
-
	if (STAILQ_EMPTY(&s->s_data)) {
-
		/*
-
		 * The section's content (if any) has not been read in
-
		 * yet.  If section is not dirty marked dirty, we can
-
		 * reuse the values in the 'sh_size' and 'sh_offset'
-
		 * fields of the section header.
-
		 */
-
		if ((s->s_flags & ELF_F_DIRTY) == 0) {
-
			/*
-
			 * If the library is doing the layout, then we
-
			 * compute the new start offset for the
-
			 * section based on the current offset and the
-
			 * section's alignment needs.
-
			 *
-
			 * If the application is doing the layout, we
-
			 * can use the value in the 'sh_offset' field
-
			 * in the section header directly.
-
			 */
-
			if (e->e_flags & ELF_F_LAYOUT)
-
				goto updatedescriptor;
-
			else
-
				goto computeoffset;
-
		}
-

-
		/*
-
		 * Otherwise, we need to bring in the section's data
-
		 * from the underlying ELF object.
-
		 */
-
		if (e->e_cmd != ELF_C_WRITE && elf_getdata(s, NULL) == NULL)
-
			return (0);
-
	}
-

-
	/*
-
	 * Loop through the section's data descriptors.
-
	 */
-
	scn_size = 0L;
-
	scn_alignment = 0;
-
	STAILQ_FOREACH(ld, &s->s_data, d_next)  {
-

-
		d = &ld->d_data;
-

-
		/*
-
		 * The data buffer's type is known.
-
		 */
-
		if (d->d_type >= ELF_T_NUM) {
-
			LIBELF_SET_ERROR(DATA, 0);
-
			return (0);
-
		}
-

-
		/*
-
		 * The data buffer's version is supported.
-
		 */
-
		if (d->d_version != e->e_version) {
-
			LIBELF_SET_ERROR(VERSION, 0);
-
			return (0);
-
		}
-

-
		/*
-
		 * The buffer's alignment is non-zero and a power of
-
		 * two.
-
		 */
-
		if ((d_align = d->d_align) == 0 ||
-
		    (d_align & (d_align - 1))) {
-
			LIBELF_SET_ERROR(DATA, 0);
-
			return (0);
-
		}
-

-
		/*
-
		 * The buffer's size should be a multiple of the
-
		 * memory size of the underlying type.
-
		 */
-
		msz = _libelf_msize(d->d_type, ec, e->e_version);
-
		if (d->d_size % msz) {
-
			LIBELF_SET_ERROR(DATA, 0);
-
			return (0);
-
		}
-

-
		/*
-
		 * If the application is controlling layout, then the
-
		 * d_offset field should be compatible with the
-
		 * buffer's specified alignment.
-
		 */
-
		if ((e->e_flags & ELF_F_LAYOUT) &&
-
		    (d->d_off & (d_align - 1))) {
-
			LIBELF_SET_ERROR(LAYOUT, 0);
-
			return (0);
-
		}
-

-
		/*
-
		 * Compute the section's size.
-
		 */
-
		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;
-
			fsz = _libelf_fsize(d->d_type, ec, d->d_version,
-
			    (size_t) d->d_size / msz);
-
			scn_size += fsz;
-
		}
-

-
		/*
-
		 * The section's alignment is the maximum alignment
-
		 * needed for its data buffers.
-
		 */
-
		if (d_align > scn_alignment)
-
			scn_alignment = d_align;
-
	}
-

-

-
	/*
-
	 * If the application is requesting full control over the
-
	 * layout of the section, check the section's specified size,
-
	 * offsets and alignment 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);
-
		}
-
		goto updatedescriptor;
-
	}
-

-
	/*
-
	 * Otherwise, compute the values in the section header.
-
	 *
-
	 * The section alignment is the maximum alignment for any of
-
	 * its contained data descriptors.
-
	 */
-
	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;
-

-
computeoffset:
-
	/*
-
	 * Compute the new offset for the section based on
-
	 * the section's alignment needs.
-
	 */
-
	sh_offset = roundup((uint64_t) rc, sh_align);
-

-
	/*
-
	 * Update the section header.
-
	 */
-
	if (ec == ELFCLASS32) {
-
		shdr32->sh_addralign = (uint32_t) sh_align;
-
		shdr32->sh_entsize   = (uint32_t) sh_entsize;
-
		shdr32->sh_offset    = (uint32_t) sh_offset;
-
		shdr32->sh_size      = (uint32_t) sh_size;
-
	} else {
-
		shdr64->sh_addralign = sh_align;
-
		shdr64->sh_entsize   = sh_entsize;
-
		shdr64->sh_offset    = sh_offset;
-
		shdr64->sh_size      = sh_size;
-
	}
-

-
updatedescriptor:
-
	/*
-
	 * Update the section descriptor.
-
	 */
-
	s->s_size = sh_size;
-
	s->s_offset = sh_offset;
-

-
	return (1);
-
}
-

-
/*
-
 * Free a list of extent descriptors.
-
 */
-

-
static void
-
_libelf_release_extents(struct _Elf_Extent_List *extents)
-
{
-
	struct _Elf_Extent *ex;
-

-
	while ((ex = SLIST_FIRST(extents)) != NULL) {
-
		SLIST_REMOVE_HEAD(extents, ex_next);
-
		free(ex);
-
	}
-
}
-

-
/*
-
 * Check if an extent 's' defined by [start..start+size) is free.
-
 * This routine assumes that the given extent list is sorted in order
-
 * of ascending extent offsets.
-
 */
-

-
static int
-
_libelf_extent_is_unused(struct _Elf_Extent_List *extents,
-
    const uint64_t start, const uint64_t size, struct _Elf_Extent **prevt)
-
{
-
	uint64_t tmax, tmin;
-
	struct _Elf_Extent *t, *pt;
-
	const uint64_t smax = start + size;
-

-
	/* First, look for overlaps with existing extents. */
-
	pt = NULL;
-
	SLIST_FOREACH(t, extents, ex_next) {
-
		tmin = t->ex_start;
-
		tmax = tmin + t->ex_size;
-

-
		if (tmax <= start) {
-
			/*
-
			 * 't' lies entirely before 's': ...| t |...| s |...
-
			 */
-
			pt = t;
-
			continue;
-
		} else if (smax <= tmin) {
-
			/*
-
			 * 's' lies entirely before 't', and after 'pt':
-
			 *      ...| pt |...| s |...| t |...
-
			 */
-
			assert(pt == NULL ||
-
			    pt->ex_start + pt->ex_size <= start);
-
			break;
-
		} else
-
			/* 's' and 't' overlap. */
-
			return (0);
-
	}
-

-
	if (prevt)
-
		*prevt = pt;
-
	return (1);
-
}
-

-
/*
-
 * Insert an extent into the list of extents.
-
 */
-

-
static int
-
_libelf_insert_extent(struct _Elf_Extent_List *extents, int type,
-
    uint64_t start, uint64_t size, void *desc)
-
{
-
	struct _Elf_Extent *ex, *prevt;
-

-
	assert(type >= ELF_EXTENT_EHDR && type <= ELF_EXTENT_SHDR);
-

-
	prevt = NULL;
-

-
	/*
-
	 * If the requested range overlaps with an existing extent,
-
	 * signal an error.
-
	 */
-
	if (!_libelf_extent_is_unused(extents, start, size, &prevt)) {
-
		LIBELF_SET_ERROR(LAYOUT, 0);
-
		return (0);
-
	}
-

-
	/* Allocate and fill in a new extent descriptor. */
-
	if ((ex = malloc(sizeof(struct _Elf_Extent))) == NULL) {
-
		LIBELF_SET_ERROR(RESOURCE, errno);
-
		return (0);
-
	}
-
	ex->ex_start = start;
-
	ex->ex_size = size;
-
	ex->ex_desc = desc;
-
	ex->ex_type = type;
-

-
	/* Insert the region descriptor into the list. */
-
	if (prevt)
-
		SLIST_INSERT_AFTER(prevt, ex, ex_next);
-
	else
-
		SLIST_INSERT_HEAD(extents, ex, ex_next);
-
	return (1);
-
}
-

-
/*
-
 * Recompute section layout.
-
 */
-

-
static off_t
-
_libelf_resync_sections(Elf *e, off_t rc, struct _Elf_Extent_List *extents)
-
{
-
	int ec;
-
	Elf_Scn *s;
-
	size_t sh_type;
-

-
	ec = e->e_class;
-

-
	/*
-
	 * Make a pass through sections, computing the extent of each
-
	 * section.
-
	 */
-
	STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) {
-
		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 (_libelf_compute_section_extents(e, s, rc) == 0)
-
			return ((off_t) -1);
-

-
		if (s->s_size == 0)
-
			continue;
-

-
		if (!_libelf_insert_extent(extents, ELF_EXTENT_SECTION,
-
		    s->s_offset, s->s_size, s))
-
			return ((off_t) -1);
-

-
		if ((size_t) rc < s->s_offset + s->s_size)
-
			rc = (off_t) (s->s_offset + s->s_size);
-
	}
-

-
	return (rc);
-
}
-

-
/*
-
 * Recompute the layout of the ELF object and update the internal data
-
 * structures associated with the ELF descriptor.
-
 *
-
 * 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 extents in the ELF object are sorted in order of ascending
-
 *   addresses.  Sections have their section header table entries
-
 *   updated.  An error is signalled if an overlap was detected among
-
 *   extents.
-
 * - Data descriptors associated with sections are checked for valid
-
 *   types, offsets and alignment.
-
 *
-
 * After a resync_elf() successfully returns, the ELF descriptor is
-
 * ready for being handed over to _libelf_write_elf().
-
 */
-

-
static off_t
-
_libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents)
-
{
-
	int ec, eh_class;
-
	unsigned int eh_byteorder, eh_version;
-
	size_t align, fsz;
-
	size_t phnum, shnum;
-
	off_t rc, phoff, shoff;
-
	void *ehdr, *phdr;
-
	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        = (off_t) eh32->e_phoff;
-
		shoff        = (off_t) eh32->e_shoff;
-
		eh_version   = eh32->e_version;
-
	} else {
-
		eh_byteorder = eh64->e_ident[EI_DATA];
-
		eh_class     = eh64->e_ident[EI_CLASS];
-
		phoff        = (off_t) eh64->e_phoff;
-
		shoff        = (off_t) eh64->e_shoff;
-
		eh_version   = eh64->e_version;
-
	}
-

-
	if (phoff < 0 || shoff < 0) {
-
		LIBELF_SET_ERROR(HEADER, 0);
-
		return ((off_t) -1);
-
	}
-

-
	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 {					\
-
		unsigned int _version = (unsigned int) (V);		\
-
		(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] = (unsigned char) (EC);		\
-
		(E)->e_ident[EI_VERSION] = (_version & 0xFFU);		\
-
		(E)->e_ehsize = (uint16_t) _libelf_fsize(ELF_T_EHDR,	\
-
		    (EC), _version, (size_t) 1);			\
-
		(E)->e_phentsize = (uint16_t) ((phnum == 0) ? 0 :	\
-
		    _libelf_fsize(ELF_T_PHDR, (EC), _version,		\
-
			(size_t) 1));					\
-
		(E)->e_shentsize = (uint16_t) _libelf_fsize(ELF_T_SHDR,	\
-
		    (EC), _version, (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 += (off_t) _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1);
-

-
	if (!_libelf_insert_extent(extents, ELF_EXTENT_EHDR, 0, (uint64_t) rc,
-
		ehdr))
-
		return ((off_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(LAYOUT, 0);
-
				return ((off_t) -1);
-
			}
-

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

-
		} else
-
			phoff = roundup(rc, (off_t) align);
-

-
		rc = phoff + (off_t) fsz;
-

-
		phdr = _libelf_getphdr(e, ec);
-

-
		if (!_libelf_insert_extent(extents, ELF_EXTENT_PHDR,
-
			(uint64_t) phoff, fsz, phdr))
-
			return ((off_t) -1);
-
	} 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_section_headers(e, ehdr) == 0)
-
		return ((off_t) -1);
-

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

-
	/*
-
	 * Compute the space taken up by the section header table, if
-
	 * one is needed.
-
	 *
-
	 * If ELF_F_LAYOUT has been 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 the library is responsible for laying out the object,
-
	 * the section header table is placed after section data.
-
	 */
-
	if (shnum) {
-
		fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, shnum);
-
		align = _libelf_falign(ELF_T_SHDR, ec);
-

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

-
		if (shoff + (off_t) fsz > rc)
-
			rc = shoff + (off_t) fsz;
-

-
		if (!_libelf_insert_extent(extents, ELF_EXTENT_SHDR,
-
			(uint64_t) shoff, fsz, NULL))
-
			return ((off_t) -1);
-
	} 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 an ELF section.
-
 */
-

-
static off_t
-
_libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
-
{
-
	int ec;
-
	off_t rc;
-
	Elf_Scn *s;
-
	int elftype;
-
	Elf_Data *d, dst;
-
	uint32_t sh_type;
-
	struct _Libelf_Data *ld;
-
	uint64_t sh_off, sh_size;
-
	size_t fsz, msz, nobjects;
-

-
	assert(ex->ex_type == ELF_EXTENT_SECTION);
-

-
	s = ex->ex_desc;
-
	rc = (off_t) ex->ex_start;
-

-
	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(ld, &s->s_rawdata, d_next) {
-

-
			d = &ld->d_data;
-

-
			if ((uint64_t) rc < sh_off + d->d_off)
-
				(void) memset(nf + rc,
-
				    LIBELF_PRIVATE(fillchar),
-
				    (size_t) (sh_off + d->d_off -
-
					(uint64_t) rc));
-
			rc = (off_t) (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,
-
			    (size_t) d->d_size);
-

-
			rc += (off_t) 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(ld, &s->s_data, d_next) {
-

-
		d = &ld->d_data;
-

-
		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),
-
			    (size_t) (sh_off + d->d_off - (uint64_t) rc));
-

-
		rc = (off_t) (sh_off + d->d_off);
-

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

-
		nobjects = (size_t) (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 += (off_t) fsz;
-
	}
-

-
	return (rc);
-
}
-

-
/*
-
 * Write out an ELF Executable Header.
-
 */
-

-
static off_t
-
_libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
-
{
-
	int ec;
-
	void *ehdr;
-
	size_t fsz, msz;
-
	Elf_Data dst, src;
-

-
	assert(ex->ex_type == ELF_EXTENT_EHDR);
-
	assert(ex->ex_start == 0); /* Ehdr always comes first. */
-

-
	ec = e->e_class;
-

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

-
	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;
-

-
	dst.d_buf     = nf;
-
	dst.d_size    = fsz;
-

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

-
	return ((off_t) fsz);
-
}
-

-
/*
-
 * Write out an ELF program header table.
-
 */
-

-
static off_t
-
_libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
-
{
-
	int ec;
-
	void *ehdr;
-
	Elf32_Ehdr *eh32;
-
	Elf64_Ehdr *eh64;
-
	Elf_Data dst, src;
-
	size_t fsz, phnum;
-
	uint64_t phoff;
-

-
	assert(ex->ex_type == ELF_EXTENT_PHDR);
-

-
	ec = e->e_class;
-
	ehdr = _libelf_ehdr(e, ec, 0);
-
	phnum = e->e_u.e_elf.e_nphdr;
-

-
	assert(phnum > 0);
-

-
	if (ec == ELFCLASS32) {
-
		eh32 = (Elf32_Ehdr *) ehdr;
-
		phoff = (uint64_t) eh32->e_phoff;
-
	} else {
-
		eh64 = (Elf64_Ehdr *) ehdr;
-
		phoff = eh64->e_phoff;
-
	}
-

-
	assert(phoff > 0);
-
	assert(ex->ex_start == phoff);
-
	assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0);
-

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

-
	fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum);
-
	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;
-
	dst.d_buf = nf + ex->ex_start;
-

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

-
	return ((off_t) (phoff + fsz));
-
}
-

-
/*
-
 * Write out an ELF section header table.
-
 */
-

-
static off_t
-
_libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex)
-
{
-
	int ec;
-
	void *ehdr;
-
	Elf_Scn *scn;
-
	uint64_t shoff;
-
	Elf32_Ehdr *eh32;
-
	Elf64_Ehdr *eh64;
-
	size_t fsz, nscn;
-
	Elf_Data dst, src;
-

-
	assert(ex->ex_type == ELF_EXTENT_SHDR);
-

-
	ec = e->e_class;
-
	ehdr = _libelf_ehdr(e, ec, 0);
-
	nscn = e->e_u.e_elf.e_nscn;
-

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

-
	assert(nscn > 0);
-
	assert(shoff % _libelf_falign(ELF_T_SHDR, ec) == 0);
-
	assert(ex->ex_start == shoff);
-

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

-
	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 = nf + ex->ex_start + scn->s_ndx * fsz;
-

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

-
	return ((off_t) (ex->ex_start + nscn * fsz));
-
}
-

-
/*
-
 * 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, struct _Elf_Extent_List *extents)
-
{
-
	off_t nrc, rc;
-
	Elf_Scn *scn, *tscn;
-
	struct _Elf_Extent *ex;
-
	unsigned char *newfile;
-

-
	assert(e->e_kind == ELF_K_ELF);
-
	assert(e->e_cmd == ELF_C_RDWR || e->e_cmd == ELF_C_WRITE);
-
	assert(e->e_fd >= 0);
-

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

-
	nrc = rc = 0;
-
	SLIST_FOREACH(ex, extents, ex_next) {
-

-
		/* Fill inter-extent gaps. */
-
		if (ex->ex_start > (size_t) rc)
-
			(void) memset(newfile + rc, LIBELF_PRIVATE(fillchar),
-
			    (size_t) (ex->ex_start - (uint64_t) rc));
-

-
		switch (ex->ex_type) {
-
		case ELF_EXTENT_EHDR:
-
			if ((nrc = _libelf_write_ehdr(e, newfile, ex)) < 0)
-
				goto error;
-
			break;
-

-
		case ELF_EXTENT_PHDR:
-
			if ((nrc = _libelf_write_phdr(e, newfile, ex)) < 0)
-
				goto error;
-
			break;
-

-
		case ELF_EXTENT_SECTION:
-
			if ((nrc = _libelf_write_scn(e, newfile, ex)) < 0)
-
				goto error;
-
			break;
-

-
		case ELF_EXTENT_SHDR:
-
			if ((nrc = _libelf_write_shdr(e, newfile, ex)) < 0)
-
				goto error;
-
			break;
-

-
		default:
-
			assert(0);
-
			break;
-
		}
-

-
		assert(ex->ex_start + ex->ex_size == (size_t) nrc);
-
		assert(rc < nrc);
-

-
		rc = nrc;
-
	}
-

-
	assert(rc == newsize);
-

-
	/*
-
	 * For regular files, throw away existing file content and
-
	 * unmap any existing mappings.
-
	 */
-
	if ((e->e_flags & LIBELF_F_SPECIAL_FILE) == 0) {
-
		if (ftruncate(e->e_fd, (off_t) 0) < 0 ||
-
		    lseek(e->e_fd, (off_t) 0, SEEK_SET)) {
-
			LIBELF_SET_ERROR(IO, errno);
-
			goto error;
-
		}
-
#if	ELFTC_HAVE_MMAP
-
		if (e->e_flags & LIBELF_F_RAWFILE_MMAP) {
-
			assert(e->e_rawfile != NULL);
-
			assert(e->e_cmd == ELF_C_RDWR);
-
			if (munmap(e->e_rawfile, e->e_rawsize) < 0) {
-
				LIBELF_SET_ERROR(IO, errno);
-
				goto error;
-
			}
-
		}
-
#endif
-
	}
-

-
	/*
-
	 * Write out the new contents.
-
	 */
-
	if (write(e->e_fd, newfile, (size_t) newsize) != newsize) {
-
		LIBELF_SET_ERROR(IO, errno);
-
		goto error;
-
	}
-

-
	/*
-
	 * For files opened in ELF_C_RDWR mode, set up the new 'raw'
-
	 * contents.
-
	 */
-
	if (e->e_cmd == ELF_C_RDWR) {
-
		assert(e->e_rawfile != NULL);
-
		assert((e->e_flags & LIBELF_F_RAWFILE_MALLOC) ||
-
		    (e->e_flags & LIBELF_F_RAWFILE_MMAP));
-
		if (e->e_flags & LIBELF_F_RAWFILE_MALLOC) {
-
			free(e->e_rawfile);
-
			e->e_rawfile = newfile;
-
			newfile = NULL;
-
		}
-
#if	ELFTC_HAVE_MMAP
-
		else if (e->e_flags & LIBELF_F_RAWFILE_MMAP) {
-
			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;
-
			}
-
		}
-
#endif	/* ELFTC_HAVE_MMAP */
-

-
		/* Record the new size of the file. */
-
		e->e_rawsize = (size_t) newsize;
-
	} else {
-
		/* File opened in ELF_C_WRITE mode. */
-
		assert(e->e_rawfile == NULL);
-
	}
-

-
	/*
-
	 * 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 (e->e_class == 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 the temporary buffer. */
-
	if (newfile)
-
		free(newfile);
-

-
	return (rc);
-

-
 error:
-
	free(newfile);
-

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

-
/*
-
 * Update an ELF object.
-
 */
-

-
off_t
-
elf_update(Elf *e, Elf_Cmd c)
-
{
-
	int ec;
-
	off_t rc;
-
	struct _Elf_Extent_List extents;
-

-
	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);
-
	}
-

-
	SLIST_INIT(&extents);
-

-
	if ((rc = _libelf_resync_elf(e, &extents)) < 0)
-
		goto done;
-

-
	if (c == ELF_C_NULL)
-
		goto done;
-

-
	if (e->e_fd < 0) {
-
		rc = (off_t) -1;
-
		LIBELF_SET_ERROR(SEQUENCE, 0);
-
		goto done;
-
	}
-

-
	rc = _libelf_write_elf(e, rc, &extents);
-

-
done:
-
	_libelf_release_extents(&extents);
-
	return (rc);
-
}
deleted external/libelf/elf_version.3
@@ -1,95 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd November 9, 2011
-
.Os
-
.Dt ELF_VERSION 3
-
.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(EXIT_FAILURE, "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
deleted external/libelf/elf_version.c
@@ -1,52 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/elfdefinitions.h
@@ -1,2700 +0,0 @@
-
/*-
-
 * Copyright (c) 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 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.
-
 *
-
 * $Id: elfdefinitions.h 3403 2016-02-13 15:39:27Z jkoshy $
-
 */
-

-
/*
-
 * These definitions are based on:
-
 * - The public specification of the ELF format as defined in the
-
 *   October 2009 draft of System V ABI.
-
 *   See: http://www.sco.com/developers/gabi/latest/ch4.intro.html
-
 * - The May 1998 (version 1.5) draft of "The ELF-64 object format".
-
 * - Processor-specific ELF ABI definitions for sparc, i386, amd64, mips,
-
 *   ia64, and powerpc processors.
-
 * - The "Linkers and Libraries Guide", from Sun Microsystems.
-
 */
-

-
#ifndef _ELFDEFINITIONS_H_
-
#define _ELFDEFINITIONS_H_
-

-
#include <stdint.h>
-

-
/*
-
 * Types of capabilities.
-
 */
-

-
#define	_ELF_DEFINE_CAPABILITIES()				\
-
_ELF_DEFINE_CA(CA_SUNW_NULL,	0,	"ignored")		\
-
_ELF_DEFINE_CA(CA_SUNW_HW_1,	1,	"hardware capability")	\
-
_ELF_DEFINE_CA(CA_SUNW_SW_1,	2,	"software capability")
-

-
#undef	_ELF_DEFINE_CA
-
#define	_ELF_DEFINE_CA(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_CAPABILITIES()
-
	CA__LAST__
-
};
-

-
/*
-
 * Flags used with dynamic linking entries.
-
 */
-

-
#define	_ELF_DEFINE_DYN_FLAGS()					\
-
_ELF_DEFINE_DF(DF_ORIGIN,           0x1,			\
-
	"object being loaded may refer to $ORIGIN")		\
-
_ELF_DEFINE_DF(DF_SYMBOLIC,         0x2,			\
-
	"search library for references before executable")	\
-
_ELF_DEFINE_DF(DF_TEXTREL,          0x4,			\
-
	"relocation entries may modify text segment")		\
-
_ELF_DEFINE_DF(DF_BIND_NOW,         0x8,			\
-
	"process relocation entries at load time")		\
-
_ELF_DEFINE_DF(DF_STATIC_TLS,       0x10,			\
-
	"uses static thread-local storage")
-
#undef	_ELF_DEFINE_DF
-
#define	_ELF_DEFINE_DF(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_DYN_FLAGS()
-
	DF__LAST__
-
};
-

-

-
/*
-
 * Dynamic linking entry types.
-
 */
-

-
#define	_ELF_DEFINE_DYN_TYPES()						\
-
_ELF_DEFINE_DT(DT_NULL,             0, "end of array")			\
-
_ELF_DEFINE_DT(DT_NEEDED,           1, "names a needed library")	\
-
_ELF_DEFINE_DT(DT_PLTRELSZ,         2,					\
-
	"size in bytes of associated relocation entries")		\
-
_ELF_DEFINE_DT(DT_PLTGOT,           3,					\
-
	"address associated with the procedure linkage table")		\
-
_ELF_DEFINE_DT(DT_HASH,             4,					\
-
	"address of the symbol hash table")				\
-
_ELF_DEFINE_DT(DT_STRTAB,           5,					\
-
	"address of the string table")					\
-
_ELF_DEFINE_DT(DT_SYMTAB,           6,					\
-
	"address of the symbol table")					\
-
_ELF_DEFINE_DT(DT_RELA,             7,					\
-
	"address of the relocation table")				\
-
_ELF_DEFINE_DT(DT_RELASZ,           8, "size of the DT_RELA table")	\
-
_ELF_DEFINE_DT(DT_RELAENT,          9, "size of each DT_RELA entry")	\
-
_ELF_DEFINE_DT(DT_STRSZ,            10, "size of the string table")	\
-
_ELF_DEFINE_DT(DT_SYMENT,           11,					\
-
	"size of a symbol table entry")					\
-
_ELF_DEFINE_DT(DT_INIT,             12,					\
-
	"address of the initialization function")			\
-
_ELF_DEFINE_DT(DT_FINI,             13,					\
-
	"address of the finalization function")				\
-
_ELF_DEFINE_DT(DT_SONAME,           14, "names the shared object")	\
-
_ELF_DEFINE_DT(DT_RPATH,            15,					\
-
	"runtime library search path")					\
-
_ELF_DEFINE_DT(DT_SYMBOLIC,         16,					\
-
	"alter symbol resolution algorithm")				\
-
_ELF_DEFINE_DT(DT_REL,              17,					\
-
	"address of the DT_REL table")					\
-
_ELF_DEFINE_DT(DT_RELSZ,            18, "size of the DT_REL table")	\
-
_ELF_DEFINE_DT(DT_RELENT,           19, "size of each DT_REL entry")	\
-
_ELF_DEFINE_DT(DT_PLTREL,           20,					\
-
	"type of relocation entry in the procedure linkage table")	\
-
_ELF_DEFINE_DT(DT_DEBUG,            21, "used for debugging")		\
-
_ELF_DEFINE_DT(DT_TEXTREL,          22,					\
-
	"text segment may be written to during relocation")		\
-
_ELF_DEFINE_DT(DT_JMPREL,           23,					\
-
	"address of relocation entries associated with the procedure linkage table") \
-
_ELF_DEFINE_DT(DT_BIND_NOW,         24,					\
-
	"bind symbols at loading time")					\
-
_ELF_DEFINE_DT(DT_INIT_ARRAY,       25,					\
-
	"pointers to initialization functions")				\
-
_ELF_DEFINE_DT(DT_FINI_ARRAY,       26,					\
-
	"pointers to termination functions")				\
-
_ELF_DEFINE_DT(DT_INIT_ARRAYSZ,     27, "size of the DT_INIT_ARRAY")	\
-
_ELF_DEFINE_DT(DT_FINI_ARRAYSZ,     28, "size of the DT_FINI_ARRAY")	\
-
_ELF_DEFINE_DT(DT_RUNPATH,          29,					\
-
	"index of library search path string")				\
-
_ELF_DEFINE_DT(DT_FLAGS,            30,					\
-
	"flags specific to the object being loaded")			\
-
_ELF_DEFINE_DT(DT_ENCODING,         32, "standard semantics")		\
-
_ELF_DEFINE_DT(DT_PREINIT_ARRAY,    32,					\
-
	"pointers to pre-initialization functions")			\
-
_ELF_DEFINE_DT(DT_PREINIT_ARRAYSZ,  33,					\
-
	"size of pre-initialization array")				\
-
_ELF_DEFINE_DT(DT_MAXPOSTAGS,	    34,					\
-
	"the number of positive tags")					\
-
_ELF_DEFINE_DT(DT_LOOS,             0x6000000DUL,			\
-
	"start of OS-specific types")					\
-
_ELF_DEFINE_DT(DT_SUNW_AUXILIARY,   0x6000000DUL,			\
-
	"offset of string naming auxiliary filtees")			\
-
_ELF_DEFINE_DT(DT_SUNW_RTLDINF,     0x6000000EUL, "rtld internal use")	\
-
_ELF_DEFINE_DT(DT_SUNW_FILTER,      0x6000000FUL,			\
-
	"offset of string naming standard filtees")			\
-
_ELF_DEFINE_DT(DT_SUNW_CAP,         0x60000010UL,			\
-
	"address of hardware capabilities section")			\
-
_ELF_DEFINE_DT(DT_HIOS,             0x6FFFF000UL,			\
-
	"end of OS-specific types")					\
-
_ELF_DEFINE_DT(DT_VALRNGLO,         0x6FFFFD00UL,			\
-
	"start of range using the d_val field")				\
-
_ELF_DEFINE_DT(DT_GNU_PRELINKED,    0x6FFFFDF5UL,			\
-
	"prelinking timestamp")						\
-
_ELF_DEFINE_DT(DT_GNU_CONFLICTSZ,   0x6FFFFDF6UL,			\
-
	"size of conflict section")					\
-
_ELF_DEFINE_DT(DT_GNU_LIBLISTSZ,    0x6FFFFDF7UL,			\
-
	"size of library list")						\
-
_ELF_DEFINE_DT(DT_CHECKSUM,         0x6FFFFDF8UL,			\
-
	"checksum for the object")					\
-
_ELF_DEFINE_DT(DT_PLTPADSZ,         0x6FFFFDF9UL,			\
-
	"size of PLT padding")						\
-
_ELF_DEFINE_DT(DT_MOVEENT,          0x6FFFFDFAUL,			\
-
	"size of DT_MOVETAB entries")					\
-
_ELF_DEFINE_DT(DT_MOVESZ,           0x6FFFFDFBUL,			\
-
	"total size of the MOVETAB table")				\
-
_ELF_DEFINE_DT(DT_FEATURE,          0x6FFFFDFCUL, "feature values")	\
-
_ELF_DEFINE_DT(DT_POSFLAG_1,        0x6FFFFDFDUL,			\
-
	"dynamic position flags")					\
-
_ELF_DEFINE_DT(DT_SYMINSZ,          0x6FFFFDFEUL,			\
-
	"size of the DT_SYMINFO table")					\
-
_ELF_DEFINE_DT(DT_SYMINENT,         0x6FFFFDFFUL,			\
-
	"size of a DT_SYMINFO entry")					\
-
_ELF_DEFINE_DT(DT_VALRNGHI,         0x6FFFFDFFUL,			\
-
	"end of range using the d_val field")				\
-
_ELF_DEFINE_DT(DT_ADDRRNGLO,        0x6FFFFE00UL,			\
-
	"start of range using the d_ptr field")				\
-
_ELF_DEFINE_DT(DT_GNU_HASH,	    0x6FFFFEF5UL,			\
-
	"GNU style hash tables")					\
-
_ELF_DEFINE_DT(DT_GNU_CONFLICT,     0x6FFFFEF8UL,			\
-
	"address of conflict section")					\
-
_ELF_DEFINE_DT(DT_GNU_LIBLIST,      0x6FFFFEF9UL,			\
-
	"address of conflict section")					\
-
_ELF_DEFINE_DT(DT_CONFIG,           0x6FFFFEFAUL,			\
-
	"configuration file")						\
-
_ELF_DEFINE_DT(DT_DEPAUDIT,         0x6FFFFEFBUL,			\
-
	"string defining audit libraries")				\
-
_ELF_DEFINE_DT(DT_AUDIT,            0x6FFFFEFCUL,			\
-
	"string defining audit libraries")				\
-
_ELF_DEFINE_DT(DT_PLTPAD,           0x6FFFFEFDUL, "PLT padding")	\
-
_ELF_DEFINE_DT(DT_MOVETAB,          0x6FFFFEFEUL,			\
-
	"address of a move table")					\
-
_ELF_DEFINE_DT(DT_SYMINFO,          0x6FFFFEFFUL,			\
-
	"address of the symbol information table")			\
-
_ELF_DEFINE_DT(DT_ADDRRNGHI,        0x6FFFFEFFUL,			\
-
	"end of range using the d_ptr field")				\
-
_ELF_DEFINE_DT(DT_VERSYM,	    0x6FFFFFF0UL,			\
-
	"address of the version section")				\
-
_ELF_DEFINE_DT(DT_RELACOUNT,        0x6FFFFFF9UL,			\
-
	"count of RELA relocations")					\
-
_ELF_DEFINE_DT(DT_RELCOUNT,         0x6FFFFFFAUL,			\
-
	"count of REL relocations")					\
-
_ELF_DEFINE_DT(DT_FLAGS_1,          0x6FFFFFFBUL, "flag values")	\
-
_ELF_DEFINE_DT(DT_VERDEF,	    0x6FFFFFFCUL,			\
-
	"address of the version definition segment")			\
-
_ELF_DEFINE_DT(DT_VERDEFNUM,	    0x6FFFFFFDUL,			\
-
	"the number of version definition entries")			\
-
_ELF_DEFINE_DT(DT_VERNEED,	    0x6FFFFFFEUL,			\
-
	"address of section with needed versions")			\
-
_ELF_DEFINE_DT(DT_VERNEEDNUM,       0x6FFFFFFFUL,			\
-
	"the number of version needed entries")				\
-
_ELF_DEFINE_DT(DT_LOPROC,           0x70000000UL,			\
-
	"start of processor-specific types")				\
-
_ELF_DEFINE_DT(DT_ARM_SYMTABSZ,	    0x70000001UL,			\
-
	"number of entries in the dynamic symbol table")		\
-
_ELF_DEFINE_DT(DT_SPARC_REGISTER,   0x70000001UL,			\
-
	"index of an STT_SPARC_REGISTER symbol")			\
-
_ELF_DEFINE_DT(DT_ARM_PREEMPTMAP,   0x70000002UL,			\
-
	"address of the preemption map")				\
-
_ELF_DEFINE_DT(DT_MIPS_RLD_VERSION, 0x70000001UL,			\
-
	"version ID for runtime linker interface")			\
-
_ELF_DEFINE_DT(DT_MIPS_TIME_STAMP,  0x70000002UL,			\
-
	"timestamp")							\
-
_ELF_DEFINE_DT(DT_MIPS_ICHECKSUM,   0x70000003UL,			\
-
	"checksum of all external strings and common sizes")		\
-
_ELF_DEFINE_DT(DT_MIPS_IVERSION,    0x70000004UL,			\
-
	"string table index of a version string")			\
-
_ELF_DEFINE_DT(DT_MIPS_FLAGS,       0x70000005UL,			\
-
	"MIPS-specific flags")						\
-
_ELF_DEFINE_DT(DT_MIPS_BASE_ADDRESS, 0x70000006UL,			\
-
	"base address for the executable/DSO")				\
-
_ELF_DEFINE_DT(DT_MIPS_CONFLICT,    0x70000008UL,			\
-
	"address of .conflict section")					\
-
_ELF_DEFINE_DT(DT_MIPS_LIBLIST,     0x70000009UL,			\
-
	"address of .liblist section")					\
-
_ELF_DEFINE_DT(DT_MIPS_LOCAL_GOTNO, 0x7000000AUL,			\
-
	"number of local GOT entries")					\
-
_ELF_DEFINE_DT(DT_MIPS_CONFLICTNO,  0x7000000BUL,			\
-
	"number of entries in the .conflict section")			\
-
_ELF_DEFINE_DT(DT_MIPS_LIBLISTNO,   0x70000010UL,			\
-
	"number of entries in the .liblist section")			\
-
_ELF_DEFINE_DT(DT_MIPS_SYMTABNO,    0x70000011UL,			\
-
	"number of entries in the .dynsym section")			\
-
_ELF_DEFINE_DT(DT_MIPS_UNREFEXTNO,  0x70000012UL,			\
-
	"index of first external dynamic symbol not ref'ed locally")	\
-
_ELF_DEFINE_DT(DT_MIPS_GOTSYM,      0x70000013UL,			\
-
	"index of first dynamic symbol corresponds to a GOT entry")	\
-
_ELF_DEFINE_DT(DT_MIPS_HIPAGENO,    0x70000014UL,			\
-
	"number of page table entries in GOT")				\
-
_ELF_DEFINE_DT(DT_MIPS_RLD_MAP,     0x70000016UL,			\
-
	"address of runtime linker map")				\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASS, 0x70000017UL,			\
-
	"Delta C++ class definition")					\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASS_NO, 0x70000018UL,			\
-
	"number of entries in DT_MIPS_DELTA_CLASS")			\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_INSTANCE, 0x70000019UL,			\
-
	"Delta C++ class instances")					\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_INSTANCE_NO, 0x7000001AUL,			\
-
	"number of entries in DT_MIPS_DELTA_INSTANCE")			\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_RELOC, 0x7000001BUL,			\
-
	"Delta relocations")						\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_RELOC_NO, 0x7000001CUL,			\
-
	"number of entries in DT_MIPS_DELTA_RELOC")			\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_SYM,   0x7000001DUL,			\
-
	"Delta symbols refered by Delta relocations")			\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_SYM_NO, 0x7000001EUL,			\
-
	"number of entries in DT_MIPS_DELTA_SYM")			\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASSSYM, 0x70000020UL,			\
-
	"Delta symbols for class declarations")				\
-
_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASSSYM_NO, 0x70000021UL,			\
-
	"number of entries in DT_MIPS_DELTA_CLASSSYM")			\
-
_ELF_DEFINE_DT(DT_MIPS_CXX_FLAGS,   0x70000022UL,			\
-
	"C++ flavor flags")						\
-
_ELF_DEFINE_DT(DT_MIPS_PIXIE_INIT,  0x70000023UL,			\
-
	"address of an initialization routine created by pixie")	\
-
_ELF_DEFINE_DT(DT_MIPS_SYMBOL_LIB,  0x70000024UL,			\
-
	"address of .MIPS.symlib section")				\
-
_ELF_DEFINE_DT(DT_MIPS_LOCALPAGE_GOTIDX, 0x70000025UL,			\
-
	"GOT index of first page table entry for a segment")		\
-
_ELF_DEFINE_DT(DT_MIPS_LOCAL_GOTIDX, 0x70000026UL,			\
-
	"GOT index of first page table entry for a local symbol")	\
-
_ELF_DEFINE_DT(DT_MIPS_HIDDEN_GOTIDX, 0x70000027UL,			\
-
	"GOT index of first page table entry for a hidden symbol")	\
-
_ELF_DEFINE_DT(DT_MIPS_PROTECTED_GOTIDX, 0x70000028UL,			\
-
	"GOT index of first page table entry for a protected symbol")	\
-
_ELF_DEFINE_DT(DT_MIPS_OPTIONS,     0x70000029UL,			\
-
	"address of .MIPS.options section")				\
-
_ELF_DEFINE_DT(DT_MIPS_INTERFACE,   0x7000002AUL,			\
-
	"address of .MIPS.interface section")				\
-
_ELF_DEFINE_DT(DT_MIPS_DYNSTR_ALIGN, 0x7000002BUL, "???")		\
-
_ELF_DEFINE_DT(DT_MIPS_INTERFACE_SIZE, 0x7000002CUL,			\
-
	"size of .MIPS.interface section")				\
-
_ELF_DEFINE_DT(DT_MIPS_RLD_TEXT_RESOLVE_ADDR, 0x7000002DUL,		\
-
	"address of _rld_text_resolve in GOT")				\
-
_ELF_DEFINE_DT(DT_MIPS_PERF_SUFFIX, 0x7000002EUL,			\
-
	"default suffix of DSO to be appended by dlopen")		\
-
_ELF_DEFINE_DT(DT_MIPS_COMPACT_SIZE, 0x7000002FUL,			\
-
	"size of a ucode compact relocation record (o32)")		\
-
_ELF_DEFINE_DT(DT_MIPS_GP_VALUE,    0x70000030UL,			\
-
	"GP value of a specified GP relative range")			\
-
_ELF_DEFINE_DT(DT_MIPS_AUX_DYNAMIC, 0x70000031UL,			\
-
	"address of an auxiliary dynamic table")			\
-
_ELF_DEFINE_DT(DT_MIPS_PLTGOT,      0x70000032UL,			\
-
	"address of the PLTGOT")					\
-
_ELF_DEFINE_DT(DT_MIPS_RLD_OBJ_UPDATE, 0x70000033UL,			\
-
	"object list update callback")					\
-
_ELF_DEFINE_DT(DT_MIPS_RWPLT,       0x70000034UL,			\
-
	"address of a writable PLT")					\
-
_ELF_DEFINE_DT(DT_PPC_GOT,          0x70000000UL,			\
-
	"value of _GLOBAL_OFFSET_TABLE_")				\
-
_ELF_DEFINE_DT(DT_PPC_TLSOPT,       0x70000001UL,			\
-
	"TLS descriptor should be optimized")				\
-
_ELF_DEFINE_DT(DT_PPC64_GLINK,      0x70000000UL,			\
-
	"address of .glink section")					\
-
_ELF_DEFINE_DT(DT_PPC64_OPD,        0x70000001UL,			\
-
	"address of .opd section")					\
-
_ELF_DEFINE_DT(DT_PPC64_OPDSZ,      0x70000002UL,			\
-
	"size of .opd section")						\
-
_ELF_DEFINE_DT(DT_PPC64_TLSOPT,     0x70000003UL,			\
-
	"TLS descriptor should be optimized")				\
-
_ELF_DEFINE_DT(DT_AUXILIARY,        0x7FFFFFFDUL,			\
-
	"offset of string naming auxiliary filtees")			\
-
_ELF_DEFINE_DT(DT_USED,             0x7FFFFFFEUL, "ignored")		\
-
_ELF_DEFINE_DT(DT_FILTER,           0x7FFFFFFFUL,			\
-
	"index of string naming filtees")				\
-
_ELF_DEFINE_DT(DT_HIPROC,           0x7FFFFFFFUL,			\
-
	"end of processor-specific types")
-

-
#undef	_ELF_DEFINE_DT
-
#define	_ELF_DEFINE_DT(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_DYN_TYPES()
-
	DT__LAST__ = DT_HIPROC
-
};
-

-
#define	DT_DEPRECATED_SPARC_REGISTER	DT_SPARC_REGISTER
-

-
/*
-
 * Flags used in the executable header (field: e_flags).
-
 */
-
#define	_ELF_DEFINE_EHDR_FLAGS()					\
-
_ELF_DEFINE_EF(EF_ARM_RELEXEC,      0x00000001UL,			\
-
	"dynamic segment describes only how to relocate segments")	\
-
_ELF_DEFINE_EF(EF_ARM_HASENTRY,     0x00000002UL,			\
-
	"e_entry contains a program entry point")			\
-
_ELF_DEFINE_EF(EF_ARM_SYMSARESORTED, 0x00000004UL,			\
-
	"subsection of symbol table is sorted by symbol value")		\
-
_ELF_DEFINE_EF(EF_ARM_DYNSYMSUSESEGIDX, 0x00000008UL,			\
-
	"dynamic symbol st_shndx = containing segment index + 1")	\
-
_ELF_DEFINE_EF(EF_ARM_MAPSYMSFIRST, 0x00000010UL,			\
-
	"mapping symbols precede other local symbols in symtab")	\
-
_ELF_DEFINE_EF(EF_ARM_BE8,          0x00800000UL,			\
-
	"file contains BE-8 code")					\
-
_ELF_DEFINE_EF(EF_ARM_LE8,          0x00400000UL,			\
-
	"file contains LE-8 code")					\
-
_ELF_DEFINE_EF(EF_ARM_EABIMASK,     0xFF000000UL,			\
-
	"mask for ARM EABI version number (0 denotes GNU or unknown)")	\
-
_ELF_DEFINE_EF(EF_ARM_EABI_UNKNOWN, 0x00000000UL,			\
-
	"Unknown or GNU ARM EABI version number")			\
-
_ELF_DEFINE_EF(EF_ARM_EABI_VER1,    0x01000000UL,			\
-
	"ARM EABI version 1")						\
-
_ELF_DEFINE_EF(EF_ARM_EABI_VER2,    0x02000000UL,			\
-
	"ARM EABI version 2")						\
-
_ELF_DEFINE_EF(EF_ARM_EABI_VER3,    0x03000000UL,			\
-
	"ARM EABI version 3")						\
-
_ELF_DEFINE_EF(EF_ARM_EABI_VER4,    0x04000000UL,			\
-
	"ARM EABI version 4")						\
-
_ELF_DEFINE_EF(EF_ARM_EABI_VER5,    0x05000000UL,			\
-
	"ARM EABI version 5")						\
-
_ELF_DEFINE_EF(EF_ARM_INTERWORK,    0x00000004UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_ARM_APCS_26,      0x00000008UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_ARM_APCS_FLOAT,   0x00000010UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_ARM_PIC,          0x00000020UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_ARM_ALIGN8,       0x00000040UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_ARM_NEW_ABI,      0x00000080UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_ARM_OLD_ABI,      0x00000100UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_ARM_SOFT_FLOAT,   0x00000200UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_ARM_VFP_FLOAT,    0x00000400UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_ARM_MAVERICK_FLOAT, 0x00000800UL,			\
-
	"GNU EABI extension")						\
-
_ELF_DEFINE_EF(EF_MIPS_NOREORDER,   0x00000001UL,			\
-
	"at least one .noreorder directive appeared in the source")	\
-
_ELF_DEFINE_EF(EF_MIPS_PIC,         0x00000002UL,			\
-
	"file contains position independent code")			\
-
_ELF_DEFINE_EF(EF_MIPS_CPIC,        0x00000004UL,			\
-
	"file's code uses standard conventions for calling PIC")	\
-
_ELF_DEFINE_EF(EF_MIPS_UCODE,       0x00000010UL,			\
-
	"file contains UCODE (obsolete)")				\
-
_ELF_DEFINE_EF(EF_MIPS_ABI2,        0x00000020UL,			\
-
	"file follows MIPS III 32-bit ABI")				\
-
_ELF_DEFINE_EF(EF_MIPS_OPTIONS_FIRST, 0x00000080UL,			\
-
	"ld(1) should process .MIPS.options section first")		\
-
_ELF_DEFINE_EF(EF_MIPS_ARCH_ASE,    0x0F000000UL,			\
-
	"file uses application-specific architectural extensions")	\
-
_ELF_DEFINE_EF(EF_MIPS_ARCH_ASE_MDMX, 0x08000000UL,			\
-
	"file uses MDMX multimedia extensions")				\
-
_ELF_DEFINE_EF(EF_MIPS_ARCH_ASE_M16, 0x04000000UL,			\
-
	"file uses MIPS-16 ISA extensions")				\
-
_ELF_DEFINE_EF(EF_MIPS_ARCH,         0xF0000000UL,			\
-
	"4-bit MIPS architecture field")				\
-
_ELF_DEFINE_EF(EF_PPC_EMB,          0x80000000UL,			\
-
	"Embedded PowerPC flag")					\
-
_ELF_DEFINE_EF(EF_PPC_RELOCATABLE,  0x00010000UL,			\
-
	"-mrelocatable flag")						\
-
_ELF_DEFINE_EF(EF_PPC_RELOCATABLE_LIB, 0x00008000UL,			\
-
	"-mrelocatable-lib flag")					\
-
_ELF_DEFINE_EF(EF_SPARC_EXT_MASK,   0x00ffff00UL,			\
-
	"Vendor Extension mask")					\
-
_ELF_DEFINE_EF(EF_SPARC_32PLUS,     0x00000100UL,			\
-
	"Generic V8+ features")						\
-
_ELF_DEFINE_EF(EF_SPARC_SUN_US1,    0x00000200UL,			\
-
	"Sun UltraSPARCTM 1 Extensions")				\
-
_ELF_DEFINE_EF(EF_SPARC_HAL_R1,     0x00000400UL, "HAL R1 Extensions")	\
-
_ELF_DEFINE_EF(EF_SPARC_SUN_US3,    0x00000800UL,			\
-
	"Sun UltraSPARC 3 Extensions")					\
-
_ELF_DEFINE_EF(EF_SPARCV9_MM,       0x00000003UL,			\
-
	"Mask for Memory Model")					\
-
_ELF_DEFINE_EF(EF_SPARCV9_TSO,      0x00000000UL,			\
-
	"Total Store Ordering")						\
-
_ELF_DEFINE_EF(EF_SPARCV9_PSO,      0x00000001UL,			\
-
	"Partial Store Ordering")					\
-
_ELF_DEFINE_EF(EF_SPARCV9_RMO,      0x00000002UL,			\
-
	"Relaxed Memory Ordering")
-

-
#undef	_ELF_DEFINE_EF
-
#define	_ELF_DEFINE_EF(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_EHDR_FLAGS()
-
	EF__LAST__
-
};
-

-
/*
-
 * Offsets in the `ei_ident[]` field of an ELF executable header.
-
 */
-
#define	_ELF_DEFINE_EI_OFFSETS()			\
-
_ELF_DEFINE_EI(EI_MAG0,     0, "magic number")		\
-
_ELF_DEFINE_EI(EI_MAG1,     1, "magic number")		\
-
_ELF_DEFINE_EI(EI_MAG2,     2, "magic number")		\
-
_ELF_DEFINE_EI(EI_MAG3,     3, "magic number")		\
-
_ELF_DEFINE_EI(EI_CLASS,    4, "file class")		\
-
_ELF_DEFINE_EI(EI_DATA,     5, "data encoding")		\
-
_ELF_DEFINE_EI(EI_VERSION,  6, "file version")		\
-
_ELF_DEFINE_EI(EI_OSABI,    7, "OS ABI kind")		\
-
_ELF_DEFINE_EI(EI_ABIVERSION, 8, "OS ABI version")	\
-
_ELF_DEFINE_EI(EI_PAD,	    9, "padding start")		\
-
_ELF_DEFINE_EI(EI_NIDENT,  16, "total size")
-

-
#undef	_ELF_DEFINE_EI
-
#define	_ELF_DEFINE_EI(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_EI_OFFSETS()
-
	EI__LAST__
-
};
-

-
/*
-
 * The ELF class of an object.
-
 */
-
#define	_ELF_DEFINE_ELFCLASS()				\
-
_ELF_DEFINE_EC(ELFCLASSNONE, 0, "Unknown ELF class")	\
-
_ELF_DEFINE_EC(ELFCLASS32,   1, "32 bit objects")	\
-
_ELF_DEFINE_EC(ELFCLASS64,   2, "64 bit objects")
-

-
#undef	_ELF_DEFINE_EC
-
#define	_ELF_DEFINE_EC(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_ELFCLASS()
-
	EC__LAST__
-
};
-

-
/*
-
 * Endianness of data in an ELF object.
-
 */
-

-
#define	_ELF_DEFINE_ELF_DATA_ENDIANNESS()			\
-
_ELF_DEFINE_ED(ELFDATANONE, 0, "Unknown data endianness")	\
-
_ELF_DEFINE_ED(ELFDATA2LSB, 1, "little endian")			\
-
_ELF_DEFINE_ED(ELFDATA2MSB, 2, "big endian")
-

-
#undef	_ELF_DEFINE_ED
-
#define	_ELF_DEFINE_ED(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_ELF_DATA_ENDIANNESS()
-
	ED__LAST__
-
};
-

-
/*
-
 * Values of the magic numbers used in identification array.
-
 */
-
#define	_ELF_DEFINE_ELF_MAGIC()			\
-
_ELF_DEFINE_EMAG(ELFMAG0, 0x7FU)		\
-
_ELF_DEFINE_EMAG(ELFMAG1, 'E')			\
-
_ELF_DEFINE_EMAG(ELFMAG2, 'L')			\
-
_ELF_DEFINE_EMAG(ELFMAG3, 'F')
-

-
#undef	_ELF_DEFINE_EMAG
-
#define	_ELF_DEFINE_EMAG(N, V)		N = V ,
-
enum {
-
	_ELF_DEFINE_ELF_MAGIC()
-
	ELFMAG__LAST__
-
};
-

-
/*
-
 * ELF OS ABI field.
-
 */
-
#define	_ELF_DEFINE_ELF_OSABI()						\
-
_ELF_DEFINE_EABI(ELFOSABI_NONE,       0,				\
-
	"No extensions or unspecified")					\
-
_ELF_DEFINE_EABI(ELFOSABI_SYSV,       0, "SYSV")			\
-
_ELF_DEFINE_EABI(ELFOSABI_HPUX,       1, "Hewlett-Packard HP-UX")	\
-
_ELF_DEFINE_EABI(ELFOSABI_NETBSD,     2, "NetBSD")			\
-
_ELF_DEFINE_EABI(ELFOSABI_GNU,        3, "GNU")				\
-
_ELF_DEFINE_EABI(ELFOSABI_HURD,       4, "GNU/HURD")			\
-
_ELF_DEFINE_EABI(ELFOSABI_86OPEN,     5, "86Open Common ABI")		\
-
_ELF_DEFINE_EABI(ELFOSABI_SOLARIS,    6, "Sun Solaris")			\
-
_ELF_DEFINE_EABI(ELFOSABI_AIX,        7, "AIX")				\
-
_ELF_DEFINE_EABI(ELFOSABI_IRIX,       8, "IRIX")			\
-
_ELF_DEFINE_EABI(ELFOSABI_FREEBSD,    9, "FreeBSD")			\
-
_ELF_DEFINE_EABI(ELFOSABI_TRU64,      10, "Compaq TRU64 UNIX")		\
-
_ELF_DEFINE_EABI(ELFOSABI_MODESTO,    11, "Novell Modesto")		\
-
_ELF_DEFINE_EABI(ELFOSABI_OPENBSD,    12, "Open BSD")			\
-
_ELF_DEFINE_EABI(ELFOSABI_OPENVMS,    13, "Open VMS")			\
-
_ELF_DEFINE_EABI(ELFOSABI_NSK,        14,				\
-
	"Hewlett-Packard Non-Stop Kernel")				\
-
_ELF_DEFINE_EABI(ELFOSABI_AROS,       15, "Amiga Research OS")		\
-
_ELF_DEFINE_EABI(ELFOSABI_FENIXOS,    16,				\
-
	"The FenixOS highly scalable multi-core OS")			\
-
_ELF_DEFINE_EABI(ELFOSABI_ARM_AEABI,  64,				\
-
	"ARM specific symbol versioning extensions")			\
-
_ELF_DEFINE_EABI(ELFOSABI_ARM,        97, "ARM ABI")			\
-
_ELF_DEFINE_EABI(ELFOSABI_STANDALONE, 255,				\
-
	"Standalone (embedded) application")
-

-
#undef	_ELF_DEFINE_EABI
-
#define	_ELF_DEFINE_EABI(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_ELF_OSABI()
-
	ELFOSABI__LAST__
-
};
-

-
#define	ELFOSABI_LINUX			ELFOSABI_GNU
-

-
/*
-
 * ELF Machine types: (EM_*).
-
 */
-
#define	_ELF_DEFINE_ELF_MACHINES()					\
-
_ELF_DEFINE_EM(EM_NONE,             0, "No machine")			\
-
_ELF_DEFINE_EM(EM_M32,              1, "AT&T WE 32100")			\
-
_ELF_DEFINE_EM(EM_SPARC,            2, "SPARC")				\
-
_ELF_DEFINE_EM(EM_386,              3, "Intel 80386")			\
-
_ELF_DEFINE_EM(EM_68K,              4, "Motorola 68000")		\
-
_ELF_DEFINE_EM(EM_88K,              5, "Motorola 88000")		\
-
_ELF_DEFINE_EM(EM_IAMCU,            6, "Intel MCU")			\
-
_ELF_DEFINE_EM(EM_860,              7, "Intel 80860")			\
-
_ELF_DEFINE_EM(EM_MIPS,             8, "MIPS I Architecture")		\
-
_ELF_DEFINE_EM(EM_S370,             9, "IBM System/370 Processor")	\
-
_ELF_DEFINE_EM(EM_MIPS_RS3_LE,      10, "MIPS RS3000 Little-endian")	\
-
_ELF_DEFINE_EM(EM_PARISC,           15, "Hewlett-Packard PA-RISC")	\
-
_ELF_DEFINE_EM(EM_VPP500,           17, "Fujitsu VPP500")		\
-
_ELF_DEFINE_EM(EM_SPARC32PLUS,      18,					\
-
	"Enhanced instruction set SPARC")				\
-
_ELF_DEFINE_EM(EM_960,              19, "Intel 80960")			\
-
_ELF_DEFINE_EM(EM_PPC,              20, "PowerPC")			\
-
_ELF_DEFINE_EM(EM_PPC64,            21, "64-bit PowerPC")		\
-
_ELF_DEFINE_EM(EM_S390,             22, "IBM System/390 Processor")	\
-
_ELF_DEFINE_EM(EM_SPU,              23, "IBM SPU/SPC")			\
-
_ELF_DEFINE_EM(EM_V800,             36, "NEC V800")			\
-
_ELF_DEFINE_EM(EM_FR20,             37, "Fujitsu FR20")			\
-
_ELF_DEFINE_EM(EM_RH32,             38, "TRW RH-32")			\
-
_ELF_DEFINE_EM(EM_RCE,              39, "Motorola RCE")			\
-
_ELF_DEFINE_EM(EM_ARM,              40, "Advanced RISC Machines ARM")	\
-
_ELF_DEFINE_EM(EM_ALPHA,            41, "Digital Alpha")		\
-
_ELF_DEFINE_EM(EM_SH,               42, "Hitachi SH")			\
-
_ELF_DEFINE_EM(EM_SPARCV9,          43, "SPARC Version 9")		\
-
_ELF_DEFINE_EM(EM_TRICORE,          44,					\
-
	"Siemens TriCore embedded processor")				\
-
_ELF_DEFINE_EM(EM_ARC,              45,					\
-
	"Argonaut RISC Core, Argonaut Technologies Inc.")		\
-
_ELF_DEFINE_EM(EM_H8_300,           46, "Hitachi H8/300")		\
-
_ELF_DEFINE_EM(EM_H8_300H,          47, "Hitachi H8/300H")		\
-
_ELF_DEFINE_EM(EM_H8S,              48, "Hitachi H8S")			\
-
_ELF_DEFINE_EM(EM_H8_500,           49, "Hitachi H8/500")		\
-
_ELF_DEFINE_EM(EM_IA_64,            50,					\
-
	"Intel IA-64 processor architecture")				\
-
_ELF_DEFINE_EM(EM_MIPS_X,           51, "Stanford MIPS-X")		\
-
_ELF_DEFINE_EM(EM_COLDFIRE,         52, "Motorola ColdFire")		\
-
_ELF_DEFINE_EM(EM_68HC12,           53, "Motorola M68HC12")		\
-
_ELF_DEFINE_EM(EM_MMA,              54,					\
-
	"Fujitsu MMA Multimedia Accelerator")				\
-
_ELF_DEFINE_EM(EM_PCP,              55, "Siemens PCP")			\
-
_ELF_DEFINE_EM(EM_NCPU,             56,					\
-
	"Sony nCPU embedded RISC processor")				\
-
_ELF_DEFINE_EM(EM_NDR1,             57, "Denso NDR1 microprocessor")	\
-
_ELF_DEFINE_EM(EM_STARCORE,         58, "Motorola Star*Core processor")	\
-
_ELF_DEFINE_EM(EM_ME16,             59, "Toyota ME16 processor")	\
-
_ELF_DEFINE_EM(EM_ST100,            60,					\
-
	"STMicroelectronics ST100 processor")				\
-
_ELF_DEFINE_EM(EM_TINYJ,            61,					\
-
	"Advanced Logic Corp. TinyJ embedded processor family")		\
-
_ELF_DEFINE_EM(EM_X86_64,           62, "AMD x86-64 architecture")	\
-
_ELF_DEFINE_EM(EM_PDSP,             63, "Sony DSP Processor")		\
-
_ELF_DEFINE_EM(EM_PDP10,            64,					\
-
	"Digital Equipment Corp. PDP-10")				\
-
_ELF_DEFINE_EM(EM_PDP11,            65,					\
-
	"Digital Equipment Corp. PDP-11")				\
-
_ELF_DEFINE_EM(EM_FX66,             66, "Siemens FX66 microcontroller")	\
-
_ELF_DEFINE_EM(EM_ST9PLUS,          67,					\
-
	"STMicroelectronics ST9+ 8/16 bit microcontroller")		\
-
_ELF_DEFINE_EM(EM_ST7,              68,					\
-
	"STMicroelectronics ST7 8-bit microcontroller")			\
-
_ELF_DEFINE_EM(EM_68HC16,           69,					\
-
	"Motorola MC68HC16 Microcontroller")				\
-
_ELF_DEFINE_EM(EM_68HC11,           70,					\
-
	"Motorola MC68HC11 Microcontroller")				\
-
_ELF_DEFINE_EM(EM_68HC08,           71,					\
-
	"Motorola MC68HC08 Microcontroller")				\
-
_ELF_DEFINE_EM(EM_68HC05,           72,					\
-
	"Motorola MC68HC05 Microcontroller")				\
-
_ELF_DEFINE_EM(EM_SVX,              73, "Silicon Graphics SVx")		\
-
_ELF_DEFINE_EM(EM_ST19,             74,					\
-
	"STMicroelectronics ST19 8-bit microcontroller")		\
-
_ELF_DEFINE_EM(EM_VAX,              75, "Digital VAX")			\
-
_ELF_DEFINE_EM(EM_CRIS,             76,					\
-
	"Axis Communications 32-bit embedded processor")		\
-
_ELF_DEFINE_EM(EM_JAVELIN,          77,					\
-
	"Infineon Technologies 32-bit embedded processor")		\
-
_ELF_DEFINE_EM(EM_FIREPATH,         78,					\
-
	"Element 14 64-bit DSP Processor")				\
-
_ELF_DEFINE_EM(EM_ZSP,              79,					\
-
	"LSI Logic 16-bit DSP Processor")				\
-
_ELF_DEFINE_EM(EM_MMIX,             80,					\
-
	"Donald Knuth's educational 64-bit processor")			\
-
_ELF_DEFINE_EM(EM_HUANY,            81,					\
-
	"Harvard University machine-independent object files")		\
-
_ELF_DEFINE_EM(EM_PRISM,            82, "SiTera Prism")			\
-
_ELF_DEFINE_EM(EM_AVR,              83,					\
-
	"Atmel AVR 8-bit microcontroller")				\
-
_ELF_DEFINE_EM(EM_FR30,             84, "Fujitsu FR30")			\
-
_ELF_DEFINE_EM(EM_D10V,             85, "Mitsubishi D10V")		\
-
_ELF_DEFINE_EM(EM_D30V,             86, "Mitsubishi D30V")		\
-
_ELF_DEFINE_EM(EM_V850,             87, "NEC v850")			\
-
_ELF_DEFINE_EM(EM_M32R,             88, "Mitsubishi M32R")		\
-
_ELF_DEFINE_EM(EM_MN10300,          89, "Matsushita MN10300")		\
-
_ELF_DEFINE_EM(EM_MN10200,          90, "Matsushita MN10200")		\
-
_ELF_DEFINE_EM(EM_PJ,               91, "picoJava")			\
-
_ELF_DEFINE_EM(EM_OPENRISC,         92,					\
-
	"OpenRISC 32-bit embedded processor")				\
-
_ELF_DEFINE_EM(EM_ARC_COMPACT,      93,					\
-
	"ARC International ARCompact processor")			\
-
_ELF_DEFINE_EM(EM_XTENSA,           94,					\
-
	"Tensilica Xtensa Architecture")				\
-
_ELF_DEFINE_EM(EM_VIDEOCORE,        95,					\
-
	"Alphamosaic VideoCore processor")				\
-
_ELF_DEFINE_EM(EM_TMM_GPP,          96,					\
-
	"Thompson Multimedia General Purpose Processor")		\
-
_ELF_DEFINE_EM(EM_NS32K,            97,					\
-
	"National Semiconductor 32000 series")				\
-
_ELF_DEFINE_EM(EM_TPC,              98, "Tenor Network TPC processor")	\
-
_ELF_DEFINE_EM(EM_SNP1K,            99, "Trebia SNP 1000 processor")	\
-
_ELF_DEFINE_EM(EM_ST200,            100,				\
-
	"STMicroelectronics (www.st.com) ST200 microcontroller")	\
-
_ELF_DEFINE_EM(EM_IP2K,             101,				\
-
	"Ubicom IP2xxx microcontroller family")				\
-
_ELF_DEFINE_EM(EM_MAX,              102, "MAX Processor")		\
-
_ELF_DEFINE_EM(EM_CR,               103,				\
-
	"National Semiconductor CompactRISC microprocessor")		\
-
_ELF_DEFINE_EM(EM_F2MC16,           104, "Fujitsu F2MC16")		\
-
_ELF_DEFINE_EM(EM_MSP430,           105,				\
-
	"Texas Instruments embedded microcontroller msp430")		\
-
_ELF_DEFINE_EM(EM_BLACKFIN,         106,				\
-
	"Analog Devices Blackfin (DSP) processor")			\
-
_ELF_DEFINE_EM(EM_SE_C33,           107,				\
-
	"S1C33 Family of Seiko Epson processors")			\
-
_ELF_DEFINE_EM(EM_SEP,              108,				\
-
	"Sharp embedded microprocessor")				\
-
_ELF_DEFINE_EM(EM_ARCA,             109, "Arca RISC Microprocessor")	\
-
_ELF_DEFINE_EM(EM_UNICORE,          110,				\
-
	"Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University") \
-
_ELF_DEFINE_EM(EM_EXCESS,           111,				\
-
	"eXcess: 16/32/64-bit configurable embedded CPU")		\
-
_ELF_DEFINE_EM(EM_DXP,              112,				\
-
	"Icera Semiconductor Inc. Deep Execution Processor")		\
-
_ELF_DEFINE_EM(EM_ALTERA_NIOS2,     113,				\
-
	"Altera Nios II soft-core processor")				\
-
_ELF_DEFINE_EM(EM_CRX,              114,				\
-
	"National Semiconductor CompactRISC CRX microprocessor")	\
-
_ELF_DEFINE_EM(EM_XGATE,            115,				\
-
	"Motorola XGATE embedded processor")				\
-
_ELF_DEFINE_EM(EM_C166,             116,				\
-
	"Infineon C16x/XC16x processor")				\
-
_ELF_DEFINE_EM(EM_M16C,             117,				\
-
	"Renesas M16C series microprocessors")				\
-
_ELF_DEFINE_EM(EM_DSPIC30F,         118,				\
-
	"Microchip Technology dsPIC30F Digital Signal Controller")	\
-
_ELF_DEFINE_EM(EM_CE,               119,				\
-
	"Freescale Communication Engine RISC core")			\
-
_ELF_DEFINE_EM(EM_M32C,             120,				\
-
	"Renesas M32C series microprocessors")				\
-
_ELF_DEFINE_EM(EM_TSK3000,          131, "Altium TSK3000 core")		\
-
_ELF_DEFINE_EM(EM_RS08,             132,				\
-
	"Freescale RS08 embedded processor")				\
-
_ELF_DEFINE_EM(EM_SHARC,            133,				\
-
	"Analog Devices SHARC family of 32-bit DSP processors")		\
-
_ELF_DEFINE_EM(EM_ECOG2,            134,				\
-
	"Cyan Technology eCOG2 microprocessor")				\
-
_ELF_DEFINE_EM(EM_SCORE7,           135,				\
-
	"Sunplus S+core7 RISC processor")				\
-
_ELF_DEFINE_EM(EM_DSP24,            136,				\
-
	"New Japan Radio (NJR) 24-bit DSP Processor")			\
-
_ELF_DEFINE_EM(EM_VIDEOCORE3,       137,				\
-
	"Broadcom VideoCore III processor")				\
-
_ELF_DEFINE_EM(EM_LATTICEMICO32,    138,				\
-
	"RISC processor for Lattice FPGA architecture")			\
-
_ELF_DEFINE_EM(EM_SE_C17,           139, "Seiko Epson C17 family")	\
-
_ELF_DEFINE_EM(EM_TI_C6000,         140,				\
-
	"The Texas Instruments TMS320C6000 DSP family")			\
-
_ELF_DEFINE_EM(EM_TI_C2000,         141,				\
-
	"The Texas Instruments TMS320C2000 DSP family")			\
-
_ELF_DEFINE_EM(EM_TI_C5500,         142,				\
-
	"The Texas Instruments TMS320C55x DSP family")			\
-
_ELF_DEFINE_EM(EM_MMDSP_PLUS,       160,				\
-
	"STMicroelectronics 64bit VLIW Data Signal Processor")		\
-
_ELF_DEFINE_EM(EM_CYPRESS_M8C,      161, "Cypress M8C microprocessor")	\
-
_ELF_DEFINE_EM(EM_R32C,             162,				\
-
	"Renesas R32C series microprocessors")				\
-
_ELF_DEFINE_EM(EM_TRIMEDIA,         163,				\
-
	"NXP Semiconductors TriMedia architecture family")		\
-
_ELF_DEFINE_EM(EM_QDSP6,            164, "QUALCOMM DSP6 Processor")	\
-
_ELF_DEFINE_EM(EM_8051,             165, "Intel 8051 and variants")	\
-
_ELF_DEFINE_EM(EM_STXP7X,           166,				\
-
	"STMicroelectronics STxP7x family of configurable and extensible RISC processors") \
-
_ELF_DEFINE_EM(EM_NDS32,            167,				\
-
	"Andes Technology compact code size embedded RISC processor family") \
-
_ELF_DEFINE_EM(EM_ECOG1,            168,				\
-
	"Cyan Technology eCOG1X family")				\
-
_ELF_DEFINE_EM(EM_ECOG1X,           168,				\
-
	"Cyan Technology eCOG1X family")				\
-
_ELF_DEFINE_EM(EM_MAXQ30,           169,				\
-
	"Dallas Semiconductor MAXQ30 Core Micro-controllers")		\
-
_ELF_DEFINE_EM(EM_XIMO16,           170,				\
-
	"New Japan Radio (NJR) 16-bit DSP Processor")			\
-
_ELF_DEFINE_EM(EM_MANIK,            171,				\
-
	"M2000 Reconfigurable RISC Microprocessor")			\
-
_ELF_DEFINE_EM(EM_CRAYNV2,          172,				\
-
	"Cray Inc. NV2 vector architecture")				\
-
_ELF_DEFINE_EM(EM_RX,               173, "Renesas RX family")		\
-
_ELF_DEFINE_EM(EM_METAG,            174,				\
-
	"Imagination Technologies META processor architecture")		\
-
_ELF_DEFINE_EM(EM_MCST_ELBRUS,      175,				\
-
	"MCST Elbrus general purpose hardware architecture")		\
-
_ELF_DEFINE_EM(EM_ECOG16,           176,				\
-
	"Cyan Technology eCOG16 family")				\
-
_ELF_DEFINE_EM(EM_CR16,             177,				\
-
	"National Semiconductor CompactRISC CR16 16-bit microprocessor") \
-
_ELF_DEFINE_EM(EM_ETPU,             178,				\
-
	"Freescale Extended Time Processing Unit")			\
-
_ELF_DEFINE_EM(EM_SLE9X,            179,				\
-
	"Infineon Technologies SLE9X core")				\
-
_ELF_DEFINE_EM(EM_AARCH64,          183,				\
-
	"AArch64 (64-bit ARM)")						\
-
_ELF_DEFINE_EM(EM_AVR32,            185,				\
-
	"Atmel Corporation 32-bit microprocessor family")		\
-
_ELF_DEFINE_EM(EM_STM8,             186,				\
-
	"STMicroeletronics STM8 8-bit microcontroller")			\
-
_ELF_DEFINE_EM(EM_TILE64,           187,				\
-
	"Tilera TILE64 multicore architecture family")			\
-
_ELF_DEFINE_EM(EM_TILEPRO,          188,				\
-
	"Tilera TILEPro multicore architecture family")			\
-
_ELF_DEFINE_EM(EM_MICROBLAZE,       189,				\
-
	"Xilinx MicroBlaze 32-bit RISC soft processor core")		\
-
_ELF_DEFINE_EM(EM_CUDA,             190, "NVIDIA CUDA architecture")	\
-
_ELF_DEFINE_EM(EM_TILEGX,           191,				\
-
	"Tilera TILE-Gx multicore architecture family")			\
-
_ELF_DEFINE_EM(EM_CLOUDSHIELD,      192,				\
-
	"CloudShield architecture family")				\
-
_ELF_DEFINE_EM(EM_COREA_1ST,        193,				\
-
	"KIPO-KAIST Core-A 1st generation processor family")		\
-
_ELF_DEFINE_EM(EM_COREA_2ND,        194,				\
-
	"KIPO-KAIST Core-A 2nd generation processor family")		\
-
_ELF_DEFINE_EM(EM_ARC_COMPACT2,     195, "Synopsys ARCompact V2")	\
-
_ELF_DEFINE_EM(EM_OPEN8,            196,				\
-
	"Open8 8-bit RISC soft processor core")				\
-
_ELF_DEFINE_EM(EM_RL78,             197, "Renesas RL78 family")		\
-
_ELF_DEFINE_EM(EM_VIDEOCORE5,       198, "Broadcom VideoCore V processor") \
-
_ELF_DEFINE_EM(EM_78KOR,            199, "Renesas 78KOR family")	\
-
_ELF_DEFINE_EM(EM_56800EX,          200,				\
-
	"Freescale 56800EX Digital Signal Controller")			\
-
_ELF_DEFINE_EM(EM_BA1,              201, "Beyond BA1 CPU architecture")	\
-
_ELF_DEFINE_EM(EM_BA2,              202, "Beyond BA2 CPU architecture")	\
-
_ELF_DEFINE_EM(EM_XCORE,            203, "XMOS xCORE processor family") \
-
_ELF_DEFINE_EM(EM_MCHP_PIC,         204, "Microchip 8-bit PIC(r) family") \
-
_ELF_DEFINE_EM(EM_INTEL205,         205, "Reserved by Intel")           \
-
_ELF_DEFINE_EM(EM_INTEL206,         206, "Reserved by Intel")           \
-
_ELF_DEFINE_EM(EM_INTEL207,         207, "Reserved by Intel")           \
-
_ELF_DEFINE_EM(EM_INTEL208,         208, "Reserved by Intel")           \
-
_ELF_DEFINE_EM(EM_INTEL209,         209, "Reserved by Intel")           \
-
_ELF_DEFINE_EM(EM_KM32,             210, "KM211 KM32 32-bit processor") \
-
_ELF_DEFINE_EM(EM_KMX32,            211, "KM211 KMX32 32-bit processor") \
-
_ELF_DEFINE_EM(EM_KMX16,            212, "KM211 KMX16 16-bit processor") \
-
_ELF_DEFINE_EM(EM_KMX8,             213, "KM211 KMX8 8-bit processor")  \
-
_ELF_DEFINE_EM(EM_KVARC,            214, "KM211 KMX32 KVARC processor") \
-
_ELF_DEFINE_EM(EM_RISCV,            243, "RISC-V")
-

-
#undef	_ELF_DEFINE_EM
-
#define	_ELF_DEFINE_EM(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_ELF_MACHINES()
-
	EM__LAST__
-
};
-

-
/* Other synonyms. */
-
#define	EM_AMD64		EM_X86_64
-
#define	EM_ARC_A5		EM_ARC_COMPACT
-

-
/*
-
 * ELF file types: (ET_*).
-
 */
-
#define	_ELF_DEFINE_ELF_TYPES()						\
-
_ELF_DEFINE_ET(ET_NONE,   0,	    "No file type")			\
-
_ELF_DEFINE_ET(ET_REL,    1, 	    "Relocatable object")		\
-
_ELF_DEFINE_ET(ET_EXEC,   2, 	    "Executable")			\
-
_ELF_DEFINE_ET(ET_DYN,    3, 	    "Shared object")			\
-
_ELF_DEFINE_ET(ET_CORE,   4, 	    "Core file")			\
-
_ELF_DEFINE_ET(ET_LOOS,   0xFE00U,  "Begin OS-specific range")		\
-
_ELF_DEFINE_ET(ET_HIOS,   0xFEFFU,  "End OS-specific range")		\
-
_ELF_DEFINE_ET(ET_LOPROC, 0xFF00U,  "Begin processor-specific range")	\
-
_ELF_DEFINE_ET(ET_HIPROC, 0xFFFFU,  "End processor-specific range")
-

-
#undef	_ELF_DEFINE_ET
-
#define	_ELF_DEFINE_ET(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_ELF_TYPES()
-
	ET__LAST__
-
};
-

-
/* ELF file format version numbers. */
-
#define	EV_NONE		0
-
#define	EV_CURRENT	1
-

-
/*
-
 * Flags for section groups.
-
 */
-
#define	GRP_COMDAT 	0x1		/* COMDAT semantics */
-
#define	GRP_MASKOS 	0x0ff00000	/* OS-specific flags */
-
#define	GRP_MASKPROC 	0xf0000000	/* processor-specific flags */
-

-
/*
-
 * Flags / mask for .gnu.versym sections.
-
 */
-
#define	VERSYM_VERSION	0x7fff
-
#define	VERSYM_HIDDEN	0x8000
-

-
/*
-
 * Flags used by program header table entries.
-
 */
-

-
#define	_ELF_DEFINE_PHDR_FLAGS()					\
-
_ELF_DEFINE_PF(PF_X,                0x1, "Execute")			\
-
_ELF_DEFINE_PF(PF_W,                0x2, "Write")			\
-
_ELF_DEFINE_PF(PF_R,                0x4, "Read")			\
-
_ELF_DEFINE_PF(PF_MASKOS,           0x0ff00000, "OS-specific flags")	\
-
_ELF_DEFINE_PF(PF_MASKPROC,         0xf0000000, "Processor-specific flags") \
-
_ELF_DEFINE_PF(PF_ARM_SB,           0x10000000,				\
-
	"segment contains the location addressed by the static base")	\
-
_ELF_DEFINE_PF(PF_ARM_PI,           0x20000000,				\
-
	"segment is position-independent")				\
-
_ELF_DEFINE_PF(PF_ARM_ABS,          0x40000000,				\
-
	"segment must be loaded at its base address")
-

-
#undef	_ELF_DEFINE_PF
-
#define	_ELF_DEFINE_PF(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_PHDR_FLAGS()
-
	PF__LAST__
-
};
-

-
/*
-
 * Types of program header table entries.
-
 */
-

-
#define	_ELF_DEFINE_PHDR_TYPES()				\
-
_ELF_DEFINE_PT(PT_NULL,             0, "ignored entry")		\
-
_ELF_DEFINE_PT(PT_LOAD,             1, "loadable segment")	\
-
_ELF_DEFINE_PT(PT_DYNAMIC,          2,				\
-
	"contains dynamic linking information")			\
-
_ELF_DEFINE_PT(PT_INTERP,           3, "names an interpreter")	\
-
_ELF_DEFINE_PT(PT_NOTE,             4, "auxiliary information")	\
-
_ELF_DEFINE_PT(PT_SHLIB,            5, "reserved")		\
-
_ELF_DEFINE_PT(PT_PHDR,             6,				\
-
	"describes the program header itself")			\
-
_ELF_DEFINE_PT(PT_TLS,              7, "thread local storage")	\
-
_ELF_DEFINE_PT(PT_LOOS,             0x60000000UL,		\
-
	"start of OS-specific range")				\
-
_ELF_DEFINE_PT(PT_SUNW_UNWIND,      0x6464E550UL,		\
-
	"Solaris/amd64 stack unwind tables")			\
-
_ELF_DEFINE_PT(PT_GNU_EH_FRAME,     0x6474E550UL,		\
-
	"GCC generated .eh_frame_hdr segment")			\
-
_ELF_DEFINE_PT(PT_GNU_STACK,	    0x6474E551UL,		\
-
	"Stack flags")						\
-
_ELF_DEFINE_PT(PT_GNU_RELRO,	    0x6474E552UL,		\
-
	"Segment becomes read-only after relocation")		\
-
_ELF_DEFINE_PT(PT_SUNWBSS,          0x6FFFFFFAUL,		\
-
	"A Solaris .SUNW_bss section")				\
-
_ELF_DEFINE_PT(PT_SUNWSTACK,        0x6FFFFFFBUL,		\
-
	"A Solaris process stack")				\
-
_ELF_DEFINE_PT(PT_SUNWDTRACE,       0x6FFFFFFCUL,		\
-
	"Used by dtrace(1)")					\
-
_ELF_DEFINE_PT(PT_SUNWCAP,          0x6FFFFFFDUL,		\
-
	"Special hardware capability requirements")		\
-
_ELF_DEFINE_PT(PT_HIOS,             0x6FFFFFFFUL,		\
-
	"end of OS-specific range")				\
-
_ELF_DEFINE_PT(PT_LOPROC,           0x70000000UL,		\
-
	"start of processor-specific range")			\
-
_ELF_DEFINE_PT(PT_ARM_ARCHEXT,      0x70000000UL,		\
-
	"platform architecture compatibility information")	\
-
_ELF_DEFINE_PT(PT_ARM_EXIDX,        0x70000001UL,		\
-
	"exception unwind tables")				\
-
_ELF_DEFINE_PT(PT_MIPS_REGINFO,     0x70000000UL,		\
-
	"register usage information")				\
-
_ELF_DEFINE_PT(PT_MIPS_RTPROC,      0x70000001UL,		\
-
	"runtime procedure table")				\
-
_ELF_DEFINE_PT(PT_MIPS_OPTIONS,     0x70000002UL,		\
-
	"options segment")					\
-
_ELF_DEFINE_PT(PT_HIPROC,           0x7FFFFFFFUL,		\
-
	"end of processor-specific range")
-

-
#undef	_ELF_DEFINE_PT
-
#define	_ELF_DEFINE_PT(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_PHDR_TYPES()
-
	PT__LAST__ = PT_HIPROC
-
};
-

-
/* synonyms. */
-
#define	PT_ARM_UNWIND	PT_ARM_EXIDX
-
#define	PT_HISUNW	PT_HIOS
-
#define	PT_LOSUNW	PT_SUNWBSS
-

-
/*
-
 * Section flags.
-
 */
-

-
#define	_ELF_DEFINE_SECTION_FLAGS()					\
-
_ELF_DEFINE_SHF(SHF_WRITE,           0x1,				\
-
	"writable during program execution")				\
-
_ELF_DEFINE_SHF(SHF_ALLOC,           0x2,				\
-
	"occupies memory during program execution")			\
-
_ELF_DEFINE_SHF(SHF_EXECINSTR,       0x4, "executable instructions")	\
-
_ELF_DEFINE_SHF(SHF_MERGE,           0x10,				\
-
	"may be merged to prevent duplication")				\
-
_ELF_DEFINE_SHF(SHF_STRINGS,         0x20,				\
-
	"NUL-terminated character strings")				\
-
_ELF_DEFINE_SHF(SHF_INFO_LINK,       0x40,				\
-
	"the sh_info field holds a link")				\
-
_ELF_DEFINE_SHF(SHF_LINK_ORDER,      0x80,				\
-
	"special ordering requirements during linking")			\
-
_ELF_DEFINE_SHF(SHF_OS_NONCONFORMING, 0x100,				\
-
	"requires OS-specific processing during linking")		\
-
_ELF_DEFINE_SHF(SHF_GROUP,           0x200,				\
-
	"member of a section group")					\
-
_ELF_DEFINE_SHF(SHF_TLS,             0x400,				\
-
	"holds thread-local storage")					\
-
_ELF_DEFINE_SHF(SHF_COMPRESSED,      0x800,				\
-
	"holds compressed data")					\
-
_ELF_DEFINE_SHF(SHF_MASKOS,          0x0FF00000UL,			\
-
	"bits reserved for OS-specific semantics")			\
-
_ELF_DEFINE_SHF(SHF_AMD64_LARGE,     0x10000000UL,			\
-
	"section uses large code model")				\
-
_ELF_DEFINE_SHF(SHF_ENTRYSECT,       0x10000000UL,			\
-
	"section contains an entry point (ARM)")			\
-
_ELF_DEFINE_SHF(SHF_COMDEF,          0x80000000UL,			\
-
	"section may be multiply defined in input to link step (ARM)")	\
-
_ELF_DEFINE_SHF(SHF_MIPS_GPREL,      0x10000000UL,			\
-
	"section must be part of global data area")			\
-
_ELF_DEFINE_SHF(SHF_MIPS_MERGE,      0x20000000UL,			\
-
	"section data should be merged to eliminate duplication")	\
-
_ELF_DEFINE_SHF(SHF_MIPS_ADDR,       0x40000000UL,			\
-
	"section data is addressed by default")				\
-
_ELF_DEFINE_SHF(SHF_MIPS_STRING,     0x80000000UL,			\
-
	"section data is string data by default")			\
-
_ELF_DEFINE_SHF(SHF_MIPS_NOSTRIP,    0x08000000UL,			\
-
	"section data may not be stripped")				\
-
_ELF_DEFINE_SHF(SHF_MIPS_LOCAL,      0x04000000UL,			\
-
	"section data local to process")				\
-
_ELF_DEFINE_SHF(SHF_MIPS_NAMES,      0x02000000UL,			\
-
	"linker must generate implicit hidden weak names")		\
-
_ELF_DEFINE_SHF(SHF_MIPS_NODUPE,     0x01000000UL,			\
-
	"linker must retain only one copy")				\
-
_ELF_DEFINE_SHF(SHF_ORDERED,         0x40000000UL,			\
-
	"section is ordered with respect to other sections")		\
-
_ELF_DEFINE_SHF(SHF_EXCLUDE,	     0x80000000UL,			\
-
	"section is excluded from executables and shared objects")	\
-
_ELF_DEFINE_SHF(SHF_MASKPROC,        0xF0000000UL,			\
-
	"bits reserved for processor-specific semantics")
-

-
#undef	_ELF_DEFINE_SHF
-
#define	_ELF_DEFINE_SHF(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_SECTION_FLAGS()
-
	SHF__LAST__
-
};
-

-
/*
-
 * Special section indices.
-
 */
-
#define _ELF_DEFINE_SECTION_INDICES()					\
-
_ELF_DEFINE_SHN(SHN_UNDEF, 	0, 	 "undefined section")		\
-
_ELF_DEFINE_SHN(SHN_LORESERVE, 	0xFF00U, "start of reserved area")	\
-
_ELF_DEFINE_SHN(SHN_LOPROC, 	0xFF00U,				\
-
	"start of processor-specific range")				\
-
_ELF_DEFINE_SHN(SHN_BEFORE,	0xFF00U, "used for section ordering")	\
-
_ELF_DEFINE_SHN(SHN_AFTER,	0xFF01U, "used for section ordering")	\
-
_ELF_DEFINE_SHN(SHN_AMD64_LCOMMON, 0xFF02U, "large common block label") \
-
_ELF_DEFINE_SHN(SHN_MIPS_ACOMMON, 0xFF00U,				\
-
	"allocated common symbols in a DSO")				\
-
_ELF_DEFINE_SHN(SHN_MIPS_TEXT,	0xFF01U, "Reserved (obsolete)")		\
-
_ELF_DEFINE_SHN(SHN_MIPS_DATA,	0xFF02U, "Reserved (obsolete)")		\
-
_ELF_DEFINE_SHN(SHN_MIPS_SCOMMON, 0xFF03U,				\
-
	"gp-addressable common symbols")				\
-
_ELF_DEFINE_SHN(SHN_MIPS_SUNDEFINED, 0xFF04U,				\
-
	"gp-addressable undefined symbols")				\
-
_ELF_DEFINE_SHN(SHN_MIPS_LCOMMON, 0xFF05U, "local common symbols")	\
-
_ELF_DEFINE_SHN(SHN_MIPS_LUNDEFINED, 0xFF06U,				\
-
	"local undefined symbols")					\
-
_ELF_DEFINE_SHN(SHN_HIPROC, 	0xFF1FU,				\
-
	"end of processor-specific range")				\
-
_ELF_DEFINE_SHN(SHN_LOOS, 	0xFF20U,				\
-
	"start of OS-specific range")					\
-
_ELF_DEFINE_SHN(SHN_SUNW_IGNORE, 0xFF3FU, "used by dtrace")		\
-
_ELF_DEFINE_SHN(SHN_HIOS, 	0xFF3FU,				\
-
	"end of OS-specific range")					\
-
_ELF_DEFINE_SHN(SHN_ABS, 	0xFFF1U, "absolute references")		\
-
_ELF_DEFINE_SHN(SHN_COMMON, 	0xFFF2U, "references to COMMON areas")	\
-
_ELF_DEFINE_SHN(SHN_XINDEX, 	0xFFFFU, "extended index")		\
-
_ELF_DEFINE_SHN(SHN_HIRESERVE, 	0xFFFFU, "end of reserved area")
-

-
#undef	_ELF_DEFINE_SHN
-
#define	_ELF_DEFINE_SHN(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_SECTION_INDICES()
-
	SHN__LAST__
-
};
-

-
/*
-
 * Section types.
-
 */
-

-
#define	_ELF_DEFINE_SECTION_TYPES()					\
-
_ELF_DEFINE_SHT(SHT_NULL,            0, "inactive header")		\
-
_ELF_DEFINE_SHT(SHT_PROGBITS,        1, "program defined information")	\
-
_ELF_DEFINE_SHT(SHT_SYMTAB,          2, "symbol table")			\
-
_ELF_DEFINE_SHT(SHT_STRTAB,          3, "string table")			\
-
_ELF_DEFINE_SHT(SHT_RELA,            4,					\
-
	"relocation entries with addends")				\
-
_ELF_DEFINE_SHT(SHT_HASH,            5, "symbol hash table")		\
-
_ELF_DEFINE_SHT(SHT_DYNAMIC,         6,					\
-
	"information for dynamic linking")				\
-
_ELF_DEFINE_SHT(SHT_NOTE,            7, "additional notes")		\
-
_ELF_DEFINE_SHT(SHT_NOBITS,          8, "section occupying no space")	\
-
_ELF_DEFINE_SHT(SHT_REL,             9,					\
-
	"relocation entries without addends")				\
-
_ELF_DEFINE_SHT(SHT_SHLIB,           10, "reserved")			\
-
_ELF_DEFINE_SHT(SHT_DYNSYM,          11, "symbol table")		\
-
_ELF_DEFINE_SHT(SHT_INIT_ARRAY,      14,				\
-
	"pointers to initialization functions")				\
-
_ELF_DEFINE_SHT(SHT_FINI_ARRAY,      15,				\
-
	"pointers to termination functions")				\
-
_ELF_DEFINE_SHT(SHT_PREINIT_ARRAY,   16,				\
-
	"pointers to functions called before initialization")		\
-
_ELF_DEFINE_SHT(SHT_GROUP,           17, "defines a section group")	\
-
_ELF_DEFINE_SHT(SHT_SYMTAB_SHNDX,    18,				\
-
	"used for extended section numbering")				\
-
_ELF_DEFINE_SHT(SHT_LOOS,            0x60000000UL,			\
-
	"start of OS-specific range")					\
-
_ELF_DEFINE_SHT(SHT_SUNW_dof,	     0x6FFFFFF4UL,			\
-
	"used by dtrace")						\
-
_ELF_DEFINE_SHT(SHT_SUNW_cap,	     0x6FFFFFF5UL,			\
-
	"capability requirements")					\
-
_ELF_DEFINE_SHT(SHT_GNU_ATTRIBUTES,  0x6FFFFFF5UL,			\
-
	"object attributes")						\
-
_ELF_DEFINE_SHT(SHT_SUNW_SIGNATURE,  0x6FFFFFF6UL,			\
-
	"module verification signature")				\
-
_ELF_DEFINE_SHT(SHT_GNU_HASH,	     0x6FFFFFF6UL,			\
-
	"GNU Hash sections")						\
-
_ELF_DEFINE_SHT(SHT_GNU_LIBLIST,     0x6FFFFFF7UL,			\
-
	"List of libraries to be prelinked")				\
-
_ELF_DEFINE_SHT(SHT_SUNW_ANNOTATE,   0x6FFFFFF7UL,			\
-
	"special section where unresolved references are allowed")	\
-
_ELF_DEFINE_SHT(SHT_SUNW_DEBUGSTR,   0x6FFFFFF8UL,			\
-
	"debugging information")					\
-
_ELF_DEFINE_SHT(SHT_CHECKSUM, 	     0x6FFFFFF8UL,			\
-
	"checksum for dynamic shared objects")				\
-
_ELF_DEFINE_SHT(SHT_SUNW_DEBUG,      0x6FFFFFF9UL,			\
-
	"debugging information")					\
-
_ELF_DEFINE_SHT(SHT_SUNW_move,       0x6FFFFFFAUL,			\
-
	"information to handle partially initialized symbols")		\
-
_ELF_DEFINE_SHT(SHT_SUNW_COMDAT,     0x6FFFFFFBUL,			\
-
	"section supporting merging of multiple copies of data")	\
-
_ELF_DEFINE_SHT(SHT_SUNW_syminfo,    0x6FFFFFFCUL,			\
-
	"additional symbol information")				\
-
_ELF_DEFINE_SHT(SHT_SUNW_verdef,     0x6FFFFFFDUL,			\
-
	"symbol versioning information")				\
-
_ELF_DEFINE_SHT(SHT_SUNW_verneed,    0x6FFFFFFEUL,			\
-
	"symbol versioning requirements")				\
-
_ELF_DEFINE_SHT(SHT_SUNW_versym,     0x6FFFFFFFUL,			\
-
	"symbol versioning table")					\
-
_ELF_DEFINE_SHT(SHT_HIOS,            0x6FFFFFFFUL,			\
-
	"end of OS-specific range")					\
-
_ELF_DEFINE_SHT(SHT_LOPROC,          0x70000000UL,			\
-
	"start of processor-specific range")				\
-
_ELF_DEFINE_SHT(SHT_ARM_EXIDX,       0x70000001UL,			\
-
	"exception index table")					\
-
_ELF_DEFINE_SHT(SHT_ARM_PREEMPTMAP,  0x70000002UL,			\
-
	"BPABI DLL dynamic linking preemption map")			\
-
_ELF_DEFINE_SHT(SHT_ARM_ATTRIBUTES,  0x70000003UL,			\
-
	"object file compatibility attributes")				\
-
_ELF_DEFINE_SHT(SHT_ARM_DEBUGOVERLAY, 0x70000004UL,			\
-
	"overlay debug information")					\
-
_ELF_DEFINE_SHT(SHT_ARM_OVERLAYSECTION, 0x70000005UL,			\
-
	"overlay debug information")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_LIBLIST,    0x70000000UL,			\
-
	"DSO library information used in link")				\
-
_ELF_DEFINE_SHT(SHT_MIPS_MSYM,       0x70000001UL,			\
-
	"MIPS symbol table extension")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_CONFLICT,   0x70000002UL,			\
-
	"symbol conflicting with DSO-defined symbols ")			\
-
_ELF_DEFINE_SHT(SHT_MIPS_GPTAB,      0x70000003UL,			\
-
	"global pointer table")						\
-
_ELF_DEFINE_SHT(SHT_MIPS_UCODE,      0x70000004UL,			\
-
	"reserved")							\
-
_ELF_DEFINE_SHT(SHT_MIPS_DEBUG,      0x70000005UL,			\
-
	"reserved (obsolete debug information)")			\
-
_ELF_DEFINE_SHT(SHT_MIPS_REGINFO,    0x70000006UL,			\
-
	"register usage information")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_PACKAGE,    0x70000007UL,			\
-
	"OSF reserved")							\
-
_ELF_DEFINE_SHT(SHT_MIPS_PACKSYM,    0x70000008UL,			\
-
	"OSF reserved")							\
-
_ELF_DEFINE_SHT(SHT_MIPS_RELD,       0x70000009UL,			\
-
	"dynamic relocation")						\
-
_ELF_DEFINE_SHT(SHT_MIPS_IFACE,      0x7000000BUL,			\
-
	"subprogram interface information")				\
-
_ELF_DEFINE_SHT(SHT_MIPS_CONTENT,    0x7000000CUL,			\
-
	"section content classification")				\
-
_ELF_DEFINE_SHT(SHT_MIPS_OPTIONS,     0x7000000DUL,			\
-
	"general options")						\
-
_ELF_DEFINE_SHT(SHT_MIPS_DELTASYM,   0x7000001BUL,			\
-
	"Delta C++: symbol table")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_DELTAINST,  0x7000001CUL,			\
-
	"Delta C++: instance table")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_DELTACLASS, 0x7000001DUL,			\
-
	"Delta C++: class table")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_DWARF,      0x7000001EUL,			\
-
	"DWARF debug information")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_DELTADECL,  0x7000001FUL,			\
-
	"Delta C++: declarations")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_SYMBOL_LIB, 0x70000020UL,			\
-
	"symbol-to-library mapping")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_EVENTS,     0x70000021UL,			\
-
	"event locations")						\
-
_ELF_DEFINE_SHT(SHT_MIPS_TRANSLATE,  0x70000022UL,			\
-
	"???")								\
-
_ELF_DEFINE_SHT(SHT_MIPS_PIXIE,      0x70000023UL,			\
-
	"special pixie sections")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_XLATE,      0x70000024UL,			\
-
	"address translation table")					\
-
_ELF_DEFINE_SHT(SHT_MIPS_XLATE_DEBUG, 0x70000025UL,			\
-
	"SGI internal address translation table")			\
-
_ELF_DEFINE_SHT(SHT_MIPS_WHIRL,      0x70000026UL,			\
-
	"intermediate code")						\
-
_ELF_DEFINE_SHT(SHT_MIPS_EH_REGION,  0x70000027UL,			\
-
	"C++ exception handling region info")				\
-
_ELF_DEFINE_SHT(SHT_MIPS_XLATE_OLD,  0x70000028UL,			\
-
	"obsolete")							\
-
_ELF_DEFINE_SHT(SHT_MIPS_PDR_EXCEPTION, 0x70000029UL,			\
-
	"runtime procedure descriptor table exception information")	\
-
_ELF_DEFINE_SHT(SHT_MIPS_ABIFLAGS,   0x7000002AUL,			\
-
	"ABI flags")							\
-
_ELF_DEFINE_SHT(SHT_SPARC_GOTDATA,   0x70000000UL,			\
-
	"SPARC-specific data")						\
-
_ELF_DEFINE_SHT(SHT_AMD64_UNWIND,    0x70000001UL,			\
-
	"unwind tables for the AMD64")					\
-
_ELF_DEFINE_SHT(SHT_ORDERED,         0x7FFFFFFFUL,			\
-
	"sort entries in the section")					\
-
_ELF_DEFINE_SHT(SHT_HIPROC,          0x7FFFFFFFUL,			\
-
	"end of processor-specific range")				\
-
_ELF_DEFINE_SHT(SHT_LOUSER,          0x80000000UL,			\
-
	"start of application-specific range")				\
-
_ELF_DEFINE_SHT(SHT_HIUSER,          0xFFFFFFFFUL,			\
-
	"end of application-specific range")
-

-
#undef	_ELF_DEFINE_SHT
-
#define	_ELF_DEFINE_SHT(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_SECTION_TYPES()
-
	SHT__LAST__ = SHT_HIUSER
-
};
-

-
/* Aliases for section types. */
-
#define	SHT_GNU_verdef		SHT_SUNW_verdef
-
#define	SHT_GNU_verneed		SHT_SUNW_verneed
-
#define	SHT_GNU_versym		SHT_SUNW_versym
-

-
/*
-
 * Symbol binding information.
-
 */
-

-
#define	_ELF_DEFINE_SYMBOL_BINDING()					\
-
_ELF_DEFINE_STB(STB_LOCAL,           0,					\
-
	"not visible outside defining object file")			\
-
_ELF_DEFINE_STB(STB_GLOBAL,          1,					\
-
	"visible across all object files being combined")		\
-
_ELF_DEFINE_STB(STB_WEAK,            2,					\
-
	"visible across all object files but with low precedence")	\
-
_ELF_DEFINE_STB(STB_LOOS,            10, "start of OS-specific range")	\
-
_ELF_DEFINE_STB(STB_GNU_UNIQUE,      10, "unique symbol (GNU)")		\
-
_ELF_DEFINE_STB(STB_HIOS,            12, "end of OS-specific range")	\
-
_ELF_DEFINE_STB(STB_LOPROC,          13,				\
-
	"start of processor-specific range")				\
-
_ELF_DEFINE_STB(STB_HIPROC,          15,				\
-
	"end of processor-specific range")
-

-
#undef	_ELF_DEFINE_STB
-
#define	_ELF_DEFINE_STB(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_SYMBOL_BINDING()
-
	STB__LAST__
-
};
-

-
/*
-
 * Symbol types
-
 */
-

-
#define	_ELF_DEFINE_SYMBOL_TYPES()					\
-
_ELF_DEFINE_STT(STT_NOTYPE,          0, "unspecified type")		\
-
_ELF_DEFINE_STT(STT_OBJECT,          1, "data object")			\
-
_ELF_DEFINE_STT(STT_FUNC,            2, "executable code")		\
-
_ELF_DEFINE_STT(STT_SECTION,         3, "section")			\
-
_ELF_DEFINE_STT(STT_FILE,            4, "source file")			\
-
_ELF_DEFINE_STT(STT_COMMON,          5, "uninitialized common block")	\
-
_ELF_DEFINE_STT(STT_TLS,             6, "thread local storage")		\
-
_ELF_DEFINE_STT(STT_LOOS,            10, "start of OS-specific types")	\
-
_ELF_DEFINE_STT(STT_HIOS,            12, "end of OS-specific types")	\
-
_ELF_DEFINE_STT(STT_LOPROC,          13,				\
-
	"start of processor-specific types")				\
-
_ELF_DEFINE_STT(STT_ARM_TFUNC,       13, "Thumb function (GNU)")	\
-
_ELF_DEFINE_STT(STT_ARM_16BIT,       15, "Thumb label (GNU)")		\
-
_ELF_DEFINE_STT(STT_SPARC_REGISTER,  13, "SPARC register information")	\
-
_ELF_DEFINE_STT(STT_HIPROC,          15,				\
-
	"end of processor-specific types")
-

-
#undef	_ELF_DEFINE_STT
-
#define	_ELF_DEFINE_STT(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_SYMBOL_TYPES()
-
	STT__LAST__
-
};
-

-
/*
-
 * Symbol binding.
-
 */
-

-
#define	_ELF_DEFINE_SYMBOL_BINDING_KINDS()		\
-
_ELF_DEFINE_SYB(SYMINFO_BT_SELF,	0xFFFFU,	\
-
	"bound to self")				\
-
_ELF_DEFINE_SYB(SYMINFO_BT_PARENT,	0xFFFEU,	\
-
	"bound to parent")				\
-
_ELF_DEFINE_SYB(SYMINFO_BT_NONE,	0xFFFDU,	\
-
	"no special binding")
-

-
#undef	_ELF_DEFINE_SYB
-
#define	_ELF_DEFINE_SYB(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_SYMBOL_BINDING_KINDS()
-
	SYMINFO__LAST__
-
};
-

-
/*
-
 * Symbol visibility.
-
 */
-

-
#define	_ELF_DEFINE_SYMBOL_VISIBILITY()		\
-
_ELF_DEFINE_STV(STV_DEFAULT,         0,		\
-
	"as specified by symbol type")		\
-
_ELF_DEFINE_STV(STV_INTERNAL,        1,		\
-
	"as defined by processor semantics")	\
-
_ELF_DEFINE_STV(STV_HIDDEN,          2,		\
-
	"hidden from other components")		\
-
_ELF_DEFINE_STV(STV_PROTECTED,       3,		\
-
	"local references are not preemptable")
-

-
#undef	_ELF_DEFINE_STV
-
#define	_ELF_DEFINE_STV(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_SYMBOL_VISIBILITY()
-
	STV__LAST__
-
};
-

-
/*
-
 * Symbol flags.
-
 */
-
#define	_ELF_DEFINE_SYMBOL_FLAGS()		\
-
_ELF_DEFINE_SYF(SYMINFO_FLG_DIRECT,	0x01,	\
-
	"directly assocated reference")		\
-
_ELF_DEFINE_SYF(SYMINFO_FLG_COPY,	0x04,	\
-
	"definition by copy-relocation")	\
-
_ELF_DEFINE_SYF(SYMINFO_FLG_LAZYLOAD,	0x08,	\
-
	"object should be lazily loaded")	\
-
_ELF_DEFINE_SYF(SYMINFO_FLG_DIRECTBIND,	0x10,	\
-
	"reference should be directly bound")	\
-
_ELF_DEFINE_SYF(SYMINFO_FLG_NOEXTDIRECT, 0x20,	\
-
	"external references not allowed to bind to definition")
-

-
#undef	_ELF_DEFINE_SYF
-
#define	_ELF_DEFINE_SYF(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_SYMBOL_FLAGS()
-
	SYMINFO_FLG__LAST__
-
};
-

-
/*
-
 * Version dependencies.
-
 */
-
#define	_ELF_DEFINE_VERSIONING_DEPENDENCIES()			\
-
_ELF_DEFINE_VERD(VER_NDX_LOCAL,		0,	"local scope")	\
-
_ELF_DEFINE_VERD(VER_NDX_GLOBAL,	1,	"global scope")
-
#undef	_ELF_DEFINE_VERD
-
#define	_ELF_DEFINE_VERD(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_VERSIONING_DEPENDENCIES()
-
	VER_NDX__LAST__
-
};
-

-
/*
-
 * Version flags.
-
 */
-
#define	_ELF_DEFINE_VERSIONING_FLAGS()				\
-
_ELF_DEFINE_VERF(VER_FLG_BASE,		0x1,	"file version") \
-
_ELF_DEFINE_VERF(VER_FLG_WEAK,		0x2,	"weak version")
-
#undef	_ELF_DEFINE_VERF
-
#define	_ELF_DEFINE_VERF(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_VERSIONING_FLAGS()
-
	VER_FLG__LAST__
-
};
-

-
/*
-
 * Version needs
-
 */
-
#define	_ELF_DEFINE_VERSIONING_NEEDS()					\
-
_ELF_DEFINE_VRN(VER_NEED_NONE,		0,	"invalid version")	\
-
_ELF_DEFINE_VRN(VER_NEED_CURRENT,	1,	"current version")
-
#undef	_ELF_DEFINE_VRN
-
#define	_ELF_DEFINE_VRN(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_VERSIONING_NEEDS()
-
	VER_NEED__LAST__
-
};
-

-
/*
-
 * Version numbers.
-
 */
-
#define	_ELF_DEFINE_VERSIONING_NUMBERS()				\
-
_ELF_DEFINE_VRNU(VER_DEF_NONE,		0,	"invalid version")	\
-
_ELF_DEFINE_VRNU(VER_DEF_CURRENT,	1, 	"current version")
-
#undef	_ELF_DEFINE_VRNU
-
#define	_ELF_DEFINE_VRNU(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_VERSIONING_NUMBERS()
-
	VER_DEF__LAST__
-
};
-

-
/**
-
 ** Relocation types.
-
 **/
-

-
#define	_ELF_DEFINE_386_RELOCATIONS()		\
-
_ELF_DEFINE_RELOC(R_386_NONE,		0)	\
-
_ELF_DEFINE_RELOC(R_386_32,		1)	\
-
_ELF_DEFINE_RELOC(R_386_PC32,		2)	\
-
_ELF_DEFINE_RELOC(R_386_GOT32,		3)	\
-
_ELF_DEFINE_RELOC(R_386_PLT32,		4)	\
-
_ELF_DEFINE_RELOC(R_386_COPY,		5)	\
-
_ELF_DEFINE_RELOC(R_386_GLOB_DAT,	6)	\
-
_ELF_DEFINE_RELOC(R_386_JUMP_SLOT,	7)	\
-
_ELF_DEFINE_RELOC(R_386_RELATIVE,	8)	\
-
_ELF_DEFINE_RELOC(R_386_GOTOFF,		9)	\
-
_ELF_DEFINE_RELOC(R_386_GOTPC,		10)	\
-
_ELF_DEFINE_RELOC(R_386_32PLT,		11)	\
-
_ELF_DEFINE_RELOC(R_386_16,		20)	\
-
_ELF_DEFINE_RELOC(R_386_PC16,		21)	\
-
_ELF_DEFINE_RELOC(R_386_8,		22)	\
-
_ELF_DEFINE_RELOC(R_386_PC8,		23)
-

-
/*
-
 */
-
#define	_ELF_DEFINE_AARCH64_RELOCATIONS()		\
-
_ELF_DEFINE_RELOC(R_AARCH64_ABS64,		257)	\
-
_ELF_DEFINE_RELOC(R_AARCH64_ABS32,		258)	\
-

-
/*
-
 * These are the symbols used in the Sun ``Linkers and Loaders
-
 * Guide'', Document No: 817-1984-17.  See the X86_64 relocations list
-
 * below for the spellings used in the ELF specification.
-
 */
-
#define	_ELF_DEFINE_AMD64_RELOCATIONS()		\
-
_ELF_DEFINE_RELOC(R_AMD64_NONE,		0)	\
-
_ELF_DEFINE_RELOC(R_AMD64_64,		1)	\
-
_ELF_DEFINE_RELOC(R_AMD64_PC32,		2)	\
-
_ELF_DEFINE_RELOC(R_AMD64_GOT32,	3)	\
-
_ELF_DEFINE_RELOC(R_AMD64_PLT32,	4)	\
-
_ELF_DEFINE_RELOC(R_AMD64_COPY,		5)	\
-
_ELF_DEFINE_RELOC(R_AMD64_GLOB_DAT,	6)	\
-
_ELF_DEFINE_RELOC(R_AMD64_JUMP_SLOT,	7)	\
-
_ELF_DEFINE_RELOC(R_AMD64_RELATIVE,	8)	\
-
_ELF_DEFINE_RELOC(R_AMD64_GOTPCREL,	9)	\
-
_ELF_DEFINE_RELOC(R_AMD64_32,		10)	\
-
_ELF_DEFINE_RELOC(R_AMD64_32S,		11)	\
-
_ELF_DEFINE_RELOC(R_AMD64_16,		12)	\
-
_ELF_DEFINE_RELOC(R_AMD64_PC16,		13)	\
-
_ELF_DEFINE_RELOC(R_AMD64_8,		14)	\
-
_ELF_DEFINE_RELOC(R_AMD64_PC8,		15)	\
-
_ELF_DEFINE_RELOC(R_AMD64_PC64,		24)	\
-
_ELF_DEFINE_RELOC(R_AMD64_GOTOFF64,	25)	\
-
_ELF_DEFINE_RELOC(R_AMD64_GOTPC32,	26)
-

-
/*
-
 * Relocation definitions from the ARM ELF ABI, version "ARM IHI
-
 * 0044E" released on 30th November 2012.
-
 */
-
#define	_ELF_DEFINE_ARM_RELOCATIONS()			\
-
_ELF_DEFINE_RELOC(R_ARM_NONE,			0)	\
-
_ELF_DEFINE_RELOC(R_ARM_PC24,			1)	\
-
_ELF_DEFINE_RELOC(R_ARM_ABS32,			2)	\
-
_ELF_DEFINE_RELOC(R_ARM_REL32,			3)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDR_PC_G0,		4)	\
-
_ELF_DEFINE_RELOC(R_ARM_ABS16,			5)	\
-
_ELF_DEFINE_RELOC(R_ARM_ABS12,			6)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_ABS5,		7)	\
-
_ELF_DEFINE_RELOC(R_ARM_ABS8,			8)	\
-
_ELF_DEFINE_RELOC(R_ARM_SBREL32,		9)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_CALL,		10)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_PC8,		11)	\
-
_ELF_DEFINE_RELOC(R_ARM_BREL_ADJ,		12)	\
-
_ELF_DEFINE_RELOC(R_ARM_SWI24,			13)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_DESC,		13)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_SWI8,		14)	\
-
_ELF_DEFINE_RELOC(R_ARM_XPC25,			15)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_XPC22,		16)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_DTPMOD32,		17)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_DTPOFF32,		18)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_TPOFF32,		19)	\
-
_ELF_DEFINE_RELOC(R_ARM_COPY,			20)	\
-
_ELF_DEFINE_RELOC(R_ARM_GLOB_DAT,		21)	\
-
_ELF_DEFINE_RELOC(R_ARM_JUMP_SLOT,		22)	\
-
_ELF_DEFINE_RELOC(R_ARM_RELATIVE,		23)	\
-
_ELF_DEFINE_RELOC(R_ARM_GOTOFF32,		24)	\
-
_ELF_DEFINE_RELOC(R_ARM_BASE_PREL,		25)	\
-
_ELF_DEFINE_RELOC(R_ARM_GOT_BREL,		26)	\
-
_ELF_DEFINE_RELOC(R_ARM_PLT32,			27)	\
-
_ELF_DEFINE_RELOC(R_ARM_CALL,			28)	\
-
_ELF_DEFINE_RELOC(R_ARM_JUMP24,			29)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP24,		30)	\
-
_ELF_DEFINE_RELOC(R_ARM_BASE_ABS,		31)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_PCREL_7_0,		32)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_PCREL_15_8,		33)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_PCREL_23_15,	34)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDR_SBREL_11_0_NC,	35)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_SBREL_19_12_NC,	36)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_SBREL_27_20_CK,	37)	\
-
_ELF_DEFINE_RELOC(R_ARM_TARGET1,		38)	\
-
_ELF_DEFINE_RELOC(R_ARM_SBREL31,		39)	\
-
_ELF_DEFINE_RELOC(R_ARM_V4BX,			40)	\
-
_ELF_DEFINE_RELOC(R_ARM_TARGET2,		41)	\
-
_ELF_DEFINE_RELOC(R_ARM_PREL31,			42)	\
-
_ELF_DEFINE_RELOC(R_ARM_MOVW_ABS_NC,		43)	\
-
_ELF_DEFINE_RELOC(R_ARM_MOVT_ABS,		44)	\
-
_ELF_DEFINE_RELOC(R_ARM_MOVW_PREL_NC,		45)	\
-
_ELF_DEFINE_RELOC(R_ARM_MOVT_PREL,		46)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_ABS_NC,	47)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_MOVT_ABS,		48)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_PREL_NC,	49)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_MOVT_PREL,		50)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP19,		51)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP6,		52)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_ALU_PREL_11_0,	53)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_PC12,		54)	\
-
_ELF_DEFINE_RELOC(R_ARM_ABS32_NOI,		55)	\
-
_ELF_DEFINE_RELOC(R_ARM_REL32_NOI,		56)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G0_NC,		57)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G0,		58)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G1_NC,		59)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G1,		60)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G2,		61)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDR_PC_G1,		62)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDR_PC_G2,		63)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDRS_PC_G0,		64)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDRS_PC_G1,		65)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDRS_PC_G2,		66)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDC_PC_G0,		67)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDC_PC_G1,		68)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDC_PC_G2,		69)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G0_NC,		70)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G0,		71)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G1_NC,		72)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G1,		73)	\
-
_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G2,		74)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDR_SB_G0,		75)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDR_SB_G1,		76)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDR_SB_G2,		77)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDRS_SB_G0,		78)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDRS_SB_G1,		79)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDRS_SB_G2,		80)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDC_SB_G0,		81)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDC_SB_G1,		82)	\
-
_ELF_DEFINE_RELOC(R_ARM_LDC_SB_G2,		83)	\
-
_ELF_DEFINE_RELOC(R_ARM_MOVW_BREL_NC,		84)	\
-
_ELF_DEFINE_RELOC(R_ARM_MOVT_BREL,		85)	\
-
_ELF_DEFINE_RELOC(R_ARM_MOVW_BREL,		86)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_BREL_NC,	87)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_MOVT_BREL,		88)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_BREL,		89)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_GOTDESC,		90)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_CALL,		91)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_DESCSEQ,		92)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_TLS_CALL,		93)	\
-
_ELF_DEFINE_RELOC(R_ARM_PLT32_ABS,		94)	\
-
_ELF_DEFINE_RELOC(R_ARM_GOT_ABS,		95)	\
-
_ELF_DEFINE_RELOC(R_ARM_GOT_PREL,		96)	\
-
_ELF_DEFINE_RELOC(R_ARM_GOT_BREL12,		97)	\
-
_ELF_DEFINE_RELOC(R_ARM_GOTOFF12,		98)	\
-
_ELF_DEFINE_RELOC(R_ARM_GOTRELAX,		99)	\
-
_ELF_DEFINE_RELOC(R_ARM_GNU_VTENTRY,		100)	\
-
_ELF_DEFINE_RELOC(R_ARM_GNU_VTINHERIT,		101)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP11,		102)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_JUMP8,		103)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_GD32,		104)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_LDM32,		105)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_LDO32,		106)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_IE32,		107)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_LE32,		108)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_LDO12,		109)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_LE12,		110)	\
-
_ELF_DEFINE_RELOC(R_ARM_TLS_IE12GP,		111)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_0,		112)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_1,		113)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_2,		114)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_3,		115)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_4,		116)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_5,		117)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_6,		118)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_7,		119)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_8,		120)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_9,		121)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_10,		122)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_11,		123)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_12,		124)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_13,		125)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_14,		126)	\
-
_ELF_DEFINE_RELOC(R_ARM_PRIVATE_15,		127)	\
-
_ELF_DEFINE_RELOC(R_ARM_ME_TOO,			128)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_TLS_DESCSEQ16,	129)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_TLS_DESCSEQ32,	130)	\
-
_ELF_DEFINE_RELOC(R_ARM_THM_GOT_BREL12,		131)	\
-
_ELF_DEFINE_RELOC(R_ARM_IRELATIVE,		140)
-

-
#define	_ELF_DEFINE_IA64_RELOCATIONS()			\
-
_ELF_DEFINE_RELOC(R_IA_64_NONE,			0)	\
-
_ELF_DEFINE_RELOC(R_IA_64_IMM14,		0x21)	\
-
_ELF_DEFINE_RELOC(R_IA_64_IMM22,		0x22)	\
-
_ELF_DEFINE_RELOC(R_IA_64_IMM64,		0x23)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DIR32MSB,		0x24)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DIR32LSB,		0x25)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DIR64MSB,		0x26)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DIR64LSB,		0x27)	\
-
_ELF_DEFINE_RELOC(R_IA_64_GPREL22,		0x2a)	\
-
_ELF_DEFINE_RELOC(R_IA_64_GPREL64I,		0x2b)	\
-
_ELF_DEFINE_RELOC(R_IA_64_GPREL32MSB,		0x2c)	\
-
_ELF_DEFINE_RELOC(R_IA_64_GPREL32LSB,		0x2d)	\
-
_ELF_DEFINE_RELOC(R_IA_64_GPREL64MSB,		0x2e)	\
-
_ELF_DEFINE_RELOC(R_IA_64_GPREL64LSB,		0x2f)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF22,		0x32)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF64I,		0x33)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PLTOFF22,		0x3a)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PLTOFF64I,		0x3b)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PLTOFF64MSB,		0x3e)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PLTOFF64LSB,		0x3f)	\
-
_ELF_DEFINE_RELOC(R_IA_64_FPTR64I,		0x43)	\
-
_ELF_DEFINE_RELOC(R_IA_64_FPTR32MSB,		0x44)	\
-
_ELF_DEFINE_RELOC(R_IA_64_FPTR32LSB,		0x45)	\
-
_ELF_DEFINE_RELOC(R_IA_64_FPTR64MSB,		0x46)	\
-
_ELF_DEFINE_RELOC(R_IA_64_FPTR64LSB,		0x47)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL60B,		0x48)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL21B,		0x49)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL21M,		0x4a)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL21F,		0x4b)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL32MSB,		0x4c)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL32LSB,		0x4d)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL64MSB,		0x4e)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL64LSB,		0x4f)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR22,		0x52)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR64I,	0x53)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR32MSB,	0x54)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR32LSB,	0x55)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR64MSB,	0x56)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR64LSB,	0x57)	\
-
_ELF_DEFINE_RELOC(R_IA_64_SEGREL32MSB,		0x5c)	\
-
_ELF_DEFINE_RELOC(R_IA_64_SEGREL32LSB,		0x5d)	\
-
_ELF_DEFINE_RELOC(R_IA_64_SEGREL64MSB,		0x5e)	\
-
_ELF_DEFINE_RELOC(R_IA_64_SEGREL64LSB,		0x5f)	\
-
_ELF_DEFINE_RELOC(R_IA_64_SECREL32MSB,		0x64)	\
-
_ELF_DEFINE_RELOC(R_IA_64_SECREL32LSB,		0x65)	\
-
_ELF_DEFINE_RELOC(R_IA_64_SECREL64MSB,		0x66)	\
-
_ELF_DEFINE_RELOC(R_IA_64_SECREL64LSB,		0x67)	\
-
_ELF_DEFINE_RELOC(R_IA_64_REL32MSB,		0x6c)	\
-
_ELF_DEFINE_RELOC(R_IA_64_REL32LSB,		0x6d)	\
-
_ELF_DEFINE_RELOC(R_IA_64_REL64MSB,		0x6e)	\
-
_ELF_DEFINE_RELOC(R_IA_64_REL64LSB,		0x6f)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTV32MSB,		0x74)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTV32LSB,		0x75)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTV64MSB,		0x76)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTV64LSB,		0x77)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL21BI,		0x79)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL22,		0x7A)	\
-
_ELF_DEFINE_RELOC(R_IA_64_PCREL64I,		0x7B)	\
-
_ELF_DEFINE_RELOC(R_IA_64_IPLTMSB,		0x80)	\
-
_ELF_DEFINE_RELOC(R_IA_64_IPLTLSB,		0x81)	\
-
_ELF_DEFINE_RELOC(R_IA_64_SUB,			0x85)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF22X,		0x86)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LDXMOV,		0x87)	\
-
_ELF_DEFINE_RELOC(R_IA_64_TPREL14,		0x91)	\
-
_ELF_DEFINE_RELOC(R_IA_64_TPREL22,		0x92)	\
-
_ELF_DEFINE_RELOC(R_IA_64_TPREL64I,		0x93)	\
-
_ELF_DEFINE_RELOC(R_IA_64_TPREL64MSB,		0x96)	\
-
_ELF_DEFINE_RELOC(R_IA_64_TPREL64LSB,		0x97)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_TPREL22,	0x9A)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DTPMOD64MSB,		0xA6)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DTPMOD64LSB,		0xA7)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_DTPMOD22,	0xAA)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DTPREL14,		0xB1)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DTPREL22,		0xB2)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DTPREL64I,		0xB3)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DTPREL32MSB,		0xB4)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DTPREL32LSB,		0xB5)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DTPREL64MSB,		0xB6)	\
-
_ELF_DEFINE_RELOC(R_IA_64_DTPREL64LSB,		0xB7)	\
-
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_DTPREL22,	0xBA)
-

-
#define	_ELF_DEFINE_MIPS_RELOCATIONS()			\
-
_ELF_DEFINE_RELOC(R_MIPS_NONE,			0)	\
-
_ELF_DEFINE_RELOC(R_MIPS_16,			1)	\
-
_ELF_DEFINE_RELOC(R_MIPS_32,			2)	\
-
_ELF_DEFINE_RELOC(R_MIPS_REL32,			3)	\
-
_ELF_DEFINE_RELOC(R_MIPS_26,			4)	\
-
_ELF_DEFINE_RELOC(R_MIPS_HI16,			5)	\
-
_ELF_DEFINE_RELOC(R_MIPS_LO16,			6)	\
-
_ELF_DEFINE_RELOC(R_MIPS_GPREL16,		7)	\
-
_ELF_DEFINE_RELOC(R_MIPS_LITERAL, 		8)	\
-
_ELF_DEFINE_RELOC(R_MIPS_GOT16,			9)	\
-
_ELF_DEFINE_RELOC(R_MIPS_PC16,			10)	\
-
_ELF_DEFINE_RELOC(R_MIPS_CALL16,		11)	\
-
_ELF_DEFINE_RELOC(R_MIPS_GPREL32,		12)	\
-
_ELF_DEFINE_RELOC(R_MIPS_64,			18)	\
-
_ELF_DEFINE_RELOC(R_MIPS_GOTHI16,		21)	\
-
_ELF_DEFINE_RELOC(R_MIPS_GOTLO16,		22)	\
-
_ELF_DEFINE_RELOC(R_MIPS_CALLHI16,		30)	\
-
_ELF_DEFINE_RELOC(R_MIPS_CALLLO16,		31)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPMOD32,		38)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL32,		39)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPMOD64,		40)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL64,		41)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_GD,		42)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_LDM,		43)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL_HI16,	44)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL_LO16,	45)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_GOTTPREL,		46)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL32,		47)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL64,		48)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL_HI16,	49)	\
-
_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL_LO16,	50)
-

-
#define	_ELF_DEFINE_PPC32_RELOCATIONS()		\
-
_ELF_DEFINE_RELOC(R_PPC_NONE,		0)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR32,		1)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR24,		2)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR16,		3)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR16_LO,	4)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR16_HI,	5)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR16_HA,	6)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR14,		7)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR14_BRTAKEN,	8)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR14_BRNTAKEN, 9)	\
-
_ELF_DEFINE_RELOC(R_PPC_REL24,		10)	\
-
_ELF_DEFINE_RELOC(R_PPC_REL14,		11)	\
-
_ELF_DEFINE_RELOC(R_PPC_REL14_BRTAKEN,	12)	\
-
_ELF_DEFINE_RELOC(R_PPC_REL14_BRNTAKEN,	13)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT16,		14)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT16_LO,	15)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT16_HI,	16)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT16_HA,	17)	\
-
_ELF_DEFINE_RELOC(R_PPC_PLTREL24,	18)	\
-
_ELF_DEFINE_RELOC(R_PPC_COPY,		19)	\
-
_ELF_DEFINE_RELOC(R_PPC_GLOB_DAT,	20)	\
-
_ELF_DEFINE_RELOC(R_PPC_JMP_SLOT,	21)	\
-
_ELF_DEFINE_RELOC(R_PPC_RELATIVE,	22)	\
-
_ELF_DEFINE_RELOC(R_PPC_LOCAL24PC,	23)	\
-
_ELF_DEFINE_RELOC(R_PPC_UADDR32,	24)	\
-
_ELF_DEFINE_RELOC(R_PPC_UADDR16,	25)	\
-
_ELF_DEFINE_RELOC(R_PPC_REL32,		26)	\
-
_ELF_DEFINE_RELOC(R_PPC_PLT32,		27)	\
-
_ELF_DEFINE_RELOC(R_PPC_PLTREL32,	28)	\
-
_ELF_DEFINE_RELOC(R_PPC_PLT16_LO,	29)	\
-
_ELF_DEFINE_RELOC(R_PPC_PLT16_HI,	30)	\
-
_ELF_DEFINE_RELOC(R_PPC_PLT16_HA,	31)	\
-
_ELF_DEFINE_RELOC(R_PPC_SDAREL16,	32)	\
-
_ELF_DEFINE_RELOC(R_PPC_SECTOFF,	33)	\
-
_ELF_DEFINE_RELOC(R_PPC_SECTOFF_LO,	34)	\
-
_ELF_DEFINE_RELOC(R_PPC_SECTOFF_HI,	35)	\
-
_ELF_DEFINE_RELOC(R_PPC_SECTOFF_HA,	36)	\
-
_ELF_DEFINE_RELOC(R_PPC_ADDR30,		37)	\
-
_ELF_DEFINE_RELOC(R_PPC_TLS,		67)	\
-
_ELF_DEFINE_RELOC(R_PPC_DTPMOD32,	68)	\
-
_ELF_DEFINE_RELOC(R_PPC_TPREL16,	69)	\
-
_ELF_DEFINE_RELOC(R_PPC_TPREL16_LO,	70)	\
-
_ELF_DEFINE_RELOC(R_PPC_TPREL16_HI,	71)	\
-
_ELF_DEFINE_RELOC(R_PPC_TPREL16_HA,	72)	\
-
_ELF_DEFINE_RELOC(R_PPC_TPREL32,	73)	\
-
_ELF_DEFINE_RELOC(R_PPC_DTPREL16,	74)	\
-
_ELF_DEFINE_RELOC(R_PPC_DTPREL16_LO,	75)	\
-
_ELF_DEFINE_RELOC(R_PPC_DTPREL16_HI,	76)	\
-
_ELF_DEFINE_RELOC(R_PPC_DTPREL16_HA,	77)	\
-
_ELF_DEFINE_RELOC(R_PPC_DTPREL32,	78)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16,	79)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16_LO,	80)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16_HI,	81)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16_HA,	82)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16,	83)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16_LO,	84)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16_HI,	85)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16_HA,	86)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16,	87)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16_LO,	88)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16_HI,	89)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16_HA,	90)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16,	91)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16_LO, 92)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16_HI, 93)	\
-
_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16_HA, 94)	\
-
_ELF_DEFINE_RELOC(R_PPC_TLSGD,		95)	\
-
_ELF_DEFINE_RELOC(R_PPC_TLSLD,		96)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR32,	101)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16,	102)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16_LO,	103)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16_HI,	104)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16_HA,	105)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_SDAI16,	106)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_SDA2I16,	107)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_SDA2REL,	108)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_SDA21,	109)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_MRKREF,	110)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_RELSEC16,	111)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_RELST_LO,	112)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_RELST_HI,	113)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_RELST_HA,	114)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_BIT_FLD,	115)	\
-
_ELF_DEFINE_RELOC(R_PPC_EMB_RELSDA,	116)	\
-

-
#define	_ELF_DEFINE_PPC64_RELOCATIONS()			\
-
_ELF_DEFINE_RELOC(R_PPC64_NONE,			0)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR32,		1)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR24,		2)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16,		3)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_LO,		4)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HI,		5)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HA,		6)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR14,		7)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR14_BRTAKEN,	8)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR14_BRNTAKEN,	9)	\
-
_ELF_DEFINE_RELOC(R_PPC64_REL24,		10)	\
-
_ELF_DEFINE_RELOC(R_PPC64_REL14,		11)	\
-
_ELF_DEFINE_RELOC(R_PPC64_REL14_BRTAKEN,	12)	\
-
_ELF_DEFINE_RELOC(R_PPC64_REL14_BRNTAKEN,	13)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT16,		14)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT16_LO,		15)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT16_HI,		16)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT16_HA,		17)	\
-
_ELF_DEFINE_RELOC(R_PPC64_COPY,			19)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GLOB_DAT,		20)	\
-
_ELF_DEFINE_RELOC(R_PPC64_JMP_SLOT,		21)	\
-
_ELF_DEFINE_RELOC(R_PPC64_RELATIVE,		22)	\
-
_ELF_DEFINE_RELOC(R_PPC64_UADDR32,		24)	\
-
_ELF_DEFINE_RELOC(R_PPC64_UADDR16,		25)	\
-
_ELF_DEFINE_RELOC(R_PPC64_REL32,		26)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLT32,		27)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLTREL32,		28)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLT16_LO,		29)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLT16_HI,		30)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLT16_HA,		31)	\
-
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF,		33)	\
-
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_LO,		34)	\
-
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_HI,		35)	\
-
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_HA,		36)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR30,		37)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR64,		38)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHER,	39)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHERA,	40)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHEST,	41)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHESTA,	42)	\
-
_ELF_DEFINE_RELOC(R_PPC64_UADDR64,		43)	\
-
_ELF_DEFINE_RELOC(R_PPC64_REL64,		44)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLT64,		45)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLTREL64,		46)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TOC16,		47)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TOC16_LO,		48)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TOC16_HI,		49)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TOC16_HA,		50)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TOC,			51)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16,		52)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_LO,		53)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_HI,		54)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_HA,		55)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_DS,		56)	\
-
_ELF_DEFINE_RELOC(R_PPC64_ADDR16_LO_DS,		57)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT16_DS,		58)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT16_LO_DS,		59)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLT16_LO_DS,		60)	\
-
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_DS,		61)	\
-
_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_LO_DS,	62)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TOC16_DS,		63)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TOC16_LO_DS,		64)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_DS,		65)	\
-
_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_LO_DS,	66)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TLS,			67)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPMOD64,		68)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16,		69)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_LO,		60)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HI,		71)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HA,		72)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL64,		73)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16,		74)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_LO,		75)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HI,		76)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HA,		77)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL64,		78)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16,		79)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16_LO,	80)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16_HI,	81)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16_HA,	82)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16,		83)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16_LO,	84)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16_HI,	85)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16_HA,	86)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_DS,	87)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_LO_DS,	88)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_HI,	89)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_HA,	90)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_DS,	91)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_LO_DS,	92)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_HI,	93)	\
-
_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_HA,	94)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_DS,		95)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_LO_DS,	96)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHER,	97)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHERA,	98)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHEST,	99)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHESTA,	100)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_DS,		101)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_LO_DS,	102)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHER,	103)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHERA,	104)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHEST,	105)	\
-
_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHESTA,	106)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TLSGD,		107)	\
-
_ELF_DEFINE_RELOC(R_PPC64_TLSLD,		108)
-

-
#define	_ELF_DEFINE_RISCV_RELOCATIONS()			\
-
_ELF_DEFINE_RELOC(R_RISCV_NONE,			0)	\
-
_ELF_DEFINE_RELOC(R_RISCV_32,			1)	\
-
_ELF_DEFINE_RELOC(R_RISCV_64,			2)	\
-
_ELF_DEFINE_RELOC(R_RISCV_RELATIVE,		3)	\
-
_ELF_DEFINE_RELOC(R_RISCV_COPY,			4)	\
-
_ELF_DEFINE_RELOC(R_RISCV_JUMP_SLOT,		5)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TLS_DTPMOD32,		6)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TLS_DTPMOD64,		7)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TLS_DTPREL32,		8)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TLS_DTPREL64,		9)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TLS_TPREL32,		10)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TLS_TPREL64,		11)	\
-
_ELF_DEFINE_RELOC(R_RISCV_BRANCH,		16)	\
-
_ELF_DEFINE_RELOC(R_RISCV_JAL,			17)	\
-
_ELF_DEFINE_RELOC(R_RISCV_CALL,			18)	\
-
_ELF_DEFINE_RELOC(R_RISCV_CALL_PLT,		19)	\
-
_ELF_DEFINE_RELOC(R_RISCV_GOT_HI20,		20)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TLS_GOT_HI20,		21)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TLS_GD_HI20,		22)	\
-
_ELF_DEFINE_RELOC(R_RISCV_PCREL_HI20,		23)	\
-
_ELF_DEFINE_RELOC(R_RISCV_PCREL_LO12_I,		24)	\
-
_ELF_DEFINE_RELOC(R_RISCV_PCREL_LO12_S,		25)	\
-
_ELF_DEFINE_RELOC(R_RISCV_HI20,			26)	\
-
_ELF_DEFINE_RELOC(R_RISCV_LO12_I,		27)	\
-
_ELF_DEFINE_RELOC(R_RISCV_LO12_S,		28)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TPREL_HI20,		29)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TPREL_LO12_I,		30)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TPREL_LO12_S,		31)	\
-
_ELF_DEFINE_RELOC(R_RISCV_TPREL_ADD,		32)	\
-
_ELF_DEFINE_RELOC(R_RISCV_ADD8,			33)	\
-
_ELF_DEFINE_RELOC(R_RISCV_ADD16,		34)	\
-
_ELF_DEFINE_RELOC(R_RISCV_ADD32,		35)	\
-
_ELF_DEFINE_RELOC(R_RISCV_ADD64,		36)	\
-
_ELF_DEFINE_RELOC(R_RISCV_SUB8,			37)	\
-
_ELF_DEFINE_RELOC(R_RISCV_SUB16,		38)	\
-
_ELF_DEFINE_RELOC(R_RISCV_SUB32,		39)	\
-
_ELF_DEFINE_RELOC(R_RISCV_SUB64,		40)	\
-
_ELF_DEFINE_RELOC(R_RISCV_GNU_VTINHERIT,	41)	\
-
_ELF_DEFINE_RELOC(R_RISCV_GNU_VTENTRY,		42)	\
-
_ELF_DEFINE_RELOC(R_RISCV_ALIGN,		43)	\
-
_ELF_DEFINE_RELOC(R_RISCV_RVC_BRANCH,		44)	\
-
_ELF_DEFINE_RELOC(R_RISCV_RVC_JUMP,		45)
-

-
#define	_ELF_DEFINE_SPARC_RELOCATIONS()		\
-
_ELF_DEFINE_RELOC(R_SPARC_NONE,		0)	\
-
_ELF_DEFINE_RELOC(R_SPARC_8,		1)	\
-
_ELF_DEFINE_RELOC(R_SPARC_16,		2)	\
-
_ELF_DEFINE_RELOC(R_SPARC_32, 		3)	\
-
_ELF_DEFINE_RELOC(R_SPARC_DISP8,	4)	\
-
_ELF_DEFINE_RELOC(R_SPARC_DISP16,	5)	\
-
_ELF_DEFINE_RELOC(R_SPARC_DISP32,	6)	\
-
_ELF_DEFINE_RELOC(R_SPARC_WDISP30,	7)	\
-
_ELF_DEFINE_RELOC(R_SPARC_WDISP22,	8)	\
-
_ELF_DEFINE_RELOC(R_SPARC_HI22,		9)	\
-
_ELF_DEFINE_RELOC(R_SPARC_22,		10)	\
-
_ELF_DEFINE_RELOC(R_SPARC_13,		11)	\
-
_ELF_DEFINE_RELOC(R_SPARC_LO10,		12)	\
-
_ELF_DEFINE_RELOC(R_SPARC_GOT10,	13)	\
-
_ELF_DEFINE_RELOC(R_SPARC_GOT13,	14)	\
-
_ELF_DEFINE_RELOC(R_SPARC_GOT22,	15)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PC10,		16)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PC22,		17)	\
-
_ELF_DEFINE_RELOC(R_SPARC_WPLT30,	18)	\
-
_ELF_DEFINE_RELOC(R_SPARC_COPY,		19)	\
-
_ELF_DEFINE_RELOC(R_SPARC_GLOB_DAT,	20)	\
-
_ELF_DEFINE_RELOC(R_SPARC_JMP_SLOT,	21)	\
-
_ELF_DEFINE_RELOC(R_SPARC_RELATIVE,	22)	\
-
_ELF_DEFINE_RELOC(R_SPARC_UA32,		23)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PLT32,	24)	\
-
_ELF_DEFINE_RELOC(R_SPARC_HIPLT22,	25)	\
-
_ELF_DEFINE_RELOC(R_SPARC_LOPLT10,	26)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PCPLT32,	27)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PCPLT22,	28)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PCPLT10,	29)	\
-
_ELF_DEFINE_RELOC(R_SPARC_10,		30)	\
-
_ELF_DEFINE_RELOC(R_SPARC_11,		31)	\
-
_ELF_DEFINE_RELOC(R_SPARC_64,		32)	\
-
_ELF_DEFINE_RELOC(R_SPARC_OLO10,	33)	\
-
_ELF_DEFINE_RELOC(R_SPARC_HH22,		34)	\
-
_ELF_DEFINE_RELOC(R_SPARC_HM10,		35)	\
-
_ELF_DEFINE_RELOC(R_SPARC_LM22,		36)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PC_HH22,	37)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PC_HM10,	38)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PC_LM22,	39)	\
-
_ELF_DEFINE_RELOC(R_SPARC_WDISP16,	40)	\
-
_ELF_DEFINE_RELOC(R_SPARC_WDISP19,	41)	\
-
_ELF_DEFINE_RELOC(R_SPARC_7,		43)	\
-
_ELF_DEFINE_RELOC(R_SPARC_5,		44)	\
-
_ELF_DEFINE_RELOC(R_SPARC_6,		45)	\
-
_ELF_DEFINE_RELOC(R_SPARC_DISP64,	46)	\
-
_ELF_DEFINE_RELOC(R_SPARC_PLT64,	47)	\
-
_ELF_DEFINE_RELOC(R_SPARC_HIX22,	48)	\
-
_ELF_DEFINE_RELOC(R_SPARC_LOX10,	49)	\
-
_ELF_DEFINE_RELOC(R_SPARC_H44,		50)	\
-
_ELF_DEFINE_RELOC(R_SPARC_M44,		51)	\
-
_ELF_DEFINE_RELOC(R_SPARC_L44,		52)	\
-
_ELF_DEFINE_RELOC(R_SPARC_REGISTER,	53)	\
-
_ELF_DEFINE_RELOC(R_SPARC_UA64,		54)	\
-
_ELF_DEFINE_RELOC(R_SPARC_UA16,		55)	\
-
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_HIX22, 80)	\
-
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_LOX10, 81)	\
-
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_OP_HIX22, 82)	\
-
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_OP_LOX10, 83)	\
-
_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_OP,	84)	\
-
_ELF_DEFINE_RELOC(R_SPARC_H34,		85)
-

-
#define	_ELF_DEFINE_X86_64_RELOCATIONS()	\
-
_ELF_DEFINE_RELOC(R_X86_64_NONE,	0)	\
-
_ELF_DEFINE_RELOC(R_X86_64_64,		1)	\
-
_ELF_DEFINE_RELOC(R_X86_64_PC32,	2)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOT32,	3)	\
-
_ELF_DEFINE_RELOC(R_X86_64_PLT32,	4)	\
-
_ELF_DEFINE_RELOC(R_X86_64_COPY,	5)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GLOB_DAT,	6)	\
-
_ELF_DEFINE_RELOC(R_X86_64_JUMP_SLOT,	7)	\
-
_ELF_DEFINE_RELOC(R_X86_64_RELATIVE,	8)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOTPCREL,	9)	\
-
_ELF_DEFINE_RELOC(R_X86_64_32,		10)	\
-
_ELF_DEFINE_RELOC(R_X86_64_32S,		11)	\
-
_ELF_DEFINE_RELOC(R_X86_64_16,		12)	\
-
_ELF_DEFINE_RELOC(R_X86_64_PC16,	13)	\
-
_ELF_DEFINE_RELOC(R_X86_64_8,		14)	\
-
_ELF_DEFINE_RELOC(R_X86_64_PC8,		15)	\
-
_ELF_DEFINE_RELOC(R_X86_64_DTPMOD64,	16)	\
-
_ELF_DEFINE_RELOC(R_X86_64_DTPOFF64,	17)	\
-
_ELF_DEFINE_RELOC(R_X86_64_TPOFF64,	18)	\
-
_ELF_DEFINE_RELOC(R_X86_64_TLSGD,	19)	\
-
_ELF_DEFINE_RELOC(R_X86_64_TLSLD,	20)	\
-
_ELF_DEFINE_RELOC(R_X86_64_DTPOFF32,	21)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOTTPOFF,	22)	\
-
_ELF_DEFINE_RELOC(R_X86_64_TPOFF32,	23)	\
-
_ELF_DEFINE_RELOC(R_X86_64_PC64,	24)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOTOFF64,	25)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOTPC32,	26)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOT64,	27)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOTPCREL64,	28)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOTPC64,	29)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOTPLT64,	30)	\
-
_ELF_DEFINE_RELOC(R_X86_64_PLTOFF64,	31)	\
-
_ELF_DEFINE_RELOC(R_X86_64_SIZE32,	32)	\
-
_ELF_DEFINE_RELOC(R_X86_64_SIZE64,	33)	\
-
_ELF_DEFINE_RELOC(R_X86_64_GOTPC32_TLSDESC, 34)	\
-
_ELF_DEFINE_RELOC(R_X86_64_TLSDESC_CALL, 35)	\
-
_ELF_DEFINE_RELOC(R_X86_64_TLSDESC,	36)	\
-
_ELF_DEFINE_RELOC(R_X86_64_IRELATIVE,	37)
-

-
#define	_ELF_DEFINE_RELOCATIONS()		\
-
_ELF_DEFINE_386_RELOCATIONS()			\
-
_ELF_DEFINE_AARCH64_RELOCATIONS()		\
-
_ELF_DEFINE_AMD64_RELOCATIONS()			\
-
_ELF_DEFINE_ARM_RELOCATIONS()			\
-
_ELF_DEFINE_IA64_RELOCATIONS()			\
-
_ELF_DEFINE_MIPS_RELOCATIONS()			\
-
_ELF_DEFINE_PPC32_RELOCATIONS()			\
-
_ELF_DEFINE_PPC64_RELOCATIONS()			\
-
_ELF_DEFINE_RISCV_RELOCATIONS()			\
-
_ELF_DEFINE_SPARC_RELOCATIONS()			\
-
_ELF_DEFINE_X86_64_RELOCATIONS()
-

-
#undef	_ELF_DEFINE_RELOC
-
#define	_ELF_DEFINE_RELOC(N, V)		N = V ,
-
enum {
-
	_ELF_DEFINE_RELOCATIONS()
-
	R__LAST__
-
};
-

-
#define	PN_XNUM			0xFFFFU /* Use extended section numbering. */
-

-
/**
-
 ** ELF Types.
-
 **/
-

-
typedef uint32_t	Elf32_Addr;	/* Program address. */
-
typedef uint8_t		Elf32_Byte;	/* Unsigned tiny integer. */
-
typedef uint16_t	Elf32_Half;	/* Unsigned medium integer. */
-
typedef uint32_t	Elf32_Off;	/* File offset. */
-
typedef uint16_t	Elf32_Section;	/* Section index. */
-
typedef int32_t		Elf32_Sword;	/* Signed integer. */
-
typedef uint32_t	Elf32_Word;	/* Unsigned integer. */
-
typedef uint64_t	Elf32_Lword;	/* Unsigned long integer. */
-

-
typedef uint64_t	Elf64_Addr;	/* Program address. */
-
typedef uint8_t		Elf64_Byte;	/* Unsigned tiny integer. */
-
typedef uint16_t	Elf64_Half;	/* Unsigned medium integer. */
-
typedef uint64_t	Elf64_Off;	/* File offset. */
-
typedef uint16_t	Elf64_Section;	/* Section index. */
-
typedef int32_t		Elf64_Sword;	/* Signed integer. */
-
typedef uint32_t	Elf64_Word;	/* Unsigned integer. */
-
typedef uint64_t	Elf64_Lword;	/* Unsigned long integer. */
-
typedef uint64_t	Elf64_Xword;	/* Unsigned long integer. */
-
typedef int64_t		Elf64_Sxword;	/* Signed long integer. */
-

-

-
/*
-
 * Capability descriptors.
-
 */
-

-
/* 32-bit capability descriptor. */
-
typedef struct {
-
	Elf32_Word	c_tag;	     /* Type of entry. */
-
	union {
-
		Elf32_Word	c_val; /* Integer value. */
-
		Elf32_Addr	c_ptr; /* Pointer value. */
-
	} c_un;
-
} Elf32_Cap;
-

-
/* 64-bit capability descriptor. */
-
typedef struct {
-
	Elf64_Xword	c_tag;	     /* Type of entry. */
-
	union {
-
		Elf64_Xword	c_val; /* Integer value. */
-
		Elf64_Addr	c_ptr; /* Pointer value. */
-
	} c_un;
-
} Elf64_Cap;
-

-
/*
-
 * MIPS .conflict section entries.
-
 */
-

-
/* 32-bit entry. */
-
typedef struct {
-
	Elf32_Addr	c_index;
-
} Elf32_Conflict;
-

-
/* 64-bit entry. */
-
typedef struct {
-
	Elf64_Addr	c_index;
-
} Elf64_Conflict;
-

-
/*
-
 * Dynamic section entries.
-
 */
-

-
/* 32-bit entry. */
-
typedef struct {
-
	Elf32_Sword	d_tag;	     /* Type of entry. */
-
	union {
-
		Elf32_Word	d_val; /* Integer value. */
-
		Elf32_Addr	d_ptr; /* Pointer value. */
-
	} d_un;
-
} Elf32_Dyn;
-

-
/* 64-bit entry. */
-
typedef struct {
-
	Elf64_Sxword	d_tag;	     /* Type of entry. */
-
	union {
-
		Elf64_Xword	d_val; /* Integer value. */
-
		Elf64_Addr	d_ptr; /* Pointer value; */
-
	} d_un;
-
} Elf64_Dyn;
-

-

-
/*
-
 * The executable header (EHDR).
-
 */
-

-
/* 32 bit EHDR. */
-
typedef struct {
-
	unsigned char   e_ident[EI_NIDENT]; /* ELF identification. */
-
	Elf32_Half      e_type;	     /* Object file type (ET_*). */
-
	Elf32_Half      e_machine;   /* Machine type (EM_*). */
-
	Elf32_Word      e_version;   /* File format version (EV_*). */
-
	Elf32_Addr      e_entry;     /* Start address. */
-
	Elf32_Off       e_phoff;     /* File offset to the PHDR table. */
-
	Elf32_Off       e_shoff;     /* File offset to the SHDRheader. */
-
	Elf32_Word      e_flags;     /* Flags (EF_*). */
-
	Elf32_Half      e_ehsize;    /* Elf header size in bytes. */
-
	Elf32_Half      e_phentsize; /* PHDR table entry size in bytes. */
-
	Elf32_Half      e_phnum;     /* Number of PHDR entries. */
-
	Elf32_Half      e_shentsize; /* SHDR table entry size in bytes. */
-
	Elf32_Half      e_shnum;     /* Number of SHDR entries. */
-
	Elf32_Half      e_shstrndx;  /* Index of section name string table. */
-
} Elf32_Ehdr;
-

-

-
/* 64 bit EHDR. */
-
typedef struct {
-
	unsigned char   e_ident[EI_NIDENT]; /* ELF identification. */
-
	Elf64_Half      e_type;	     /* Object file type (ET_*). */
-
	Elf64_Half      e_machine;   /* Machine type (EM_*). */
-
	Elf64_Word      e_version;   /* File format version (EV_*). */
-
	Elf64_Addr      e_entry;     /* Start address. */
-
	Elf64_Off       e_phoff;     /* File offset to the PHDR table. */
-
	Elf64_Off       e_shoff;     /* File offset to the SHDRheader. */
-
	Elf64_Word      e_flags;     /* Flags (EF_*). */
-
	Elf64_Half      e_ehsize;    /* Elf header size in bytes. */
-
	Elf64_Half      e_phentsize; /* PHDR table entry size in bytes. */
-
	Elf64_Half      e_phnum;     /* Number of PHDR entries. */
-
	Elf64_Half      e_shentsize; /* SHDR table entry size in bytes. */
-
	Elf64_Half      e_shnum;     /* Number of SHDR entries. */
-
	Elf64_Half      e_shstrndx;  /* Index of section name string table. */
-
} Elf64_Ehdr;
-

-

-
/*
-
 * Shared object information.
-
 */
-

-
/* 32-bit entry. */
-
typedef struct {
-
	Elf32_Word l_name;	     /* The name of a shared object. */
-
	Elf32_Word l_time_stamp;     /* 32-bit timestamp. */
-
	Elf32_Word l_checksum;	     /* Checksum of visible symbols, sizes. */
-
	Elf32_Word l_version;	     /* Interface version string index. */
-
	Elf32_Word l_flags;	     /* Flags (LL_*). */
-
} Elf32_Lib;
-

-
/* 64-bit entry. */
-
typedef struct {
-
	Elf64_Word l_name;	     /* The name of a shared object. */
-
	Elf64_Word l_time_stamp;     /* 32-bit timestamp. */
-
	Elf64_Word l_checksum;	     /* Checksum of visible symbols, sizes. */
-
	Elf64_Word l_version;	     /* Interface version string index. */
-
	Elf64_Word l_flags;	     /* Flags (LL_*). */
-
} Elf64_Lib;
-

-
#define	_ELF_DEFINE_LL_FLAGS()			\
-
_ELF_DEFINE_LL(LL_NONE,			0,	\
-
	"no flags")				\
-
_ELF_DEFINE_LL(LL_EXACT_MATCH,		0x1,	\
-
	"require an exact match")		\
-
_ELF_DEFINE_LL(LL_IGNORE_INT_VER,	0x2,	\
-
	"ignore version incompatibilities")	\
-
_ELF_DEFINE_LL(LL_REQUIRE_MINOR,	0x4,	\
-
	"")					\
-
_ELF_DEFINE_LL(LL_EXPORTS,		0x8,	\
-
	"")					\
-
_ELF_DEFINE_LL(LL_DELAY_LOAD,		0x10,	\
-
	"")					\
-
_ELF_DEFINE_LL(LL_DELTA,		0x20,	\
-
	"")
-

-
#undef	_ELF_DEFINE_LL
-
#define	_ELF_DEFINE_LL(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_LL_FLAGS()
-
	LL__LAST__
-
};
-

-
/*
-
 * Note tags
-
 */
-

-
#define	_ELF_DEFINE_NOTE_ENTRY_TYPES()					\
-
_ELF_DEFINE_NT(NT_ABI_TAG,	1,	"Tag indicating the ABI")	\
-
_ELF_DEFINE_NT(NT_GNU_HWCAP,	2,	"Hardware capabilities")	\
-
_ELF_DEFINE_NT(NT_GNU_BUILD_ID,	3,	"Build id, set by ld(1)")	\
-
_ELF_DEFINE_NT(NT_GNU_GOLD_VERSION, 4,					\
-
	"Version number of the GNU gold linker")			\
-
_ELF_DEFINE_NT(NT_PRSTATUS,	1,	"Process status")		\
-
_ELF_DEFINE_NT(NT_FPREGSET,	2,	"Floating point information")	\
-
_ELF_DEFINE_NT(NT_PRPSINFO,	3,	"Process information")		\
-
_ELF_DEFINE_NT(NT_AUXV,		6,	"Auxiliary vector")		\
-
_ELF_DEFINE_NT(NT_PRXFPREG,	0x46E62B7FUL,				\
-
	"Linux user_xfpregs structure")					\
-
_ELF_DEFINE_NT(NT_PSTATUS,	10,	"Linux process status")		\
-
_ELF_DEFINE_NT(NT_FPREGS,	12,	"Linux floating point regset")	\
-
_ELF_DEFINE_NT(NT_PSINFO,	13,	"Linux process information")	\
-
_ELF_DEFINE_NT(NT_LWPSTATUS,	16,	"Linux lwpstatus_t type")	\
-
_ELF_DEFINE_NT(NT_LWPSINFO,	17,	"Linux lwpinfo_t type")
-

-
#undef	_ELF_DEFINE_NT
-
#define	_ELF_DEFINE_NT(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_NOTE_ENTRY_TYPES()
-
	NT__LAST__
-
};
-

-
/* Aliases for the ABI tag. */
-
#define	NT_FREEBSD_ABI_TAG	NT_ABI_TAG
-
#define	NT_GNU_ABI_TAG		NT_ABI_TAG
-
#define	NT_NETBSD_IDENT		NT_ABI_TAG
-
#define	NT_OPENBSD_IDENT	NT_ABI_TAG
-

-
/*
-
 * Note descriptors.
-
 */
-

-
typedef	struct {
-
	uint32_t	n_namesz;    /* Length of note's name. */
-
	uint32_t	n_descsz;    /* Length of note's value. */
-
	uint32_t	n_type;	     /* Type of note. */
-
} Elf_Note;
-

-
typedef Elf_Note Elf32_Nhdr;	     /* 32-bit note header. */
-
typedef Elf_Note Elf64_Nhdr;	     /* 64-bit note header. */
-

-
/*
-
 * MIPS ELF options descriptor header.
-
 */
-

-
typedef struct {
-
	Elf64_Byte	kind;        /* Type of options. */
-
	Elf64_Byte     	size;	     /* Size of option descriptor. */
-
	Elf64_Half	section;     /* Index of section affected. */
-
	Elf64_Word	info;        /* Kind-specific information. */
-
} Elf_Options;
-

-
/*
-
 * Option kinds.
-
 */
-

-
#define	_ELF_DEFINE_OPTION_KINDS()					\
-
_ELF_DEFINE_ODK(ODK_NULL,       0,      "undefined")			\
-
_ELF_DEFINE_ODK(ODK_REGINFO,    1,      "register usage info")		\
-
_ELF_DEFINE_ODK(ODK_EXCEPTIONS, 2,      "exception processing info")	\
-
_ELF_DEFINE_ODK(ODK_PAD,        3,      "section padding")		\
-
_ELF_DEFINE_ODK(ODK_HWPATCH,    4,      "hardware patch applied")	\
-
_ELF_DEFINE_ODK(ODK_FILL,       5,      "fill value used by linker")	\
-
_ELF_DEFINE_ODK(ODK_TAGS,       6,      "reserved space for tools")	\
-
_ELF_DEFINE_ODK(ODK_HWAND,      7,      "hardware AND patch applied")	\
-
_ELF_DEFINE_ODK(ODK_HWOR,       8,      "hardware OR patch applied")	\
-
_ELF_DEFINE_ODK(ODK_GP_GROUP,   9,					\
-
	"GP group to use for text/data sections")			\
-
_ELF_DEFINE_ODK(ODK_IDENT,      10,     "ID information")		\
-
_ELF_DEFINE_ODK(ODK_PAGESIZE,   11,     "page size infomation")
-

-
#undef	_ELF_DEFINE_ODK
-
#define	_ELF_DEFINE_ODK(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_OPTION_KINDS()
-
	ODK__LAST__
-
};
-

-
/*
-
 * ODK_EXCEPTIONS info field masks.
-
 */
-

-
#define	_ELF_DEFINE_ODK_EXCEPTIONS_MASK()				\
-
_ELF_DEFINE_OEX(OEX_FPU_MIN,    0x0000001FUL,				\
-
	"minimum FPU exception which must be enabled")			\
-
_ELF_DEFINE_OEX(OEX_FPU_MAX,    0x00001F00UL,				\
-
	"maximum FPU exception which can be enabled")			\
-
_ELF_DEFINE_OEX(OEX_PAGE0,      0x00010000UL,				\
-
	"page zero must be mapped")					\
-
_ELF_DEFINE_OEX(OEX_SMM,        0x00020000UL,				\
-
	"run in sequential memory mode")				\
-
_ELF_DEFINE_OEX(OEX_PRECISEFP,  0x00040000UL,				\
-
	"run in precise FP exception mode")				\
-
_ELF_DEFINE_OEX(OEX_DISMISS,    0x00080000UL,				\
-
	"dismiss invalid address traps")
-

-
#undef	_ELF_DEFINE_OEX
-
#define	_ELF_DEFINE_OEX(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_ODK_EXCEPTIONS_MASK()
-
	OEX__LAST__
-
};
-

-
/*
-
 * ODK_PAD info field masks.
-
 */
-

-
#define	_ELF_DEFINE_ODK_PAD_MASK()					\
-
_ELF_DEFINE_OPAD(OPAD_PREFIX,   0x0001)					\
-
_ELF_DEFINE_OPAD(OPAD_POSTFIX,  0x0002)					\
-
_ELF_DEFINE_OPAD(OPAD_SYMBOL,   0x0004)
-

-
#undef	_ELF_DEFINE_OPAD
-
#define	_ELF_DEFINE_OPAD(N, V)		N = V ,
-
enum {
-
	_ELF_DEFINE_ODK_PAD_MASK()
-
	OPAD__LAST__
-
};
-

-
/*
-
 * ODK_HWPATCH info field masks.
-
 */
-

-
#define	_ELF_DEFINE_ODK_HWPATCH_MASK()					\
-
_ELF_DEFINE_OHW(OHW_R4KEOP,     0x00000001UL,				\
-
	"patch for R4000 branch at end-of-page bug")			\
-
_ELF_DEFINE_OHW(OHW_R8KPFETCH,  0x00000002UL,				\
-
	"R8000 prefetch bug may occur")					\
-
_ELF_DEFINE_OHW(OHW_R5KEOP,     0x00000004UL,				\
-
	"patch for R5000 branch at end-of-page bug")			\
-
_ELF_DEFINE_OHW(OHW_R5KCVTL,    0x00000008UL,				\
-
	"R5000 cvt.[ds].l bug: clean == 1")				\
-
_ELF_DEFINE_OHW(OHW_R10KLDL,    0x00000010UL,				\
-
	"needd patch for R10000 misaligned load")
-

-
#undef	_ELF_DEFINE_OHW
-
#define	_ELF_DEFINE_OHW(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_ODK_HWPATCH_MASK()
-
	OHW__LAST__
-
};
-

-
/*
-
 * ODK_HWAND/ODK_HWOR info field and hwp_flags[12] masks.
-
 */
-

-
#define	_ELF_DEFINE_ODK_HWP_MASK()					\
-
_ELF_DEFINE_HWP(OHWA0_R4KEOP_CHECKED, 0x00000001UL,			\
-
	"object checked for R4000 end-of-page bug")			\
-
_ELF_DEFINE_HWP(OHWA0_R4KEOP_CLEAN, 0x00000002UL,			\
-
	"object verified clean for R4000 end-of-page bug")		\
-
_ELF_DEFINE_HWP(OHWO0_FIXADE,   0x00000001UL,				\
-
	"object requires call to fixade")
-

-
#undef	_ELF_DEFINE_HWP
-
#define	_ELF_DEFINE_HWP(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_ODK_HWP_MASK()
-
	OHWX0__LAST__
-
};
-

-
/*
-
 * ODK_IDENT/ODK_GP_GROUP info field masks.
-
 */
-

-
#define	_ELF_DEFINE_ODK_GP_MASK()					\
-
_ELF_DEFINE_OGP(OGP_GROUP,      0x0000FFFFUL, "GP group number")	\
-
_ELF_DEFINE_OGP(OGP_SELF,       0x00010000UL,				\
-
	"GP group is self-contained")
-

-
#undef	_ELF_DEFINE_OGP
-
#define	_ELF_DEFINE_OGP(N, V, DESCR)	N = V ,
-
enum {
-
	_ELF_DEFINE_ODK_GP_MASK()
-
	OGP__LAST__
-
};
-

-
/*
-
 * MIPS ELF register info descriptor.
-
 */
-

-
/* 32 bit RegInfo entry. */
-
typedef struct {
-
	Elf32_Word	ri_gprmask;  /* Mask of general register used. */
-
	Elf32_Word	ri_cprmask[4]; /* Mask of coprocessor register used. */
-
	Elf32_Addr	ri_gp_value; /* GP register value. */
-
} Elf32_RegInfo;
-

-
/* 64 bit RegInfo entry. */
-
typedef struct {
-
	Elf64_Word	ri_gprmask;  /* Mask of general register used. */
-
	Elf64_Word	ri_pad;	     /* Padding. */
-
	Elf64_Word	ri_cprmask[4]; /* Mask of coprocessor register used. */
-
	Elf64_Addr	ri_gp_value; /* GP register value. */
-
} Elf64_RegInfo;
-

-
/*
-
 * Program Header Table (PHDR) entries.
-
 */
-

-
/* 32 bit PHDR entry. */
-
typedef struct {
-
	Elf32_Word	p_type;	     /* Type of segment. */
-
	Elf32_Off	p_offset;    /* File offset to segment. */
-
	Elf32_Addr	p_vaddr;     /* Virtual address in memory. */
-
	Elf32_Addr	p_paddr;     /* Physical address (if relevant). */
-
	Elf32_Word	p_filesz;    /* Size of segment in file. */
-
	Elf32_Word	p_memsz;     /* Size of segment in memory. */
-
	Elf32_Word	p_flags;     /* Segment flags. */
-
	Elf32_Word	p_align;     /* Alignment constraints. */
-
} Elf32_Phdr;
-

-
/* 64 bit PHDR entry. */
-
typedef struct {
-
	Elf64_Word	p_type;	     /* Type of segment. */
-
	Elf64_Word	p_flags;     /* Segment flags. */
-
	Elf64_Off	p_offset;    /* File offset to segment. */
-
	Elf64_Addr	p_vaddr;     /* Virtual address in memory. */
-
	Elf64_Addr	p_paddr;     /* Physical address (if relevant). */
-
	Elf64_Xword	p_filesz;    /* Size of segment in file. */
-
	Elf64_Xword	p_memsz;     /* Size of segment in memory. */
-
	Elf64_Xword	p_align;     /* Alignment constraints. */
-
} Elf64_Phdr;
-

-

-
/*
-
 * Move entries, for describing data in COMMON blocks in a compact
-
 * manner.
-
 */
-

-
/* 32-bit move entry. */
-
typedef struct {
-
	Elf32_Lword	m_value;     /* Initialization value. */
-
	Elf32_Word 	m_info;	     /* Encoded size and index. */
-
	Elf32_Word	m_poffset;   /* Offset relative to symbol. */
-
	Elf32_Half	m_repeat;    /* Repeat count. */
-
	Elf32_Half	m_stride;    /* Number of units to skip. */
-
} Elf32_Move;
-

-
/* 64-bit move entry. */
-
typedef struct {
-
	Elf64_Lword	m_value;     /* Initialization value. */
-
	Elf64_Xword 	m_info;	     /* Encoded size and index. */
-
	Elf64_Xword	m_poffset;   /* Offset relative to symbol. */
-
	Elf64_Half	m_repeat;    /* Repeat count. */
-
	Elf64_Half	m_stride;    /* Number of units to skip. */
-
} Elf64_Move;
-

-
#define ELF32_M_SYM(I)		((I) >> 8)
-
#define ELF32_M_SIZE(I)		((unsigned char) (I))
-
#define ELF32_M_INFO(M, S)	(((M) << 8) + (unsigned char) (S))
-

-
#define ELF64_M_SYM(I)		((I) >> 8)
-
#define ELF64_M_SIZE(I)		((unsigned char) (I))
-
#define ELF64_M_INFO(M, S)	(((M) << 8) + (unsigned char) (S))
-

-
/*
-
 * Section Header Table (SHDR) entries.
-
 */
-

-
/* 32 bit SHDR */
-
typedef struct {
-
	Elf32_Word	sh_name;     /* index of section name */
-
	Elf32_Word	sh_type;     /* section type */
-
	Elf32_Word	sh_flags;    /* section flags */
-
	Elf32_Addr	sh_addr;     /* in-memory address of section */
-
	Elf32_Off	sh_offset;   /* file offset of section */
-
	Elf32_Word	sh_size;     /* section size in bytes */
-
	Elf32_Word	sh_link;     /* section header table link */
-
	Elf32_Word	sh_info;     /* extra information */
-
	Elf32_Word	sh_addralign; /* alignment constraint */
-
	Elf32_Word	sh_entsize;   /* size for fixed-size entries */
-
} Elf32_Shdr;
-

-
/* 64 bit SHDR */
-
typedef struct {
-
	Elf64_Word	sh_name;     /* index of section name */
-
	Elf64_Word	sh_type;     /* section type */
-
	Elf64_Xword	sh_flags;    /* section flags */
-
	Elf64_Addr	sh_addr;     /* in-memory address of section */
-
	Elf64_Off	sh_offset;   /* file offset of section */
-
	Elf64_Xword	sh_size;     /* section size in bytes */
-
	Elf64_Word	sh_link;     /* section header table link */
-
	Elf64_Word	sh_info;     /* extra information */
-
	Elf64_Xword	sh_addralign; /* alignment constraint */
-
	Elf64_Xword	sh_entsize;  /* size for fixed-size entries */
-
} Elf64_Shdr;
-

-

-
/*
-
 * Symbol table entries.
-
 */
-

-
typedef struct {
-
	Elf32_Word	st_name;     /* index of symbol's name */
-
	Elf32_Addr	st_value;    /* value for the symbol */
-
	Elf32_Word	st_size;     /* size of associated data */
-
	unsigned char	st_info;     /* type and binding attributes */
-
	unsigned char	st_other;    /* visibility */
-
	Elf32_Half	st_shndx;    /* index of related section */
-
} Elf32_Sym;
-

-
typedef struct {
-
	Elf64_Word	st_name;     /* index of symbol's name */
-
	unsigned char	st_info;     /* type and binding attributes */
-
	unsigned char	st_other;    /* visibility */
-
	Elf64_Half	st_shndx;    /* index of related section */
-
	Elf64_Addr	st_value;    /* value for the symbol */
-
	Elf64_Xword	st_size;     /* size of associated data */
-
} Elf64_Sym;
-

-
#define ELF32_ST_BIND(I)	((I) >> 4)
-
#define ELF32_ST_TYPE(I)	((I) & 0xFU)
-
#define ELF32_ST_INFO(B,T)	(((B) << 4) + ((T) & 0xF))
-

-
#define ELF64_ST_BIND(I)	((I) >> 4)
-
#define ELF64_ST_TYPE(I)	((I) & 0xFU)
-
#define ELF64_ST_INFO(B,T)	(((B) << 4) + ((T) & 0xF))
-

-
#define ELF32_ST_VISIBILITY(O)	((O) & 0x3)
-
#define ELF64_ST_VISIBILITY(O)	((O) & 0x3)
-

-
/*
-
 * Syminfo descriptors, containing additional symbol information.
-
 */
-

-
/* 32-bit entry. */
-
typedef struct {
-
	Elf32_Half	si_boundto;  /* Entry index with additional flags. */
-
	Elf32_Half	si_flags;    /* Flags. */
-
} Elf32_Syminfo;
-

-
/* 64-bit entry. */
-
typedef struct {
-
	Elf64_Half	si_boundto;  /* Entry index with additional flags. */
-
	Elf64_Half	si_flags;    /* Flags. */
-
} Elf64_Syminfo;
-

-
/*
-
 * Relocation descriptors.
-
 */
-

-
typedef struct {
-
	Elf32_Addr	r_offset;    /* location to apply relocation to */
-
	Elf32_Word	r_info;	     /* type+section for relocation */
-
} Elf32_Rel;
-

-
typedef struct {
-
	Elf32_Addr	r_offset;    /* location to apply relocation to */
-
	Elf32_Word	r_info;      /* type+section for relocation */
-
	Elf32_Sword	r_addend;    /* constant addend */
-
} Elf32_Rela;
-

-
typedef struct {
-
	Elf64_Addr	r_offset;    /* location to apply relocation to */
-
	Elf64_Xword	r_info;      /* type+section for relocation */
-
} Elf64_Rel;
-

-
typedef struct {
-
	Elf64_Addr	r_offset;    /* location to apply relocation to */
-
	Elf64_Xword	r_info;      /* type+section for relocation */
-
	Elf64_Sxword	r_addend;    /* constant addend */
-
} Elf64_Rela;
-

-

-
#define ELF32_R_SYM(I)		((I) >> 8)
-
#define ELF32_R_TYPE(I)		((unsigned char) (I))
-
#define ELF32_R_INFO(S,T)	(((S) << 8) + (unsigned char) (T))
-

-
#define ELF64_R_SYM(I)		((I) >> 32)
-
#define ELF64_R_TYPE(I)		((I) & 0xFFFFFFFFUL)
-
#define ELF64_R_INFO(S,T)	(((S) << 32) + ((T) & 0xFFFFFFFFUL))
-

-
/*
-
 * Symbol versioning structures.
-
 */
-

-
/* 32-bit structures. */
-
typedef struct
-
{
-
	Elf32_Word	vda_name;    /* Index to name. */
-
	Elf32_Word	vda_next;    /* Offset to next entry. */
-
} Elf32_Verdaux;
-

-
typedef struct
-
{
-
	Elf32_Word	vna_hash;    /* Hash value of dependency name. */
-
	Elf32_Half	vna_flags;   /* Flags. */
-
	Elf32_Half	vna_other;   /* Unused. */
-
	Elf32_Word	vna_name;    /* Offset to dependency name. */
-
	Elf32_Word	vna_next;    /* Offset to next vernaux entry. */
-
} Elf32_Vernaux;
-

-
typedef struct
-
{
-
	Elf32_Half	vd_version;  /* Version information. */
-
	Elf32_Half	vd_flags;    /* Flags. */
-
	Elf32_Half	vd_ndx;	     /* Index into the versym section. */
-
	Elf32_Half	vd_cnt;	     /* Number of aux entries. */
-
	Elf32_Word	vd_hash;     /* Hash value of name. */
-
	Elf32_Word	vd_aux;	     /* Offset to aux entries. */
-
	Elf32_Word	vd_next;     /* Offset to next version definition. */
-
} Elf32_Verdef;
-

-
typedef struct
-
{
-
	Elf32_Half	vn_version;  /* Version number. */
-
	Elf32_Half	vn_cnt;	     /* Number of aux entries. */
-
	Elf32_Word	vn_file;     /* Offset of associated file name. */
-
	Elf32_Word	vn_aux;	     /* Offset of vernaux array. */
-
	Elf32_Word	vn_next;     /* Offset of next verneed entry. */
-
} Elf32_Verneed;
-

-
typedef Elf32_Half	Elf32_Versym;
-

-
/* 64-bit structures. */
-

-
typedef struct {
-
	Elf64_Word	vda_name;    /* Index to name. */
-
	Elf64_Word	vda_next;    /* Offset to next entry. */
-
} Elf64_Verdaux;
-

-
typedef struct {
-
	Elf64_Word	vna_hash;    /* Hash value of dependency name. */
-
	Elf64_Half	vna_flags;   /* Flags. */
-
	Elf64_Half	vna_other;   /* Unused. */
-
	Elf64_Word	vna_name;    /* Offset to dependency name. */
-
	Elf64_Word	vna_next;    /* Offset to next vernaux entry. */
-
} Elf64_Vernaux;
-

-
typedef struct {
-
	Elf64_Half	vd_version;  /* Version information. */
-
	Elf64_Half	vd_flags;    /* Flags. */
-
	Elf64_Half	vd_ndx;	     /* Index into the versym section. */
-
	Elf64_Half	vd_cnt;	     /* Number of aux entries. */
-
	Elf64_Word	vd_hash;     /* Hash value of name. */
-
	Elf64_Word	vd_aux;	     /* Offset to aux entries. */
-
	Elf64_Word	vd_next;     /* Offset to next version definition. */
-
} Elf64_Verdef;
-

-
typedef struct {
-
	Elf64_Half	vn_version;  /* Version number. */
-
	Elf64_Half	vn_cnt;	     /* Number of aux entries. */
-
	Elf64_Word	vn_file;     /* Offset of associated file name. */
-
	Elf64_Word	vn_aux;	     /* Offset of vernaux array. */
-
	Elf64_Word	vn_next;     /* Offset of next verneed entry. */
-
} Elf64_Verneed;
-

-
typedef Elf64_Half	Elf64_Versym;
-

-

-
/*
-
 * The header for GNU-style hash sections.
-
 */
-

-
typedef struct {
-
	uint32_t	gh_nbuckets;	/* Number of hash buckets. */
-
	uint32_t	gh_symndx;	/* First visible symbol in .dynsym. */
-
	uint32_t	gh_maskwords;	/* #maskwords used in bloom filter. */
-
	uint32_t	gh_shift2;	/* Bloom filter shift count. */
-
} Elf_GNU_Hash_Header;
-

-
#endif	/* _ELFDEFINITIONS_H_ */
deleted external/libelf/gelf.3
@@ -1,201 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd September 1, 2006
-
.Os
-
.Dt GELF 3
-
.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
-
.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
-
.It "Allocating ELF Data Structures"
-
.Bl -tag -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 -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 -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 -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 -compact -width ".Fn gelf_update_shdr"
-
.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 .
deleted external/libelf/gelf.h
@@ -1,108 +0,0 @@
-
/*-
-
 * 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.
-
 *
-
 * $Id$
-
 */
-

-
#ifndef	_GELF_H_
-
#define	_GELF_H_
-

-
#include <sys/cdefs.h>
-

-
#include <libelf.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 */
-

-
typedef	Elf64_Cap	GElf_Cap;	/* SW/HW capabilities */
-
typedef Elf64_Move	GElf_Move;	/* Move entries */
-
typedef Elf64_Syminfo	GElf_Syminfo;	/* Symbol information */
-

-
#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);
-

-
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);
-
__END_DECLS
-

-
#endif	/* _GELF_H_ */
deleted external/libelf/gelf_cap.c
@@ -1,151 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <limits.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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 * (size_t) ndx >= d->d_data.d_size) {
-
		LIBELF_SET_ERROR(ARGUMENT, 0);
-
		return (NULL);
-
	}
-

-
	if (ec == ELFCLASS32) {
-

-
		cap32 = (Elf32_Cap *) d->d_data.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_data.d_buf + ndx;
-

-
		*dst = *cap64;
-
	}
-

-
	return (dst);
-
}
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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 * (size_t) ndx >= d->d_data.d_size) {
-
		LIBELF_SET_ERROR(ARGUMENT, 0);
-
		return (0);
-
	}
-

-
	if (ec == ELFCLASS32) {
-
		cap32 = (Elf32_Cap *) d->d_data.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_data.d_buf + ndx;
-

-
		*cap64 = *gc;
-
	}
-

-
	return (1);
-
}
deleted external/libelf/gelf_checksum.3
@@ -1,115 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 29, 2006
-
.Os
-
.Dt GELF_CHECKSUM 3
-
.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
deleted external/libelf/gelf_checksum.c
@@ -1,58 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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));
-
}
deleted external/libelf/gelf_dyn.c
@@ -1,153 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <limits.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-
		dyn32 = (Elf32_Dyn *) d->d_data.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_data.d_buf + ndx;
-

-
		*dst = *dyn64;
-
	}
-

-
	return (dst);
-
}
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-
		dyn32 = (Elf32_Dyn *) d->d_data.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_data.d_buf + ndx;
-

-
		*dyn64 = *ds;
-
	}
-

-
	return (1);
-
}
deleted external/libelf/gelf_ehdr.c
@@ -1,168 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <libelf.h>
-
#include <limits.h>
-
#include <string.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/gelf_fsize.3
@@ -1,96 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd February 5, 2008
-
.Os
-
.Dt GELF_FSIZE 3
-
.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
deleted external/libelf/gelf_fsize.c
@@ -1,62 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/gelf_getcap.3
@@ -1,121 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 29, 2006
-
.Os
-
.Dt GELF_GETCAP 3
-
.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
deleted external/libelf/gelf_getclass.3
@@ -1,61 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd July 3, 2006
-
.Os
-
.Dt GELF_GETCLASS 3
-
.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
deleted external/libelf/gelf_getclass.c
@@ -1,39 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <gelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
int
-
gelf_getclass(Elf *e)
-
{
-
	return (e != NULL ? e->e_class : ELFCLASSNONE);
-
}
deleted external/libelf/gelf_getdyn.3
@@ -1,123 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 29, 2006
-
.Os
-
.Dt GELF_GETDYN 3
-
.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
deleted external/libelf/gelf_getehdr.3
@@ -1,123 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd December 16, 2006
-
.Os
-
.Dt GELF_GETEHDR 3
-
.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
deleted external/libelf/gelf_getmove.3
@@ -1,120 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 29, 2006
-
.Os
-
.Dt GELF_GETMOVE 3
-
.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
deleted external/libelf/gelf_getphdr.3
@@ -1,141 +0,0 @@
-
.\" Copyright (c) 2006-2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd October 21, 2007
-
.Os
-
.Dt GELF_GETPHDR 3
-
.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
deleted external/libelf/gelf_getrel.3
@@ -1,121 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 29, 2006
-
.Os
-
.Dt GELF_GETREL 3
-
.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
deleted external/libelf/gelf_getrela.3
@@ -1,121 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 29, 2006
-
.Os
-
.Dt GELF_GETRELA 3
-
.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
deleted external/libelf/gelf_getshdr.3
@@ -1,115 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 27, 2006
-
.Os
-
.Dt GELF_GETSHDR 3
-
.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
deleted external/libelf/gelf_getsym.3
@@ -1,125 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 29, 2006
-
.Os
-
.Dt GELF_GETSYM 3
-
.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
deleted external/libelf/gelf_getsyminfo.3
@@ -1,115 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 29, 2006
-
.Os
-
.Dt GELF_GETSYMINFO 3
-
.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
deleted external/libelf/gelf_getsymshndx.3
@@ -1,162 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd November 5, 2006
-
.Os
-
.Dt GELF_GETSYMSHNDX 3
-
.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
deleted external/libelf/gelf_move.c
@@ -1,160 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <limits.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-

-
		move32 = (Elf32_Move *) d->d_data.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_data.d_buf + ndx;
-

-
		*dst = *move64;
-
	}
-

-
	return (dst);
-
}
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-
		move32 = (Elf32_Move *) d->d_data.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_data.d_buf + ndx;
-

-
		*move64 = *gm;
-
	}
-

-
	return (1);
-
}
deleted external/libelf/gelf_newehdr.3
@@ -1,185 +0,0 @@
-
.\" Copyright (c) 2006-2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd October 22, 2007
-
.Os
-
.Dt GELF_NEWEHDR 3
-
.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
deleted external/libelf/gelf_newphdr.3
@@ -1,133 +0,0 @@
-
.\" Copyright (c) 2006-2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd October 22, 2007
-
.Os
-
.Dt GELF_NEWPHDR 3
-
.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
deleted external/libelf/gelf_phdr.c
@@ -1,178 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <gelf.h>
-
#include <libelf.h>
-
#include <limits.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/gelf_rel.c
@@ -1,163 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <limits.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-
		rel32 = (Elf32_Rel *) d->d_data.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_data.d_buf + ndx;
-

-
		*dst = *rel64;
-
	}
-

-
	return (dst);
-
}
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-
		rel32 = (Elf32_Rel *) d->d_data.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(
-
			(Elf32_Word) ELF64_R_SYM(dr->r_info),
-
			(Elf32_Word) ELF64_R_TYPE(dr->r_info));
-
	} else {
-
		rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx;
-

-
		*rel64 = *dr;
-
	}
-

-
	return (1);
-
}
deleted external/libelf/gelf_rela.c
@@ -1,166 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <limits.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-
		rela32 = (Elf32_Rela *) d->d_data.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_data.d_buf + ndx;
-

-
		*dst = *rela64;
-
	}
-

-
	return (dst);
-
}
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-
		rela32 = (Elf32_Rela *) d->d_data.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(
-
			(Elf32_Word) ELF64_R_SYM(dr->r_info),
-
			(Elf32_Word) ELF64_R_TYPE(dr->r_info));
-

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

-
		*rela64 = *dr;
-
	}
-

-
	return (1);
-
}
deleted external/libelf/gelf_shdr.c
@@ -1,131 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <libelf.h>
-
#include <limits.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/gelf_sym.c
@@ -1,160 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <limits.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-
		sym32 = (Elf32_Sym *) d->d_data.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  = sym32->st_info;
-
		dst->st_other = sym32->st_other;
-
		dst->st_shndx = sym32->st_shndx;
-
	} else {
-
		sym64 = (Elf64_Sym *) d->d_data.d_buf + ndx;
-

-
		*dst = *sym64;
-
	}
-

-
	return (dst);
-
}
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-
		sym32 = (Elf32_Sym *) d->d_data.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_data.d_buf + ndx;
-

-
		*sym64 = *gs;
-
	}
-

-
	return (1);
-
}
deleted external/libelf/gelf_syminfo.c
@@ -1,154 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

-
	if (ec == ELFCLASS32) {
-

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

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

-
	} else {
-

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

-
		*dst = *syminfo64;
-
	}
-

-
	return (dst);
-
}
-

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

-
	d = (struct _Libelf_Data *) ed;
-

-
	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);
-
	assert(ndx >= 0);
-

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

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

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

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

-
		*syminfo64 = *gs;
-
	}
-

-
	return (1);
-
}
deleted external/libelf/gelf_symshndx.c
@@ -1,139 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

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

-
	ld = (struct _Libelf_Data *) d;
-
	lid = (struct _Libelf_Data *) id;
-

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

-
	if (lid == NULL || (scn = lid->d_scn) == NULL ||
-
	    (e = scn->s_elf) == NULL || (e != ld->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);
-
	assert(ndx >= 0);
-

-
	if (msz * (size_t) 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;
-
	size_t msz;
-
	Elf_Scn *scn;
-
	uint32_t sh_type;
-
	struct _Libelf_Data *ld, *lid;
-

-
	ld = (struct _Libelf_Data *) d;
-
	lid = (struct _Libelf_Data *) id;
-

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

-
	if (lid == NULL || (scn = lid->d_scn) == NULL ||
-
	    (e = scn->s_elf) == NULL || (e != ld->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);
-
	assert(ndx >= 0);
-

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

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

-
	return (1);
-
}
deleted external/libelf/gelf_update_ehdr.3
@@ -1,123 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd August 27, 2006
-
.Os
-
.Dt GELF_UPDATE_EHDR 3
-
.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
deleted external/libelf/gelf_xlate.c
@@ -1,81 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/gelf_xlatetof.3
@@ -1,247 +0,0 @@
-
.\" Copyright (c) 2006,2008 Joseph Koshy.  All rights reserved.
-
.\"
-
.\" Redistribution and use in source and binary forms, with or without
-
.\" modification, are permitted provided that the following conditions
-
.\" are met:
-
.\" 1. Redistributions of source code must retain the above copyright
-
.\"    notice, this list of conditions and the following disclaimer.
-
.\" 2. Redistributions in binary form must reproduce the above copyright
-
.\"    notice, this list of conditions and the following disclaimer in the
-
.\"    documentation and/or other materials provided with the distribution.
-
.\"
-
.\" This software is provided by Joseph Koshy ``as is'' and
-
.\" any express or implied warranties, including, but not limited to, the
-
.\" implied warranties of merchantability and fitness for a particular purpose
-
.\" are disclaimed.  in no event shall Joseph Koshy be liable
-
.\" for any direct, indirect, incidental, special, exemplary, or consequential
-
.\" damages (including, but not limited to, procurement of substitute goods
-
.\" or services; loss of use, data, or profits; or business interruption)
-
.\" however caused and on any theory of liability, whether in contract, strict
-
.\" liability, or tort (including negligence or otherwise) arising in any way
-
.\" out of the use of this software, even if advised of the possibility of
-
.\" such damage.
-
.\"
-
.\" $Id$
-
.\"
-
.Dd July 24, 2006
-
.Os
-
.Dt GELF_XLATETOF 3
-
.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
deleted external/libelf/libelf.h
@@ -1,253 +0,0 @@
-
/*-
-
 * 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 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.
-
 *
-
 * $Id$
-
 */
-

-
#ifndef	_LIBELF_H_
-
#define	_LIBELF_H_
-

-
#include <sys/types.h>
-

-
#include <elfdefinitions.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;
-
} 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;
-

-
	/*
-
	 * Members that are not part of the public API.
-
	 */
-
	unsigned int	ar_flags;
-
} 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 */
-

-
/* ELF(3) API extensions. */
-
#define	ELF_F_ARCHIVE	   0x100U /* archive creation */
-
#define	ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */
-

-
#include <sys/cdefs.h>
-
__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_flagarhdr(Elf_Arhdr *_arh, Elf_Cmd _cmd,
-
			unsigned int _flags);
-
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);
-
Elf		*elf_open(int _fd);
-
Elf		*elf_openmemory(char *_image, size_t _size);
-
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);
-
__END_DECLS
-

-
#endif	/* _LIBELF_H_ */
deleted external/libelf/libelf_align.c
@@ -1,137 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <sys/types.h>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
struct align {
-
	unsigned int a32;
-
	unsigned 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 },
-
	[ELF_T_CAP]	= MALIGN(Cap),
-
	[ELF_T_DYN]	= MALIGN(Dyn),
-
	[ELF_T_EHDR]	= MALIGN(Ehdr),
-
	[ELF_T_HALF]	= MALIGN(Half),
-
	[ELF_T_LWORD]	= MALIGN(Lword),
-
	[ELF_T_MOVE]	= MALIGN(Move),
-
	[ELF_T_MOVEP] 	= UNSUPPORTED(),
-
	[ELF_T_NOTE]	= MALIGN(Nhdr),
-
	[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),
-
	[ELF_T_SYMINFO]	= MALIGN(Syminfo),
-
	[ELF_T_VDEF]	= MALIGN(Verdef),
-
	[ELF_T_VNEED]	= MALIGN(Verneed),
-
	[ELF_T_WORD]	= MALIGN(Word),
-
	[ELF_T_XWORD]	= MALIGN64(Xword),
-
	[ELF_T_GNUHASH] = MALIGN_WORD()
-
};
-

-
unsigned 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),
-
	[ELF_T_CAP]	= FALIGN(4,8),
-
	[ELF_T_DYN]	= FALIGN(4,8),
-
	[ELF_T_EHDR]	= FALIGN(4,8),
-
	[ELF_T_HALF]	= FALIGN(2,2),
-
	[ELF_T_LWORD]	= FALIGN(8,8),
-
	[ELF_T_MOVE]	= FALIGN(8,8),
-
	[ELF_T_MOVEP] 	= UNSUPPORTED(),
-
	[ELF_T_NOTE]	= FALIGN(4,4),
-
	[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),
-
	[ELF_T_SYMINFO]	= FALIGN(2,2),
-
	[ELF_T_VDEF]	= FALIGN(4,4),
-
	[ELF_T_VNEED]	= FALIGN(4,4),
-
	[ELF_T_WORD]	= FALIGN(4,4),
-
	[ELF_T_XWORD]	= FALIGN(0,8),
-
	[ELF_T_GNUHASH] = FALIGN(4,8)
-
};
-

-
unsigned 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);
-
}
deleted external/libelf/libelf_allocate.c
@@ -1,212 +0,0 @@
-
/*-
-
 * 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 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>
-

-
#include <assert.h>
-
#include <errno.h>
-
#include <libelf.h>
-
#include <stdlib.h>
-
#include <string.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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_hdr.e_rawhdr = 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)
-
{
-
	Elf_Arhdr *arh;
-

-
	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_flags & LIBELF_F_AR_HEADER) {
-
			arh = e->e_hdr.e_arhdr;
-
			FREE(arh->ar_name);
-
			FREE(arh->ar_rawname);
-
			free(arh);
-
		}
-

-
		break;
-

-
	default:
-
		break;
-
	}
-

-
	free(e);
-

-
	return (NULL);
-
}
-

-
struct _Libelf_Data *
-
_libelf_allocate_data(Elf_Scn *s)
-
{
-
	struct _Libelf_Data *d;
-

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

-
	d->d_scn = s;
-

-
	return (d);
-
}
-

-
struct _Libelf_Data *
-
_libelf_release_data(struct _Libelf_Data *d)
-
{
-

-
	if (d->d_flags & LIBELF_F_DATA_MALLOCED)
-
		free(d->d_data.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;
-
	struct _Libelf_Data *d, *td;
-

-
	assert(s != NULL);
-

-
	STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) {
-
		STAILQ_REMOVE(&s->s_data, d, _Libelf_Data, d_next);
-
		d = _libelf_release_data(d);
-
	}
-

-
	STAILQ_FOREACH_SAFE(d, &s->s_rawdata, d_next, td) {
-
		assert((d->d_flags & LIBELF_F_DATA_MALLOCED) == 0);
-
		STAILQ_REMOVE(&s->s_rawdata, d, _Libelf_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);
-
}
deleted external/libelf/libelf_ar.c
@@ -1,468 +0,0 @@
-
/*-
-
 * 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 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>
-

-
#include <assert.h>
-
#include <ctype.h>
-
#include <libelf.h>
-
#include <stdlib.h>
-
#include <string.h>
-

-
#include "_libelf.h"
-
#include "_libelf_ar.h"
-

-
ELFTC_VCSID("$Id$");
-

-
#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:
-
 *
-
 * There are two variants of the `ar' archive format: traditional BSD
-
 * and SVR4.  These differ in the way long file names are treated, and
-
 * in the layout of the archive symbol table.
-
 *
-
 * The `ar' header only has space for a 16 character file name.
-
 *
-
 * In the SVR4 format, file names are terminated with a '/', so this
-
 * effectively leaves 15 characters for the actual file name.  Longer
-
 * file names stored in a separate 'string table' and referenced
-
 * indirectly from the name field.  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.
-
 *
-
 * In the BSD format, file names can be upto 16 characters.  File
-
 * names shorter than 16 characters are padded to 16 characters using
-
 * (ASCII) space characters.  File names with embedded spaces and file
-
 * names longer than 16 characters are stored immediately after the
-
 * archive header and the name field set to a special indirect name
-
 * matching the pattern "#1/[0-9]+".  The digits form a decimal number
-
 * that corresponds to the actual length of the file name following
-
 * the archive header.  The content of the archive member immediately
-
 * follows the file name, and the size field of the archive member
-
 * holds the sum of the sizes of the member and of the appended file
-
 * name.
-
 *
-
 * Archives may also have a symbol table (see ranlib(1)), mapping
-
 * program symbols to object files inside the archive.
-
 *
-
 * In the SVR4 format, 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.
-
 *
-
 * In the BSD format, the symbol table uses a file name of "__.SYMDEF".
-
 * It is structured as two parts:
-
 *  - The first part is an array of "ranlib" structures preceded by
-
 *    the size of the array in bytes.  Each "ranlib" structure
-
 *    describes one symbol.  Each structure contains an offset into
-
 *    the string table for the symbol name, and a file offset into the
-
 *    archive for the member defining the symbol.
-
 *  - The second part is a string table containing NUL-terminated
-
 *    strings, preceded by the size of the string table in bytes.
-
 *
-
 * If the symbol table and string table are is present in an archive
-
 * they must be the very first objects and in that order.
-
 */
-

-

-
/*
-
 * Retrieve an archive header descriptor.
-
 */
-

-
Elf_Arhdr *
-
_libelf_ar_gethdr(Elf *e)
-
{
-
	Elf *parent;
-
	Elf_Arhdr *eh;
-
	char *namelen;
-
	size_t n, nlen;
-
	struct ar_hdr *arh;
-

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

-
	assert((e->e_flags & LIBELF_F_AR_HEADER) == 0);
-

-
	arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr;
-

-
	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_hdr.e_arhdr = eh;
-
	e->e_flags |= LIBELF_F_AR_HEADER;
-

-
	eh->ar_name = eh->ar_rawname = NULL;
-

-
	if ((eh->ar_name = _libelf_ar_get_translated_name(arh, 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;
-

-
	/*
-
	 * Get the true size of the member if extended naming is being used.
-
	 */
-
	if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
-
		namelen = arh->ar_name +
-
		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
-
		if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
-
		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nlen) == 0)
-
			goto error;
-
		n -= nlen;
-
	}
-

-
	eh->ar_size = n;
-

-
	if ((eh->ar_rawname = _libelf_ar_get_raw_name(arh)) == NULL)
-
		goto error;
-

-
	eh->ar_flags = 0;
-

-
	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_flags &= ~LIBELF_F_AR_HEADER;
-
	e->e_hdr.e_rawhdr = (unsigned char *) arh;
-

-
	return (NULL);
-
}
-

-
Elf *
-
_libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf)
-
{
-
	Elf *e;
-
	off_t next;
-
	size_t nsz, sz;
-
	struct ar_hdr *arh;
-
	char *member, *namelen;
-

-
	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);
-

-
	/*
-
	 * Retrieve the size of the member.
-
	 */
-
	if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10,
-
	    &sz) == 0) {
-
		LIBELF_SET_ERROR(ARCHIVE, 0);
-
		return (NULL);
-
	}
-

-
	/*
-
	 * Adjust the size field for members in BSD archives using
-
	 * extended naming.
-
	 */
-
	if (IS_EXTENDED_BSD_NAME(arh->ar_name)) {
-
		namelen = arh->ar_name +
-
		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
-
		if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) -
-
		    LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nsz) == 0) {
-
			LIBELF_SET_ERROR(ARCHIVE, 0);
-
			return (NULL);
-
		}
-

-
		member = (char *) (arh + 1) + nsz;
-
		sz -= nsz;
-
	} else
-
		member = (char *) (arh + 1);
-

-

-
	if ((e = elf_memory(member, sz)) == NULL)
-
		return (NULL);
-

-
	e->e_fd = fd;
-
	e->e_cmd = c;
-
	e->e_hdr.e_rawhdr = (unsigned char *) arh;
-

-
	elf->e_u.e_ar.e_nchildren++;
-
	e->e_parent = elf;
-

-
	return (e);
-
}
-

-
/*
-
 * A BSD-style ar(1) symbol table has the following layout:
-
 *
-
 * - A count of bytes used by the following array of 'ranlib'
-
 *   structures, stored as a 'long'.
-
 * - An array of 'ranlib' structures.  Each array element is
-
 *   two 'long's in size.
-
 * - A count of bytes used for the following symbol table.
-
 * - The symbol table itself.
-
 */
-

-
/*
-
 * A helper macro to read in a 'long' value from the archive.
-
 *
-
 * We use memcpy() since the source pointer may be misaligned with
-
 * respect to the natural alignment for a C 'long'.
-
 */
-
#define	GET_LONG(P, V)do {				\
-
		memcpy(&(V), (P), sizeof(long));	\
-
		(P) += sizeof(long);			\
-
	} while (0)
-

-
Elf_Arsym *
-
_libelf_ar_process_bsd_symtab(Elf *e, size_t *count)
-
{
-
	Elf_Arsym *symtab, *sym;
-
	unsigned int n, nentries;
-
	unsigned char *end, *p, *p0, *s, *s0;
-
	const size_t entrysize = 2 * sizeof(long);
-
	long arraysize, fileoffset, stroffset, strtabsize;
-

-
	assert(e != NULL);
-
	assert(count != NULL);
-
	assert(e->e_u.e_ar.e_symtab == NULL);
-

-
	symtab = NULL;
-

-
	/*
-
	 * The BSD symbol table always contains the count fields even
-
	 * if there are no entries in it.
-
	 */
-
	if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long))
-
		goto symtaberror;
-

-
	p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab;
-
	end = p0 + e->e_u.e_ar.e_rawsymtabsz;
-

-
	/*
-
	 * Retrieve the size of the array of ranlib descriptors and
-
	 * check it for validity.
-
	 */
-
	GET_LONG(p, arraysize);
-

-
	if (arraysize < 0 || p0 + arraysize >= end ||
-
	    ((size_t) arraysize % entrysize != 0))
-
		goto symtaberror;
-

-
	/*
-
	 * Check the value of the string table size.
-
	 */
-
	s = p + arraysize;
-
	GET_LONG(s, strtabsize);
-

-
	s0 = s;			/* Start of string table. */
-
	if (strtabsize < 0 || s0 + strtabsize > end)
-
		goto symtaberror;
-

-
	nentries = (size_t) arraysize / entrysize;
-

-
	/*
-
	 * Allocate space for the returned Elf_Arsym array.
-
	 */
-
	if ((symtab = malloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) {
-
		LIBELF_SET_ERROR(RESOURCE, 0);
-
		return (NULL);
-
	}
-

-
	/* Read in symbol table entries. */
-
	for (n = 0, sym = symtab; n < nentries; n++, sym++) {
-
		GET_LONG(p, stroffset);
-
		GET_LONG(p, fileoffset);
-

-
		if (stroffset < 0 || fileoffset <  0 ||
-
		    (size_t) fileoffset >= e->e_rawsize)
-
			goto symtaberror;
-

-
		s = s0 + stroffset;
-

-
		if (s >= end)
-
			goto symtaberror;
-

-
		sym->as_off = (off_t) fileoffset;
-
		sym->as_hash = elf_hash((char *) s);
-
		sym->as_name = (char *) s;
-
	}
-

-
	/* Fill up the sentinel entry. */
-
	sym->as_name = NULL;
-
	sym->as_hash = ~0UL;
-
	sym->as_off = (off_t) 0;
-

-
	/* Remember the processed symbol table. */
-
	e->e_u.e_ar.e_symtab = symtab;
-

-
	*count = e->e_u.e_ar.e_symtabsz = nentries + 1;
-

-
	return (symtab);
-

-
symtaberror:
-
	if (symtab)
-
		free(symtab);
-
	LIBELF_SET_ERROR(ARCHIVE, 0);
-
	return (NULL);
-
}
-

-
/*
-
 * An SVR4-style 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_svr4_symtab(Elf *e, size_t *count)
-
{
-
	uint32_t off;
-
	size_t n, nentries;
-
	Elf_Arsym *symtab, *sym;
-
	unsigned char *p, *s, *end;
-

-
	assert(e != NULL);
-
	assert(count != NULL);
-
	assert(e->e_u.e_ar.e_symtab == NULL);
-

-
	symtab = NULL;
-

-
	if (e->e_u.e_ar.e_rawsymtabsz < INTSZ)
-
		goto symtaberror;
-

-
	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)
-
		goto symtaberror;
-

-
	/* 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--) {
-
		if (s >= end)
-
			goto symtaberror;
-

-
		GET_WORD(p, off);
-
		if (off >= e->e_rawsize)
-
			goto symtaberror;
-

-
		sym->as_off = (off_t) off;
-
		sym->as_hash = elf_hash((char *) s);
-
		sym->as_name = (char *) s;
-

-
		p += INTSZ;
-
		sym++;
-

-
		for (; s < end && *s++ != '\0';) /* skip to next string */
-
			;
-
	}
-

-
	/* 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);
-

-
symtaberror:
-
	if (symtab)
-
		free(symtab);
-
	LIBELF_SET_ERROR(ARCHIVE, 0);
-
	return (NULL);
-
}
deleted external/libelf/libelf_ar_util.c
@@ -1,362 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2009,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 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>
-

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

-
#include "_libelf.h"
-
#include "_libelf_ar.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * Convert a string bounded by `start' and `start+sz' (exclusive) to a
-
 * number in the specified base.
-
 */
-
int
-
_libelf_ar_get_number(const char *src, size_t sz, unsigned int base,
-
    size_t *ret)
-
{
-
	size_t r;
-
	unsigned int c, v;
-
	const unsigned char *e, *s;
-

-
	assert(base <= 10);
-

-
	s = (const unsigned char *) src;
-
	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);
-
}
-

-
/*
-
 * Return the translated name for an archive member.
-
 */
-
char *
-
_libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
-
{
-
	char *s;
-
	unsigned char c;
-
	size_t len, offset;
-
	const unsigned char *buf, *p, *q, *r;
-
	const size_t bufsize = sizeof(arh->ar_name);
-

-
	assert(arh != NULL);
-
	assert(ar->e_kind == ELF_K_AR);
-
	assert((const unsigned char *) arh >= ar->e_rawfile &&
-
	    (const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize);
-

-
	buf = (const unsigned char *) arh->ar_name;
-

-
	/*
-
	 * Check for extended naming.
-
	 *
-
	 * If the name matches the pattern "^/[0-9]+", it is an
-
	 * SVR4-style extended name.  If the name matches the pattern
-
	 * "#1/[0-9]+", the entry uses BSD style extended naming.
-
	 */
-
	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((const char *) (buf + 1),
-
			bufsize - 1, 10, &offset) == 0) {
-
			LIBELF_SET_ERROR(ARCHIVE, 0);
-
			return (NULL);
-
		}
-

-
		if (offset > ar->e_u.e_ar.e_rawstrtabsz) {
-
			LIBELF_SET_ERROR(ARCHIVE, 0);
-
			return (NULL);
-
		}
-

-
		p = q = ar->e_u.e_ar.e_rawstrtab + offset;
-
		r = ar->e_u.e_ar.e_rawstrtab + ar->e_u.e_ar.e_rawstrtabsz;
-

-
		for (; p < r && *p != '/'; p++)
-
			;
-
		len = (size_t) (p - q + 1); /* space for the trailing NUL */
-

-
		if ((s = malloc(len)) == NULL) {
-
			LIBELF_SET_ERROR(RESOURCE, 0);
-
			return (NULL);
-
		}
-

-
		(void) strncpy(s, (const char *) q, len - 1);
-
		s[len - 1] = '\0';
-

-
		return (s);
-
	} else if (IS_EXTENDED_BSD_NAME(buf)) {
-
		r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
-

-
		if (_libelf_ar_get_number((const char *) r, bufsize -
-
			LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10,
-
			&len) == 0) {
-
			LIBELF_SET_ERROR(ARCHIVE, 0);
-
			return (NULL);
-
		}
-

-
		/*
-
		 * Allocate space for the file name plus a
-
		 * trailing NUL.
-
		 */
-
		if ((s = malloc(len + 1)) == NULL) {
-
			LIBELF_SET_ERROR(RESOURCE, 0);
-
			return (NULL);
-
		}
-

-
		/*
-
		 * The file name follows the archive header.
-
		 */
-
		q = (const unsigned char *) (arh + 1);
-

-
		(void) strncpy(s, (const char *) q, len);
-
		s[len] = '\0';
-

-
		return (s);
-
	}
-

-
	/*
-
	 * A 'normal' name.
-
	 *
-
	 * Skip back over trailing blanks from the end of the field.
-
	 * In the SVR4 format, a '/' is used as a terminator for
-
	 * non-special names.
-
	 */
-
	for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q)
-
		;
-

-
	if (q >= buf) {
-
		if (*q == '/') {
-
			/*
-
			 * SVR4 style names: ignore the trailing
-
			 * character '/', but only if the name is not
-
			 * one of the special names "/" and "//".
-
			 */
-
			if (q > buf + 1 ||
-
			    (q == (buf + 1) && *buf != '/'))
-
				q--;
-
		}
-

-
		len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */
-
	} else {
-
		/* The buffer only had blanks. */
-
		buf = (const unsigned char *) "";
-
		len = 1;
-
	}
-

-
	if ((s = malloc(len)) == NULL) {
-
		LIBELF_SET_ERROR(RESOURCE, 0);
-
		return (NULL);
-
	}
-

-
	(void) strncpy(s, (const char *) buf, len - 1);
-
	s[len - 1] = '\0';
-

-
	return (s);
-
}
-

-
/*
-
 * Return the raw name for an archive member, inclusive of any
-
 * formatting characters.
-
 */
-
char *
-
_libelf_ar_get_raw_name(const struct ar_hdr *arh)
-
{
-
	char *rawname;
-
	const size_t namesz = sizeof(arh->ar_name);
-

-
	if ((rawname = malloc(namesz + 1)) == NULL) {
-
		LIBELF_SET_ERROR(RESOURCE, 0);
-
		return (NULL);
-
	}
-

-
	(void) strncpy(rawname, arh->ar_name, namesz);
-
	rawname[namesz] = '\0';
-
	return (rawname);
-
}
-

-
/*
-
 * Open an 'ar' archive.
-
 */
-
Elf *
-
_libelf_ar_open(Elf *e, int reporterror)
-
{
-
	size_t sz;
-
	int scanahead;
-
	struct ar_hdr arh;
-
	unsigned char *s, *end;
-

-
	_libelf_init_elf(e, 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);
-

-
	/*
-
	 * We use heuristics to determine the flavor of the archive we
-
	 * are examining.
-
	 *
-
	 * SVR4 flavor archives use the name "/ " and "// " for
-
	 * special members.
-
	 *
-
	 * In BSD flavor archives the symbol table, if present, is the
-
	 * first archive with name "__.SYMDEF".
-
	 */
-

-
#define	READ_AR_HEADER(S, ARH, SZ, END)					\
-
	do {								\
-
		if ((S) + sizeof((ARH)) > (END))			\
-
		        goto error;					\
-
		(void) memcpy(&(ARH), (S), sizeof((ARH)));		\
-
		if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
-
			goto error;					\
-
		if (_libelf_ar_get_number((char *) (ARH).ar_size,	\
-
		    sizeof((ARH).ar_size), 10, &(SZ)) == 0)		\
-
			goto error;					\
-
	} while (0)
-

-
	READ_AR_HEADER(s, arh, sz, end);
-

-
	/*
-
	 * Handle special archive members for the SVR4 format.
-
	 */
-
	if (arh.ar_name[0] == '/') {
-
		if (sz == 0)
-
			goto error;
-

-
		e->e_flags |= LIBELF_F_AR_VARIANT_SVR4;
-

-
		scanahead = 0;
-

-
		/*
-
		 * The symbol table (file name "/ ") always comes before the
-
		 * string table (file name "// ").
-
		 */
-
		if (arh.ar_name[1] == ' ') {
-
			/* "/ " => symbol table. */
-
			scanahead = 1;	/* The string table to follow. */
-

-
			s += sizeof(arh);
-
			e->e_u.e_ar.e_rawsymtab = s;
-
			e->e_u.e_ar.e_rawsymtabsz = sz;
-

-
			sz = LIBELF_ADJUST_AR_SIZE(sz);
-
			s += sz;
-

-
		} else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') {
-
			/* "// " => string table for long file names. */
-
			s += sizeof(arh);
-
			e->e_u.e_ar.e_rawstrtab = s;
-
			e->e_u.e_ar.e_rawstrtabsz = sz;
-

-
			sz = LIBELF_ADJUST_AR_SIZE(sz);
-
			s += sz;
-
		}
-

-
		/*
-
		 * If the string table hasn't been seen yet, look for
-
		 * it in the next member.
-
		 */
-
		if (scanahead) {
-
			READ_AR_HEADER(s, arh, sz, end);
-

-
			/* "// " => string table for long file names. */
-
			if (arh.ar_name[0] == '/' && arh.ar_name[1] == '/' &&
-
			    arh.ar_name[2] == ' ') {
-

-
				s += sizeof(arh);
-

-
				e->e_u.e_ar.e_rawstrtab = s;
-
				e->e_u.e_ar.e_rawstrtabsz = sz;
-

-
				sz = LIBELF_ADJUST_AR_SIZE(sz);
-
				s += sz;
-
			}
-
		}
-
	} else if (strncmp(arh.ar_name, LIBELF_AR_BSD_SYMTAB_NAME,
-
		sizeof(LIBELF_AR_BSD_SYMTAB_NAME) - 1) == 0) {
-
		/*
-
		 * BSD style archive symbol table.
-
		 */
-
		s += sizeof(arh);
-
		e->e_u.e_ar.e_rawsymtab = s;
-
		e->e_u.e_ar.e_rawsymtabsz = sz;
-

-
		sz = LIBELF_ADJUST_AR_SIZE(sz);
-
		s += sz;
-
	}
-

-
	/*
-
	 * Update the 'next' offset, so that a subsequent elf_begin()
-
	 * works as expected.
-
	 */
-
	e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile);
-

-
	return (e);
-

-
error:
-
	if (!reporterror) {
-
		e->e_kind = ELF_K_NONE;
-
		return (e);
-
	}
-

-
	LIBELF_SET_ERROR(ARCHIVE, 0);
-
	return (NULL);
-
}
deleted external/libelf/libelf_checksum.c
@@ -1,100 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <gelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
-

-
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, (size_t) d->d_size);
-
	}
-

-
	/*
-
	 * Return a 16-bit checksum compatible with Solaris.
-
	 */
-
	return (long) (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL));
-
}
deleted external/libelf/libelf_convert.m4
@@ -1,1089 +0,0 @@
-
/*-
-
 * Copyright (c) 2006-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 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>
-

-
#include <assert.h>
-
#include <libelf.h>
-
#include <string.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/* WARNING: GENERATED FROM __file__. */
-

-
divert(-1)
-

-
# Generate conversion routines for converting between in-memory and
-
# file representations of Elf data structures.
-
#
-
# These conversions use the type information defined in `elf_types.m4'.
-

-
include(SRCDIR`/elf_types.m4')
-

-
# For the purposes of generating conversion code, ELF types may be
-
# classified according to the following characteristics:
-
#
-
# 1. Whether the ELF type can be directly mapped to an integral C
-
#    language type.  For example, the ELF_T_WORD type maps directly to
-
#    a 'uint32_t', but ELF_T_GNUHASH lacks a matching C type.
-
#
-
# 2. Whether the type has word size dependent variants.  For example,
-
#    ELT_T_EHDR is represented using C types Elf32_Ehdr and El64_Ehdr,
-
#    and the ELF_T_ADDR and ELF_T_OFF types have integral C types that
-
#    can be 32- or 64- bit wide.
-
#
-
# 3. Whether the ELF types has a fixed representation or not.  For
-
#    example, the ELF_T_SYM type has a fixed size file representation,
-
#    some types like ELF_T_NOTE and ELF_T_GNUHASH use a variable size
-
#    representation.
-
#
-
# We use m4 macros to generate conversion code for ELF types that have
-
# a fixed size representation.  Conversion functions for the remaining
-
# types are coded by hand.
-
#
-
#* Handling File and Memory Representations
-
#
-
# `In-memory' representations of an Elf data structure use natural
-
# alignments and native byte ordering.  This allows pointer arithmetic
-
# and casting to work as expected.  On the other hand, the `file'
-
# representation of an ELF data structure could possibly be packed
-
# tighter than its `in-memory' representation, and could be of a
-
# differing byte order.  Reading ELF objects that are members of `ar'
-
# archives present an additional complication: `ar' pads file data to
-
# even addresses, so file data structures in an archive member
-
# residing inside an `ar' archive could be at misaligned memory
-
# addresses when brought into memory.
-
#
-
# In summary, 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.
-

-
# NOCVT(TYPE) -- Do not generate the cvt[] structure entry for TYPE
-
define(`NOCVT',`define(`NOCVT_'$1,1)')
-

-
# NOFUNC(TYPE) -- Do not generate a conversion function for TYPE
-
define(`NOFUNC',`define(`NOFUNC_'$1,1)')
-

-
# IGNORE(TYPE) -- Completely ignore the type.
-
define(`IGNORE',`NOCVT($1)NOFUNC($1)')
-

-
# Mark ELF types that should not be processed by the M4 macros below.
-

-
# Types for which we use functions with non-standard names.
-
IGNORE(`BYTE')			# Uses a wrapper around memcpy().
-
IGNORE(`NOTE')			# Not a fixed size type.
-

-
# Types for which we supply hand-coded functions.
-
NOFUNC(`GNUHASH')		# A type with complex internal structure.
-
NOFUNC(`VDEF')			# See MAKE_VERSION_CONVERTERS below.
-
NOFUNC(`VNEED')			# ..
-

-
# Unimplemented types.
-
IGNORE(`MOVEP')
-

-
# ELF types that don't exist in a 32-bit world.
-
NOFUNC(`XWORD32')
-
NOFUNC(`SXWORD32')
-

-
# `Primitive' ELF types are those that are an alias for an integral
-
# type.  As they have no internal structure, they can be copied using
-
# a `memcpy()', and byteswapped in straightforward way.
-
#
-
# Mark all ELF types that directly map to integral C types.
-
define(`PRIM_ADDR',	1)
-
define(`PRIM_BYTE',	1)
-
define(`PRIM_HALF',	1)
-
define(`PRIM_LWORD',	1)
-
define(`PRIM_OFF',	1)
-
define(`PRIM_SWORD',	1)
-
define(`PRIM_SXWORD',	1)
-
define(`PRIM_WORD',	1)
-
define(`PRIM_XWORD',	1)
-

-
# Note the primitive types that are size-dependent.
-
define(`SIZEDEP_ADDR',	1)
-
define(`SIZEDEP_OFF',	1)
-

-
# Generate conversion functions for primitive types.
-
#
-
# Macro use: MAKEPRIMFUNCS(ELFTYPE,CTYPE,TYPESIZE,SYMSIZE)
-
# `$1': Name of the ELF type.
-
# `$2': C structure name suffix.
-
# `$3': ELF class specifier for types, one of [`32', `64'].
-
# `$4': Additional ELF class specifier, one of [`', `32', `64'].
-
#
-
# Generates a pair of conversion functions.
-
define(`MAKEPRIMFUNCS',`
-
static int
-
_libelf_cvt_$1$4_tof(unsigned char *dst, size_t dsz, unsigned char *src,
-
    size_t count, int byteswap)
-
{
-
	Elf$3_$2 t, *s = (Elf$3_$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$4(t);
-
		WRITE_$1$4(dst,t);
-
	}
-

-
	return (1);
-
}
-

-
static int
-
_libelf_cvt_$1$4_tom(unsigned char *dst, size_t dsz, unsigned char *src,
-
    size_t count, int byteswap)
-
{
-
	Elf$3_$2 t, *d = (Elf$3_$2 *) (uintptr_t) dst;
-
	size_t c;
-

-
	if (dsz < count * sizeof(Elf$3_$2))
-
		return (0);
-

-
	if (!byteswap) {
-
		(void) memcpy(dst, src, count * sizeof(*d));
-
		return (1);
-
	}
-

-
	for (c = 0; c < count; c++) {
-
		READ_$1$4(src,t);
-
		SWAP_$1$4(t);
-
		*d++ = t;
-
	}
-

-
	return (1);
-
}
-
')
-

-
#
-
# Handling composite ELF types
-
#
-

-
# SWAP_FIELD(FIELDNAME,ELFTYPE) -- Generate code to swap one field.
-
define(`SWAP_FIELD',
-
  `ifdef(`SIZEDEP_'$2,
-
    `SWAP_$2'SZ()`(t.$1);
-
			',
-
    `SWAP_$2(t.$1);
-
			')')
-

-
# SWAP_MEMBERS(STRUCT) -- Iterate over a structure definition.
-
define(`SWAP_MEMBERS',
-
  `ifelse($#,1,`/**/',
-
     `SWAP_FIELD($1)SWAP_MEMBERS(shift($@))')')
-

-
# SWAP_STRUCT(CTYPE,SIZE) -- Generate code to swap an ELF structure.
-
define(`SWAP_STRUCT',
-
  `pushdef(`SZ',$2)/* Swap an Elf$2_$1 */
-
			SWAP_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
-

-
# WRITE_FIELD(ELFTYPE,FIELDNAME) -- Generate code to write one field.
-
define(`WRITE_FIELD',
-
  `ifdef(`SIZEDEP_'$2,
-
    `WRITE_$2'SZ()`(dst,t.$1);
-
		',
-
    `WRITE_$2(dst,t.$1);
-
		')')
-

-
# WRITE_MEMBERS(ELFTYPELIST) -- Iterate over a structure definition.
-
define(`WRITE_MEMBERS',
-
  `ifelse($#,1,`/**/',
-
    `WRITE_FIELD($1)WRITE_MEMBERS(shift($@))')')
-

-
# WRITE_STRUCT(CTYPE,SIZE) -- Generate code to write out an ELF structure.
-
define(`WRITE_STRUCT',
-
  `pushdef(`SZ',$2)/* Write an Elf$2_$1 */
-
		WRITE_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
-

-
# READ_FIELD(ELFTYPE,CTYPE) -- Generate code to read one field.
-
define(`READ_FIELD',
-
  `ifdef(`SIZEDEP_'$2,
-
    `READ_$2'SZ()`(s,t.$1);
-
		',
-
    `READ_$2(s,t.$1);
-
		')')
-

-
# READ_MEMBERS(ELFTYPELIST) -- Iterate over a structure definition.
-
define(`READ_MEMBERS',
-
  `ifelse($#,1,`/**/',
-
    `READ_FIELD($1)READ_MEMBERS(shift($@))')')
-

-
# READ_STRUCT(CTYPE,SIZE) -- Generate code to read an ELF structure.
-
define(`READ_STRUCT',
-
  `pushdef(`SZ',$2)/* Read an Elf$2_$1 */
-
		READ_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
-

-

-
# MAKECOMPFUNCS -- Generate converters for composite ELF 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(`MAKECOMPFUNCS', `ifdef(`NOFUNC_'$1$3,`',`
-
static int
-
_libelf_cvt_$1$3_tof(unsigned char *dst, size_t dsz, unsigned 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);
-
}
-

-
static int
-
_libelf_cvt_$1$3_tom(unsigned char *dst, size_t dsz, unsigned char *src,
-
    size_t count, int byteswap)
-
{
-
	Elf$3_$2	t, *d;
-
	unsigned 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  = 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_CONVERTER(ELFTYPE,CTYPE)
-
#
-
# Make type convertor functions from the type definition
-
# of the ELF type:
-
# - Skip convertors marked as `NOFUNC'.
-
# - Invoke `MAKEPRIMFUNCS' or `MAKECOMPFUNCS' as appropriate.
-
define(`MAKE_TYPE_CONVERTER',
-
  `ifdef(`NOFUNC_'$1,`',
-
    `ifdef(`PRIM_'$1,
-
      `ifdef(`SIZEDEP_'$1,
-
	`MAKEPRIMFUNCS($1,$2,32,32)dnl
-
	 MAKEPRIMFUNCS($1,$2,64,64)',
-
	`MAKEPRIMFUNCS($1,$2,64)')',
-
      `MAKECOMPFUNCS($1,$2,32)dnl
-
       MAKECOMPFUNCS($1,$2,64)')')')
-

-
# MAKE_TYPE_CONVERTERS(ELFTYPELIST) -- Generate conversion functions.
-
define(`MAKE_TYPE_CONVERTERS',
-
  `ifelse($#,1,`',
-
    `MAKE_TYPE_CONVERTER($1)MAKE_TYPE_CONVERTERS(shift($@))')')
-

-

-
#
-
# Macros to generate entries for the table of convertors.
-
#
-

-
# CONV(ELFTYPE,SIZE,DIRECTION)
-
#
-
# Generate the name of a convertor function.
-
define(`CONV',
-
  `ifdef(`NOFUNC_'$1$2,
-
    `.$3$2 = NULL',
-
    `ifdef(`PRIM_'$1,
-
      `ifdef(`SIZEDEP_'$1,
-
	`.$3$2 = _libelf_cvt_$1$2_$3',
-
	`.$3$2 = _libelf_cvt_$1_$3')',
-
      `.$3$2 = _libelf_cvt_$1$2_$3')')')
-

-
# CONVERTER_NAME(ELFTYPE)
-
#
-
# Generate the contents of one `struct cvt' instance.
-
define(`CONVERTER_NAME',
-
  `ifdef(`NOCVT_'$1,`',
-
    `	[ELF_T_$1] = {
-
		CONV($1,32,tof),
-
		CONV($1,32,tom),
-
		CONV($1,64,tof),
-
		CONV($1,64,tom)
-
	},
-

-
')')
-

-
# CONVERTER_NAMES(ELFTYPELIST)
-
#
-
# Generate the `struct cvt[]' array.
-
define(`CONVERTER_NAMES',
-
  `ifelse($#,1,`',
-
    `CONVERTER_NAME($1)CONVERTER_NAMES(shift($@))')')
-

-
#
-
# Handling ELF version sections.
-
#
-

-
# _FSZ(FIELD,BASETYPE) - return the file size for a field.
-
define(`_FSZ',
-
  `ifelse($2,`HALF',2,
-
     $2,`WORD',4)')
-

-
# FSZ(STRUCT) - determine the file size of a structure.
-
define(`FSZ',
-
  `ifelse($#,1,0,
-
    `eval(_FSZ($1) + FSZ(shift($@)))')')
-

-
# MAKE_VERSION_CONVERTERS(TYPE,BASE,AUX,PFX) -- Generate conversion
-
# functions for versioning structures.
-
define(`MAKE_VERSION_CONVERTERS',
-
  `MAKE_VERSION_CONVERTER($1,$2,$3,$4,32)
-
   MAKE_VERSION_CONVERTER($1,$2,$3,$4,64)')
-

-
# MAKE_VERSION_CONVERTOR(TYPE,CBASE,CAUX,PFX,SIZE) -- Generate a
-
# conversion function.
-
define(`MAKE_VERSION_CONVERTER',`
-
static int
-
_libelf_cvt_$1$5_tof(unsigned char *dst, size_t dsz, unsigned char *src,
-
    size_t count, int byteswap)
-
{
-
	Elf$5_$2	t;
-
	Elf$5_$3	a;
-
	const size_t	verfsz = FSZ(Elf$5_$2_DEF);
-
	const size_t	auxfsz = FSZ(Elf$5_$3_DEF);
-
	const size_t	vermsz = sizeof(Elf$5_$2);
-
	const size_t	auxmsz = sizeof(Elf$5_$3);
-
	unsigned char * const dstend = dst + dsz;
-
	unsigned char * const srcend = src + count;
-
	unsigned char	*dtmp, *dstaux, *srcaux;
-
	Elf$5_Word	aux, anext, cnt, vnext;
-

-
	for (dtmp = dst, vnext = ~0U;
-
	     vnext != 0 && dtmp + verfsz <= dstend && src + vermsz <= srcend;
-
	     dtmp += vnext, src += vnext) {
-

-
		/* Read in an Elf$5_$2 structure. */
-
		t = *((Elf$5_$2 *) (uintptr_t) src);
-

-
		aux = t.$4_aux;
-
		cnt = t.$4_cnt;
-
		vnext = t.$4_next;
-

-
		if (byteswap) {
-
			SWAP_STRUCT($2, $5)
-
		}
-

-
		dst = dtmp;
-
		WRITE_STRUCT($2, $5)
-

-
		if (aux < verfsz)
-
			return (0);
-

-
		/* Process AUX entries. */
-
		for (anext = ~0U, dstaux = dtmp + aux, srcaux = src + aux;
-
		     cnt != 0 && anext != 0 && dstaux + auxfsz <= dstend &&
-
			srcaux + auxmsz <= srcend;
-
		     dstaux += anext, srcaux += anext, cnt--) {
-

-
			/* Read in an Elf$5_$3 structure. */
-
			a = *((Elf$5_$3 *) (uintptr_t) srcaux);
-
			anext = a.$4a_next;
-

-
			if (byteswap) {
-
				pushdef(`t',`a')SWAP_STRUCT($3, $5)popdef(`t')
-
			}
-

-
			dst = dstaux;
-
			pushdef(`t',`a')WRITE_STRUCT($3, $5)popdef(`t')
-
		}
-

-
		if (anext || cnt)
-
			return (0);
-
	}
-

-
	if (vnext)
-
		return (0);
-

-
	return (1);
-
}
-

-
static int
-
_libelf_cvt_$1$5_tom(unsigned char *dst, size_t dsz, unsigned char *src,
-
    size_t count, int byteswap)
-
{
-
	Elf$5_$2	t, *dp;
-
	Elf$5_$3	a, *ap;
-
	const size_t	verfsz = FSZ(Elf$5_$2_DEF);
-
	const size_t	auxfsz = FSZ(Elf$5_$3_DEF);
-
	const size_t	vermsz = sizeof(Elf$5_$2);
-
	const size_t	auxmsz = sizeof(Elf$5_$3);
-
	unsigned char * const dstend = dst + dsz;
-
	unsigned char * const srcend = src + count;
-
	unsigned char	*dstaux, *s, *srcaux, *stmp;
-
	Elf$5_Word	aux, anext, cnt, vnext;
-

-
	for (stmp = src, vnext = ~0U;
-
	     vnext != 0 && stmp + verfsz <= srcend && dst + vermsz <= dstend;
-
	     stmp += vnext, dst += vnext) {
-

-
		/* Read in a $1 structure. */
-
		s = stmp;
-
		READ_STRUCT($2, $5)
-
		if (byteswap) {
-
			SWAP_STRUCT($2, $5)
-
		}
-

-
		dp = (Elf$5_$2 *) (uintptr_t) dst;
-
		*dp = t;
-

-
		aux = t.$4_aux;
-
		cnt = t.$4_cnt;
-
		vnext = t.$4_next;
-

-
		if (aux < vermsz)
-
			return (0);
-

-
		/* Process AUX entries. */
-
		for (anext = ~0U, dstaux = dst + aux, srcaux = stmp + aux;
-
		     cnt != 0 && anext != 0 && dstaux + auxmsz <= dstend &&
-
			srcaux + auxfsz <= srcend;
-
		     dstaux += anext, srcaux += anext, cnt--) {
-

-
			s = srcaux;
-
			pushdef(`t',`a')READ_STRUCT($3, $5)popdef(`t')
-

-
			if (byteswap) {
-
				pushdef(`t',`a')SWAP_STRUCT($3, $5)popdef(`t')
-
			}
-

-
			anext = a.$4a_next;
-

-
			ap = ((Elf$5_$3 *) (uintptr_t) dstaux);
-
			*ap = a;
-
		}
-

-
		if (anext || cnt)
-
			return (0);
-
	}
-

-
	if (vnext)
-
		return (0);
-

-
	return (1);
-
}')
-

-
divert(0)
-

-
/*
-
 * C macros to byte swap integral quantities.
-
 */
-

-
#define	SWAP_BYTE(X)	do { (void) (X); } while (0)
-
#define	SWAP_IDENT(X)	do { (void) (X); } while (0)
-
#define	SWAP_HALF(X)	do {						\
-
		uint16_t _x = (uint16_t) (X);				\
-
		uint32_t _t = _x & 0xFFU;				\
-
		_t <<= 8U; _x >>= 8U; _t |= _x & 0xFFU;			\
-
		(X) = (uint16_t) _t;					\
-
	} while (0)
-
#define	_SWAP_WORD(X, T) 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) _t;						\
-
	} while (0)
-
#define	SWAP_ADDR32(X)	_SWAP_WORD(X, Elf32_Addr)
-
#define	SWAP_OFF32(X)	_SWAP_WORD(X, Elf32_Off)
-
#define	SWAP_SWORD(X)	_SWAP_WORD(X, Elf32_Sword)
-
#define	SWAP_WORD(X)	_SWAP_WORD(X, Elf32_Word)
-
#define	_SWAP_WORD64(X, T) 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) _t;						\
-
	} while (0)
-
#define	SWAP_ADDR64(X)	_SWAP_WORD64(X, Elf64_Addr)
-
#define	SWAP_LWORD(X)	_SWAP_WORD64(X, Elf64_Lword)
-
#define	SWAP_OFF64(X)	_SWAP_WORD64(X, Elf64_Off)
-
#define	SWAP_SXWORD(X)	_SWAP_WORD64(X, Elf64_Sxword)
-
#define	SWAP_XWORD(X)	_SWAP_WORD64(X, Elf64_Xword)
-

-
/*
-
 * C macros to write out various integral values.
-
 *
-
 * Note:
-
 * - 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 {						\
-
		unsigned char *const _p = (unsigned char *) (P);	\
-
		_p[0]		= (unsigned char) (X);			\
-
		(P)		= _p + 1;				\
-
	} while (0)
-
#define	WRITE_HALF(P,X)	do {						\
-
		uint16_t _t	= (X);					\
-
		unsigned char *const _p	= (unsigned char *) (P);	\
-
		const unsigned char *const _q = (unsigned char *) &_t;	\
-
		_p[0]		= _q[0];				\
-
		_p[1]		= _q[1];				\
-
		(P)		= _p + 2;				\
-
	} while (0)
-
#define	WRITE_WORD(P,X) do {						\
-
		uint32_t _t	= (uint32_t) (X);			\
-
		unsigned char *const _p	= (unsigned char *) (P);	\
-
		const unsigned char *const _q = (unsigned 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	= (uint64_t) (X);			\
-
		unsigned char *const _p	= (unsigned char *) (P);	\
-
		const unsigned char *const _q = (unsigned 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)
-

-
/*
-
 * C macros to read in various integral values.
-
 *
-
 * Note:
-
 * - 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 unsigned char *const _p =				\
-
			(const unsigned char *) (P);			\
-
		(X)		= _p[0];				\
-
		(P)		= (P) + 1;				\
-
	} while (0)
-
#define	READ_HALF(P,X)	do {						\
-
		uint16_t _t;						\
-
		unsigned char *const _q = (unsigned char *) &_t;	\
-
		const unsigned char *const _p =				\
-
			(const unsigned char *) (P);			\
-
		_q[0]		= _p[0];				\
-
		_q[1]		= _p[1];				\
-
		(P)		= (P) + 2;				\
-
		(X)		= _t;					\
-
	} while (0)
-
#define	_READ_WORD(P,X,T) do {						\
-
		uint32_t _t;						\
-
		unsigned char *const _q = (unsigned char *) &_t;	\
-
		const unsigned char *const _p =				\
-
			(const unsigned char *) (P);			\
-
		_q[0]		= _p[0];				\
-
		_q[1]		= _p[1];				\
-
		_q[2]		= _p[2];				\
-
		_q[3]		= _p[3];				\
-
		(P)		= (P) + 4;				\
-
		(X)		= (T) _t;				\
-
	} while (0)
-
#define	READ_ADDR32(P,X)	_READ_WORD(P, X, Elf32_Addr)
-
#define	READ_OFF32(P,X)		_READ_WORD(P, X, Elf32_Off)
-
#define	READ_SWORD(P,X)		_READ_WORD(P, X, Elf32_Sword)
-
#define	READ_WORD(P,X)		_READ_WORD(P, X, Elf32_Word)
-
#define	_READ_WORD64(P,X,T)	do {					\
-
		uint64_t _t;						\
-
		unsigned char *const _q = (unsigned char *) &_t;	\
-
		const unsigned char *const _p =				\
-
			(const unsigned 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) _t;				\
-
	} while (0)
-
#define	READ_ADDR64(P,X)	_READ_WORD64(P, X, Elf64_Addr)
-
#define	READ_LWORD(P,X)		_READ_WORD64(P, X, Elf64_Lword)
-
#define	READ_OFF64(P,X)		_READ_WORD64(P, X, Elf64_Off)
-
#define	READ_SXWORD(P,X)	_READ_WORD64(P, X, Elf64_Sxword)
-
#define	READ_XWORD(P,X)		_READ_WORD64(P, X, Elf64_Xword)
-
#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))
-

-
/*[*/
-
MAKE_TYPE_CONVERTERS(ELF_TYPE_LIST)
-
MAKE_VERSION_CONVERTERS(VDEF,Verdef,Verdaux,vd)
-
MAKE_VERSION_CONVERTERS(VNEED,Verneed,Vernaux,vn)
-
/*]*/
-

-
/*
-
 * 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(unsigned char *dst, size_t dsz, unsigned char *src,
-
    size_t count, int byteswap)
-
{
-
	(void) byteswap;
-
	if (dsz < count)
-
		return (0);
-
	if (dst != src)
-
		(void) memcpy(dst, src, count);
-
	return (1);
-
}
-

-
/*
-
 * 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_cvt_GNUHASH32_tom(unsigned char *dst, size_t dsz, unsigned char *src,
-
    size_t srcsz, int byteswap)
-
{
-
	return (_libelf_cvt_WORD_tom(dst, dsz, src, srcsz / sizeof(uint32_t),
-
		byteswap));
-
}
-

-
static int
-
_libelf_cvt_GNUHASH32_tof(unsigned char *dst, size_t dsz, unsigned char *src,
-
    size_t srcsz, int byteswap)
-
{
-
	return (_libelf_cvt_WORD_tof(dst, dsz, src, srcsz / sizeof(uint32_t),
-
		byteswap));
-
}
-

-
static int
-
_libelf_cvt_GNUHASH64_tom(unsigned char *dst, size_t dsz, unsigned 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_cvt_GNUHASH64_tof(unsigned char *dst, size_t dsz, unsigned 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);
-
}
-

-
/*
-
 * 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(unsigned char *dst, size_t dsz, unsigned 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, 4U);
-
		ROUNDUP2(descsz, 4U);
-

-
		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(unsigned char *dst, size_t dsz, unsigned 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;
-

-
		sz = namesz;
-
		ROUNDUP2(sz, 4U);
-
		sz += descsz;
-
		ROUNDUP2(sz, 4U);
-

-
		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);
-

-
		if (count < sz)
-
			sz = count;
-

-
		(void) memcpy(dst, src, sz);
-

-
		src += sz;
-
		dst += sz;
-
		count -= sz;
-
	}
-

-
	return (1);
-
}
-

-
struct converters {
-
	int	(*tof32)(unsigned char *dst, size_t dsz, unsigned char *src,
-
		    size_t cnt, int byteswap);
-
	int	(*tom32)(unsigned char *dst, size_t dsz, unsigned char *src,
-
		    size_t cnt, int byteswap);
-
	int	(*tof64)(unsigned char *dst, size_t dsz, unsigned char *src,
-
		    size_t cnt, int byteswap);
-
	int	(*tom64)(unsigned char *dst, size_t dsz, unsigned char *src,
-
		    size_t cnt, int byteswap);
-
};
-

-

-
static struct converters cvt[ELF_T_NUM] = {
-
	/*[*/
-
CONVERTER_NAMES(ELF_TYPE_LIST)
-
	/*]*/
-

-
	/*
-
	 * Types that need 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
-
	},
-

-
	[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))
-
 (unsigned char *_dst, size_t dsz, unsigned 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));
-
}
deleted external/libelf/libelf_data.c
@@ -1,103 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
int
-
_libelf_xlate_shtype(uint32_t sht)
-
{
-
	/*
-
	 * Look for known section types.
-
	 */
-
	switch (sht) {
-
	case SHT_DYNAMIC:
-
		return (ELF_T_DYN);
-
	case SHT_DYNSYM:
-
		return (ELF_T_SYM);
-
	case SHT_FINI_ARRAY:
-
		return (ELF_T_ADDR);
-
	case SHT_GNU_HASH:
-
		return (ELF_T_GNUHASH);
-
	case SHT_GNU_LIBLIST:
-
		return (ELF_T_WORD);
-
	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);
-
	case SHT_SUNW_dof:
-
		return (ELF_T_BYTE);
-
	case SHT_SUNW_move:
-
		return (ELF_T_MOVE);
-
	case SHT_SUNW_syminfo:
-
		return (ELF_T_SYMINFO);
-
	case SHT_SUNW_verdef:	/* == SHT_GNU_verdef */
-
		return (ELF_T_VDEF);
-
	case SHT_SUNW_verneed:	/* == SHT_GNU_verneed */
-
		return (ELF_T_VNEED);
-
	case SHT_SUNW_versym:	/* == SHT_GNU_versym */
-
		return (ELF_T_HALF);
-
	default:
-
		/*
-
		 * Values in the range [SHT_LOOS..SHT_HIUSER] (i.e.,
-
		 * OS, processor and user-defined section types) are
-
		 * legal, but since we do not know anything more about
-
		 * their semantics, we return a type of ELF_T_BYTE.
-
		 */
-
		if (sht >= SHT_LOOS && sht <= SHT_HIUSER)
-
			return (ELF_T_BYTE);
-

-
		/*
-
		 * Other values are unsupported.
-
		 */
-
		return (-1);
-
	}
-
}
deleted external/libelf/libelf_ehdr.c
@@ -1,206 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <libelf.h>
-
#include <stdlib.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * 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)(unsigned char *_d, size_t _dsz, unsigned 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)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr),
-
	    (unsigned char *) 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 = (size_t) 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) & 0xFFU; \
-
		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)(unsigned char *_d, size_t _dsz, unsigned 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)((unsigned char*) 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);
-
}
deleted external/libelf/libelf_extended.c
@@ -1,136 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * 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 & 0xFFFFU;
-
	else
-
		((Elf64_Ehdr *) eh)->e_shnum = shnum & 0xFFFFU;
-

-

-
	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 & 0xFFFFU;
-
	else
-
		((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx & 0xFFFFU;
-

-
	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 & 0xFFFFU;
-
	else
-
		((Elf64_Ehdr *) eh)->e_phnum = phnum & 0xFFFFU;
-

-
	return (1);
-
}
deleted external/libelf/libelf_fsize.m4
@@ -1,159 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008-2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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 <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/* WARNING: GENERATED FROM __file__. */
-

-
/*
-
 * 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)
-
define(`VDEF_SIZE',	1)
-
define(`VNEED_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',
-
  `[ELF_T_$1] = { .fsz32 = $1_SIZE32, .fsz64 = $1_SIZE64 },
-
')
-
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);
-
}
deleted external/libelf/libelf_memory.c
@@ -1,96 +0,0 @@
-
/*-
-
 * Copyright (c) 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 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 <ar.h>
-
#include <assert.h>
-
#include <string.h>
-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * Create an ELF descriptor for a memory image, optionally reporting
-
 * parse errors.
-
 */
-

-
Elf *
-
_libelf_memory(unsigned char *image, size_t sz, int reporterror)
-
{
-
	Elf *e;
-
	int e_class;
-
	enum Elf_Error error;
-
	unsigned int e_byteorder, e_version;
-

-
	assert(image != NULL);
-
	assert(sz > 0);
-

-
	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)) {
-
		e_byteorder = image[EI_DATA];
-
		e_class     = image[EI_CLASS];
-
		e_version   = image[EI_VERSION];
-

-
		error = ELF_E_NONE;
-

-
		if (e_version > EV_CURRENT)
-
			error = ELF_E_VERSION;
-
		else if ((e_byteorder != ELFDATA2LSB && e_byteorder !=
-
 		    ELFDATA2MSB) || (e_class != ELFCLASS32 && e_class !=
-
		    ELFCLASS64))
-
			error = ELF_E_HEADER;
-

-
		if (error != ELF_E_NONE) {
-
			if (reporterror) {
-
				LIBELF_PRIVATE(error) = LIBELF_ERROR(error, 0);
-
				(void) _libelf_release_elf(e);
-
				return (NULL);
-
			}
-
		} else {
-
			_libelf_init_elf(e, ELF_K_ELF);
-

-
			e->e_byteorder = e_byteorder;
-
			e->e_class = e_class;
-
			e->e_version = e_version;
-
		}
-
	} else if (sz >= SARMAG &&
-
	    strncmp((const char *) image, ARMAG, (size_t) SARMAG) == 0)
-
		return (_libelf_ar_open(e, reporterror));
-

-
	return (e);
-
}
deleted external/libelf/libelf_msize.m4
@@ -1,108 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008-2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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>
-

-
#include <assert.h>
-
#include <libelf.h>
-
#include <string.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/* WARNING: GENERATED FROM __file__. */
-

-
struct msize {
-
	size_t	msz32;
-
	size_t	msz64;
-
};
-

-
divert(-1)
-
include(SRCDIR`/elf_types.m4')
-

-
/*
-
 * ELF types whose memory representations have a variable size.
-
 */
-
define(BYTE_SIZE,	1)
-
define(GNUHASH_SIZE,	1)
-
define(NOTE_SIZE,	1)
-
define(VDEF_SIZE,	1)
-
define(VNEED_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',
-
  `[ELF_T_$1] = { .msz32 = $1_SIZE32, .msz64 = $1_SIZE64 },
-
')
-
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);
-
}
deleted external/libelf/libelf_open.c
@@ -1,249 +0,0 @@
-
/*-
-
 * Copyright (c) 2006,2008-2011 Joseph Koshy
-
 * All rights reserved.
-
 *
-
 * Redistribution and use in source and binary forms, with or without
-
 * modification, are permitted provided that the following conditions
-
 * are met:
-
 * 1. Redistributions of source code must retain the above copyright
-
 *    notice, this list of conditions and the following disclaimer.
-
 * 2. Redistributions in binary form must reproduce the above copyright
-
 *    notice, this list of conditions and the following disclaimer in the
-
 *    documentation and/or other materials provided with the distribution.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY 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/types.h>
-
#include <sys/stat.h>
-

-
#include <assert.h>
-
#include <errno.h>
-
#include <libelf.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-

-
#include "_libelf.h"
-

-
#if	ELFTC_HAVE_MMAP
-
#include <sys/mman.h>
-
#endif
-

-
ELFTC_VCSID("$Id$");
-

-
#define	_LIBELF_INITSIZE	(64*1024)
-

-
/*
-
 * Read from a device file, pipe or socket.
-
 */
-
static void *
-
_libelf_read_special_file(int fd, size_t *fsz)
-
{
-
	ssize_t readsz;
-
	size_t bufsz, datasz;
-
	unsigned char *buf, *t;
-

-
	datasz = 0;
-
	readsz = 0;
-
	bufsz = _LIBELF_INITSIZE;
-
	if ((buf = malloc(bufsz)) == NULL)
-
		goto resourceerror;
-

-
	/*
-
	 * Read data from the file descriptor till we reach EOF, or
-
	 * till an error is encountered.
-
	 */
-
	do {
-
		/* Check if we need to expand the data buffer. */
-
		if (datasz == bufsz) {
-
			bufsz *= 2;
-
			if ((t = realloc(buf, bufsz)) == NULL)
-
				goto resourceerror;
-
			buf = t;
-
		}
-

-
		do {
-
			assert(bufsz - datasz > 0);
-
			t = buf + datasz;
-
			if ((readsz = read(fd, t, bufsz - datasz)) <= 0)
-
				break;
-
			datasz += (size_t) readsz;
-
		} while (datasz < bufsz);
-

-
	} while (readsz > 0);
-

-
	if (readsz < 0) {
-
		LIBELF_SET_ERROR(IO, errno);
-
		goto error;
-
	}
-

-
	assert(readsz == 0);
-

-
	/*
-
	 * Free up extra buffer space.
-
	 */
-
	if (bufsz > datasz) {
-
		if (datasz > 0) {
-
			if ((t = realloc(buf, datasz)) == NULL)
-
				goto resourceerror;
-
			buf = t;
-
		} else {	/* Zero bytes read. */
-
			LIBELF_SET_ERROR(ARGUMENT, 0);
-
			free(buf);
-
			buf = NULL;
-
		}
-
	}
-

-
	*fsz = datasz;
-
	return (buf);
-

-
resourceerror:
-
	LIBELF_SET_ERROR(RESOURCE, 0);
-
error:
-
	if (buf != NULL)
-
		free(buf);
-
	return (NULL);
-
}
-

-
/*
-
 * Read the contents of the file referenced by the file descriptor
-
 * 'fd'.
-
 */
-

-
Elf *
-
_libelf_open_object(int fd, Elf_Cmd c, int reporterror)
-
{
-
	Elf *e;
-
	void *m;
-
	mode_t mode;
-
	size_t fsize;
-
	struct stat sb;
-
	unsigned int flags;
-

-
	assert(c == ELF_C_READ || c == ELF_C_RDWR || c == ELF_C_WRITE);
-

-
	if (fstat(fd, &sb) < 0) {
-
		LIBELF_SET_ERROR(IO, errno);
-
		return (NULL);
-
	}
-

-
	mode = sb.st_mode;
-
	fsize = (size_t) sb.st_size;
-

-
	/*
-
	 * Reject unsupported file types.
-
	 */
-
	if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&
-
	    !S_ISSOCK(mode)) {
-
		LIBELF_SET_ERROR(ARGUMENT, 0);
-
		return (NULL);
-
	}
-

-
	/*
-
	 * For ELF_C_WRITE mode, allocate and return a descriptor.
-
	 */
-
	if (c == ELF_C_WRITE) {
-
		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;
-
			if (!S_ISREG(mode))
-
				e->e_flags |= LIBELF_F_SPECIAL_FILE;
-
		}
-

-
		return (e);
-
	}
-

-

-
	/*
-
	 * ELF_C_READ and ELF_C_RDWR mode.
-
	 */
-
	m = NULL;
-
	flags = 0;
-
	if (S_ISREG(mode)) {
-

-
		/*
-
		 * Reject zero length files.
-
		 */
-
		if (fsize == 0) {
-
			LIBELF_SET_ERROR(ARGUMENT, 0);
-
			return (NULL);
-
		}
-

-
#if	ELFTC_HAVE_MMAP
-
		/*
-
		 * Always map regular files in with 'PROT_READ'
-
		 * permissions.
-
		 *
-
		 * For objects opened in ELF_C_RDWR mode, when
-
		 * elf_update(3) is called, we remove this mapping,
-
		 * write file data out using write(2), and map the new
-
		 * contents back.
-
		 */
-
		m = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, (off_t) 0);
-

-
		if (m == MAP_FAILED)
-
			m = NULL;
-
		else
-
			flags = LIBELF_F_RAWFILE_MMAP;
-
#endif
-

-
		/*
-
		 * Fallback to a read() if the call to mmap() failed,
-
		 * or if mmap() is not available.
-
		 */
-
		if (m == NULL) {
-
			if ((m = malloc(fsize)) == NULL) {
-
				LIBELF_SET_ERROR(RESOURCE, 0);
-
				return (NULL);
-
			}
-

-
			if (read(fd, m, fsize) != (ssize_t) fsize) {
-
				LIBELF_SET_ERROR(IO, errno);
-
				free(m);
-
				return (NULL);
-
			}
-

-
			flags = LIBELF_F_RAWFILE_MALLOC;
-
		}
-
	} else if ((m = _libelf_read_special_file(fd, &fsize)) != NULL)
-
		flags = LIBELF_F_RAWFILE_MALLOC | LIBELF_F_SPECIAL_FILE;
-
	else
-
		return (NULL);
-

-
	if ((e = _libelf_memory(m, fsize, reporterror)) == NULL) {
-
		assert((flags & LIBELF_F_RAWFILE_MALLOC) ||
-
		    (flags & LIBELF_F_RAWFILE_MMAP));
-
		if (flags & LIBELF_F_RAWFILE_MALLOC)
-
			free(m);
-
#if	ELFTC_HAVE_MMAP
-
		else
-
			(void) munmap(m, fsize);
-
#endif
-
		return (NULL);
-
	}
-

-
	/* ar(1) archives aren't supported in RDWR mode. */
-
	if (c == ELF_C_RDWR && e->e_kind == ELF_K_AR) {
-
		(void) elf_end(e);
-
		LIBELF_SET_ERROR(ARGUMENT, 0);
-
		return (NULL);
-
	}
-

-
	e->e_flags |= flags;
-
	e->e_fd = fd;
-
	e->e_cmd = c;
-

-
	return (e);
-
}
deleted external/libelf/libelf_phdr.c
@@ -1,155 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <gelf.h>
-
#include <libelf.h>
-
#include <stdlib.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
void *
-
_libelf_getphdr(Elf *e, int ec)
-
{
-
	size_t phnum;
-
	size_t fsz, msz;
-
	uint64_t phoff;
-
	Elf32_Ehdr *eh32;
-
	Elf64_Ehdr *eh64;
-
	void *ehdr, *phdr;
-
	int (*xlator)(unsigned char *_d, size_t _dsz, unsigned 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;
-
		phoff     = (uint64_t) eh32->e_phoff;
-
	} else {
-
		eh64      = (Elf64_Ehdr *) ehdr;
-
		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);
-
}
deleted external/libelf/libelf_shdr.c
@@ -1,56 +0,0 @@
-
/*-
-
 * 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>
-

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

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
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);
-
}
deleted external/libelf/libelf_xlate.c
@@ -1,150 +0,0 @@
-
/*-
-
 * 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>
-

-
#include <assert.h>
-
#include <libelf.h>
-

-
#include "_libelf.h"
-

-
ELFTC_VCSID("$Id$");
-

-
/*
-
 * 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 = (size_t) src->d_size / fsz;
-
		dsz = cnt * msz;
-
	} else {
-
		cnt = (size_t) 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 + (size_t) src->d_size;
-
	db = (uintptr_t) dst->d_buf;
-
	de = db + (size_t) 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);
-
}
deleted external/libelf/os.FreeBSD.mk
@@ -1,7 +0,0 @@
-
#
-
# Building for a FreeBSD target.
-
#
-
# $Id$
-

-
# Symbol versioning support [FreeBSD 7.X and later]
-
VERSION_MAP=		${.CURDIR}/Version.map
deleted external/libelf/os.NetBSD.mk
@@ -1,7 +0,0 @@
-
#
-
# Build recipes for NetBSD.
-
#
-
# $Id$
-
#
-

-
MKLINT=		no		# lint dies with a sigbus
deleted external/libfetch/Makefile.autosetup
@@ -1,22 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	fetch
-
SRCS=	common.c \
-
	fetch.c \
-
	http.c
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
-
		-I$(top_srcdir) \
-
		-I$(top_builddir) \
-
		-Wno-unused-parameter \
-
		-Wno-pointer-sign \
-
		-DWITH_SSL \
-
		-DINET6 \
-
		-DOPENSSL_API_COMPAT=0x10100000L
-

-
@if PKG_OPENSSL_CFLAGS
-
CFLAGS+=	@PKG_OPENSSL_CFLAGS@
-
@endif
-

-
VPATH=	$(top_srcdir)/external/libfetch
-

-
include $(MK)/static-lib.mk
added external/libfetch/Makefile.in
@@ -0,0 +1,22 @@
+
include @builddir@/mk/defs.mk
+
LIB=	fetch
+
SRCS=	common.c \
+
	fetch.c \
+
	http.c
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
+
		-I$(top_srcdir) \
+
		-I$(top_builddir) \
+
		-Wno-unused-parameter \
+
		-Wno-pointer-sign \
+
		-DWITH_SSL \
+
		-DINET6 \
+
		-DOPENSSL_API_COMPAT=0x10100000L
+

+
@if PKG_OPENSSL_CFLAGS
+
CFLAGS+=	@PKG_OPENSSL_CFLAGS@
+
@endif
+

+
VPATH=	$(top_srcdir)/external/fetch
+

+
include $(MK)/static-lib.mk
deleted external/libfetch/common.c
@@ -1,1782 +0,0 @@
-
/*-
-
 * SPDX-License-Identifier: BSD-3-Clause
-
 *
-
 * Copyright (c) 1998-2016 Dag-Erling Smørgrav
-
 * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
-
 * 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
-
 *    in this position and unchanged.
-
 * 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.
-
 * 3. The name of the author may not be used to endorse or promote products
-
 *    derived from this software without specific prior written permission
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "bsd_compat.h"
-

-
#include <sys/param.h>
-
#include <sys/socket.h>
-
#include <sys/time.h>
-
#include <sys/uio.h>
-

-
#include <netinet/in.h>
-

-
#include <ctype.h>
-
#include <errno.h>
-
#include <fcntl.h>
-
#include <inttypes.h>
-
#include <netdb.h>
-
#include <paths.h>
-
#include <poll.h>
-
#include <pwd.h>
-
#include <stdarg.h>
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <string.h>
-
#include <unistd.h>
-

-
#ifdef WITH_SSL
-
#include <openssl/x509v3.h>
-
#endif
-

-
#include "fetch.h"
-
#include "common.h"
-

-
#ifndef INFTIM
-
#define INFTIM (-1)
-
#endif
-

-
/*** Local data **************************************************************/
-

-
/*
-
 * Error messages for resolver errors
-
 */
-
static struct fetcherr netdb_errlist[] = {
-
#ifdef EAI_ADDRFAMILY
-
	{ EAI_ADDRFAMILY, FETCH_RESOLV, "Address family for host not supported" },
-
#endif
-
#ifdef EAI_NODATA
-
	{ EAI_NODATA,	FETCH_RESOLV,	"No address for host" },
-
#endif
-
	{ EAI_AGAIN,	FETCH_TEMP,	"Transient resolver failure" },
-
	{ EAI_FAIL,	FETCH_RESOLV,	"Non-recoverable resolver failure" },
-
	{ EAI_NONAME,	FETCH_RESOLV,	"Host does not resolve" },
-
	{ -1,		FETCH_UNKNOWN,	"Unknown resolver error" }
-
};
-

-
/*
-
 * SOCKS5 error enumerations
-
 */
-
enum SOCKS5_ERR {
-
/* Protocol errors */
-
	SOCKS5_ERR_SELECTION,
-
	SOCKS5_ERR_READ_METHOD,
-
	SOCKS5_ERR_VER5_ONLY,
-
	SOCKS5_ERR_NOMETHODS,
-
	SOCKS5_ERR_NOTIMPLEMENTED,
-
	SOCKS5_ERR_HOSTNAME_SIZE,
-
	SOCKS5_ERR_REQUEST,
-
	SOCKS5_ERR_REPLY,
-
	SOCKS5_ERR_NON_VER5_RESP,
-
	SOCKS5_ERR_GENERAL,
-
	SOCKS5_ERR_NOT_ALLOWED,
-
	SOCKS5_ERR_NET_UNREACHABLE,
-
	SOCKS5_ERR_HOST_UNREACHABLE,
-
	SOCKS5_ERR_CONN_REFUSED,
-
	SOCKS5_ERR_TTL_EXPIRED,
-
	SOCKS5_ERR_COM_UNSUPPORTED,
-
	SOCKS5_ERR_ADDR_UNSUPPORTED,
-
	SOCKS5_ERR_UNSPECIFIED,
-
/* Configuration errors */
-
	SOCKS5_ERR_BAD_HOST,
-
	SOCKS5_ERR_BAD_PROXY_FORMAT,
-
	SOCKS5_ERR_BAD_PORT
-
};
-

-
/*
-
 * Error messages for SOCKS5 errors
-
 */
-
static struct fetcherr socks5_errlist[] = {
-
/* SOCKS5 protocol errors */
-
	{ SOCKS5_ERR_SELECTION,		FETCH_ABORT,	"SOCKS5: Failed to send selection method" },
-
	{ SOCKS5_ERR_READ_METHOD,	FETCH_ABORT,	"SOCKS5: Failed to read method" },
-
	{ SOCKS5_ERR_VER5_ONLY,		FETCH_PROTO,	"SOCKS5: Only version 5 is implemented" },
-
	{ SOCKS5_ERR_NOMETHODS,		FETCH_PROTO,	"SOCKS5: No acceptable methods" },
-
	{ SOCKS5_ERR_NOTIMPLEMENTED,	FETCH_PROTO,	"SOCKS5: Method currently not implemented" },
-
	{ SOCKS5_ERR_HOSTNAME_SIZE,	FETCH_PROTO,	"SOCKS5: Hostname size is above 256 bytes" },
-
	{ SOCKS5_ERR_REQUEST,		FETCH_PROTO,	"SOCKS5: Failed to request" },
-
	{ SOCKS5_ERR_REPLY,		FETCH_PROTO,	"SOCKS5: Failed to receive reply" },
-
	{ SOCKS5_ERR_NON_VER5_RESP,	FETCH_PROTO,	"SOCKS5: Server responded with a non-version 5 response" },
-
	{ SOCKS5_ERR_GENERAL,		FETCH_ABORT,	"SOCKS5: General server failure" },
-
	{ SOCKS5_ERR_NOT_ALLOWED,	FETCH_AUTH,	"SOCKS5: Connection not allowed by ruleset" },
-
	{ SOCKS5_ERR_NET_UNREACHABLE,	FETCH_NETWORK,	"SOCKS5: Network unreachable" },
-
	{ SOCKS5_ERR_HOST_UNREACHABLE,	FETCH_ABORT,	"SOCKS5: Host unreachable" },
-
	{ SOCKS5_ERR_CONN_REFUSED,	FETCH_ABORT,	"SOCKS5: Connection refused" },
-
	{ SOCKS5_ERR_TTL_EXPIRED,	FETCH_TIMEOUT,	"SOCKS5: TTL expired" },
-
	{ SOCKS5_ERR_COM_UNSUPPORTED,	FETCH_PROTO,	"SOCKS5: Command not supported" },
-
	{ SOCKS5_ERR_ADDR_UNSUPPORTED,	FETCH_ABORT,	"SOCKS5: Address type not supported" },
-
	{ SOCKS5_ERR_UNSPECIFIED,	FETCH_UNKNOWN,	"SOCKS5: Unspecified error" },
-
/* Configuration error */
-
	{ SOCKS5_ERR_BAD_HOST,		FETCH_ABORT,	"SOCKS5: Bad proxy host" },
-
	{ SOCKS5_ERR_BAD_PROXY_FORMAT,	FETCH_ABORT,	"SOCKS5: Bad proxy format" },
-
	{ SOCKS5_ERR_BAD_PORT,		FETCH_ABORT,	"SOCKS5: Bad port" }
-
};
-

-
/* End-of-Line */
-
static const char ENDL[2] = { '\r', '\n' };
-

-

-
/*** Error-reporting functions ***********************************************/
-

-
/*
-
 * Map error code to string
-
 */
-
static struct fetcherr *
-
fetch_finderr(struct fetcherr *p, int e)
-
{
-
	while (p->num != -1 && p->num != e)
-
		p++;
-
	return (p);
-
}
-

-
/*
-
 * Set error code
-
 */
-
void
-
fetch_seterr(struct fetcherr *p, int e)
-
{
-
	p = fetch_finderr(p, e);
-
	fetchLastErrCode = p->cat;
-
	snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
-
}
-

-
/*
-
 * Set error code according to errno
-
 */
-
void
-
fetch_syserr(void)
-
{
-
	switch (errno) {
-
	case 0:
-
		fetchLastErrCode = FETCH_OK;
-
		break;
-
	case EPERM:
-
	case EACCES:
-
	case EROFS:
-
	case EAUTH:
-
	case ENEEDAUTH:
-
		fetchLastErrCode = FETCH_AUTH;
-
		break;
-
	case ENOENT:
-
	case EISDIR: /* XXX */
-
		fetchLastErrCode = FETCH_UNAVAIL;
-
		break;
-
	case ENOMEM:
-
		fetchLastErrCode = FETCH_MEMORY;
-
		break;
-
	case EBUSY:
-
	case EAGAIN:
-
		fetchLastErrCode = FETCH_TEMP;
-
		break;
-
	case EEXIST:
-
		fetchLastErrCode = FETCH_EXISTS;
-
		break;
-
	case ENOSPC:
-
		fetchLastErrCode = FETCH_FULL;
-
		break;
-
	case EADDRINUSE:
-
	case EADDRNOTAVAIL:
-
	case ENETDOWN:
-
	case ENETUNREACH:
-
	case ENETRESET:
-
	case EHOSTUNREACH:
-
		fetchLastErrCode = FETCH_NETWORK;
-
		break;
-
	case ECONNABORTED:
-
	case ECONNRESET:
-
		fetchLastErrCode = FETCH_ABORT;
-
		break;
-
	case ETIMEDOUT:
-
		fetchLastErrCode = FETCH_TIMEOUT;
-
		break;
-
	case ECONNREFUSED:
-
	case EHOSTDOWN:
-
		fetchLastErrCode = FETCH_DOWN;
-
		break;
-
	default:
-
		fetchLastErrCode = FETCH_UNKNOWN;
-
	}
-
	snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
-
}
-

-

-
/*
-
 * Emit status message
-
 */
-
void
-
fetch_info(const char *fmt, ...)
-
{
-
	va_list ap;
-
	int serrno = errno;
-

-
	va_start(ap, fmt);
-
	vfprintf(stderr, fmt, ap);
-
	va_end(ap);
-
	fputc('\n', stderr);
-
	errno = serrno;
-
}
-
#define fetch_verbose(...)						\
-
	do { if (verbose) fetch_info(__VA_ARGS__); } while (0)
-

-

-
/*** Network-related utility functions ***************************************/
-

-
/*
-
 * Return the default port for a scheme
-
 */
-
int
-
fetch_default_port(const char *scheme)
-
{
-
	struct servent *se;
-

-
	if ((se = getservbyname(scheme, "tcp")) != NULL)
-
		return (ntohs(se->s_port));
-
	if (strcmp(scheme, SCHEME_FTP) == 0)
-
		return (FTP_DEFAULT_PORT);
-
	if (strcmp(scheme, SCHEME_HTTP) == 0)
-
		return (HTTP_DEFAULT_PORT);
-
	return (0);
-
}
-

-
/*
-
 * Return the default proxy port for a scheme
-
 */
-
int
-
fetch_default_proxy_port(const char *scheme)
-
{
-
	if (strcmp(scheme, SCHEME_FTP) == 0)
-
		return (FTP_DEFAULT_PROXY_PORT);
-
	if (strcmp(scheme, SCHEME_HTTP) == 0)
-
		return (HTTP_DEFAULT_PROXY_PORT);
-
	return (0);
-
}
-

-

-
/*
-
 * Create a connection for an existing descriptor.
-
 */
-
conn_t *
-
fetch_reopen(int sd)
-
{
-
	conn_t *conn;
-
	int flags;
-
#ifdef SO_NOSIGPIPE
-
	int opt = 1;
-
#endif
-

-
	/* allocate and fill connection structure */
-
	if ((conn = calloc(1, sizeof(*conn))) == NULL)
-
		return (NULL);
-
	flags = fcntl(sd, F_GETFD);
-
	if (flags != -1 && (flags & FD_CLOEXEC) == 0)
-
		(void)fcntl(sd, F_SETFD, flags | FD_CLOEXEC);
-
	flags = fcntl(sd, F_GETFL);
-
	if (flags != -1 && (flags & O_NONBLOCK) == 0)
-
		(void)fcntl(sd, F_SETFL, flags | O_NONBLOCK);
-
#ifdef SO_NOSIGPIPE
-
	(void)setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
-
#endif
-
	conn->sd = sd;
-
	++conn->ref;
-
	return (conn);
-
}
-

-

-
/*
-
 * Bump a connection's reference count.
-
 */
-
conn_t *
-
fetch_ref(conn_t *conn)
-
{
-
	++conn->ref;
-
	return (conn);
-
}
-

-

-
/*
-
 * Resolve an address
-
 */
-
struct addrinfo *
-
fetch_resolve(const char *addr, int port, int af)
-
{
-
	char hbuf[256], sbuf[8];
-
	struct addrinfo hints, *res;
-
	const char *hb, *he, *sep;
-
	const char *host, *service;
-
	int err, len;
-

-
	/* first, check for a bracketed IPv6 address */
-
	if (*addr == '[') {
-
		hb = addr + 1;
-
		if ((sep = strchr(hb, ']')) == NULL) {
-
			errno = EINVAL;
-
			goto syserr;
-
		}
-
		he = sep++;
-
	} else {
-
		hb = addr;
-
		sep = strchrnul(hb, ':');
-
		he = sep;
-
	}
-

-
	/* see if we need to copy the host name */
-
	if (*he != '\0') {
-
		len = snprintf(hbuf, sizeof(hbuf),
-
		    "%.*s", (int)(he - hb), hb);
-
		if (len < 0)
-
			goto syserr;
-
		if (len >= (int)sizeof(hbuf)) {
-
			errno = ENAMETOOLONG;
-
			goto syserr;
-
		}
-
		host = hbuf;
-
	} else {
-
		host = hb;
-
	}
-

-
	/* was it followed by a service name? */
-
	if (*sep == '\0' && port != 0) {
-
		if (port < 1 || port > 65535) {
-
			errno = EINVAL;
-
			goto syserr;
-
		}
-
		if (snprintf(sbuf, sizeof(sbuf), "%d", port) < 0)
-
			goto syserr;
-
		service = sbuf;
-
	} else if (*sep != '\0') {
-
		service = sep + 1;
-
	} else {
-
		service = NULL;
-
	}
-

-
	/* resolve */
-
	memset(&hints, 0, sizeof(hints));
-
	hints.ai_family = af;
-
	hints.ai_socktype = SOCK_STREAM;
-
	hints.ai_flags = AI_ADDRCONFIG;
-
	if ((err = getaddrinfo(host, service, &hints, &res)) != 0) {
-
		netdb_seterr(err);
-
		return (NULL);
-
	}
-
	return (res);
-
syserr:
-
	fetch_syserr();
-
	return (NULL);
-
}
-

-

-
/*
-
 * Bind a socket to a specific local address
-
 */
-
int
-
fetch_bind(int sd, int af, const char *addr)
-
{
-
	struct addrinfo *cliai, *ai;
-
	int err;
-

-
	if ((cliai = fetch_resolve(addr, 0, af)) == NULL)
-
		return (-1);
-
	for (ai = cliai; ai != NULL; ai = ai->ai_next)
-
		if ((err = bind(sd, ai->ai_addr, ai->ai_addrlen)) == 0)
-
			break;
-
	if (err != 0)
-
		fetch_syserr();
-
	freeaddrinfo(cliai);
-
	return (err == 0 ? 0 : -1);
-
}
-

-

-
/*
-
 * SOCKS5 connection initiation, based on RFC 1928
-
 * Default DNS resolution over SOCKS5
-
 */
-
int
-
fetch_socks5_init(conn_t *conn, const char *host, int port, int verbose)
-
{
-
	/*
-
	 * Size is based on largest packet prefix (4 bytes) +
-
	 * Largest FQDN (256) + one byte size (1) +
-
	 * Port (2)
-
	 */
-
	unsigned char buf[BUFF_SIZE];
-
	unsigned char *ptr;
-
	int ret = 1;
-

-
	fetch_verbose("Initializing SOCKS5 connection: %s:%d", host, port);
-

-
	/* Connection initialization */
-
	ptr = buf;
-
	*ptr++ = SOCKS_VERSION_5;
-
	*ptr++ = SOCKS_CONNECTION;
-
	*ptr++ = SOCKS_RSV;
-

-
	if (fetch_write(conn, buf, 3) != 3) {
-
		ret = SOCKS5_ERR_SELECTION;
-
		goto fail;
-
	}
-

-
	/* Verify response from SOCKS5 server */
-
	if (fetch_read(conn, buf, 2) != 2) {
-
		ret = SOCKS5_ERR_READ_METHOD;
-
		goto fail;
-
	}
-

-
	ptr = buf;
-
	if (ptr[0] != SOCKS_VERSION_5) {
-
		ret = SOCKS5_ERR_VER5_ONLY;
-
		goto fail;
-
	}
-
	if (ptr[1] == SOCKS_NOMETHODS) {
-
		ret = SOCKS5_ERR_NOMETHODS;
-
		goto fail;
-
	}
-
	else if (ptr[1] != SOCKS5_NOTIMPLEMENTED) {
-
		ret = SOCKS5_ERR_NOTIMPLEMENTED;
-
		goto fail;
-
	}
-

-
	/* Send Request */
-
	*ptr++ = SOCKS_VERSION_5;
-
	*ptr++ = SOCKS_CONNECTION;
-
	*ptr++ = SOCKS_RSV;
-
	/* Encode all targets as a hostname to avoid DNS leaks */
-
	*ptr++ = SOCKS_ATYP_DOMAINNAME;
-
	if (strlen(host) > FQDN_SIZE) {
-
		ret = SOCKS5_ERR_HOSTNAME_SIZE;
-
		goto fail;
-
	}
-
	*ptr++ = strlen(host);
-
	memcpy(ptr, host, strlen(host));
-
	ptr = ptr + strlen(host);
-

-
	port = htons(port);
-
	*ptr++ = port & 0x00ff;
-
	*ptr++ = (port & 0xff00) >> 8;
-

-
	if (fetch_write(conn, buf, ptr - buf) != ptr - buf) {
-
		ret = SOCKS5_ERR_REQUEST;
-
		goto fail;
-
	}
-

-
	/* BND.ADDR is variable length, read the largest on non-blocking socket */
-
	if (!fetch_read(conn, buf, BUFF_SIZE)) {
-
		ret = SOCKS5_ERR_REPLY;
-
		goto fail;
-
	}
-

-
	ptr = buf;
-
	if (*ptr++ != SOCKS_VERSION_5) {
-
		ret = SOCKS5_ERR_NON_VER5_RESP;
-
		goto fail;
-
	}
-

-
	switch (*ptr++) {
-
	case SOCKS_SUCCESS:
-
		break;
-
	case SOCKS_GENERAL_FAILURE:
-
		ret = SOCKS5_ERR_GENERAL;
-
		goto fail;
-
	case SOCKS_CONNECTION_NOT_ALLOWED:
-
		ret = SOCKS5_ERR_NOT_ALLOWED;
-
		goto fail;
-
	case SOCKS_NETWORK_UNREACHABLE:
-
		ret = SOCKS5_ERR_NET_UNREACHABLE;
-
		goto fail;
-
	case SOCKS_HOST_UNREACHABLE:
-
		ret = SOCKS5_ERR_HOST_UNREACHABLE;
-
		goto fail;
-
	case SOCKS_CONNECTION_REFUSED:
-
		ret = SOCKS5_ERR_CONN_REFUSED;
-
		goto fail;
-
	case SOCKS_TTL_EXPIRED:
-
		ret = SOCKS5_ERR_TTL_EXPIRED;
-
		goto fail;
-
	case SOCKS_COMMAND_NOT_SUPPORTED:
-
		ret = SOCKS5_ERR_COM_UNSUPPORTED;
-
		goto fail;
-
	case SOCKS_ADDRESS_NOT_SUPPORTED:
-
		ret = SOCKS5_ERR_ADDR_UNSUPPORTED;
-
		goto fail;
-
	default:
-
		ret = SOCKS5_ERR_UNSPECIFIED;
-
		goto fail;
-
	}
-

-
	return (ret);
-

-
fail:
-
	socks5_seterr(ret);
-
	return (0);
-
}
-

-
/*
-
 * Perform SOCKS5 initialization
-
 */
-
int
-
fetch_socks5_getenv(char **host, int *port)
-
{
-
	char *socks5env, *endptr, *ext;
-
	const char *portDelim;
-
	size_t slen;
-

-
	portDelim = ":";
-
	if ((socks5env = getenv("SOCKS5_PROXY")) == NULL || *socks5env == '\0') {
-
		*host = NULL;
-
		*port = -1;
-
		return (-1);
-
	}
-

-
	/*
-
	 * IPv6 addresses begin and end in brackets.  Set the port delimiter
-
	 * accordingly and search for it so we can do appropriate validation.
-
	 */
-
	if (socks5env[0] == '[')
-
		portDelim = "]:";
-

-
	slen = strlen(socks5env);
-
	ext = strstr(socks5env, portDelim);
-
	if (socks5env[0] == '[') {
-
		if (socks5env[slen - 1] == ']') {
-
			*host = strndup(socks5env, slen);
-
		} else if (ext != NULL) {
-
			*host = strndup(socks5env, ext - socks5env + 1);
-
		} else {
-
			socks5_seterr(SOCKS5_ERR_BAD_PROXY_FORMAT);
-
			return (0);
-
		}
-
	} else {
-
		*host = strndup(socks5env, ext - socks5env);
-
	}
-

-
	if (*host == NULL)
-
		return (-1);
-
	if (ext == NULL) {
-
		*port = 1080; /* Default port as defined in RFC1928 */
-
	} else {
-
		ext += strlen(portDelim);
-
		errno = 0;
-
		*port = strtoimax(ext, (char **)&endptr, 10);
-
		if (*endptr != '\0' || errno != 0 || *port < 0 ||
-
		    *port > 65535) {
-
			free(*host);
-
			*host = NULL;
-
			socks5_seterr(SOCKS5_ERR_BAD_PORT);
-
			return (0);
-
		}
-
	}
-

-
	return (2);
-
}
-

-

-
/*
-
 * Establish a TCP connection to the specified port on the specified host.
-
 */
-
conn_t *
-
fetch_connect(const char *host, int port, int af, int verbose)
-
{
-
	struct addrinfo *cais = NULL, *sais = NULL, *cai, *sai;
-
	const char *bindaddr;
-
	conn_t *conn = NULL;
-
	int err = 0, sd = -1;
-
	char *sockshost;
-
	int socksport;
-

-
	DEBUGF("---> %s:%d\n", host, port);
-

-
	/*
-
	 * Check if SOCKS5_PROXY env variable is set.  fetch_socks5_getenv
-
	 * will either set sockshost = NULL or allocate memory in all cases.
-
	 */
-
	sockshost = NULL;
-
	if (!fetch_socks5_getenv(&sockshost, &socksport))
-
		goto fail;
-

-
	/* Not using SOCKS5 proxy */
-
	if (sockshost == NULL) {
-
		/* resolve server address */
-
		fetch_verbose("resolving server address: %s:%d", host, port);
-
		if ((sais = fetch_resolve(host, port, af)) == NULL)
-
			goto fail;
-

-
		/* resolve client address */
-
		bindaddr = getenv("FETCH_BIND_ADDRESS");
-
		if (bindaddr != NULL && *bindaddr != '\0') {
-
			fetch_verbose("resolving client address: %s", bindaddr);
-
			if ((cais = fetch_resolve(bindaddr, 0, af)) == NULL)
-
				goto fail;
-
		}
-
	} else {
-
		/* resolve socks5 proxy address */
-
		fetch_verbose("resolving SOCKS5 server address: %s:%d",
-
		    sockshost, socksport);
-
		if ((sais = fetch_resolve(sockshost, socksport, af)) == NULL) {
-
			socks5_seterr(SOCKS5_ERR_BAD_HOST);
-
			goto fail;
-
		}
-
	}
-

-
	/* try each server address in turn */
-
	for (err = 0, sai = sais; sai != NULL; sai = sai->ai_next) {
-
		/* open socket */
-
		if ((sd = socket(sai->ai_family, SOCK_STREAM, 0)) < 0) {
-
			err = -1;
-
			if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
-
				continue;
-
			goto syserr;
-
		}
-
		/* attempt to bind to client address */
-
		for (err = 0, cai = cais; cai != NULL; cai = cai->ai_next) {
-
			if (cai->ai_family != sai->ai_family)
-
				continue;
-
			if ((err = bind(sd, cai->ai_addr, cai->ai_addrlen)) == 0)
-
				break;
-
		}
-
		if (err != 0) {
-
			fetch_verbose("failed to bind to %s", bindaddr);
-
			goto syserr;
-
		}
-
		/* attempt to connect to server address */
-
		while ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) < 0) {
-
			if (errno == EINTR && fetchRestartCalls)
-
				continue;
-
			break;
-
		}
-
		/* success? */
-
		if (err == 0)
-
			break;
-
		/* clean up before next attempt */
-
		close(sd);
-
		sd = -1;
-
	}
-
	if (err != 0) {
-
		if (verbose && sockshost == NULL) {
-
			fetch_info("failed to connect to %s:%d", host, port);
-
			goto syserr;
-
		} else if (sockshost != NULL) {
-
			fetch_verbose("failed to connect to SOCKS5 server %s:%d",
-
			    sockshost, socksport);
-
			socks5_seterr(SOCKS5_ERR_CONN_REFUSED);
-
			goto fail;
-
		}
-
		goto syserr;
-
	}
-

-
	if ((conn = fetch_reopen(sd)) == NULL)
-
		goto syserr;
-

-
	if (sockshost)
-
		if (!fetch_socks5_init(conn, host, port, verbose))
-
			goto fail;
-
	free(sockshost);
-
	if (cais != NULL)
-
		freeaddrinfo(cais);
-
	if (sais != NULL)
-
		freeaddrinfo(sais);
-
	return (conn);
-
syserr:
-
	fetch_syserr();
-
fail:
-
	free(sockshost);
-
	/* Fully close if it was opened; otherwise just don't leak the fd. */
-
	if (conn != NULL)
-
		fetch_close(conn);
-
	else if (sd >= 0)
-
		close(sd);
-
	if (cais != NULL)
-
		freeaddrinfo(cais);
-
	if (sais != NULL)
-
		freeaddrinfo(sais);
-
	return (NULL);
-
}
-

-
#ifdef WITH_SSL
-
/*
-
 * Convert characters A-Z to lowercase (intentionally avoid any locale
-
 * specific conversions).
-
 */
-
static char
-
fetch_ssl_tolower(char in)
-
{
-
	if (in >= 'A' && in <= 'Z')
-
		return (in + 32);
-
	else
-
		return (in);
-
}
-

-
/*
-
 * isalpha implementation that intentionally avoids any locale specific
-
 * conversions.
-
 */
-
static int
-
fetch_ssl_isalpha(char in)
-
{
-
	return ((in >= 'A' && in <= 'Z') || (in >= 'a' && in <= 'z'));
-
}
-

-
/*
-
 * Check if passed hostnames a and b are equal.
-
 */
-
static int
-
fetch_ssl_hname_equal(const char *a, size_t alen, const char *b,
-
    size_t blen)
-
{
-
	size_t i;
-

-
	if (alen != blen)
-
		return (0);
-
	for (i = 0; i < alen; ++i) {
-
		if (fetch_ssl_tolower(a[i]) != fetch_ssl_tolower(b[i]))
-
			return (0);
-
	}
-
	return (1);
-
}
-

-
/*
-
 * Check if domain label is traditional, meaning that only A-Z, a-z, 0-9
-
 * and '-' (hyphen) are allowed. Hyphens have to be surrounded by alpha-
-
 * numeric characters. Double hyphens (like they're found in IDN a-labels
-
 * 'xn--') are not allowed. Empty labels are invalid.
-
 */
-
static int
-
fetch_ssl_is_trad_domain_label(const char *l, size_t len, int wcok)
-
{
-
	size_t i;
-

-
	if (!len || l[0] == '-' || l[len-1] == '-')
-
		return (0);
-
	for (i = 0; i < len; ++i) {
-
		if (!isdigit(l[i]) &&
-
		    !fetch_ssl_isalpha(l[i]) &&
-
		    !(l[i] == '*' && wcok) &&
-
		    !(l[i] == '-' && l[i - 1] != '-'))
-
			return (0);
-
	}
-
	return (1);
-
}
-

-
/*
-
 * Check if host name consists only of numbers. This might indicate an IP
-
 * address, which is not a good idea for CN wildcard comparison.
-
 */
-
static int
-
fetch_ssl_hname_is_only_numbers(const char *hostname, size_t len)
-
{
-
	size_t i;
-

-
	for (i = 0; i < len; ++i) {
-
		if (!((hostname[i] >= '0' && hostname[i] <= '9') ||
-
		    hostname[i] == '.'))
-
			return (0);
-
	}
-
	return (1);
-
}
-

-
/*
-
 * Check if the host name h passed matches the pattern passed in m which
-
 * is usually part of subjectAltName or CN of a certificate presented to
-
 * the client. This includes wildcard matching. The algorithm is based on
-
 * RFC6125, sections 6.4.3 and 7.2, which clarifies RFC2818 and RFC3280.
-
 */
-
static int
-
fetch_ssl_hname_match(const char *h, size_t hlen, const char *m,
-
    size_t mlen)
-
{
-
	int delta, hdotidx, mdot1idx, wcidx;
-
	const char *hdot, *mdot1, *mdot2;
-
	const char *wc; /* wildcard */
-

-
	if (!(h && *h && m && *m))
-
		return (0);
-
	if ((wc = strnstr(m, "*", mlen)) == NULL)
-
		return (fetch_ssl_hname_equal(h, hlen, m, mlen));
-
	wcidx = wc - m;
-
	/* hostname should not be just dots and numbers */
-
	if (fetch_ssl_hname_is_only_numbers(h, hlen))
-
		return (0);
-
	/* only one wildcard allowed in pattern */
-
	if (strnstr(wc + 1, "*", mlen - wcidx - 1) != NULL)
-
		return (0);
-
	/*
-
	 * there must be at least two more domain labels and
-
	 * wildcard has to be in the leftmost label (RFC6125)
-
	 */
-
	mdot1 = strnstr(m, ".", mlen);
-
	if (mdot1 == NULL || mdot1 < wc || (mlen - (mdot1 - m)) < 4)
-
		return (0);
-
	mdot1idx = mdot1 - m;
-
	mdot2 = strnstr(mdot1 + 1, ".", mlen - mdot1idx - 1);
-
	if (mdot2 == NULL || (mlen - (mdot2 - m)) < 2)
-
		return (0);
-
	/* hostname must contain a dot and not be the 1st char */
-
	hdot = strnstr(h, ".", hlen);
-
	if (hdot == NULL || hdot == h)
-
		return (0);
-
	hdotidx = hdot - h;
-
	/*
-
	 * host part of hostname must be at least as long as
-
	 * pattern it's supposed to match
-
	 */
-
	if (hdotidx < mdot1idx)
-
		return (0);
-
	/*
-
	 * don't allow wildcards in non-traditional domain names
-
	 * (IDN, A-label, U-label...)
-
	 */
-
	if (!fetch_ssl_is_trad_domain_label(h, hdotidx, 0) ||
-
	    !fetch_ssl_is_trad_domain_label(m, mdot1idx, 1))
-
		return (0);
-
	/* match domain part (part after first dot) */
-
	if (!fetch_ssl_hname_equal(hdot, hlen - hdotidx, mdot1,
-
	    mlen - mdot1idx))
-
		return (0);
-
	/* match part left of wildcard */
-
	if (!fetch_ssl_hname_equal(h, wcidx, m, wcidx))
-
		return (0);
-
	/* match part right of wildcard */
-
	delta = mdot1idx - wcidx - 1;
-
	if (!fetch_ssl_hname_equal(hdot - delta, delta,
-
	    mdot1 - delta, delta))
-
		return (0);
-
	/* all tests succeeded, it's a match */
-
	return (1);
-
}
-

-
/*
-
 * Get numeric host address info - returns NULL if host was not an IP
-
 * address. The caller is responsible for deallocation using
-
 * freeaddrinfo(3).
-
 */
-
static struct addrinfo *
-
fetch_ssl_get_numeric_addrinfo(const char *hostname, size_t len)
-
{
-
	struct addrinfo hints, *res;
-
	char *host;
-

-
	host = (char *)malloc(len + 1);
-
	memcpy(host, hostname, len);
-
	host[len] = '\0';
-
	memset(&hints, 0, sizeof(hints));
-
	hints.ai_family = PF_UNSPEC;
-
	hints.ai_socktype = SOCK_STREAM;
-
	hints.ai_protocol = 0;
-
	hints.ai_flags = AI_NUMERICHOST;
-
	/* port is not relevant for this purpose */
-
	if (getaddrinfo(host, "443", &hints, &res) != 0)
-
		res = NULL;
-
	free(host);
-
	return res;
-
}
-

-
/*
-
 * Compare ip address in addrinfo with address passes.
-
 */
-
static int
-
fetch_ssl_ipaddr_match_bin(const struct addrinfo *lhost, const char *rhost,
-
    size_t rhostlen)
-
{
-
	const void *left;
-

-
	if (lhost->ai_family == AF_INET && rhostlen == 4) {
-
		left = (void *)&((struct sockaddr_in*)(void *)
-
		    lhost->ai_addr)->sin_addr.s_addr;
-
#ifdef INET6
-
	} else if (lhost->ai_family == AF_INET6 && rhostlen == 16) {
-
		left = (void *)&((struct sockaddr_in6 *)(void *)
-
		    lhost->ai_addr)->sin6_addr;
-
#endif
-
	} else
-
		return (0);
-
	return (!memcmp(left, (const void *)rhost, rhostlen) ? 1 : 0);
-
}
-

-
/*
-
 * Compare ip address in addrinfo with host passed. If host is not an IP
-
 * address, comparison will fail.
-
 */
-
static int
-
fetch_ssl_ipaddr_match(const struct addrinfo *laddr, const char *r,
-
    size_t rlen)
-
{
-
	struct addrinfo *raddr;
-
	int ret;
-
	char *rip;
-

-
	ret = 0;
-
	if ((raddr = fetch_ssl_get_numeric_addrinfo(r, rlen)) == NULL)
-
		return 0; /* not a numeric host */
-

-
	if (laddr->ai_family == raddr->ai_family) {
-
		if (laddr->ai_family == AF_INET) {
-
			rip = (char *)&((struct sockaddr_in *)(void *)
-
			    raddr->ai_addr)->sin_addr.s_addr;
-
			ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 4);
-
#ifdef INET6
-
		} else if (laddr->ai_family == AF_INET6) {
-
			rip = (char *)&((struct sockaddr_in6 *)(void *)
-
			    raddr->ai_addr)->sin6_addr;
-
			ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 16);
-
#endif
-
		}
-

-
	}
-
	freeaddrinfo(raddr);
-
	return (ret);
-
}
-

-
/*
-
 * Verify server certificate by subjectAltName.
-
 */
-
static int
-
fetch_ssl_verify_altname(STACK_OF(GENERAL_NAME) *altnames,
-
    const char *host, struct addrinfo *ip)
-
{
-
	const GENERAL_NAME *name;
-
	size_t nslen;
-
	int i;
-
	const char *ns;
-

-
	for (i = 0; i < sk_GENERAL_NAME_num(altnames); ++i) {
-
		name = sk_GENERAL_NAME_value(altnames, i);
-
		ns = (const char *)ASN1_STRING_get0_data(name->d.ia5);
-
		nslen = (size_t)ASN1_STRING_length(name->d.ia5);
-

-
		if (name->type == GEN_DNS && ip == NULL &&
-
		    fetch_ssl_hname_match(host, strlen(host), ns, nslen))
-
			return (1);
-
		else if (name->type == GEN_IPADD && ip != NULL &&
-
		    fetch_ssl_ipaddr_match_bin(ip, ns, nslen))
-
			return (1);
-
	}
-
	return (0);
-
}
-

-
/*
-
 * Verify server certificate by CN.
-
 */
-
static int
-
fetch_ssl_verify_cn(X509_NAME *subject, const char *host,
-
    struct addrinfo *ip)
-
{
-
	ASN1_STRING *namedata;
-
	X509_NAME_ENTRY *nameentry;
-
	int cnlen, lastpos, loc, ret;
-
	unsigned char *cn;
-

-
	ret = 0;
-
	lastpos = -1;
-
	loc = -1;
-
	cn = NULL;
-
	/* get most specific CN (last entry in list) and compare */
-
	while ((lastpos = X509_NAME_get_index_by_NID(subject,
-
	    NID_commonName, lastpos)) != -1)
-
		loc = lastpos;
-

-
	if (loc > -1) {
-
		nameentry = X509_NAME_get_entry(subject, loc);
-
		namedata = X509_NAME_ENTRY_get_data(nameentry);
-
		cnlen = ASN1_STRING_to_UTF8(&cn, namedata);
-
		if (ip == NULL &&
-
		    fetch_ssl_hname_match(host, strlen(host), cn, cnlen))
-
			ret = 1;
-
		else if (ip != NULL && fetch_ssl_ipaddr_match(ip, cn, cnlen))
-
			ret = 1;
-
		OPENSSL_free(cn);
-
	}
-
	return (ret);
-
}
-

-
/*
-
 * Verify that server certificate subjectAltName/CN matches
-
 * hostname. First check, if there are alternative subject names. If yes,
-
 * those have to match. Only if those don't exist it falls back to
-
 * checking the subject's CN.
-
 */
-
static int
-
fetch_ssl_verify_hname(X509 *cert, const char *host)
-
{
-
	struct addrinfo *ip;
-
	STACK_OF(GENERAL_NAME) *altnames;
-
	X509_NAME *subject;
-
	int ret;
-

-
	ret = 0;
-
	ip = fetch_ssl_get_numeric_addrinfo(host, strlen(host));
-
	altnames = X509_get_ext_d2i(cert, NID_subject_alt_name,
-
	    NULL, NULL);
-

-
	if (altnames != NULL) {
-
		ret = fetch_ssl_verify_altname(altnames, host, ip);
-
	} else {
-
		subject = X509_get_subject_name(cert);
-
		if (subject != NULL)
-
			ret = fetch_ssl_verify_cn(subject, host, ip);
-
	}
-

-
	if (ip != NULL)
-
		freeaddrinfo(ip);
-
	if (altnames != NULL)
-
		GENERAL_NAMES_free(altnames);
-
	return (ret);
-
}
-

-
/*
-
 * Configure transport security layer based on environment.
-
 */
-
static void
-
fetch_ssl_setup_transport_layer(SSL_CTX *ctx, int verbose)
-
{
-
	long ssl_ctx_options;
-

-
	ssl_ctx_options = SSL_OP_ALL | SSL_OP_NO_SSLv3 | SSL_OP_NO_TICKET;
-
	if (getenv("SSL_NO_TLS1") != NULL)
-
		ssl_ctx_options |= SSL_OP_NO_TLSv1;
-
	if (getenv("SSL_NO_TLS1_1") != NULL)
-
		ssl_ctx_options |= SSL_OP_NO_TLSv1_1;
-
	if (getenv("SSL_NO_TLS1_2") != NULL)
-
		ssl_ctx_options |= SSL_OP_NO_TLSv1_2;
-
	if (getenv("SSL_NO_TLS1_3") != NULL)
-
		ssl_ctx_options |= SSL_OP_NO_TLSv1_3;
-
	fetch_verbose("SSL options: %lx", ssl_ctx_options);
-
	SSL_CTX_set_options(ctx, ssl_ctx_options);
-
}
-

-

-
/*
-
 * Configure peer verification based on environment.
-
 */
-
static int
-
fetch_ssl_setup_peer_verification(SSL_CTX *ctx, int verbose)
-
{
-
	X509_LOOKUP *crl_lookup;
-
	X509_STORE *crl_store;
-
	const char *ca_cert_file, *ca_cert_path, *crl_file;
-

-
	if (getenv("SSL_NO_VERIFY_PEER") == NULL) {
-
		ca_cert_file = getenv("SSL_CA_CERT_FILE");
-
		ca_cert_path = getenv("SSL_CA_CERT_PATH");
-
		if (verbose) {
-
			fetch_info("Peer verification enabled");
-
			if (ca_cert_file != NULL)
-
				fetch_info("Using CA cert file: %s",
-
				    ca_cert_file);
-
			if (ca_cert_path != NULL)
-
				fetch_info("Using CA cert path: %s",
-
				    ca_cert_path);
-
			if (ca_cert_file == NULL && ca_cert_path == NULL)
-
				fetch_info("Using OpenSSL default "
-
				    "CA cert file and path");
-
		}
-
		SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER,
-
		    fetch_ssl_cb_verify_crt);
-
		if (ca_cert_file != NULL || ca_cert_path != NULL)
-
			SSL_CTX_load_verify_locations(ctx, ca_cert_file,
-
			    ca_cert_path);
-
		else
-
			SSL_CTX_set_default_verify_paths(ctx);
-
		if ((crl_file = getenv("SSL_CRL_FILE")) != NULL) {
-
			fetch_verbose("Using CRL file: %s", crl_file);
-
			crl_store = SSL_CTX_get_cert_store(ctx);
-
			crl_lookup = X509_STORE_add_lookup(crl_store,
-
			    X509_LOOKUP_file());
-
			if (crl_lookup == NULL ||
-
			    !X509_load_crl_file(crl_lookup, crl_file,
-
				X509_FILETYPE_PEM)) {
-
				fetch_info("Could not load CRL file %s",
-
				    crl_file);
-
				return (0);
-
			}
-
			X509_STORE_set_flags(crl_store,
-
			    X509_V_FLAG_CRL_CHECK |
-
			    X509_V_FLAG_CRL_CHECK_ALL);
-
		}
-
	}
-
	return (1);
-
}
-

-
/*
-
 * Configure client certificate based on environment.
-
 */
-
static int
-
fetch_ssl_setup_client_certificate(SSL_CTX *ctx, int verbose)
-
{
-
	const char *client_cert_file, *client_key_file;
-

-
	if ((client_cert_file = getenv("SSL_CLIENT_CERT_FILE")) != NULL) {
-
		client_key_file = getenv("SSL_CLIENT_KEY_FILE") != NULL ?
-
		    getenv("SSL_CLIENT_KEY_FILE") : client_cert_file;
-
		fetch_verbose("Using client cert file: %s", client_cert_file);
-
		fetch_verbose("Using client key file: %s", client_key_file);
-
		if (SSL_CTX_use_certificate_chain_file(ctx,
-
			client_cert_file) != 1) {
-
			fetch_info("Could not load client certificate %s",
-
			    client_cert_file);
-
			return (0);
-
		}
-
		if (SSL_CTX_use_PrivateKey_file(ctx, client_key_file,
-
			SSL_FILETYPE_PEM) != 1) {
-
			fetch_info("Could not load client key %s",
-
			    client_key_file);
-
			return (0);
-
		}
-
	}
-
	return (1);
-
}
-

-
/*
-
 * Callback for SSL certificate verification, this is called on server
-
 * cert verification. It takes no decision, but informs the user in case
-
 * verification failed.
-
 */
-
int
-
fetch_ssl_cb_verify_crt(int verified, X509_STORE_CTX *ctx)
-
{
-
	X509 *crt;
-
	X509_NAME *name;
-
	char *str;
-

-
	str = NULL;
-
	if (!verified) {
-
		if ((crt = X509_STORE_CTX_get_current_cert(ctx)) != NULL &&
-
		    (name = X509_get_subject_name(crt)) != NULL)
-
			str = X509_NAME_oneline(name, 0, 0);
-
		fetch_info("Certificate verification failed for %s",
-
		    str != NULL ? str : "no relevant certificate");
-
		OPENSSL_free(str);
-
	}
-
	return (verified);
-
}
-

-
#endif
-

-
/*
-
 * Enable SSL on a connection.
-
 */
-
int
-
fetch_ssl(conn_t *conn, const struct url *URL, int verbose)
-
{
-
#ifdef WITH_SSL
-
	int ret, ssl_err;
-
	X509_NAME *name;
-
	char *str;
-

-
	if ((conn->ssl_ctx = SSL_CTX_new(TLS_client_method())) == NULL) {
-
		fetch_info("SSL context creation failed");
-
		ERR_print_errors_fp(stderr);
-
		return (-1);
-
	}
-
	SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
-

-
	fetch_ssl_setup_transport_layer(conn->ssl_ctx, verbose);
-
	if (!fetch_ssl_setup_peer_verification(conn->ssl_ctx, verbose))
-
		return (-1);
-
	if (!fetch_ssl_setup_client_certificate(conn->ssl_ctx, verbose))
-
		return (-1);
-

-
	conn->ssl = SSL_new(conn->ssl_ctx);
-
	if (conn->ssl == NULL) {
-
		fetch_info("SSL connection creation failed");
-
		ERR_print_errors_fp(stderr);
-
		return (-1);
-
	}
-
	SSL_set_fd(conn->ssl, conn->sd);
-

-
#if !defined(OPENSSL_NO_TLSEXT)
-
	if (!SSL_set_tlsext_host_name(conn->ssl, __DECONST(char *, URL->host))) {
-
		fetch_info("Failed to set TLS server name indication for host %s",
-
		    URL->host);
-
		return (-1);
-
	}
-
#endif
-
	while ((ret = SSL_connect(conn->ssl)) == -1) {
-
		ssl_err = SSL_get_error(conn->ssl, ret);
-
		if (ssl_err != SSL_ERROR_WANT_READ &&
-
		    ssl_err != SSL_ERROR_WANT_WRITE) {
-
			ERR_print_errors_fp(stderr);
-
			return (-1);
-
		}
-
	}
-
	conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
-

-
	if (conn->ssl_cert == NULL) {
-
		fetch_info("No server SSL certificate");
-
		return (-1);
-
	}
-

-
	if (getenv("SSL_NO_VERIFY_HOSTNAME") == NULL) {
-
		fetch_verbose("Verify hostname");
-
		if (!fetch_ssl_verify_hname(conn->ssl_cert, URL->host)) {
-
			fetch_info("SSL certificate subject does not match host %s",
-
			    URL->host);
-
			return (-1);
-
		}
-
	}
-

-
	if (verbose) {
-
		fetch_info("%s connection established using %s",
-
		    SSL_get_version(conn->ssl), SSL_get_cipher(conn->ssl));
-
		name = X509_get_subject_name(conn->ssl_cert);
-
		str = X509_NAME_oneline(name, 0, 0);
-
		fetch_info("Certificate subject: %s", str);
-
		OPENSSL_free(str);
-
		name = X509_get_issuer_name(conn->ssl_cert);
-
		str = X509_NAME_oneline(name, 0, 0);
-
		fetch_info("Certificate issuer: %s", str);
-
		OPENSSL_free(str);
-
	}
-

-
	return (0);
-
#else
-
	(void)conn;
-
	(void)verbose;
-
	(void)URL;
-
	fetch_info("SSL support disabled");
-
	return (-1);
-
#endif
-
}
-

-
#define FETCH_READ_WAIT		-2
-
#define FETCH_READ_ERROR	-1
-
#define FETCH_READ_DONE		 0
-

-
#ifdef WITH_SSL
-
static ssize_t
-
fetch_ssl_read(SSL *ssl, char *buf, size_t len)
-
{
-
	ssize_t rlen;
-
	int ssl_err;
-

-
	rlen = SSL_read(ssl, buf, len);
-
	if (rlen < 0) {
-
		ssl_err = SSL_get_error(ssl, rlen);
-
		if (ssl_err == SSL_ERROR_WANT_READ ||
-
		    ssl_err == SSL_ERROR_WANT_WRITE) {
-
			return (FETCH_READ_WAIT);
-
		} else {
-
			ERR_print_errors_fp(stderr);
-
			return (FETCH_READ_ERROR);
-
		}
-
	}
-
	return (rlen);
-
}
-
#endif
-

-
static ssize_t
-
fetch_socket_read(int sd, char *buf, size_t len)
-
{
-
	ssize_t rlen;
-

-
	rlen = read(sd, buf, len);
-
	if (rlen < 0) {
-
		if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls)) {
-
			return (FETCH_READ_WAIT);
-
		} else {
-
			return (FETCH_READ_ERROR);
-
		}
-
	}
-
	return (rlen);
-
}
-

-
/*
-
 * Read a character from a connection w/ timeout
-
 */
-
ssize_t
-
fetch_read(conn_t *conn, char *buf, size_t len)
-
{
-
	struct timeval now, timeout, delta;
-
	struct pollfd pfd;
-
	ssize_t rlen;
-
	int deltams;
-

-
	if (fetchTimeout > 0) {
-
		gettimeofday(&timeout, NULL);
-
		timeout.tv_sec += fetchTimeout;
-
	}
-

-
	deltams = INFTIM;
-
	memset(&pfd, 0, sizeof pfd);
-
	pfd.fd = conn->sd;
-
	pfd.events = POLLIN | POLLERR;
-

-
	for (;;) {
-
		/*
-
		 * The socket is non-blocking.  Instead of the canonical
-
		 * poll() -> read(), we do the following:
-
		 *
-
		 * 1) call read() or SSL_read().
-
		 * 2) if we received some data, return it.
-
		 * 3) if an error occurred, return -1.
-
		 * 4) if read() or SSL_read() signaled EOF, return.
-
		 * 5) if we did not receive any data but we're not at EOF,
-
		 *    call poll().
-
		 *
-
		 * In the SSL case, this is necessary because if we
-
		 * receive a close notification, we have to call
-
		 * SSL_read() one additional time after we've read
-
		 * everything we received.
-
		 *
-
		 * In the non-SSL case, it may improve performance (very
-
		 * slightly) when reading small amounts of data.
-
		 */
-
#ifdef WITH_SSL
-
		if (conn->ssl != NULL)
-
			rlen = fetch_ssl_read(conn->ssl, buf, len);
-
		else
-
#endif
-
			rlen = fetch_socket_read(conn->sd, buf, len);
-
		if (rlen >= 0) {
-
			break;
-
		} else if (rlen == FETCH_READ_ERROR) {
-
			fetch_syserr();
-
			return (-1);
-
		}
-
		// assert(rlen == FETCH_READ_WAIT);
-
		if (fetchTimeout > 0) {
-
			gettimeofday(&now, NULL);
-
			if (!timercmp(&timeout, &now, >)) {
-
				errno = ETIMEDOUT;
-
				fetch_syserr();
-
				return (-1);
-
			}
-
			timersub(&timeout, &now, &delta);
-
			deltams = delta.tv_sec * 1000 +
-
			    delta.tv_usec / 1000;
-
		}
-
		errno = 0;
-
		pfd.revents = 0;
-
		if (poll(&pfd, 1, deltams) < 0) {
-
			if (errno == EINTR && fetchRestartCalls)
-
				continue;
-
			fetch_syserr();
-
			return (-1);
-
		}
-
	}
-
	return (rlen);
-
}
-

-

-
/*
-
 * Read a line of text from a connection w/ timeout
-
 */
-
#define MIN_BUF_SIZE 1024
-

-
int
-
fetch_getln(conn_t *conn)
-
{
-
	char *tmp;
-
	size_t tmpsize;
-
	ssize_t len;
-
	char c;
-

-
	if (conn->buf == NULL) {
-
		if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
-
			errno = ENOMEM;
-
			return (-1);
-
		}
-
		conn->bufsize = MIN_BUF_SIZE;
-
	}
-

-
	conn->buf[0] = '\0';
-
	conn->buflen = 0;
-

-
	do {
-
		len = fetch_read(conn, &c, 1);
-
		if (len == -1)
-
			return (-1);
-
		if (len == 0)
-
			break;
-
		conn->buf[conn->buflen++] = c;
-
		if (conn->buflen == conn->bufsize) {
-
			tmp = conn->buf;
-
			tmpsize = conn->bufsize * 2 + 1;
-
			if ((tmp = realloc(tmp, tmpsize)) == NULL) {
-
				errno = ENOMEM;
-
				return (-1);
-
			}
-
			conn->buf = tmp;
-
			conn->bufsize = tmpsize;
-
		}
-
	} while (c != '\n');
-

-
	conn->buf[conn->buflen] = '\0';
-
	DEBUGF("<<< %s", conn->buf);
-
	return (0);
-
}
-

-

-
/*
-
 * Write to a connection w/ timeout
-
 */
-
ssize_t
-
fetch_write(conn_t *conn, const char *buf, size_t len)
-
{
-
	struct iovec iov;
-

-
	iov.iov_base = __DECONST(char *, buf);
-
	iov.iov_len = len;
-
	return (fetch_writev(conn, &iov, 1));
-
}
-

-
/*
-
 * Write a vector to a connection w/ timeout
-
 * Note: can modify the iovec.
-
 */
-
ssize_t
-
fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
-
{
-
	struct timeval now, timeout, delta;
-
	struct pollfd pfd;
-
	ssize_t wlen, total;
-
	int deltams;
-

-
	memset(&pfd, 0, sizeof pfd);
-
	if (fetchTimeout) {
-
		pfd.fd = conn->sd;
-
		pfd.events = POLLOUT | POLLERR;
-
		gettimeofday(&timeout, NULL);
-
		timeout.tv_sec += fetchTimeout;
-
	}
-

-
	total = 0;
-
	while (iovcnt > 0) {
-
		while (fetchTimeout && pfd.revents == 0) {
-
			gettimeofday(&now, NULL);
-
			if (!timercmp(&timeout, &now, >)) {
-
				errno = ETIMEDOUT;
-
				fetch_syserr();
-
				return (-1);
-
			}
-
			timersub(&timeout, &now, &delta);
-
			deltams = delta.tv_sec * 1000 +
-
			    delta.tv_usec / 1000;
-
			errno = 0;
-
			pfd.revents = 0;
-
			if (poll(&pfd, 1, deltams) < 0) {
-
				/* POSIX compliance */
-
				if (errno == EAGAIN)
-
					continue;
-
				if (errno == EINTR && fetchRestartCalls)
-
					continue;
-
				return (-1);
-
			}
-
		}
-
		errno = 0;
-
#ifdef WITH_SSL
-
		if (conn->ssl != NULL)
-
			wlen = SSL_write(conn->ssl,
-
			    iov->iov_base, iov->iov_len);
-
		else
-
#endif
-
			wlen = writev(conn->sd, iov, iovcnt);
-
		if (wlen == 0) {
-
			/* we consider a short write a failure */
-
			/* XXX perhaps we shouldn't in the SSL case */
-
			errno = EPIPE;
-
			fetch_syserr();
-
			return (-1);
-
		}
-
		if (wlen < 0) {
-
			if (errno == EINTR && fetchRestartCalls)
-
				continue;
-
			return (-1);
-
		}
-
		total += wlen;
-
		while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
-
			wlen -= iov->iov_len;
-
			iov++;
-
			iovcnt--;
-
		}
-
		if (iovcnt > 0) {
-
			iov->iov_len -= wlen;
-
			iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
-
		}
-
	}
-
	return (total);
-
}
-

-

-
/*
-
 * Write a line of text to a connection w/ timeout
-
 */
-
int
-
fetch_putln(conn_t *conn, const char *str, size_t len)
-
{
-
	struct iovec iov[2];
-
	int ret;
-

-
	DEBUGF(">>> %s\n", str);
-
	iov[0].iov_base = __DECONST(char *, str);
-
	iov[0].iov_len = len;
-
	iov[1].iov_base = __DECONST(char *, ENDL);
-
	iov[1].iov_len = sizeof(ENDL);
-
	if (len == 0)
-
		ret = fetch_writev(conn, &iov[1], 1);
-
	else
-
		ret = fetch_writev(conn, iov, 2);
-
	if (ret == -1)
-
		return (-1);
-
	return (0);
-
}
-

-

-
/*
-
 * Close connection
-
 */
-
int
-
fetch_close(conn_t *conn)
-
{
-
	int ret;
-

-
	if (--conn->ref > 0)
-
		return (0);
-
#ifdef WITH_SSL
-
	if (conn->ssl) {
-
		SSL_shutdown(conn->ssl);
-
		SSL_set_connect_state(conn->ssl);
-
		SSL_free(conn->ssl);
-
		conn->ssl = NULL;
-
	}
-
	if (conn->ssl_ctx) {
-
		SSL_CTX_free(conn->ssl_ctx);
-
		conn->ssl_ctx = NULL;
-
	}
-
	if (conn->ssl_cert) {
-
		X509_free(conn->ssl_cert);
-
		conn->ssl_cert = NULL;
-
	}
-
#endif
-
	ret = close(conn->sd);
-
	free(conn->buf);
-
	free(conn);
-
	return (ret);
-
}
-

-

-
/*** Directory-related utility functions *************************************/
-

-
int
-
fetch_add_entry(struct url_ent **p, int *size, int *len,
-
    const char *name, struct url_stat *us)
-
{
-
	struct url_ent *tmp;
-

-
	if (*p == NULL) {
-
		*size = 0;
-
		*len = 0;
-
	}
-

-
	if (*len >= *size - 1) {
-
		tmp = realloc(*p, (*size * 2 + 1) * sizeof(**p));
-
		if (tmp == NULL) {
-
			errno = ENOMEM;
-
			fetch_syserr();
-
			return (-1);
-
		}
-
		*size = (*size * 2 + 1);
-
		*p = tmp;
-
	}
-

-
	tmp = *p + *len;
-
	snprintf(tmp->name, PATH_MAX, "%s", name);
-
	memcpy(&tmp->stat, us, sizeof(*us));
-

-
	(*len)++;
-
	(++tmp)->name[0] = 0;
-

-
	return (0);
-
}
-

-

-
/*** Authentication-related utility functions ********************************/
-

-
static const char *
-
fetch_read_word(FILE *f)
-
{
-
	static char word[1024];
-

-
	if (fscanf(f, " %1023s ", word) != 1)
-
		return (NULL);
-
	return (word);
-
}
-

-
static int
-
fetch_netrc_open(void)
-
{
-
	struct passwd *pwd;
-
	char fn[PATH_MAX];
-
	const char *p;
-
	int fd, serrno;
-

-
	if ((p = getenv("NETRC")) != NULL) {
-
		DEBUGF("NETRC=%s\n", p);
-
		if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
-
			fetch_info("$NETRC specifies a file name "
-
			    "longer than PATH_MAX");
-
			return (-1);
-
		}
-
	} else {
-
		if ((p = getenv("HOME")) == NULL) {
-
			if ((pwd = getpwuid(getuid())) == NULL ||
-
			    (p = pwd->pw_dir) == NULL)
-
				return (-1);
-
		}
-
		if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
-
			return (-1);
-
	}
-

-
	if ((fd = open(fn, O_RDONLY)) < 0) {
-
		serrno = errno;
-
		DEBUGF("%s: %s\n", fn, strerror(serrno));
-
		errno = serrno;
-
	}
-
	return (fd);
-
}
-

-
/*
-
 * Get authentication data for a URL from .netrc
-
 */
-
int
-
fetch_netrc_auth(struct url *url)
-
{
-
	const char *word;
-
	int serrno;
-
	FILE *f;
-

-
	if (url->netrcfd < 0)
-
		url->netrcfd = fetch_netrc_open();
-
	if (url->netrcfd < 0)
-
		return (-1);
-
	if ((f = fdopen(url->netrcfd, "r")) == NULL) {
-
		serrno = errno;
-
		DEBUGF("fdopen(netrcfd): %s", strerror(errno));
-
		close(url->netrcfd);
-
		url->netrcfd = -1;
-
		errno = serrno;
-
		return (-1);
-
	}
-
	rewind(f);
-
	DEBUGF("searching netrc for %s\n", url->host);
-
	while ((word = fetch_read_word(f)) != NULL) {
-
		if (strcmp(word, "default") == 0) {
-
			DEBUGF("using default netrc settings\n");
-
			break;
-
		}
-
		if (strcmp(word, "machine") == 0 &&
-
		    (word = fetch_read_word(f)) != NULL &&
-
		    strcasecmp(word, url->host) == 0) {
-
			DEBUGF("using netrc settings for %s\n", word);
-
			break;
-
		}
-
	}
-
	if (word == NULL)
-
		goto ferr;
-
	while ((word = fetch_read_word(f)) != NULL) {
-
		if (strcmp(word, "login") == 0) {
-
			if ((word = fetch_read_word(f)) == NULL)
-
				goto ferr;
-
			if (snprintf(url->user, sizeof(url->user),
-
				"%s", word) > (int)sizeof(url->user)) {
-
				fetch_info("login name in .netrc is too long");
-
				url->user[0] = '\0';
-
			}
-
		} else if (strcmp(word, "password") == 0) {
-
			if ((word = fetch_read_word(f)) == NULL)
-
				goto ferr;
-
			if (snprintf(url->pwd, sizeof(url->pwd),
-
				"%s", word) > (int)sizeof(url->pwd)) {
-
				fetch_info("password in .netrc is too long");
-
				url->pwd[0] = '\0';
-
			}
-
		} else if (strcmp(word, "account") == 0) {
-
			if ((word = fetch_read_word(f)) == NULL)
-
				goto ferr;
-
			/* XXX not supported! */
-
		} else {
-
			break;
-
		}
-
	}
-
	fclose(f);
-
	url->netrcfd = -1;
-
	return (0);
-
ferr:
-
	serrno = errno;
-
	fclose(f);
-
	url->netrcfd = -1;
-
	errno = serrno;
-
	return (-1);
-
}
-

-
/*
-
 * The no_proxy environment variable specifies a set of domains for
-
 * which the proxy should not be consulted; the contents is a comma-,
-
 * or space-separated list of domain names.  A single asterisk will
-
 * override all proxy variables and no transactions will be proxied
-
 * (for compatibility with lynx and curl, see the discussion at
-
 * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
-
 */
-
int
-
fetch_no_proxy_match(const char *host)
-
{
-
	const char *no_proxy, *p, *q;
-
	size_t h_len, d_len;
-

-
	if ((no_proxy = getenv("NO_PROXY")) == NULL &&
-
	    (no_proxy = getenv("no_proxy")) == NULL)
-
		return (0);
-

-
	/* asterisk matches any hostname */
-
	if (strcmp(no_proxy, "*") == 0)
-
		return (1);
-

-
	h_len = strlen(host);
-
	p = no_proxy;
-
	do {
-
		/* position p at the beginning of a domain suffix */
-
		while (*p == ',' || isspace((unsigned char)*p))
-
			p++;
-

-
		/* position q at the first separator character */
-
		for (q = p; *q; ++q)
-
			if (*q == ',' || isspace((unsigned char)*q))
-
				break;
-

-
		d_len = q - p;
-
		if (d_len > 0 && h_len >= d_len &&
-
		    strncasecmp(host + h_len - d_len,
-
			p, d_len) == 0) {
-
			/* domain name matches */
-
			return (1);
-
		}
-

-
		p = q + 1;
-
	} while (*q);
-

-
	return (0);
-
}
deleted external/libfetch/common.h
@@ -1,175 +0,0 @@
-
/*-
-
 * SPDX-License-Identifier: BSD-3-Clause
-
 *
-
 * Copyright (c) 1998-2014 Dag-Erling Smørgrav
-
 * 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
-
 *    in this position and unchanged.
-
 * 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.
-
 * 3. The name of the author may not be used to endorse or promote products
-
 *    derived from this software without specific prior written permission
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
-
 */
-

-
#ifndef _COMMON_H_INCLUDED
-
#define _COMMON_H_INCLUDED
-

-
#define FTP_DEFAULT_PORT	21
-
#define HTTP_DEFAULT_PORT	80
-
#define FTP_DEFAULT_PROXY_PORT	21
-
#define HTTP_DEFAULT_PROXY_PORT	3128
-

-
#ifdef WITH_SSL
-
#include <openssl/crypto.h>
-
#include <openssl/x509.h>
-
#include <openssl/pem.h>
-
#include <openssl/ssl.h>
-
#include <openssl/err.h>
-
#endif
-

-
/* Connection */
-
typedef struct fetchconn conn_t;
-
struct fetchconn {
-
	int		 sd;		/* socket descriptor */
-
	char		*buf;		/* buffer */
-
	size_t		 bufsize;	/* buffer size */
-
	size_t		 buflen;	/* length of buffer contents */
-
	int		 err;		/* last protocol reply code */
-
#ifdef WITH_SSL
-
	SSL		*ssl;		/* SSL handle */
-
	SSL_CTX		*ssl_ctx;	/* SSL context */
-
	X509		*ssl_cert;	/* server certificate */
-
#endif
-
	int		 ref;		/* reference count */
-
	char		 cache_host[MAXHOSTNAMELEN + 1];
-
	int		 cache_port;
-
	int		 cache_ssl;	/* 1 if HTTPS */
-
};
-

-
/* Structure used for error message lists */
-
struct fetcherr {
-
	const int	 num;
-
	const int	 cat;
-
	const char	*string;
-
};
-

-
/* For SOCKS header size */
-
#define HEAD_SIZE	4
-
#define FQDN_SIZE	256
-
#define PACK_SIZE	1
-
#define PORT_SIZE	2
-
#define BUFF_SIZE	HEAD_SIZE + FQDN_SIZE + PACK_SIZE + PORT_SIZE
-

-
/* SOCKS5 Request Header */
-
#define SOCKS_VERSION_5		0x05
-
/* SOCKS5 CMD */
-
#define SOCKS_CONNECTION	0x01
-
#define SOCKS_BIND		0x02
-
#define SOCKS_UDP		0x03
-
#define SOCKS_NOMETHODS		0xFF
-
#define SOCKS5_NOTIMPLEMENTED	0x00
-
/* SOCKS5 Reserved */
-
#define SOCKS_RSV		0x00
-
/* SOCKS5 Address Type */
-
#define SOCKS_ATYP_IPV4		0x01
-
#define SOCKS_ATYP_DOMAINNAME	0x03
-
#define SOCKS_ATYP_IPV6		0x04
-
/* SOCKS5 Reply Field */
-
#define SOCKS_SUCCESS			0x00
-
#define SOCKS_GENERAL_FAILURE		0x01
-
#define SOCKS_CONNECTION_NOT_ALLOWED	0x02
-
#define SOCKS_NETWORK_UNREACHABLE	0x03
-
#define SOCKS_HOST_UNREACHABLE		0x04
-
#define SOCKS_CONNECTION_REFUSED	0x05
-
#define SOCKS_TTL_EXPIRED		0x06
-
#define SOCKS_COMMAND_NOT_SUPPORTED	0x07
-
#define SOCKS_ADDRESS_NOT_SUPPORTED	0x08
-

-
/* for fetch_writev */
-
struct iovec;
-

-
void		 fetch_seterr(struct fetcherr *, int);
-
void		 fetch_syserr(void);
-
void		 fetch_info(const char *, ...) __printflike(1, 2);
-
int		 fetch_socks5_getenv(char **host, int *port);
-
int		 fetch_socks5_init(conn_t *conn, const char *host,
-
		     int port, int verbose);
-
int		 fetch_default_port(const char *);
-
int		 fetch_default_proxy_port(const char *);
-
struct addrinfo *fetch_resolve(const char *, int, int);
-
int		 fetch_bind(int, int, const char *);
-
conn_t		*fetch_connect(const char *, int, int, int);
-
conn_t		*fetch_reopen(int);
-
conn_t		*fetch_ref(conn_t *);
-
#ifdef WITH_SSL
-
int		 fetch_ssl_cb_verify_crt(int, X509_STORE_CTX*);
-
#endif
-
int		 fetch_ssl(conn_t *, const struct url *, int);
-
ssize_t		 fetch_read(conn_t *, char *, size_t);
-
int		 fetch_getln(conn_t *);
-
ssize_t		 fetch_write(conn_t *, const char *, size_t);
-
ssize_t		 fetch_writev(conn_t *, struct iovec *, int);
-
int		 fetch_putln(conn_t *, const char *, size_t);
-
int		 fetch_close(conn_t *);
-
int		 fetch_add_entry(struct url_ent **, int *, int *,
-
		     const char *, struct url_stat *);
-
int		 fetch_netrc_auth(struct url *url);
-
int		 fetch_no_proxy_match(const char *);
-

-
#define ftp_seterr(n)	 fetch_seterr(ftp_errlist, n)
-
#define http_seterr(n)	 fetch_seterr(http_errlist, n)
-
#define netdb_seterr(n)	 fetch_seterr(netdb_errlist, n)
-
#define url_seterr(n)	 fetch_seterr(url_errlist, n)
-
#define socks5_seterr(n) fetch_seterr(socks5_errlist, n)
-

-
#ifndef NDEBUG
-
#define DEBUGF(...)							\
-
	do {								\
-
		if (fetchDebug)						\
-
			fprintf(stderr, __VA_ARGS__);			\
-
	} while (0)
-
#else
-
#define DEBUGF(...)							\
-
	do {								\
-
		/* nothing */						\
-
	} while (0)
-
#endif
-

-
/*
-
 * I don't really like exporting http_request(),
-
 * but the HTTP and FTP code occasionally needs to cross-call
-
 * eachother, and this saves me from adding a lot of special-case code
-
 * to handle those cases.
-
 *
-
 * Note that _*_request() free purl, which is way ugly but saves us a
-
 * whole lot of trouble.
-
 */
-
FILE		*http_request(struct url *, const char *,
-
		     struct url_stat *, struct url *, const char *);
-
FILE		*http_request_body(struct url *, const char *,
-
		     struct url_stat *, struct url *, const char *,
-
		     const char *, const char *);
-

-
/*
-
 * Check whether a particular flag is set
-
 */
-
#define CHECK_FLAG(x)	(flags && strchr(flags, (x)))
-

-
#endif
deleted external/libfetch/fetch.3
@@ -1,841 +0,0 @@
-
.\"-
-
.\" Copyright (c) 1998-2013 Dag-Erling Smørgrav
-
.\" Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
-
.\" 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: head/lib/libfetch/fetch.3 273124 2014-10-15 07:35:50Z des $
-
.\"
-
.Dd October 15, 2014
-
.Dt FETCH 3
-
.Os
-
.Sh NAME
-
.Nm fetchMakeURL ,
-
.Nm fetchParseURL ,
-
.Nm fetchFreeURL ,
-
.Nm fetchXGetURL ,
-
.Nm fetchGetURL ,
-
.Nm fetchPutURL ,
-
.Nm fetchStatURL ,
-
.Nm fetchListURL ,
-
.Nm fetchXGet ,
-
.Nm fetchGet ,
-
.Nm fetchPut ,
-
.Nm fetchStat ,
-
.Nm fetchList ,
-
.Nm fetchXGetFile ,
-
.Nm fetchGetFile ,
-
.Nm fetchPutFile ,
-
.Nm fetchStatFile ,
-
.Nm fetchListFile ,
-
.Nm fetchXGetHTTP ,
-
.Nm fetchGetHTTP ,
-
.Nm fetchPutHTTP ,
-
.Nm fetchStatHTTP ,
-
.Nm fetchListHTTP ,
-
.Nm fetchXGetFTP ,
-
.Nm fetchGetFTP ,
-
.Nm fetchPutFTP ,
-
.Nm fetchStatFTP ,
-
.Nm fetchListFTP
-
.Nd file transfer functions
-
.Sh LIBRARY
-
.Lb libfetch
-
.Sh SYNOPSIS
-
.In sys/param.h
-
.In stdio.h
-
.In fetch.h
-
.Ft struct url *
-
.Fn fetchMakeURL "const char *scheme" "const char *host" "int port" "const char *doc" "const char *user" "const char *pwd"
-
.Ft struct url *
-
.Fn fetchParseURL "const char *URL"
-
.Ft void
-
.Fn fetchFreeURL "struct url *u"
-
.Ft FILE *
-
.Fn fetchXGetURL "const char *URL" "struct url_stat *us" "const char *flags"
-
.Ft FILE *
-
.Fn fetchGetURL "const char *URL" "const char *flags"
-
.Ft FILE *
-
.Fn fetchPutURL "const char *URL" "const char *flags"
-
.Ft int
-
.Fn fetchStatURL "const char *URL" "struct url_stat *us" "const char *flags"
-
.Ft struct url_ent *
-
.Fn fetchListURL "const char *URL" "const char *flags"
-
.Ft FILE *
-
.Fn fetchXGet "struct url *u" "struct url_stat *us" "const char *flags"
-
.Ft FILE *
-
.Fn fetchGet "struct url *u" "const char *flags"
-
.Ft FILE *
-
.Fn fetchPut "struct url *u" "const char *flags"
-
.Ft int
-
.Fn fetchStat "struct url *u" "struct url_stat *us" "const char *flags"
-
.Ft struct url_ent *
-
.Fn fetchList "struct url *u" "const char *flags"
-
.Ft FILE *
-
.Fn fetchXGetFile "struct url *u" "struct url_stat *us" "const char *flags"
-
.Ft FILE *
-
.Fn fetchGetFile "struct url *u" "const char *flags"
-
.Ft FILE *
-
.Fn fetchPutFile "struct url *u" "const char *flags"
-
.Ft int
-
.Fn fetchStatFile "struct url *u" "struct url_stat *us" "const char *flags"
-
.Ft struct url_ent *
-
.Fn fetchListFile "struct url *u" "const char *flags"
-
.Ft FILE *
-
.Fn fetchXGetHTTP "struct url *u" "struct url_stat *us" "const char *flags"
-
.Ft FILE *
-
.Fn fetchGetHTTP "struct url *u" "const char *flags"
-
.Ft FILE *
-
.Fn fetchPutHTTP "struct url *u" "const char *flags"
-
.Ft int
-
.Fn fetchStatHTTP "struct url *u" "struct url_stat *us" "const char *flags"
-
.Ft struct url_ent *
-
.Fn fetchListHTTP "struct url *u" "const char *flags"
-
.Ft FILE *
-
.Fn fetchXGetFTP "struct url *u" "struct url_stat *us" "const char *flags"
-
.Ft FILE *
-
.Fn fetchGetFTP "struct url *u" "const char *flags"
-
.Ft FILE *
-
.Fn fetchPutFTP "struct url *u" "const char *flags"
-
.Ft int
-
.Fn fetchStatFTP "struct url *u" "struct url_stat *us" "const char *flags"
-
.Ft struct url_ent *
-
.Fn fetchListFTP "struct url *u" "const char *flags"
-
.Sh DESCRIPTION
-
These functions implement a high-level library for retrieving and
-
uploading files using Uniform Resource Locators (URLs).
-
.Pp
-
.Fn fetchParseURL
-
takes a URL in the form of a null-terminated string and splits it into
-
its components function according to the Common Internet Scheme Syntax
-
detailed in RFC1738.
-
A regular expression which produces this syntax is:
-
.Bd -literal
-
    <scheme>:(//(<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
-
.Ed
-
.Pp
-
If the URL does not seem to begin with a scheme name, the following
-
syntax is assumed:
-
.Bd -literal
-
    ((<user>(:<pwd>)?@)?<host>(:<port>)?)?/(<document>)?
-
.Ed
-
.Pp
-
Note that some components of the URL are not necessarily relevant to
-
all URL schemes.
-
For instance, the file scheme only needs the <scheme> and <document>
-
components.
-
.Pp
-
.Fn fetchMakeURL
-
and
-
.Fn fetchParseURL
-
return a pointer to a
-
.Vt url
-
structure, which is defined as follows in
-
.In fetch.h :
-
.Bd -literal
-
#define URL_SCHEMELEN 16
-
#define URL_USERLEN 256
-
#define URL_PWDLEN 256
-

-
struct url {
-
    char	 scheme[URL_SCHEMELEN+1];
-
    char	 user[URL_USERLEN+1];
-
    char	 pwd[URL_PWDLEN+1];
-
    char	 host[MAXHOSTNAMELEN+1];
-
    int		 port;
-
    char	*doc;
-
    off_t	 offset;
-
    size_t	 length;
-
    time_t	 ims_time;
-
};
-
.Ed
-
.Pp
-
The
-
.Va ims_time
-
field stores the time value for
-
.Li If-Modified-Since
-
HTTP requests.
-
.Pp
-
The pointer returned by
-
.Fn fetchMakeURL
-
or
-
.Fn fetchParseURL
-
should be freed using
-
.Fn fetchFreeURL .
-
.Pp
-
.Fn fetchXGetURL ,
-
.Fn fetchGetURL ,
-
and
-
.Fn fetchPutURL
-
constitute the recommended interface to the
-
.Nm fetch
-
library.
-
They examine the URL passed to them to determine the transfer
-
method, and call the appropriate lower-level functions to perform the
-
actual transfer.
-
.Fn fetchXGetURL
-
also returns the remote document's metadata in the
-
.Vt url_stat
-
structure pointed to by the
-
.Fa us
-
argument.
-
.Pp
-
The
-
.Fa flags
-
argument is a string of characters which specify transfer options.
-
The
-
meaning of the individual flags is scheme-dependent, and is detailed
-
in the appropriate section below.
-
.Pp
-
.Fn fetchStatURL
-
attempts to obtain the requested document's metadata and fill in the
-
structure pointed to by its second argument.
-
The
-
.Vt url_stat
-
structure is defined as follows in
-
.In fetch.h :
-
.Bd -literal
-
struct url_stat {
-
    off_t	 size;
-
    time_t	 atime;
-
    time_t	 mtime;
-
};
-
.Ed
-
.Pp
-
If the size could not be obtained from the server, the
-
.Fa size
-
field is set to -1.
-
If the modification time could not be obtained from the server, the
-
.Fa mtime
-
field is set to the epoch.
-
If the access time could not be obtained from the server, the
-
.Fa atime
-
field is set to the modification time.
-
.Pp
-
.Fn fetchListURL
-
attempts to list the contents of the directory pointed to by the URL
-
provided.
-
If successful, it returns a malloced array of
-
.Vt url_ent
-
structures.
-
The
-
.Vt url_ent
-
structure is defined as follows in
-
.In fetch.h :
-
.Bd -literal
-
struct url_ent {
-
    char         name[PATH_MAX];
-
    struct url_stat stat;
-
};
-
.Ed
-
.Pp
-
The list is terminated by an entry with an empty name.
-
.Pp
-
The pointer returned by
-
.Fn fetchListURL
-
should be freed using
-
.Fn free .
-
.Pp
-
.Fn fetchXGet ,
-
.Fn fetchGet ,
-
.Fn fetchPut
-
and
-
.Fn fetchStat
-
are similar to
-
.Fn fetchXGetURL ,
-
.Fn fetchGetURL ,
-
.Fn fetchPutURL
-
and
-
.Fn fetchStatURL ,
-
except that they expect a pre-parsed URL in the form of a pointer to
-
a
-
.Vt struct url
-
rather than a string.
-
.Pp
-
All of the
-
.Fn fetchXGetXXX ,
-
.Fn fetchGetXXX
-
and
-
.Fn fetchPutXXX
-
functions return a pointer to a stream which can be used to read or
-
write data from or to the requested document, respectively.
-
Note that
-
although the implementation details of the individual access methods
-
vary, it can generally be assumed that a stream returned by one of the
-
.Fn fetchXGetXXX
-
or
-
.Fn fetchGetXXX
-
functions is read-only, and that a stream returned by one of the
-
.Fn fetchPutXXX
-
functions is write-only.
-
.Sh FILE SCHEME
-
.Fn fetchXGetFile ,
-
.Fn fetchGetFile
-
and
-
.Fn fetchPutFile
-
provide access to documents which are files in a locally mounted file
-
system.
-
Only the <document> component of the URL is used.
-
.Pp
-
.Fn fetchXGetFile
-
and
-
.Fn fetchGetFile
-
do not accept any flags.
-
.Pp
-
.Fn fetchPutFile
-
accepts the
-
.Ql a
-
(append to file) flag.
-
If that flag is specified, the data written to
-
the stream returned by
-
.Fn fetchPutFile
-
will be appended to the previous contents of the file, instead of
-
replacing them.
-
.Sh FTP SCHEME
-
.Fn fetchXGetFTP ,
-
.Fn fetchGetFTP
-
and
-
.Fn fetchPutFTP
-
implement the FTP protocol as described in RFC959.
-
.Pp
-
If the
-
.Ql P
-
(not passive) flag is specified, an active (rather than passive)
-
connection will be attempted.
-
.Pp
-
The
-
.Ql p
-
flag is supported for compatibility with earlier versions where active
-
connections were the default.
-
It has precedence over the
-
.Ql P
-
flag, so if both are specified,
-
.Nm
-
will use a passive connection.
-
.Pp
-
If the
-
.Ql l
-
(low) flag is specified, data sockets will be allocated in the low (or
-
default) port range instead of the high port range (see
-
.Xr ip 4 ) .
-
.Pp
-
If the
-
.Ql d
-
(direct) flag is specified,
-
.Fn fetchXGetFTP ,
-
.Fn fetchGetFTP
-
and
-
.Fn fetchPutFTP
-
will use a direct connection even if a proxy server is defined.
-
.Pp
-
If no user name or password is given, the
-
.Nm fetch
-
library will attempt an anonymous login, with user name "anonymous"
-
and password "anonymous@<hostname>".
-
.Sh HTTP SCHEME
-
The
-
.Fn fetchXGetHTTP ,
-
.Fn fetchGetHTTP
-
and
-
.Fn fetchPutHTTP
-
functions implement the HTTP/1.1 protocol.
-
With a little luck, there is
-
even a chance that they comply with RFC2616 and RFC2617.
-
.Pp
-
If the
-
.Ql d
-
(direct) flag is specified,
-
.Fn fetchXGetHTTP ,
-
.Fn fetchGetHTTP
-
and
-
.Fn fetchPutHTTP
-
will use a direct connection even if a proxy server is defined.
-
.Pp
-
If the
-
.Ql i
-
(if-modified-since) flag is specified, and
-
the
-
.Va ims_time
-
field is set in
-
.Vt "struct url" ,
-
then
-
.Fn fetchXGetHTTP
-
and
-
.Fn fetchGetHTTP
-
will send a conditional
-
.Li If-Modified-Since
-
HTTP header to only fetch the content if it is newer than
-
.Va ims_time .
-
.Pp
-
Since there seems to be no good way of implementing the HTTP PUT
-
method in a manner consistent with the rest of the
-
.Nm fetch
-
library,
-
.Fn fetchPutHTTP
-
is currently unimplemented.
-
.Sh HTTPS SCHEME
-
Based on HTTP SCHEME.
-
By default the peer is verified using the CA bundle located in
-
.Pa /etc/ssl/cert.pem .
-
The file may contain multiple CA certificates.
-
A common source of a current CA bundle is
-
.Pa \%security/ca_root_nss .
-
.Pp
-
The CA bundle used for peer verification can be changed by setting the
-
environment variables
-
.Ev SSL_CA_CERT_FILE
-
to point to a concatenated bundle of trusted certificates and
-
.Ev SSL_CA_CERT_PATH
-
to point to a directory containing hashes of trusted CAs (see
-
.Xr verify 1 ) .
-
.Pp
-
A certificate revocation list (CRL) can be used by setting the
-
environment variable
-
.Ev SSL_CRL_FILE
-
(see
-
.Xr crl 1 ) .
-
.Pp
-
Peer verification can be disabled by setting the environment variable
-
.Ev SSL_NO_VERIFY_PEER .
-
Note that this also disables CRL checking.
-
.Pp
-
By default the service identity is verified according to the rules
-
detailed in RFC6125 (also known as hostname verification).
-
This feature can be disabled by setting the environment variable
-
.Ev SSL_NO_VERIFY_HOSTNAME .
-
.Pp
-
Client certificate based authentication is supported.
-
The environment variable
-
.Ev SSL_CLIENT_CERT_FILE
-
should be set to point to a file containing key and client certificate
-
to be used in PEM format. In case the key is stored in a separate
-
file, the environment variable
-
.Ev SSL_CLIENT_KEY_FILE
-
can be set to point to the key in PEM format.
-
In case the key uses a password, the user will be prompted on standard
-
input (see
-
.Xr PEM 3 ) .
-
.Pp
-
By default
-
.Nm libfetch
-
allows TLSv1 and newer when negotiating the connecting with the remote
-
peer.
-
You can change this behavior by setting the
-
.Ev SSL_ALLOW_SSL2
-
and
-
.Ev SSL_ALLOW_SSL3
-
environment variables to allow SSLv2 and SSLv3, respectively, and
-
.Ev SSL_NO_TLS1 ,
-
.Ev SSL_NO_TLS1_1 and
-
.Ev SSL_NO_TLS1_2
-
to disable TLS 1.0, 1.1 and 1.2 respectively.
-
.Sh AUTHENTICATION
-
Apart from setting the appropriate environment variables and
-
specifying the user name and password in the URL or the
-
.Vt struct url ,
-
the calling program has the option of defining an authentication
-
function with the following prototype:
-
.Pp
-
.Ft int
-
.Fn myAuthMethod "struct url *u"
-
.Pp
-
The callback function should fill in the
-
.Fa user
-
and
-
.Fa pwd
-
fields in the provided
-
.Vt struct url
-
and return 0 on success, or any other value to indicate failure.
-
.Pp
-
To register the authentication callback, simply set
-
.Va fetchAuthMethod
-
to point at it.
-
The callback will be used whenever a site requires authentication and
-
the appropriate environment variables are not set.
-
.Pp
-
This interface is experimental and may be subject to change.
-
.Sh RETURN VALUES
-
.Fn fetchParseURL
-
returns a pointer to a
-
.Vt struct url
-
containing the individual components of the URL.
-
If it is
-
unable to allocate memory, or the URL is syntactically incorrect,
-
.Fn fetchParseURL
-
returns a NULL pointer.
-
.Pp
-
The
-
.Fn fetchStat
-
functions return 0 on success and -1 on failure.
-
.Pp
-
All other functions return a stream pointer which may be used to
-
access the requested document, or NULL if an error occurred.
-
.Pp
-
The following error codes are defined in
-
.In fetch.h :
-
.Bl -tag -width 18n
-
.It Bq Er FETCH_ABORT
-
Operation aborted
-
.It Bq Er FETCH_AUTH
-
Authentication failed
-
.It Bq Er FETCH_DOWN
-
Service unavailable
-
.It Bq Er FETCH_EXISTS
-
File exists
-
.It Bq Er FETCH_FULL
-
File system full
-
.It Bq Er FETCH_INFO
-
Informational response
-
.It Bq Er FETCH_MEMORY
-
Insufficient memory
-
.It Bq Er FETCH_MOVED
-
File has moved
-
.It Bq Er FETCH_NETWORK
-
Network error
-
.It Bq Er FETCH_OK
-
No error
-
.It Bq Er FETCH_PROTO
-
Protocol error
-
.It Bq Er FETCH_RESOLV
-
Resolver error
-
.It Bq Er FETCH_SERVER
-
Server error
-
.It Bq Er FETCH_TEMP
-
Temporary error
-
.It Bq Er FETCH_TIMEOUT
-
Operation timed out
-
.It Bq Er FETCH_UNAVAIL
-
File is not available
-
.It Bq Er FETCH_UNKNOWN
-
Unknown error
-
.It Bq Er FETCH_URL
-
Invalid URL
-
.El
-
.Pp
-
The accompanying error message includes a protocol-specific error code
-
and message, e.g.\& "File is not available (404 Not Found)"
-
.Sh ENVIRONMENT
-
.Bl -tag -width ".Ev FETCH_BIND_ADDRESS"
-
.It Ev FETCH_BIND_ADDRESS
-
Specifies a hostname or IP address to which sockets used for outgoing
-
connections will be bound.
-
.It Ev FTP_LOGIN
-
Default FTP login if none was provided in the URL.
-
.It Ev FTP_PASSIVE_MODE
-
If set to
-
.Ql no ,
-
forces the FTP code to use active mode.
-
If set to any other value, forces passive mode even if the application
-
requested active mode.
-
.It Ev FTP_PASSWORD
-
Default FTP password if the remote server requests one and none was
-
provided in the URL.
-
.It Ev FTP_PROXY
-
URL of the proxy to use for FTP requests.
-
The document part is ignored.
-
FTP and HTTP proxies are supported; if no scheme is specified, FTP is
-
assumed.
-
If the proxy is an FTP proxy,
-
.Nm libfetch
-
will send
-
.Ql user@host
-
as user name to the proxy, where
-
.Ql user
-
is the real user name, and
-
.Ql host
-
is the name of the FTP server.
-
.Pp
-
If this variable is set to an empty string, no proxy will be used for
-
FTP requests, even if the
-
.Ev HTTP_PROXY
-
variable is set.
-
.It Ev ftp_proxy
-
Same as
-
.Ev FTP_PROXY ,
-
for compatibility.
-
.It Ev HTTP_ACCEPT
-
Specifies the value of the
-
.Va Accept
-
header for HTTP requests.
-
If empty, no
-
.Va Accept
-
header is sent.
-
The default is
-
.Dq */* .
-
.It Ev HTTP_AUTH
-
Specifies HTTP authorization parameters as a colon-separated list of
-
items.
-
The first and second item are the authorization scheme and realm
-
respectively; further items are scheme-dependent.
-
Currently, the
-
.Dq basic
-
and
-
.Dq digest
-
authorization methods are supported.
-
.Pp
-
Both methods require two parameters: the user name and
-
password, in that order.
-
.Pp
-
This variable is only used if the server requires authorization and
-
no user name or password was specified in the URL.
-
.It Ev HTTP_PROXY
-
URL of the proxy to use for HTTP requests.
-
The document part is ignored.
-
Only HTTP proxies are supported for HTTP requests.
-
If no port number is specified, the default is 3128.
-
.Pp
-
Note that this proxy will also be used for FTP documents, unless the
-
.Ev FTP_PROXY
-
variable is set.
-
.It Ev http_proxy
-
Same as
-
.Ev HTTP_PROXY ,
-
for compatibility.
-
.It Ev HTTP_PROXY_AUTH
-
Specifies authorization parameters for the HTTP proxy in the same
-
format as the
-
.Ev HTTP_AUTH
-
variable.
-
.Pp
-
This variable is used if and only if connected to an HTTP proxy, and
-
is ignored if a user and/or a password were specified in the proxy
-
URL.
-
.It Ev HTTP_REFERER
-
Specifies the referrer URL to use for HTTP requests.
-
If set to
-
.Dq auto ,
-
the document URL will be used as referrer URL.
-
.It Ev HTTP_USER_AGENT
-
Specifies the User-Agent string to use for HTTP requests.
-
This can be useful when working with HTTP origin or proxy servers that
-
differentiate between user agents.
-
If defined but empty, no User-Agent header is sent.
-
.It Ev NETRC
-
Specifies a file to use instead of
-
.Pa ~/.netrc
-
to look up login names and passwords for FTP sites.
-
See
-
.Xr ftp 1
-
for a description of the file format.
-
This feature is experimental.
-
.It Ev NO_PROXY
-
Either a single asterisk, which disables the use of proxies
-
altogether, or a comma- or whitespace-separated list of hosts for
-
which proxies should not be used.
-
.It Ev no_proxy
-
Same as
-
.Ev NO_PROXY ,
-
for compatibility.
-
.It Ev SSL_ALLOW_SSL2
-
Allow SSL version 2 when negotiating the connection (not recommended).
-
.It Ev SSL_ALLOW_SSL3
-
Allow SSL version 3 when negotiating the connection (not recommended).
-
.It Ev SSL_CA_CERT_FILE
-
CA certificate bundle containing trusted CA certificates.
-
Default value:
-
.Pa /etc/ssl/cert.pem .
-
.It Ev SSL_CA_CERT_PATH
-
Path containing trusted CA hashes.
-
.It Ev SSL_CLIENT_CERT_FILE
-
PEM encoded client certificate/key which will be used in
-
client certificate authentication.
-
.It Ev SSL_CLIENT_KEY_FILE
-
PEM encoded client key in case key and client certificate
-
are stored separately.
-
.It Ev SSL_CRL_FILE
-
File containing certificate revocation list.
-
.It Ev SSL_NO_TLS1
-
Do not allow TLS version 1.0 when negotiating the connection.
-
.It Ev SSL_NO_TLS1_1
-
Do not allow TLS version 1.1 when negotiating the connection.
-
.It Ev SSL_NO_TLS1_2
-
Do not allow TLS version 1.2 when negotiating the connection.
-
.It Ev SSL_NO_VERIFY_HOSTNAME
-
If set, do not verify that the hostname matches the subject of the
-
certificate presented by the server.
-
.It Ev SSL_NO_VERIFY_PEER
-
If set, do not verify the peer certificate against trusted CAs.
-
.El
-
.Sh EXAMPLES
-
To access a proxy server on
-
.Pa proxy.example.com
-
port 8080, set the
-
.Ev HTTP_PROXY
-
environment variable in a manner similar to this:
-
.Pp
-
.Dl HTTP_PROXY=http://proxy.example.com:8080
-
.Pp
-
If the proxy server requires authentication, there are
-
two options available for passing the authentication data.
-
The first method is by using the proxy URL:
-
.Pp
-
.Dl HTTP_PROXY=http://<user>:<pwd>@proxy.example.com:8080
-
.Pp
-
The second method is by using the
-
.Ev HTTP_PROXY_AUTH
-
environment variable:
-
.Bd -literal -offset indent
-
HTTP_PROXY=http://proxy.example.com:8080
-
HTTP_PROXY_AUTH=basic:*:<user>:<pwd>
-
.Ed
-
.Pp
-
To disable the use of a proxy for an HTTP server running on the local
-
host, define
-
.Ev NO_PROXY
-
as follows:
-
.Bd -literal -offset indent
-
NO_PROXY=localhost,127.0.0.1
-
.Ed
-
.Pp
-
Access HTTPS website without any certificate verification whatsoever:
-
.Bd -literal -offset indent
-
SSL_NO_VERIFY_PEER=1
-
SSL_NO_VERIFY_HOSTNAME=1
-
.Ed
-
.Pp
-
Access HTTPS website using client certificate based authentication
-
and a private CA:
-
.Bd -literal -offset indent
-
SSL_CLIENT_CERT_FILE=/path/to/client.pem
-
SSL_CA_CERT_FILE=/path/to/myca.pem
-
.Ed
-
.Sh SEE ALSO
-
.Xr fetch 1 ,
-
.Xr ftpio 3 ,
-
.Xr ip 4
-
.Rs
-
.%A J. Postel
-
.%A J. K. Reynolds
-
.%D October 1985
-
.%B File Transfer Protocol
-
.%O RFC959
-
.Re
-
.Rs
-
.%A P. Deutsch
-
.%A A. Emtage
-
.%A A. Marine.
-
.%D May 1994
-
.%T How to Use Anonymous FTP
-
.%O RFC1635
-
.Re
-
.Rs
-
.%A T. Berners-Lee
-
.%A L. Masinter
-
.%A M. McCahill
-
.%D December 1994
-
.%T Uniform Resource Locators (URL)
-
.%O RFC1738
-
.Re
-
.Rs
-
.%A R. Fielding
-
.%A J. Gettys
-
.%A J. Mogul
-
.%A H. Frystyk
-
.%A L. Masinter
-
.%A P. Leach
-
.%A T. Berners-Lee
-
.%D January 1999
-
.%B Hypertext Transfer Protocol -- HTTP/1.1
-
.%O RFC2616
-
.Re
-
.Rs
-
.%A J. Franks
-
.%A P. Hallam-Baker
-
.%A J. Hostetler
-
.%A S. Lawrence
-
.%A P. Leach
-
.%A A. Luotonen
-
.%A L. Stewart
-
.%D June 1999
-
.%B HTTP Authentication: Basic and Digest Access Authentication
-
.%O RFC2617
-
.Re
-
.Sh HISTORY
-
The
-
.Nm fetch
-
library first appeared in
-
.Fx 3.0 .
-
.Sh AUTHORS
-
.An -nosplit
-
The
-
.Nm fetch
-
library was mostly written by
-
.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org
-
with numerous suggestions and contributions from
-
.An Jordan K. Hubbard Aq Mt jkh@FreeBSD.org ,
-
.An Eugene Skepner Aq Mt eu@qub.com ,
-
.An Hajimu Umemoto Aq Mt ume@FreeBSD.org ,
-
.An Henry Whincup Aq Mt henry@techiebod.com ,
-
.An Jukka A. Ukkonen Aq Mt jau@iki.fi ,
-
.An Jean-Fran\(,cois Dockes Aq Mt jf@dockes.org ,
-
.An Michael Gmelin Aq Mt freebsd@grem.de
-
and others.
-
It replaces the older
-
.Nm ftpio
-
library written by
-
.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org
-
and
-
.An Jordan K. Hubbard Aq Mt jkh@FreeBSD.org .
-
.Pp
-
This manual page was written by
-
.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org
-
and
-
.An Michael Gmelin Aq Mt freebsd@grem.de .
-
.Sh BUGS
-
Some parts of the library are not yet implemented.
-
The most notable
-
examples of this are
-
.Fn fetchPutHTTP ,
-
.Fn fetchListHTTP ,
-
.Fn fetchListFTP
-
and FTP proxy support.
-
.Pp
-
There is no way to select a proxy at run-time other than setting the
-
.Ev HTTP_PROXY
-
or
-
.Ev FTP_PROXY
-
environment variables as appropriate.
-
.Pp
-
.Nm libfetch
-
does not understand or obey 305 (Use Proxy) replies.
-
.Pp
-
Error numbers are unique only within a certain context; the error
-
codes used for FTP and HTTP overlap, as do those used for resolver and
-
system errors.
-
For instance, error code 202 means "Command not
-
implemented, superfluous at this site" in an FTP context and
-
"Accepted" in an HTTP context.
-
.Pp
-
.Fn fetchStatFTP
-
does not check that the result of an MDTM command is a valid date.
-
.Pp
-
In case password protected keys are used for client certificate based
-
authentication the user is prompted for the password on each and every
-
fetch operation.
-
.Pp
-
The man page is incomplete, poorly written and produces badly
-
formatted text.
-
.Pp
-
The error reporting mechanism is unsatisfactory.
-
.Pp
-
Some parts of the code are not fully reentrant.
deleted external/libfetch/fetch.c
@@ -1,485 +0,0 @@
-
/*-
-
 * SPDX-License-Identifier: BSD-3-Clause
-
 *
-
 * Copyright (c) 1998-2004 Dag-Erling Smørgrav
-
 * 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
-
 *    in this position and unchanged.
-
 * 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.
-
 * 3. The name of the author may not be used to endorse or promote products
-
 *    derived from this software without specific prior written permission
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "bsd_compat.h"
-

-
#include <sys/param.h>
-

-
#include <netinet/in.h>
-

-
#include <errno.h>
-
#include <ctype.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-

-
#include "fetch.h"
-
#include "common.h"
-

-
auth_t	 fetchAuthMethod;
-
int	 fetchLastErrCode;
-
char	 fetchLastErrString[MAXERRSTRING];
-
int	 fetchTimeout;
-
int	 fetchRestartCalls = 1;
-
int	 fetchDebug;
-
const char	*fetchCustomHTTPHeaders;
-
int	 fetchSpeedLimit;
-
int	 fetchSpeedTime;
-

-

-
/*** Local data **************************************************************/
-

-
/*
-
 * Error messages for parser errors
-
 */
-
#define URL_MALFORMED		1
-
#define URL_BAD_SCHEME		2
-
#define URL_BAD_PORT		3
-
static struct fetcherr url_errlist[] = {
-
	{ URL_MALFORMED,	FETCH_URL,	"Malformed URL" },
-
	{ URL_BAD_SCHEME,	FETCH_URL,	"Invalid URL scheme" },
-
	{ URL_BAD_PORT,		FETCH_URL,	"Invalid server port" },
-
	{ -1,			FETCH_UNKNOWN,	"Unknown parser error" }
-
};
-

-

-
/*** Public API **************************************************************/
-

-
/*
-
 * Select the appropriate protocol for the URL scheme, and return a
-
 * read-only stream connected to the document referenced by the URL.
-
 * Also fill out the struct url_stat.
-
 */
-
FILE *
-
fetchXGet(struct url *URL, struct url_stat *us, const char *flags)
-
{
-

-
	if (us != NULL) {
-
		us->size = -1;
-
		us->atime = us->mtime = 0;
-
	}
-
	if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
-
		return (fetchXGetHTTP(URL, us, flags));
-
	else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
-
		return (fetchXGetHTTP(URL, us, flags));
-
	url_seterr(URL_BAD_SCHEME);
-
	return (NULL);
-
}
-

-
/*
-
 * Select the appropriate protocol for the URL scheme, and return a
-
 * read-only stream connected to the document referenced by the URL.
-
 */
-
FILE *
-
fetchGet(struct url *URL, const char *flags)
-
{
-
	return (fetchXGet(URL, NULL, flags));
-
}
-

-
/*
-
 * Select the appropriate protocol for the URL scheme, and return a
-
 * write-only stream connected to the document referenced by the URL.
-
 */
-
FILE *
-
fetchPut(struct url *URL, const char *flags)
-
{
-

-
	if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
-
		return (fetchPutHTTP(URL, flags));
-
	else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
-
		return (fetchPutHTTP(URL, flags));
-
	url_seterr(URL_BAD_SCHEME);
-
	return (NULL);
-
}
-

-
/*
-
 * Select the appropriate protocol for the URL scheme, and return the
-
 * size of the document referenced by the URL if it exists.
-
 */
-
int
-
fetchStat(struct url *URL, struct url_stat *us, const char *flags)
-
{
-

-
	if (us != NULL) {
-
		us->size = -1;
-
		us->atime = us->mtime = 0;
-
	}
-
	if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
-
		return (fetchStatHTTP(URL, us, flags));
-
	else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
-
		return (fetchStatHTTP(URL, us, flags));
-
	url_seterr(URL_BAD_SCHEME);
-
	return (-1);
-
}
-

-
/*
-
 * Select the appropriate protocol for the URL scheme, and return a
-
 * list of files in the directory pointed to by the URL.
-
 */
-
struct url_ent *
-
fetchList(struct url *URL, const char *flags)
-
{
-

-
	if (strcmp(URL->scheme, SCHEME_HTTP) == 0)
-
		return (fetchListHTTP(URL, flags));
-
	else if (strcmp(URL->scheme, SCHEME_HTTPS) == 0)
-
		return (fetchListHTTP(URL, flags));
-
	url_seterr(URL_BAD_SCHEME);
-
	return (NULL);
-
}
-

-
/*
-
 * Attempt to parse the given URL; if successful, call fetchXGet().
-
 */
-
FILE *
-
fetchXGetURL(const char *URL, struct url_stat *us, const char *flags)
-
{
-
	struct url *u;
-
	FILE *f;
-

-
	if ((u = fetchParseURL(URL)) == NULL)
-
		return (NULL);
-

-
	f = fetchXGet(u, us, flags);
-

-
	fetchFreeURL(u);
-
	return (f);
-
}
-

-
/*
-
 * Attempt to parse the given URL; if successful, call fetchGet().
-
 */
-
FILE *
-
fetchGetURL(const char *URL, const char *flags)
-
{
-
	return (fetchXGetURL(URL, NULL, flags));
-
}
-

-
/*
-
 * Attempt to parse the given URL; if successful, call fetchPut().
-
 */
-
FILE *
-
fetchPutURL(const char *URL, const char *flags)
-
{
-
	struct url *u;
-
	FILE *f;
-

-
	if ((u = fetchParseURL(URL)) == NULL)
-
		return (NULL);
-

-
	f = fetchPut(u, flags);
-

-
	fetchFreeURL(u);
-
	return (f);
-
}
-

-
/*
-
 * Attempt to parse the given URL; if successful, call fetchStat().
-
 */
-
int
-
fetchStatURL(const char *URL, struct url_stat *us, const char *flags)
-
{
-
	struct url *u;
-
	int s;
-

-
	if ((u = fetchParseURL(URL)) == NULL)
-
		return (-1);
-

-
	s = fetchStat(u, us, flags);
-

-
	fetchFreeURL(u);
-
	return (s);
-
}
-

-
/*
-
 * Attempt to parse the given URL; if successful, call fetchList().
-
 */
-
struct url_ent *
-
fetchListURL(const char *URL, const char *flags)
-
{
-
	struct url *u;
-
	struct url_ent *ue;
-

-
	if ((u = fetchParseURL(URL)) == NULL)
-
		return (NULL);
-

-
	ue = fetchList(u, flags);
-

-
	fetchFreeURL(u);
-
	return (ue);
-
}
-

-
/*
-
 * Make a URL
-
 */
-
struct url *
-
fetchMakeURL(const char *scheme, const char *host, int port, const char *doc,
-
    const char *user, const char *pwd)
-
{
-
	struct url *u;
-

-
	if (!scheme || (!host && !doc)) {
-
		url_seterr(URL_MALFORMED);
-
		return (NULL);
-
	}
-

-
	if (port < 0 || port > 65535) {
-
		url_seterr(URL_BAD_PORT);
-
		return (NULL);
-
	}
-

-
	/* allocate struct url */
-
	if ((u = calloc(1, sizeof(*u))) == NULL) {
-
		fetch_syserr();
-
		return (NULL);
-
	}
-
	u->netrcfd = -1;
-

-
	if ((u->doc = strdup(doc ? doc : "/")) == NULL) {
-
		fetch_syserr();
-
		free(u);
-
		return (NULL);
-
	}
-

-
#define seturl(x) snprintf(u->x, sizeof(u->x), "%s", x)
-
	seturl(scheme);
-
	seturl(host);
-
	seturl(user);
-
	seturl(pwd);
-
#undef seturl
-
	u->port = port;
-

-
	return (u);
-
}
-

-
/*
-
 * Return value of the given hex digit.
-
 */
-
static int
-
fetch_hexval(char ch)
-
{
-

-
	if (ch >= '0' && ch <= '9')
-
		return (ch - '0');
-
	else if (ch >= 'a' && ch <= 'f')
-
		return (ch - 'a' + 10);
-
	else if (ch >= 'A' && ch <= 'F')
-
		return (ch - 'A' + 10);
-
	return (-1);
-
}
-

-
/*
-
 * Decode percent-encoded URL component from src into dst, stopping at end
-
 * of string, or at @ or : separators.  Returns a pointer to the unhandled
-
 * part of the input string (null terminator, @, or :).  No terminator is
-
 * written to dst (it is the caller's responsibility).
-
 */
-
static const char *
-
fetch_pctdecode(char *dst, const char *src, size_t dlen)
-
{
-
	int d1, d2;
-
	char c;
-
	const char *s;
-

-
	for (s = src; *s != '\0' && *s != '@' && *s != ':'; s++) {
-
		if (s[0] == '%' && (d1 = fetch_hexval(s[1])) >= 0 &&
-
		    (d2 = fetch_hexval(s[2])) >= 0 && (d1 > 0 || d2 > 0)) {
-
			c = d1 << 4 | d2;
-
			s += 2;
-
		} else if (s[0] == '%') {
-
			/* Invalid escape sequence. */
-
			return (NULL);
-
		} else {
-
			c = *s;
-
		}
-
		if (dlen-- > 0)
-
			*dst++ = c;
-
		else
-
			return (NULL);
-
	}
-
	return (s);
-
}
-

-
/*
-
 * Split an URL into components. URL syntax is:
-
 * [method:/][/[user[:pwd]@]host[:port]/][document]
-
 * This almost, but not quite, RFC1738 URL syntax.
-
 */
-
struct url *
-
fetchParseURL(const char *URL)
-
{
-
	char *doc;
-
	const char *p, *q;
-
	struct url *u;
-
	int i, n;
-

-
	/* allocate struct url */
-
	if ((u = calloc(1, sizeof(*u))) == NULL) {
-
		fetch_syserr();
-
		return (NULL);
-
	}
-
	u->netrcfd = -1;
-

-
	/* scheme name */
-
	if ((p = strstr(URL, ":/"))) {
-
                if (p - URL > URL_SCHEMELEN)
-
                        goto ouch;
-
                for (i = 0; URL + i < p; i++)
-
                        u->scheme[i] = tolower((unsigned char)URL[i]);
-
		URL = ++p;
-
		/*
-
		 * Only one slash: no host, leave slash as part of document
-
		 * Two slashes: host follows, strip slashes
-
		 */
-
		if (URL[1] == '/')
-
			URL = (p += 2);
-
	} else {
-
		p = URL;
-
	}
-
	if (!*URL || *URL == '/' || *URL == '.' ||
-
	    (u->scheme[0] == '\0' &&
-
		strchr(URL, '/') == NULL && strchr(URL, ':') == NULL))
-
		goto nohost;
-

-
	p = strpbrk(URL, "/@");
-
	if (p && *p == '@') {
-
		/* username */
-
		q = fetch_pctdecode(u->user, URL, URL_USERLEN);
-
		if (q == NULL)
-
			goto ouch;
-

-
		/* password */
-
		if (*q == ':') {
-
			q = fetch_pctdecode(u->pwd, q + 1, URL_PWDLEN);
-
			if (q == NULL)
-
				goto ouch;
-
		}
-
		p++;
-
	} else {
-
		p = URL;
-
	}
-

-
	/* hostname */
-
	if (*p == '[') {
-
		q = p + 1 + strspn(p + 1, ":0123456789ABCDEFabcdef.");
-
		if (*q++ != ']')
-
			goto ouch;
-
	} else {
-
		/* valid characters in a DNS name */
-
		q = p + strspn(p, "-." "0123456789"
-
		    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "_"
-
		    "abcdefghijklmnopqrstuvwxyz");
-
	}
-
	if ((*q != '\0' && *q != '/' && *q != ':') || q - p > MAXHOSTNAMELEN)
-
		goto ouch;
-
	for (i = 0; p + i < q; i++)
-
		u->host[i] = tolower((unsigned char)p[i]);
-
	u->host[i] = '\0';
-
	p = q;
-

-
	/* port */
-
	if (*p == ':') {
-
		for (n = 0, q = ++p; *q && (*q != '/'); q++) {
-
			if (*q >= '0' && *q <= '9' && n < INT_MAX / 10) {
-
				n = n * 10 + (*q - '0');
-
			} else {
-
				/* invalid port */
-
				url_seterr(URL_BAD_PORT);
-
				goto ouch;
-
			}
-
		}
-
#ifndef IPPORT_MAX
-
#define IPPORT_MAX 65535
-
#endif
-
		if (p != q && (n < 1 || n > IPPORT_MAX))
-
			goto ouch;
-
		u->port = n;
-
		p = q;
-
	}
-

-
nohost:
-
	/* document */
-
	if (!*p)
-
		p = "/";
-

-
	if (strcmp(u->scheme, SCHEME_HTTP) == 0 ||
-
	    strcmp(u->scheme, SCHEME_HTTPS) == 0) {
-
		const char hexnums[] = "0123456789abcdef";
-

-
		/* percent-escape whitespace. */
-
		if ((doc = malloc(strlen(p) * 3 + 1)) == NULL) {
-
			fetch_syserr();
-
			goto ouch;
-
		}
-
		u->doc = doc;
-
		/* fragments are reserved for client-side processing, see
-
		 * https://www.rfc-editor.org/rfc/rfc9110.html#section-7.1
-
		 */
-
		while (*p != '\0' && *p != '#') {
-
			if (!isspace((unsigned char)*p)) {
-
				*doc++ = *p++;
-
			} else {
-
				*doc++ = '%';
-
				*doc++ = hexnums[((unsigned int)*p) >> 4];
-
				*doc++ = hexnums[((unsigned int)*p) & 0xf];
-
				p++;
-
			}
-
		}
-
		*doc = '\0';
-
	} else if ((u->doc = strdup(p)) == NULL) {
-
		fetch_syserr();
-
		goto ouch;
-
	}
-

-
	DEBUGF("scheme:   \"%s\"\n"
-
	    "user:     \"%s\"\n"
-
	    "password: \"%s\"\n"
-
	    "host:     \"%s\"\n"
-
	    "port:     \"%d\"\n"
-
	    "document: \"%s\"\n",
-
	    u->scheme, u->user, u->pwd,
-
	    u->host, u->port, u->doc);
-

-
	return (u);
-

-
ouch:
-
	free(u);
-
	return (NULL);
-
}
-

-
/*
-
 * Free a URL
-
 */
-
void
-
fetchFreeURL(struct url *u)
-
{
-
	free(u->doc);
-
	free(u);
-
}
deleted external/libfetch/fetch.h
@@ -1,159 +0,0 @@
-
/*-
-
 * SPDX-License-Identifier: BSD-3-Clause
-
 *
-
 * Copyright (c) 1998-2004 Dag-Erling Smørgrav
-
 * 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
-
 *    in this position and unchanged.
-
 * 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.
-
 * 3. The name of the author may not be used to endorse or promote products
-
 *    derived from this software without specific prior written permission
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
-
 */
-

-
#ifndef _FETCH_H_INCLUDED
-
#define _FETCH_H_INCLUDED
-

-
#define _LIBFETCH_VER "libfetch/2.0"
-

-
#define URL_SCHEMELEN 16
-
#define URL_USERLEN 256
-
#define URL_PWDLEN 256
-

-
struct url {
-
	char		 scheme[URL_SCHEMELEN+1];
-
	char		 user[URL_USERLEN+1];
-
	char		 pwd[URL_PWDLEN+1];
-
	char		 host[MAXHOSTNAMELEN+1];
-
	int		 port;
-
	char		*doc;
-
	off_t		 offset;
-
	size_t		 length;
-
	time_t		 ims_time;
-
	int		 netrcfd;
-
};
-

-
struct url_stat {
-
	off_t		 size;
-
	time_t		 atime;
-
	time_t		 mtime;
-
};
-

-
struct url_ent {
-
	char		 name[PATH_MAX];
-
	struct url_stat	 stat;
-
};
-

-
/* Recognized schemes */
-
#define SCHEME_FTP	"ftp"
-
#define SCHEME_HTTP	"http"
-
#define SCHEME_HTTPS	"https"
-
#define SCHEME_FILE	"file"
-

-
/* Error codes */
-
#define	FETCH_ABORT	 1
-
#define	FETCH_AUTH	 2
-
#define	FETCH_DOWN	 3
-
#define	FETCH_EXISTS	 4
-
#define	FETCH_FULL	 5
-
#define	FETCH_INFO	 6
-
#define	FETCH_MEMORY	 7
-
#define	FETCH_MOVED	 8
-
#define	FETCH_NETWORK	 9
-
#define	FETCH_OK	10
-
#define	FETCH_PROTO	11
-
#define	FETCH_RESOLV	12
-
#define	FETCH_SERVER	13
-
#define	FETCH_TEMP	14
-
#define	FETCH_TIMEOUT	15
-
#define	FETCH_UNAVAIL	16
-
#define	FETCH_UNKNOWN	17
-
#define	FETCH_URL	18
-
#define	FETCH_VERBOSE	19
-

-
#ifdef __cplusplus
-
extern "C" {
-
#endif
-

-
/* FILE-specific functions */
-
FILE		*fetchXGetFile(struct url *, struct url_stat *, const char *);
-
FILE		*fetchGetFile(struct url *, const char *);
-
FILE		*fetchPutFile(struct url *, const char *);
-
int		 fetchStatFile(struct url *, struct url_stat *, const char *);
-
struct url_ent	*fetchListFile(struct url *, const char *);
-

-
/* HTTP-specific functions */
-
FILE		*fetchXGetHTTP(struct url *, struct url_stat *, const char *);
-
FILE		*fetchGetHTTP(struct url *, const char *);
-
FILE		*fetchPutHTTP(struct url *, const char *);
-
int		 fetchStatHTTP(struct url *, struct url_stat *, const char *);
-
struct url_ent	*fetchListHTTP(struct url *, const char *);
-
FILE		*fetchReqHTTP(struct url *, const char *, const char *,
-
		    const char *, const char *);
-

-
/* Generic functions */
-
FILE		*fetchXGetURL(const char *, struct url_stat *, const char *);
-
FILE		*fetchGetURL(const char *, const char *);
-
FILE		*fetchPutURL(const char *, const char *);
-
int		 fetchStatURL(const char *, struct url_stat *, const char *);
-
struct url_ent	*fetchListURL(const char *, const char *);
-
FILE		*fetchXGet(struct url *, struct url_stat *, const char *);
-
FILE		*fetchGet(struct url *, const char *);
-
FILE		*fetchPut(struct url *, const char *);
-
int		 fetchStat(struct url *, struct url_stat *, const char *);
-
struct url_ent	*fetchList(struct url *, const char *);
-

-
/* URL parsing */
-
struct url	*fetchMakeURL(const char *, const char *, int,
-
		     const char *, const char *, const char *);
-
struct url	*fetchParseURL(const char *);
-
void		 fetchFreeURL(struct url *);
-

-
#ifdef __cplusplus
-
}
-
#endif
-

-
/* Authentication */
-
typedef int (*auth_t)(struct url *);
-
extern auth_t		 fetchAuthMethod;
-

-
/* Last error code */
-
extern int		 fetchLastErrCode;
-
#define MAXERRSTRING 256
-
extern char		 fetchLastErrString[MAXERRSTRING];
-

-
/* I/O timeout */
-
extern int		 fetchTimeout;
-

-
/* Restart interrupted syscalls */
-
extern int		 fetchRestartCalls;
-

-
/* Extra verbosity */
-
extern int		 fetchDebug;
-

-
/* Custom HTTP headers (newline-separated) */
-
extern const char	*fetchCustomHTTPHeaders;
-

-
/* Low speed limit: abort if speed drops below fetchSpeedLimit bytes/sec
-
 * for fetchSpeedTime consecutive seconds.  Both must be set to enable. */
-
extern int		 fetchSpeedLimit;
-
extern int		 fetchSpeedTime;
-

-
#endif
deleted external/libfetch/http.c
@@ -1,2328 +0,0 @@
-
/*-
-
 * SPDX-License-Identifier: BSD-3-Clause
-
 *
-
 * Copyright (c) 2000-2014 Dag-Erling Smørgrav
-
 * 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
-
 *    in this position and unchanged.
-
 * 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.
-
 * 3. The name of the author may not be used to endorse or promote products
-
 *    derived from this software without specific prior written permission.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 "bsd_compat.h"
-

-
#if !defined(TCP_NOPUSH) && defined(TCP_CORK)
-
#define TCP_NOPUSH TCP_CORK
-
#endif
-

-
/*
-
 * The following copyright applies to the base64 code:
-
 *
-
 *-
-
 * Copyright 1997 Massachusetts Institute of Technology
-
 *
-
 * Permission to use, copy, modify, and distribute this software and
-
 * its documentation for any purpose and without fee is hereby
-
 * granted, provided that both the above copyright notice and this
-
 * permission notice appear in all copies, that both the above
-
 * copyright notice and this permission notice appear in all
-
 * supporting documentation, and that the name of M.I.T. not be used
-
 * in advertising or publicity pertaining to distribution of the
-
 * software without specific, written prior permission.  M.I.T. makes
-
 * no representations about the suitability of this software for any
-
 * purpose.  It is provided "as is" without express or implied
-
 * warranty.
-
 *
-
 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
-
 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
-
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
-
 * SHALL M.I.T. 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/param.h>
-
#include <sys/socket.h>
-
#include <sys/time.h>
-

-
#include <ctype.h>
-
#include <err.h>
-
#include <errno.h>
-
#include <locale.h>
-
#include <netdb.h>
-
#include <stdarg.h>
-
#include <stdbool.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <time.h>
-
#include <unistd.h>
-

-
#ifdef WITH_SSL
-
#include <openssl/md5.h>
-
#define MD5Init(c) MD5_Init(c)
-
#define MD5Update(c, data, len) MD5_Update(c, data, len)
-
#define MD5Final(md, c) MD5_Final(md, c)
-
#else
-
#include <md5.h>
-
#endif
-

-
#include <netinet/in.h>
-
#include <netinet/tcp.h>
-

-
#include "fetch.h"
-
#include "common.h"
-
#include "httperr.h"
-

-
/* Maximum number of redirects to follow */
-
#define MAX_REDIRECT 20
-

-
/* Persistent connection cache */
-
static conn_t *http_cached_conn;
-

-
static void
-
http_cache_flush(void)
-
{
-
	if (http_cached_conn != NULL) {
-
		fetch_close(http_cached_conn);
-
		http_cached_conn = NULL;
-
	}
-
}
-

-
/* Symbolic names for reply codes we care about */
-
#define HTTP_OK			200
-
#define HTTP_PARTIAL		206
-
#define HTTP_MOVED_PERM		301
-
#define HTTP_MOVED_TEMP		302
-
#define HTTP_SEE_OTHER		303
-
#define HTTP_NOT_MODIFIED	304
-
#define HTTP_USE_PROXY		305
-
#define HTTP_TEMP_REDIRECT	307
-
#define HTTP_PERM_REDIRECT	308
-
#define HTTP_NEED_AUTH		401
-
#define HTTP_NEED_PROXY_AUTH	407
-
#define HTTP_BAD_RANGE		416
-
#define HTTP_PROTOCOL_ERROR	999
-

-
#define HTTP_REDIRECT(xyz) ((xyz) == HTTP_MOVED_PERM \
-
			    || (xyz) == HTTP_MOVED_TEMP \
-
			    || (xyz) == HTTP_TEMP_REDIRECT \
-
			    || (xyz) == HTTP_PERM_REDIRECT \
-
			    || (xyz) == HTTP_USE_PROXY \
-
			    || (xyz) == HTTP_SEE_OTHER)
-

-
#define HTTP_ERROR(xyz) ((xyz) >= 400 && (xyz) <= 599)
-

-

-
/*****************************************************************************
-
 * I/O functions for decoding chunked streams
-
 */
-

-
struct httpio
-
{
-
	conn_t		*conn;		/* connection */
-
	int		 chunked;	/* chunked mode */
-
	int		 keep_alive;	/* eligible for connection reuse */
-
	char		*buf;		/* chunk buffer */
-
	size_t		 bufsize;	/* size of chunk buffer */
-
	size_t		 buflen;	/* amount of data currently in buffer */
-
	size_t		 bufpos;	/* current read offset in buffer */
-
	int		 eof;		/* end-of-file flag */
-
	int		 error;		/* error flag */
-
	size_t		 chunksize;	/* remaining size of current chunk */
-
	off_t		 contentlength;	/* Content-Length or -1 if unknown */
-
	off_t		 bytes_read;	/* total bytes read from body */
-
#ifndef NDEBUG
-
	size_t		 total;
-
#endif
-
	/* low speed limit tracking */
-
	time_t		 lowspeed_start;  /* when slow period began (0=none) */
-
	size_t		 lowspeed_bytes;  /* bytes received since lowspeed_start */
-
};
-

-
/*
-
 * Get next chunk header
-
 */
-
static int
-
http_new_chunk(struct httpio *io)
-
{
-
	char *p;
-

-
	if (fetch_getln(io->conn) == -1)
-
		return (-1);
-

-
	if (io->conn->buflen < 2 || !isxdigit((unsigned char)*io->conn->buf))
-
		return (-1);
-

-
	for (p = io->conn->buf; *p && !isspace((unsigned char)*p); ++p) {
-
		if (*p == ';')
-
			break;
-
		if (!isxdigit((unsigned char)*p))
-
			return (-1);
-
		if (isdigit((unsigned char)*p)) {
-
			io->chunksize = io->chunksize * 16 +
-
			    *p - '0';
-
		} else {
-
			io->chunksize = io->chunksize * 16 +
-
			    10 + tolower((unsigned char)*p) - 'a';
-
		}
-
	}
-

-
#ifndef NDEBUG
-
	if (fetchDebug) {
-
		io->total += io->chunksize;
-
		if (io->chunksize == 0)
-
			fprintf(stderr, "%s(): end of last chunk\n", __func__);
-
		else
-
			fprintf(stderr, "%s(): new chunk: %lu (%lu)\n",
-
			    __func__, (unsigned long)io->chunksize,
-
			    (unsigned long)io->total);
-
	}
-
#endif
-

-
	return (io->chunksize);
-
}
-

-
/*
-
 * Grow the input buffer to at least len bytes
-
 */
-
static inline int
-
http_growbuf(struct httpio *io, size_t len)
-
{
-
	char *tmp;
-

-
	if (io->bufsize >= len)
-
		return (0);
-

-
	if ((tmp = realloc(io->buf, len)) == NULL)
-
		return (-1);
-
	io->buf = tmp;
-
	io->bufsize = len;
-
	return (0);
-
}
-

-
/*
-
 * Fill the input buffer, do chunk decoding on the fly
-
 */
-
static ssize_t
-
http_fillbuf(struct httpio *io, size_t len)
-
{
-
	ssize_t nbytes;
-
	char ch;
-

-
	if (io->error)
-
		return (-1);
-
	if (io->eof)
-
		return (0);
-

-
	/* not chunked: just fetch the requested amount */
-
	if (io->chunked == 0) {
-
		/* limit read to remaining content if Content-Length is known */
-
		if (io->contentlength >= 0) {
-
			off_t remaining = io->contentlength - io->bytes_read;
-
			if (remaining <= 0) {
-
				io->eof = 1;
-
				return (0);
-
			}
-
			if ((off_t)len > remaining)
-
				len = (size_t)remaining;
-
		}
-
		if (http_growbuf(io, len) == -1)
-
			return (-1);
-
		if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
-
			io->error = errno;
-
			return (-1);
-
		}
-
		if (nbytes == 0) {
-
			io->eof = 1;
-
			return (0);
-
		}
-
		io->bytes_read += nbytes;
-
		io->buflen = nbytes;
-
		io->bufpos = 0;
-
		return (io->buflen);
-
	}
-

-
	/* chunked, but we ran out: get the next chunk header */
-
	if (io->chunksize == 0) {
-
		switch (http_new_chunk(io)) {
-
		case -1:
-
			io->error = EPROTO;
-
			return (-1);
-
		case 0:
-
			io->eof = 1;
-
			return (0);
-
		}
-
	}
-

-
	/* fetch the requested amount, but no more than the current chunk */
-
	if (len > io->chunksize)
-
		len = io->chunksize;
-
	if (http_growbuf(io, len) == -1)
-
		return (-1);
-
	if ((nbytes = fetch_read(io->conn, io->buf, len)) == -1) {
-
		io->error = errno;
-
		return (-1);
-
	}
-
	io->bufpos = 0;
-
	io->buflen = nbytes;
-
	io->chunksize -= nbytes;
-

-
	if (io->chunksize == 0) {
-
		if (fetch_read(io->conn, &ch, 1) != 1 || ch != '\r' ||
-
		    fetch_read(io->conn, &ch, 1) != 1 || ch != '\n')
-
			return (-1);
-
	}
-

-
	return (io->buflen);
-
}
-

-
/*
-
 * Read function
-
 */
-
static int
-
http_readfn(void *v, char *buf, int len)
-
{
-
	struct httpio *io = (struct httpio *)v;
-
	int rlen;
-

-
	if (io->error)
-
		return (-1);
-
	if (io->eof)
-
		return (0);
-

-
	/* empty buffer */
-
	if (!io->buf || io->bufpos == io->buflen) {
-
		if ((rlen = http_fillbuf(io, len)) < 0) {
-
			if ((errno = io->error) == EINTR)
-
				io->error = 0;
-
			return (-1);
-
		} else if (rlen == 0) {
-
			return (0);
-
		}
-
	}
-

-
	rlen = io->buflen - io->bufpos;
-
	if (len < rlen)
-
		rlen = len;
-
	memcpy(buf, io->buf + io->bufpos, rlen);
-
	io->bufpos += rlen;
-

-
	/* low speed limit check */
-
	if (fetchSpeedLimit > 0 && fetchSpeedTime > 0) {
-
		time_t now = time(NULL);
-
		if (io->lowspeed_start == 0) {
-
			io->lowspeed_start = now;
-
			io->lowspeed_bytes = rlen;
-
		} else {
-
			io->lowspeed_bytes += rlen;
-
			time_t elapsed = now - io->lowspeed_start;
-
			if (elapsed >= fetchSpeedTime) {
-
				if ((off_t)io->lowspeed_bytes / elapsed <
-
				    fetchSpeedLimit) {
-
					io->error = ETIMEDOUT;
-
					errno = ETIMEDOUT;
-
					fetch_syserr();
-
					return (-1);
-
				}
-
				/* reset window */
-
				io->lowspeed_start = now;
-
				io->lowspeed_bytes = 0;
-
			}
-
		}
-
	}
-

-
	return (rlen);
-
}
-

-
/*
-
 * Write function
-
 */
-
static int
-
http_writefn(void *v, const char *buf, int len)
-
{
-
	struct httpio *io = (struct httpio *)v;
-

-
	return (fetch_write(io->conn, buf, len));
-
}
-

-
/*
-
 * Drain any unread response body data
-
 */
-
static void
-
http_drain(struct httpio *io)
-
{
-
	char buf[4096];
-

-
	while (!io->eof && !io->error) {
-
		if (http_fillbuf(io, sizeof(buf)) <= 0)
-
			break;
-
		io->bufpos = io->buflen;
-
	}
-
}
-

-
/*
-
 * Close function — caches the connection for reuse when possible
-
 */
-
static int
-
http_closefn(void *v)
-
{
-
	struct httpio *io = (struct httpio *)v;
-
	int r;
-

-
	if (io->keep_alive && !io->error) {
-
		/* drain unread body so the connection is at a clean state */
-
		http_drain(io);
-
		if (!io->error) {
-
			/* connection is reusable — cache it */
-
			if (http_cached_conn != NULL)
-
				fetch_close(http_cached_conn);
-
			http_cached_conn = io->conn;
-
			/* clear leftover line buffer */
-
			io->conn->buflen = 0;
-
			DEBUGF("cached connection to %s:%d\n",
-
			    io->conn->cache_host, io->conn->cache_port);
-
			if (io->buf)
-
				free(io->buf);
-
			free(io);
-
			return (0);
-
		}
-
	}
-
	r = fetch_close(io->conn);
-
	if (io->buf)
-
		free(io->buf);
-
	free(io);
-
	return (r);
-
}
-

-
/*
-
 * Wrap a file descriptor up
-
 */
-
static FILE *
-
http_funopen(conn_t *conn, int chunked, int keep_alive, off_t clength)
-
{
-
	struct httpio *io;
-
	FILE *f;
-

-
	if ((io = calloc(1, sizeof(*io))) == NULL) {
-
		fetch_syserr();
-
		return (NULL);
-
	}
-
	io->conn = conn;
-
	io->chunked = chunked;
-
	io->keep_alive = keep_alive;
-
	io->contentlength = clength;
-
	io->bytes_read = 0;
-
	f = funopen(io, http_readfn, http_writefn, NULL, http_closefn);
-
	if (f == NULL) {
-
		fetch_syserr();
-
		free(io);
-
		return (NULL);
-
	}
-
	return (f);
-
}
-

-

-
/*****************************************************************************
-
 * Helper functions for talking to the server and parsing its replies
-
 */
-

-
/* Header types */
-
typedef enum {
-
	hdr_syserror = -2,
-
	hdr_error = -1,
-
	hdr_end = 0,
-
	hdr_unknown = 1,
-
	hdr_content_length,
-
	hdr_content_range,
-
	hdr_last_modified,
-
	hdr_location,
-
	hdr_transfer_encoding,
-
	hdr_www_authenticate,
-
	hdr_proxy_authenticate,
-
	hdr_connection,
-
} hdr_t;
-

-
/* Names of interesting headers */
-
static struct {
-
	hdr_t		 num;
-
	const char	*name;
-
} hdr_names[] = {
-
	{ hdr_content_length,		"Content-Length" },
-
	{ hdr_content_range,		"Content-Range" },
-
	{ hdr_last_modified,		"Last-Modified" },
-
	{ hdr_location,			"Location" },
-
	{ hdr_transfer_encoding,	"Transfer-Encoding" },
-
	{ hdr_www_authenticate,		"WWW-Authenticate" },
-
	{ hdr_proxy_authenticate,	"Proxy-Authenticate" },
-
	{ hdr_connection,		"Connection" },
-
	{ hdr_unknown,			NULL },
-
};
-

-
/*
-
 * Send a formatted line; optionally echo to terminal
-
 */
-
static int
-
http_cmd(conn_t *conn, const char *fmt, ...)
-
{
-
	va_list ap;
-
	size_t len;
-
	char *msg;
-
	int r;
-

-
	va_start(ap, fmt);
-
	len = vasprintf(&msg, fmt, ap);
-
	va_end(ap);
-

-
	if (msg == NULL) {
-
		errno = ENOMEM;
-
		fetch_syserr();
-
		return (-1);
-
	}
-

-
	r = fetch_putln(conn, msg, len);
-
	free(msg);
-

-
	if (r == -1) {
-
		fetch_syserr();
-
		return (-1);
-
	}
-

-
	return (0);
-
}
-

-
/*
-
 * Get and parse status line
-
 */
-
static int
-
http_get_reply(conn_t *conn)
-
{
-
	char *p;
-

-
	if (fetch_getln(conn) == -1)
-
		return (-1);
-
	/*
-
	 * A valid status line looks like "HTTP/m.n xyz reason" where m
-
	 * and n are the major and minor protocol version numbers and xyz
-
	 * is the reply code.
-
	 * Unfortunately, there are servers out there (NCSA 1.5.1, to name
-
	 * just one) that do not send a version number, so we can't rely
-
	 * on finding one, but if we do, insist on it being 1.0 or 1.1.
-
	 * We don't care about the reason phrase.
-
	 */
-
	if (strncmp(conn->buf, "HTTP", 4) != 0)
-
		return (HTTP_PROTOCOL_ERROR);
-
	p = conn->buf + 4;
-
	if (*p == '/') {
-
		if (p[1] != '1' || p[2] != '.' || (p[3] != '0' && p[3] != '1'))
-
			return (HTTP_PROTOCOL_ERROR);
-
		p += 4;
-
	}
-
	if (*p != ' ' ||
-
	    !isdigit((unsigned char)p[1]) ||
-
	    !isdigit((unsigned char)p[2]) ||
-
	    !isdigit((unsigned char)p[3]))
-
		return (HTTP_PROTOCOL_ERROR);
-

-
	conn->err = (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0');
-
	return (conn->err);
-
}
-

-
/*
-
 * Check a header; if the type matches the given string, return a pointer
-
 * to the beginning of the value.
-
 */
-
static const char *
-
http_match(const char *str, const char *hdr)
-
{
-
	while (*str && *hdr &&
-
	    tolower((unsigned char)*str++) == tolower((unsigned char)*hdr++))
-
		/* nothing */;
-
	if (*str || *hdr != ':')
-
		return (NULL);
-
	while (*hdr && isspace((unsigned char)*++hdr))
-
		/* nothing */;
-
	return (hdr);
-
}
-

-

-
/*
-
 * Get the next header and return the appropriate symbolic code.  We
-
 * need to read one line ahead for checking for a continuation line
-
 * belonging to the current header (continuation lines start with
-
 * white space).
-
 *
-
 * We get called with a fresh line already in the conn buffer, either
-
 * from the previous http_next_header() invocation, or, the first
-
 * time, from a fetch_getln() performed by our caller.
-
 *
-
 * This stops when we encounter an empty line (we dont read beyond the header
-
 * area).
-
 *
-
 * Note that the "headerbuf" is just a place to return the result. Its
-
 * contents are not used for the next call. This means that no cleanup
-
 * is needed when ie doing another connection, just call the cleanup when
-
 * fully done to deallocate memory.
-
 */
-

-
/* Limit the max number of continuation lines to some reasonable value */
-
#define HTTP_MAX_CONT_LINES 10
-

-
/* Place into which to build a header from one or several lines */
-
typedef struct {
-
	char	*buf;		/* buffer */
-
	size_t	 bufsize;	/* buffer size */
-
	size_t	 buflen;	/* length of buffer contents */
-
} http_headerbuf_t;
-

-
static void
-
init_http_headerbuf(http_headerbuf_t *buf)
-
{
-
	buf->buf = NULL;
-
	buf->bufsize = 0;
-
	buf->buflen = 0;
-
}
-

-
static void
-
clean_http_headerbuf(http_headerbuf_t *buf)
-
{
-
	if (buf->buf)
-
		free(buf->buf);
-
	init_http_headerbuf(buf);
-
}
-

-
/* Remove whitespace at the end of the buffer */
-
static void
-
http_conn_trimright(conn_t *conn)
-
{
-
	while (conn->buflen &&
-
	       isspace((unsigned char)conn->buf[conn->buflen - 1]))
-
		conn->buflen--;
-
	conn->buf[conn->buflen] = '\0';
-
}
-

-
static hdr_t
-
http_next_header(conn_t *conn, http_headerbuf_t *hbuf, const char **p)
-
{
-
	unsigned int i, len;
-

-
	/*
-
	 * Have to do the stripping here because of the first line. So
-
	 * it's done twice for the subsequent lines. No big deal
-
	 */
-
	http_conn_trimright(conn);
-
	if (conn->buflen == 0)
-
		return (hdr_end);
-

-
	/* Copy the line to the headerbuf */
-
	if (hbuf->bufsize < conn->buflen + 1) {
-
		if ((hbuf->buf = realloc(hbuf->buf, conn->buflen + 1)) == NULL)
-
			return (hdr_syserror);
-
		hbuf->bufsize = conn->buflen + 1;
-
	}
-
	strcpy(hbuf->buf, conn->buf);
-
	hbuf->buflen = conn->buflen;
-

-
	/*
-
	 * Fetch possible continuation lines. Stop at 1st non-continuation
-
	 * and leave it in the conn buffer
-
	 */
-
	for (i = 0; i < HTTP_MAX_CONT_LINES; i++) {
-
		if (fetch_getln(conn) == -1)
-
			return (hdr_syserror);
-

-
		/*
-
		 * Note: we carry on the idea from the previous version
-
		 * that a pure whitespace line is equivalent to an empty
-
		 * one (so it's not continuation and will be handled when
-
		 * we are called next)
-
		 */
-
		http_conn_trimright(conn);
-
		if (conn->buf[0] != ' ' && conn->buf[0] != "\t"[0])
-
			break;
-

-
		/* Got a continuation line. Concatenate to previous */
-
		len = hbuf->buflen + conn->buflen;
-
		if (hbuf->bufsize < len + 1) {
-
			len *= 2;
-
			if ((hbuf->buf = realloc(hbuf->buf, len + 1)) == NULL)
-
				return (hdr_syserror);
-
			hbuf->bufsize = len + 1;
-
		}
-
		strcpy(hbuf->buf + hbuf->buflen, conn->buf);
-
		hbuf->buflen += conn->buflen;
-
	}
-

-
	/*
-
	 * We could check for malformed headers but we don't really care.
-
	 * A valid header starts with a token immediately followed by a
-
	 * colon; a token is any sequence of non-control, non-whitespace
-
	 * characters except "()<>@,;:\\\"{}".
-
	 */
-
	for (i = 0; hdr_names[i].num != hdr_unknown; i++)
-
		if ((*p = http_match(hdr_names[i].name, hbuf->buf)) != NULL)
-
			return (hdr_names[i].num);
-

-
	return (hdr_unknown);
-
}
-

-
/**************************
-
 * [Proxy-]Authenticate header parsing
-
 */
-

-
/*
-
 * Read doublequote-delimited string into output buffer obuf (allocated
-
 * by caller, whose responsibility it is to ensure that it's big enough)
-
 * cp points to the first char after the initial '"'
-
 * Handles \ quoting
-
 * Returns pointer to the first char after the terminating double quote, or
-
 * NULL for error.
-
 */
-
static const char *
-
http_parse_headerstring(const char *cp, char *obuf)
-
{
-
	for (;;) {
-
		switch (*cp) {
-
		case 0: /* Unterminated string */
-
			*obuf = 0;
-
			return (NULL);
-
		case '"': /* Ending quote */
-
			*obuf = 0;
-
			return (++cp);
-
		case '\\':
-
			if (*++cp == 0) {
-
				*obuf = 0;
-
				return (NULL);
-
			}
-
			/* FALLTHROUGH */
-
		default:
-
			*obuf++ = *cp++;
-
		}
-
	}
-
}
-

-
/* Http auth challenge schemes */
-
typedef enum {HTTPAS_UNKNOWN, HTTPAS_BASIC,HTTPAS_DIGEST} http_auth_schemes_t;
-

-
/* Data holder for a Basic or Digest challenge. */
-
typedef struct {
-
	http_auth_schemes_t scheme;
-
	char	*realm;
-
	char	*qop;
-
	char	*nonce;
-
	char	*opaque;
-
	char	*algo;
-
	int	 stale;
-
	int	 nc; /* Nonce count */
-
} http_auth_challenge_t;
-

-
static void
-
init_http_auth_challenge(http_auth_challenge_t *b)
-
{
-
	b->scheme = HTTPAS_UNKNOWN;
-
	b->realm = b->qop = b->nonce = b->opaque = b->algo = NULL;
-
	b->stale = b->nc = 0;
-
}
-

-
static void
-
clean_http_auth_challenge(http_auth_challenge_t *b)
-
{
-
	if (b->realm)
-
		free(b->realm);
-
	if (b->qop)
-
		free(b->qop);
-
	if (b->nonce)
-
		free(b->nonce);
-
	if (b->opaque)
-
		free(b->opaque);
-
	if (b->algo)
-
		free(b->algo);
-
	init_http_auth_challenge(b);
-
}
-

-
/* Data holder for an array of challenges offered in an http response. */
-
#define MAX_CHALLENGES 10
-
typedef struct {
-
	http_auth_challenge_t *challenges[MAX_CHALLENGES];
-
	int	count; /* Number of parsed challenges in the array */
-
	int	valid; /* We did parse an authenticate header */
-
} http_auth_challenges_t;
-

-
static void
-
init_http_auth_challenges(http_auth_challenges_t *cs)
-
{
-
	int i;
-
	for (i = 0; i < MAX_CHALLENGES; i++)
-
		cs->challenges[i] = NULL;
-
	cs->count = cs->valid = 0;
-
}
-

-
static void
-
clean_http_auth_challenges(http_auth_challenges_t *cs)
-
{
-
	int i;
-
	/* We rely on non-zero pointers being allocated, not on the count */
-
	for (i = 0; i < MAX_CHALLENGES; i++) {
-
		if (cs->challenges[i] != NULL) {
-
			clean_http_auth_challenge(cs->challenges[i]);
-
			free(cs->challenges[i]);
-
		}
-
	}
-
	init_http_auth_challenges(cs);
-
}
-

-
/*
-
 * Enumeration for lexical elements. Separators will be returned as their own
-
 * ascii value
-
 */
-
typedef enum {HTTPHL_WORD=256, HTTPHL_STRING=257, HTTPHL_END=258,
-
	      HTTPHL_ERROR = 259} http_header_lex_t;
-

-
/*
-
 * Determine what kind of token comes next and return possible value
-
 * in buf, which is supposed to have been allocated big enough by
-
 * caller. Advance input pointer and return element type.
-
 */
-
static int
-
http_header_lex(const char **cpp, char *buf)
-
{
-
	size_t l;
-
	/* Eat initial whitespace */
-
	*cpp += strspn(*cpp, " \t");
-
	if (**cpp == 0)
-
		return (HTTPHL_END);
-

-
	/* Separator ? */
-
	if (**cpp == ',' || **cpp == '=')
-
		return (*((*cpp)++));
-

-
	/* String ? */
-
	if (**cpp == '"') {
-
		*cpp = http_parse_headerstring(++*cpp, buf);
-
		if (*cpp == NULL)
-
			return (HTTPHL_ERROR);
-
		return (HTTPHL_STRING);
-
	}
-

-
	/* Read other token, until separator or whitespace */
-
	l = strcspn(*cpp, " \t,=");
-
	memcpy(buf, *cpp, l);
-
	buf[l] = 0;
-
	*cpp += l;
-
	return (HTTPHL_WORD);
-
}
-

-
/*
-
 * Read challenges from http xxx-authenticate header and accumulate them
-
 * in the challenges list structure.
-
 *
-
 * Headers with multiple challenges are specified by rfc2617, but
-
 * servers (ie: squid) often send them in separate headers instead,
-
 * which in turn is forbidden by the http spec (multiple headers with
-
 * the same name are only allowed for pure comma-separated lists, see
-
 * rfc2616 sec 4.2).
-
 *
-
 * We support both approaches anyway
-
 */
-
static int
-
http_parse_authenticate(const char *cp, http_auth_challenges_t *cs)
-
{
-
	int ret = -1;
-
	http_header_lex_t lex;
-
	char *key = malloc(strlen(cp) + 1);
-
	char *value = malloc(strlen(cp) + 1);
-
	char *buf = malloc(strlen(cp) + 1);
-

-
	if (key == NULL || value == NULL || buf == NULL) {
-
		fetch_syserr();
-
		goto out;
-
	}
-

-
	/* In any case we've seen the header and we set the valid bit */
-
	cs->valid = 1;
-

-
	/* Need word first */
-
	lex = http_header_lex(&cp, key);
-
	if (lex != HTTPHL_WORD)
-
		goto out;
-

-
	/* Loop on challenges */
-
	for (; cs->count < MAX_CHALLENGES; cs->count++) {
-
		cs->challenges[cs->count] =
-
			malloc(sizeof(http_auth_challenge_t));
-
		if (cs->challenges[cs->count] == NULL) {
-
			fetch_syserr();
-
			goto out;
-
		}
-
		init_http_auth_challenge(cs->challenges[cs->count]);
-
		if (strcasecmp(key, "basic") == 0) {
-
			cs->challenges[cs->count]->scheme = HTTPAS_BASIC;
-
		} else if (strcasecmp(key, "digest") == 0) {
-
			cs->challenges[cs->count]->scheme = HTTPAS_DIGEST;
-
		} else {
-
			cs->challenges[cs->count]->scheme = HTTPAS_UNKNOWN;
-
			/*
-
			 * Continue parsing as basic or digest may
-
			 * follow, and the syntax is the same for
-
			 * all. We'll just ignore this one when
-
			 * looking at the list
-
			 */
-
		}
-

-
		/* Loop on attributes */
-
		for (;;) {
-
			/* Key */
-
			lex = http_header_lex(&cp, key);
-
			if (lex != HTTPHL_WORD)
-
				goto out;
-

-
			/* Equal sign */
-
			lex = http_header_lex(&cp, buf);
-
			if (lex != '=')
-
				goto out;
-

-
			/* Value */
-
			lex = http_header_lex(&cp, value);
-
			if (lex != HTTPHL_WORD && lex != HTTPHL_STRING)
-
				goto out;
-

-
			if (strcasecmp(key, "realm") == 0) {
-
				cs->challenges[cs->count]->realm =
-
				    strdup(value);
-
			} else if (strcasecmp(key, "qop") == 0) {
-
				cs->challenges[cs->count]->qop =
-
				    strdup(value);
-
			} else if (strcasecmp(key, "nonce") == 0) {
-
				cs->challenges[cs->count]->nonce =
-
				    strdup(value);
-
			} else if (strcasecmp(key, "opaque") == 0) {
-
				cs->challenges[cs->count]->opaque =
-
				    strdup(value);
-
			} else if (strcasecmp(key, "algorithm") == 0) {
-
				cs->challenges[cs->count]->algo =
-
				    strdup(value);
-
			} else if (strcasecmp(key, "stale") == 0) {
-
				cs->challenges[cs->count]->stale =
-
				    strcasecmp(value, "no");
-
			} else {
-
				/* ignore unknown attributes */
-
			}
-

-
			/* Comma or Next challenge or End */
-
			lex = http_header_lex(&cp, key);
-
			/*
-
			 * If we get a word here, this is the beginning of the
-
			 * next challenge. Break the attributes loop
-
			 */
-
			if (lex == HTTPHL_WORD)
-
				break;
-

-
			if (lex == HTTPHL_END) {
-
				/* End while looking for ',' is normal exit */
-
				cs->count++;
-
				ret = 0;
-
				goto out;
-
			}
-
			/* Anything else is an error */
-
			if (lex != ',')
-
				goto out;
-

-
		} /* End attributes loop */
-
	} /* End challenge loop */
-

-
	/*
-
	 * Challenges max count exceeded. This really can't happen
-
	 * with normal data, something's fishy -> error
-
	 */
-

-
out:
-
	if (key)
-
		free(key);
-
	if (value)
-
		free(value);
-
	if (buf)
-
		free(buf);
-
	return (ret);
-
}
-

-

-
/*
-
 * Parse a last-modified header
-
 */
-
static int
-
http_parse_mtime(const char *p, time_t *mtime)
-
{
-
	char locale[64], *r;
-
	struct tm tm;
-

-
	memset(&tm, 0, sizeof(tm));
-
	strlcpy(locale, setlocale(LC_TIME, NULL), sizeof(locale));
-
	setlocale(LC_TIME, "C");
-
	r = strptime(p, "%a, %d %b %Y %H:%M:%S GMT", &tm);
-
	/*
-
	 * Some proxies use UTC in response, but it should still be
-
	 * parsed. RFC2616 states GMT and UTC are exactly equal for HTTP.
-
	 */
-
	if (r == NULL)
-
		r = strptime(p, "%a, %d %b %Y %H:%M:%S UTC", &tm);
-
	/* XXX should add support for date-2 and date-3 */
-
	setlocale(LC_TIME, locale);
-
	if (r == NULL)
-
		return (-1);
-
	DEBUGF("last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
-
	    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
-
	    tm.tm_hour, tm.tm_min, tm.tm_sec);
-
	*mtime = timegm(&tm);
-
	return (0);
-
}
-

-
/*
-
 * Parse a content-length header
-
 */
-
static int
-
http_parse_length(const char *p, off_t *length)
-
{
-
	off_t len;
-

-
	for (len = 0; *p && isdigit((unsigned char)*p); ++p)
-
		len = len * 10 + (*p - '0');
-
	if (*p)
-
		return (-1);
-
	DEBUGF("content length: [%lld]\n", (long long)len);
-
	*length = len;
-
	return (0);
-
}
-

-
/*
-
 * Parse a content-range header
-
 */
-
static int
-
http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size)
-
{
-
	off_t first, last, len;
-

-
	if (strncasecmp(p, "bytes ", 6) != 0)
-
		return (-1);
-
	p += 6;
-
	if (*p == '*') {
-
		first = last = -1;
-
		++p;
-
	} else {
-
		for (first = 0; *p && isdigit((unsigned char)*p); ++p)
-
			first = first * 10 + *p - '0';
-
		if (*p != '-')
-
			return (-1);
-
		for (last = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
-
			last = last * 10 + *p - '0';
-
	}
-
	if (first > last || *p != '/')
-
		return (-1);
-
	for (len = 0, ++p; *p && isdigit((unsigned char)*p); ++p)
-
		len = len * 10 + *p - '0';
-
	if (*p || len < last - first + 1)
-
		return (-1);
-
	if (first == -1) {
-
		DEBUGF("content range: [*/%lld]\n", (long long)len);
-
		*length = 0;
-
	} else {
-
		DEBUGF("content range: [%lld-%lld/%lld]\n",
-
		    (long long)first, (long long)last, (long long)len);
-
		*length = last - first + 1;
-
	}
-
	*offset = first;
-
	*size = len;
-
	return (0);
-
}
-

-

-
/*****************************************************************************
-
 * Helper functions for authorization
-
 */
-

-
/*
-
 * Base64 encoding
-
 */
-
static char *
-
http_base64(const char *src)
-
{
-
	static const char base64[] =
-
	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-
	    "abcdefghijklmnopqrstuvwxyz"
-
	    "0123456789+/";
-
	char *str, *dst;
-
	size_t l;
-
	int t;
-

-
	l = strlen(src);
-
	if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
-
		return (NULL);
-
	dst = str;
-

-
	while (l >= 3) {
-
		t = (src[0] << 16) | (src[1] << 8) | src[2];
-
		dst[0] = base64[(t >> 18) & 0x3f];
-
		dst[1] = base64[(t >> 12) & 0x3f];
-
		dst[2] = base64[(t >> 6) & 0x3f];
-
		dst[3] = base64[(t >> 0) & 0x3f];
-
		src += 3; l -= 3;
-
		dst += 4;
-
	}
-

-
	switch (l) {
-
	case 2:
-
		t = (src[0] << 16) | (src[1] << 8);
-
		dst[0] = base64[(t >> 18) & 0x3f];
-
		dst[1] = base64[(t >> 12) & 0x3f];
-
		dst[2] = base64[(t >> 6) & 0x3f];
-
		dst[3] = '=';
-
		dst += 4;
-
		break;
-
	case 1:
-
		t = src[0] << 16;
-
		dst[0] = base64[(t >> 18) & 0x3f];
-
		dst[1] = base64[(t >> 12) & 0x3f];
-
		dst[2] = dst[3] = '=';
-
		dst += 4;
-
		break;
-
	case 0:
-
		break;
-
	}
-

-
	*dst = 0;
-
	return (str);
-
}
-

-

-
/*
-
 * Extract authorization parameters from environment value.
-
 * The value is like scheme:realm:user:pass
-
 */
-
typedef struct {
-
	char	*scheme;
-
	char	*realm;
-
	char	*user;
-
	char	*password;
-
} http_auth_params_t;
-

-
static void
-
init_http_auth_params(http_auth_params_t *s)
-
{
-
	s->scheme = s->realm = s->user = s->password = NULL;
-
}
-

-
static void
-
clean_http_auth_params(http_auth_params_t *s)
-
{
-
	if (s->scheme)
-
		free(s->scheme);
-
	if (s->realm)
-
		free(s->realm);
-
	if (s->user)
-
		free(s->user);
-
	if (s->password)
-
		free(s->password);
-
	init_http_auth_params(s);
-
}
-

-
static int
-
http_authfromenv(const char *p, http_auth_params_t *parms)
-
{
-
	int ret = -1;
-
	char *v, *ve;
-
	char *str = strdup(p);
-

-
	if (str == NULL) {
-
		fetch_syserr();
-
		return (-1);
-
	}
-
	v = str;
-

-
	if ((ve = strchr(v, ':')) == NULL)
-
		goto out;
-

-
	*ve = 0;
-
	if ((parms->scheme = strdup(v)) == NULL) {
-
		fetch_syserr();
-
		goto out;
-
	}
-
	v = ve + 1;
-

-
	if ((ve = strchr(v, ':')) == NULL)
-
		goto out;
-

-
	*ve = 0;
-
	if ((parms->realm = strdup(v)) == NULL) {
-
		fetch_syserr();
-
		goto out;
-
	}
-
	v = ve + 1;
-

-
	if ((ve = strchr(v, ':')) == NULL)
-
		goto out;
-

-
	*ve = 0;
-
	if ((parms->user = strdup(v)) == NULL) {
-
		fetch_syserr();
-
		goto out;
-
	}
-
	v = ve + 1;
-

-

-
	if ((parms->password = strdup(v)) == NULL) {
-
		fetch_syserr();
-
		goto out;
-
	}
-
	ret = 0;
-
out:
-
	if (ret == -1)
-
		clean_http_auth_params(parms);
-
	if (str)
-
		free(str);
-
	return (ret);
-
}
-

-

-
/*
-
 * Digest response: the code to compute the digest is taken from the
-
 * sample implementation in RFC2616
-
 */
-
#define IN const
-
#define OUT
-

-
#define HASHLEN 16
-
typedef char HASH[HASHLEN];
-
#define HASHHEXLEN 32
-
typedef char HASHHEX[HASHHEXLEN+1];
-

-
static const char *hexchars = "0123456789abcdef";
-
static void
-
CvtHex(IN HASH Bin, OUT HASHHEX Hex)
-
{
-
	unsigned short i;
-
	unsigned char j;
-

-
	for (i = 0; i < HASHLEN; i++) {
-
		j = (Bin[i] >> 4) & 0xf;
-
		Hex[i*2] = hexchars[j];
-
		j = Bin[i] & 0xf;
-
		Hex[i*2+1] = hexchars[j];
-
	}
-
	Hex[HASHHEXLEN] = '\0';
-
};
-

-
/* calculate H(A1) as per spec */
-
static void
-
DigestCalcHA1(
-
	IN char * pszAlg,
-
	IN char * pszUserName,
-
	IN char * pszRealm,
-
	IN char * pszPassword,
-
	IN char * pszNonce,
-
	IN char * pszCNonce,
-
	OUT HASHHEX SessionKey
-
	)
-
{
-
	MD5_CTX Md5Ctx;
-
	HASH HA1;
-

-
	MD5Init(&Md5Ctx);
-
	MD5Update(&Md5Ctx, pszUserName, strlen(pszUserName));
-
	MD5Update(&Md5Ctx, ":", 1);
-
	MD5Update(&Md5Ctx, pszRealm, strlen(pszRealm));
-
	MD5Update(&Md5Ctx, ":", 1);
-
	MD5Update(&Md5Ctx, pszPassword, strlen(pszPassword));
-
	MD5Final(HA1, &Md5Ctx);
-
	if (strcasecmp(pszAlg, "md5-sess") == 0) {
-

-
		MD5Init(&Md5Ctx);
-
		MD5Update(&Md5Ctx, HA1, HASHLEN);
-
		MD5Update(&Md5Ctx, ":", 1);
-
		MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
-
		MD5Update(&Md5Ctx, ":", 1);
-
		MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
-
		MD5Final(HA1, &Md5Ctx);
-
	}
-
	CvtHex(HA1, SessionKey);
-
}
-

-
/* calculate request-digest/response-digest as per HTTP Digest spec */
-
static void
-
DigestCalcResponse(
-
	IN HASHHEX HA1,           /* H(A1) */
-
	IN char * pszNonce,       /* nonce from server */
-
	IN char * pszNonceCount,  /* 8 hex digits */
-
	IN char * pszCNonce,      /* client nonce */
-
	IN char * pszQop,         /* qop-value: "", "auth", "auth-int" */
-
	IN char * pszMethod,      /* method from the request */
-
	IN char * pszDigestUri,   /* requested URL */
-
	IN HASHHEX HEntity,       /* H(entity body) if qop="auth-int" */
-
	OUT HASHHEX Response      /* request-digest or response-digest */
-
	)
-
{
-
#if 0
-
	DEBUGF("Calc: HA1[%s] Nonce[%s] qop[%s] method[%s] URI[%s]\n",
-
	    HA1, pszNonce, pszQop, pszMethod, pszDigestUri);
-
#endif
-
	MD5_CTX Md5Ctx;
-
	HASH HA2;
-
	HASH RespHash;
-
	HASHHEX HA2Hex;
-

-
	// calculate H(A2)
-
	MD5Init(&Md5Ctx);
-
	MD5Update(&Md5Ctx, pszMethod, strlen(pszMethod));
-
	MD5Update(&Md5Ctx, ":", 1);
-
	MD5Update(&Md5Ctx, pszDigestUri, strlen(pszDigestUri));
-
	if (strcasecmp(pszQop, "auth-int") == 0) {
-
		MD5Update(&Md5Ctx, ":", 1);
-
		MD5Update(&Md5Ctx, HEntity, HASHHEXLEN);
-
	}
-
	MD5Final(HA2, &Md5Ctx);
-
	CvtHex(HA2, HA2Hex);
-

-
	// calculate response
-
	MD5Init(&Md5Ctx);
-
	MD5Update(&Md5Ctx, HA1, HASHHEXLEN);
-
	MD5Update(&Md5Ctx, ":", 1);
-
	MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
-
	MD5Update(&Md5Ctx, ":", 1);
-
	if (*pszQop) {
-
		MD5Update(&Md5Ctx, pszNonceCount, strlen(pszNonceCount));
-
		MD5Update(&Md5Ctx, ":", 1);
-
		MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
-
		MD5Update(&Md5Ctx, ":", 1);
-
		MD5Update(&Md5Ctx, pszQop, strlen(pszQop));
-
		MD5Update(&Md5Ctx, ":", 1);
-
	}
-
	MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
-
	MD5Final(RespHash, &Md5Ctx);
-
	CvtHex(RespHash, Response);
-
}
-

-
/*
-
 * Generate/Send a Digest authorization header
-
 * This looks like: [Proxy-]Authorization: credentials
-
 *
-
 *  credentials      = "Digest" digest-response
-
 *  digest-response  = 1#( username | realm | nonce | digest-uri
-
 *                      | response | [ algorithm ] | [cnonce] |
-
 *                      [opaque] | [message-qop] |
-
 *                          [nonce-count]  | [auth-param] )
-
 *  username         = "username" "=" username-value
-
 *  username-value   = quoted-string
-
 *  digest-uri       = "uri" "=" digest-uri-value
-
 *  digest-uri-value = request-uri   ; As specified by HTTP/1.1
-
 *  message-qop      = "qop" "=" qop-value
-
 *  cnonce           = "cnonce" "=" cnonce-value
-
 *  cnonce-value     = nonce-value
-
 *  nonce-count      = "nc" "=" nc-value
-
 *  nc-value         = 8LHEX
-
 *  response         = "response" "=" request-digest
-
 *  request-digest = <"> 32LHEX <">
-
 */
-
static int
-
http_digest_auth(conn_t *conn, const char *hdr, http_auth_challenge_t *c,
-
		 http_auth_params_t *parms, struct url *url)
-
{
-
	int r;
-
	char noncecount[10];
-
	char cnonce[40];
-
	char *options = NULL;
-

-
	if (!c->realm || !c->nonce) {
-
		DEBUGF("realm/nonce not set in challenge\n");
-
		return(-1);
-
	}
-
	if (!c->algo)
-
		c->algo = strdup("");
-

-
	if (asprintf(&options, "%s%s%s%s",
-
	    *c->algo? ",algorithm=" : "", c->algo,
-
	    c->opaque? ",opaque=" : "", c->opaque?c->opaque:"") < 0)
-
		return (-1);
-

-
	if (!c->qop) {
-
		c->qop = strdup("");
-
		*noncecount = 0;
-
		*cnonce = 0;
-
	} else {
-
		c->nc++;
-
		sprintf(noncecount, "%08x", c->nc);
-
		/* We don't try very hard with the cnonce ... */
-
		sprintf(cnonce, "%x%lx", getpid(), (unsigned long)time(0));
-
	}
-

-
	HASHHEX HA1;
-
	DigestCalcHA1(c->algo, parms->user, c->realm,
-
		      parms->password, c->nonce, cnonce, HA1);
-
	DEBUGF("HA1: [%s]\n", HA1);
-
	HASHHEX digest, null;
-
	memset(null, 0, sizeof(null));
-
	DigestCalcResponse(HA1, c->nonce, noncecount, cnonce, c->qop,
-
			   "GET", url->doc, null, digest);
-

-
	if (c->qop[0]) {
-
		r = http_cmd(conn, "%s: Digest username=\"%s\",realm=\"%s\","
-
			     "nonce=\"%s\",uri=\"%s\",response=\"%s\","
-
			     "qop=\"auth\", cnonce=\"%s\", nc=%s%s",
-
			     hdr, parms->user, c->realm,
-
			     c->nonce, url->doc, digest,
-
			     cnonce, noncecount, options);
-
	} else {
-
		r = http_cmd(conn, "%s: Digest username=\"%s\",realm=\"%s\","
-
			     "nonce=\"%s\",uri=\"%s\",response=\"%s\"%s",
-
			     hdr, parms->user, c->realm,
-
			     c->nonce, url->doc, digest, options);
-
	}
-
	if (options)
-
		free(options);
-
	return (r);
-
}
-

-
/*
-
 * Encode username and password
-
 */
-
static int
-
http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd)
-
{
-
	char *upw, *auth;
-
	int r;
-

-
	DEBUGF("basic: usr: [%s]\n", usr);
-
	DEBUGF("basic: pwd: [%s]\n", pwd);
-
	if (asprintf(&upw, "%s:%s", usr, pwd) == -1)
-
		return (-1);
-
	auth = http_base64(upw);
-
	free(upw);
-
	if (auth == NULL)
-
		return (-1);
-
	r = http_cmd(conn, "%s: Basic %s", hdr, auth);
-
	free(auth);
-
	return (r);
-
}
-

-
/*
-
 * Chose the challenge to answer and call the appropriate routine to
-
 * produce the header.
-
 */
-
static int
-
http_authorize(conn_t *conn, const char *hdr, http_auth_challenges_t *cs,
-
	       http_auth_params_t *parms, struct url *url)
-
{
-
	http_auth_challenge_t *digest = NULL;
-
	int i;
-

-
	/* If user or pass are null we're not happy */
-
	if (!parms->user || !parms->password) {
-
		DEBUGF("NULL usr or pass\n");
-
		return (-1);
-
	}
-

-
	/* Look for a Digest */
-
	for (i = 0; i < cs->count; i++) {
-
		if (cs->challenges[i]->scheme == HTTPAS_DIGEST)
-
			digest = cs->challenges[i];
-
	}
-

-
	/* Error if "Digest" was specified and there is no Digest challenge */
-
	if (!digest &&
-
	    (parms->scheme && strcasecmp(parms->scheme, "digest") == 0)) {
-
		DEBUGF("Digest auth in env, not supported by peer\n");
-
		return (-1);
-
	}
-
	/*
-
	 * If "basic" was specified in the environment, or there is no Digest
-
	 * challenge, do the basic thing. Don't need a challenge for this,
-
	 * so no need to check basic!=NULL
-
	 */
-
	if (!digest ||
-
	    (parms->scheme && strcasecmp(parms->scheme, "basic") == 0))
-
		return (http_basic_auth(conn,hdr,parms->user,parms->password));
-

-
	/* Else, prefer digest. We just checked that it's not NULL */
-
	return (http_digest_auth(conn, hdr, digest, parms, url));
-
}
-

-
/*
-
 * Send custom headers from a newline-separated string.
-
 * Each line should be a complete "Header-Name: value" pair.
-
 * Empty lines and lines without a colon are silently skipped.
-
 */
-
static void
-
http_custom_headers(conn_t *conn, const char *hdrs)
-
{
-
	const char *p, *eol;
-
	size_t len;
-

-
	for (p = hdrs; *p != '\0'; p = eol) {
-
		eol = strpbrk(p, "\r\n");
-
		if (eol == NULL)
-
			eol = p + strlen(p);
-
		len = eol - p;
-
		/* skip empty lines and lines without ':' */
-
		if (len > 0 && memchr(p, ':', len) != NULL)
-
			http_cmd(conn, "%.*s", (int)len, p);
-
		/* skip past \r\n or \n */
-
		if (*eol == '\r')
-
			eol++;
-
		if (*eol == '\n')
-
			eol++;
-
	}
-
}
-

-

-
/*****************************************************************************
-
 * Helper functions for connecting to a server or proxy
-
 */
-

-
/*
-
 * Connect to the correct HTTP server or proxy.
-
 */
-
static conn_t *
-
http_connect(struct url *URL, struct url *purl, const char *flags)
-
{
-
	struct url *curl;
-
	conn_t *conn;
-
	hdr_t h;
-
	http_headerbuf_t headerbuf;
-
	const char *p;
-
	int verbose;
-
	int af, val;
-
	int serrno;
-
	bool isproxyauth = false;
-
	http_auth_challenges_t proxy_challenges;
-

-
#ifdef INET6
-
	af = AF_UNSPEC;
-
#else
-
	af = AF_INET;
-
#endif
-

-
	verbose = CHECK_FLAG('v');
-
	if (CHECK_FLAG('4'))
-
		af = AF_INET;
-
#ifdef INET6
-
	else if (CHECK_FLAG('6'))
-
		af = AF_INET6;
-
#endif
-

-
	curl = (purl != NULL) ? purl : URL;
-

-
	/*
-
	 * Try to reuse a cached connection.  Only for direct (non-proxy)
-
	 * connections where host, port, and scheme match.
-
	 */
-
	if (purl == NULL && http_cached_conn != NULL) {
-
		int is_ssl = (strcmp(URL->scheme, SCHEME_HTTPS) == 0);
-
		if (strcmp(http_cached_conn->cache_host, URL->host) == 0 &&
-
		    http_cached_conn->cache_port == URL->port &&
-
		    http_cached_conn->cache_ssl == is_ssl) {
-
			conn = http_cached_conn;
-
			http_cached_conn = NULL;
-
			DEBUGF("reusing cached connection to %s:%d\n",
-
			    URL->host, URL->port);
-
			val = 1;
-
#ifdef TCP_NOPUSH
-
			setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH,
-
			    &val, sizeof(val));
-
#endif
-
			return (conn);
-
		}
-
		/* stale cache — different host */
-
		http_cache_flush();
-
	}
-

-
retry:
-
	if ((conn = fetch_connect(curl->host, curl->port, af, verbose)) == NULL)
-
		/* fetch_connect() has already set an error code */
-
		return (NULL);
-

-
	/* record connection identity for later cache matching */
-
	strlcpy(conn->cache_host, URL->host, sizeof(conn->cache_host));
-
	conn->cache_port = URL->port;
-
	conn->cache_ssl = (strcmp(URL->scheme, SCHEME_HTTPS) == 0);
-
	init_http_headerbuf(&headerbuf);
-
	if (strcmp(URL->scheme, SCHEME_HTTPS) == 0 && purl) {
-
		init_http_auth_challenges(&proxy_challenges);
-
		http_cmd(conn, "CONNECT %s:%d HTTP/1.1", URL->host, URL->port);
-
		http_cmd(conn, "Host: %s:%d", URL->host, URL->port);
-
		if (isproxyauth) {
-
			http_auth_params_t aparams;
-
			init_http_auth_params(&aparams);
-
			if (*purl->user || *purl->pwd) {
-
				aparams.user = strdup(purl->user);
-
				aparams.password = strdup(purl->pwd);
-
			} else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL &&
-
				    *p != '\0') {
-
				if (http_authfromenv(p, &aparams) < 0) {
-
					http_seterr(HTTP_NEED_PROXY_AUTH);
-
					fetch_syserr();
-
					goto ouch;
-
				}
-
			} else if (fetch_netrc_auth(purl) == 0) {
-
				aparams.user = strdup(purl->user);
-
				aparams.password = strdup(purl->pwd);
-
			} else {
-
				/*
-
				 * No auth information found in system - exiting
-
				 * with warning.
-
				 */
-
				warnx("Missing username and/or password set");
-
				fetch_syserr();
-
				goto ouch;
-
			}
-
			http_authorize(conn, "Proxy-Authorization",
-
			    &proxy_challenges, &aparams, purl);
-
			clean_http_auth_params(&aparams);
-
		}
-
		http_cmd(conn, "");
-
		/* Get reply from CONNECT Tunnel attempt */
-
		int httpreply = http_get_reply(conn);
-
		if (httpreply != HTTP_OK) {
-
			http_seterr(httpreply);
-
			/* If the error is a 407/HTTP_NEED_PROXY_AUTH */
-
			if (httpreply == HTTP_NEED_PROXY_AUTH &&
-
			    ! isproxyauth) {
-
				/* Try again with authentication. */
-
				clean_http_headerbuf(&headerbuf);
-
				fetch_close(conn);
-
				isproxyauth = true;
-
				goto retry;
-
			}
-
			goto ouch;
-
		}
-
		/* Read and discard the rest of the proxy response */
-
		if (fetch_getln(conn) < 0) {
-
			fetch_syserr();
-
			goto ouch;
-
		}
-
		do {
-
			switch ((h = http_next_header(conn, &headerbuf, &p))) {
-
			case hdr_syserror:
-
				fetch_syserr();
-
				goto ouch;
-
			case hdr_error:
-
				http_seterr(HTTP_PROTOCOL_ERROR);
-
				goto ouch;
-
			default:
-
				/* ignore */ ;
-
			}
-
		} while (h > hdr_end);
-
	}
-
	if (strcmp(URL->scheme, SCHEME_HTTPS) == 0 &&
-
	    fetch_ssl(conn, URL, verbose) == -1) {
-
		/* grrr */
-
		errno = EAUTH;
-
		fetch_syserr();
-
		goto ouch;
-
	}
-

-
	val = 1;
-
#ifdef TCP_NOPUSH
-
	setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val, sizeof(val));
-
#endif
-

-
	clean_http_headerbuf(&headerbuf);
-
	return (conn);
-
ouch:
-
	serrno = errno;
-
	clean_http_headerbuf(&headerbuf);
-
	fetch_close(conn);
-
	errno = serrno;
-
	return (NULL);
-
}
-

-
static struct url *
-
http_get_proxy(struct url * url, const char *flags)
-
{
-
	struct url *purl;
-
	char *p;
-

-
	if (flags != NULL && strchr(flags, 'd') != NULL)
-
		return (NULL);
-
	if (fetch_no_proxy_match(url->host))
-
		return (NULL);
-
	if (((p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
-
	    *p && (purl = fetchParseURL(p))) {
-
		if (!*purl->scheme)
-
			strcpy(purl->scheme, SCHEME_HTTP);
-
		if (!purl->port)
-
			purl->port = fetch_default_proxy_port(purl->scheme);
-
		if (strcmp(purl->scheme, SCHEME_HTTP) == 0)
-
			return (purl);
-
		fetchFreeURL(purl);
-
	}
-
	return (NULL);
-
}
-

-
static void
-
http_print_html(FILE *out, FILE *in)
-
{
-
	ssize_t len = 0;
-
	size_t cap;
-
	char *line = NULL, *p, *q;
-
	int comment, tag;
-

-
	comment = tag = 0;
-
	while ((len = getline(&line, &cap, in)) >= 0) {
-
		while (len && isspace((unsigned char)line[len - 1]))
-
			--len;
-
		for (p = q = line; q < line + len; ++q) {
-
			if (comment && *q == '-') {
-
				if (q + 2 < line + len &&
-
				    strcmp(q, "-->") == 0) {
-
					tag = comment = 0;
-
					q += 2;
-
				}
-
			} else if (tag && !comment && *q == '>') {
-
				p = q + 1;
-
				tag = 0;
-
			} else if (!tag && *q == '<') {
-
				if (q > p)
-
					fwrite(p, q - p, 1, out);
-
				tag = 1;
-
				if (q + 3 < line + len &&
-
				    strcmp(q, "<!--") == 0) {
-
					comment = 1;
-
					q += 3;
-
				}
-
			}
-
		}
-
		if (!tag && q > p)
-
			fwrite(p, q - p, 1, out);
-
		fputc('\n', out);
-
	}
-

-
	free(line);
-
}
-

-

-
/*****************************************************************************
-
 * Core
-
 */
-

-
FILE *
-
http_request(struct url *URL, const char *op, struct url_stat *us,
-
	struct url *purl, const char *flags)
-
{
-

-
	return (http_request_body(URL, op, us, purl, flags, NULL, NULL));
-
}
-

-
/*
-
 * Send a request and process the reply
-
 *
-
 * XXX This function is way too long, the do..while loop should be split
-
 * XXX off into a separate function.
-
 */
-
FILE *
-
http_request_body(struct url *URL, const char *op, struct url_stat *us,
-
	struct url *purl, const char *flags, const char *content_type,
-
	const char *body)
-
{
-
	char timebuf[80];
-
	char hbuf[MAXHOSTNAMELEN + 7], *host;
-
	conn_t *conn;
-
	struct url *url, *new;
-
	int chunked, conn_close, direct, from_cache, ims, noredirect, verbose;
-
	int e, i, n, val;
-
	off_t offset, clength, length, size;
-
	time_t mtime;
-
	const char *p;
-
	FILE *f;
-
	hdr_t h;
-
	struct tm *timestruct;
-
	http_headerbuf_t headerbuf;
-
	http_auth_challenges_t server_challenges;
-
	http_auth_challenges_t proxy_challenges;
-
	size_t body_len;
-

-
	/* The following calls don't allocate anything */
-
	init_http_headerbuf(&headerbuf);
-
	init_http_auth_challenges(&server_challenges);
-
	init_http_auth_challenges(&proxy_challenges);
-

-
	direct = CHECK_FLAG('d');
-
	noredirect = CHECK_FLAG('A');
-
	verbose = CHECK_FLAG('v');
-
	ims = CHECK_FLAG('i');
-

-
	if (direct && purl) {
-
		fetchFreeURL(purl);
-
		purl = NULL;
-
	}
-

-
	/* try the provided URL first */
-
	url = URL;
-

-
	n = MAX_REDIRECT;
-
	i = 0;
-

-
	e = HTTP_PROTOCOL_ERROR;
-
	do {
-
		new = NULL;
-
		chunked = 0;
-
		conn_close = 0;
-
		offset = 0;
-
		clength = -1;
-
		length = -1;
-
		size = -1;
-
		mtime = 0;
-

-
		/* check port */
-
		if (!url->port)
-
			url->port = fetch_default_port(url->scheme);
-

-
		/* connect to server or proxy */
-
		from_cache = (http_cached_conn != NULL);
-
		if ((conn = http_connect(url, purl, flags)) == NULL)
-
			goto ouch;
-

-
		/* append port number only if necessary */
-
		host = url->host;
-
		if (url->port != fetch_default_port(url->scheme)) {
-
			snprintf(hbuf, sizeof(hbuf), "%s:%d", host, url->port);
-
			host = hbuf;
-
		}
-

-
		/* send request */
-
		if (verbose)
-
			fetch_info("requesting %s://%s%s",
-
			    url->scheme, host, url->doc);
-
		if (purl && strcmp(url->scheme, SCHEME_HTTPS) != 0) {
-
			http_cmd(conn, "%s %s://%s%s HTTP/1.1",
-
			    op, url->scheme, host, url->doc);
-
		} else {
-
			http_cmd(conn, "%s %s HTTP/1.1",
-
			    op, url->doc);
-
		}
-

-
		if (ims && url->ims_time) {
-
			timestruct = gmtime((time_t *)&url->ims_time);
-
			(void)strftime(timebuf, 80, "%a, %d %b %Y %T GMT",
-
			    timestruct);
-
			if (verbose)
-
				fetch_info("If-Modified-Since: %s", timebuf);
-
			http_cmd(conn, "If-Modified-Since: %s", timebuf);
-
		}
-
		/* virtual host */
-
		http_cmd(conn, "Host: %s", host);
-

-
		/*
-
		 * Proxy authorization: we only send auth after we received
-
		 * a 407 error. We do not first try basic anyway (changed
-
		 * when support was added for digest-auth)
-
		 */
-
		if (purl && proxy_challenges.valid) {
-
			http_auth_params_t aparams;
-
			init_http_auth_params(&aparams);
-
			if (*purl->user || *purl->pwd) {
-
				aparams.user = strdup(purl->user);
-
				aparams.password = strdup(purl->pwd);
-
			} else if ((p = getenv("HTTP_PROXY_AUTH")) != NULL &&
-
				   *p != '\0') {
-
				if (http_authfromenv(p, &aparams) < 0) {
-
					http_seterr(HTTP_NEED_PROXY_AUTH);
-
					goto ouch;
-
				}
-
			} else if (fetch_netrc_auth(purl) == 0) {
-
				aparams.user = strdup(purl->user);
-
				aparams.password = strdup(purl->pwd);
-
			}
-
			http_authorize(conn, "Proxy-Authorization",
-
				       &proxy_challenges, &aparams, url);
-
			clean_http_auth_params(&aparams);
-
		}
-

-
		/*
-
		 * Server authorization: we never send "a priori"
-
		 * Basic auth, which used to be done if user/pass were
-
		 * set in the url. This would be weird because we'd send the
-
		 * password in the clear even if Digest is finally to be
-
		 * used (it would have made more sense for the
-
		 * pre-digest version to do this when Basic was specified
-
		 * in the environment)
-
		 */
-
		if (server_challenges.valid) {
-
			http_auth_params_t aparams;
-
			init_http_auth_params(&aparams);
-
			if (*url->user || *url->pwd) {
-
				aparams.user = strdup(url->user);
-
				aparams.password = strdup(url->pwd);
-
			} else if ((p = getenv("HTTP_AUTH")) != NULL &&
-
				   *p != '\0') {
-
				if (http_authfromenv(p, &aparams) < 0) {
-
					http_seterr(HTTP_NEED_AUTH);
-
					goto ouch;
-
				}
-
			} else if (fetch_netrc_auth(url) == 0) {
-
				aparams.user = strdup(url->user);
-
				aparams.password = strdup(url->pwd);
-
			} else if (fetchAuthMethod &&
-
				   fetchAuthMethod(url) == 0) {
-
				aparams.user = strdup(url->user);
-
				aparams.password = strdup(url->pwd);
-
			} else {
-
				http_seterr(HTTP_NEED_AUTH);
-
				goto ouch;
-
			}
-
			http_authorize(conn, "Authorization",
-
				       &server_challenges, &aparams, url);
-
			clean_http_auth_params(&aparams);
-
		}
-

-
		/* other headers */
-
		if ((p = getenv("HTTP_ACCEPT")) != NULL) {
-
			if (*p != '\0')
-
				http_cmd(conn, "Accept: %s", p);
-
		} else {
-
			http_cmd(conn, "Accept: */*");
-
		}
-
		if ((p = getenv("HTTP_REFERER")) != NULL && *p != '\0') {
-
			if (strcasecmp(p, "auto") == 0)
-
				http_cmd(conn, "Referer: %s://%s%s",
-
				    url->scheme, host, url->doc);
-
			else
-
				http_cmd(conn, "Referer: %s", p);
-
		}
-
		if ((p = getenv("HTTP_USER_AGENT")) != NULL) {
-
			/* no User-Agent if defined but empty */
-
			if  (*p != '\0')
-
				http_cmd(conn, "User-Agent: %s", p);
-
		} else {
-
			/* default User-Agent */
-
			http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER,
-
			    getprogname());
-
		}
-
		if (url->offset > 0)
-
			http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
-

-
		/* custom headers from the API and environment */
-
		if (fetchCustomHTTPHeaders != NULL)
-
			http_custom_headers(conn, fetchCustomHTTPHeaders);
-
		if ((p = getenv("HTTP_HEADERS")) != NULL && *p != '\0')
-
			http_custom_headers(conn, p);
-

-
		if (body) {
-
			body_len = strlen(body);
-
			http_cmd(conn, "Content-Length: %zu", body_len);
-
			if (content_type != NULL)
-
				http_cmd(conn, "Content-Type: %s", content_type);
-
		}
-

-
		http_cmd(conn, "");
-

-
		if (body)
-
			fetch_write(conn, body, body_len);
-

-
		/*
-
		 * Force the queued request to be dispatched.  Normally, one
-
		 * would do this with shutdown(2) but squid proxies can be
-
		 * configured to disallow such half-closed connections.  To
-
		 * be compatible with such configurations, fiddle with socket
-
		 * options to force the pending data to be written.
-
		 */
-
		val = 0;
-
#ifdef TCP_NOPUSH
-
		setsockopt(conn->sd, IPPROTO_TCP, TCP_NOPUSH, &val,
-
			   sizeof(val));
-
#endif
-
		val = 1;
-
		setsockopt(conn->sd, IPPROTO_TCP, TCP_NODELAY, &val,
-
			   sizeof(val));
-

-
		/* get reply */
-
		switch (http_get_reply(conn)) {
-
		case HTTP_OK:
-
		case HTTP_PARTIAL:
-
		case HTTP_NOT_MODIFIED:
-
			/* fine */
-
			break;
-
		case HTTP_MOVED_PERM:
-
		case HTTP_MOVED_TEMP:
-
		case HTTP_TEMP_REDIRECT:
-
		case HTTP_PERM_REDIRECT:
-
		case HTTP_SEE_OTHER:
-
		case HTTP_USE_PROXY:
-
			/*
-
			 * Not so fine, but we still have to read the
-
			 * headers to get the new location.
-
			 */
-
			break;
-
		case HTTP_NEED_AUTH:
-
			if (server_challenges.valid) {
-
				/*
-
				 * We already sent out authorization code,
-
				 * so there's nothing more we can do.
-
				 */
-
				http_seterr(conn->err);
-
				goto ouch;
-
			}
-
			/* try again, but send the password this time */
-
			if (verbose)
-
				fetch_info("server requires authorization");
-
			break;
-
		case HTTP_NEED_PROXY_AUTH:
-
			if (proxy_challenges.valid) {
-
				/*
-
				 * We already sent our proxy
-
				 * authorization code, so there's
-
				 * nothing more we can do. */
-
				http_seterr(conn->err);
-
				goto ouch;
-
			}
-
			/* try again, but send the password this time */
-
			if (verbose)
-
				fetch_info("proxy requires authorization");
-
			break;
-
		case HTTP_BAD_RANGE:
-
			/*
-
			 * This can happen if we ask for 0 bytes because
-
			 * we already have the whole file.  Consider this
-
			 * a success for now, and check sizes later.
-
			 */
-
			break;
-
		case HTTP_PROTOCOL_ERROR:
-
			/* fall through */
-
		case -1:
-
			/*
-
			 * If we got this connection from the cache, the
-
			 * server may have closed it while idle.  Retry
-
			 * once with a fresh connection.
-
			 */
-
			if (from_cache) {
-
				if (verbose)
-
					fetch_info("stale cached connection, "
-
					    "retrying");
-
				fetch_close(conn);
-
				conn = NULL;
-
				from_cache = 0;
-
				continue;
-
			}
-
			fetch_syserr();
-
			goto ouch;
-
		default:
-
			http_seterr(conn->err);
-
			if (!verbose)
-
				goto ouch;
-
			/* fall through so we can get the full error message */
-
		}
-

-
		/* get headers. http_next_header expects one line readahead */
-
		if (fetch_getln(conn) == -1) {
-
			fetch_syserr();
-
			goto ouch;
-
		}
-
		do {
-
			switch ((h = http_next_header(conn, &headerbuf, &p))) {
-
			case hdr_syserror:
-
				fetch_syserr();
-
				goto ouch;
-
			case hdr_error:
-
				http_seterr(HTTP_PROTOCOL_ERROR);
-
				goto ouch;
-
			case hdr_content_length:
-
				http_parse_length(p, &clength);
-
				break;
-
			case hdr_content_range:
-
				http_parse_range(p, &offset, &length, &size);
-
				break;
-
			case hdr_last_modified:
-
				http_parse_mtime(p, &mtime);
-
				break;
-
			case hdr_location:
-
				if (!HTTP_REDIRECT(conn->err))
-
					break;
-
				/*
-
				 * if the A flag is set, we don't follow
-
				 * temporary redirects.
-
				 */
-
				if (noredirect &&
-
				    conn->err != HTTP_MOVED_PERM &&
-
				    conn->err != HTTP_PERM_REDIRECT &&
-
				    conn->err != HTTP_USE_PROXY) {
-
					n = 1;
-
					break;
-
				}
-
				if (new)
-
					free(new);
-
				if (verbose)
-
					fetch_info("%d redirect to %s",
-
					    conn->err, p);
-
				if (*p == '/')
-
					/* absolute path */
-
					new = fetchMakeURL(url->scheme, url->host,
-
					    url->port, p, url->user, url->pwd);
-
				else
-
					new = fetchParseURL(p);
-
				if (new == NULL) {
-
					/* XXX should set an error code */
-
					DEBUGF("failed to parse new URL\n");
-
					goto ouch;
-
				}
-

-
				/* Only copy credentials if the host matches */
-
				if (strcmp(new->host, url->host) == 0 &&
-
				    !*new->user && !*new->pwd) {
-
					strcpy(new->user, url->user);
-
					strcpy(new->pwd, url->pwd);
-
				}
-
				new->offset = url->offset;
-
				new->length = url->length;
-
				new->ims_time = url->ims_time;
-
				break;
-
			case hdr_transfer_encoding:
-
				/* XXX weak test*/
-
				chunked = (strcasecmp(p, "chunked") == 0);
-
				break;
-
			case hdr_www_authenticate:
-
				if (conn->err != HTTP_NEED_AUTH)
-
					break;
-
				if (http_parse_authenticate(p, &server_challenges) == 0)
-
					++n;
-
				break;
-
			case hdr_proxy_authenticate:
-
				if (conn->err != HTTP_NEED_PROXY_AUTH)
-
					break;
-
				if (http_parse_authenticate(p, &proxy_challenges) == 0)
-
					++n;
-
				break;
-
			case hdr_connection:
-
				if (strcasecmp(p, "close") == 0)
-
					conn_close = 1;
-
				break;
-
			case hdr_end:
-
				/* fall through */
-
			case hdr_unknown:
-
				/* ignore */
-
				break;
-
			}
-
		} while (h > hdr_end);
-

-
		/* we need to provide authentication */
-
		if (conn->err == HTTP_NEED_AUTH ||
-
		    conn->err == HTTP_NEED_PROXY_AUTH) {
-
			e = conn->err;
-
			if ((conn->err == HTTP_NEED_AUTH &&
-
			     !server_challenges.valid) ||
-
			    (conn->err == HTTP_NEED_PROXY_AUTH &&
-
			     !proxy_challenges.valid)) {
-
				/* 401/7 but no www/proxy-authenticate ?? */
-
				DEBUGF("%03d without auth header\n", conn->err);
-
				goto ouch;
-
			}
-
			fetch_close(conn);
-
			conn = NULL;
-
			continue;
-
		}
-

-
		/* requested range not satisfiable */
-
		if (conn->err == HTTP_BAD_RANGE) {
-
			if (url->offset > 0 && url->length == 0) {
-
				/* asked for 0 bytes; fake it */
-
				offset = url->offset;
-
				clength = -1;
-
				conn->err = HTTP_OK;
-
				break;
-
			} else {
-
				http_seterr(conn->err);
-
				goto ouch;
-
			}
-
		}
-

-
		/* we have a hit or an error */
-
		if (conn->err == HTTP_OK
-
		    || conn->err == HTTP_NOT_MODIFIED
-
		    || conn->err == HTTP_PARTIAL
-
		    || HTTP_ERROR(conn->err))
-
			break;
-

-
		/* all other cases: we got a redirect */
-
		e = conn->err;
-
		clean_http_auth_challenges(&server_challenges);
-
		fetch_close(conn);
-
		conn = NULL;
-
		if (!new) {
-
			DEBUGF("redirect with no new location\n");
-
			break;
-
		}
-
		if (url != URL)
-
			fetchFreeURL(url);
-
		url = new;
-
	} while (++i < n);
-

-
	/* we failed, or ran out of retries */
-
	if (conn == NULL) {
-
		http_seterr(e);
-
		goto ouch;
-
	}
-

-
	DEBUGF("offset %lld, length %lld, size %lld, clength %lld\n",
-
	    (long long)offset, (long long)length,
-
	    (long long)size, (long long)clength);
-

-
	if (conn->err == HTTP_NOT_MODIFIED) {
-
		http_seterr(HTTP_NOT_MODIFIED);
-
		return (NULL);
-
	}
-

-
	/* check for inconsistencies */
-
	if (clength != -1 && length != -1 && clength != length) {
-
		http_seterr(HTTP_PROTOCOL_ERROR);
-
		goto ouch;
-
	}
-
	if (clength == -1)
-
		clength = length;
-
	if (clength != -1)
-
		length = offset + clength;
-
	if (length != -1 && size != -1 && length != size) {
-
		http_seterr(HTTP_PROTOCOL_ERROR);
-
		goto ouch;
-
	}
-
	if (size == -1)
-
		size = length;
-

-
	/* fill in stats */
-
	if (us) {
-
		us->size = size;
-
		us->atime = us->mtime = mtime;
-
	}
-

-
	/* too far? */
-
	if (URL->offset > 0 && offset > URL->offset) {
-
		http_seterr(HTTP_PROTOCOL_ERROR);
-
		goto ouch;
-
	}
-

-
	/* report back real offset and size */
-
	URL->offset = offset;
-
	URL->length = clength;
-

-
	/* wrap it up in a FILE */
-
	if ((f = http_funopen(conn, chunked, !conn_close && purl == NULL, clength)) == NULL) {
-
		fetch_syserr();
-
		goto ouch;
-
	}
-

-
	if (url != URL)
-
		fetchFreeURL(url);
-
	if (purl)
-
		fetchFreeURL(purl);
-

-
	if (HTTP_ERROR(conn->err)) {
-
		http_print_html(stderr, f);
-
		fclose(f);
-
		f = NULL;
-
	}
-
	clean_http_headerbuf(&headerbuf);
-
	clean_http_auth_challenges(&server_challenges);
-
	clean_http_auth_challenges(&proxy_challenges);
-
	return (f);
-

-
ouch:
-
	if (url != URL)
-
		fetchFreeURL(url);
-
	if (purl)
-
		fetchFreeURL(purl);
-
	if (conn != NULL)
-
		fetch_close(conn);
-
	clean_http_headerbuf(&headerbuf);
-
	clean_http_auth_challenges(&server_challenges);
-
	clean_http_auth_challenges(&proxy_challenges);
-
	return (NULL);
-
}
-

-

-
/*****************************************************************************
-
 * Entry points
-
 */
-

-
/*
-
 * Retrieve and stat a file by HTTP
-
 */
-
FILE *
-
fetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags)
-
{
-
	return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags));
-
}
-

-
/*
-
 * Retrieve a file by HTTP
-
 */
-
FILE *
-
fetchGetHTTP(struct url *URL, const char *flags)
-
{
-
	return (fetchXGetHTTP(URL, NULL, flags));
-
}
-

-
/*
-
 * Store a file by HTTP
-
 */
-
FILE *
-
fetchPutHTTP(struct url *URL __unused, const char *flags __unused)
-
{
-
	warnx("fetchPutHTTP(): not implemented");
-
	return (NULL);
-
}
-

-
/*
-
 * Get an HTTP document's metadata
-
 */
-
int
-
fetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags)
-
{
-
	FILE *f;
-

-
	f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags);
-
	if (f == NULL)
-
		return (-1);
-
	fclose(f);
-
	return (0);
-
}
-

-
/*
-
 * List a directory
-
 */
-
struct url_ent *
-
fetchListHTTP(struct url *url __unused, const char *flags __unused)
-
{
-
	warnx("fetchListHTTP(): not implemented");
-
	return (NULL);
-
}
-

-
/*
-
 * Arbitrary HTTP verb and content requests
-
 */
-
FILE *
-
fetchReqHTTP(struct url *URL, const char *method, const char *flags,
-
	const char *content_type, const char *body)
-
{
-

-
	return (http_request_body(URL, method, NULL, http_get_proxy(URL, flags),
-
	    flags, content_type, body));
-
}
deleted external/libfetch/http.errors
@@ -1,45 +0,0 @@
-
#
-
# This list is taken from RFC 2068.
-
#
-
100 OK		Continue
-
101 OK		Switching Protocols
-
200 OK		OK
-
201 OK		Created
-
202 OK		Accepted
-
203 INFO	Non-Authoritative Information
-
204 OK		No Content
-
205 OK		Reset Content
-
206 OK		Partial Content
-
300 MOVED	Multiple Choices
-
301 MOVED	Moved Permanently
-
302 MOVED	Moved Temporarily
-
303 MOVED	See Other
-
304 OK		Not Modified
-
305 INFO	Use Proxy
-
307 MOVED	Temporary Redirect
-
308 MOVED	Permanent Redirect
-
400 PROTO	Bad Request
-
401 AUTH	Unauthorized
-
402 AUTH	Payment Required
-
403 AUTH	Forbidden
-
404 UNAVAIL	Not Found
-
405 PROTO	Method Not Allowed
-
406 PROTO	Not Acceptable
-
407 AUTH	Proxy Authentication Required
-
408 TIMEOUT	Request Time-out
-
409 EXISTS	Conflict
-
410 UNAVAIL	Gone
-
411 PROTO	Length Required
-
412 SERVER	Precondition Failed
-
413 PROTO	Request Entity Too Large
-
414 PROTO	Request-URI Too Large
-
415 PROTO	Unsupported Media Type
-
416 UNAVAIL	Requested Range Not Satisfiable
-
417 SERVER	Expectation Failed
-
500 SERVER	Internal Server Error
-
501 PROTO	Not Implemented
-
502 SERVER	Bad Gateway
-
503 TEMP	Service Unavailable
-
504 TIMEOUT	Gateway Time-out
-
505 PROTO	HTTP Version not supported
-
999 PROTO	Protocol error
deleted external/libfetch/httperr.h
@@ -1,45 +0,0 @@
-
static struct fetcherr http_errlist[] = {
-
    { 100, FETCH_OK, "Continue" },
-
    { 101, FETCH_OK, "Switching Protocols" },
-
    { 200, FETCH_OK, "OK" },
-
    { 201, FETCH_OK, "Created" },
-
    { 202, FETCH_OK, "Accepted" },
-
    { 203, FETCH_INFO, "Non-Authoritative Information" },
-
    { 204, FETCH_OK, "No Content" },
-
    { 205, FETCH_OK, "Reset Content" },
-
    { 206, FETCH_OK, "Partial Content" },
-
    { 300, FETCH_MOVED, "Multiple Choices" },
-
    { 301, FETCH_MOVED, "Moved Permanently" },
-
    { 302, FETCH_MOVED, "Moved Temporarily" },
-
    { 303, FETCH_MOVED, "See Other" },
-
    { 304, FETCH_OK, "Not Modified" },
-
    { 305, FETCH_INFO, "Use Proxy" },
-
    { 307, FETCH_MOVED, "Temporary Redirect" },
-
    { 308, FETCH_MOVED, "Permanent Redirect" },
-
    { 400, FETCH_PROTO, "Bad Request" },
-
    { 401, FETCH_AUTH, "Unauthorized" },
-
    { 402, FETCH_AUTH, "Payment Required" },
-
    { 403, FETCH_AUTH, "Forbidden" },
-
    { 404, FETCH_UNAVAIL, "Not Found" },
-
    { 405, FETCH_PROTO, "Method Not Allowed" },
-
    { 406, FETCH_PROTO, "Not Acceptable" },
-
    { 407, FETCH_AUTH, "Proxy Authentication Required" },
-
    { 408, FETCH_TIMEOUT, "Request Time-out" },
-
    { 409, FETCH_EXISTS, "Conflict" },
-
    { 410, FETCH_UNAVAIL, "Gone" },
-
    { 411, FETCH_PROTO, "Length Required" },
-
    { 412, FETCH_SERVER, "Precondition Failed" },
-
    { 413, FETCH_PROTO, "Request Entity Too Large" },
-
    { 414, FETCH_PROTO, "Request-URI Too Large" },
-
    { 415, FETCH_PROTO, "Unsupported Media Type" },
-
    { 416, FETCH_UNAVAIL, "Requested Range Not Satisfiable" },
-
    { 417, FETCH_SERVER, "Expectation Failed" },
-
    { 500, FETCH_SERVER, "Internal Server Error" },
-
    { 501, FETCH_PROTO, "Not Implemented" },
-
    { 502, FETCH_SERVER, "Bad Gateway" },
-
    { 503, FETCH_TEMP, "Service Unavailable" },
-
    { 504, FETCH_TIMEOUT, "Gateway Time-out" },
-
    { 505, FETCH_PROTO, "HTTP Version not supported" },
-
    { 999, FETCH_PROTO, "Protocol error" },
-
    { -1, FETCH_UNKNOWN, "Unknown HTTP error" }
-
};
added external/liblinenoise/Makefile.in
@@ -0,0 +1,6 @@
+
include @builddir@/mk/defs.mk
+
LIB=	linenoise
+
SRCS=	linenoise.c stringbuf.c utf8.c
+
VPATH=	$(top_srcdir)/external/linenoise
+

+
include $(MK)/static-lib.mk
deleted external/liblua/Makefile.autosetup
@@ -1,42 +0,0 @@
-
include @builddir@/mk/defs.mk
-

-
LIB=	lua
-
.PATH:	$(top_srcdir)/external/lua/src/
-
VPATH=	$(top_srcdir)/external/lua/src/
-

-
SRCS=	lapi.c \
-
	lcode.c \
-
	lctype.c \
-
	ldebug.c \
-
	ldo.c \
-
	ldump.c \
-
	lfunc.c \
-
	lgc.c \
-
	llex.c \
-
	lmem.c \
-
	lobject.c \
-
	lopcodes.c \
-
	lparser.c \
-
	lstate.c \
-
	lstring.c \
-
	ltable.c \
-
	ltm.c \
-
	lundump.c \
-
	lvm.c \
-
	lzio.c \
-
	lauxlib.c \
-
	lbaselib.c \
-
	lcorolib.c \
-
	ldblib.c \
-
	liolib.c \
-
	lmathlib.c \
-
	loslib.c \
-
	lstrlib.c \
-
	ltablib.c \
-
	lutf8lib.c \
-
	loadlib.c \
-
	linit.c
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/external/lua/src -DLUA_USE_POSIX
-

-
include $(MK)/static-lib.mk
added external/liblua/Makefile.in
@@ -0,0 +1,42 @@
+
include @builddir@/mk/defs.mk
+

+
LIB=	lua
+
.PATH:	$(top_srcdir)/external/lua/src/
+
VPATH=	$(top_srcdir)/external/lua/src/
+

+
SRCS=	lapi.c \
+
	lcode.c \
+
	lctype.c \
+
	ldebug.c \
+
	ldo.c \
+
	ldump.c \
+
	lfunc.c \
+
	lgc.c \
+
	llex.c \
+
	lmem.c \
+
	lobject.c \
+
	lopcodes.c \
+
	lparser.c \
+
	lstate.c \
+
	lstring.c \
+
	ltable.c \
+
	ltm.c \
+
	lundump.c \
+
	lvm.c \
+
	lzio.c \
+
	lauxlib.c \
+
	lbaselib.c \
+
	lcorolib.c \
+
	ldblib.c \
+
	liolib.c \
+
	lmathlib.c \
+
	loslib.c \
+
	lstrlib.c \
+
	ltablib.c \
+
	lutf8lib.c \
+
	loadlib.c \
+
	linit.c
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/external/lua/src -DLUA_USE_POSIX
+

+
include $(MK)/static-lib.mk
added external/libpicosat/Makefile.in
@@ -0,0 +1,10 @@
+
include @builddir@/mk/defs.mk
+
LIB=	picosat
+
SRCS=	picosat.c \
+
	version.c
+
LOCAL_CFLAGS=	-Wno-strict-aliasing \
+
		-Wno-unused -DNGETRUSAGE
+

+
VPATH=	$(top_srcdir)/external/picosat
+

+
include $(MK)/static-lib.mk
added external/libsqlite/Makefile.in
@@ -0,0 +1,45 @@
+
include @builddir@/mk/defs.mk
+
LIB=	sqlite
+
SRCS=	sqlite3.c \
+
	shell.c
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/external/linenoise \
+
		-I$(top_builddir)/external/libsqlite \
+
		-D_HAVE_SQLITE_CONFIG_H \
+
		-DHAVE_LINENOISE=2 \
+
		-Wno-unused-variable \
+
		-Wno-unused-function \
+
		-Wno-strict-aliasing \
+
		-Wno-unused-parameter \
+
		-DSQLITE_OS_UNIX=1 \
+
		-DDHAVE_MALLOC_USABLE_SIZE=1 \
+
		-DSQLITE_OMIT_AUTOVACUUM \
+
		-DSQLITE_OMIT_BLOB_LITERAL \
+
		-DSQLITE_OMIT_DECLTYPE \
+
		-DSQLITE_OMIT_EXPLAIN \
+
		-DSQLITE_OMIT_DEPRECATED \
+
		-DSQLITE_OMIT_LOAD_EXTENSION \
+
		-DSQLITE_OMIT_PROGRESS_CALLBACK \
+
		-DSQLITE_OMIT_TCL_VARIABLE \
+
		-DSQLITE_OMIT_UTF16 \
+
		-DSQLITE_OMIT_CAT \
+
		-DSQLITE_OMIT_CHECK \
+
		-DSQLITE_OMIT_AUTOINIT \
+
		-DSQLITE_OMIT_COMPILEOPTION_DIAGS \
+
		-DSQLITE_OMIT_INTEGRITY_CHECK \
+
		-DSQLITE_OMIT_BUILTIN_TEST \
+
		-DSQLITE_OMIT_SHARED_CACHE \
+
		-DSQLITE_ENABLE_UNLOCK_NOTIFY=1 \
+
		-DUSE_PREAD \
+
		-DUSE_PWRITE \
+
		-DSQLITE_THREADSAFE=0 \
+
		-DSQLITE_TEMP_STORE=3 \
+
		-DSQLITE_ENABLE_FTS4 \
+
		-DSQLITE_SHELL_DBNAME_PROC=pkgshell_opendb \
+
		-DSQLITE_SHELL_INIT_PROC=pkgdb_init_proc \
+
		-Dmain=sqlite3_shell \
+
		-DNDEBUG
+

+
VPATH=	$(top_srcdir)/external/sqlite
+

+
include $(MK)/static-lib.mk
deleted external/libucl/Makefile.autosetup
@@ -1,22 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	ucl
-
SRCS=	src/ucl_emitter_streamline.c \
-
	src/ucl_emitter_utils.c \
-
	src/ucl_emitter.c \
-
	src/ucl_hash.c \
-
	src/ucl_msgpack.c \
-
	src/ucl_parser.c \
-
	src/ucl_schema.c \
-
	src/ucl_sexp.c \
-
	src/ucl_util.c
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/external/libucl/include \
-
		-I$(top_srcdir)/external/libucl/uthash \
-
		-I$(top_srcdir)/external/libucl/klib \
-
		-I$(top_srcdir)/external/libucl/src/ \
-
		-Wno-unused-parameter \
-
		-Wno-pointer-sign
-

-
VPATH=	$(top_srcdir)/external/libucl
-

-
include $(MK)/static-lib.mk
added external/libucl/Makefile.in
@@ -0,0 +1,22 @@
+
include @builddir@/mk/defs.mk
+
LIB=	ucl
+
SRCS=	src/ucl_emitter_streamline.c \
+
	src/ucl_emitter_utils.c \
+
	src/ucl_emitter.c \
+
	src/ucl_hash.c \
+
	src/ucl_msgpack.c \
+
	src/ucl_parser.c \
+
	src/ucl_schema.c \
+
	src/ucl_sexp.c \
+
	src/ucl_util.c
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/external/libucl/include \
+
		-I$(top_srcdir)/external/libucl/uthash \
+
		-I$(top_srcdir)/external/libucl/klib \
+
		-I$(top_srcdir)/external/libucl/src/ \
+
		-Wno-unused-parameter \
+
		-Wno-pointer-sign
+

+
VPATH=	$(top_srcdir)/external/libucl
+

+
include $(MK)/static-lib.mk
added external/libyxml/Makefile.in
@@ -0,0 +1,13 @@
+
include @builddir@/mk/defs.mk
+

+
LIB=	yxml
+

+
SRCS=	yxml.c
+

+
LOCAL_CFLAGS=	-Wno-unused-parameter -I. -I$(top_srcdir)/external/yxml
+

+
VPATH=	$(top_srcdir)/external/yxml
+

+
include $(MK)/static-lib.mk
+

+

deleted external/linenoise/Makefile.autosetup
@@ -1,6 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	linenoise
-
SRCS=	linenoise.c stringbuf.c utf8.c
-
VPATH=	$(top_srcdir)/external/linenoise
-

-
include $(MK)/static-lib.mk
deleted external/picosat/Makefile.autosetup
@@ -1,10 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	picosat
-
SRCS=	picosat.c \
-
	version.c
-
LOCAL_CFLAGS=	-Wno-strict-aliasing \
-
		-Wno-unused -DNGETRUSAGE
-

-
VPATH=	$(top_srcdir)/external/picosat
-

-
include $(MK)/static-lib.mk
deleted external/sqlite/Makefile.autosetup
@@ -1,45 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	sqlite
-
SRCS=	sqlite3.c \
-
	shell.c
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/external/linenoise \
-
		-I$(top_builddir)/external/sqlite \
-
		-D_HAVE_SQLITE_CONFIG_H \
-
		-DHAVE_LINENOISE=2 \
-
		-Wno-unused-variable \
-
		-Wno-unused-function \
-
		-Wno-strict-aliasing \
-
		-Wno-unused-parameter \
-
		-DSQLITE_OS_UNIX=1 \
-
		-DDHAVE_MALLOC_USABLE_SIZE=1 \
-
		-DSQLITE_OMIT_AUTOVACUUM \
-
		-DSQLITE_OMIT_BLOB_LITERAL \
-
		-DSQLITE_OMIT_DECLTYPE \
-
		-DSQLITE_OMIT_EXPLAIN \
-
		-DSQLITE_OMIT_DEPRECATED \
-
		-DSQLITE_OMIT_LOAD_EXTENSION \
-
		-DSQLITE_OMIT_PROGRESS_CALLBACK \
-
		-DSQLITE_OMIT_TCL_VARIABLE \
-
		-DSQLITE_OMIT_UTF16 \
-
		-DSQLITE_OMIT_CAT \
-
		-DSQLITE_OMIT_CHECK \
-
		-DSQLITE_OMIT_AUTOINIT \
-
		-DSQLITE_OMIT_COMPILEOPTION_DIAGS \
-
		-DSQLITE_OMIT_INTEGRITY_CHECK \
-
		-DSQLITE_OMIT_BUILTIN_TEST \
-
		-DSQLITE_OMIT_SHARED_CACHE \
-
		-DSQLITE_ENABLE_UNLOCK_NOTIFY=1 \
-
		-DUSE_PREAD \
-
		-DUSE_PWRITE \
-
		-DSQLITE_THREADSAFE=0 \
-
		-DSQLITE_TEMP_STORE=3 \
-
		-DSQLITE_ENABLE_FTS4 \
-
		-DSQLITE_SHELL_DBNAME_PROC=pkgshell_opendb \
-
		-DSQLITE_SHELL_INIT_PROC=pkgdb_init_proc \
-
		-Dmain=sqlite3_shell \
-
		-DNDEBUG
-

-
VPATH=	$(top_srcdir)/external/sqlite
-

-
include $(MK)/static-lib.mk
deleted external/yxml/Makefile.autosetup
@@ -1,13 +0,0 @@
-
include @builddir@/mk/defs.mk
-

-
LIB=	yxml
-

-
SRCS=	yxml.c
-

-
LOCAL_CFLAGS=	-Wno-unused-parameter -I. -I$(top_srcdir)/external/yxml
-

-
VPATH=	$(top_srcdir)/external/yxml
-

-
include $(MK)/static-lib.mk
-

-

deleted libpkg/Makefile.autosetup
@@ -1,211 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	pkg
-
SRCS=	backup_lib.c \
-
	merge3.c \
-
	pkg_audit.c \
-
	pkg_deps.c \
-
	pkg_repo_meta.c \
-
	pkg.c \
-
	pkg_key.c \
-
	pkgsign.c \
-
	pkgsign_ecc.c \
-
	pkgsign_ossl.c \
-
	clean_cache.c \
-
	metalog.c \
-
	pkg_checksum.c \
-
	pkg_manifest.c \
-
	pkg_repo_update.c \
-
	pkgdb_iterator.c \
-
	lua.c \
-
	lua_scripts.c \
-
	scripts.c \
-
	diff.c \
-
	packing.c \
-
	pkg_config.c \
-
	pkg_event.c \
-
	pkg_object.c \
-
	pkg_osvf.c \
-
	pkg_repo.c \
-
	pkgdb_query.c \
-
	sha256.c dns_utils.c \
-
	pkg_add.c \
-
	pkg_create.c \
-
	pkg_jobs_conflicts.c pkg_ports.c \
-
	pkg_solve.c \
-
	pkgdb.c \
-
	pkg_abi.c \
-
	pkg_elf.c \
-
	pkg_abi_macho.c \
-
	binfmt_macho.c \
-
	ssh.c \
-
	pkgbase.c \
-
	pkg_arch.c \
-
	pkg_cudf.c \
-
	pkg_cpe.c \
-
	pkg_jobs_universe.c  pkg_printf.c \
-
	pkg_status.c \
-
	plugins.c \
-
	utils.c fetch.c \
-
	pkg_sandbox.c \
-
	pkg_attributes.c \
-
	pkg_delete.c \
-
	pkg_jobs.c \
-
	pkg_jobs_schedule.c \
-
	pkg_repo_create.c \
-
	pkg_version.c \
-
	rcscripts.c \
-
	flags.c \
-
	fetch_ssh.c \
-
	fetch_libfetch.c \
-
	fetch_file.c \
-
	triggers.c \
-
	pkghash.c \
-
	yuarel.c \
-
	json.c
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
-
		-I$(top_srcdir)/external/blake2 \
-
		-I$(top_srcdir)/external/sqlite \
-
		-I$(top_srcdir)/external/uthash \
-
		-I$(top_srcdir)/external/picosat \
-
		-I$(top_srcdir)/external/yxml \
-
		-I$(top_srcdir)/external/uthash \
-
		-I$(top_srcdir)/external/include \
-
		-I$(top_srcdir)/external/libucl/include \
-
		-I$(top_srcdir)/external/lua/src \
-
		-I$(top_srcdir)/external/liblua/ \
-
		-I$(top_srcdir)/external/libder/libder \
-
		-I$(top_srcdir)/external/libecc/include \
-
		-I$(top_srcdir)/external/libfetch \
-
		-I$(top_srcdir)/libpkg/repo \
-
		-I$(top_builddir)/libpkg/repo \
-
		-I$(top_srcdir)/libpkg \
-
		-I$(top_builddir)/libpkg \
-
		-I$(top_srcdir) \
-
		-I$(top_builddir) \
-
		-Wl,--as-needed \
-
		-Wno-pointer-sign \
-
		-Wno-missing-field-initializers \
-
		-Wno-sign-compare \
-
		-DPREFIX=\"$(PREFIX)\" \
-
		-DHAVE_CONFIG_H
-

-
LOCAL_LDFLAGS=	@waflags@ \
-
		@REPOS_LDFLAGS@ \
-
		-L$(top_builddir)/external/sqlite -lsqlite_pic \
-
		-L$(top_builddir)/external/libucl -lucl_pic \
-
		-L$(top_builddir)/external/picosat -lpicosat_pic \
-
		-L$(top_builddir)/external/linenoise -llinenoise_pic \
-
		-L$(top_builddir)/external/yxml -lyxml_pic \
-
		-L$(top_builddir)/external/blake2 -lblake2_pic \
-
		-L$(top_builddir)/compat -lbsd_compat_pic \
-
		-L$(top_builddir)/external/liblua -llua_pic \
-
		-L$(top_builddir)/external/libder -lder_pic \
-
		-L$(top_builddir)/external/libecc -lecc_pic \
-
		-L$(top_builddir)/external/libfetch -lfetch_pic \
-
		@nowaflags@ \
-
		-lm
-

-
STATIC_LIBS=	@REPOS_STATIC_LIBS@ \
-
		$(top_builddir)/external/sqlite/libsqlite.a \
-
		$(top_builddir)/external/libucl/libucl.a \
-
		$(top_builddir)/external/picosat/libpicosat.a \
-
		$(top_builddir)/external/linenoise/liblinenoise.a \
-
		$(top_builddir)/external/yxml/libyxml.a \
-
		$(top_builddir)/external/blake2/libblake2.a \
-
		$(top_builddir)/external/liblua/liblua.a \
-
		$(top_builddir)/compat/libbsd_compat.a \
-
		$(top_builddir)/external/libder/libder.a \
-
		$(top_builddir)/external/libecc/libecc.a \
-
		$(top_builddir)/external/libfetch/libfetch.a \
-
		lib$(LIB).a
-

-
@if HAVE_LIBUTIL
-
LOCAL_LDFLAGS+=	-lutil
-
@endif
-
@if HAVE_LIBFTS
-
LOCAL_LDFLAGS+=	-lfts
-
@endif
-
@if HAVE_LIBDL
-
LOCAL_LDFLAGS+= -ldl
-
@endif
-

-
@if pkgos_darwin
-
LOCAL_LDFLAGS+=	-lresolv
-
@else
-
@if pkgos_freebsd
-
LOCAL_LDFLAGS+=	-Wl,--version-script=$(top_srcdir)/libpkg/libpkg.ver,--undefined-version
-
@else
-
### --undefined-version is a FreeBSD ld option
-
LOCAL_LDFLAGS+=	-Wl,--version-script=$(top_srcdir)/libpkg/libpkg.ver
-
@endif
-
@endif
-

-
@if libelf-internal
-
LOCAL_CFLAGS+= -I$(top_srcdir)/external/libelf
-
STATIC_LIBS+=	$(top_builddir)/external/libelf/libelf.a
-
LOCAL_LDFLAGS+=	-L$(top_builddir)/external/libelf -lelf
-
@endif
-

-
@if libelf-external
-
LOCAL_LDFLAGS+=	-lelf
-
@endif
-

-
@if PKG_LIBARCHIVE_LIBS
-
LOCAL_CFLAGS+=	@PKG_LIBARCHIVE_CFLAGS@
-
LOCAL_LDFLAGS+=	@PKG_LIBARCHIVE_LDFLAGS@ @PKG_LIBARCHIVE_LIBS@
-
@else
-
LOCAL_LDFLAGS+=	-larchive
-
@endif
-

-
@if PKG_OPENSSL_LIBS
-
LOCAL_CFLAGS+=	@PKG_OPENSSL_CFLAGS@
-
LOCAL_LDFLAGS+=	@PKG_OPENSSL_LDFLAGS@ @PKG_OPENSSL_LIBS@
-
@else
-
LOCAL_LDFLAGS+=	-lssl -lcrypto
-
@endif
-

-
@if PKG_LIBLDNS_LIBS
-
LOCAL_CFLAGS+=	@PKG_LIBLDNS_CFLAGS@
-
LOCAL_LDFLAGS+=	@PKG_LIBLDNS_LDFLAGS@ @PKG_LIBLDNS_LIBS@
-
@endif
-

-
VPATH=	$(top_srcdir)/libpkg
-
.PATH:	$(top_srcdir)/libpkg
-

-
include $(MK)/lib.mk
-

-
$(OBJS) $(SHOBJS): $(top_builddir)/pkg_config.h
-

-
all: lib$(LIB)_flat.a
-

-
lib$(LIB)$(LIBSOEXT): $(STATIC_LIBS)
-

-
@if pkgos_darwin
-
lib$(LIB)_flat.a: $(STATIC_LIBS)
-
	libtool -static -o lib$(LIB)_flat.a $(STATIC_LIBS)
-
@else
-
lib$(LIB)_flat.a: ${STATIC_LIBS} mergelib_script
-
	$(AR) -M < mergelib_script
-
@endif
-

-
mergelib_script: $(STATIC_LIBS)
-
	@echo "CREATE lib$(LIB)_flat.a" > $@
-
	@for l in $(STATIC_LIBS); do \
-
		echo "ADDLIB $$l"; \
-
	done >> $@
-
	@echo "SAVE" >> $@
-
	@echo "END" >> $@
-

-
install: all pkg.h lib$(LIB)$(LIBSOEXT) lib$(LIB)_flat.a
-
	install -d -m 755 $(DESTDIR)$(libdir)
-
	install -d -m 755 $(DESTDIR)$(includedir)
-
	install -d -m 755 $(DESTDIR)$(includedir)/pkg
-
	install -d -m 755 $(DESTDIR)$(pkgconfigdir)
-
	install -m 644 lib$(LIB)$(LIBSOEXT) $(DESTDIR)$(libdir)/
-
	ln -sf lib$(LIB)$(LIBSOEXT) $(DESTDIR)$(libdir)/lib$(LIB)$(SH_SOEXT)
-
	install -m 644 lib$(LIB)_flat.a $(DESTDIR)$(libdir)/lib$(LIB).a
-
	install -m 644 pkg.h $(DESTDIR)$(includedir)/
-
	install -m 644 $(top_srcdir)/libpkg/pkg/vec.h $(DESTDIR)$(includedir)/pkg
-
	install -m 644 $(top_srcdir)/libpkg/pkg/audit.h $(DESTDIR)$(includedir)/pkg
-
	install -m 644 pkg.pc $(DESTDIR)$(pkgconfigdir)/
added libpkg/Makefile.in
@@ -0,0 +1,212 @@
+
include @builddir@/mk/defs.mk
+
LIB=	pkg
+
SRCS=	backup_lib.c \
+
	merge3.c \
+
	pkg_audit.c \
+
	pkg_deps.c \
+
	pkg_repo_meta.c \
+
	pkg.c \
+
	pkg_key.c \
+
	pkgsign.c \
+
	pkgsign_ecc.c \
+
	pkgsign_ossl.c \
+
	clean_cache.c \
+
	metalog.c \
+
	pkg_checksum.c \
+
	pkg_manifest.c \
+
	pkg_repo_update.c \
+
	pkgdb_iterator.c \
+
	lua.c \
+
	lua_scripts.c \
+
	scripts.c \
+
	diff.c \
+
	packing.c \
+
	pkg_config.c \
+
	pkg_event.c \
+
	pkg_object.c \
+
	pkg_osvf.c \
+
	pkg_repo.c \
+
	pkgdb_query.c \
+
	sha256.c dns_utils.c \
+
	pkg_add.c \
+
	pkg_create.c \
+
	pkg_jobs_conflicts.c pkg_ports.c \
+
	pkg_solve.c \
+
	pkgdb.c \
+
	pkg_abi.c \
+
	pkg_elf.c \
+
	pkg_abi_macho.c \
+
	binfmt_macho.c \
+
	ssh.c \
+
	pkgbase.c \
+
	pkg_arch.c \
+
	pkg_cudf.c \
+
	pkg_cpe.c \
+
	pkg_jobs_universe.c  pkg_printf.c \
+
	pkg_status.c \
+
	plugins.c \
+
	utils.c fetch.c \
+
	pkg_sandbox.c \
+
	pkg_attributes.c \
+
	pkg_delete.c \
+
	pkg_jobs.c \
+
	pkg_jobs_schedule.c \
+
	pkg_repo_create.c \
+
	pkg_version.c \
+
	rcscripts.c \
+
	flags.c \
+
	fetch_ssh.c \
+
	fetch_libfetch.c \
+
	fetch_file.c \
+
	triggers.c \
+
	pkghash.c \
+
	yuarel.c \
+
	json.c
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
+
		-I$(top_srcdir)/external/blake2 \
+
		-I$(top_srcdir)/external/sqlite \
+
		-I$(top_srcdir)/external/uthash \
+
		-I$(top_srcdir)/external/picosat \
+
		-I$(top_srcdir)/external/yxml \
+
		-I$(top_srcdir)/external/uthash \
+
		-I$(top_srcdir)/external/include \
+
		-I$(top_srcdir)/external/libucl/include \
+
		-I$(top_srcdir)/external/lua/src \
+
		-I$(top_srcdir)/external/liblua/ \
+
		-I$(top_srcdir)/external/libder/libder \
+
		-I$(top_srcdir)/external/libecc/include \
+
		-I$(top_srcdir)/external/fetch \
+
		-I$(top_srcdir)/libpkg/repo \
+
		-I$(top_builddir)/libpkg/repo \
+
		-I$(top_srcdir)/libpkg \
+
		-I$(top_builddir)/libpkg \
+
		-I$(top_srcdir) \
+
		-I$(top_builddir) \
+
		-Wl,--as-needed \
+
		-Wno-pointer-sign \
+
		-Wno-missing-field-initializers \
+
		-Wno-sign-compare \
+
		-DPREFIX=\"$(PREFIX)\" \
+
		-DHAVE_CONFIG_H
+

+
LOCAL_LDFLAGS=	@waflags@ \
+
		@REPOS_LDFLAGS@ \
+
		-L$(top_builddir)/external/libsqlite -lsqlite_pic \
+
		-L$(top_builddir)/external/libucl -lucl_pic \
+
		-L$(top_builddir)/external/libpicosat -lpicosat_pic \
+
		-L$(top_builddir)/external/liblinenoise -llinenoise_pic \
+
		-L$(top_builddir)/external/libyxml -lyxml_pic \
+
		-L$(top_builddir)/external/libblake2 -lblake2_pic \
+
		-L$(top_builddir)/compat -lbsd_compat_pic \
+
		-L$(top_builddir)/external/liblua -llua_pic \
+
		-L$(top_builddir)/external/libder -lder_pic \
+
		-L$(top_builddir)/external/libecc -lecc_pic \
+
		-L$(top_builddir)/external/libfetch -lfetch_pic \
+

+
		@nowaflags@ \
+
		-lm
+

+
STATIC_LIBS=	@REPOS_STATIC_LIBS@ \
+
		$(top_builddir)/external/libsqlite/libsqlite.a \
+
		$(top_builddir)/external/libucl/libucl.a \
+
		$(top_builddir)/external/libpicosat/libpicosat.a \
+
		$(top_builddir)/external/liblinenoise/liblinenoise.a \
+
		$(top_builddir)/external/libyxml/libyxml.a \
+
		$(top_builddir)/external/libblake2/libblake2.a \
+
		$(top_builddir)/external/liblua/liblua.a \
+
		$(top_builddir)/compat/libbsd_compat.a \
+
		$(top_builddir)/external/libder/libder.a \
+
		$(top_builddir)/external/libecc/libecc.a \
+
		$(top_builddir)/external/libfetch/libfetch.a \
+
		lib$(LIB).a
+

+
@if HAVE_LIBUTIL
+
LOCAL_LDFLAGS+=	-lutil
+
@endif
+
@if HAVE_LIBFTS
+
LOCAL_LDFLAGS+=	-lfts
+
@endif
+
@if HAVE_LIBDL
+
LOCAL_LDFLAGS+= -ldl
+
@endif
+

+
@if pkgos_darwin
+
LOCAL_LDFLAGS+=	-lresolv
+
@else
+
@if pkgos_freebsd
+
LOCAL_LDFLAGS+=	-Wl,--version-script=$(top_srcdir)/libpkg/libpkg.ver,--undefined-version
+
@else
+
### --undefined-version is a FreeBSD ld option
+
LOCAL_LDFLAGS+=	-Wl,--version-script=$(top_srcdir)/libpkg/libpkg.ver
+
@endif
+
@endif
+

+
@if libelf-internal
+
LOCAL_CFLAGS+= -I$(top_srcdir)/external/elf
+
STATIC_LIBS+=	$(top_builddir)/external/libelf/libelf.a
+
LOCAL_LDFLAGS+=	-L$(top_builddir)/external/libelf -lelf
+
@endif
+

+
@if libelf-external
+
LOCAL_LDFLAGS+=	-lelf
+
@endif
+

+
@if PKG_LIBARCHIVE_LIBS
+
LOCAL_CFLAGS+=	@PKG_LIBARCHIVE_CFLAGS@
+
LOCAL_LDFLAGS+=	@PKG_LIBARCHIVE_LDFLAGS@ @PKG_LIBARCHIVE_LIBS@
+
@else
+
LOCAL_LDFLAGS+=	-larchive
+
@endif
+

+
@if PKG_OPENSSL_LIBS
+
LOCAL_CFLAGS+=	@PKG_OPENSSL_CFLAGS@
+
LOCAL_LDFLAGS+=	@PKG_OPENSSL_LDFLAGS@ @PKG_OPENSSL_LIBS@
+
@else
+
LOCAL_LDFLAGS+=	-lssl -lcrypto
+
@endif
+

+
@if PKG_LIBLDNS_LIBS
+
LOCAL_CFLAGS+=	@PKG_LIBLDNS_CFLAGS@
+
LOCAL_LDFLAGS+=	@PKG_LIBLDNS_LDFLAGS@ @PKG_LIBLDNS_LIBS@
+
@endif
+

+
VPATH=	$(top_srcdir)/libpkg
+
.PATH:	$(top_srcdir)/libpkg
+

+
include $(MK)/lib.mk
+

+
$(OBJS) $(SHOBJS): $(top_builddir)/pkg_config.h
+

+
all: lib$(LIB)_flat.a
+

+
lib$(LIB)$(LIBSOEXT): $(STATIC_LIBS)
+

+
@if pkgos_darwin
+
lib$(LIB)_flat.a: $(STATIC_LIBS)
+
	libtool -static -o lib$(LIB)_flat.a $(STATIC_LIBS)
+
@else
+
lib$(LIB)_flat.a: ${STATIC_LIBS} mergelib_script
+
	$(AR) -M < mergelib_script
+
@endif
+

+
mergelib_script: $(STATIC_LIBS)
+
	@echo "CREATE lib$(LIB)_flat.a" > $@
+
	@for l in $(STATIC_LIBS); do \
+
		echo "ADDLIB $$l"; \
+
	done >> $@
+
	@echo "SAVE" >> $@
+
	@echo "END" >> $@
+

+
install: all pkg.h lib$(LIB)$(LIBSOEXT) lib$(LIB)_flat.a
+
	install -d -m 755 $(DESTDIR)$(libdir)
+
	install -d -m 755 $(DESTDIR)$(includedir)
+
	install -d -m 755 $(DESTDIR)$(includedir)/pkg
+
	install -d -m 755 $(DESTDIR)$(pkgconfigdir)
+
	install -m 644 lib$(LIB)$(LIBSOEXT) $(DESTDIR)$(libdir)/
+
	ln -sf lib$(LIB)$(LIBSOEXT) $(DESTDIR)$(libdir)/lib$(LIB)$(SH_SOEXT)
+
	install -m 644 lib$(LIB)_flat.a $(DESTDIR)$(libdir)/lib$(LIB).a
+
	install -m 644 pkg.h $(DESTDIR)$(includedir)/
+
	install -m 644 $(top_srcdir)/libpkg/pkg/vec.h $(DESTDIR)$(includedir)/pkg
+
	install -m 644 $(top_srcdir)/libpkg/pkg/audit.h $(DESTDIR)$(includedir)/pkg
+
	install -m 644 pkg.pc $(DESTDIR)$(pkgconfigdir)/
deleted libpkg/repo/Makefile.autosetup
@@ -1,21 +0,0 @@
-
include @builddir@/mk/defs.mk
-
DIRS=	@REPOS@
-

-
all: pkg_repos.h
-

-
pkg_repos.h: $(DIRS)
-
	@echo "#ifndef PKG_REPOS_H" > $@
-
	@echo "#define PKG_REPOS_H" >> $@
-
	@for b in $(DIRS); do \
-
		echo "extern struct pkg_repo_ops pkg_repo_$${b}_ops;" ; \
-
	done >> $@
-
	@echo "struct pkg_repo_ops *repos_ops[] = {" >> $@
-
	@for b in $(DIRS); do \
-
		echo "&pkg_repo_$${b}_ops," ; \
-
	done >> $@
-
	@echo "NULL" >> $@
-
	@echo "};" >> $@
-
	@echo "#endif /* PKG_REPOS_H */" >> $@
-

-
include $(MK)/dir.mk
-

added libpkg/repo/Makefile.in
@@ -0,0 +1,21 @@
+
include @builddir@/mk/defs.mk
+
DIRS=	@REPOS@
+

+
all: pkg_repos.h
+

+
pkg_repos.h: $(DIRS)
+
	@echo "#ifndef PKG_REPOS_H" > $@
+
	@echo "#define PKG_REPOS_H" >> $@
+
	@for b in $(DIRS); do \
+
		echo "extern struct pkg_repo_ops pkg_repo_$${b}_ops;" ; \
+
	done >> $@
+
	@echo "struct pkg_repo_ops *repos_ops[] = {" >> $@
+
	@for b in $(DIRS); do \
+
		echo "&pkg_repo_$${b}_ops," ; \
+
	done >> $@
+
	@echo "NULL" >> $@
+
	@echo "};" >> $@
+
	@echo "#endif /* PKG_REPOS_H */" >> $@
+

+
include $(MK)/dir.mk
+

deleted libpkg/repo/binary/Makefile.autosetup
@@ -1,26 +0,0 @@
-
include @builddir@/mk/defs.mk
-
LIB=	repo-binary
-
SRCS=	binary.c \
-
	common.c \
-
	fetch.c \
-
	init.c \
-
	query.c \
-
	update.c
-
LOCAL_CFLAGS=	-I$(top_srcdir)/libpkg \
-
		-I$(top_builddir)/libpkg \
-
		-I$(top_srcdir)/external/uthash \
-
		-I$(top_srcdir)/external/sqlite \
-
		-I$(top_srcdir)/external/libucl/include \
-
		-I$(top_srcdir)/external/include \
-
		-I$(top_srcdir)/compat \
-
		-I$(top_srcdir)/ \
-
		-I$(top_builddir)/
-

-
VPATH=	$(top_srcdir)/libpkg/repo/binary
-
.PATH:	$(top_srcdir)/libpkg/repo/binary
-

-
@if HAVE_PKG_LIBARCHIVE
-
LOCAL_CFLAGS+=	@PKG_LIBARCHIVE_CFLAGS@
-
@endif
-

-
include $(MK)/static-lib.mk
added libpkg/repo/binary/Makefile.in
@@ -0,0 +1,26 @@
+
include @builddir@/mk/defs.mk
+
LIB=	repo-binary
+
SRCS=	binary.c \
+
	common.c \
+
	fetch.c \
+
	init.c \
+
	query.c \
+
	update.c
+
LOCAL_CFLAGS=	-I$(top_srcdir)/libpkg \
+
		-I$(top_builddir)/libpkg \
+
		-I$(top_srcdir)/external/uthash \
+
		-I$(top_srcdir)/external/sqlite \
+
		-I$(top_srcdir)/external/libucl/include \
+
		-I$(top_srcdir)/external/include \
+
		-I$(top_srcdir)/compat \
+
		-I$(top_srcdir)/ \
+
		-I$(top_builddir)/
+

+
VPATH=	$(top_srcdir)/libpkg/repo/binary
+
.PATH:	$(top_srcdir)/libpkg/repo/binary
+

+
@if HAVE_PKG_LIBARCHIVE
+
LOCAL_CFLAGS+=	@PKG_LIBARCHIVE_CFLAGS@
+
@endif
+

+
include $(MK)/static-lib.mk
deleted scripts/Makefile.autosetup
@@ -1,46 +0,0 @@
-
include @builddir@/mk/defs.mk
-

-
.SUFFIXES: .in
-

-
.in:
-
	sed -e 's|%prefix%|$(PREFIX)|g' $< > $@
-

-
pdailydir=		$(sysconfdir)/periodic/daily
-
psecuritydir=		$(sysconfdir)/periodic/security
-
pweeklydir=		$(sysconfdir)/periodic/weekly
-
bashcompdir=		$(sysconfdir)/bash_completion.d
-
zshcompdir=		$(PREFIX)/share/zsh/site-functions
-

-
PDAILY=	periodic/411.pkg-backup \
-
	periodic/490.status-pkg-changes
-
PSECURITY=	periodic/410.pkg-audit \
-
		periodic/405.pkg-base-audit \
-
		periodic/460.pkg-checksum
-
PWEEKLY=	periodic/400.status-pkg
-
COMPLETION=	completion/_pkg \
-
		completion/_pkg.bash
-

-
VPATH=	$(top_srcdir)/scripts
-

-
all: $(PDAILY) $(PSECURITY) $(PWEEKLY) $(COMPLETION)
-

-
clean:
-
	rm -f $(PDAILY) $(PSECURITY) $(PWEEKLY)
-

-
install: $(PDAILY) $(PSECURITY) $(PWEEKLY) $(COMPLETION)
-
	install -d -m 755 $(DESTDIR)$(pdailydir)
-
	install -d -m 755 $(DESTDIR)$(pweeklydir)
-
	install -d -m 755 $(DESTDIR)$(psecuritydir)
-
	install -d -m 755 $(DESTDIR)$(bashcompdir)
-
	install -d -m 755 $(DESTDIR)$(zshcompdir)
-
	for script in $(PDAILY); do \
-
		install -m 755 $$script $(DESTDIR)$(pdailydir)/ ; \
-
	done
-
	for script in $(PSECURITY); do \
-
		install -m 755 $$script $(DESTDIR)$(psecuritydir)/ ; \
-
	done
-
	for script in $(PWEEKLY); do \
-
		install -m 755 $$script $(DESTDIR)$(pweeklydir)/ ; \
-
	done
-
	install -m 644 completion/_pkg.bash $(DESTDIR)$(bashcompdir)/
-
	install -m 644 completion/_pkg  $(DESTDIR)$(zshcompdir)/
added scripts/Makefile.in
@@ -0,0 +1,46 @@
+
include @builddir@/mk/defs.mk
+

+
.SUFFIXES: .in
+

+
.in:
+
	sed -e 's|%prefix%|$(PREFIX)|g' $< > $@
+

+
pdailydir=		$(sysconfdir)/periodic/daily
+
psecuritydir=		$(sysconfdir)/periodic/security
+
pweeklydir=		$(sysconfdir)/periodic/weekly
+
bashcompdir=		$(sysconfdir)/bash_completion.d
+
zshcompdir=		$(PREFIX)/share/zsh/site-functions
+

+
PDAILY=	periodic/411.pkg-backup \
+
	periodic/490.status-pkg-changes
+
PSECURITY=	periodic/410.pkg-audit \
+
		periodic/405.pkg-base-audit \
+
		periodic/460.pkg-checksum
+
PWEEKLY=	periodic/400.status-pkg
+
COMPLETION=	completion/_pkg \
+
		completion/_pkg.bash
+

+
VPATH=	$(top_srcdir)/scripts
+

+
all: $(PDAILY) $(PSECURITY) $(PWEEKLY) $(COMPLETION)
+

+
clean:
+
	rm -f $(PDAILY) $(PSECURITY) $(PWEEKLY)
+

+
install: $(PDAILY) $(PSECURITY) $(PWEEKLY) $(COMPLETION)
+
	install -d -m 755 $(DESTDIR)$(pdailydir)
+
	install -d -m 755 $(DESTDIR)$(pweeklydir)
+
	install -d -m 755 $(DESTDIR)$(psecuritydir)
+
	install -d -m 755 $(DESTDIR)$(bashcompdir)
+
	install -d -m 755 $(DESTDIR)$(zshcompdir)
+
	for script in $(PDAILY); do \
+
		install -m 755 $$script $(DESTDIR)$(pdailydir)/ ; \
+
	done
+
	for script in $(PSECURITY); do \
+
		install -m 755 $$script $(DESTDIR)$(psecuritydir)/ ; \
+
	done
+
	for script in $(PWEEKLY); do \
+
		install -m 755 $$script $(DESTDIR)$(pweeklydir)/ ; \
+
	done
+
	install -m 644 completion/_pkg.bash $(DESTDIR)$(bashcompdir)/
+
	install -m 644 completion/_pkg  $(DESTDIR)$(zshcompdir)/
deleted src/Makefile.autosetup
@@ -1,131 +0,0 @@
-
include @builddir@/mk/defs.mk
-
PROG=	pkg
-
SRCS=	add.c \
-
	alias.c \
-
	annotate.c \
-
	audit.c \
-
	autoremove.c \
-
	check.c \
-
	clean.c \
-
	config.c \
-
	create.c \
-
	delete.c \
-
	event.c \
-
	fetch.c \
-
	globals.c \
-
	info.c \
-
	install.c \
-
	key.c \
-
	lock.c \
-
	main.c \
-
	plugins.c \
-
	query.c \
-
	register.c \
-
	repo.c \
-
	repositories.c \
-
	rquery.c \
-
	search.c \
-
	set.c \
-
	shell.c \
-
	shlib.c \
-
	ssh.c \
-
	stats.c \
-
	triggers.c \
-
	unregister.c \
-
	update.c \
-
	updating.c \
-
	upgrade.c \
-
	utils.c \
-
	version.c \
-
	which.c
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
-
		-I$(top_srcdir)/external/libucl/include \
-
		-I$(top_srcdir)/external/include \
-
		-I$(top_builddir)/ \
-
		-I$(top_srcdir)/libpkg \
-
		-I$(top_builddir)/libpkg \
-
		-DGITHASH=\"@GITHASH@\" \
-
		-DHAVE_CONFIG_H \
-
		-Werror=switch
-
LIBPKGFLAT=	-L$(top_builddir)/libpkg -lpkg_flat
-
OTHER_LIBS=	@EXTRA_LIBS@ -lm -pthread
-

-
@if HAVE_PKG_LIBARCHIVE
-
OTHER_LIBS+=	@PKG_LIBARCHIVE_LDFLAGS@ @PKG_LIBARCHIVE_LIBS@
-
LOCAL_CFLAGS+=	@PKG_LIBARCHIVE_CFLAGS@
-
@else
-
OTHER_LIBS+=	-larchive
-
@endif
-

-
@if HAVE_LIBUTIL
-
OTHER_LIBS+=	-lutil
-
@endif
-

-
@if HAVE_LIBFTS
-
OTHER_LIBS+=	-lfts
-
@endif
-

-
@if HAVE_LIBDL
-
OTHER_LIBS+=	-ldl
-
@endif
-

-
@if PKG_OPENSSL_LIBS
-
LOCAL_CFLAGS+=	@PKG_OPENSSL_CFLAGS@
-
OTHER_LIBS+=	@PKG_OPENSSL_LDFLAGS@ @PKG_OPENSSL_LIBS@
-
@else
-
OTHER_LIBS+=	-lssl -lcrypto
-
@endif
-

-
# libmd must be linked after libssl/libcrypto.
-
@if HAVE_LIBMD
-
OTHER_LIBS+=	-lmd
-
@endif
-

-
@if pkgos_darwin
-
LOCAL_LDFLAGS=	$(LIBPKGFLAT) $(LIBS) $(OTHER_LIBS) -lresolv
-
STATIC_LDFLAGS=	$(LIBPKGFLAT) $(LIBS) $(OTHER_LIBS) -lresolv
-
# OSX doesn't support static binaries, sigh
-
STATIC_ARG=
-
@else
-
LOCAL_LDFLAGS=	-Wl,-Bstatic \
-
		@waflags@ $(LIBPKGFLAT) @nowaflags@ \
-
		$(LIBS) \
-
		-Wl,-Bdynamic -Wl,--as-needed $(OTHER_LIBS) \
-
		-Wl,--export-dynamic
-
STATIC_ARG=	-static
-
STATIC_LDFLAGS=	$(LIBPKGFLAT) $(LIBS) $(OTHER_LIBS)
-
@endif
-

-
# For libarchive
-
STATIC_LDFLAGS+=	-lbz2 -lz -llzma @ZSTDLIB@
-

-
VPATH=	$(top_srcdir)/src
-

-
include $(MK)/prog.mk
-

-
@if HAVE_STATIC
-
all: pkg-static
-
install: install-static
-

-
pkg-static: $(OBJS) $(top_builddir)/libpkg/libpkg_flat.a
-
	$(CC) -o $@ $(OBJS) $(STATIC_ARG) $(LDFLAGS) $(STATIC_LDFLAGS)
-

-
install-static: pkg-static
-
	install -d -m 755 $(DESTDIR)$(sbindir)
-
	install -m 755 pkg-static $(DESTDIR)$(sbindir)/pkg-static
-
@endif
-

-
${OBJS}: $(top_builddir)/pkg_config.h
-

-
$(PROG): $(top_builddir)/libpkg/libpkg_flat.a
-

-
install: $(PROG)
-
	install -d -m 755 $(DESTDIR)$(sbindir)
-
	install -m 755 pkg $(DESTDIR)$(sbindir)/pkg
-
	install -d -m 755 $(DESTDIR)$(etcdir)
-
	install -m 644 $(top_srcdir)/src/pkg.conf.sample $(DESTDIR)$(etcdir)/
-

-
clean: clean-pkg-static
-
clean-pkg-static:
-
	rm -f pkg-static
added src/Makefile.in
@@ -0,0 +1,131 @@
+
include @builddir@/mk/defs.mk
+
PROG=	pkg
+
SRCS=	add.c \
+
	alias.c \
+
	annotate.c \
+
	audit.c \
+
	autoremove.c \
+
	check.c \
+
	clean.c \
+
	config.c \
+
	create.c \
+
	delete.c \
+
	event.c \
+
	fetch.c \
+
	globals.c \
+
	info.c \
+
	install.c \
+
	key.c \
+
	lock.c \
+
	main.c \
+
	plugins.c \
+
	query.c \
+
	register.c \
+
	repo.c \
+
	repositories.c \
+
	rquery.c \
+
	search.c \
+
	set.c \
+
	shell.c \
+
	shlib.c \
+
	ssh.c \
+
	stats.c \
+
	triggers.c \
+
	unregister.c \
+
	update.c \
+
	updating.c \
+
	upgrade.c \
+
	utils.c \
+
	version.c \
+
	which.c
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
+
		-I$(top_srcdir)/external/libucl/include \
+
		-I$(top_srcdir)/external/include \
+
		-I$(top_builddir)/ \
+
		-I$(top_srcdir)/libpkg \
+
		-I$(top_builddir)/libpkg \
+
		-DGITHASH=\"@GITHASH@\" \
+
		-DHAVE_CONFIG_H \
+
		-Werror=switch
+
LIBPKGFLAT=	-L$(top_builddir)/libpkg -lpkg_flat
+
OTHER_LIBS=	@EXTRA_LIBS@ -lm -pthread
+

+
@if HAVE_PKG_LIBARCHIVE
+
OTHER_LIBS+=	@PKG_LIBARCHIVE_LDFLAGS@ @PKG_LIBARCHIVE_LIBS@
+
LOCAL_CFLAGS+=	@PKG_LIBARCHIVE_CFLAGS@
+
@else
+
OTHER_LIBS+=	-larchive
+
@endif
+

+
@if HAVE_LIBUTIL
+
OTHER_LIBS+=	-lutil
+
@endif
+

+
@if HAVE_LIBFTS
+
OTHER_LIBS+=	-lfts
+
@endif
+

+
@if HAVE_LIBDL
+
OTHER_LIBS+=	-ldl
+
@endif
+

+
@if PKG_OPENSSL_LIBS
+
LOCAL_CFLAGS+=	@PKG_OPENSSL_CFLAGS@
+
OTHER_LIBS+=	@PKG_OPENSSL_LDFLAGS@ @PKG_OPENSSL_LIBS@
+
@else
+
OTHER_LIBS+=	-lssl -lcrypto
+
@endif
+

+
# libmd must be linked after libssl/libcrypto.
+
@if HAVE_LIBMD
+
OTHER_LIBS+=	-lmd
+
@endif
+

+
@if pkgos_darwin
+
LOCAL_LDFLAGS=	$(LIBPKGFLAT) $(LIBS) $(OTHER_LIBS) -lresolv
+
STATIC_LDFLAGS=	$(LIBPKGFLAT) $(LIBS) $(OTHER_LIBS) -lresolv
+
# OSX doesn't support static binaries, sigh
+
STATIC_ARG=
+
@else
+
LOCAL_LDFLAGS=	-Wl,-Bstatic \
+
		@waflags@ $(LIBPKGFLAT) @nowaflags@ \
+
		$(LIBS) \
+
		-Wl,-Bdynamic -Wl,--as-needed $(OTHER_LIBS) \
+
		-Wl,--export-dynamic
+
STATIC_ARG=	-static
+
STATIC_LDFLAGS=	$(LIBPKGFLAT) $(LIBS) $(OTHER_LIBS)
+
@endif
+

+
# For libarchive
+
STATIC_LDFLAGS+=	-lbz2 -lz -llzma @ZSTDLIB@
+

+
VPATH=	$(top_srcdir)/src
+

+
include $(MK)/prog.mk
+

+
@if HAVE_STATIC
+
all: pkg-static
+
install: install-static
+

+
pkg-static: $(OBJS) $(top_builddir)/libpkg/libpkg_flat.a
+
	$(CC) -o $@ $(OBJS) $(STATIC_ARG) $(LDFLAGS) $(STATIC_LDFLAGS)
+

+
install-static: pkg-static
+
	install -d -m 755 $(DESTDIR)$(sbindir)
+
	install -m 755 pkg-static $(DESTDIR)$(sbindir)/pkg-static
+
@endif
+

+
${OBJS}: $(top_builddir)/pkg_config.h
+

+
$(PROG): $(top_builddir)/libpkg/libpkg_flat.a
+

+
install: $(PROG)
+
	install -d -m 755 $(DESTDIR)$(sbindir)
+
	install -m 755 pkg $(DESTDIR)$(sbindir)/pkg
+
	install -d -m 755 $(DESTDIR)$(etcdir)
+
	install -m 644 $(top_srcdir)/src/pkg.conf.sample $(DESTDIR)$(etcdir)/
+

+
clean: clean-pkg-static
+
clean-pkg-static:
+
	rm -f pkg-static
deleted tests/Makefile.autosetup
@@ -1,242 +0,0 @@
-
include @builddir@/mk/defs.mk
-

-
TESTS=	\
-
	checksum \
-
	deps_formula \
-
	merge \
-
	packing \
-
	pkg_add_dir_to_del \
-
	pkg_cpe \
-
	pkg_printf \
-
	pkg_validation \
-
	pkg_osvf \
-
	pkg \
-
	plist \
-
	lua \
-
	ssh \
-
	utils \
-
	metalog \
-
	vec \
-
	pkg_elf \
-
	hash \
-
	shlibs \
-
	kv
-

-
TESTS_SH= \
-
	frontend/audit.sh \
-
	frontend/pkg.sh \
-
	frontend/add.sh \
-
	frontend/alias.sh \
-
	frontend/annotate.sh \
-
	frontend/autoremove.sh \
-
	frontend/autoupgrade.sh \
-
	frontend/check.sh \
-
	frontend/config.sh \
-
	frontend/configmerge.sh \
-
	frontend/conflicts.sh \
-
	frontend/conflicts-multirepo.sh \
-
	frontend/create.sh \
-
	frontend/create-parsebin.sh \
-
	frontend/delete.sh \
-
	frontend/extract.sh \
-
	frontend/fetch.sh \
-
	frontend/install.sh \
-
	frontend/jpeg.sh \
-
	frontend/key.sh \
-
	frontend/lock.sh \
-
	frontend/messages.sh \
-
	frontend/multipleprovider.sh \
-
	frontend/packagesplit.sh \
-
	frontend/packagemerge.sh \
-
	frontend/php-pr.sh \
-
	frontend/pubkey.sh \
-
	frontend/query.sh \
-
	frontend/register.sh \
-
	frontend/repo.sh \
-
	frontend/rquery.sh \
-
	frontend/requires.sh \
-
	frontend/rootdir.sh \
-
	frontend/rubypuppet.sh \
-
	frontend/search.sh \
-
	frontend/set.sh \
-
	frontend/version.sh \
-
	frontend/vital.sh \
-
	frontend/which.sh \
-
	frontend/unregister.sh \
-
	frontend/update.sh \
-
	frontend/updating.sh \
-
	frontend/upgrade.sh \
-
	frontend/issue1374.sh \
-
	frontend/issue1425.sh \
-
	frontend/issue1440.sh \
-
	frontend/issue1445.sh \
-
	frontend/fingerprint.sh \
-
	frontend/lua.sh \
-
	frontend/shellscript.sh \
-
	frontend/clean.sh \
-
	frontend/backup_lib.sh \
-
	frontend/abi.sh \
-
	frontend/http.sh \
-
	frontend/triggers.sh \
-
	frontend/info.sh \
-
	frontend/force_reinstall_shlib.sh
-

-
#
-
# These files are mostly simple binaries obtained from
-
# int main(void) { return 0; }
-
#
-
# The freebsd-*.bin files are copies of /usr/bin/uname from official
-
# 14.1 release artifacts for the given architecture.
-

-

-
TESTS_SHELL_BINS= \
-
	frontend/dfly.bin \
-
	frontend/freebsd-aarch64.bin \
-
	frontend/freebsd-amd64.bin \
-
	frontend/freebsd-armv6.bin \
-
	frontend/freebsd-armv7.bin \
-
	frontend/freebsd-i386.bin \
-
	frontend/freebsd-powerpc.bin \
-
	frontend/freebsd-powerpc64.bin \
-
	frontend/freebsd-powerpc64le.bin \
-
	frontend/freebsd-riscv64.bin \
-
	frontend/linux.bin \
-
	frontend/macos.bin \
-
	frontend/macos106.bin \
-
	frontend/macos150.bin \
-
	frontend/macosfat.bin \
-
	frontend/macosfatlib.bin \
-
	frontend/libtestfbsd.so.1 \
-
	frontend/libtest2fbsd.so.1 \
-
	frontend/libfoo.so.1
-

-
lua_OBJS=	lib/lua.o
-
merge_OBJS=	lib/merge.o
-
plist_OBJS=	lib/plist.o
-
checksum_OBJS=	lib/checksum.o
-
pkg_printf_OBJS=	lib/pkg_printf_test.o \
-
			lib/pkg_printf.o
-
deps_formula_OBJS=	lib/deps_formula.o
-
pkg_add_dir_to_del_OBJS=	lib/pkg_add_dir_to_del.o
-
pkg_cpe_OBJS=	lib/pkg_cpe.o
-
pkg_validation_OBJS=	lib/pkg_validation.o
-
packing_OBJS=	lib/packing.o
-
ssh_OBJS=	lib/ssh.o
-
utils_OBJS=	lib/utils.o
-
metalog_OBJS=	lib/metalog.o
-
vec_OBJS=	lib/vec.o
-
pkg_elf_OBJS=	lib/pkg_elf.o
-
hash_OBJS=	lib/hash.o
-
shlibs_OBJS=	lib/shlibs.o
-
kv_OBJS=	lib/kv.o
-
pkg_OBJS=	lib/pkg.o
-
pkg_osvf_OBJS=	lib/pkg_osvf.o
-

-
SRCS=	\
-
	$(packing_OBJS:.o=.c) \
-
	$(merge_OBJS:.o=.c) \
-
	$(plist_OBJS:.o=.c) \
-
	$(lua_OBJS:.o=.c) \
-
	$(checksum_OBJS:.o=.c) \
-
	$(pkg_printf_OBJS:.o=.c) \
-
	$(deps_formula_OBJS:.o=.c) \
-
	$(pkg_add_dir_to_del_OBJS:.o=.c) \
-
	$(pkg_cpe_OBJS:.o=.c) \
-
	$(pkg_validation_OBJS:.o=.c) \
-
	$(pkg_osvf_OBJS:.o=.c) \
-
	$(ssh_OBJS:.o=.c) \
-
	$(utils_OBJS:.o=.c) \
-
	$(metalog_OBJS:.o=.c) \
-
	$(vec_OBJS:.o=.c) \
-
	$(pkg_elf_OBJS:.o=.c) \
-
	$(hash_OBJS:.o=.c) \
-
	$(shlibs_OBJS:.o=.c) \
-
	$(kv_OBJS:.o=.c) \
-
	$(pkg_OBJS:.o=.c)
-

-
include $(MK)/common.mk
-

-
TESTS_SHELL=	$(TESTS_SH:.sh=)
-

-
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
-
		-I$(top_srcdir)/external/libucl/include \
-
		-I$(top_srcdir)/external/uthash \
-
		-I$(top_srcdir)/external/include \
-
		-I$(top_srcdir)/external/sqlite \
-
		-I$(top_srcdir)/external/lua/src \
-
		-I$(top_srcdir)/libpkg \
-
		-I$(top_builddir)/ \
-
		-I$(top_builddir)/libpkg \
-
		@PKG_ATF_C_CFLAGS@ \
-
		-DTESTING \
-
		-DTESTING_TOP_DIR="\"@abs_top_srcdir@/tests\""
-

-
LIBS=	\
-
	-L$(top_builddir)/libpkg -lpkg_flat \
-
	-lm \
-
	@EXTRA_LIBS@
-

-
@if HAVE_PKG_LIBARCHIVE
-
CFLAGS+=	@PKG_LIBARCHIVE_CFLAGS@
-
LIBS+=	@PKG_LIBARCHIVE_LDFLAGS@ @PKG_LIBARCHIVE_LIBS@
-
@else
-
LIBS+=	-L/usr/lib -larchive -lbz2 -lz -llzma
-
@endif
-
OTHER_LIBS=	-pthread
-
@if HAVE_LIBUTIL
-
OTHER_LIBS+=	-lutil
-
@endif
-
@if HAVE_LIBFTS
-
OTHER_LIBS+=	-lfts
-
@endif
-
@if HAVE_LIBDL
-
OTHER_LIBS+=	-ldl
-
@endif
-
@if PKG_OPENSSL_LIBS
-
CFLAGS+=	@PKG_OPENSSL_CFLAGS@
-
LIBS+=	@PKG_OPENSSL_LDFLAGS@ @PKG_OPENSSL_LIBS@
-
@else
-
OTHER_LIBS+=	-lssl -lcrypto
-
@endif
-

-

-
# Hack to determine we are on osx
-
@if pkgos_darwin
-
OTHER_LIBS+=	-lresolv
-
@endif
-

-
LOCAL_LDFLAGS=	$(LIBS) $(OTHER_LIBS)
-
LOCAL_LDFLAGS+=	@PKG_ATF_C_LDFLAGS@ @PKG_ATF_C_LIBS@
-

-
VPATH=	$(top_srcdir)/tests
-

-
all: $(TESTS) Kyuafile ${TESTS_SHELL} $(TESTS_SHELL_BINS) frontend/Kyuafile
-

-
$(TESTS): $(OBJS) $(top_builddir)/libpkg/libpkg_flat.a
-
	$(CC) -o $@ $($@_OBJS) $(LDFLAGS) $(LOCAL_LDFLAGS)
-

-
lib/pkg_printf.c: $(top_srcdir)/libpkg/pkg_printf.c
-
	cp $(top_srcdir)/libpkg/pkg_printf.c $@
-

-
.sh:
-
	install -m 755 $< $@
-

-
clean:
-
	rm -f lib/pkg_printf.c $(OBJS) $(TESTS) $(TESTS_SHELL) $(TESTS_SHELL_BINS) Kyuafile frontend/Kyuafile
-

-
Kyuafile: $(TESTS)
-
	@echo "Generating $@"
-
	@echo 'syntax(2)' > $@
-
	@echo 'test_suite("pkg")' >> $@
-
	@for p in $(TESTS); do \
-
		echo "atf_test_program{name='$$p'}" >> $@ ; \
-
	done
-
	@echo 'include("frontend/Kyuafile")' >> $@
-

-
frontend/Kyuafile: $(TESTS_SHELL) $(TESTS_SHELL_BINS)
-
	@echo "Generating $@"
-
	@echo 'syntax(2)' > $@
-
	@echo 'test_suite("pkg")' >> $@
-
	@for p in $(TESTS_SHELL); do \
-
		echo "atf_test_program{name='$${p##*/}'}" >> $@ ; \
-
	done
added tests/Makefile.in
@@ -0,0 +1,242 @@
+
include @builddir@/mk/defs.mk
+

+
TESTS=	\
+
	checksum \
+
	deps_formula \
+
	merge \
+
	packing \
+
	pkg_add_dir_to_del \
+
	pkg_cpe \
+
	pkg_printf \
+
	pkg_validation \
+
	pkg_osvf \
+
	pkg \
+
	plist \
+
	lua \
+
	ssh \
+
	utils \
+
	metalog \
+
	vec \
+
	pkg_elf \
+
	hash \
+
	shlibs \
+
	kv
+

+
TESTS_SH= \
+
	frontend/audit.sh \
+
	frontend/pkg.sh \
+
	frontend/add.sh \
+
	frontend/alias.sh \
+
	frontend/annotate.sh \
+
	frontend/autoremove.sh \
+
	frontend/autoupgrade.sh \
+
	frontend/check.sh \
+
	frontend/config.sh \
+
	frontend/configmerge.sh \
+
	frontend/conflicts.sh \
+
	frontend/conflicts-multirepo.sh \
+
	frontend/create.sh \
+
	frontend/create-parsebin.sh \
+
	frontend/delete.sh \
+
	frontend/extract.sh \
+
	frontend/fetch.sh \
+
	frontend/install.sh \
+
	frontend/jpeg.sh \
+
	frontend/key.sh \
+
	frontend/lock.sh \
+
	frontend/messages.sh \
+
	frontend/multipleprovider.sh \
+
	frontend/packagesplit.sh \
+
	frontend/packagemerge.sh \
+
	frontend/php-pr.sh \
+
	frontend/pubkey.sh \
+
	frontend/query.sh \
+
	frontend/register.sh \
+
	frontend/repo.sh \
+
	frontend/rquery.sh \
+
	frontend/requires.sh \
+
	frontend/rootdir.sh \
+
	frontend/rubypuppet.sh \
+
	frontend/search.sh \
+
	frontend/set.sh \
+
	frontend/version.sh \
+
	frontend/vital.sh \
+
	frontend/which.sh \
+
	frontend/unregister.sh \
+
	frontend/update.sh \
+
	frontend/updating.sh \
+
	frontend/upgrade.sh \
+
	frontend/issue1374.sh \
+
	frontend/issue1425.sh \
+
	frontend/issue1440.sh \
+
	frontend/issue1445.sh \
+
	frontend/fingerprint.sh \
+
	frontend/lua.sh \
+
	frontend/shellscript.sh \
+
	frontend/clean.sh \
+
	frontend/backup_lib.sh \
+
	frontend/abi.sh \
+
	frontend/http.sh \
+
	frontend/triggers.sh \
+
	frontend/info.sh \
+
	frontend/force_reinstall_shlib.sh
+

+
#
+
# These files are mostly simple binaries obtained from
+
# int main(void) { return 0; }
+
#
+
# The freebsd-*.bin files are copies of /usr/bin/uname from official
+
# 14.1 release artifacts for the given architecture.
+

+

+
TESTS_SHELL_BINS= \
+
	frontend/dfly.bin \
+
	frontend/freebsd-aarch64.bin \
+
	frontend/freebsd-amd64.bin \
+
	frontend/freebsd-armv6.bin \
+
	frontend/freebsd-armv7.bin \
+
	frontend/freebsd-i386.bin \
+
	frontend/freebsd-powerpc.bin \
+
	frontend/freebsd-powerpc64.bin \
+
	frontend/freebsd-powerpc64le.bin \
+
	frontend/freebsd-riscv64.bin \
+
	frontend/linux.bin \
+
	frontend/macos.bin \
+
	frontend/macos106.bin \
+
	frontend/macos150.bin \
+
	frontend/macosfat.bin \
+
	frontend/macosfatlib.bin \
+
	frontend/libtestfbsd.so.1 \
+
	frontend/libtest2fbsd.so.1 \
+
	frontend/libfoo.so.1
+

+
lua_OBJS=	lib/lua.o
+
merge_OBJS=	lib/merge.o
+
plist_OBJS=	lib/plist.o
+
checksum_OBJS=	lib/checksum.o
+
pkg_printf_OBJS=	lib/pkg_printf_test.o \
+
			lib/pkg_printf.o
+
deps_formula_OBJS=	lib/deps_formula.o
+
pkg_add_dir_to_del_OBJS=	lib/pkg_add_dir_to_del.o
+
pkg_cpe_OBJS=	lib/pkg_cpe.o
+
pkg_validation_OBJS=	lib/pkg_validation.o
+
packing_OBJS=	lib/packing.o
+
ssh_OBJS=	lib/ssh.o
+
utils_OBJS=	lib/utils.o
+
metalog_OBJS=	lib/metalog.o
+
vec_OBJS=	lib/vec.o
+
pkg_elf_OBJS=	lib/pkg_elf.o
+
hash_OBJS=	lib/hash.o
+
shlibs_OBJS=	lib/shlibs.o
+
kv_OBJS=	lib/kv.o
+
pkg_OBJS=	lib/pkg.o
+
pkg_osvf_OBJS=	lib/pkg_osvf.o
+

+
SRCS=	\
+
	$(packing_OBJS:.o=.c) \
+
	$(merge_OBJS:.o=.c) \
+
	$(plist_OBJS:.o=.c) \
+
	$(lua_OBJS:.o=.c) \
+
	$(checksum_OBJS:.o=.c) \
+
	$(pkg_printf_OBJS:.o=.c) \
+
	$(deps_formula_OBJS:.o=.c) \
+
	$(pkg_add_dir_to_del_OBJS:.o=.c) \
+
	$(pkg_cpe_OBJS:.o=.c) \
+
	$(pkg_validation_OBJS:.o=.c) \
+
	$(pkg_osvf_OBJS:.o=.c) \
+
	$(ssh_OBJS:.o=.c) \
+
	$(utils_OBJS:.o=.c) \
+
	$(metalog_OBJS:.o=.c) \
+
	$(vec_OBJS:.o=.c) \
+
	$(pkg_elf_OBJS:.o=.c) \
+
	$(hash_OBJS:.o=.c) \
+
	$(shlibs_OBJS:.o=.c) \
+
	$(kv_OBJS:.o=.c) \
+
	$(pkg_OBJS:.o=.c)
+

+
include $(MK)/common.mk
+

+
TESTS_SHELL=	$(TESTS_SH:.sh=)
+

+
LOCAL_CFLAGS=	-I$(top_srcdir)/compat \
+
		-I$(top_srcdir)/external/libucl/include \
+
		-I$(top_srcdir)/external/uthash \
+
		-I$(top_srcdir)/external/include \
+
		-I$(top_srcdir)/external/sqlite \
+
		-I$(top_srcdir)/external/lua/src \
+
		-I$(top_srcdir)/libpkg \
+
		-I$(top_builddir)/ \
+
		-I$(top_builddir)/libpkg \
+
		@PKG_ATF_C_CFLAGS@ \
+
		-DTESTING \
+
		-DTESTING_TOP_DIR="\"@abs_top_srcdir@/tests\""
+

+
LIBS=	\
+
	-L$(top_builddir)/libpkg -lpkg_flat \
+
	-lm \
+
	@EXTRA_LIBS@
+

+
@if HAVE_PKG_LIBARCHIVE
+
CFLAGS+=	@PKG_LIBARCHIVE_CFLAGS@
+
LIBS+=	@PKG_LIBARCHIVE_LDFLAGS@ @PKG_LIBARCHIVE_LIBS@
+
@else
+
LIBS+=	-L/usr/lib -larchive -lbz2 -lz -llzma
+
@endif
+
OTHER_LIBS=	-pthread
+
@if HAVE_LIBUTIL
+
OTHER_LIBS+=	-lutil
+
@endif
+
@if HAVE_LIBFTS
+
OTHER_LIBS+=	-lfts
+
@endif
+
@if HAVE_LIBDL
+
OTHER_LIBS+=	-ldl
+
@endif
+
@if PKG_OPENSSL_LIBS
+
CFLAGS+=	@PKG_OPENSSL_CFLAGS@
+
LIBS+=	@PKG_OPENSSL_LDFLAGS@ @PKG_OPENSSL_LIBS@
+
@else
+
OTHER_LIBS+=	-lssl -lcrypto
+
@endif
+

+

+
# Hack to determine we are on osx
+
@if pkgos_darwin
+
OTHER_LIBS+=	-lresolv
+
@endif
+

+
LOCAL_LDFLAGS=	$(LIBS) $(OTHER_LIBS)
+
LOCAL_LDFLAGS+=	@PKG_ATF_C_LDFLAGS@ @PKG_ATF_C_LIBS@
+

+
VPATH=	$(top_srcdir)/tests
+

+
all: $(TESTS) Kyuafile ${TESTS_SHELL} $(TESTS_SHELL_BINS) frontend/Kyuafile
+

+
$(TESTS): $(OBJS) $(top_builddir)/libpkg/libpkg_flat.a
+
	$(CC) -o $@ $($@_OBJS) $(LDFLAGS) $(LOCAL_LDFLAGS)
+

+
lib/pkg_printf.c: $(top_srcdir)/libpkg/pkg_printf.c
+
	cp $(top_srcdir)/libpkg/pkg_printf.c $@
+

+
.sh:
+
	install -m 755 $< $@
+

+
clean:
+
	rm -f lib/pkg_printf.c $(OBJS) $(TESTS) $(TESTS_SHELL) $(TESTS_SHELL_BINS) Kyuafile frontend/Kyuafile
+

+
Kyuafile: $(TESTS)
+
	@echo "Generating $@"
+
	@echo 'syntax(2)' > $@
+
	@echo 'test_suite("pkg")' >> $@
+
	@for p in $(TESTS); do \
+
		echo "atf_test_program{name='$$p'}" >> $@ ; \
+
	done
+
	@echo 'include("frontend/Kyuafile")' >> $@
+

+
frontend/Kyuafile: $(TESTS_SHELL) $(TESTS_SHELL_BINS)
+
	@echo "Generating $@"
+
	@echo 'syntax(2)' > $@
+
	@echo 'test_suite("pkg")' >> $@
+
	@for p in $(TESTS_SHELL); do \
+
		echo "atf_test_program{name='$${p##*/}'}" >> $@ ; \
+
	done
modified tests/lib/ssh.c
@@ -110,7 +110,7 @@ ATF_TC_BODY(getfile, tc)
	dprintf(stdin_pipe[1], "get /nonexistent 0\n");
	mkdir("test", 0755);
	dprintf(stdin_pipe[1], "get test 0\n");
-
	dprintf(stdin_pipe[1], "get %s/Makefile.autosetup 0\n", atf_tc_get_config_var(tc, "srcdir"));
+
	dprintf(stdin_pipe[1], "get %s/Makefile.in 0\n", atf_tc_get_config_var(tc, "srcdir"));
	dprintf(stdin_pipe[1], "quit\n");
	atf_utils_wait(p, 0, strout, "");
}