xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/begin.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
28*6812Sraf  *	Copyright (c) 1988 AT&T
29*6812Sraf  *	All Rights Reserved
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
32*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <ar.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <memory.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <libelf.h>
390Sstevel@tonic-gate #include <sys/mman.h>
400Sstevel@tonic-gate #include "decl.h"
410Sstevel@tonic-gate #include "member.h"
420Sstevel@tonic-gate #include "msg.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static const char	armag[] = ARMAG;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Initialize archive member
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate Elf *
_elf_member(int fd,Elf * ref,unsigned flags)510Sstevel@tonic-gate _elf_member(int fd, Elf * ref, unsigned flags)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	register Elf	*elf;
540Sstevel@tonic-gate 	Member		*mh;
550Sstevel@tonic-gate 	size_t		base;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if (ref->ed_nextoff >= ref->ed_fsz)
580Sstevel@tonic-gate 		return (0);
590Sstevel@tonic-gate 	if (ref->ed_fd == -1)		/* disabled */
600Sstevel@tonic-gate 		fd = -1;
610Sstevel@tonic-gate 	if (flags & EDF_WRITE) {
620Sstevel@tonic-gate 		_elf_seterr(EREQ_ARRDWR, 0);
630Sstevel@tonic-gate 		return (0);
640Sstevel@tonic-gate 	}
650Sstevel@tonic-gate 	if (ref->ed_fd != fd) {
660Sstevel@tonic-gate 		_elf_seterr(EREQ_ARMEMFD, 0);
670Sstevel@tonic-gate 		return (0);
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	if ((_elf_vm(ref, ref->ed_nextoff, sizeof (struct ar_hdr)) !=
700Sstevel@tonic-gate 	    OK_YES) || ((mh = _elf_armem(ref,
710Sstevel@tonic-gate 	    ref->ed_ident + ref->ed_nextoff, ref->ed_fsz)) == 0))
720Sstevel@tonic-gate 		return (0);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	base = ref->ed_nextoff + sizeof (struct ar_hdr);
750Sstevel@tonic-gate 	if (ref->ed_fsz - base < mh->m_hdr.ar_size) {
760Sstevel@tonic-gate 		_elf_seterr(EFMT_ARMEMSZ, 0);
770Sstevel@tonic-gate 		return (0);
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 	if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
800Sstevel@tonic-gate 		_elf_seterr(EMEM_ELF, errno);
810Sstevel@tonic-gate 		return (0);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 	++ref->ed_activ;
840Sstevel@tonic-gate 	elf->ed_parent = ref;
850Sstevel@tonic-gate 	elf->ed_fd = fd;
860Sstevel@tonic-gate 	elf->ed_myflags |= flags;
870Sstevel@tonic-gate 	elf->ed_armem = mh;
880Sstevel@tonic-gate 	elf->ed_fsz = mh->m_hdr.ar_size;
890Sstevel@tonic-gate 	elf->ed_baseoff = ref->ed_baseoff + base;
900Sstevel@tonic-gate 	elf->ed_memoff = base - mh->m_slide;
910Sstevel@tonic-gate 	elf->ed_siboff = base + elf->ed_fsz + (elf->ed_fsz & 1);
920Sstevel@tonic-gate 	ref->ed_nextoff = elf->ed_siboff;
930Sstevel@tonic-gate 	elf->ed_image = ref->ed_image;
940Sstevel@tonic-gate 	elf->ed_imagesz = ref->ed_imagesz;
950Sstevel@tonic-gate 	elf->ed_vm = ref->ed_vm;
960Sstevel@tonic-gate 	elf->ed_vmsz = ref->ed_vmsz;
970Sstevel@tonic-gate 	elf->ed_ident = ref->ed_ident + base - mh->m_slide;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * If this member is the archive string table,
1010Sstevel@tonic-gate 	 * we've already altered the bytes.
1020Sstevel@tonic-gate 	 */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (ref->ed_arstroff == ref->ed_nextoff)
1050Sstevel@tonic-gate 		elf->ed_status = ES_COOKED;
1060Sstevel@tonic-gate 	return (elf);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate Elf *
_elf_regular(int fd,unsigned flags)1110Sstevel@tonic-gate _elf_regular(int fd, unsigned flags)		/* initialize regular file */
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	Elf		*elf;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
1160Sstevel@tonic-gate 		_elf_seterr(EMEM_ELF, errno);
1170Sstevel@tonic-gate 		return (0);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf))
1210Sstevel@tonic-gate 	elf->ed_fd = fd;
1220Sstevel@tonic-gate 	elf->ed_myflags |= flags;
1230Sstevel@tonic-gate 	if (_elf_inmap(elf) != OK_YES) {
1240Sstevel@tonic-gate 		free(elf);
1250Sstevel@tonic-gate 		return (0);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf))
1280Sstevel@tonic-gate 	return (elf);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate Elf *
_elf_config(Elf * elf)1330Sstevel@tonic-gate _elf_config(Elf * elf)
1340Sstevel@tonic-gate {
135*6812Sraf 	char		*base;
1360Sstevel@tonic-gate 	unsigned	encode;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	ELFRWLOCKINIT(&elf->ed_rwlock);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * Determine if this is a ELF file.
1420Sstevel@tonic-gate 	 */
1430Sstevel@tonic-gate 	base = elf->ed_ident;
1440Sstevel@tonic-gate 	if ((elf->ed_fsz >= EI_NIDENT) &&
1450Sstevel@tonic-gate 	    (_elf_vm(elf, (size_t)0, (size_t)EI_NIDENT) == OK_YES) &&
1460Sstevel@tonic-gate 	    (base[EI_MAG0] == ELFMAG0) &&
1470Sstevel@tonic-gate 	    (base[EI_MAG1] == ELFMAG1) &&
1480Sstevel@tonic-gate 	    (base[EI_MAG2] == ELFMAG2) &&
1490Sstevel@tonic-gate 	    (base[EI_MAG3] == ELFMAG3)) {
1500Sstevel@tonic-gate 		elf->ed_kind = ELF_K_ELF;
1510Sstevel@tonic-gate 		elf->ed_class = base[EI_CLASS];
1520Sstevel@tonic-gate 		elf->ed_encode = base[EI_DATA];
1530Sstevel@tonic-gate 		if ((elf->ed_version = base[EI_VERSION]) == 0)
1540Sstevel@tonic-gate 			elf->ed_version = 1;
1550Sstevel@tonic-gate 		elf->ed_identsz = EI_NIDENT;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		/*
1580Sstevel@tonic-gate 		 * Allow writing only if originally specified read only.
1590Sstevel@tonic-gate 		 * This is only necessary if the file must be translating
1600Sstevel@tonic-gate 		 * from one encoding to another.
1610Sstevel@tonic-gate 		 */
1620Sstevel@tonic-gate 		ELFACCESSDATA(encode, _elf_encode)
1630Sstevel@tonic-gate 		if ((elf->ed_vm == 0) && ((elf->ed_myflags & EDF_WRITE) == 0) &&
1640Sstevel@tonic-gate 		    (elf->ed_encode != encode)) {
1650Sstevel@tonic-gate 			if (mprotect((char *)elf->ed_image, elf->ed_imagesz,
1660Sstevel@tonic-gate 			    PROT_READ|PROT_WRITE) == -1) {
1670Sstevel@tonic-gate 				_elf_seterr(EIO_VM, errno);
1680Sstevel@tonic-gate 				return (0);
1690Sstevel@tonic-gate 			}
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 		return (elf);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	/*
1750Sstevel@tonic-gate 	 * Determine if this is an Archive
1760Sstevel@tonic-gate 	 */
1770Sstevel@tonic-gate 	if ((elf->ed_fsz >= SARMAG) &&
1780Sstevel@tonic-gate 	    (_elf_vm(elf, (size_t)0, (size_t)SARMAG) == OK_YES) &&
1790Sstevel@tonic-gate 	    (memcmp(base, armag, SARMAG) == 0)) {
1800Sstevel@tonic-gate 		_elf_arinit(elf);
1810Sstevel@tonic-gate 		elf->ed_kind = ELF_K_AR;
1820Sstevel@tonic-gate 		elf->ed_identsz = SARMAG;
1830Sstevel@tonic-gate 		return (elf);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 *	Return a few ident bytes, but not so many that
1880Sstevel@tonic-gate 	 *	getident() must read a large file.  512 is arbitrary.
1890Sstevel@tonic-gate 	 */
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	elf->ed_kind = ELF_K_NONE;
1920Sstevel@tonic-gate 	if ((elf->ed_identsz = elf->ed_fsz) > 512)
1930Sstevel@tonic-gate 		elf->ed_identsz = 512;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	return (elf);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate Elf *
elf_memory(char * image,size_t sz)199*6812Sraf elf_memory(char *image, size_t sz)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate 	Elf		*elf;
2020Sstevel@tonic-gate 	unsigned	work;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * version() no called yet?
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	ELFACCESSDATA(work, _elf_work)
2080Sstevel@tonic-gate 	if (work == EV_NONE) {
2090Sstevel@tonic-gate 		_elf_seterr(ESEQ_VER, 0);
2100Sstevel@tonic-gate 		return (0);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
2140Sstevel@tonic-gate 		_elf_seterr(EMEM_ELF, errno);
2150Sstevel@tonic-gate 		return (0);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 	NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf))
2180Sstevel@tonic-gate 	elf->ed_fd = -1;
2190Sstevel@tonic-gate 	elf->ed_myflags |= EDF_READ | EDF_MEMORY;
2200Sstevel@tonic-gate 	elf->ed_image = elf->ed_ident = image;
2210Sstevel@tonic-gate 	elf->ed_imagesz = elf->ed_fsz = elf->ed_identsz = sz;
2220Sstevel@tonic-gate 	elf->ed_kind = ELF_K_ELF;
2230Sstevel@tonic-gate 	elf->ed_class = image[EI_CLASS];
2240Sstevel@tonic-gate 	elf->ed_encode = image[EI_DATA];
2250Sstevel@tonic-gate 	if ((elf->ed_version = image[EI_VERSION]) == 0)
2260Sstevel@tonic-gate 		elf->ed_version = 1;
2270Sstevel@tonic-gate 	elf->ed_identsz = EI_NIDENT;
2280Sstevel@tonic-gate 	elf->ed_activ = 1;
2290Sstevel@tonic-gate 	elf = _elf_config(elf);
2300Sstevel@tonic-gate 	NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf))
2310Sstevel@tonic-gate 	return (elf);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * The following is a private interface between the linkers (ld & ld.so.1)
2360Sstevel@tonic-gate  * and libelf.
2370Sstevel@tonic-gate  *
2380Sstevel@tonic-gate  * elf_begin(0, ELF_C_IMAGE, ref)
2390Sstevel@tonic-gate  *	Return a new elf_descriptor which uses the memory image from
2400Sstevel@tonic-gate  *	ref as the base image of the elf file.  Before this elf_begin()
2410Sstevel@tonic-gate  *	is called an elf_update(ref, ELF_C_WRIMAGE) must have been
2420Sstevel@tonic-gate  *	done to the ref elf descriptor.
2430Sstevel@tonic-gate  *	The ELF_C_IMAGE is unique in that modificatino of the Elf structure
2440Sstevel@tonic-gate  *	is illegal (no elf_new*()) but you can modify the actual
2450Sstevel@tonic-gate  *	data image of the file in question.
2460Sstevel@tonic-gate  *
2470Sstevel@tonic-gate  *	When you are done processing this file you can then perform a
2480Sstevel@tonic-gate  *	elf_end() on it.
2490Sstevel@tonic-gate  *
2500Sstevel@tonic-gate  *	NOTE: if an elf_update(ref, ELF_C_WRITE) is done on the ref Elf
2510Sstevel@tonic-gate  *		descriptor then the memory image that the ELF_C_IMAGE
2520Sstevel@tonic-gate  *		is using has been discarded.  The proper calling convention
2530Sstevel@tonic-gate  *		for this is as follows:
2540Sstevel@tonic-gate  *
2550Sstevel@tonic-gate  *	elf1 = elf_begin(fd, ELF_C_WRITE, 0);
2560Sstevel@tonic-gate  *	...
2570Sstevel@tonic-gate  *	elf_update(elf1, ELF_C_WRIMAGE);	 build memory image
2580Sstevel@tonic-gate  *	elf2 = elf_begin(0, ELF_C_IMAGE, elf1);
2590Sstevel@tonic-gate  *	...
2600Sstevel@tonic-gate  *	elf_end(elf2);
2610Sstevel@tonic-gate  *	elf_updage(elf1, ELF_C_WRITE);		flush memory image to disk
2620Sstevel@tonic-gate  *	elf_end(elf1);
2630Sstevel@tonic-gate  *
2640Sstevel@tonic-gate  *
2650Sstevel@tonic-gate  * elf_begin(0, ELF_C_IMAGE, 0);
2660Sstevel@tonic-gate  *	returns a pointer to an elf descriptor as if it were opened
2670Sstevel@tonic-gate  *	with ELF_C_WRITE except that it has no file descriptor and it
2680Sstevel@tonic-gate  *	will not create a file.  It's to be used with the command:
2690Sstevel@tonic-gate  *
2700Sstevel@tonic-gate  *		elf_update(elf, ELF_C_WRIMAGE)
2710Sstevel@tonic-gate  *
2720Sstevel@tonic-gate  *	which will build a memory image instead of a file image.
2730Sstevel@tonic-gate  *	The memory image is allocated via dynamic memory (malloc) and
2740Sstevel@tonic-gate  *	can be free with a subsequent call to
2750Sstevel@tonic-gate  *
2760Sstevel@tonic-gate  *		elf_update(elf, ELF_C_WRITE)
2770Sstevel@tonic-gate  *
2780Sstevel@tonic-gate  *	NOTE: that if elf_end(elf) is called it will not free the
2790Sstevel@tonic-gate  *		memory image if it is still allocated.  It is then
2800Sstevel@tonic-gate  *		the callers responsiblity to free it via a call
2810Sstevel@tonic-gate  *		to free().
2820Sstevel@tonic-gate  *
2830Sstevel@tonic-gate  *	Here is a potential calling sequence for this interface:
2840Sstevel@tonic-gate  *
2850Sstevel@tonic-gate  *	elf1 = elf_begin(0, ELF_C_IMAGE, 0);
2860Sstevel@tonic-gate  *	...
2870Sstevel@tonic-gate  *	elf_update(elf1, ELF_C_WRIMAGE);	build memory image
2880Sstevel@tonic-gate  *	elf2 = elf_begin(0, ELF_C_IMAGE, elf1);
2890Sstevel@tonic-gate  *	...
2900Sstevel@tonic-gate  *	image_ptr = elf32_getehdr(elf2);	get pointer to image
2910Sstevel@tonic-gate  *	elf_end(elf2);
2920Sstevel@tonic-gate  *	elf_end(elf1);
2930Sstevel@tonic-gate  *	...
2940Sstevel@tonic-gate  *	use image
2950Sstevel@tonic-gate  *	...
2960Sstevel@tonic-gate  *	free(image_ptr);
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate Elf *
elf_begin(int fd,Elf_Cmd cmd,Elf * ref)3000Sstevel@tonic-gate elf_begin(int fd, Elf_Cmd cmd, Elf *ref)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	register Elf	*elf;
3030Sstevel@tonic-gate 	unsigned	work;
3040Sstevel@tonic-gate 	unsigned	flags = 0;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	ELFACCESSDATA(work, _elf_work)
3070Sstevel@tonic-gate 	if (work == EV_NONE)	/* version() not called yet */
3080Sstevel@tonic-gate 	{
3090Sstevel@tonic-gate 		_elf_seterr(ESEQ_VER, 0);
3100Sstevel@tonic-gate 		return (0);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	switch (cmd) {
3130Sstevel@tonic-gate 	default:
3140Sstevel@tonic-gate 		_elf_seterr(EREQ_BEGIN, 0);
3150Sstevel@tonic-gate 		return (0);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	case ELF_C_NULL:
3180Sstevel@tonic-gate 		return (0);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	case ELF_C_IMAGE:
3210Sstevel@tonic-gate 		if (ref) {
322*6812Sraf 			char	*image;
3230Sstevel@tonic-gate 			size_t	imagesz;
3240Sstevel@tonic-gate 			ELFRLOCK(ref);
3250Sstevel@tonic-gate 			if ((image = ref->ed_wrimage) == 0) {
3260Sstevel@tonic-gate 				_elf_seterr(EREQ_NOWRIMAGE, 0);
3270Sstevel@tonic-gate 				ELFUNLOCK(ref);
3280Sstevel@tonic-gate 				return (0);
3290Sstevel@tonic-gate 			}
3300Sstevel@tonic-gate 			imagesz = ref->ed_wrimagesz;
3310Sstevel@tonic-gate 			ELFUNLOCK(ref);
3320Sstevel@tonic-gate 			return (elf_memory(image, imagesz));
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		/* FALLTHROUGH */
3350Sstevel@tonic-gate 	case ELF_C_WRITE:
3360Sstevel@tonic-gate 		if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
3370Sstevel@tonic-gate 			_elf_seterr(EMEM_ELF, errno);
3380Sstevel@tonic-gate 			return (0);
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 		NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf))
3410Sstevel@tonic-gate 		ELFRWLOCKINIT(&elf->ed_rwlock);
3420Sstevel@tonic-gate 		elf->ed_fd = fd;
3430Sstevel@tonic-gate 		elf->ed_activ = 1;
3440Sstevel@tonic-gate 		elf->ed_myflags |= EDF_WRITE;
3450Sstevel@tonic-gate 		if (cmd == ELF_C_IMAGE)
3460Sstevel@tonic-gate 			elf->ed_myflags |= EDF_WRALLOC;
3470Sstevel@tonic-gate 		NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf))
3480Sstevel@tonic-gate 		return (elf);
3490Sstevel@tonic-gate 	case ELF_C_RDWR:
3500Sstevel@tonic-gate 		flags = EDF_WRITE | EDF_READ;
3510Sstevel@tonic-gate 		break;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	case ELF_C_READ:
3540Sstevel@tonic-gate 		flags = EDF_READ;
3550Sstevel@tonic-gate 		break;
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	/*
3590Sstevel@tonic-gate 	 *	A null ref asks for a new file
3600Sstevel@tonic-gate 	 *	Non-null ref bumps the activation count
3610Sstevel@tonic-gate 	 *		or gets next archive member
3620Sstevel@tonic-gate 	 */
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if (ref == 0) {
3650Sstevel@tonic-gate 		if ((elf = _elf_regular(fd, flags)) == 0)
3660Sstevel@tonic-gate 			return (0);
3670Sstevel@tonic-gate 	} else {
3680Sstevel@tonic-gate 		ELFWLOCK(ref);
3690Sstevel@tonic-gate 		if ((ref->ed_myflags & flags) != flags) {
3700Sstevel@tonic-gate 			_elf_seterr(EREQ_RDWR, 0);
3710Sstevel@tonic-gate 			ELFUNLOCK(ref);
3720Sstevel@tonic-gate 			return (0);
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 		/*
3750Sstevel@tonic-gate 		 * new activation ?
3760Sstevel@tonic-gate 		 */
3770Sstevel@tonic-gate 		if (ref->ed_kind != ELF_K_AR) {
3780Sstevel@tonic-gate 			++ref->ed_activ;
3790Sstevel@tonic-gate 			ELFUNLOCK(ref);
3800Sstevel@tonic-gate 			return (ref);
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 		if ((elf = _elf_member(fd, ref, flags)) == 0) {
3830Sstevel@tonic-gate 			ELFUNLOCK(ref);
3840Sstevel@tonic-gate 			return (0);
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 		ELFUNLOCK(ref);
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf))
3900Sstevel@tonic-gate 	elf->ed_activ = 1;
3910Sstevel@tonic-gate 	elf = _elf_config(elf);
3920Sstevel@tonic-gate 	NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf))
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	return (elf);
3950Sstevel@tonic-gate }
396