xref: /titanic_44/usr/src/cmd/sgs/libelf/common/decl.h (revision b509e89b2befbaa42939abad9da1d7f5a8c6aaae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*	Copyright (c) 1988 AT&T	*/
22 /*	  All Rights Reserved  	*/
23 
24 
25 /*
26  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #ifndef	_DECL_H
31 #define	_DECL_H
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.9	*/
34 
35 #include <thread.h>
36 #include <note.h>
37 #include <_libelf.h>
38 #include <sys/machelf.h>
39 #include <msg.h>
40 
41 
42 #ifdef	__cplusplus
43 extern "C" {
44 #endif
45 
46 typedef struct Member	Member;
47 typedef struct Memlist	Memlist;
48 typedef struct Memident	Memident;
49 typedef struct Dnode	Dnode;
50 typedef struct Snode32	Snode32;
51 typedef struct Snode64	Snode64;
52 
53 
54 /*
55  * Data alignment
56  *	An elf file is defined to have its structures aligned on
57  *	appropriate boundaries.  The following type lets the
58  *	library test whether the file's alignment meets its own
59  *	constraints in memory.  This assumes every machine uses
60  *	an alignment that is no greater than an object's size.
61  *	The pointer isn't relevant for the file, but the code uses
62  *	it to get memory alignment.  ANSI C void * holds any pointer,
63  *	making it appropriate here.
64  */
65 
66 typedef union
67 {
68 	Elf32_Word	w;
69 	Elf32_Addr	a;
70 	Elf32_Off	o;
71 } Elf32;
72 
73 typedef union {
74 	Elf64_Xword	x;
75 	Elf64_Word	w;
76 	Elf64_Addr	a;
77 	Elf64_Off	o;
78 	Elf_Void	*p;
79 } Elf64;
80 
81 
82 /*
83  * Memory allocation
84  *	Structures are obtained several ways: file mapping,
85  *	malloc(), from the user.  A status bit in the structures
86  *	tells whether an object was obtained with malloc() and
87  *	therefore should be released with free().  The bits
88  *	named ...ALLOC indicate this.
89  */
90 
91 
92 /*
93  * Data descriptor
94  *	db_data must be first in the Dnode structure, because
95  *	&db_data must == &Dnode.
96  *
97  *	db_buf is a pointer to an allocated buffer.  The same value
98  *	goes into db_data.d_buf originally, but the user can touch
99  *	it.  If the data buffer is not to be freed, db_buf is null.
100  *
101  *	When "reading" an input file's buffer, the data are left
102  *	alone until needed.  When they've been converted to internal
103  *	form, the READY flag is set.
104  *
105  *	db_raw points to a parallel raw buffer.  Raw buffers
106  *	have null db_raw.
107  */
108 
109 struct	Dnode
110 {
111 	Elf_Data	db_data;
112 	Elf_Scn		*db_scn;	/* section parent */
113 	Dnode		*db_next;
114 	Dnode		*db_raw;	/* raw data */
115 	off_t		db_off;		/* orig file offset, 0 o/w */
116 	size_t		db_fsz;		/* orig file size, 0 o/w */
117 	size_t		db_shsz;	/* orig shdr size, 0 o/w */
118 	size_t		db_osz;		/* output size for update */
119 	Elf_Void	*db_buf;	/* allocated data buffer */
120 	unsigned	db_uflags;	/* user flags: ELF_F_... */
121 	unsigned	db_myflags;	/* internal flags: DBF_... */
122 	Elf64_Off	db_xoff;	/* extended offset for 32-bit Elf64 */
123 };
124 
125 #define	DBF_ALLOC	0x1	/* applies to Dnode itself */
126 #define	DBF_READY	0x2	/* buffer ready */
127 
128 
129 /*
130  * Section descriptor
131  *	These are sometimes allocated in a block.  If the SF_ALLOC
132  *	bit is set in the flags, the Scn address may be passed to free.
133  *	The caller must first follow the s_next list to the next freeable
134  *	node, because free can clobber the s_next value in the block.
135  */
136 
137 struct	Elf_Scn
138 {
139 	mutex_t		s_mutex;
140 	Elf_Scn		*s_next;	/* next section */
141 	Elf		*s_elf; 	/* parent file */
142 	Dnode		*s_hdnode;	/* head Dnode */
143 	Dnode		*s_tlnode;	/* tail Dnode */
144 	Elf_Void	*s_shdr;	/* Elf32 or Elf64 scn header */
145 	size_t		s_index;	/* section index */
146 	int		s_err;		/* for delaying data error */
147 	unsigned	s_shflags;	/* user shdr flags */
148 	unsigned	s_uflags;	/* user flags */
149 	unsigned	s_myflags;	/* SF_... */
150 	Dnode		s_dnode;	/* every scn needs one */
151 };
152 
153 NOTE(MUTEX_PROTECTS_DATA(Elf_Scn::s_mutex, Elf_Scn Dnode Elf_Data))
154 NOTE(SCHEME_PROTECTS_DATA("Scn lock held", Elf_Data))
155 NOTE(SCHEME_PROTECTS_DATA("Scn lock held", Elf32_Shdr Elf32_Sym))
156 NOTE(READ_ONLY_DATA(Elf_Scn::s_elf))
157 NOTE(READ_ONLY_DATA(Dnode::db_scn))
158 
159 
160 /*
161  * Designates whether or not we are in a threaded_app.
162  */
163 extern int *_elf_libc_threaded;
164 #define	elf_threaded	(_elf_libc_threaded && *_elf_libc_threaded)
165 
166 #ifdef	__lock_lint
167 #define	SCNLOCK(x)	(void) mutex_lock(&((Elf_Scn *)x)->s_mutex);
168 #else
169 #define	SCNLOCK(x) \
170 	if (elf_threaded) \
171 		(void) mutex_lock(&((Elf_Scn *)x)->s_mutex);
172 #endif
173 
174 #ifdef	__lock_lint
175 #define	SCNUNLOCK(x)	(void) mutex_unlock(&((Elf_Scn *)x)->s_mutex);
176 #else
177 #define	SCNUNLOCK(x) \
178 	if (elf_threaded) \
179 		(void) mutex_unlock(&((Elf_Scn *)x)->s_mutex);
180 #endif
181 
182 #ifdef	__lock_lint
183 #define	UPGRADELOCKS(e, s)\
184 		(void) mutex_unlock(&((Elf_Scn *)s)->s_mutex); \
185 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
186 		(void) rw_wrlock(&((Elf *)e)->ed_rwlock);
187 #else
188 #define	UPGRADELOCKS(e, s)\
189 	if (elf_threaded) { \
190 		(void) mutex_unlock(&((Elf_Scn *)s)->s_mutex); \
191 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
192 		(void) rw_wrlock(&((Elf *)e)->ed_rwlock); \
193 	}
194 #endif
195 
196 #ifdef	__lock_lint
197 #define	DOWNGRADELOCKS(e, s)\
198 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
199 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock); \
200 		(void) mutex_lock(&((Elf_Scn *)s)->s_mutex);
201 #else
202 #define	DOWNGRADELOCKS(e, s)\
203 	if (elf_threaded) { \
204 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
205 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock); \
206 		(void) mutex_lock(&((Elf_Scn *)s)->s_mutex); \
207 	}
208 #endif
209 
210 #ifdef	__lock_lint
211 #define	READLOCKS(e, s) \
212 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock); \
213 		(void) mutex_lock(&((Elf_Scn *)s)->s_mutex);
214 #else
215 #define	READLOCKS(e, s) \
216 	if (elf_threaded) { \
217 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock); \
218 		(void) mutex_lock(&((Elf_Scn *)s)->s_mutex); \
219 	}
220 #endif
221 
222 #ifdef	__lock_lint
223 #define	READUNLOCKS(e, s) \
224 		(void) mutex_unlock(&((Elf_Scn *)s)->s_mutex); \
225 		(void) rw_unlock(&((Elf *)e)->ed_rwlock);
226 #else
227 #define	READUNLOCKS(e, s) \
228 	if (elf_threaded) { \
229 		(void) mutex_unlock(&((Elf_Scn *)s)->s_mutex); \
230 		(void) rw_unlock(&((Elf *)e)->ed_rwlock); \
231 	}
232 #endif
233 
234 
235 
236 
237 #define	SF_ALLOC	0x1	/* applies to Scn */
238 #define	SF_READY	0x2	/* has section been cooked */
239 
240 
241 struct	Snode32
242 {
243 	Elf_Scn		sb_scn;		/* must be first */
244 	Elf32_Shdr	sb_shdr;
245 };
246 
247 struct	Snode64
248 {
249 	Elf_Scn		sb_scn;		/* must be first */
250 	Elf64_Shdr	sb_shdr;
251 };
252 
253 
254 /*
255  *	A file's status controls how the library can use file data.
256  *	This is important to keep "raw" operations and "cooked"
257  *	operations from interfering with each other.
258  *
259  *	A file's status is "fresh" until something touches it.
260  *	If the first thing is a raw operation, we freeze the data
261  *	and force all cooking operations to make a copy.  If the
262  *	first operation cooks, raw operations use the file system.
263  */
264 
265 typedef enum
266 {
267 	ES_FRESH = 0,	/* unchanged */
268 	ES_COOKED,	/* translated */
269 	ES_FROZEN	/* raw, can't be translated */
270 } Status;
271 
272 
273 /*
274  * Elf descriptor
275  *	The major handle between user code and the library.
276  *
277  *	Descriptors can have parents: archive members reference
278  *	the archive itself.  Relevant "offsets:"
279  *
280  *	ed_baseoff	The file offset, relative to zero, to the first
281  *			byte in the file.  For all files, this gives
282  *			the lseek(fd, ed_baseoff, 0) value.
283  *
284  *	ed_memoff	The offset from the beginning of the nesting file
285  *			to the bytes of a member.  For an archive member,
286  *			this is the offset from the beginning of the
287  *			archive to the member bytes (not the hdr).  If an
288  *			archive member slides, memoff changes.
289  *
290  *	ed_siboff	Similar to ed_memoff, this gives the offset from
291  *			the beginning of the nesting file to the following
292  *			sibling's header (not the sibling's bytes).  This
293  *			value is necessary, because of archive sliding.
294  *
295  *	ed_nextoff	For an archive, this gives the offset of the next
296  *			member to process on elf_begin.  That is,
297  *			(ed_ident + ed_nextoff) gives pointer to member hdr.
298  *
299  *	Keeping these absolute and relative offsets allows nesting of
300  *	files, including archives within archives, etc.  The only current
301  *	nesting file is archive, but others might be supported.
302  *
303  *	ed_image	This is a pointer to the base memory image holding
304  *			the file.  Library code assumes the image is aligned
305  *			to a boundary appropriate for any object.  This must
306  *			be true, because we get an image only from malloc
307  *			or mmap, both of which guarantee alignment.
308  */
309 
310 struct Elf
311 {
312 	rwlock_t	ed_rwlock;
313 	Elf		*ed_parent;	/* archive parent */
314 	int		ed_activ;	/* activation count */
315 	int		ed_fd;		/* file descriptor */
316 	Status		ed_status;	/* file's memory status */
317 	off_t		ed_baseoff;	/* base file offset, zero based */
318 	size_t		ed_memoff;	/* offset within archive */
319 	size_t		ed_siboff;	/* sibling offset with archive */
320 	size_t		ed_nextoff;	/* next archive member hdr offset */
321 	char		*ed_image;	/* pointer to file image */
322 	size_t		ed_imagesz;	/* # bytes in ed_image */
323 	char		*ed_wrimage;	/* pointer to output image */
324 	size_t		ed_wrimagesz;	/* # bytes in ed_wrimagesz */
325 	char		*ed_ident;	/* file start, getident() bytes */
326 	size_t		ed_identsz;	/* # bytes for getident() */
327 	char		*ed_raw;	/* raw file ptr */
328 	size_t		ed_fsz;		/* file size */
329 	unsigned	*ed_vm;		/* virtual memory map */
330 	size_t		ed_vmsz;	/* # regions in vm */
331 	unsigned	ed_encode;	/* data encoding */
332 	unsigned	ed_version;	/* file version */
333 	int		ed_class;	/* file class */
334 	Elf_Kind	ed_kind;	/* file type */
335 	Elf_Void	*ed_ehdr;	/* Elf{32,64}_Ehdr elf header */
336 	Elf_Void	*ed_phdr;	/* Elf{32,64}_Phdr phdr table */
337 	size_t		ed_phdrsz;	/* sizeof phdr table */
338 	Elf_Void	*ed_shdr;	/* Elf{32,64}_Shdr shdr table */
339 	Elf_Scn		*ed_hdscn;	/* head scn */
340 	Elf_Scn		*ed_tlscn;	/* tail scn */
341 	size_t		ed_scntabsz;	/* number sects. alloc. in table */
342 	Memlist		*ed_memlist;	/* list of archive member nodes */
343 	Member		*ed_armem;	/* archive member header */
344 	Elf_Void	*ed_arsym;	/* archive symbol table */
345 	size_t		ed_arsymsz;	/* archive symbol table size */
346 	size_t		ed_arsymoff;	/* archive symbol table hdr offset */
347 	char		*ed_arstr;	/* archive string table */
348 	size_t		ed_arstrsz;	/* archive string table size */
349 	size_t		ed_arstroff;	/* archive string table hdr offset */
350 	unsigned	ed_myflags;	/* EDF_... */
351 	unsigned	ed_ehflags;	/* ehdr flags */
352 	unsigned	ed_phflags;	/* phdr flags */
353 	unsigned	ed_uflags;	/* elf descriptor flags */
354 };
355 
356 NOTE(RWLOCK_PROTECTS_DATA(Elf::ed_rwlock, Elf))
357 NOTE(RWLOCK_COVERS_LOCKS(Elf::ed_rwlock, Elf_Scn::s_mutex))
358 
359 #ifdef	__lock_lint
360 #define	ELFRLOCK(e)	(void) rw_rdlock(&((Elf *)e)->ed_rwlock);
361 #else
362 #define	ELFRLOCK(e) \
363 	if (elf_threaded) \
364 		(void) rw_rdlock(&((Elf *)e)->ed_rwlock);
365 #endif
366 
367 #ifdef	__lock_lint
368 #define	ELFWLOCK(e)	(void) rw_wrlock(&((Elf *)e)->ed_rwlock);
369 #else
370 #define	ELFWLOCK(e) \
371 	if (elf_threaded) \
372 		(void) rw_wrlock(&((Elf *)e)->ed_rwlock);
373 #endif
374 
375 #ifdef	__lock_lint
376 #define	ELFUNLOCK(e)	(void) rw_unlock(&((Elf *)e)->ed_rwlock);
377 #else
378 #define	ELFUNLOCK(e) \
379 	if (elf_threaded) \
380 		(void) rw_unlock(&((Elf *)e)->ed_rwlock);
381 #endif
382 
383 #define	EDF_ASALLOC	0x1	/* applies to ed_arsym */
384 #define	EDF_EHALLOC	0x2	/* applies to ed_ehdr */
385 #define	EDF_PHALLOC	0x4	/* applies to ed_phdr */
386 #define	EDF_SHALLOC	0x8	/* applies to ed_shdr */
387 #define	EDF_COFFAOUT	0x10	/* original file was coff a.out */
388 #define	EDF_RAWALLOC	0x20	/* applies to ed_raw */
389 #define	EDF_READ	0x40	/* file can be read */
390 #define	EDF_WRITE	0x80	/* file can be written */
391 #define	EDF_MEMORY	0x100	/* file opened via elf_memory() */
392 #define	EDF_ASTRALLOC	0x200	/* applies to ed_arstr */
393 #define	EDF_MPROTECT	0x400	/* applies to slideable archives */
394 #define	EDF_IMALLOC	0x800	/* wrimage dynamically allocated */
395 #define	EDF_WRALLOC	0x1000	/* wrimage is to by dyn allocated */
396 
397 
398 typedef enum
399 {
400 	OK_YES = 0,
401 	OK_NO = ~0
402 } Okay;
403 
404 #define	_(a)		a
405 
406 /*
407  * Max size for an Elf error message string
408  */
409 #define	MAXELFERR	1024
410 
411 /*
412  * General thread management macros
413  */
414 #ifdef __lock_lint
415 #define	ELFACCESSDATA(a, b) \
416 	(void) mutex_lock(&_elf_globals_mutex); \
417 	a = b; \
418 	(void) mutex_unlock(&_elf_globals_mutex);
419 #else
420 #define	ELFACCESSDATA(a, b) \
421 	if (elf_threaded) { \
422 		(void) mutex_lock(&_elf_globals_mutex); \
423 		a = b; \
424 		(void) mutex_unlock(&_elf_globals_mutex); \
425 	} else \
426 		a = b;
427 #endif
428 
429 #ifdef __lock_lint
430 #define	ELFRWLOCKINIT(lock) \
431 	(void) rwlock_init((lock), USYNC_THREAD, 0);
432 #else
433 #define	ELFRWLOCKINIT(lock) \
434 	if (elf_threaded) { \
435 		(void) rwlock_init((lock), USYNC_THREAD, 0); \
436 	}
437 #endif
438 
439 #ifdef	__lock_lint
440 #define	ELFMUTEXINIT(lock) \
441 	(void) mutex_init(lock, USYNC_THREAD, 0);
442 #else
443 #define	ELFMUTEXINIT(lock) \
444 	if (elf_threaded) { \
445 		(void) mutex_init(lock, USYNC_THREAD, 0); \
446 	}
447 #endif
448 
449 
450 extern Member		*_elf_armem(Elf *, char *, size_t);
451 extern void		_elf_arinit(Elf *);
452 extern Okay		_elf_cook(Elf *);
453 extern Okay		_elf_cookscn(Elf_Scn * s);
454 extern Okay		_elf32_cookscn(Elf_Scn * s);
455 extern Okay		_elf64_cookscn(Elf_Scn * s);
456 extern Dnode		*_elf_dnode(void);
457 extern Elf_Data		*_elf_locked_getdata(Elf_Scn *, Elf_Data *);
458 extern size_t		_elf32_entsz(Elf *elf, Elf32_Word, unsigned);
459 extern size_t		_elf64_entsz(Elf *elf, Elf64_Word, unsigned);
460 extern Okay		_elf_inmap(Elf *);
461 extern char		*_elf_outmap(int, size_t, unsigned *);
462 extern size_t		_elf_outsync(int, char *, size_t, unsigned);
463 extern size_t		_elf32_msize(Elf_Type, unsigned);
464 extern size_t		_elf64_msize(Elf_Type, unsigned);
465 extern Elf_Type		_elf32_mtype(Elf *, Elf32_Word, unsigned);
466 extern Elf_Type		_elf64_mtype(Elf *, Elf64_Word, unsigned);
467 extern char		*_elf_read(int, off_t, size_t);
468 extern Snode32		*_elf32_snode(void);
469 extern Snode64		*_elf64_snode(void);
470 extern void		_elf_unmap(char *, size_t);
471 extern Okay		_elf_vm(Elf *, size_t, size_t);
472 extern int		_elf32_ehdr(Elf *, int);
473 extern int		_elf32_phdr(Elf *, int);
474 extern int		_elf32_shdr(Elf *, int);
475 extern int		_elf64_ehdr(Elf *, int);
476 extern int		_elf64_phdr(Elf *, int);
477 extern int		_elf64_shdr(Elf *, int);
478 extern int		_elf_byte;
479 extern const Elf32_Ehdr	_elf32_ehdr_init;
480 extern const Elf64_Ehdr	_elf64_ehdr_init;
481 extern unsigned		_elf_encode;
482 extern void		_elf_seterr(Msg, int);
483 extern const Snode32	_elf32_snode_init;
484 extern const Snode64	_elf64_snode_init;
485 extern const Dnode	_elf_dnode_init;
486 extern unsigned		_elf_work;
487 extern mutex_t		_elf_globals_mutex;
488 extern off_t		_elf64_update(Elf * elf, Elf_Cmd cmd);
489 extern int		_elf64_swap_wrimage(Elf *elf);
490 
491 /* CSTYLED */
492 NOTE(MUTEX_PROTECTS_DATA(_elf_globals_mutex, \
493 	_elf_byte _elf32_ehdr_init _elf64_ehdr_init _elf_encode \
494 	_elf_snode_init _elf_work))
495 
496 #ifdef	__cplusplus
497 }
498 #endif
499 
500 #endif	/* _DECL_H */
501