xref: /onnv-gate/usr/src/cmd/sgs/libelf/common/begin.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
24*0Sstevel@tonic-gate  *	All Rights Reserved
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate /*
29*0Sstevel@tonic-gate  * Copyright (c) 1998 by Sun Microsystems, Inc.
30*0Sstevel@tonic-gate  * All rights reserved.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.13	*/
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma weak	elf_begin = _elf_begin
36*0Sstevel@tonic-gate #pragma weak	elf_memory = _elf_memory
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include "syn.h"
40*0Sstevel@tonic-gate #include <ar.h>
41*0Sstevel@tonic-gate #include <stdlib.h>
42*0Sstevel@tonic-gate #include <memory.h>
43*0Sstevel@tonic-gate #include <errno.h>
44*0Sstevel@tonic-gate #include <libelf.h>
45*0Sstevel@tonic-gate #include <sys/mman.h>
46*0Sstevel@tonic-gate #include "decl.h"
47*0Sstevel@tonic-gate #include "member.h"
48*0Sstevel@tonic-gate #include "msg.h"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static const char	armag[] = ARMAG;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate  * Initialize archive member
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate Elf *
57*0Sstevel@tonic-gate _elf_member(int fd, Elf * ref, unsigned flags)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	register Elf	*elf;
60*0Sstevel@tonic-gate 	Member		*mh;
61*0Sstevel@tonic-gate 	size_t		base;
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	if (ref->ed_nextoff >= ref->ed_fsz)
64*0Sstevel@tonic-gate 		return (0);
65*0Sstevel@tonic-gate 	if (ref->ed_fd == -1)		/* disabled */
66*0Sstevel@tonic-gate 		fd = -1;
67*0Sstevel@tonic-gate 	if (flags & EDF_WRITE) {
68*0Sstevel@tonic-gate 		_elf_seterr(EREQ_ARRDWR, 0);
69*0Sstevel@tonic-gate 		return (0);
70*0Sstevel@tonic-gate 	}
71*0Sstevel@tonic-gate 	if (ref->ed_fd != fd) {
72*0Sstevel@tonic-gate 		_elf_seterr(EREQ_ARMEMFD, 0);
73*0Sstevel@tonic-gate 		return (0);
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 	if ((_elf_vm(ref, ref->ed_nextoff, sizeof (struct ar_hdr)) !=
76*0Sstevel@tonic-gate 	    OK_YES) || ((mh = _elf_armem(ref,
77*0Sstevel@tonic-gate 	    ref->ed_ident + ref->ed_nextoff, ref->ed_fsz)) == 0))
78*0Sstevel@tonic-gate 		return (0);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	base = ref->ed_nextoff + sizeof (struct ar_hdr);
81*0Sstevel@tonic-gate 	if (ref->ed_fsz - base < mh->m_hdr.ar_size) {
82*0Sstevel@tonic-gate 		_elf_seterr(EFMT_ARMEMSZ, 0);
83*0Sstevel@tonic-gate 		return (0);
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 	if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
86*0Sstevel@tonic-gate 		_elf_seterr(EMEM_ELF, errno);
87*0Sstevel@tonic-gate 		return (0);
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate 	++ref->ed_activ;
90*0Sstevel@tonic-gate 	elf->ed_parent = ref;
91*0Sstevel@tonic-gate 	elf->ed_fd = fd;
92*0Sstevel@tonic-gate 	elf->ed_myflags |= flags;
93*0Sstevel@tonic-gate 	elf->ed_armem = mh;
94*0Sstevel@tonic-gate 	elf->ed_fsz = mh->m_hdr.ar_size;
95*0Sstevel@tonic-gate 	elf->ed_baseoff = ref->ed_baseoff + base;
96*0Sstevel@tonic-gate 	elf->ed_memoff = base - mh->m_slide;
97*0Sstevel@tonic-gate 	elf->ed_siboff = base + elf->ed_fsz + (elf->ed_fsz & 1);
98*0Sstevel@tonic-gate 	ref->ed_nextoff = elf->ed_siboff;
99*0Sstevel@tonic-gate 	elf->ed_image = ref->ed_image;
100*0Sstevel@tonic-gate 	elf->ed_imagesz = ref->ed_imagesz;
101*0Sstevel@tonic-gate 	elf->ed_vm = ref->ed_vm;
102*0Sstevel@tonic-gate 	elf->ed_vmsz = ref->ed_vmsz;
103*0Sstevel@tonic-gate 	elf->ed_ident = ref->ed_ident + base - mh->m_slide;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	/*
106*0Sstevel@tonic-gate 	 * If this member is the archive string table,
107*0Sstevel@tonic-gate 	 * we've already altered the bytes.
108*0Sstevel@tonic-gate 	 */
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	if (ref->ed_arstroff == ref->ed_nextoff)
111*0Sstevel@tonic-gate 		elf->ed_status = ES_COOKED;
112*0Sstevel@tonic-gate 	return (elf);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate Elf *
117*0Sstevel@tonic-gate _elf_regular(int fd, unsigned flags)		/* initialize regular file */
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	Elf		*elf;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
122*0Sstevel@tonic-gate 		_elf_seterr(EMEM_ELF, errno);
123*0Sstevel@tonic-gate 		return (0);
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf))
127*0Sstevel@tonic-gate 	elf->ed_fd = fd;
128*0Sstevel@tonic-gate 	elf->ed_myflags |= flags;
129*0Sstevel@tonic-gate 	if (_elf_inmap(elf) != OK_YES) {
130*0Sstevel@tonic-gate 		free(elf);
131*0Sstevel@tonic-gate 		return (0);
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate 	NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf))
134*0Sstevel@tonic-gate 	return (elf);
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate Elf *
139*0Sstevel@tonic-gate _elf_config(Elf * elf)
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate 	char *		base;
142*0Sstevel@tonic-gate 	unsigned	encode;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	ELFRWLOCKINIT(&elf->ed_rwlock);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	/*
147*0Sstevel@tonic-gate 	 * Determine if this is a ELF file.
148*0Sstevel@tonic-gate 	 */
149*0Sstevel@tonic-gate 	base = elf->ed_ident;
150*0Sstevel@tonic-gate 	if ((elf->ed_fsz >= EI_NIDENT) &&
151*0Sstevel@tonic-gate 	    (_elf_vm(elf, (size_t)0, (size_t)EI_NIDENT) == OK_YES) &&
152*0Sstevel@tonic-gate 	    (base[EI_MAG0] == ELFMAG0) &&
153*0Sstevel@tonic-gate 	    (base[EI_MAG1] == ELFMAG1) &&
154*0Sstevel@tonic-gate 	    (base[EI_MAG2] == ELFMAG2) &&
155*0Sstevel@tonic-gate 	    (base[EI_MAG3] == ELFMAG3)) {
156*0Sstevel@tonic-gate 		elf->ed_kind = ELF_K_ELF;
157*0Sstevel@tonic-gate 		elf->ed_class = base[EI_CLASS];
158*0Sstevel@tonic-gate 		elf->ed_encode = base[EI_DATA];
159*0Sstevel@tonic-gate 		if ((elf->ed_version = base[EI_VERSION]) == 0)
160*0Sstevel@tonic-gate 			elf->ed_version = 1;
161*0Sstevel@tonic-gate 		elf->ed_identsz = EI_NIDENT;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 		/*
164*0Sstevel@tonic-gate 		 * Allow writing only if originally specified read only.
165*0Sstevel@tonic-gate 		 * This is only necessary if the file must be translating
166*0Sstevel@tonic-gate 		 * from one encoding to another.
167*0Sstevel@tonic-gate 		 */
168*0Sstevel@tonic-gate 		ELFACCESSDATA(encode, _elf_encode)
169*0Sstevel@tonic-gate 		if ((elf->ed_vm == 0) && ((elf->ed_myflags & EDF_WRITE) == 0) &&
170*0Sstevel@tonic-gate 		    (elf->ed_encode != encode)) {
171*0Sstevel@tonic-gate 			if (mprotect((char *)elf->ed_image, elf->ed_imagesz,
172*0Sstevel@tonic-gate 			    PROT_READ|PROT_WRITE) == -1) {
173*0Sstevel@tonic-gate 				_elf_seterr(EIO_VM, errno);
174*0Sstevel@tonic-gate 				return (0);
175*0Sstevel@tonic-gate 			}
176*0Sstevel@tonic-gate 		}
177*0Sstevel@tonic-gate 		return (elf);
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	/*
181*0Sstevel@tonic-gate 	 * Determine if this is an Archive
182*0Sstevel@tonic-gate 	 */
183*0Sstevel@tonic-gate 	if ((elf->ed_fsz >= SARMAG) &&
184*0Sstevel@tonic-gate 	    (_elf_vm(elf, (size_t)0, (size_t)SARMAG) == OK_YES) &&
185*0Sstevel@tonic-gate 	    (memcmp(base, armag, SARMAG) == 0)) {
186*0Sstevel@tonic-gate 		_elf_arinit(elf);
187*0Sstevel@tonic-gate 		elf->ed_kind = ELF_K_AR;
188*0Sstevel@tonic-gate 		elf->ed_identsz = SARMAG;
189*0Sstevel@tonic-gate 		return (elf);
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	/*
193*0Sstevel@tonic-gate 	 *	Return a few ident bytes, but not so many that
194*0Sstevel@tonic-gate 	 *	getident() must read a large file.  512 is arbitrary.
195*0Sstevel@tonic-gate 	 */
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	elf->ed_kind = ELF_K_NONE;
198*0Sstevel@tonic-gate 	if ((elf->ed_identsz = elf->ed_fsz) > 512)
199*0Sstevel@tonic-gate 		elf->ed_identsz = 512;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	return (elf);
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate Elf *
205*0Sstevel@tonic-gate elf_memory(char * image, size_t sz)
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate 	Elf		*elf;
208*0Sstevel@tonic-gate 	unsigned	work;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	/*
211*0Sstevel@tonic-gate 	 * version() no called yet?
212*0Sstevel@tonic-gate 	 */
213*0Sstevel@tonic-gate 	ELFACCESSDATA(work, _elf_work)
214*0Sstevel@tonic-gate 	if (work == EV_NONE) {
215*0Sstevel@tonic-gate 		_elf_seterr(ESEQ_VER, 0);
216*0Sstevel@tonic-gate 		return (0);
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
220*0Sstevel@tonic-gate 		_elf_seterr(EMEM_ELF, errno);
221*0Sstevel@tonic-gate 		return (0);
222*0Sstevel@tonic-gate 	}
223*0Sstevel@tonic-gate 	NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf))
224*0Sstevel@tonic-gate 	elf->ed_fd = -1;
225*0Sstevel@tonic-gate 	elf->ed_myflags |= EDF_READ | EDF_MEMORY;
226*0Sstevel@tonic-gate 	elf->ed_image = elf->ed_ident = image;
227*0Sstevel@tonic-gate 	elf->ed_imagesz = elf->ed_fsz = elf->ed_identsz = sz;
228*0Sstevel@tonic-gate 	elf->ed_kind = ELF_K_ELF;
229*0Sstevel@tonic-gate 	elf->ed_class = image[EI_CLASS];
230*0Sstevel@tonic-gate 	elf->ed_encode = image[EI_DATA];
231*0Sstevel@tonic-gate 	if ((elf->ed_version = image[EI_VERSION]) == 0)
232*0Sstevel@tonic-gate 		elf->ed_version = 1;
233*0Sstevel@tonic-gate 	elf->ed_identsz = EI_NIDENT;
234*0Sstevel@tonic-gate 	elf->ed_activ = 1;
235*0Sstevel@tonic-gate 	elf = _elf_config(elf);
236*0Sstevel@tonic-gate 	NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf))
237*0Sstevel@tonic-gate 	return (elf);
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate /*
241*0Sstevel@tonic-gate  * The following is a private interface between the linkers (ld & ld.so.1)
242*0Sstevel@tonic-gate  * and libelf.
243*0Sstevel@tonic-gate  *
244*0Sstevel@tonic-gate  * elf_begin(0, ELF_C_IMAGE, ref)
245*0Sstevel@tonic-gate  *	Return a new elf_descriptor which uses the memory image from
246*0Sstevel@tonic-gate  *	ref as the base image of the elf file.  Before this elf_begin()
247*0Sstevel@tonic-gate  *	is called an elf_update(ref, ELF_C_WRIMAGE) must have been
248*0Sstevel@tonic-gate  *	done to the ref elf descriptor.
249*0Sstevel@tonic-gate  *	The ELF_C_IMAGE is unique in that modificatino of the Elf structure
250*0Sstevel@tonic-gate  *	is illegal (no elf_new*()) but you can modify the actual
251*0Sstevel@tonic-gate  *	data image of the file in question.
252*0Sstevel@tonic-gate  *
253*0Sstevel@tonic-gate  *	When you are done processing this file you can then perform a
254*0Sstevel@tonic-gate  *	elf_end() on it.
255*0Sstevel@tonic-gate  *
256*0Sstevel@tonic-gate  *	NOTE: if an elf_update(ref, ELF_C_WRITE) is done on the ref Elf
257*0Sstevel@tonic-gate  *		descriptor then the memory image that the ELF_C_IMAGE
258*0Sstevel@tonic-gate  *		is using has been discarded.  The proper calling convention
259*0Sstevel@tonic-gate  *		for this is as follows:
260*0Sstevel@tonic-gate  *
261*0Sstevel@tonic-gate  *	elf1 = elf_begin(fd, ELF_C_WRITE, 0);
262*0Sstevel@tonic-gate  *	...
263*0Sstevel@tonic-gate  *	elf_update(elf1, ELF_C_WRIMAGE);	 build memory image
264*0Sstevel@tonic-gate  *	elf2 = elf_begin(0, ELF_C_IMAGE, elf1);
265*0Sstevel@tonic-gate  *	...
266*0Sstevel@tonic-gate  *	elf_end(elf2);
267*0Sstevel@tonic-gate  *	elf_updage(elf1, ELF_C_WRITE);		flush memory image to disk
268*0Sstevel@tonic-gate  *	elf_end(elf1);
269*0Sstevel@tonic-gate  *
270*0Sstevel@tonic-gate  *
271*0Sstevel@tonic-gate  * elf_begin(0, ELF_C_IMAGE, 0);
272*0Sstevel@tonic-gate  *	returns a pointer to an elf descriptor as if it were opened
273*0Sstevel@tonic-gate  *	with ELF_C_WRITE except that it has no file descriptor and it
274*0Sstevel@tonic-gate  *	will not create a file.  It's to be used with the command:
275*0Sstevel@tonic-gate  *
276*0Sstevel@tonic-gate  *		elf_update(elf, ELF_C_WRIMAGE)
277*0Sstevel@tonic-gate  *
278*0Sstevel@tonic-gate  *	which will build a memory image instead of a file image.
279*0Sstevel@tonic-gate  *	The memory image is allocated via dynamic memory (malloc) and
280*0Sstevel@tonic-gate  *	can be free with a subsequent call to
281*0Sstevel@tonic-gate  *
282*0Sstevel@tonic-gate  *		elf_update(elf, ELF_C_WRITE)
283*0Sstevel@tonic-gate  *
284*0Sstevel@tonic-gate  *	NOTE: that if elf_end(elf) is called it will not free the
285*0Sstevel@tonic-gate  *		memory image if it is still allocated.  It is then
286*0Sstevel@tonic-gate  *		the callers responsiblity to free it via a call
287*0Sstevel@tonic-gate  *		to free().
288*0Sstevel@tonic-gate  *
289*0Sstevel@tonic-gate  *	Here is a potential calling sequence for this interface:
290*0Sstevel@tonic-gate  *
291*0Sstevel@tonic-gate  *	elf1 = elf_begin(0, ELF_C_IMAGE, 0);
292*0Sstevel@tonic-gate  *	...
293*0Sstevel@tonic-gate  *	elf_update(elf1, ELF_C_WRIMAGE);	build memory image
294*0Sstevel@tonic-gate  *	elf2 = elf_begin(0, ELF_C_IMAGE, elf1);
295*0Sstevel@tonic-gate  *	...
296*0Sstevel@tonic-gate  *	image_ptr = elf32_getehdr(elf2);	get pointer to image
297*0Sstevel@tonic-gate  *	elf_end(elf2);
298*0Sstevel@tonic-gate  *	elf_end(elf1);
299*0Sstevel@tonic-gate  *	...
300*0Sstevel@tonic-gate  *	use image
301*0Sstevel@tonic-gate  *	...
302*0Sstevel@tonic-gate  *	free(image_ptr);
303*0Sstevel@tonic-gate  */
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate Elf *
306*0Sstevel@tonic-gate elf_begin(int fd, Elf_Cmd cmd, Elf *ref)
307*0Sstevel@tonic-gate {
308*0Sstevel@tonic-gate 	register Elf	*elf;
309*0Sstevel@tonic-gate 	unsigned	work;
310*0Sstevel@tonic-gate 	unsigned	flags = 0;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	ELFACCESSDATA(work, _elf_work)
313*0Sstevel@tonic-gate 	if (work == EV_NONE)	/* version() not called yet */
314*0Sstevel@tonic-gate 	{
315*0Sstevel@tonic-gate 		_elf_seterr(ESEQ_VER, 0);
316*0Sstevel@tonic-gate 		return (0);
317*0Sstevel@tonic-gate 	}
318*0Sstevel@tonic-gate 	switch (cmd) {
319*0Sstevel@tonic-gate 	default:
320*0Sstevel@tonic-gate 		_elf_seterr(EREQ_BEGIN, 0);
321*0Sstevel@tonic-gate 		return (0);
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	case ELF_C_NULL:
324*0Sstevel@tonic-gate 		return (0);
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	case ELF_C_IMAGE:
327*0Sstevel@tonic-gate 		if (ref) {
328*0Sstevel@tonic-gate 			char *	image;
329*0Sstevel@tonic-gate 			size_t	imagesz;
330*0Sstevel@tonic-gate 			ELFRLOCK(ref);
331*0Sstevel@tonic-gate 			if ((image = ref->ed_wrimage) == 0) {
332*0Sstevel@tonic-gate 				_elf_seterr(EREQ_NOWRIMAGE, 0);
333*0Sstevel@tonic-gate 				ELFUNLOCK(ref);
334*0Sstevel@tonic-gate 				return (0);
335*0Sstevel@tonic-gate 			}
336*0Sstevel@tonic-gate 			imagesz = ref->ed_wrimagesz;
337*0Sstevel@tonic-gate 			ELFUNLOCK(ref);
338*0Sstevel@tonic-gate 			return (elf_memory(image, imagesz));
339*0Sstevel@tonic-gate 		}
340*0Sstevel@tonic-gate 		/* FALLTHROUGH */
341*0Sstevel@tonic-gate 	case ELF_C_WRITE:
342*0Sstevel@tonic-gate 		if ((elf = (Elf *)calloc(1, sizeof (Elf))) == 0) {
343*0Sstevel@tonic-gate 			_elf_seterr(EMEM_ELF, errno);
344*0Sstevel@tonic-gate 			return (0);
345*0Sstevel@tonic-gate 		}
346*0Sstevel@tonic-gate 		NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf))
347*0Sstevel@tonic-gate 		ELFRWLOCKINIT(&elf->ed_rwlock);
348*0Sstevel@tonic-gate 		elf->ed_fd = fd;
349*0Sstevel@tonic-gate 		elf->ed_activ = 1;
350*0Sstevel@tonic-gate 		elf->ed_myflags |= EDF_WRITE;
351*0Sstevel@tonic-gate 		if (cmd == ELF_C_IMAGE)
352*0Sstevel@tonic-gate 			elf->ed_myflags |= EDF_WRALLOC;
353*0Sstevel@tonic-gate 		NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf))
354*0Sstevel@tonic-gate 		return (elf);
355*0Sstevel@tonic-gate 	case ELF_C_RDWR:
356*0Sstevel@tonic-gate 		flags = EDF_WRITE | EDF_READ;
357*0Sstevel@tonic-gate 		break;
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	case ELF_C_READ:
360*0Sstevel@tonic-gate 		flags = EDF_READ;
361*0Sstevel@tonic-gate 		break;
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	/*
365*0Sstevel@tonic-gate 	 *	A null ref asks for a new file
366*0Sstevel@tonic-gate 	 *	Non-null ref bumps the activation count
367*0Sstevel@tonic-gate 	 *		or gets next archive member
368*0Sstevel@tonic-gate 	 */
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	if (ref == 0) {
371*0Sstevel@tonic-gate 		if ((elf = _elf_regular(fd, flags)) == 0)
372*0Sstevel@tonic-gate 			return (0);
373*0Sstevel@tonic-gate 	} else {
374*0Sstevel@tonic-gate 		ELFWLOCK(ref);
375*0Sstevel@tonic-gate 		if ((ref->ed_myflags & flags) != flags) {
376*0Sstevel@tonic-gate 			_elf_seterr(EREQ_RDWR, 0);
377*0Sstevel@tonic-gate 			ELFUNLOCK(ref);
378*0Sstevel@tonic-gate 			return (0);
379*0Sstevel@tonic-gate 		}
380*0Sstevel@tonic-gate 		/*
381*0Sstevel@tonic-gate 		 * new activation ?
382*0Sstevel@tonic-gate 		 */
383*0Sstevel@tonic-gate 		if (ref->ed_kind != ELF_K_AR) {
384*0Sstevel@tonic-gate 			++ref->ed_activ;
385*0Sstevel@tonic-gate 			ELFUNLOCK(ref);
386*0Sstevel@tonic-gate 			return (ref);
387*0Sstevel@tonic-gate 		}
388*0Sstevel@tonic-gate 		if ((elf = _elf_member(fd, ref, flags)) == 0) {
389*0Sstevel@tonic-gate 			ELFUNLOCK(ref);
390*0Sstevel@tonic-gate 			return (0);
391*0Sstevel@tonic-gate 		}
392*0Sstevel@tonic-gate 		ELFUNLOCK(ref);
393*0Sstevel@tonic-gate 	}
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*elf))
396*0Sstevel@tonic-gate 	elf->ed_activ = 1;
397*0Sstevel@tonic-gate 	elf = _elf_config(elf);
398*0Sstevel@tonic-gate 	NOTE(NOW_VISIBLE_TO_OTHER_THREADS(*elf))
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	return (elf);
401*0Sstevel@tonic-gate }
402