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