xref: /titanic_44/usr/src/cmd/sgs/include/rtld.h (revision cb8a054b1ab30d5caa746e6c44f29d4c9d3071c1)
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 
22 /*
23  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 #ifndef	_RTLD_H
26 #define	_RTLD_H
27 
28 /*
29  * Global include file for the runtime linker.
30  */
31 #include <sys/mman.h>
32 #include <time.h>
33 #include <sgs.h>
34 #include <thread.h>
35 #include <synch.h>
36 #include <link.h>
37 #include <sys/avl.h>
38 #include <alist.h>
39 #include <libc_int.h>
40 #include <elfcap.h>
41 
42 #ifdef	_SYSCALL32
43 #include <inttypes.h>
44 #endif
45 
46 #ifdef	__cplusplus
47 extern "C" {
48 #endif
49 
50 /*
51  * We use rtld_ino_t instead of ino_t so that we can get
52  * access to large inode values from 32-bit code.
53  */
54 #ifdef _LP64
55 typedef ino_t		rtld_ino_t;
56 #else
57 typedef ino64_t		rtld_ino_t;
58 #endif
59 
60 typedef struct rt_map	Rt_map;
61 typedef struct slookup	Slookup;
62 typedef struct sresult	Sresult;
63 
64 /*
65  * A binding descriptor.  Establishes the binding relationship between two
66  * objects, the caller (originator) and the dependency (destination).
67  *
68  * Every relationship between two objects is tracked by a binding descriptor.
69  * This descriptor is referenced from a link-map's DEPENDS and CALLERS lists.
70  * Note, Aplist's are diagramed to fully expose the allocations required to
71  * establish the data structure relationships.
72  *
73  *                                  Bnd_desc
74  *                                 ----------
75  *                    ------------| b_caller |
76  *                   |            | b_depend | ----------
77  *                   |            |          |           |
78  *      Rt_map       |             ----------            |       Rt_map
79  *    ----------     |                ^ ^                |     ----------
80  *   |          | <--                 | |                 --> |          |
81  *   |          |        --------     | |                     |          |
82  *   | DEPENDS  | ----> |        |    | |     --------        |          |
83  *   |          |       |        |    | |    |        | <---- | CALLERS  |
84  *   |          |       |        | ---  |    |        |       |          |
85  *   |          |       |        |       --- |        |       |          |
86  *   |          |        --------            |        |       |          |
87  *    ----------          Aplist              --------         ----------
88  *                                             Aplist
89  */
90 typedef struct {
91 	Rt_map		*b_caller;	/* caller (originator) of a binding */
92 	Rt_map		*b_depend;	/* dependency (destination) of a */
93 					/*	binding */
94 	uint_t		b_flags;	/* relationship of caller to the */
95 					/*	dependency */
96 } Bnd_desc;
97 
98 #define	BND_NEEDED	0x0001		/* caller NEEDED the dependency */
99 #define	BND_REFER	0x0002		/* caller relocation references the */
100 					/*	dependency */
101 #define	BND_FILTER	0x0004		/* binding identifies filter, used */
102 					/*	for diagnostics only */
103 /*
104  * Private structure for communication between rtld_db and rtld.
105  *
106  * We must bump the version number when ever an update in one of the
107  * structures/fields that rtld_db reads is updated.  This hopefully permits
108  * rtld_db implementations of the future to recognize core files produced on
109  * older systems and deal with these core files accordingly.
110  *
111  * As of version 'R_RTLDDB_VERSION <= 2' the following fields were valid for
112  * core file examination (basically the public Link_map):
113  *
114  *		ADDR()
115  *		NAME()
116  *		DYN()
117  *		NEXT()
118  *		PREV()
119  *
120  * Valid fields for R_RTLDDB_VERSION3
121  *
122  *		PATHNAME()
123  *		PADSTART()
124  *		PADIMLEN()
125  *		MSIZE()
126  *		FLAGS()
127  *		FLAGS1()
128  *
129  * Valid fields for R_RTLDDB_VERSION4
130  *
131  *		TLSMODID()
132  *
133  * Valid fields for R_RTLDDB_VERSION5
134  *
135  *		Added rtld_flags & FLG_RT_RELOCED to stable flags range
136  *
137  * Valid fields for R_RTLDDB_VERSION6
138  *
139  *		rtd_dynlmlst converted from a List to APlist
140  */
141 #define	R_RTLDDB_VERSION1	1	/* base version level - used for core */
142 					/*	file examination */
143 #define	R_RTLDDB_VERSION2	2	/* minor revision - not relevant for */
144 					/*	core files */
145 #define	R_RTLDDB_VERSION3	3
146 #define	R_RTLDDB_VERSION4	4
147 #define	R_RTLDDB_VERSION5	5
148 #define	R_RTLDDB_VERSION6	6
149 #define	R_RTLDDB_VERSION	R_RTLDDB_VERSION6	/* current version */
150 
151 typedef struct rtld_db_priv {
152 	struct r_debug	rtd_rdebug;	/* original r_debug structure */
153 	Word		rtd_version;	/* version no. */
154 	size_t		rtd_objpad;	/* padding around mmap()ed objects */
155 	APlist		**rtd_dynlmlst;	/* pointer to dynlm_list pointer */
156 } Rtld_db_priv;
157 
158 #ifdef _SYSCALL32
159 typedef struct rtld_db_priv32 {
160 	struct r_debug32 rtd_rdebug;	/* original r_debug structure */
161 	Elf32_Word	rtd_version;	/* version no. */
162 	Elf32_Word	rtd_objpad;	/* padding around mmap()ed objects */
163 	Elf32_Addr	rtd_dynlmlst;	/* pointer to dynlm_list */
164 } Rtld_db_priv32;
165 #endif	/* _SYSCALL32 */
166 
167 /*
168  * External function definitions.  ld.so.1 must convey information to libc in
169  * regards to threading.  libc also provides routines for atexit() and message
170  * localization.  libc provides the necessary interfaces via its RTLDINFO
171  * structure and/or later _ld_libc() calls.
172  *
173  * These external functions are maintained for each link-map list, and used
174  * where appropriate.  The functions are associated with the object that
175  * provided them, so that should the object be deleted (say, from an alternative
176  * link-map), the functions can be removed.
177  */
178 typedef struct {
179 	Rt_map	*lc_lmp;			/* function provider */
180 	union {
181 		int		(*lc_func)();	/* external function pointer */
182 		uintptr_t	lc_val;		/* external value */
183 		char    	*lc_ptr;	/* external character pointer */
184 	} lc_un;
185 } Lc_desc;
186 
187 /*
188  * Link map list definition.  Link-maps are used to describe each loaded object.
189  * Lists of these link-maps describe the various namespaces within a process.
190  * The process executable and its dependencies are maintained on the lml_main
191  * list.  The runtime linker, and its dependencies are maintained on the
192  * lml_rtld list.  Additional lists can be created (see dlmopen()) for such
193  * things as auditors and their dependencies.
194  *
195  * Each link-map list maintains an Alist of one, or more, linked lists of
196  * link-maps.  For backward compatibility, the lm_head/lm_tail elements are
197  * initialized to the first linked-list of link-maps:
198  *
199  *      Lm_list
200  *    ----------
201  *   | lm_tail  | ------------------------------------
202  *   | lm_head  | --------------------                |
203  *   |          |                     |     Rt_map    |     Rt_map
204  *   |          |                     |     ------    |     ------
205  *   |          |          Alist       --> |      |   |--> |      |
206  *   |          |        ---------    |    |      | --     |      |
207  *   | lm_lists | ----> |         |   |    |      |    --> |      |
208  *   |          |       |---------|   |    |      |   |    |      |
209  *   |          |       | lc_head | --      ------    |     ------
210  *   |          |       | lc_tail | ------------------
211  *   |          |       |---------|
212  *    ----------        | lc_head |
213  *                      | lc_tail |
214  *                      |---------|
215  *
216  * Multiple link-map lists exist to support the addition of lazy loaded
217  * families, filtee families, and dlopen() families.  The intent of these
218  * lists is to insure that a family of objects that are to be loaded are
219  * fully relocatable, and hence usable, before they become part of the main
220  * (al_data[0]) link-map control list.  This main link-map control list is
221  * the only list in existence when control is transferred to user code.
222  *
223  * During process initialization, the dynamic executable and its non-lazy
224  * dependencies are maintained on al_data[0].  If a new object is loaded, then
225  * this object is added to the next available control list [1], typically
226  * al_data[1].  Any dependencies of this object that have not already been
227  * loaded are added to the same control list.  Once all of the objects on the
228  * new control list have been successfully relocated, the objects are moved from
229  * the new control list to the highest control list to which objects of the new
230  * control list bound to, typically al_data[1] to al_data[0].
231  *
232  * Each loading scenario can be broken down as follows:
233  *
234  *  setup() - only the initial link-map control list is used:
235  *   i.	  create al_data[0]
236  *   ii.  add new link-map for main on al_data[0]
237  *   iii. analyze al_data[0] to add all non-lazy dependencies
238  *   iv.  relocate al_data[0] dependencies.
239  *
240  *  dlopen() - the initiator can only be the initial link-map control list:
241  *   i.   create al_data[1] from caller al_data[0]
242  *   ii.  add new link-map for the dlopen'ed object on al_data[1]
243  *   iii. analyze al_data[1] to add all non-lazy dependencies
244  *   iv.  relocate al_data[1] dependencies, and move to al_data[0].
245  *
246  *  filtee and lazy loading processing - the initiator can be any link-map
247  *  control list that is being relocated:
248  *   i.   create al_data[y] from caller al_data[x]
249  *   ii.  add new link-map for the new object on al_data[y]
250  *   iii. analyze al_data[y] to add all non-lazy dependencies
251  *   iv.  relocate al_data[y] dependencies, and move to al_data[x].
252  *
253  * This Alist therefore maintains a stack of link-map control lists.  The newest
254  * link-map control list can locate symbols within any of the former lists,
255  * however, control is not passed to a former list until the newest lists
256  * processing is complete.  Thus, objects can't bind to new objects until they
257  * have been fully analyzed and relocated.
258  *
259  * [1]  Note, additional link-map control list creation occurs after the head
260  * link-map object (typically the dynamic executable) has been relocated.  This
261  * staging is required to satisfy the binding requirements of copy relocations.
262  * Copy relocations, effectively, transfer the bindings of the copied data
263  * (say _iob in libc.so.1) to the copy location (_iob in the application).
264  * Thus an object that might bind to the original copy data must be redirected
265  * to the copy reference.  As the knowledge of a copy relocation having taken
266  * place is only known after relocating the application, link-map control list
267  * additions are suspended until after this relocation has completed.
268  */
269 typedef struct {
270 	Rt_map		*lc_head;
271 	Rt_map		*lc_tail;
272 	APlist		*lc_now;	/* pending promoted bind-now objects */
273 	uint_t		lc_flags;
274 } Lm_cntl;
275 
276 #define	LMC_FLG_ANALYZING	0x01	/* control list is being analyzed */
277 #define	LMC_FLG_RELOCATING	0x02	/* control list is being relocated */
278 #define	LMC_FLG_REANALYZE	0x04	/* repeat analysis (established when */
279 					/*	interposers are added */
280 
281 struct lm_list {
282 	/*
283 	 * BEGIN: Exposed to rtld_db - don't move, don't delete
284 	 */
285 	Rt_map		*lm_head;	/* linked list pointers to active */
286 	Rt_map		*lm_tail;	/*	link-map list */
287 	APlist		*lm_handle;	/* not used by rtld_db - but spacing */
288 					/*	is required for flags */
289 	Word		lm_flags;
290 	/*
291 	 * END: Exposed to rtld_db - don't move, don't delete
292 	 */
293 	Alist		*lm_rti;	/* list of RTLDINFO tables */
294 	Audit_list	*lm_alp;	/* audit list descriptor */
295 	avl_tree_t	*lm_fpavl;	/* avl tree of objects loaded */
296 	Alist		*lm_lists;	/* active and pending link-map lists */
297 	char		***lm_environ;	/* pointer to environment array */
298 	Word		lm_tflags;	/* transferable flags */
299 	uint_t		lm_obj;		/* total number of objs on link-map */
300 	uint_t		lm_init;	/* new obj since last init processing */
301 	uint_t		lm_lazy;	/* number of objects with pending */
302 					/*	lazy dependencies */
303 	uint_t		lm_tls;		/* new obj that require TLS */
304 	uint_t		lm_lmid;	/* unique link-map list identifier, */
305 	char		*lm_lmidstr;	/* and associated diagnostic string */
306 	APlist		*lm_actaudit;	/* list of pending audit activity */
307 	Lc_desc		lm_lcs[CI_MAX];	/* external libc functions */
308 };
309 
310 #ifdef	_SYSCALL32
311 struct lm_list32 {
312 	/*
313 	 * BEGIN: Exposed to rtld_db - don't move, don't delete
314 	 */
315 	Elf32_Addr	lm_head;
316 	Elf32_Addr	lm_tail;
317 	Elf32_Addr	lm_handle;
318 	Elf32_Word	lm_flags;
319 	/*
320 	 * END: Exposed to rtld_db - don't move, don't delete
321 	 */
322 	Elf32_Addr	lm_rti;
323 	Elf32_Addr	lm_fpavl;
324 	Elf32_Addr	lm_lists;
325 	Elf32_Addr	lm_environ;
326 	Elf32_Word	lm_tflags;
327 	uint_t		lm_obj;
328 	uint_t		lm_init;
329 	uint_t		lm_lazy;
330 	uint_t		lm_tls;
331 	uint_t		lm_lmid;
332 	Elf32_Addr	lm_lmidstr;
333 	Elf32_Addr	lm_actaudit;
334 	Elf32_Addr	lm_lcs[CI_MAX];
335 };
336 #endif /* _SYSCALL32 */
337 
338 /*
339  * Possible Link_map list flags (Lm_list.lm_flags)
340  */
341 /*
342  * BEGIN: Exposed to rtld_db - don't move, don't delete
343  */
344 #define	LML_FLG_BASELM		0x00000001	/* primary link-map */
345 #define	LML_FLG_RTLDLM		0x00000002	/* rtld link-map */
346 /*
347  * END: Exposed to rtld_db - don't move, don't delete
348  */
349 #define	LML_FLG_NOAUDIT		0x00000004	/* symbol auditing disabled */
350 #define	LML_FLG_PLTREL		0x00000008	/* deferred plt relocation */
351 						/* 	initialization */
352 						/*	(ld.so.1 only) */
353 #define	LML_FLG_HOLDLOCK	0x00000010	/* hold the rtld mutex lock */
354 #define	LML_FLG_ENVIRON		0x00000020	/* environ var initialized */
355 #define	LML_FLG_INTRPOSE	0x00000040	/* interposing objs on list */
356 #define	LML_FLG_LOCAUDIT	0x00000080	/* local auditors exists for */
357 						/*	this link-map list */
358 #define	LML_FLG_LOADAVAIL	0x00000100	/* load anything available */
359 #define	LML_FLG_IGNRELERR	0x00000200	/* ignore relocation errors - */
360 						/*	internal for crle(1) */
361 #define	LML_FLG_DBNOTIF		0x00000400	/* binding activity going on */
362 #define	LML_FLG_STARTREL	0x00000800	/* relocation started */
363 #define	LML_FLG_ATEXIT		0x00001000	/* atexit processing */
364 #define	LML_FLG_OBJADDED	0x00002000	/* object(s) added */
365 #define	LML_FLG_OBJDELETED	0x00004000	/* object(s) deleted */
366 #define	LML_FLG_OBJREEVAL	0x00008000	/* existing object(s) needs */
367 						/*	tsort reevaluation */
368 #define	LML_FLG_INTRPOSETSORT	0x00020000	/* interpose tsorting done */
369 #define	LML_FLG_AUDITNOTIFY	0x00040000	/* audit consistent required */
370 #define	LML_FLG_GROUPSEXIST	0x00080000	/* local groups exist */
371 
372 #define	LML_FLG_TRC_LDDSTUB	0x00100000	/* identify lddstub */
373 #define	LML_FLG_TRC_ENABLE	0x00200000	/* tracing enabled (ldd) */
374 #define	LML_FLG_TRC_WARN	0x00400000	/* print warnings for undefs */
375 #define	LML_FLG_TRC_VERBOSE	0x00800000	/* verbose (versioning) trace */
376 #define	LML_FLG_TRC_SEARCH	0x01000000	/* trace search paths */
377 #define	LML_FLG_TRC_UNREF	0x02000000	/* trace unreferenced */
378 						/*	dependencies */
379 #define	LML_FLG_TRC_UNUSED	0x04000000	/* trace unused dependencies */
380 #define	LML_FLG_TRC_INIT	0x08000000	/* print .init order */
381 #define	LML_FLG_TRC_NOUNRESWEAK	0x10000000	/* unresolved weak references */
382 						/*	are not allowed */
383 #define	LML_FLG_TRC_NOPAREXT	0x20000000	/* unresolved PARENT/EXTERN */
384 						/*	references are not */
385 						/*	allowed */
386 #define	LML_MSK_TRC		0xfff00000	/* tracing mask */
387 
388 /*
389  * Possible Link_map transferable flags (Lm_list.lm_tflags), i.e., link-map
390  * list flags that can be propagated to any new link-map list created.
391  */
392 #define	LML_TFLG_NOLAZYLD	0x00000001	/* lazy loading disabled */
393 #define	LML_TFLG_NODIRECT	0x00000002	/* direct bindings disabled */
394 
395 #define	LML_TFLG_LOADFLTR	0x00000008	/* trigger filtee loading */
396 
397 #define	LML_TFLG_AUD_PREINIT	0x00001000	/* preinit (audit) exists */
398 #define	LML_TFLG_AUD_OBJSEARCH	0x00002000	/* objsearch (audit) exists */
399 #define	LML_TFLG_AUD_OBJOPEN	0x00004000	/* objopen (audit) exists */
400 #define	LML_TFLG_AUD_OBJFILTER	0x00008000	/* objfilter (audit) exists */
401 #define	LML_TFLG_AUD_OBJCLOSE	0x00010000	/* objclose (audit) exists */
402 #define	LML_TFLG_AUD_SYMBIND	0x00020000	/* symbind (audit) exists */
403 #define	LML_TFLG_AUD_PLTENTER	0x00040000	/* pltenter (audit) exists */
404 #define	LML_TFLG_AUD_PLTEXIT	0x00080000	/* pltexit (audit) exists */
405 #define	LML_TFLG_AUD_ACTIVITY	0x00100000	/* activity (audit) exists */
406 
407 /*
408  * NOTE: Each auditing module establishes a set of audit flags, AFLAGS(), that
409  * define the auditing interfaces the module offers.  These auditing flags are
410  * the LML_TFLG_AUD_ flags defined above.  Global auditors result in setting
411  * the lm_tflags too.  Local auditors only use the AFLAGS().  All tests for
412  * auditing inspect the lm_tflags and AFLAGS() for a specific auditing
413  * interface, and thus use the same flag to test for both types of auditors.
414  */
415 #define	LML_TFLG_AUD_MASK	0x0ffff000	/* audit interfaces mask */
416 
417 /*
418  * Define a Group Handle.
419  *
420  * The capability of ld.so.1 to associate a group of objects, look for symbols
421  * within that group, ensure that groups are isolated from one another (with
422  * regard to relocations), and to unload a group, centers around a handle.
423  *
424  * Dependencies can be added to an existing handle as the dependencies are
425  * lazily loaded.  The core dependencies on the handle are the ldd(1) list of
426  * the referenced object.
427  *
428  * Handles can be created from:
429  *
430  *  -	a dlopen() request.  This associates a caller to a reference object,
431  * 	and the referenced objects dependencies.  This group of objects can
432  *	then be inspected for symbols (dlsym()).
433  *  -	a filtering request.  This associates a filter (caller) to a referenced
434  *	object (filtee).  The redirection of filter symbols to their filtee
435  *	counterpart is essentially a dlsym() using the filtee's handle.
436  *
437  * The handle created for these events is referred to as a public handle.  This
438  * handle tracks the referenced object, all of the dependencies of the
439  * referenced object, and the caller (parent).
440  *
441  * Presently, an object may have two handles, one requested with RTLD_FIRST
442  * and one without.
443  *
444  * A handle may be referenced by any number of callers (parents).  A reference
445  * count tracks the number.  A dlclose() operation drops the reference count,
446  * and when the count is zero, the handle is used to determine the family of
447  * objects to unload.  As bindings may occur to objects on the handle from
448  * other handles, it may not be possible to remove a complete family of objects
449  * or the handle itself.  Handles in this state are moved to an orphan list.
450  * A handle on the orphan list is taken off the orphan list if the associated
451  * object is reopened.  Otherwise, the handle remains on the orphan list for
452  * the duration of the process.  The orphan list is inspected any time objects
453  * are unloaded, to determine if the orphaned objects can also be unloaded.
454  *
455  * Handles can also be created for internal uses:
456  *
457  *  -	to promote objects to RTLD_NOW.
458  *  -	to establish families for symbol binding fallback, required when lazy
459  *	loadable objects are still pending.
460  *
461  * The handle created for these events is referred to as a private handle.  This
462  * handle does not need to track the caller (parent), and because of this, does
463  * not need to be considered during dlclose() operations, as the handle can not
464  * be referenced by callers outside of the referenced objects family.
465  *
466  * Note, a private handle is essentially a subset of a public handle.  Should
467  * an internal operation require a private handle, and a public handle already
468  * exist, the public handle can be used.  Should an external operation require
469  * a public handle, and a private handle exist, the private handle is promoted
470  * to a public handle.  Any handle that gets created will remain in existence
471  * for the life time of the referenced object.
472  *
473  * Objects can be dlopened using RTLD_NOW.  This attribute requires that all
474  * relocations of the object, and its dependencies are processed immediately,
475  * before return to the caller.  Typically, an object is loaded without
476  * RTLD_NOW, and procedure linkage relocations are satisfied when their
477  * associated function is first called.  If an object is already loaded, and an
478  * RTLD_NOW request is made, then the object, and its dependencies, most undergo
479  * additional relocation processing.   This promotion from lazy binding to
480  * immediate binding is carried out using handles, as the handle defines the
481  * dependencies that must be processed.
482  *
483  * To ensure that objects within a lazy loadable environment can be relocated,
484  * no matter whether the objects have their dependencies described completely,
485  * a symbol lookup fallback is employed.  Any pending lazy loadable objects are
486  * loaded, and a handle established to search the object and it's dependencies
487  * for the required symbol.
488  *
489  * A group handle (and its associated group descriptors), is referenced from
490  * a link-map's HANDLES and GROUPS lists.  Note, Aplist's are diagramed to
491  * fully expose the allocations required to establish the data structure
492  * relationships.
493  *
494  *                                  Grp_desc
495  *                                   Alist
496  *                                 -----------
497  *                            --> |           |
498  *                           |    |-----------|
499  *                           |    | gd_depend | ---------
500  *                           |    |           |          |
501  *                           |    |-----------|          |
502  *                   --------|--- | gd_depend |          |
503  *                  |        |    | (parent)  |          |
504  *                  |        |    |-----------|          |
505  *                  |        |    | gd_depend |          |
506  *                  |        |    |           |          |
507  *                  |        |    |           |          |
508  *                  |        |     -----------           |
509  *                  |        |                           |
510  *                  |        |      Grp_hdl              |
511  *                  |        |    -----------            |
512  *                  |         -- | gh_depends |          |
513  *                  |  --------- | gh_ownlmp  |          |
514  *                  | |          |            |          |
515  *                  | |          |            |          |
516  *                  | |          |            |          |
517  *      Rt_map      | |           ------------           |       Rt_map
518  *    ----------    | |               ^ ^                |     ----------
519  *   |          | <-  |               | |                 --> |          |
520  *   |          | <---   --------     | |                     |          |
521  *   | HANDLES  | ----> |        |    | |     --------        |          |
522  *   |          |       |        |    | |    |        | <---- |  GROUPS  |
523  *   |          |       |        | ---  |    |        |       |          |
524  *   |          |       |        |       --- |        |       |          |
525  *   |          |        --------            |        |       |          |
526  *    ----------          Aplist              --------         ----------
527  *                                             Aplist
528  */
529 typedef struct {
530 	Alist		*gh_depends;	/* handle dependency list */
531 	Rt_map		*gh_ownlmp;	/* handle owners link-map */
532 	Lm_list		*gh_ownlml;	/* handle owners link-map list */
533 	uint_t		gh_refcnt;	/* handle reference count */
534 	uint_t		gh_flags;	/* handle flags (GPH_ values) */
535 } Grp_hdl;
536 
537 /*
538  * Define the two categories of handle.
539  */
540 #define	GPH_PUBLIC	0x0001		/* handle returned to caller(s) */
541 #define	GPH_PRIVATE	0x0002		/* handle used internally */
542 
543 /*
544  * Define any flags that affects how the handle is used.
545  */
546 #define	GPH_ZERO	0x0010		/* special handle for dlopen(0) */
547 #define	GPH_LDSO	0x0020		/* special handle for ld.so.1 */
548 #define	GPH_FIRST	0x0040		/* dlsym() can only use originating */
549 					/*	dependency */
550 #define	GPH_FILTEE	0x0080		/* handle identifies a filtee, used */
551 					/*	for diagnostics only */
552 /*
553  * Define any state that is associated with the handle.
554  */
555 #define	GPH_INITIAL	0x0100		/* handle is initialized */
556 
557 /*
558  * Define a Group Descriptor.
559  *
560  * Each dependency associated with a group handle is maintained by a group
561  * descriptor.  The descriptor defines the associated dependency together with
562  * flags that indicate how the dependency can be used.
563  */
564 typedef struct {
565 	Rt_map		*gd_depend;	/* dependency */
566 	uint_t		gd_flags;	/* dependency flags (GPD_ values) */
567 } Grp_desc;
568 
569 #define	GPD_DLSYM	0x0001		/* dependency available to dlsym() */
570 #define	GPD_RELOC	0x0002		/* dependency available to satisfy */
571 					/*	relocation binding */
572 #define	GPD_ADDEPS	0x0004		/* dependencies of this dependency */
573 					/*	should be added to handle */
574 #define	GPD_PARENT	0x0008		/* dependency is a parent */
575 #define	GPD_FILTER	0x0010		/* dependency is our filter */
576 #define	GPD_REMOVE	0x0100		/* descriptor is a candidate for */
577 					/*	removal from the group */
578 
579 /*
580  * Define threading structures.  For compatibility with libthread (T1_VERSION 1
581  * and TI_VERSION 2) our locking structure is sufficient to hold a mutex or a
582  * readers/writers lock.
583  */
584 typedef struct {
585 	union {
586 		mutex_t		l_mutex;
587 		rwlock_t	l_rwlock;
588 	} u;
589 } Rt_lock;
590 
591 typedef	cond_t	Rt_cond;
592 
593 /*
594  * Define a dynamic section information descriptor.  This parallels the entries
595  * in the .dynamic section and holds auxiliary information to implement lazy
596  * loading and filtee processing.
597  */
598 typedef struct {
599 	uint_t		di_flags;
600 	void		*di_info;
601 	const char	*di_name;
602 } Dyninfo;
603 
604 #define	FLG_DI_STDFLTR	0x00001		/* .dynamic entry for DT_FILTER */
605 #define	FLG_DI_AUXFLTR	0x00002		/* .dynamic entry for DT_AUXILIARY */
606 #define	FLG_DI_SYMFLTR	0x00004		/* .dynamic entry for DT_SYMFILTER */
607 					/*	and DT_SYMAUXILIARY */
608 #define	MSK_DI_FILTER	0x0000f		/* mask for all filter possibilities */
609 
610 #define	FLG_DI_POSFLAG1	0x00010		/* .dynamic entry for DT_POSFLAG_1 */
611 #define	FLG_DI_NEEDED	0x00020		/* .dynamic entry for DT_NEEDED */
612 #define	FLG_DI_REGISTER	0x00040		/* .dynamic entry for DT_REGISTER */
613 #define	FLG_DI_IGNORE	0x00080		/* .dynamic entry should be ignored */
614 
615 #define	FLG_DI_LAZY	0x00100		/* lazy needed entry, preceded by */
616 					/*    DF_P1_LAZYLOAD (DT_POSFLAG_1) */
617 #define	FLG_DI_GROUP	0x00200		/* group needed entry, preceded by */
618 					/*    DF_P1_GROUPPERM (DT_POSFLAG_1) */
619 #define	FLG_DI_DEFERRED	0x00400		/* deferred needed entry, preceded by */
620 					/*    DF_P1_DEFERRED (DT_POSFLAG_1) */
621 
622 #define	FLG_DI_LAZYFAIL	0x01000		/* the lazy loading of this entry */
623 					/*    failed */
624 #define	FLG_DI_LDD_DONE	0x02000		/* entry has been processed (ldd) */
625 #define	FLG_DI_DEF_DONE	0x04000		/* entry has been processed (dlinfo) */
626 
627 /*
628  * Data structure to track AVL tree of pathnames.  This structure provides the
629  * basis of both the "not-found" node tree, and the "full-path" node tree.  Both
630  * of these trees persist for the life of a process, although the "not-found"
631  * tree may be moved aside during a dlopen() or dlsym() fall back operation.
632  */
633 typedef struct {
634 	const char	*pn_name;	/* path name */
635 	avl_node_t	pn_avl;		/* avl book-keeping (see SGSOFFSETOF) */
636 	uint_t		pn_hash;	/* path name hash value */
637 } PathNode;
638 
639 /*
640  * Data structure to track AVL tree for full path names of objects that are
641  * loaded into memory.
642  */
643 typedef struct {
644 	PathNode	fpn_node;	/* path node */
645 	Rt_map		*fpn_lmp;	/* object link-map */
646 } FullPathNode;
647 
648 /*
649  * A given link-map can hold either a supplier or receiver copy
650  * relocation list, but not both. This union is used to overlap
651  * the space used for the two lists.
652  */
653 typedef union {
654 	Alist	*rtc_r;		/* receiver list (Rel_copy) */
655 	APlist	*rtc_s;		/* supplier list (Rt_map *) */
656 } Rt_map_copy;
657 
658 
659 /*
660  * Link-map definition.
661  */
662 struct rt_map {
663 	/*
664 	 * BEGIN: Exposed to rtld_db - don't move, don't delete
665 	 */
666 	Link_map	rt_public;	/* public data */
667 	const char	*rt_pathname;	/* full pathname of loaded object */
668 	ulong_t		rt_padstart;	/* start of image (including padding) */
669 	ulong_t		rt_padimlen;	/* size of image (including padding */
670 	ulong_t		rt_msize;	/* total memory reservation range */
671 	uint_t		rt_flags;	/* state flags, see FLG below */
672 	uint_t		rt_flags1;	/* state flags1, see FL1 below */
673 	ulong_t		rt_tlsmodid;	/* TLS module id */
674 	/*
675 	 * END: Exposed to rtld_db - don't move, don't delete
676 	 */
677 	APlist		*rt_alias;	/* list of linked file names */
678 	APlist		*rt_fpnode;	/* list of FullPathNode AVL nodes */
679 	char		*rt_runpath;	/* LD_RUN_PATH and its equivalent */
680 	Alist		*rt_runlist;	/*	Pdesc structures */
681 	APlist		*rt_depends;	/* list of dependencies */
682 	APlist		*rt_callers;	/* list of callers */
683 	APlist		*rt_handles;	/* dlopen handles */
684 	APlist		*rt_groups;	/* groups we're a member of */
685 	struct fct	*rt_fct;	/* file class table for this object */
686 	void		*rt_priv;	/* private data, object type specific */
687 	Lm_list		*rt_list;	/* link map list we belong to */
688 	uint_t		rt_objfltrndx;	/* object filtees .dynamic index */
689 	uint_t		rt_symsfltrcnt;	/* number of standard symbol filtees */
690 	uint_t		rt_symafltrcnt;	/* number of auxiliary symbol filtees */
691 	int		rt_mode;	/* usage mode, see RTLD mode flags */
692 	int		rt_sortval;	/* temporary buffer to traverse graph */
693 	uint_t		rt_cycgroup;	/* cyclic group */
694 	dev_t		rt_stdev;	/* device id and inode number for .so */
695 	rtld_ino_t	rt_stino;	/*	multiple inclusion checks */
696 	const char	*rt_origname;	/* original pathname of loaded object */
697 	size_t		rt_dirsz;	/*	and its size */
698 	size_t		rt_lmsize;	/* size of the link-map allocation */
699 	Rt_map_copy	rt_copy;	/* list of copy relocations */
700 	Audit_desc	*rt_auditors;	/* audit descriptor array */
701 	Audit_info	*rt_audinfo;	/* audit information descriptor */
702 	Syminfo		*rt_syminfo;	/* elf .syminfo section - here */
703 					/*	because it is checked in */
704 					/*	common code */
705 	Addr		*rt_initarray;	/* .initarray table */
706 	Addr		*rt_finiarray;	/* .finiarray table */
707 	Addr		*rt_preinitarray; /* .preinitarray table */
708 	mmapobj_result_t *rt_mmaps;	/* array of mapping information */
709 	uint_t		rt_mmapcnt;	/*	and associated number */
710 	uint_t		rt_initarraysz;	/* size of .initarray table */
711 	uint_t		rt_finiarraysz;	/* size of .finiarray table */
712 	uint_t		rt_preinitarraysz; /* size of .preinitarray table */
713 	Dyninfo		*rt_dyninfo;	/* .dynamic information descriptors */
714 	uint_t		rt_dyninfocnt;	/* count of dyninfo entries */
715 	uint_t		rt_relacount;	/* no. of RELATIVE relocations */
716 	uint_t		rt_idx;		/* hold index within linkmap list */
717 	uint_t		rt_lazy;	/* number of lazy dependencies */
718 					/*	pending */
719 	Cap		*rt_cap;	/* capabilities data */
720 	Capchain	*rt_capchain;	/* capabilities chain data */
721 	uint_t		rt_cntl;	/* link-map control list we belong to */
722 	uint_t		rt_aflags;	/* auditor flags, see LML_TFLG_AUD_ */
723 					/* address of _init */
724 	void		(*rt_init)(void);
725 					/* address of _fini */
726 	void		(*rt_fini)(void);
727 					/* link map symbol interpreter */
728 	int		(*rt_symintp)(Slookup *, Sresult *, uint_t *, int *);
729 };
730 
731 #ifdef _SYSCALL32
732 /*
733  * Structure to allow 64-bit rtld_db to read 32-bit processes out of procfs.
734  */
735 typedef union {
736 	uint32_t	rtc_r;
737 	uint32_t	rtc_s;
738 } Rt_map_copy32;
739 
740 typedef struct rt_map32 {
741 	/*
742 	 * BEGIN: Exposed to rtld_db - don't move, don't delete
743 	 */
744 	Link_map32	rt_public;
745 	uint32_t	rt_pathname;
746 	uint32_t	rt_padstart;
747 	uint32_t	rt_padimlen;
748 	uint32_t	rt_msize;
749 	uint32_t	rt_flags;
750 	uint32_t	rt_flags1;
751 	uint32_t	rt_tlsmodid;
752 	/*
753 	 * END: Exposed to rtld_db - don't move, don't delete
754 	 */
755 	uint32_t	rt_alias;
756 	uint32_t	rt_fpnode;
757 	uint32_t	rt_runpath;
758 	uint32_t	rt_runlist;
759 	uint32_t	rt_depends;
760 	uint32_t	rt_callers;
761 	uint32_t	rt_handles;
762 	uint32_t	rt_groups;
763 	uint32_t	rt_fct;
764 	uint32_t	rt_priv;
765 	uint32_t 	rt_list;
766 	uint32_t 	rt_objfltrndx;
767 	uint32_t 	rt_symsfltrcnt;
768 	uint32_t 	rt_symafltrcnt;
769 	int32_t		rt_mode;
770 	int32_t		rt_sortval;
771 	uint32_t	rt_cycgroup;
772 	uint32_t	rt_stdev;
773 	uint32_t	rt_stino;
774 	uint32_t	rt_origname;
775 	uint32_t	rt_dirsz;
776 	Rt_map_copy32	rt_copy;
777 	uint32_t 	rt_auditors;
778 	uint32_t 	rt_audinfo;
779 	uint32_t	rt_syminfo;
780 	uint32_t	rt_initarray;
781 	uint32_t	rt_finiarray;
782 	uint32_t	rt_preinitarray;
783 	uint32_t	rt_mmaps;
784 	uint32_t	rt_mmapcnt;
785 	uint32_t	rt_initarraysz;
786 	uint32_t	rt_finiarraysz;
787 	uint32_t	rt_preinitarraysz;
788 	uint32_t 	rt_dyninfo;
789 	uint32_t 	rt_dyninfocnt;
790 	uint32_t	rt_relacount;
791 	uint32_t	rt_idx;
792 	uint32_t	rt_lazy;
793 	uint32_t	rt_cap;
794 	uint32_t	rt_capchain;
795 	uint32_t	rt_cntl;
796 	uint32_t	rt_aflags;
797 	uint32_t 	rt_init;
798 	uint32_t	rt_fini;
799 	uint32_t	rt_symintp;
800 } Rt_map32;
801 
802 #endif	/* _SYSCALL32 */
803 
804 /*
805  * Link map state flags.
806  */
807 /*
808  * BEGIN: Exposed to rtld_db - don't move, don't delete
809  */
810 #define	FLG_RT_ISMAIN	0x00000001	/* object represents main executable */
811 #define	FLG_RT_IMGALLOC	0x00000002	/* image is allocated (not mmap'ed) */
812 	/*
813 	 * Available for r_debug version >= R_RTLDDB_VERSION5
814 	 */
815 #define	FLG_RT_RELOCED	0x00000004	/* object has been relocated */
816 /*
817  * END: Exposed to rtld_db - don't move, don't delete
818  */
819 #define	FLG_RT_SETGROUP	0x00000008	/* group establishment required */
820 #define	FLG_RT_CAP	0x00000010	/* process $CAPABILITY expansion */
821 #define	FLG_RT_OBJECT	0x00000020	/* object processing (ie. .o's) */
822 #define	FLG_RT_NEWLOAD	0x00000040	/* object is newly loaded */
823 #define	FLG_RT_NODUMP	0x00000080	/* object can't be dldump(3x)'ed */
824 #define	FLG_RT_DELETE	0x00000100	/* object can be deleted */
825 #define	FLG_RT_ANALYZED	0x00000200	/* object has been analyzed */
826 #define	FLG_RT_INITDONE	0x00000400	/* objects .init has been completed */
827 #define	FLG_RT_TRANS	0x00000800	/* object is acting as a translator */
828 #define	FLG_RT_FIXED	0x00001000	/* image location is fixed */
829 #define	FLG_RT_PRELOAD	0x00002000	/* object was preloaded */
830 #define	FLG_RT_ALTER	0x00004000	/* alternative object used */
831 #define	FLG_RT_LOADFLTR	0x00008000	/* trigger filtee loading */
832 #define	FLG_RT_AUDIT	0x00010000	/* object is an auditor */
833 #define	FLG_RT_MODESET	0x00020000	/* MODE() has been initialized */
834 #define	FLG_RT_ANALZING	0x00040000	/* object is being analyzed */
835 #define	FLG_RT_INITFRST 0x00080000	/* execute .init first */
836 #define	FLG_RT_NOOPEN	0x00100000	/* dlopen() not allowed */
837 #define	FLG_RT_FINICLCT	0x00200000	/* fini has been collected (tsort) */
838 #define	FLG_RT_INITCALL	0x00400000	/* objects .init has been called */
839 #define	FLG_RT_OBJINTPO	0x00800000	/* object is a global interposer */
840 #define	FLG_RT_SYMINTPO	0x01000000	/* object contains symbol interposer */
841 #define	MSK_RT_INTPOSE	0x01800000	/* mask for all interposer */
842 					/*	possibilities */
843 #define	FLG_RT_MOVE	0x02000000	/* object needs move operation */
844 #define	FLG_RT_RELOCING	0x04000000	/* object is being relocated */
845 #define	FLG_RT_REGSYMS	0x08000000	/* object has DT_REGISTER entries */
846 #define	FLG_RT_INITCLCT	0x10000000	/* init has been collected (tsort) */
847 #define	FLG_RT_PUBHDL	0x20000000	/* generate a handle for this object */
848 #define	FLG_RT_PRIHDL	0x40000000	/*	either public or private */
849 
850 #define	FL1_RT_COPYTOOK	0x00000001	/* copy relocation taken */
851 #define	FL1_RT_ALTCHECK	0x00000002	/* alternative system capabilities */
852 					/*	checked */
853 #define	FL1_RT_ALTCAP	0x00000004	/* alternative system capabilities */
854 					/*	should be used */
855 #define	FL1_RT_CONFSET	0x00000008	/* object was loaded by crle(1) */
856 #define	FL1_RT_NODEFLIB	0x00000010	/* ignore default library search */
857 #define	FL1_RT_ENDFILTE	0x00000020	/* filtee terminates filters search */
858 #define	FL1_RT_DISPREL	0x00000040	/* object has *disp* relocation */
859 #define	FL1_RT_DTFLAGS	0x00000080	/* DT_FLAGS element exists */
860 #define	FL1_RT_LDDSTUB	0x00000100	/* identify lddstub */
861 #define	FL1_RT_NOINIFIN	0x00000200	/* no .init or .fini exists */
862 #define	FL1_RT_USED	0x00000400	/* symbol referenced from this object */
863 #define	FL1_RT_SYMBOLIC	0x00000800	/* DF_SYMBOLIC was set - use */
864 					/*	symbolic sym resolution */
865 #define	FL1_RT_OBJSFLTR	0x00001000	/* object is acting as a standard */
866 #define	FL1_RT_OBJAFLTR	0x00002000	/*	or auxiliary filter */
867 #define	FL1_RT_SYMSFLTR	0x00004000	/* symbol is acting as a standard */
868 #define	FL1_RT_SYMAFLTR	0x00008000	/*	or auxiliary filter */
869 #define	MSK_RT_FILTER	0x0000f000	/* mask for all filter possibilities */
870 
871 #define	FL1_RT_TLSADD	0x00010000	/* objects TLS has been registered */
872 #define	FL1_RT_TLSSTAT	0x00020000	/* object requires static TLS */
873 #define	FL1_RT_DIRECT	0x00040000	/* object has DIRECT bindings enabled */
874 #define	FL1_RT_GLOBAUD	0x00080000	/* establish global auditing */
875 
876 /*
877  * Flags for the tls_modactivity() routine
878  */
879 #define	TM_FLG_MODADD	0x01		/* call tls_modadd() interface */
880 #define	TM_FLG_MODREM	0x02		/* call tls_modrem() interface */
881 
882 /*
883  * Macros for getting to exposed, link_map data (R_RTLDDB_VERSION <= 2).
884  */
885 #define	ADDR(X)		((X)->rt_public.l_addr)
886 #define	NAME(X)		((X)->rt_public.l_name)
887 #define	DYN(X)		((X)->rt_public.l_ld)
888 #define	NEXT(X)		((X)->rt_public.l_next)
889 #define	PREV(X)		((X)->rt_public.l_prev)
890 #define	REFNAME(X)	((X)->rt_public.l_refname)
891 
892 /*
893  * An Rt_map starts with a Link_map, followed by other information.
894  * ld.so.1 allocates Rt_map structures, and then casts them to Link_map,
895  * and back, depending on context.
896  *
897  * On some platforms, Rt_map can have a higher alignment requirement
898  * than Link_map. On such platforms, the cast from Link_map to Rt_map will
899  * draw an E_BAD_PTR_CAST_ALIGN warning from lint. Since we allocate
900  * the memory as the higher alignment Rt_map, we know that this is a safe
901  * conversion. The LINKMAP_TO_RTMAP macro is used to handle the conversion
902  * in a manner that satisfies lint.
903  */
904 #ifdef lint
905 #define	LINKMAP_TO_RTMAP(X)	(Rt_map *)(void *)(X)
906 #else
907 #define	LINKMAP_TO_RTMAP(X)	(Rt_map *)(X)
908 #endif
909 
910 /*
911  * Convenience macros for the common case of using
912  * NEXT()/PREV() and casting the result to (Rt_map *)
913  */
914 #define	NEXT_RT_MAP(X)	LINKMAP_TO_RTMAP(NEXT(X))
915 #define	PREV_RT_MAP(X)	LINKMAP_TO_RTMAP(PREV(X))
916 
917 /*
918  * Macros for getting to exposed, link_map data (R_RTLDDB_VERSION3).
919  */
920 #define	PATHNAME(X)	((X)->rt_pathname)
921 #define	PADSTART(X)	((X)->rt_padstart)
922 #define	PADIMLEN(X)	((X)->rt_padimlen)
923 #define	MSIZE(X)	((X)->rt_msize)
924 #define	FLAGS(X)	((X)->rt_flags)
925 #define	FLAGS1(X)	((X)->rt_flags1)
926 
927 /*
928  * Macros for getting to exposed, link_map data (R_RTLDDB_VERSION4).
929  */
930 #define	TLSMODID(X)	((X)->rt_tlsmodid)
931 
932 /*
933  * Macros for getting to unexposed, link-map data.
934  */
935 #define	LMSIZE(X)	((X)->rt_lmsize)
936 #define	AFLAGS(X)	((X)->rt_aflags)
937 #define	ALIAS(X)	((X)->rt_alias)
938 #define	FPNODE(X)	((X)->rt_fpnode)
939 #define	INIT(X)		((X)->rt_init)
940 #define	FINI(X)		((X)->rt_fini)
941 #define	RPATH(X)	((X)->rt_runpath)
942 #define	RLIST(X)	((X)->rt_runlist)
943 #define	DEPENDS(X)	((X)->rt_depends)
944 #define	CALLERS(X)	((X)->rt_callers)
945 #define	HANDLES(X)	((X)->rt_handles)
946 #define	GROUPS(X)	((X)->rt_groups)
947 #define	FCT(X)		((X)->rt_fct)
948 #define	SYMINTP(X)	((X)->rt_symintp)
949 #define	LIST(X)		((X)->rt_list)
950 #define	OBJFLTRNDX(X)	((X)->rt_objfltrndx)
951 #define	SYMSFLTRCNT(X)	((X)->rt_symsfltrcnt)
952 #define	SYMAFLTRCNT(X)	((X)->rt_symafltrcnt)
953 #define	MODE(X)		((X)->rt_mode)
954 #define	SORTVAL(X)	((X)->rt_sortval)
955 #define	CYCGROUP(X)	((X)->rt_cycgroup)
956 #define	STDEV(X)	((X)->rt_stdev)
957 #define	STINO(X)	((X)->rt_stino)
958 #define	ORIGNAME(X)	((X)->rt_origname)
959 #define	DIRSZ(X)	((X)->rt_dirsz)
960 #define	COPY_R(X)	((X)->rt_copy.rtc_r)
961 #define	COPY_S(X)	((X)->rt_copy.rtc_s)
962 #define	AUDITORS(X)	((X)->rt_auditors)
963 #define	AUDINFO(X)	((X)->rt_audinfo)
964 #define	SYMINFO(X)	((X)->rt_syminfo)
965 #define	INITARRAY(X)	((X)->rt_initarray)
966 #define	FINIARRAY(X)	((X)->rt_finiarray)
967 #define	PREINITARRAY(X)	((X)->rt_preinitarray)
968 #define	MMAPS(X)	((X)->rt_mmaps)
969 #define	MMAPCNT(X)	((X)->rt_mmapcnt)
970 #define	INITARRAYSZ(X)	((X)->rt_initarraysz)
971 #define	FINIARRAYSZ(X)	((X)->rt_finiarraysz)
972 #define	PREINITARRAYSZ(X) ((X)->rt_preinitarraysz)
973 #define	DYNINFO(X)	((X)->rt_dyninfo)
974 #define	DYNINFOCNT(X)	((X)->rt_dyninfocnt)
975 #define	RELACOUNT(X)	((X)->rt_relacount)
976 #define	IDX(X)		((X)->rt_idx)
977 #define	LAZY(X)		((X)->rt_lazy)
978 #define	CNTL(X)		((X)->rt_cntl)
979 #define	CAP(X)		((X)->rt_cap)
980 #define	CAPCHAIN(X)	((X)->rt_capchain)
981 
982 /*
983  * Flags for tsorting.
984  */
985 #define	RT_SORT_FWD	0x01		/* topological sort (.fini) */
986 #define	RT_SORT_REV	0x02		/* reverse topological sort (.init) */
987 #define	RT_SORT_DELETE	0x10		/* process FLG_RT_DELETE objects */
988 					/*	only (called via dlclose()) */
989 #define	RT_SORT_INTPOSE	0x20		/* process interposer objects */
990 
991 /*
992  * Flags for lookup_sym (and hence find_sym) routines.
993  */
994 #define	LKUP_DEFT	0x0000		/* simple lookup request */
995 #define	LKUP_SPEC	0x0001		/* special ELF lookup (allows address */
996 					/*	resolutions to plt[] entries) */
997 #define	LKUP_LDOT	0x0002		/* indicates the original A_OUT */
998 					/*	symbol had a leading `.' */
999 #define	LKUP_FIRST	0x0004		/* lookup symbol in first link map */
1000 					/*	only */
1001 #define	LKUP_COPY	0x0008		/* lookup symbol for a COPY reloc, do */
1002 					/*	not bind to symbol at head */
1003 #define	LKUP_STDRELOC	0x0010		/* lookup originates from a standard */
1004 					/*	relocation (elf_reloc()) */
1005 #define	LKUP_SELF	0x0020		/* lookup symbol in ourself - undef */
1006 					/*	is valid */
1007 #define	LKUP_WEAK	0x0040		/* relocation reference is weak */
1008 #define	LKUP_NEXT	0x0080		/* request originates from RTLD_NEXT */
1009 #define	LKUP_NODESCENT	0x0100		/* don't descend through dependencies */
1010 #define	LKUP_NOFALLBACK	0x0200		/* don't fall back to loading */
1011 					/*	pending lazy dependencies */
1012 #define	LKUP_DIRECT	0x0400		/* direct binding request */
1013 #define	LKUP_SYMNDX	0x0800		/* establish symbol index */
1014 #define	LKUP_SINGLETON	0x1000		/* search for a singleton symbol */
1015 #define	LKUP_STANDARD	0x2000		/* standard lookup - originated from */
1016 					/* 	head link-map element */
1017 #define	LKUP_WORLD	0x4000		/* ensure world lookup */
1018 #define	LKUP_DLSYM	0x8000		/* lookup stems from dlsym() request */
1019 
1020 /*
1021  * For the runtime linker to perform a symbol search, a number of data items
1022  * related to the search are required.  An Slookup data structure is used to
1023  * convey this data to lookup_sym(), and in special cases, to other core
1024  * routines that provide the implementation details for lookup_sym()
1025  *
1026  * The symbol name (sl_name), the caller (sl_cmap), and the link-map from which
1027  * to start the search (sl_imap) are fundamental to the symbol search.  The
1028  * initial search link-map might get modified by the core routines that provide
1029  * the implementation details for lookup_sym().  This modification accommodates
1030  * requirements such as processing a handle, direct binding and interposition.
1031  * The association between the caller and the potential destination also
1032  * determines whether the destination is a candidate to search.
1033  *
1034  * The lookup identifier (sl_id) is used to identify a runtime linker operation.
1035  * Within this operation, any lazy loads that fail are not re-examined.  This
1036  * technique keeps the overhead of processing a failed lazy load to a minimum.
1037  *
1038  * Symbol searches that originate from a relocation record are accompanied by
1039  * the relocation index (sl_rsymndx), the symbol reference (sl_rsym) and
1040  * possibly the relocation type (sl_rtype).  This data provides for determining
1041  * lazy loading, direct binding, and special symbol processing requirements
1042  * such as copy relocations and singleton lookup.
1043  *
1044  * The symbols hash value is computed by lookup_sym, and propagated throughout
1045  * the search engine.  Note, occasionally the Slookup data is passed to a core
1046  * routine that provides the implementation details for lookup_sym(), ie.
1047  * elf_find_sym(), in which case the caller must initialize the hash value.
1048  *
1049  * The symbols binding information is established by lookup_sym() when the
1050  * symbols relocation type is supplied.  Weak bindings allow relocations to
1051  * be set to zero should a symbol lookup fail.
1052  *
1053  * The flags allow the caller to control aspects of the search, including the
1054  * interpretation of copy relocations, etc.  Note, a number of flag settings
1055  * are established in lookup_sym() from attributes of the symbol reference.
1056  */
1057 struct slookup {
1058 	const char	*sl_name;	/* symbol name */
1059 	Rt_map		*sl_cmap;	/* callers link-map */
1060 	Rt_map		*sl_imap;	/* initial link-map to search */
1061 	ulong_t		sl_id;		/* identifier for this lookup */
1062 	ulong_t		sl_hash;	/* symbol hash value */
1063 	ulong_t		sl_rsymndx;	/* referencing reloc symndx */
1064 	Sym		*sl_rsym;	/* referencing symbol */
1065 	uchar_t		sl_rtype;	/* relocation type associate with */
1066 					/*    symbol */
1067 	uchar_t		sl_bind;	/* symbols binding (returned) */
1068 	uint_t		sl_flags;	/* lookup flags */
1069 };
1070 
1071 #define	SLOOKUP_INIT(sl, name, cmap, imap, id, hash, rsymndx, rsym, rtype, \
1072     flags) \
1073 	(void) (sl.sl_name = (name), sl.sl_cmap = (cmap), sl.sl_imap = (imap), \
1074 	    sl.sl_id = (id), sl.sl_hash = (hash), sl.sl_rsymndx = (rsymndx), \
1075 	    sl.sl_rsym = (rsym), sl.sl_rtype = (rtype), sl.sl_bind = 0, \
1076 	    sl.sl_flags = (flags))
1077 
1078 /*
1079  * After a symbol lookup has been resolved, the runtime linker needs to retain
1080  * information regarding the bound definition.  An Sresult data structure is
1081  * used to provide this information.
1082  *
1083  * The symbol name (sr_name) may differ from the original referenced symbol if
1084  * a symbol capabilities family member has resolved the binding.  The defining
1085  * object (sr_dmap) indicates the object in which the definition has been found.
1086  * The symbol table entry (sr_sym) defines the bound symbol definition.
1087  *
1088  * Note, a symbol lookup may start with one Sresult buffer, but underlying
1089  * routines (for example, those that probe filters) might employ their own
1090  * Sresult buffer.  If a binding is allowed, the latter buffer may get inherited
1091  * by the former.  Along with this chain of requests, binding info (binfo) and
1092  * not-found information (in_nfavl), may be passed between all the associated
1093  * functions.  Hence, the binfo and in_nfavl data is not maintained as part of
1094  * a Sresult structure.
1095  */
1096 struct sresult {
1097 	const char	*sr_name;	/* symbol definition name */
1098 	Rt_map		*sr_dmap;	/* defining objects link-map */
1099 	Sym		*sr_sym;	/* symbol table pointer */
1100 };
1101 
1102 #define	SRESULT_INIT(sr, name) \
1103 	(void) (sr.sr_name = (name), sr.sr_dmap = NULL, sr.sr_sym = NULL)
1104 
1105 /*
1106  * Define a system capabilities structure for maintaining the various
1107  * capabilities of the system.  This structure follows the Objcapset definition
1108  * from libld.h, however the system can only have one platform or machine
1109  * hardware name, thus this structure is a little simpler.
1110  */
1111 typedef	struct {
1112 	elfcap_mask_t	sc_hw_1;	/* CA_SUNW_HW_1 capabilities */
1113 	elfcap_mask_t	sc_sf_1;	/* CA_SUNW_SF_1 capabilities */
1114 	elfcap_mask_t	sc_hw_2;	/* CA_SUNW_HW_2 capabilities */
1115 	char		*sc_plat;	/* CA_SUNW_PLAT capability */
1116 	size_t		sc_platsz;	/*	and size */
1117 	char		*sc_mach;	/* CA_SUNW_MACH capability */
1118 	size_t		sc_machsz;	/*	and size */
1119 } Syscapset;
1120 
1121 /*
1122  * Define a number of .plt lookup outcomes, for use in binding diagnostics.
1123  */
1124 typedef	enum {
1125 	PLT_T_NONE = 0,
1126 	PLT_T_21D,
1127 	PLT_T_24D,
1128 	PLT_T_U32,
1129 	PLT_T_U44,
1130 	PLT_T_FULL,
1131 	PLT_T_FAR,
1132 	PLT_T_NUM			/* Must be last */
1133 } Pltbindtype;
1134 
1135 /*
1136  * Prototypes.
1137  */
1138 extern ulong_t		ld_entry_cnt;	/* counter bumped on each entry to */
1139 					/*    ld.so.1. */
1140 extern Lm_list		lml_main;	/* main's link map list */
1141 extern Lm_list		lml_rtld;	/* rtld's link map list */
1142 extern Lm_list		*lml_list[];
1143 
1144 extern Pltbindtype	elf_plt_write(uintptr_t, uintptr_t, void *, uintptr_t,
1145 			    Xword);
1146 extern Rt_map		*is_so_loaded(Lm_list *, const char *, int *);
1147 extern int		lookup_sym(Slookup *, Sresult *, uint_t *, int *);
1148 extern int		rt_dldump(Rt_map *, const char *, int, Addr);
1149 
1150 #ifdef	__cplusplus
1151 }
1152 #endif
1153 
1154 #endif /* _RTLD_H */
1155