xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/decl.h (revision 4899432a)
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*4899432aSab  * Common Development and Distribution License (the "License").
6*4899432aSab  * 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  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*4899432aSab  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #ifndef	_DECL_H
317c478bd9Sstevel@tonic-gate #define	_DECL_H
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.9	*/
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <thread.h>
367c478bd9Sstevel@tonic-gate #include <note.h>
37*4899432aSab #include <_libelf.h>
387c478bd9Sstevel@tonic-gate #include <sys/machelf.h>
397c478bd9Sstevel@tonic-gate #include <msg.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
437c478bd9Sstevel@tonic-gate extern "C" {
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate typedef struct Member	Member;
477c478bd9Sstevel@tonic-gate typedef struct Memlist	Memlist;
487c478bd9Sstevel@tonic-gate typedef struct Memident	Memident;
497c478bd9Sstevel@tonic-gate typedef struct Dnode	Dnode;
507c478bd9Sstevel@tonic-gate typedef struct Snode32	Snode32;
517c478bd9Sstevel@tonic-gate typedef struct Snode64	Snode64;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Data alignment
567c478bd9Sstevel@tonic-gate  *	An elf file is defined to have its structures aligned on
577c478bd9Sstevel@tonic-gate  *	appropriate boundaries.  The following type lets the
587c478bd9Sstevel@tonic-gate  *	library test whether the file's alignment meets its own
597c478bd9Sstevel@tonic-gate  *	constraints in memory.  This assumes every machine uses
607c478bd9Sstevel@tonic-gate  *	an alignment that is no greater than an object's size.
617c478bd9Sstevel@tonic-gate  *	The pointer isn't relevant for the file, but the code uses
627c478bd9Sstevel@tonic-gate  *	it to get memory alignment.  ANSI C void * holds any pointer,
637c478bd9Sstevel@tonic-gate  *	making it appropriate here.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate typedef union
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	Elf32_Word	w;
697c478bd9Sstevel@tonic-gate 	Elf32_Addr	a;
707c478bd9Sstevel@tonic-gate 	Elf32_Off	o;
717c478bd9Sstevel@tonic-gate } Elf32;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate typedef union {
747c478bd9Sstevel@tonic-gate 	Elf64_Xword	x;
757c478bd9Sstevel@tonic-gate 	Elf64_Word	w;
767c478bd9Sstevel@tonic-gate 	Elf64_Addr	a;
777c478bd9Sstevel@tonic-gate 	Elf64_Off	o;
787c478bd9Sstevel@tonic-gate 	Elf_Void	*p;
797c478bd9Sstevel@tonic-gate } Elf64;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Memory allocation
847c478bd9Sstevel@tonic-gate  *	Structures are obtained several ways: file mapping,
857c478bd9Sstevel@tonic-gate  *	malloc(), from the user.  A status bit in the structures
867c478bd9Sstevel@tonic-gate  *	tells whether an object was obtained with malloc() and
877c478bd9Sstevel@tonic-gate  *	therefore should be released with free().  The bits
887c478bd9Sstevel@tonic-gate  *	named ...ALLOC indicate this.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * Data descriptor
947c478bd9Sstevel@tonic-gate  *	db_data must be first in the Dnode structure, because
957c478bd9Sstevel@tonic-gate  *	&db_data must == &Dnode.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  *	db_buf is a pointer to an allocated buffer.  The same value
987c478bd9Sstevel@tonic-gate  *	goes into db_data.d_buf originally, but the user can touch
997c478bd9Sstevel@tonic-gate  *	it.  If the data buffer is not to be freed, db_buf is null.
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  *	When "reading" an input file's buffer, the data are left
1027c478bd9Sstevel@tonic-gate  *	alone until needed.  When they've been converted to internal
1037c478bd9Sstevel@tonic-gate  *	form, the READY flag is set.
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  *	db_raw points to a parallel raw buffer.  Raw buffers
1067c478bd9Sstevel@tonic-gate  *	have null db_raw.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate struct	Dnode
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	Elf_Data	db_data;
1127c478bd9Sstevel@tonic-gate 	Elf_Scn		*db_scn;	/* section parent */
1137c478bd9Sstevel@tonic-gate 	Dnode		*db_next;
1147c478bd9Sstevel@tonic-gate 	Dnode		*db_raw;	/* raw data */
1157c478bd9Sstevel@tonic-gate 	off_t		db_off;		/* orig file offset, 0 o/w */
1167c478bd9Sstevel@tonic-gate 	size_t		db_fsz;		/* orig file size, 0 o/w */
1177c478bd9Sstevel@tonic-gate 	size_t		db_shsz;	/* orig shdr size, 0 o/w */
1187c478bd9Sstevel@tonic-gate 	size_t		db_osz;		/* output size for update */
1197c478bd9Sstevel@tonic-gate 	Elf_Void	*db_buf;	/* allocated data buffer */
1207c478bd9Sstevel@tonic-gate 	unsigned	db_uflags;	/* user flags: ELF_F_... */
1217c478bd9Sstevel@tonic-gate 	unsigned	db_myflags;	/* internal flags: DBF_... */
1227c478bd9Sstevel@tonic-gate 	Elf64_Off	db_xoff;	/* extended offset for 32-bit Elf64 */
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	DBF_ALLOC	0x1	/* applies to Dnode itself */
1267c478bd9Sstevel@tonic-gate #define	DBF_READY	0x2	/* buffer ready */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Section descriptor
1317c478bd9Sstevel@tonic-gate  *	These are sometimes allocated in a block.  If the SF_ALLOC
1327c478bd9Sstevel@tonic-gate  *	bit is set in the flags, the Scn address may be passed to free.
1337c478bd9Sstevel@tonic-gate  *	The caller must first follow the s_next list to the next freeable
1347c478bd9Sstevel@tonic-gate  *	node, because free can clobber the s_next value in the block.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate struct	Elf_Scn
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	mutex_t		s_mutex;
1407c478bd9Sstevel@tonic-gate 	Elf_Scn		*s_next;	/* next section */
1417c478bd9Sstevel@tonic-gate 	Elf		*s_elf; 	/* parent file */
1427c478bd9Sstevel@tonic-gate 	Dnode		*s_hdnode;	/* head Dnode */
1437c478bd9Sstevel@tonic-gate 	Dnode		*s_tlnode;	/* tail Dnode */
1447c478bd9Sstevel@tonic-gate 	Elf_Void	*s_shdr;	/* Elf32 or Elf64 scn header */
1457c478bd9Sstevel@tonic-gate 	size_t		s_index;	/* section index */
1467c478bd9Sstevel@tonic-gate 	int		s_err;		/* for delaying data error */
1477c478bd9Sstevel@tonic-gate 	unsigned	s_shflags;	/* user shdr flags */
1487c478bd9Sstevel@tonic-gate 	unsigned	s_uflags;	/* user flags */
1497c478bd9Sstevel@tonic-gate 	unsigned	s_myflags;	/* SF_... */
1507c478bd9Sstevel@tonic-gate 	Dnode		s_dnode;	/* every scn needs one */
1517c478bd9Sstevel@tonic-gate };
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate NOTE(MUTEX_PROTECTS_DATA(Elf_Scn::s_mutex, Elf_Scn Dnode Elf_Data))
1547c478bd9Sstevel@tonic-gate NOTE(SCHEME_PROTECTS_DATA("Scn lock held", Elf_Data))
1557c478bd9Sstevel@tonic-gate NOTE(SCHEME_PROTECTS_DATA("Scn lock held", Elf32_Shdr Elf32_Sym))
1567c478bd9Sstevel@tonic-gate NOTE(READ_ONLY_DATA(Elf_Scn::s_elf))
1577c478bd9Sstevel@tonic-gate NOTE(READ_ONLY_DATA(Dnode::db_scn))
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Designates whether or not we are in a threaded_app.
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate extern int *_elf_libc_threaded;
1647c478bd9Sstevel@tonic-gate #define	elf_threaded	(_elf_libc_threaded && *_elf_libc_threaded)
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
1677c478bd9Sstevel@tonic-gate #define	SCNLOCK(x)	(void) mutex_lock(&((Elf_Scn *)x)->s_mutex);
1687c478bd9Sstevel@tonic-gate #else
1697c478bd9Sstevel@tonic-gate #define	SCNLOCK(x) \
1707c478bd9Sstevel@tonic-gate 	if (elf_threaded) \
1717c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&((Elf_Scn *)x)->s_mutex);
1727c478bd9Sstevel@tonic-gate #endif
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
1757c478bd9Sstevel@tonic-gate #define	SCNUNLOCK(x)	(void) mutex_unlock(&((Elf_Scn *)x)->s_mutex);
1767c478bd9Sstevel@tonic-gate #else
1777c478bd9Sstevel@tonic-gate #define	SCNUNLOCK(x) \
1787c478bd9Sstevel@tonic-gate 	if (elf_threaded) \
1797c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&((Elf_Scn *)x)->s_mutex);
1807c478bd9Sstevel@tonic-gate #endif
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
1837c478bd9Sstevel@tonic-gate #define	UPGRADELOCKS(e, s)\
1847c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&((Elf_Scn *)s)->s_mutex); \
1857c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
1867c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&((Elf *)e)->ed_rwlock);
1877c478bd9Sstevel@tonic-gate #else
1887c478bd9Sstevel@tonic-gate #define	UPGRADELOCKS(e, s)\
1897c478bd9Sstevel@tonic-gate 	if (elf_threaded) { \
1907c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&((Elf_Scn *)s)->s_mutex); \
1917c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
1927c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&((Elf *)e)->ed_rwlock); \
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate #endif
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
1977c478bd9Sstevel@tonic-gate #define	DOWNGRADELOCKS(e, s)\
1987c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
1997c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock); \
2007c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&((Elf_Scn *)s)->s_mutex);
2017c478bd9Sstevel@tonic-gate #else
2027c478bd9Sstevel@tonic-gate #define	DOWNGRADELOCKS(e, s)\
2037c478bd9Sstevel@tonic-gate 	if (elf_threaded) { \
2047c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
2057c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock); \
2067c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&((Elf_Scn *)s)->s_mutex); \
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate #endif
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
2117c478bd9Sstevel@tonic-gate #define	READLOCKS(e, s) \
2127c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock); \
2137c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&((Elf_Scn *)s)->s_mutex);
2147c478bd9Sstevel@tonic-gate #else
2157c478bd9Sstevel@tonic-gate #define	READLOCKS(e, s) \
2167c478bd9Sstevel@tonic-gate 	if (elf_threaded) { \
2177c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock); \
2187c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&((Elf_Scn *)s)->s_mutex); \
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate #endif
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
2237c478bd9Sstevel@tonic-gate #define	READUNLOCKS(e, s) \
2247c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&((Elf_Scn *)s)->s_mutex); \
2257c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&((Elf *)e)->ed_rwlock);
2267c478bd9Sstevel@tonic-gate #else
2277c478bd9Sstevel@tonic-gate #define	READUNLOCKS(e, s) \
2287c478bd9Sstevel@tonic-gate 	if (elf_threaded) { \
2297c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&((Elf_Scn *)s)->s_mutex); \
2307c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate #endif
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate #define	SF_ALLOC	0x1	/* applies to Scn */
2387c478bd9Sstevel@tonic-gate #define	SF_READY	0x2	/* has section been cooked */
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate struct	Snode32
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	Elf_Scn		sb_scn;		/* must be first */
2447c478bd9Sstevel@tonic-gate 	Elf32_Shdr	sb_shdr;
2457c478bd9Sstevel@tonic-gate };
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate struct	Snode64
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	Elf_Scn		sb_scn;		/* must be first */
2507c478bd9Sstevel@tonic-gate 	Elf64_Shdr	sb_shdr;
2517c478bd9Sstevel@tonic-gate };
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  *	A file's status controls how the library can use file data.
2567c478bd9Sstevel@tonic-gate  *	This is important to keep "raw" operations and "cooked"
2577c478bd9Sstevel@tonic-gate  *	operations from interfering with each other.
2587c478bd9Sstevel@tonic-gate  *
2597c478bd9Sstevel@tonic-gate  *	A file's status is "fresh" until something touches it.
2607c478bd9Sstevel@tonic-gate  *	If the first thing is a raw operation, we freeze the data
2617c478bd9Sstevel@tonic-gate  *	and force all cooking operations to make a copy.  If the
2627c478bd9Sstevel@tonic-gate  *	first operation cooks, raw operations use the file system.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate typedef enum
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	ES_FRESH = 0,	/* unchanged */
2687c478bd9Sstevel@tonic-gate 	ES_COOKED,	/* translated */
2697c478bd9Sstevel@tonic-gate 	ES_FROZEN	/* raw, can't be translated */
2707c478bd9Sstevel@tonic-gate } Status;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * Elf descriptor
2757c478bd9Sstevel@tonic-gate  *	The major handle between user code and the library.
2767c478bd9Sstevel@tonic-gate  *
2777c478bd9Sstevel@tonic-gate  *	Descriptors can have parents: archive members reference
2787c478bd9Sstevel@tonic-gate  *	the archive itself.  Relevant "offsets:"
2797c478bd9Sstevel@tonic-gate  *
2807c478bd9Sstevel@tonic-gate  *	ed_baseoff	The file offset, relative to zero, to the first
2817c478bd9Sstevel@tonic-gate  *			byte in the file.  For all files, this gives
2827c478bd9Sstevel@tonic-gate  *			the lseek(fd, ed_baseoff, 0) value.
2837c478bd9Sstevel@tonic-gate  *
2847c478bd9Sstevel@tonic-gate  *	ed_memoff	The offset from the beginning of the nesting file
2857c478bd9Sstevel@tonic-gate  *			to the bytes of a member.  For an archive member,
2867c478bd9Sstevel@tonic-gate  *			this is the offset from the beginning of the
2877c478bd9Sstevel@tonic-gate  *			archive to the member bytes (not the hdr).  If an
2887c478bd9Sstevel@tonic-gate  *			archive member slides, memoff changes.
2897c478bd9Sstevel@tonic-gate  *
2907c478bd9Sstevel@tonic-gate  *	ed_siboff	Similar to ed_memoff, this gives the offset from
2917c478bd9Sstevel@tonic-gate  *			the beginning of the nesting file to the following
2927c478bd9Sstevel@tonic-gate  *			sibling's header (not the sibling's bytes).  This
2937c478bd9Sstevel@tonic-gate  *			value is necessary, because of archive sliding.
2947c478bd9Sstevel@tonic-gate  *
2957c478bd9Sstevel@tonic-gate  *	ed_nextoff	For an archive, this gives the offset of the next
2967c478bd9Sstevel@tonic-gate  *			member to process on elf_begin.  That is,
2977c478bd9Sstevel@tonic-gate  *			(ed_ident + ed_nextoff) gives pointer to member hdr.
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  *	Keeping these absolute and relative offsets allows nesting of
3007c478bd9Sstevel@tonic-gate  *	files, including archives within archives, etc.  The only current
3017c478bd9Sstevel@tonic-gate  *	nesting file is archive, but others might be supported.
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  *	ed_image	This is a pointer to the base memory image holding
3047c478bd9Sstevel@tonic-gate  *			the file.  Library code assumes the image is aligned
3057c478bd9Sstevel@tonic-gate  *			to a boundary appropriate for any object.  This must
3067c478bd9Sstevel@tonic-gate  *			be true, because we get an image only from malloc
3077c478bd9Sstevel@tonic-gate  *			or mmap, both of which guarantee alignment.
3087c478bd9Sstevel@tonic-gate  */
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate struct Elf
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	rwlock_t	ed_rwlock;
3137c478bd9Sstevel@tonic-gate 	Elf		*ed_parent;	/* archive parent */
3147c478bd9Sstevel@tonic-gate 	int		ed_activ;	/* activation count */
3157c478bd9Sstevel@tonic-gate 	int		ed_fd;		/* file descriptor */
3167c478bd9Sstevel@tonic-gate 	Status		ed_status;	/* file's memory status */
3177c478bd9Sstevel@tonic-gate 	off_t		ed_baseoff;	/* base file offset, zero based */
3187c478bd9Sstevel@tonic-gate 	size_t		ed_memoff;	/* offset within archive */
3197c478bd9Sstevel@tonic-gate 	size_t		ed_siboff;	/* sibling offset with archive */
3207c478bd9Sstevel@tonic-gate 	size_t		ed_nextoff;	/* next archive member hdr offset */
3217c478bd9Sstevel@tonic-gate 	char		*ed_image;	/* pointer to file image */
3227c478bd9Sstevel@tonic-gate 	size_t		ed_imagesz;	/* # bytes in ed_image */
3237c478bd9Sstevel@tonic-gate 	char		*ed_wrimage;	/* pointer to output image */
3247c478bd9Sstevel@tonic-gate 	size_t		ed_wrimagesz;	/* # bytes in ed_wrimagesz */
3257c478bd9Sstevel@tonic-gate 	char		*ed_ident;	/* file start, getident() bytes */
3267c478bd9Sstevel@tonic-gate 	size_t		ed_identsz;	/* # bytes for getident() */
3277c478bd9Sstevel@tonic-gate 	char		*ed_raw;	/* raw file ptr */
3287c478bd9Sstevel@tonic-gate 	size_t		ed_fsz;		/* file size */
3297c478bd9Sstevel@tonic-gate 	unsigned	*ed_vm;		/* virtual memory map */
3307c478bd9Sstevel@tonic-gate 	size_t		ed_vmsz;	/* # regions in vm */
3317c478bd9Sstevel@tonic-gate 	unsigned	ed_encode;	/* data encoding */
3327c478bd9Sstevel@tonic-gate 	unsigned	ed_version;	/* file version */
3337c478bd9Sstevel@tonic-gate 	int		ed_class;	/* file class */
3347c478bd9Sstevel@tonic-gate 	Elf_Kind	ed_kind;	/* file type */
3357c478bd9Sstevel@tonic-gate 	Elf_Void	*ed_ehdr;	/* Elf{32,64}_Ehdr elf header */
3367c478bd9Sstevel@tonic-gate 	Elf_Void	*ed_phdr;	/* Elf{32,64}_Phdr phdr table */
3377c478bd9Sstevel@tonic-gate 	size_t		ed_phdrsz;	/* sizeof phdr table */
3387c478bd9Sstevel@tonic-gate 	Elf_Void	*ed_shdr;	/* Elf{32,64}_Shdr shdr table */
3397c478bd9Sstevel@tonic-gate 	Elf_Scn		*ed_hdscn;	/* head scn */
3407c478bd9Sstevel@tonic-gate 	Elf_Scn		*ed_tlscn;	/* tail scn */
3417c478bd9Sstevel@tonic-gate 	size_t		ed_scntabsz;	/* number sects. alloc. in table */
3427c478bd9Sstevel@tonic-gate 	Memlist		*ed_memlist;	/* list of archive member nodes */
3437c478bd9Sstevel@tonic-gate 	Member		*ed_armem;	/* archive member header */
3447c478bd9Sstevel@tonic-gate 	Elf_Void	*ed_arsym;	/* archive symbol table */
3457c478bd9Sstevel@tonic-gate 	size_t		ed_arsymsz;	/* archive symbol table size */
3467c478bd9Sstevel@tonic-gate 	size_t		ed_arsymoff;	/* archive symbol table hdr offset */
3477c478bd9Sstevel@tonic-gate 	char		*ed_arstr;	/* archive string table */
3487c478bd9Sstevel@tonic-gate 	size_t		ed_arstrsz;	/* archive string table size */
3497c478bd9Sstevel@tonic-gate 	size_t		ed_arstroff;	/* archive string table hdr offset */
3507c478bd9Sstevel@tonic-gate 	unsigned	ed_myflags;	/* EDF_... */
3517c478bd9Sstevel@tonic-gate 	unsigned	ed_ehflags;	/* ehdr flags */
3527c478bd9Sstevel@tonic-gate 	unsigned	ed_phflags;	/* phdr flags */
3537c478bd9Sstevel@tonic-gate 	unsigned	ed_uflags;	/* elf descriptor flags */
3547c478bd9Sstevel@tonic-gate };
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate NOTE(RWLOCK_PROTECTS_DATA(Elf::ed_rwlock, Elf))
3577c478bd9Sstevel@tonic-gate NOTE(RWLOCK_COVERS_LOCKS(Elf::ed_rwlock, Elf_Scn::s_mutex))
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
3607c478bd9Sstevel@tonic-gate #define	ELFRLOCK(e)	(void) rw_rdlock(&((Elf *)e)->ed_rwlock);
3617c478bd9Sstevel@tonic-gate #else
3627c478bd9Sstevel@tonic-gate #define	ELFRLOCK(e) \
3637c478bd9Sstevel@tonic-gate 	if (elf_threaded) \
3647c478bd9Sstevel@tonic-gate 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock);
3657c478bd9Sstevel@tonic-gate #endif
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
3687c478bd9Sstevel@tonic-gate #define	ELFWLOCK(e)	(void) rw_wrlock(&((Elf *)e)->ed_rwlock);
3697c478bd9Sstevel@tonic-gate #else
3707c478bd9Sstevel@tonic-gate #define	ELFWLOCK(e) \
3717c478bd9Sstevel@tonic-gate 	if (elf_threaded) \
3727c478bd9Sstevel@tonic-gate 		(void) rw_wrlock(&((Elf *)e)->ed_rwlock);
3737c478bd9Sstevel@tonic-gate #endif
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
3767c478bd9Sstevel@tonic-gate #define	ELFUNLOCK(e)	(void) rw_unlock(&((Elf *)e)->ed_rwlock);
3777c478bd9Sstevel@tonic-gate #else
3787c478bd9Sstevel@tonic-gate #define	ELFUNLOCK(e) \
3797c478bd9Sstevel@tonic-gate 	if (elf_threaded) \
3807c478bd9Sstevel@tonic-gate 		(void) rw_unlock(&((Elf *)e)->ed_rwlock);
3817c478bd9Sstevel@tonic-gate #endif
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate #define	EDF_ASALLOC	0x1	/* applies to ed_arsym */
3847c478bd9Sstevel@tonic-gate #define	EDF_EHALLOC	0x2	/* applies to ed_ehdr */
3857c478bd9Sstevel@tonic-gate #define	EDF_PHALLOC	0x4	/* applies to ed_phdr */
3867c478bd9Sstevel@tonic-gate #define	EDF_SHALLOC	0x8	/* applies to ed_shdr */
3877c478bd9Sstevel@tonic-gate #define	EDF_COFFAOUT	0x10	/* original file was coff a.out */
3887c478bd9Sstevel@tonic-gate #define	EDF_RAWALLOC	0x20	/* applies to ed_raw */
3897c478bd9Sstevel@tonic-gate #define	EDF_READ	0x40	/* file can be read */
3907c478bd9Sstevel@tonic-gate #define	EDF_WRITE	0x80	/* file can be written */
3917c478bd9Sstevel@tonic-gate #define	EDF_MEMORY	0x100	/* file opened via elf_memory() */
3927c478bd9Sstevel@tonic-gate #define	EDF_ASTRALLOC	0x200	/* applies to ed_arstr */
3937c478bd9Sstevel@tonic-gate #define	EDF_MPROTECT	0x400	/* applies to slideable archives */
3947c478bd9Sstevel@tonic-gate #define	EDF_IMALLOC	0x800	/* wrimage dynamically allocated */
3957c478bd9Sstevel@tonic-gate #define	EDF_WRALLOC	0x1000	/* wrimage is to by dyn allocated */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate typedef enum
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	OK_YES = 0,
4017c478bd9Sstevel@tonic-gate 	OK_NO = ~0
4027c478bd9Sstevel@tonic-gate } Okay;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate #define	_(a)		a
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate  * Max size for an Elf error message string
4087c478bd9Sstevel@tonic-gate  */
4097c478bd9Sstevel@tonic-gate #define	MAXELFERR	1024
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate  * General thread management macros
4137c478bd9Sstevel@tonic-gate  */
4147c478bd9Sstevel@tonic-gate #ifdef __lock_lint
4157c478bd9Sstevel@tonic-gate #define	ELFACCESSDATA(a, b) \
4167c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&_elf_globals_mutex); \
4177c478bd9Sstevel@tonic-gate 	a = b; \
4187c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&_elf_globals_mutex);
4197c478bd9Sstevel@tonic-gate #else
4207c478bd9Sstevel@tonic-gate #define	ELFACCESSDATA(a, b) \
4217c478bd9Sstevel@tonic-gate 	if (elf_threaded) { \
4227c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&_elf_globals_mutex); \
4237c478bd9Sstevel@tonic-gate 		a = b; \
4247c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&_elf_globals_mutex); \
4257c478bd9Sstevel@tonic-gate 	} else \
4267c478bd9Sstevel@tonic-gate 		a = b;
4277c478bd9Sstevel@tonic-gate #endif
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate #ifdef __lock_lint
4307c478bd9Sstevel@tonic-gate #define	ELFRWLOCKINIT(lock) \
4317c478bd9Sstevel@tonic-gate 	(void) rwlock_init((lock), USYNC_THREAD, 0);
4327c478bd9Sstevel@tonic-gate #else
4337c478bd9Sstevel@tonic-gate #define	ELFRWLOCKINIT(lock) \
4347c478bd9Sstevel@tonic-gate 	if (elf_threaded) { \
4357c478bd9Sstevel@tonic-gate 		(void) rwlock_init((lock), USYNC_THREAD, 0); \
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate #endif
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate #ifdef	__lock_lint
4407c478bd9Sstevel@tonic-gate #define	ELFMUTEXINIT(lock) \
4417c478bd9Sstevel@tonic-gate 	(void) mutex_init(lock, USYNC_THREAD, 0);
4427c478bd9Sstevel@tonic-gate #else
4437c478bd9Sstevel@tonic-gate #define	ELFMUTEXINIT(lock) \
4447c478bd9Sstevel@tonic-gate 	if (elf_threaded) { \
4457c478bd9Sstevel@tonic-gate 		(void) mutex_init(lock, USYNC_THREAD, 0); \
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate #endif
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 
450*4899432aSab extern Member		*_elf_armem(Elf *, char *, size_t);
4517c478bd9Sstevel@tonic-gate extern void		_elf_arinit(Elf *);
4527c478bd9Sstevel@tonic-gate extern Okay		_elf_cook(Elf *);
4537c478bd9Sstevel@tonic-gate extern Okay		_elf_cookscn(Elf_Scn * s);
4547c478bd9Sstevel@tonic-gate extern Okay		_elf32_cookscn(Elf_Scn * s);
4557c478bd9Sstevel@tonic-gate extern Okay		_elf64_cookscn(Elf_Scn * s);
456*4899432aSab extern Dnode		*_elf_dnode(void);
457*4899432aSab extern Elf_Data		*_elf_locked_getdata(Elf_Scn *, Elf_Data *);
4587c478bd9Sstevel@tonic-gate extern size_t		_elf32_entsz(Elf32_Word, unsigned);
4597c478bd9Sstevel@tonic-gate extern size_t		_elf64_entsz(Elf64_Word, unsigned);
4607c478bd9Sstevel@tonic-gate extern Okay		_elf_inmap(Elf *);
461*4899432aSab extern char		*_elf_outmap(int, size_t, unsigned *);
4627c478bd9Sstevel@tonic-gate extern size_t		_elf_outsync(int, char *, size_t, unsigned);
4637c478bd9Sstevel@tonic-gate extern size_t		_elf32_msize(Elf_Type, unsigned);
4647c478bd9Sstevel@tonic-gate extern size_t		_elf64_msize(Elf_Type, unsigned);
4657c478bd9Sstevel@tonic-gate extern Elf_Type		_elf32_mtype(Elf *, Elf32_Word, unsigned);
4667c478bd9Sstevel@tonic-gate extern Elf_Type		_elf64_mtype(Elf *, Elf64_Word, unsigned);
467*4899432aSab extern char		*_elf_read(int, off_t, size_t);
468*4899432aSab extern Snode32		*_elf32_snode(void);
469*4899432aSab extern Snode64		*_elf64_snode(void);
4707c478bd9Sstevel@tonic-gate extern void		_elf_unmap(char *, size_t);
4717c478bd9Sstevel@tonic-gate extern Okay		_elf_vm(Elf *, size_t, size_t);
4727c478bd9Sstevel@tonic-gate extern int		_elf32_ehdr(Elf *, int);
4737c478bd9Sstevel@tonic-gate extern int		_elf32_phdr(Elf *, int);
4747c478bd9Sstevel@tonic-gate extern int		_elf32_shdr(Elf *, int);
4757c478bd9Sstevel@tonic-gate extern int		_elf64_ehdr(Elf *, int);
4767c478bd9Sstevel@tonic-gate extern int		_elf64_phdr(Elf *, int);
4777c478bd9Sstevel@tonic-gate extern int		_elf64_shdr(Elf *, int);
4787c478bd9Sstevel@tonic-gate extern int		_elf_byte;
4797c478bd9Sstevel@tonic-gate extern const Elf32_Ehdr	_elf32_ehdr_init;
4807c478bd9Sstevel@tonic-gate extern const Elf64_Ehdr	_elf64_ehdr_init;
4817c478bd9Sstevel@tonic-gate extern unsigned		_elf_encode;
4827c478bd9Sstevel@tonic-gate extern void		_elf_seterr(Msg, int);
4837c478bd9Sstevel@tonic-gate extern const Snode32	_elf32_snode_init;
4847c478bd9Sstevel@tonic-gate extern const Snode64	_elf64_snode_init;
4857c478bd9Sstevel@tonic-gate extern const Dnode	_elf_dnode_init;
4867c478bd9Sstevel@tonic-gate extern unsigned		_elf_work;
4877c478bd9Sstevel@tonic-gate extern mutex_t		_elf_globals_mutex;
4887c478bd9Sstevel@tonic-gate extern off_t		_elf64_update(Elf * elf, Elf_Cmd cmd);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /* CSTYLED */
4917c478bd9Sstevel@tonic-gate NOTE(MUTEX_PROTECTS_DATA(_elf_globals_mutex, \
4927c478bd9Sstevel@tonic-gate 	_elf_byte _elf32_ehdr_init _elf64_ehdr_init _elf_encode \
4937c478bd9Sstevel@tonic-gate 	_elf_snode_init _elf_work))
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate #endif
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate #endif	/* _DECL_H */
500