xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/begin.c (revision e2f4f3dab373b605b62ac175115f264a5c417eb6)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*7257d1b4Sraf 
22*7257d1b4Sraf /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*7257d1b4Sraf  * Use is subject to license terms.
25*7257d1b4Sraf  */
26*7257d1b4Sraf 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
297c478bd9Sstevel@tonic-gate  *	All Rights Reserved
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <ar.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <memory.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <libelf.h>
377c478bd9Sstevel@tonic-gate #include <sys/mman.h>
387c478bd9Sstevel@tonic-gate #include "decl.h"
397c478bd9Sstevel@tonic-gate #include "member.h"
407c478bd9Sstevel@tonic-gate #include "msg.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static const char	armag[] = ARMAG;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Initialize archive member
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate Elf *
_elf_member(int fd,Elf * ref,unsigned flags)497c478bd9Sstevel@tonic-gate _elf_member(int fd, Elf * ref, unsigned flags)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	register Elf	*elf;
527c478bd9Sstevel@tonic-gate 	Member		*mh;
537c478bd9Sstevel@tonic-gate 	size_t		base;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if (ref->ed_nextoff >= ref->ed_fsz)
567c478bd9Sstevel@tonic-gate 		return (0);
577c478bd9Sstevel@tonic-gate 	if (ref->ed_fd == -1)		/* disabled */
587c478bd9Sstevel@tonic-gate 		fd = -1;
597c478bd9Sstevel@tonic-gate 	if (flags & EDF_WRITE) {
607c478bd9Sstevel@tonic-gate 		_elf_seterr(EREQ_ARRDWR, 0);
617c478bd9Sstevel@tonic-gate 		return (0);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 	if (ref->ed_fd != fd) {
647c478bd9Sstevel@tonic-gate 		_elf_seterr(EREQ_ARMEMFD, 0);
657c478bd9Sstevel@tonic-gate 		return (0);
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	if ((_elf_vm(ref, ref->ed_nextoff, sizeof (struct ar_hdr)) !=
687c478bd9Sstevel@tonic-gate 	    OK_YES) || ((mh = _elf_armem(ref,
697c478bd9Sstevel@tonic-gate 	    ref->ed_ident + ref->ed_nextoff, ref->ed_fsz)) == 0))
707c478bd9Sstevel@tonic-gate 		return (0);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	base = ref->ed_nextoff + sizeof (struct ar_hdr);
737c478bd9Sstevel@tonic-gate 	if (ref->ed_fsz - base < mh->m_hdr.ar_size) {
747c478bd9Sstevel@tonic-gate 		_elf_seterr(EFMT_ARMEMSZ, 0);
757c478bd9Sstevel@tonic-gate 		return (0);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
787c478bd9Sstevel@tonic-gate 		_elf_seterr(EMEM_ELF, errno);
797c478bd9Sstevel@tonic-gate 		return (0);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	++ref->ed_activ;
827c478bd9Sstevel@tonic-gate 	elf->ed_parent = ref;
837c478bd9Sstevel@tonic-gate 	elf->ed_fd = fd;
847c478bd9Sstevel@tonic-gate 	elf->ed_myflags |= flags;
857c478bd9Sstevel@tonic-gate 	elf->ed_armem = mh;
867c478bd9Sstevel@tonic-gate 	elf->ed_fsz = mh->m_hdr.ar_size;
877c478bd9Sstevel@tonic-gate 	elf->ed_baseoff = ref->ed_baseoff + base;
887c478bd9Sstevel@tonic-gate 	elf->ed_memoff = base - mh->m_slide;
897c478bd9Sstevel@tonic-gate 	elf->ed_siboff = base + elf->ed_fsz + (elf->ed_fsz & 1);
907c478bd9Sstevel@tonic-gate 	ref->ed_nextoff = elf->ed_siboff;
917c478bd9Sstevel@tonic-gate 	elf->ed_image = ref->ed_image;
927c478bd9Sstevel@tonic-gate 	elf->ed_imagesz = ref->ed_imagesz;
937c478bd9Sstevel@tonic-gate 	elf->ed_vm = ref->ed_vm;
947c478bd9Sstevel@tonic-gate 	elf->ed_vmsz = ref->ed_vmsz;
957c478bd9Sstevel@tonic-gate 	elf->ed_ident = ref->ed_ident + base - mh->m_slide;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * If this member is the archive string table,
997c478bd9Sstevel@tonic-gate 	 * we've already altered the bytes.
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (ref->ed_arstroff == ref->ed_nextoff)
1037c478bd9Sstevel@tonic-gate 		elf->ed_status = ES_COOKED;
1047c478bd9Sstevel@tonic-gate 	return (elf);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate Elf *
_elf_regular(int fd,unsigned flags)1097c478bd9Sstevel@tonic-gate _elf_regular(int fd, unsigned flags)		/* initialize regular file */
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	Elf		*elf;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
1147c478bd9Sstevel@tonic-gate 		_elf_seterr(EMEM_ELF, errno);
1157c478bd9Sstevel@tonic-gate 		return (0);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	elf->ed_fd = fd;
1197c478bd9Sstevel@tonic-gate 	elf->ed_myflags |= flags;
1207c478bd9Sstevel@tonic-gate 	if (_elf_inmap(elf) != OK_YES) {
1217c478bd9Sstevel@tonic-gate 		free(elf);
1227c478bd9Sstevel@tonic-gate 		return (0);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	return (elf);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate Elf *
_elf_config(Elf * elf)1297c478bd9Sstevel@tonic-gate _elf_config(Elf * elf)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	char		*base;
1327c478bd9Sstevel@tonic-gate 	unsigned	encode;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	ELFRWLOCKINIT(&elf->ed_rwlock);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * Determine if this is a ELF file.
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 	base = elf->ed_ident;
1407c478bd9Sstevel@tonic-gate 	if ((elf->ed_fsz >= EI_NIDENT) &&
1417c478bd9Sstevel@tonic-gate 	    (_elf_vm(elf, (size_t)0, (size_t)EI_NIDENT) == OK_YES) &&
1427c478bd9Sstevel@tonic-gate 	    (base[EI_MAG0] == ELFMAG0) &&
1437c478bd9Sstevel@tonic-gate 	    (base[EI_MAG1] == ELFMAG1) &&
1447c478bd9Sstevel@tonic-gate 	    (base[EI_MAG2] == ELFMAG2) &&
1457c478bd9Sstevel@tonic-gate 	    (base[EI_MAG3] == ELFMAG3)) {
1467c478bd9Sstevel@tonic-gate 		elf->ed_kind = ELF_K_ELF;
1477c478bd9Sstevel@tonic-gate 		elf->ed_class = base[EI_CLASS];
1487c478bd9Sstevel@tonic-gate 		elf->ed_encode = base[EI_DATA];
1497c478bd9Sstevel@tonic-gate 		if ((elf->ed_version = base[EI_VERSION]) == 0)
1507c478bd9Sstevel@tonic-gate 			elf->ed_version = 1;
1517c478bd9Sstevel@tonic-gate 		elf->ed_identsz = EI_NIDENT;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		/*
1547c478bd9Sstevel@tonic-gate 		 * Allow writing only if originally specified read only.
1557c478bd9Sstevel@tonic-gate 		 * This is only necessary if the file must be translating
1567c478bd9Sstevel@tonic-gate 		 * from one encoding to another.
1577c478bd9Sstevel@tonic-gate 		 */
1587c478bd9Sstevel@tonic-gate 		ELFACCESSDATA(encode, _elf_encode)
1597c478bd9Sstevel@tonic-gate 		if ((elf->ed_vm == 0) && ((elf->ed_myflags & EDF_WRITE) == 0) &&
1607c478bd9Sstevel@tonic-gate 		    (elf->ed_encode != encode)) {
1617c478bd9Sstevel@tonic-gate 			if (mprotect((char *)elf->ed_image, elf->ed_imagesz,
1627c478bd9Sstevel@tonic-gate 			    PROT_READ|PROT_WRITE) == -1) {
1637c478bd9Sstevel@tonic-gate 				_elf_seterr(EIO_VM, errno);
1647c478bd9Sstevel@tonic-gate 				return (0);
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		return (elf);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Determine if this is an Archive
1727c478bd9Sstevel@tonic-gate 	 */
1737c478bd9Sstevel@tonic-gate 	if ((elf->ed_fsz >= SARMAG) &&
1747c478bd9Sstevel@tonic-gate 	    (_elf_vm(elf, (size_t)0, (size_t)SARMAG) == OK_YES) &&
1757c478bd9Sstevel@tonic-gate 	    (memcmp(base, armag, SARMAG) == 0)) {
1767c478bd9Sstevel@tonic-gate 		_elf_arinit(elf);
1777c478bd9Sstevel@tonic-gate 		elf->ed_kind = ELF_K_AR;
1787c478bd9Sstevel@tonic-gate 		elf->ed_identsz = SARMAG;
1797c478bd9Sstevel@tonic-gate 		return (elf);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 *	Return a few ident bytes, but not so many that
1847c478bd9Sstevel@tonic-gate 	 *	getident() must read a large file.  512 is arbitrary.
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	elf->ed_kind = ELF_K_NONE;
1887c478bd9Sstevel@tonic-gate 	if ((elf->ed_identsz = elf->ed_fsz) > 512)
1897c478bd9Sstevel@tonic-gate 		elf->ed_identsz = 512;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	return (elf);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate Elf *
elf_memory(char * image,size_t sz)1957c478bd9Sstevel@tonic-gate elf_memory(char *image, size_t sz)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	Elf		*elf;
1987c478bd9Sstevel@tonic-gate 	unsigned	work;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/*
2017c478bd9Sstevel@tonic-gate 	 * version() no called yet?
2027c478bd9Sstevel@tonic-gate 	 */
2037c478bd9Sstevel@tonic-gate 	ELFACCESSDATA(work, _elf_work)
2047c478bd9Sstevel@tonic-gate 	if (work == EV_NONE) {
2057c478bd9Sstevel@tonic-gate 		_elf_seterr(ESEQ_VER, 0);
2067c478bd9Sstevel@tonic-gate 		return (0);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
2107c478bd9Sstevel@tonic-gate 		_elf_seterr(EMEM_ELF, errno);
2117c478bd9Sstevel@tonic-gate 		return (0);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	elf->ed_fd = -1;
2147c478bd9Sstevel@tonic-gate 	elf->ed_myflags |= EDF_READ | EDF_MEMORY;
2157c478bd9Sstevel@tonic-gate 	elf->ed_image = elf->ed_ident = image;
2167c478bd9Sstevel@tonic-gate 	elf->ed_imagesz = elf->ed_fsz = elf->ed_identsz = sz;
2177c478bd9Sstevel@tonic-gate 	elf->ed_kind = ELF_K_ELF;
2187c478bd9Sstevel@tonic-gate 	elf->ed_class = image[EI_CLASS];
2197c478bd9Sstevel@tonic-gate 	elf->ed_encode = image[EI_DATA];
2207c478bd9Sstevel@tonic-gate 	if ((elf->ed_version = image[EI_VERSION]) == 0)
2217c478bd9Sstevel@tonic-gate 		elf->ed_version = 1;
2227c478bd9Sstevel@tonic-gate 	elf->ed_identsz = EI_NIDENT;
2237c478bd9Sstevel@tonic-gate 	elf->ed_activ = 1;
2247c478bd9Sstevel@tonic-gate 	elf = _elf_config(elf);
2257c478bd9Sstevel@tonic-gate 	return (elf);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * The following is a private interface between the linkers (ld & ld.so.1)
2307c478bd9Sstevel@tonic-gate  * and libelf.
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  * elf_begin(0, ELF_C_IMAGE, ref)
2337c478bd9Sstevel@tonic-gate  *	Return a new elf_descriptor which uses the memory image from
2347c478bd9Sstevel@tonic-gate  *	ref as the base image of the elf file.  Before this elf_begin()
2357c478bd9Sstevel@tonic-gate  *	is called an elf_update(ref, ELF_C_WRIMAGE) must have been
2367c478bd9Sstevel@tonic-gate  *	done to the ref elf descriptor.
2377c478bd9Sstevel@tonic-gate  *	The ELF_C_IMAGE is unique in that modificatino of the Elf structure
2387c478bd9Sstevel@tonic-gate  *	is illegal (no elf_new*()) but you can modify the actual
2397c478bd9Sstevel@tonic-gate  *	data image of the file in question.
2407c478bd9Sstevel@tonic-gate  *
2417c478bd9Sstevel@tonic-gate  *	When you are done processing this file you can then perform a
2427c478bd9Sstevel@tonic-gate  *	elf_end() on it.
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  *	NOTE: if an elf_update(ref, ELF_C_WRITE) is done on the ref Elf
2457c478bd9Sstevel@tonic-gate  *		descriptor then the memory image that the ELF_C_IMAGE
2467c478bd9Sstevel@tonic-gate  *		is using has been discarded.  The proper calling convention
2477c478bd9Sstevel@tonic-gate  *		for this is as follows:
2487c478bd9Sstevel@tonic-gate  *
2497c478bd9Sstevel@tonic-gate  *	elf1 = elf_begin(fd, ELF_C_WRITE, 0);
2507c478bd9Sstevel@tonic-gate  *	...
2517c478bd9Sstevel@tonic-gate  *	elf_update(elf1, ELF_C_WRIMAGE);	 build memory image
2527c478bd9Sstevel@tonic-gate  *	elf2 = elf_begin(0, ELF_C_IMAGE, elf1);
2537c478bd9Sstevel@tonic-gate  *	...
2547c478bd9Sstevel@tonic-gate  *	elf_end(elf2);
2557c478bd9Sstevel@tonic-gate  *	elf_updage(elf1, ELF_C_WRITE);		flush memory image to disk
2567c478bd9Sstevel@tonic-gate  *	elf_end(elf1);
2577c478bd9Sstevel@tonic-gate  *
2587c478bd9Sstevel@tonic-gate  *
2597c478bd9Sstevel@tonic-gate  * elf_begin(0, ELF_C_IMAGE, 0);
2607c478bd9Sstevel@tonic-gate  *	returns a pointer to an elf descriptor as if it were opened
2617c478bd9Sstevel@tonic-gate  *	with ELF_C_WRITE except that it has no file descriptor and it
2627c478bd9Sstevel@tonic-gate  *	will not create a file.  It's to be used with the command:
2637c478bd9Sstevel@tonic-gate  *
2647c478bd9Sstevel@tonic-gate  *		elf_update(elf, ELF_C_WRIMAGE)
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  *	which will build a memory image instead of a file image.
2677c478bd9Sstevel@tonic-gate  *	The memory image is allocated via dynamic memory (malloc) and
2687c478bd9Sstevel@tonic-gate  *	can be free with a subsequent call to
2697c478bd9Sstevel@tonic-gate  *
2707c478bd9Sstevel@tonic-gate  *		elf_update(elf, ELF_C_WRITE)
2717c478bd9Sstevel@tonic-gate  *
2727c478bd9Sstevel@tonic-gate  *	NOTE: that if elf_end(elf) is called it will not free the
2737c478bd9Sstevel@tonic-gate  *		memory image if it is still allocated.  It is then
2747c478bd9Sstevel@tonic-gate  *		the callers responsiblity to free it via a call
2757c478bd9Sstevel@tonic-gate  *		to free().
2767c478bd9Sstevel@tonic-gate  *
2777c478bd9Sstevel@tonic-gate  *	Here is a potential calling sequence for this interface:
2787c478bd9Sstevel@tonic-gate  *
2797c478bd9Sstevel@tonic-gate  *	elf1 = elf_begin(0, ELF_C_IMAGE, 0);
2807c478bd9Sstevel@tonic-gate  *	...
2817c478bd9Sstevel@tonic-gate  *	elf_update(elf1, ELF_C_WRIMAGE);	build memory image
2827c478bd9Sstevel@tonic-gate  *	elf2 = elf_begin(0, ELF_C_IMAGE, elf1);
2837c478bd9Sstevel@tonic-gate  *	...
2847c478bd9Sstevel@tonic-gate  *	image_ptr = elf32_getehdr(elf2);	get pointer to image
2857c478bd9Sstevel@tonic-gate  *	elf_end(elf2);
2867c478bd9Sstevel@tonic-gate  *	elf_end(elf1);
2877c478bd9Sstevel@tonic-gate  *	...
2887c478bd9Sstevel@tonic-gate  *	use image
2897c478bd9Sstevel@tonic-gate  *	...
2907c478bd9Sstevel@tonic-gate  *	free(image_ptr);
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate Elf *
elf_begin(int fd,Elf_Cmd cmd,Elf * ref)2947c478bd9Sstevel@tonic-gate elf_begin(int fd, Elf_Cmd cmd, Elf *ref)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	register Elf	*elf;
2977c478bd9Sstevel@tonic-gate 	unsigned	work;
2987c478bd9Sstevel@tonic-gate 	unsigned	flags = 0;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	ELFACCESSDATA(work, _elf_work)
3017c478bd9Sstevel@tonic-gate 	if (work == EV_NONE)	/* version() not called yet */
3027c478bd9Sstevel@tonic-gate 	{
3037c478bd9Sstevel@tonic-gate 		_elf_seterr(ESEQ_VER, 0);
3047c478bd9Sstevel@tonic-gate 		return (0);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	switch (cmd) {
3077c478bd9Sstevel@tonic-gate 	default:
3087c478bd9Sstevel@tonic-gate 		_elf_seterr(EREQ_BEGIN, 0);
3097c478bd9Sstevel@tonic-gate 		return (0);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	case ELF_C_NULL:
3127c478bd9Sstevel@tonic-gate 		return (0);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	case ELF_C_IMAGE:
3157c478bd9Sstevel@tonic-gate 		if (ref) {
3167c478bd9Sstevel@tonic-gate 			char	*image;
3177c478bd9Sstevel@tonic-gate 			size_t	imagesz;
3187c478bd9Sstevel@tonic-gate 			ELFRLOCK(ref);
3197c478bd9Sstevel@tonic-gate 			if ((image = ref->ed_wrimage) == 0) {
3207c478bd9Sstevel@tonic-gate 				_elf_seterr(EREQ_NOWRIMAGE, 0);
3217c478bd9Sstevel@tonic-gate 				ELFUNLOCK(ref);
3227c478bd9Sstevel@tonic-gate 				return (0);
3237c478bd9Sstevel@tonic-gate 			}
3247c478bd9Sstevel@tonic-gate 			imagesz = ref->ed_wrimagesz;
3257c478bd9Sstevel@tonic-gate 			ELFUNLOCK(ref);
3267c478bd9Sstevel@tonic-gate 			return (elf_memory(image, imagesz));
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
3297c478bd9Sstevel@tonic-gate 	case ELF_C_WRITE:
3307c478bd9Sstevel@tonic-gate 		if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
3317c478bd9Sstevel@tonic-gate 			_elf_seterr(EMEM_ELF, errno);
3327c478bd9Sstevel@tonic-gate 			return (0);
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		ELFRWLOCKINIT(&elf->ed_rwlock);
3357c478bd9Sstevel@tonic-gate 		elf->ed_fd = fd;
3367c478bd9Sstevel@tonic-gate 		elf->ed_activ = 1;
3377c478bd9Sstevel@tonic-gate 		elf->ed_myflags |= EDF_WRITE;
3387c478bd9Sstevel@tonic-gate 		if (cmd == ELF_C_IMAGE)
3397c478bd9Sstevel@tonic-gate 			elf->ed_myflags |= EDF_WRALLOC;
3407c478bd9Sstevel@tonic-gate 		return (elf);
3417c478bd9Sstevel@tonic-gate 	case ELF_C_RDWR:
3427c478bd9Sstevel@tonic-gate 		flags = EDF_WRITE | EDF_READ;
3437c478bd9Sstevel@tonic-gate 		break;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	case ELF_C_READ:
3467c478bd9Sstevel@tonic-gate 		flags = EDF_READ;
3477c478bd9Sstevel@tonic-gate 		break;
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 *	A null ref asks for a new file
3527c478bd9Sstevel@tonic-gate 	 *	Non-null ref bumps the activation count
3537c478bd9Sstevel@tonic-gate 	 *		or gets next archive member
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (ref == 0) {
3577c478bd9Sstevel@tonic-gate 		if ((elf = _elf_regular(fd, flags)) == 0)
3587c478bd9Sstevel@tonic-gate 			return (0);
3597c478bd9Sstevel@tonic-gate 	} else {
3607c478bd9Sstevel@tonic-gate 		ELFWLOCK(ref);
3617c478bd9Sstevel@tonic-gate 		if ((ref->ed_myflags & flags) != flags) {
3627c478bd9Sstevel@tonic-gate 			_elf_seterr(EREQ_RDWR, 0);
3637c478bd9Sstevel@tonic-gate 			ELFUNLOCK(ref);
3647c478bd9Sstevel@tonic-gate 			return (0);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		/*
3677c478bd9Sstevel@tonic-gate 		 * new activation ?
3687c478bd9Sstevel@tonic-gate 		 */
3697c478bd9Sstevel@tonic-gate 		if (ref->ed_kind != ELF_K_AR) {
3707c478bd9Sstevel@tonic-gate 			++ref->ed_activ;
3717c478bd9Sstevel@tonic-gate 			ELFUNLOCK(ref);
3727c478bd9Sstevel@tonic-gate 			return (ref);
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		if ((elf = _elf_member(fd, ref, flags)) == 0) {
3757c478bd9Sstevel@tonic-gate 			ELFUNLOCK(ref);
3767c478bd9Sstevel@tonic-gate 			return (0);
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 		ELFUNLOCK(ref);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	elf->ed_activ = 1;
3827c478bd9Sstevel@tonic-gate 	elf = _elf_config(elf);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	return (elf);
3857c478bd9Sstevel@tonic-gate }
386