xref: /titanic_41/usr/src/cmd/sgs/include/libld.h (revision e4f5a11d4a234623168c1558fcdf4341e11769e1)
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) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
27  */
28 
29 #ifndef	_LIBLD_H
30 #define	_LIBLD_H
31 
32 #include <stdlib.h>
33 #include <libelf.h>
34 #include <sgs.h>
35 #include <_machelf.h>
36 #include <string_table.h>
37 #include <sys/avl.h>
38 #include <alist.h>
39 #include <elfcap.h>
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 /*
46  * Default directory search path manipulation for the link-editor.  YLDIR
47  * indicates which directory in LIBPATH is replaced by the -YL option to cc
48  * and ld.  YUDIR indicates which directory is replaced by -YU.
49  */
50 #define	YLDIR	1
51 #define	YUDIR	2
52 
53 /*
54  * Define a hash value that can never be returned from elf_hash().
55  */
56 #define	SYM_NOHASH	(~(Word)0)
57 
58 /*
59  * Macro that can be used to represent both ORDER flags
60  * in a section header.
61  */
62 #define	ALL_SHF_ORDER	(SHF_ORDERED | SHF_LINK_ORDER)
63 
64 /*
65  * The linker merges (concatenates) sections with the same name and
66  * compatible section header flags. When comparing these flags,
67  * there are some that should not be included in the decision.
68  * The ALL_SHF_IGNORE constant defines these flags.
69  *
70  * NOTE: SHF_MERGE|SHF_STRINGS:
71  * The compiler is allowed to set the SHF_MERGE|SHF_STRINGS flags in
72  * order to tell the linker that:
73  *
74  *      1) There is nothing in the section except null terminated strings.
75  *	2) Those strings do not contain NULL bytes, except as termination.
76  *	3) All references to these strings occur via standard relocation
77  *		records.
78  *
79  * As a result, if two compatible sections both have these flags set, it is
80  * OK to combine the strings they contain into a single merged string table
81  * with duplicates removed and tail strings merged.
82  *
83  * This is a different meaning than the simple concatenating of sections
84  * that the linker always does. It is a hint that an additional optimization
85  * is possible, but not required. This means that sections that do not
86  * share the same SHF_MERGE|SHF_STRINGS values can be concatenated,
87  * but cannot have their duplicate strings combined. Hence, the
88  * SHF_MERGE|SHF_STRINGS flags should be ignored when deciding whether
89  * two sections can be concatenated.
90  */
91 #define	ALL_SHF_IGNORE	(ALL_SHF_ORDER | SHF_GROUP | SHF_MERGE | SHF_STRINGS)
92 
93 /*
94  * Define symbol reference types for use in symbol resolution.
95  */
96 typedef enum {
97 	REF_DYN_SEEN,			/* a .so symbol has been seen */
98 	REF_DYN_NEED,			/* a .so symbol satisfies a .o symbol */
99 	REF_REL_NEED,			/* a .o symbol */
100 	REF_NUM				/* the number of symbol references */
101 } Symref;
102 
103 /*
104  * Relocation descriptor cache
105  */
106 struct rel_cache {
107 	APlist		*rc_list;	/* list of Rel_cachebuf */
108 	Word		rc_cnt;		/* 	and count */
109 };
110 
111 /*
112  * GOT reference models
113  */
114 typedef enum {
115 	GOT_REF_GENERIC,	/* generic symbol reference */
116 	GOT_REF_TLSIE,		/* TLS initial exec (gnu) reference */
117 	GOT_REF_TLSLD,		/* TLS local dynamic reference */
118 	GOT_REF_TLSGD		/* TLS general dynamic reference */
119 } Gotref;
120 
121 typedef struct {
122 	Xword		gn_addend;	/* addend associated with GOT entry */
123 	Sword		gn_gotndx;	/* GOT table index */
124 	Gotref		gn_gotref;
125 } Gotndx;
126 
127 /*
128  * Got debugging structure.  The got index is defined as a signed value as we
129  * do so much mucking around with negative and positive gots on SPARC, and sign
130  * extension is necessary when building 64-bit objects.  On intel we explicitly
131  * cast this variable to an unsigned value.
132  */
133 typedef struct {
134 	Sym_desc	*gt_sym;
135 	Gotndx		gt_gndx;
136 } Gottable;
137 
138 /*
139  * The link-editor caches the results of sloppy relocation processing
140  * in a variable of type Rlxrel_cache. Symbols come for processing in sorted
141  * order, so a single item cache suffices to eliminate duplicate lookups.
142  *
143  * When sloppy relocation processing fails, the Rlxrel_rej enum reports
144  * the underlying reason.
145  */
146 typedef enum {
147 	RLXREL_REJ_NONE = 0,	/* Replacement symbol was found */
148 	RLXREL_REJ_TARGET,	/* Target sec disallows relaxed relocations */
149 	RLXREL_REJ_SECTION,	/* Either there is no replacement section, */
150 				/* 	or its attributes are incompatible */
151 	RLXREL_REJ_SYMBOL,	/* Replacement symbol not found */
152 } Rlxrel_rej;
153 
154 typedef struct sreloc_cache {
155 	Sym_desc	*sr_osdp;	/* Original symbol */
156 	Sym_desc	*sr_rsdp;	/* Replacement symbol */
157 	Rlxrel_rej	sr_rej;		/* Reason for failure if NULL sr_rsdp */
158 } Rlxrel_cache;
159 
160 /*
161  * Nodes in an ofl_wrap AVL tree
162  *
163  * wsn_name is the name of the symbol to be wrapped. wsn_wrapname is used
164  * when we need to refer to the wrap symbol, and consists of the symbol
165  * name with a __wrap_ prefix.
166  */
167 typedef struct wrap_sym_node {
168 	avl_node_t	wsn_avlnode;	/* AVL book-keeping */
169 	const char	*wsn_name;	/* Symbol name: XXX */
170 	const char	*wsn_wrapname;	/* Wrap symbol name: __wrap_XXX */
171 } WrapSymNode;
172 
173 /*
174  * Capabilities structures, used to maintain a capabilities set.
175  *
176  * Capabilities can be defined within input relocatable objects, and can be
177  * augmented or replaced by mapfile directives.  In addition, mapfile directives
178  * can be used to exclude capabilities that would otherwise be carried over to
179  * the output object.
180  *
181  * CA_SUNW_HW_1, CA_SUNW_SF_1 and CA_SUNW_HW_2 values are bitmasks.  A current
182  * value, and an exclude value are maintained for each capability.
183  *
184  * There can be multiple CA_SUNW_PLAT and CA_SUNW_MACH entries and thus Alists
185  * are used to collect these entries.  A current list for each capability is
186  * maintained as Capstr entries, which provide for maintaining the strings
187  * eventual index into a string table.  An exclude list is maintained as a
188  * list of string pointers.
189  */
190 typedef struct {
191 	elfcap_mask_t	cm_val;		/* bitmask value */
192 	elfcap_mask_t	cm_exc;		/* bits to exclude from final object */
193 } Capmask;
194 
195 typedef struct {
196 	Alist		*cl_val;	/* string (Capstr) value */
197 	APlist		*cl_exc;	/* strings to exclude from final */
198 } Caplist;				/*	object */
199 
200 typedef	struct {
201 	char		*cs_str;	/* platform or machine name */
202 	Word		cs_ndx;		/* the entries output Cap index */
203 } Capstr;
204 
205 typedef	uint_t		oc_flag_t;
206 typedef	struct {
207 	Capmask		oc_hw_1;	/* CA_SUNW_HW_1 capabilities */
208 	Capmask		oc_sf_1;	/* CA_SUNW_SF_1 capabilities */
209 	Capmask		oc_hw_2;	/* CA_SUNW_HW_2 capabilities */
210 	Caplist		oc_plat;	/* CA_SUNW_PLAT capabilities */
211 	Caplist		oc_mach;	/* CA_SUNW_MACH capabilities */
212 	Capstr		oc_id;		/* CA_SUNW_ID capability */
213 	oc_flag_t	oc_flags;
214 } Objcapset;
215 
216 #define	FLG_OCS_USRDEFID	0x1	/* user defined CA_SUNW_ID */
217 
218 /*
219  * Bitmasks for a single capability. Capabilities come from input objects,
220  * augmented or replaced by mapfile directives. In addition, mapfile directives
221  * can be used to exclude bits that would otherwise be set in the output object.
222  */
223 typedef struct {
224 	elfcap_mask_t	cm_value;	/* Bitmask value */
225 	elfcap_mask_t	cm_exclude;	/* Bits to remove from final object */
226 } CapMask;
227 
228 /*
229  * Combine the bitmask in a CapMask with the exclusion mask and
230  * return the resulting final value.
231  */
232 #define	CAPMASK_VALUE(_cbmp) ((_cbmp)->cm_value & ~(_cbmp)->cm_exclude)
233 
234 typedef struct {
235 	CapMask		c_hw_1;		/* CA_SUNW_HW_1 capabilities */
236 	CapMask		c_sf_1;		/* CA_SUNW_SF_1 capabilities */
237 	CapMask		c_hw_2;		/* CA_SUNW_HW_2 capabilities */
238 } Outcapset;
239 
240 
241 /*
242  * Output file processing structure
243  */
244 typedef Lword ofl_flag_t;
245 struct ofl_desc {
246 	char		*ofl_sgsid;	/* link-editor identification */
247 	const char	*ofl_name;	/* full file name */
248 	Elf		*ofl_elf;	/* elf_memory() elf descriptor */
249 	Elf		*ofl_welf;	/* ELF_C_WRITE elf descriptor */
250 	Ehdr		*ofl_dehdr;	/* default elf header, and new elf */
251 	Ehdr		*ofl_nehdr;	/*	header describing this file */
252 	Phdr		*ofl_phdr;	/* program header descriptor */
253 	Phdr		*ofl_tlsphdr;	/* TLS phdr */
254 	int		ofl_fd;		/* file descriptor */
255 	size_t		ofl_size;	/* image size */
256 	APlist		*ofl_maps;	/* list of input mapfiles */
257 	APlist		*ofl_segs;	/* list of segments */
258 	APlist		*ofl_segs_order; /* SEGMENT_ORDER segments */
259 	avl_tree_t	ofl_segs_avl;	/* O(log N) access to named segments */
260 	APlist		*ofl_ents;	/* list of entrance descriptors */
261 	avl_tree_t	ofl_ents_avl;	/* O(log N) access to named ent. desc */
262 	APlist		*ofl_objs;	/* relocatable object file list */
263 	Word		ofl_objscnt;	/* 	and count */
264 	APlist		*ofl_ars;	/* archive library list */
265 	Word		ofl_arscnt;	/* 	and count */
266 	int		ofl_ars_gsandx; /* archive group argv index. 0 means */
267 					/*	no current group, < 0 means */
268 					/*	error reported. >0 is cur ndx */
269 	Word		ofl_ars_gsndx;	/* current -zrescan-start ofl_ars ndx */
270 	APlist		*ofl_sos;	/* shared object list */
271 	Word		ofl_soscnt;	/* 	and count */
272 	APlist		*ofl_soneed;	/* list of implicitly required .so's */
273 	APlist		*ofl_socntl;	/* list of .so control definitions */
274 	Rel_cache	ofl_outrels;	/* list of output relocations */
275 	Rel_cache	ofl_actrels;	/* list of relocations to perform */
276 	APlist		*ofl_relaux;	/* Rel_aux cache for outrels/actrels */
277 	Word		ofl_entrelscnt;	/* no of relocations entered */
278 	Alist		*ofl_copyrels;	/* list of copy relocations */
279 	APlist		*ofl_ordered;	/* list of shf_ordered sections */
280 	APlist		*ofl_symdtent;	/* list of syminfo symbols that need */
281 					/*	to reference .dynamic entries */
282 	APlist		*ofl_ismove;	/* list of .SUNW_move sections */
283 	APlist		*ofl_ismoverel;	/* list of relocation input section */
284 					/* targeting to expanded area */
285 	APlist		*ofl_parsyms; 	/* list of partially initialized */
286 					/*	symbols (ie. move symbols) */
287 	APlist		*ofl_extrarels;	/* relocation sections which have */
288 					/*    a NULL sh_info */
289 	avl_tree_t	*ofl_groups;	/* pointer to head of Groups AVL tree */
290 	APlist		*ofl_initarray;	/* list of init array func names */
291 	APlist		*ofl_finiarray;	/* list of fini array func names */
292 	APlist		*ofl_preiarray;	/* list of preinit array func names */
293 	APlist		*ofl_rtldinfo;	/* list of rtldinfo syms */
294 	APlist		*ofl_osgroups;	/* list of output GROUP sections */
295 	APlist		*ofl_ostlsseg;	/* pointer to sections in TLS segment */
296 	APlist		*ofl_unwind;	/* list of unwind output sections */
297 	Os_desc		*ofl_unwindhdr;	/* Unwind hdr */
298 	avl_tree_t	ofl_symavl;	/* pointer to head of Syms AVL tree */
299 	Sym_desc	**ofl_regsyms;	/* array of potential register */
300 	Word		ofl_regsymsno;	/*    symbols and array count */
301 	Word		ofl_regsymcnt;	/* no. of output register symbols */
302 	Word		ofl_lregsymcnt;	/* no. of local register symbols */
303 	Sym_desc	*ofl_dtracesym;	/* ld -zdtrace= */
304 	ofl_flag_t	ofl_flags;	/* various state bits, args etc. */
305 	ofl_flag_t	ofl_flags1;	/*	more flags */
306 	void		*ofl_entry;	/* entry point (-e and Sym_desc *) */
307 	char		*ofl_filtees;	/* shared objects we are a filter for */
308 	const char	*ofl_soname;	/* (-h option) output file name for */
309 					/*	dynamic structure */
310 	const char	*ofl_interp;	/* interpreter name used by exec() */
311 	char		*ofl_rpath;	/* run path to store in .dynamic */
312 	char		*ofl_config;	/* config path to store in .dynamic */
313 	APlist		*ofl_ulibdirs;	/* user supplied library search list */
314 	APlist		*ofl_dlibdirs;	/* default library search list */
315 	Word		ofl_vercnt;	/* number of versions to generate */
316 	APlist		*ofl_verdesc;	/* list of version descriptors */
317 	size_t		ofl_verdefsz;	/* size of version definition section */
318 	size_t		ofl_verneedsz;	/* size of version needed section */
319 	Word		ofl_entercnt;	/* no. of global symbols entered */
320 	Word		ofl_globcnt;	/* no. of global symbols to output */
321 	Word		ofl_scopecnt;	/* no. of scoped symbols to output */
322 	Word		ofl_dynscopecnt; /* no. scoped syms in .SUNW_ldynsym */
323 	Word		ofl_elimcnt;	/* no. of eliminated symbols */
324 	Word		ofl_locscnt;	/* no. of local symbols in .symtab */
325 	Word		ofl_dynlocscnt;	/* no. local symbols in .SUNW_ldynsym */
326 	Word		ofl_dynsymsortcnt; /* no. ndx in .SUNW_dynsymsort */
327 	Word		ofl_dyntlssortcnt; /* no. ndx in .SUNW_dyntlssort */
328 	Word		ofl_dynshdrcnt;	/* no. of output section in .dynsym */
329 	Word		ofl_shdrcnt;	/* no. of output sections */
330 	Word		ofl_caploclcnt;	/* no. of local capabilities symbols */
331 	Word		ofl_capsymcnt;	/* no. of symbol capabilities entries */
332 					/*	required */
333 	Word		ofl_capchaincnt; /* no. of Capchain symbols */
334 	APlist		*ofl_capgroups;	/* list of capabilities groups */
335 	avl_tree_t	*ofl_capfamilies; /* capability family AVL tree */
336 	Str_tbl		*ofl_shdrsttab;	/* Str_tbl for shdr strtab */
337 	Str_tbl		*ofl_strtab;	/* Str_tbl for symtab strtab */
338 	Str_tbl		*ofl_dynstrtab;	/* Str_tbl for dymsym strtab */
339 	Gotndx		*ofl_tlsldgotndx; /* index to LD TLS_index structure */
340 	Xword		ofl_relocsz;	/* size of output relocations */
341 	Xword		ofl_relocgotsz;	/* size of .got relocations */
342 	Xword		ofl_relocpltsz;	/* size of .plt relocations */
343 	Xword		ofl_relocbsssz;	/* size of .bss (copy) relocations */
344 	Xword		ofl_relocrelsz;	/* size of .rel[a] relocations */
345 	Word		ofl_relocincnt;	/* no. of input relocations */
346 	Word		ofl_reloccnt;	/* tot number of output relocations */
347 	Word		ofl_reloccntsub; /* tot numb of output relocations to */
348 					/*	skip (-zignore) */
349 	Word		ofl_relocrelcnt; /* tot number of relative */
350 					/*	relocations */
351 	Word		ofl_gotcnt;	/* no. of .got entries */
352 	Word		ofl_pltcnt;	/* no. of .plt entries */
353 	Word		ofl_pltpad;	/* no. of .plt padd entries */
354 	Word		ofl_hashbkts;	/* no. of hash buckets required */
355 	Is_desc		*ofl_isbss;	/* .bss input section (globals) */
356 	Is_desc		*ofl_islbss;	/* .lbss input section (globals) */
357 	Is_desc		*ofl_istlsbss;	/* .tlsbss input section (globals) */
358 	Is_desc		*ofl_isparexpn;	/* -z nopartial .data input section */
359 	Os_desc		*ofl_osdynamic;	/* .dynamic output section */
360 	Os_desc		*ofl_osdynsym;	/* .dynsym output section */
361 	Os_desc		*ofl_osldynsym;	/* .SUNW_ldynsym output section */
362 	Os_desc		*ofl_osdynstr;	/* .dynstr output section */
363 	Os_desc		*ofl_osdynsymsort; /* .SUNW_dynsymsort output section */
364 	Os_desc		*ofl_osdyntlssort; /* .SUNW_dyntlssort output section */
365 	Os_desc		*ofl_osgot;	/* .got output section */
366 	Os_desc		*ofl_oshash;	/* .hash output section */
367 	Os_desc		*ofl_osinitarray; /* .init_array output section */
368 	Os_desc		*ofl_osfiniarray; /* .fini_array output section */
369 	Os_desc		*ofl_ospreinitarray; /* .preinit_array output section */
370 	Os_desc		*ofl_osinterp;	/* .interp output section */
371 	Os_desc		*ofl_oscap;	/* .SUNW_cap output section */
372 	Os_desc		*ofl_oscapinfo;	/* .SUNW_capinfo output section */
373 	Os_desc		*ofl_oscapchain; /* .SUNW_capchain output section */
374 	Os_desc		*ofl_osplt;	/* .plt output section */
375 	Os_desc		*ofl_osmove;	/* .SUNW_move output section */
376 	Os_desc		*ofl_osrelhead;	/* first relocation section */
377 	Os_desc		*ofl_osrel;	/* .rel[a] relocation section */
378 	Os_desc		*ofl_osshstrtab; /* .shstrtab output section */
379 	Os_desc		*ofl_osstrtab;	/* .strtab output section */
380 	Os_desc		*ofl_ossymtab;	/* .symtab output section */
381 	Os_desc		*ofl_ossymshndx; /* .symtab_shndx output section */
382 	Os_desc		*ofl_osdynshndx; /* .dynsym_shndx output section */
383 	Os_desc		*ofl_osldynshndx; /* .SUNW_ldynsym_shndx output sec */
384 	Os_desc		*ofl_osverdef;	/* .version definition output section */
385 	Os_desc		*ofl_osverneed;	/* .version needed output section */
386 	Os_desc		*ofl_osversym;	/* .version symbol ndx output section */
387 	Word		ofl_dtflags_1;	/* DT_FLAGS_1 entries */
388 	Word		ofl_dtflags;	/* DT_FLAGS entries */
389 	Os_desc		*ofl_ossyminfo;	/* .SUNW_syminfo output section */
390 	Half		ofl_parexpnndx;	/* -z nopartial section index */
391 					/* Ref. at perform_outreloc() in */
392 					/* libld/{mach}/machrel.c */
393 	Xword		*ofl_checksum;	/* DT_CHECKSUM value address */
394 	char		*ofl_depaudit;	/* dependency auditing required (-P) */
395 	char		*ofl_audit;	/* object auditing required (-p) */
396 	Alist		*ofl_symfltrs;	/* per-symbol filtees and their */
397 	Alist		*ofl_dtsfltrs;	/*	associated .dynamic/.dynstrs */
398 	Objcapset	ofl_ocapset;	/* object capabilities */
399 	Lm_list		*ofl_lml;	/* runtime link-map list */
400 	Gottable	*ofl_gottable;	/* debugging got information */
401 	Rlxrel_cache	ofl_sr_cache;	/* Cache last result from */
402 					/*	sloppy_comdat_reloc() */
403 	APlist		*ofl_maptext;	/* mapfile added text sections */
404 	APlist		*ofl_mapdata;	/* mapfile added data sections */
405 	avl_tree_t	*ofl_wrap;	/* -z wrap symbols */
406 };
407 
408 #define	FLG_OF_DYNAMIC	0x00000001	/* generate dynamic output module */
409 #define	FLG_OF_STATIC	0x00000002	/* generate static output module */
410 #define	FLG_OF_EXEC	0x00000004	/* generate an executable */
411 #define	FLG_OF_RELOBJ	0x00000008	/* generate a relocatable object */
412 #define	FLG_OF_SHAROBJ	0x00000010	/* generate a shared object */
413 #define	FLG_OF_BFLAG	0x00000020	/* do no special plt building: -b */
414 #define	FLG_OF_IGNENV	0x00000040	/* ignore LD_LIBRARY_PATH: -i */
415 #define	FLG_OF_STRIP	0x00000080	/* strip output: -s */
416 #define	FLG_OF_NOWARN	0x00000100	/* disable symbol warnings: -t */
417 #define	FLG_OF_NOUNDEF	0x00000200	/* allow no undefined symbols: -zdefs */
418 #define	FLG_OF_PURETXT	0x00000400	/* allow no text relocations: -ztext */
419 #define	FLG_OF_GENMAP	0x00000800	/* generate a memory map: -m */
420 #define	FLG_OF_DYNLIBS	0x00001000	/* dynamic input allowed: -Bdynamic */
421 #define	FLG_OF_SYMBOLIC	0x00002000	/* bind global symbols: -Bsymbolic */
422 #define	FLG_OF_ADDVERS	0x00004000	/* add version stamp: -Qy */
423 #define	FLG_OF_NOLDYNSYM 0x00008000	/* -znoldynsym set */
424 #define	FLG_OF_IS_ORDER	0x00010000	/* input section ordering within a */
425 					/*	segment is required */
426 #define	FLG_OF_EC_FILES	0x00020000	/* Ent_desc exist w/non-NULL ec_files */
427 #define	FLG_OF_TEXTREL	0x00040000	/* text relocations have been found */
428 #define	FLG_OF_MULDEFS	0x00080000	/* multiple symbols are allowed */
429 #define	FLG_OF_TLSPHDR	0x00100000	/* a TLS program header is required */
430 #define	FLG_OF_BLDGOT	0x00200000	/* build GOT table */
431 #define	FLG_OF_VERDEF	0x00400000	/* record version definitions */
432 #define	FLG_OF_VERNEED	0x00800000	/* record version dependencies */
433 #define	FLG_OF_NOVERSEC 0x01000000	/* don't record version sections */
434 #define	FLG_OF_KEY	0x02000000	/* file requires sort keys */
435 #define	FLG_OF_PROCRED	0x04000000	/* process any symbol reductions by */
436 					/*	effecting the symbol table */
437 					/*	output and relocations */
438 #define	FLG_OF_SYMINFO	0x08000000	/* create a syminfo section */
439 #define	FLG_OF_AUX	0x10000000	/* ofl_filter is an auxiliary filter */
440 #define	FLG_OF_FATAL	0x20000000	/* fatal error during input */
441 #define	FLG_OF_WARN	0x40000000	/* warning during input processing. */
442 #define	FLG_OF_VERBOSE	0x80000000	/* -z verbose flag set */
443 
444 #define	FLG_OF_MAPSYMB	0x000100000000	/* symbolic scope definition seen */
445 #define	FLG_OF_MAPGLOB	0x000200000000	/* global scope definition seen */
446 #define	FLG_OF_COMREL	0x000400000000	/* -z combreloc set, which enables */
447 					/*	DT_RELACNT tracking, */
448 #define	FLG_OF_NOCOMREL	0x000800000000	/* -z nocombreloc set */
449 #define	FLG_OF_AUTOLCL	0x001000000000	/* automatically reduce unspecified */
450 					/*	global symbols to locals */
451 #define	FLG_OF_AUTOELM	0x002000000000	/* automatically eliminate */
452 					/*	unspecified global symbols */
453 #define	FLG_OF_REDLSYM	0x004000000000	/* reduce local symbols */
454 #define	FLG_OF_OS_ORDER	0x008000000000	/* output section ordering required */
455 #define	FLG_OF_OSABI	0x010000000000	/* tag object as ELFOSABI_SOLARIS */
456 #define	FLG_OF_ADJOSCNT	0x020000000000	/* adjust ofl_shdrcnt to accommodate */
457 					/*	discarded sections */
458 #define	FLG_OF_OTOSCAP	0x040000000000	/* convert object capabilities to */
459 					/*	symbol capabilities */
460 #define	FLG_OF_PTCAP	0x080000000000	/* PT_SUNWCAP required */
461 #define	FLG_OF_CAPSTRS	0x100000000000	/* capability strings are required */
462 #define	FLG_OF_EHFRAME	0x200000000000	/* output contains .eh_frame section */
463 
464 /*
465  * In the flags1 arena, establish any options that are applicable to archive
466  * extraction first, and associate a mask.  These values are recorded with any
467  * archive descriptor so that they may be reset should the archive require a
468  * rescan to try and resolve undefined symbols.
469  */
470 #define	FLG_OF1_ALLEXRT	0x0000000001	/* extract all members from an */
471 					/*	archive file */
472 #define	FLG_OF1_WEAKEXT	0x0000000002	/* allow archive extraction to */
473 					/*	resolve weak references */
474 #define	MSK_OF1_ARCHIVE	0x0000000003	/* archive flags mask */
475 
476 #define	FLG_OF1_NOINTRP	0x0000000008	/* -z nointerp flag set */
477 #define	FLG_OF1_ZDIRECT	0x0000000010	/* -z direct flag set */
478 #define	FLG_OF1_NDIRECT	0x0000000020	/* no-direct bindings specified */
479 #define	FLG_OF1_DEFERRED 0x0000000040	/* deferred dependency recording */
480 
481 #define	FLG_OF1_RELDYN	0x0000000100	/* process .dynamic in rel obj */
482 #define	FLG_OF1_NRLXREL	0x0000000200	/* -z norelaxreloc flag set */
483 #define	FLG_OF1_RLXREL	0x0000000400	/* -z relaxreloc flag set */
484 #define	FLG_OF1_IGNORE	0x0000000800	/* ignore unused dependencies */
485 #define	FLG_OF1_NOSGHND	0x0000001000	/* -z nosighandler flag set */
486 #define	FLG_OF1_TEXTOFF 0x0000002000	/* text relocations are ok */
487 #define	FLG_OF1_ABSEXEC	0x0000004000	/* -zabsexec set */
488 #define	FLG_OF1_LAZYLD	0x0000008000	/* lazy loading of objects enabled */
489 #define	FLG_OF1_GRPPRM	0x0000010000	/* dependencies are to have */
490 					/*	GROUPPERM enabled */
491 #define	FLG_OF1_OVRFLW	0x0000020000	/* size exceeds 32-bit limitation */
492 					/*	of 32-bit libld */
493 #define	FLG_OF1_NOPARTI	0x0000040000	/* -znopartial set */
494 #define	FLG_OF1_BSSOREL	0x0000080000	/* output relocation against bss */
495 					/*	section */
496 #define	FLG_OF1_TLSOREL	0x0000100000	/* output relocation against .tlsbss */
497 					/*	section */
498 #define	FLG_OF1_MEMORY	0x0000200000	/* produce a memory model */
499 #define	FLG_OF1_NGLBDIR	0x0000400000	/* no DT_1_DIRECT flag allowed */
500 #define	FLG_OF1_ENCDIFF	0x0000800000	/* host running linker has different */
501 					/*	byte order than output object */
502 #define	FLG_OF1_VADDR	0x0001000000	/* a segment defines explicit vaddr */
503 #define	FLG_OF1_EXTRACT	0x0002000000	/* archive member has been extracted */
504 #define	FLG_OF1_RESCAN	0x0004000000	/* any archives should be rescanned */
505 #define	FLG_OF1_IGNPRC	0x0008000000	/* ignore processing required */
506 #define	FLG_OF1_NCSTTAB	0x0010000000	/* -znocompstrtab set */
507 #define	FLG_OF1_DONE	0x0020000000	/* link-editor processing complete */
508 #define	FLG_OF1_NONREG	0x0040000000	/* non-regular file specified as */
509 					/*	the output file */
510 #define	FLG_OF1_ALNODIR	0x0080000000	/* establish NODIRECT for all */
511 					/*	exported interfaces. */
512 #define	FLG_OF1_OVHWCAP1 0x0100000000	/* override CA_SUNW_HW_1 capabilities */
513 #define	FLG_OF1_OVSFCAP1 0x0200000000	/* override CA_SUNW_SF_1 capabilities */
514 #define	FLG_OF1_OVHWCAP2 0x0400000000	/* override CA_SUNW_HW_2 capabilities */
515 #define	FLG_OF1_OVMACHCAP 0x0800000000	/* override CA_SUNW_MACH capability */
516 #define	FLG_OF1_OVPLATCAP 0x1000000000	/* override CA_SUNW_PLAT capability */
517 #define	FLG_OF1_OVIDCAP	0x2000000000	/* override CA_SUNW_ID capability */
518 
519 /*
520  * Test to see if the output file would allow the presence of
521  * a .dynsym section.
522  */
523 #define	OFL_ALLOW_DYNSYM(_ofl) (((_ofl)->ofl_flags & \
524 	(FLG_OF_DYNAMIC | FLG_OF_RELOBJ)) == FLG_OF_DYNAMIC)
525 
526 /*
527  * Test to see if the output file would allow the presence of
528  * a .SUNW_ldynsym section. The requirements are that a .dynsym
529  * is allowed, and -znoldynsym has not been specified. Note that
530  * even if the answer is True (1), we will only generate one if there
531  * are local symbols that require it.
532  */
533 #define	OFL_ALLOW_LDYNSYM(_ofl) (((_ofl)->ofl_flags & \
534 	(FLG_OF_DYNAMIC | FLG_OF_RELOBJ | FLG_OF_NOLDYNSYM)) == FLG_OF_DYNAMIC)
535 
536 /*
537  * Test to see if relocation processing should be done. This is normally
538  * true, but can be disabled via the '-z noreloc' option. Note that
539  * relocatable objects are still relocated even if '-z noreloc' is present.
540  */
541 #define	OFL_DO_RELOC(_ofl) (((_ofl)->ofl_flags & FLG_OF_RELOBJ) || \
542 	!((_ofl)->ofl_dtflags_1 & DF_1_NORELOC))
543 
544 /*
545  * Determine whether a static executable is being built.
546  */
547 #define	OFL_IS_STATIC_EXEC(_ofl) (((_ofl)->ofl_flags & \
548 	(FLG_OF_STATIC | FLG_OF_EXEC)) == (FLG_OF_STATIC | FLG_OF_EXEC))
549 
550 /*
551  * Determine whether a static object is being built.  This macro is used
552  * to select the appropriate string table, and symbol table that other
553  * sections need to reference.
554  */
555 #define	OFL_IS_STATIC_OBJ(_ofl) ((_ofl)->ofl_flags & \
556 	(FLG_OF_RELOBJ | FLG_OF_STATIC))
557 
558 /*
559  * Macros for counting symbol table entries.  These are used to size symbol
560  * tables and associated sections (.syminfo, SUNW_capinfo, .hash, etc.) and
561  * set required sh_info entries (the offset to the first global symbol).
562  */
563 #define	SYMTAB_LOC_CNT(_ofl)		/* local .symtab entries */	\
564 	(2 +				/*    NULL and STT_FILE */	\
565 	(_ofl)->ofl_shdrcnt +		/*    section symbol */		\
566 	(_ofl)->ofl_caploclcnt +	/*    local capabilities */	\
567 	(_ofl)->ofl_scopecnt +		/*    scoped symbols */		\
568 	(_ofl)->ofl_locscnt)		/*    standard locals */
569 #define	SYMTAB_ALL_CNT(_ofl)		/* all .symtab entries */	\
570 	(SYMTAB_LOC_CNT(_ofl) +		/*    .symtab locals */		\
571 	(_ofl)->ofl_globcnt)		/*    standard globals */
572 
573 #define	DYNSYM_LOC_CNT(_ofl)		/* local .dynsym entries */	\
574 	(1 +				/*    NULL */			\
575 	(_ofl)->ofl_dynshdrcnt +	/*    section symbols */	\
576 	(_ofl)->ofl_caploclcnt +	/*    local capabilities */	\
577 	(_ofl)->ofl_lregsymcnt)		/*    local register symbols */
578 #define	DYNSYM_ALL_CNT(_ofl)		/* all .dynsym entries */	\
579 	(DYNSYM_LOC_CNT(_ofl) +		/*    .dynsym locals */		\
580 	(_ofl)->ofl_globcnt)		/*    standard globals */
581 
582 /*
583  * Define a move descriptor used within relocation structures.
584  */
585 typedef struct {
586 	Move		*mr_move;
587 	Sym_desc	*mr_sym;
588 } Mv_reloc;
589 
590 /*
591  * Relocation (active & output) processing structure - transparent to common
592  * code. There can be millions of these structures in a large link, so it
593  * is important to keep it small. You should only add new items to Rel_desc
594  * if they are critical, apply to most relocations, and cannot be easily
595  * computed from the other information.
596  *
597  * Items that can be derived should be implemented as a function that accepts
598  * a Rel_desc argument, and returns the desired data. ld_reloc_sym_name() is
599  * an example of this.
600  *
601  * Lesser used relocation data is kept in an auxiliary block, Rel_aux,
602  * that is only allocated as necessary. In exchange for adding one pointer
603  * of overhead to Rel_desc (rel_aux), most relocations are reduced in size
604  * by the size of Rel_aux. This strategy relies on the data in Rel_aux
605  * being rarely needed --- otherwise it will backfire badly.
606  *
607  * Note that rel_raddend is primarily only of interest to RELA relocations,
608  * and is set to 0 for REL. However, there is an exception: If FLG_REL_NADDEND
609  * is set, then rel_raddend contains a replacement value for the implicit
610  * addend found in the relocation target.
611  *
612  * Fields should be ordered from largest to smallest, to minimize packing
613  * holes in the struct layout.
614  */
615 struct rel_desc {
616 	Is_desc		*rel_isdesc;	/* input section reloc is against */
617 	Sym_desc	*rel_sym;	/* sym relocation is against */
618 	Rel_aux		*rel_aux;	/* NULL, or auxiliary data */
619 	Xword		rel_roffset;	/* relocation offset */
620 	Sxword		rel_raddend;	/* addend from input relocation */
621 	Word		rel_flags;	/* misc. flags for relocations */
622 	Word		rel_rtype;	/* relocation type */
623 };
624 
625 /*
626  * Data that would be kept in Rel_desc if the size of that structure was
627  * not an issue. This auxiliary block is only allocated as needed,
628  * and must only contain rarely needed items. The goal is for the vast
629  * majority of Rel_desc structs to not have an auxiliary block.
630  *
631  * When a Rel_desc does not have an auxiliary block, a default value
632  * is assumed for each auxiliary item:
633  *
634  * -	ra_osdesc:
635  *	Output section to which relocation applies. The default
636  *	value for this is the output section associated with the
637  *	input section (rel_isdesc->is_osdesc), or NULL if there
638  *	is no associated input section.
639  *
640  * -	ra_usym:
641  *	If the symbol associated with a relocation is part of a weak/strong
642  *	pair, then ra_usym contains the strong symbol and rel_sym the weak.
643  *	Otherwise, the default value is the same value as rel_sym.
644  *
645  * -	ra_move:
646  *	Move table data. The default value is NULL.
647  *
648  * -	ra_typedata:
649  *	ELF_R_TYPE_DATA(info). This value applies only to a small
650  *	subset of 64-bit sparc relocations, and is otherwise 0. The
651  *	default value is 0.
652  *
653  * If any value in Rel_aux is non-default, then an auxiliary block is
654  * necessary, and each field contains its actual value. If all the auxiliary
655  * values are default, no Rel_aux is needed, and the RELAUX_GET_xxx()
656  * macros below are able to supply the proper default.
657  *
658  * To set a Rel_aux value, use the ld_reloc_set_aux_XXX() functions.
659  * These functions are written to avoid unnecessary auxiliary allocations,
660  * and know the rules for each item.
661  */
662 struct rel_aux {
663 	Os_desc		*ra_osdesc;	/* output section reloc is against */
664 	Sym_desc	*ra_usym;	/* strong sym if this is a weak pair */
665 	Mv_reloc	*ra_move;	/* move table information */
666 	Word		ra_typedata;	/* ELF_R_TYPE_DATA(info) */
667 };
668 
669 /*
670  * Test a given auxiliary value to determine if it has the default value
671  * for that item, as described above. If all the auxiliary items have
672  * their default values, no auxiliary place is necessary to represent them.
673  * If any one of them is non-default, the auxiliary block is needed.
674  */
675 #define	RELAUX_ISDEFAULT_MOVE(_rdesc, _mv) (_mv == NULL)
676 #define	RELAUX_ISDEFAULT_USYM(_rdesc, _usym) ((_rdesc)->rel_sym == _usym)
677 #define	RELAUX_ISDEFAULT_OSDESC(_rdesc, _osdesc) \
678 	((((_rdesc)->rel_isdesc == NULL) && (_osdesc == NULL)) || \
679 	((_rdesc)->rel_isdesc && ((_rdesc)->rel_isdesc->is_osdesc == _osdesc)))
680 #define	RELAUX_ISDEFAULT_TYPEDATA(_rdesc, _typedata) (_typedata == 0)
681 
682 /*
683  * Retrieve the value of an auxiliary relocation item, preserving the illusion
684  * that every relocation descriptor has an auxiliary block attached. The
685  * real implementation is that an auxiliary block is only present if one or
686  * more auxiliary items have non-default values. These macros return the true
687  * value if an auxiliary block is present, and the default value for the
688  * item otherwise.
689  */
690 #define	RELAUX_GET_MOVE(_rdesc) \
691 	((_rdesc)->rel_aux ? (_rdesc)->rel_aux->ra_move : NULL)
692 #define	RELAUX_GET_USYM(_rdesc) \
693 	((_rdesc)->rel_aux ? (_rdesc)->rel_aux->ra_usym : (_rdesc)->rel_sym)
694 #define	RELAUX_GET_OSDESC(_rdesc) \
695 	((_rdesc)->rel_aux ? (_rdesc)->rel_aux->ra_osdesc : \
696 	((_rdesc)->rel_isdesc ? (_rdesc)->rel_isdesc->is_osdesc : NULL))
697 #define	RELAUX_GET_TYPEDATA(_rdesc) \
698 	((_rdesc)->rel_aux ? (_rdesc)->rel_aux->ra_typedata : 0)
699 
700 /*
701  * common flags used on the Rel_desc structure (defined in machrel.h).
702  */
703 #define	FLG_REL_GOT	0x00000001	/* relocation against GOT */
704 #define	FLG_REL_PLT	0x00000002	/* relocation against PLT */
705 #define	FLG_REL_BSS	0x00000004	/* relocation against BSS */
706 #define	FLG_REL_LOAD	0x00000008	/* section loadable */
707 #define	FLG_REL_SCNNDX	0x00000010	/* use section index for symbol ndx */
708 #define	FLG_REL_CLVAL	0x00000020	/* clear VALUE for active relocation */
709 #define	FLG_REL_ADVAL	0x00000040	/* add VALUE for output relocation, */
710 					/*	only relevant to SPARC and */
711 					/*	R_SPARC_RELATIVE */
712 #define	FLG_REL_GOTCL	0x00000080	/* clear the GOT entry.  This is */
713 					/* relevant to RELA relocations, */
714 					/* not REL (i386) relocations */
715 #define	FLG_REL_MOVETAB	0x00000100	/* Relocation against .SUNW_move */
716 					/*	adjustments required before */
717 					/*	actual relocation */
718 #define	FLG_REL_NOINFO	0x00000200	/* Relocation comes from a section */
719 					/*	with a null sh_info field */
720 #define	FLG_REL_REG	0x00000400	/* Relocation target is reg sym */
721 #define	FLG_REL_FPTR	0x00000800	/* relocation against func. desc. */
722 #define	FLG_REL_RFPTR1	0x00001000	/* Relative relocation against */
723 					/*   1st part of FD */
724 #define	FLG_REL_RFPTR2	0x00002000	/* Relative relocation against */
725 					/*   2nd part of FD */
726 #define	FLG_REL_DISP	0x00004000	/* *disp* relocation */
727 #define	FLG_REL_STLS	0x00008000	/* IE TLS reference to */
728 					/*	static TLS GOT index */
729 #define	FLG_REL_DTLS	0x00010000	/* GD TLS reference relative to */
730 					/*	dynamic TLS GOT index */
731 #define	FLG_REL_MTLS	0x00020000	/* LD TLS reference against GOT */
732 #define	FLG_REL_STTLS	0x00040000	/* LE TLS reference directly */
733 					/*	to static tls index */
734 #define	FLG_REL_TLSFIX	0x00080000	/* relocation points to TLS instr. */
735 					/*	which needs updating */
736 #define	FLG_REL_RELA	0x00100000	/* descriptor captures a Rela */
737 #define	FLG_REL_GOTFIX	0x00200000	/* relocation points to GOTOP instr. */
738 					/*	which needs updating */
739 #define	FLG_REL_NADDEND	0x00400000	/* Replace implicit addend in dest */
740 					/*	with value in rel_raddend */
741 					/*	Relevant to REL (i386) */
742 					/*	relocations, not to RELA. */
743 
744 /*
745  * We often need the name of the symbol contained in a relocation descriptor
746  * for diagnostic or error output. This is usually the symbol name, but
747  * we substitute a constructed name in some cases. Hence, the name is
748  * generated on the fly by a private function within libld. This is the
749  * prototype for that function.
750  */
751 typedef const char *(* rel_desc_sname_func_t)(Rel_desc *);
752 
753 /*
754  * Header for a relocation descriptor cache buffer.
755  */
756 struct rel_cachebuf {
757 	Rel_desc	*rc_end;
758 	Rel_desc	*rc_free;
759 	Rel_desc	rc_arr[1];
760 };
761 
762 /*
763  * Header for a relocation auxiliary descriptor cache buffer.
764  */
765 struct rel_aux_cachebuf {
766 	Rel_aux		*rac_end;
767 	Rel_aux		*rac_free;
768 	Rel_aux		rac_arr[1];
769 };
770 
771 /*
772  * Convenience macro for traversing every relocation descriptor found within
773  * a given relocation cache, transparently handling the cache buffers and
774  * skipping any unallocated descriptors within the buffers.
775  *
776  * entry:
777  *	_rel_cache - Relocate descriptor cache (Rel_cache) to traverse
778  *	_idx - Aliste index variable for use by the macro
779  *	_rcbp - Cache buffer pointer, for use by the macro
780  *	_orsp - Rel_desc pointer, which will take on the value of a different
781  *		relocation descriptor in the cache in each iteration.
782  *
783  * The caller must not assign new values to _idx, _rcbp, or _orsp within
784  * the scope of REL_CACHE_TRAVERSE.
785  */
786 #define	REL_CACHE_TRAVERSE(_rel_cache, _idx, _rcbp, _orsp) \
787 	for (APLIST_TRAVERSE((_rel_cache)->rc_list, _idx, _rcbp)) \
788 		for (_orsp = _rcbp->rc_arr; _orsp < _rcbp->rc_free; _orsp++)
789 
790 /*
791  * Symbol value descriptor.  For relocatable objects, each symbols value is
792  * its offset within its associated section.  Therefore, to uniquely define
793  * each symbol within a relocatable object, record and sort the sh_offset and
794  * symbol value.  This information is used to search for displacement
795  * relocations as part of copy relocation validation.
796  */
797 typedef struct {
798 	Addr		ssv_value;
799 	Sym_desc	*ssv_sdp;
800 } Ssv_desc;
801 
802 /*
803  * Input file processing structures.
804  */
805 struct ifl_desc {			/* input file descriptor */
806 	const char	*ifl_name;	/* full file name */
807 	const char	*ifl_soname;	/* shared object name */
808 	dev_t		ifl_stdev;	/* device id and inode number for .so */
809 	ino_t		ifl_stino;	/*	multiple inclusion checks */
810 	Ehdr		*ifl_ehdr;	/* elf header describing this file */
811 	Elf		*ifl_elf;	/* elf descriptor for this file */
812 	Sym_desc	**ifl_oldndx;	/* original symbol table indices */
813 	Sym_desc	*ifl_locs;	/* symbol desc version of locals */
814 	Ssv_desc	*ifl_sortsyms;	/* sorted list of symbols by value */
815 	Word		ifl_locscnt;	/* no. of local symbols to process */
816 	Word		ifl_symscnt;	/* total no. of symbols to process */
817 	Word		ifl_sortcnt;	/* no. of sorted symbols to process */
818 	Word		ifl_shnum;	/* number of sections in file */
819 	Word		ifl_shstrndx;	/* index to .shstrtab */
820 	Word		ifl_vercnt;	/* number of versions in file */
821 	Half		ifl_neededndx;	/* index to NEEDED in .dyn section */
822 	Word		ifl_flags;	/* explicit/implicit reference */
823 	Is_desc		**ifl_isdesc;	/* isdesc[scn ndx] = Is_desc ptr */
824 	Sdf_desc	*ifl_sdfdesc;	/* control definition */
825 	Versym		*ifl_versym;	/* version symbol table array */
826 	Ver_index	*ifl_verndx;	/* verndx[ver ndx] = Ver_index */
827 	APlist		*ifl_verdesc;	/* version descriptor list */
828 	APlist		*ifl_relsect;	/* relocation section list */
829 	Alist		*ifl_groups;	/* SHT_GROUP section list */
830 	Cap_desc	*ifl_caps;	/* capabilities descriptor */
831 };
832 
833 #define	FLG_IF_CMDLINE	0x00000001	/* full filename specified from the */
834 					/*	command line (no -l) */
835 #define	FLG_IF_NEEDED	0x00000002	/* shared object should be recorded */
836 #define	FLG_IF_DIRECT	0x00000004	/* establish direct bindings to this */
837 					/*	object */
838 #define	FLG_IF_EXTRACT	0x00000008	/* file extracted from an archive */
839 #define	FLG_IF_VERNEED	0x00000010	/* version dependency information is */
840 					/*	required */
841 #define	FLG_IF_DEPREQD	0x00000020	/* dependency is required to satisfy */
842 					/*	symbol references */
843 #define	FLG_IF_NEEDSTR	0x00000040	/* dependency specified by -Nn */
844 					/*	flag */
845 #define	FLG_IF_IGNORE	0x00000080	/* ignore unused dependencies */
846 #define	FLG_IF_NODIRECT	0x00000100	/* object contains symbols that */
847 					/*	cannot be directly bound to */
848 #define	FLG_IF_LAZYLD	0x00000200	/* dependency should be lazy loaded */
849 #define	FLG_IF_GRPPRM	0x00000400	/* dependency establishes a group */
850 #define	FLG_IF_DISPPEND 0x00000800	/* displacement relocation done */
851 					/*	in the ld time. */
852 #define	FLG_IF_DISPDONE 0x00001000	/* displacement relocation done */
853 					/* 	at the run time */
854 #define	FLG_IF_MAPFILE	0x00002000	/* file is a mapfile */
855 #define	FLG_IF_HSTRTAB	0x00004000	/* file has a string section */
856 #define	FLG_IF_FILEREF	0x00008000	/* file contains a section which */
857 					/*	is included in the output */
858 					/*	allocatable image */
859 #define	FLG_IF_GNUVER	0x00010000	/* file used GNU-style versioning */
860 #define	FLG_IF_ORDERED	0x00020000	/* ordered section processing */
861 					/*	required */
862 #define	FLG_IF_OTOSCAP	0x00040000	/* convert object capabilities to */
863 					/*	symbol capabilities */
864 #define	FLG_IF_DEFERRED	0x00080000	/* dependency is deferred */
865 
866 /*
867  * Symbol states that require the generation of a DT_POSFLAG_1 .dynamic entry.
868  */
869 #define	MSK_IF_POSFLAG1	(FLG_IF_LAZYLD | FLG_IF_GRPPRM | FLG_IF_DEFERRED)
870 
871 /*
872  * Symbol states that require an associated Syminfo entry.
873  */
874 #define	MSK_IF_SYMINFO	(FLG_IF_LAZYLD | FLG_IF_DIRECT | FLG_IF_DEFERRED)
875 
876 
877 struct is_desc {			/* input section descriptor */
878 	const char	*is_name;	/* original section name */
879 	const char	*is_sym_name;	/* NULL, or name string to use for */
880 					/*	related STT_SECTION symbols */
881 	Shdr		*is_shdr;	/* the elf section header */
882 	Ifl_desc	*is_file;	/* infile desc for this section */
883 	Os_desc		*is_osdesc;	/* new output section for this */
884 					/*	input section */
885 	Elf_Data	*is_indata;	/* input sections raw data */
886 	Is_desc		*is_symshndx;	/* related SHT_SYM_SHNDX section */
887 	Is_desc		*is_comdatkeep;	/* If COMDAT section is discarded, */
888 					/* 	this is section that was kept */
889 	Word		is_scnndx;	/* original section index in file */
890 	Word		is_ordndx;	/* index for section.  Used to decide */
891 					/*	where to insert section when */
892 					/* 	reordering sections */
893 	Word		is_keyident;	/* key for SHF_{ORDERED|LINK_ORDER} */
894 					/*	processing and ident used for */
895 					/*	 placing/ordering sections */
896 	Word		is_flags;	/* Various flags */
897 };
898 
899 #define	FLG_IS_ORDERED	0x0001		/* this is a SHF_ORDERED section */
900 #define	FLG_IS_KEY	0x0002		/* section requires sort keys */
901 #define	FLG_IS_DISCARD	0x0004		/* section is to be discarded */
902 #define	FLG_IS_RELUPD	0x0008		/* symbol defined here may have moved */
903 #define	FLG_IS_SECTREF	0x0010		/* section has been referenced */
904 #define	FLG_IS_GDATADEF	0x0020		/* section contains global data sym */
905 #define	FLG_IS_EXTERNAL	0x0040		/* isp from a user file */
906 #define	FLG_IS_INSTRMRG	0x0080		/* Usable SHF_MERGE|SHF_STRINGS sec */
907 #define	FLG_IS_GNSTRMRG	0x0100		/* Generated mergeable string section */
908 #define	FLG_IS_GROUPS	0x0200		/* section has groups to process */
909 #define	FLG_IS_PLACE	0x0400		/* section requires to be placed */
910 #define	FLG_IS_COMDAT	0x0800		/* section is COMDAT */
911 #define	FLG_IS_EHFRAME	0x1000		/* section is .eh_frame */
912 
913 /*
914  * Output sections contain lists of input sections that are assigned to them.
915  * These items fall into 4 categories:
916  *	BEFORE - Ordered sections that specify SHN_BEFORE, in input order.
917  *	ORDERED - Ordered sections that are sorted using unsorted sections
918  *		as the sort key.
919  *	DEFAULT - Sections that are placed into the output section
920  *		in input order.
921  *	AFTER - Ordered sections that specify SHN_AFTER, in input order.
922  */
923 #define	OS_ISD_BEFORE	0
924 #define	OS_ISD_ORDERED	1
925 #define	OS_ISD_DEFAULT	2
926 #define	OS_ISD_AFTER	3
927 #define	OS_ISD_NUM	4
928 typedef APlist *os_isdecs_arr[OS_ISD_NUM];
929 
930 /*
931  * Convenience macro for traversing every input section associated
932  * with a given output section. The primary benefit of this macro
933  * is that it preserves a precious level of code indentation in the
934  * code that uses it.
935  */
936 #define	OS_ISDESCS_TRAVERSE(_list_idx, _osp, _idx, _isp) \
937 	for (_list_idx = 0; _list_idx < OS_ISD_NUM; _list_idx++) \
938 		for (APLIST_TRAVERSE(_osp->os_isdescs[_list_idx], _idx, _isp))
939 
940 
941 /*
942  * Map file and output file processing structures
943  */
944 struct os_desc {			/* Output section descriptor */
945 	const char	*os_name;	/* the section name */
946 	Elf_Scn		*os_scn;	/* the elf section descriptor */
947 	Shdr		*os_shdr;	/* the elf section header */
948 	Os_desc		*os_relosdesc;	/* the output relocation section */
949 	APlist		*os_relisdescs;	/* reloc input section descriptors */
950 					/*	for this output section */
951 	os_isdecs_arr	os_isdescs;	/* lists of input sections in output */
952 	APlist		*os_mstrisdescs; /* FLG_IS_INSTRMRG input sections */
953 	Sg_desc		*os_sgdesc;	/* segment os_desc is placed on */
954 	Elf_Data	*os_outdata;	/* output sections raw data */
955 	avl_tree_t	*os_comdats;	/* AVL tree of COMDAT input sections */
956 					/*	associated to output section */
957 	Word		os_identndx;	/* section identifier for input */
958 					/*	section processing, followed */
959 					/*	by section symbol index */
960 	Word		os_ordndx;	/* index for section.  Used to decide */
961 					/*	where to insert section when */
962 					/* 	reordering sections */
963 	Xword		os_szoutrels;	/* size of output relocation section */
964 	uint_t		os_namehash;	/* hash on section name */
965 	uchar_t		os_flags;	/* various flags */
966 };
967 
968 #define	FLG_OS_KEY		0x01	/* section requires sort keys */
969 #define	FLG_OS_OUTREL		0x02	/* output rel against this section */
970 #define	FLG_OS_SECTREF		0x04	/* isps are not affected by -zignore */
971 #define	FLG_OS_EHFRAME		0x08	/* section is .eh_frame */
972 
973 /*
974  * The sg_id field of the segment descriptor is used to establish the default
975  * order for program headers and segments in the output object. Segments are
976  * ordered according to the following SGID values that classify them based on
977  * their attributes. The initial set of built in segments are in this order,
978  * and new mapfile defined segments are inserted into these groups. Within a
979  * given SGID group, the position of new segments depends on the syntax
980  * version of the mapfile that creates them. Version 1 (original sysv)
981  * mapfiles place the new segment at the head of their group (reverse creation
982  * order). The newer syntax places them at the end, following the others
983  * (creation order).
984  *
985  * Note that any new segments must always be added after PT_PHDR and
986  * PT_INTERP (refer Generic ABI, Page 5-4).
987  */
988 #define	SGID_PHDR	0	/* PT_PHDR */
989 #define	SGID_INTERP	1	/* PT_INTERP */
990 #define	SGID_SUNWCAP	2	/* PT_SUNWCAP */
991 #define	SGID_TEXT	3	/* PT_LOAD */
992 #define	SGID_DATA	4	/* PT_LOAD */
993 #define	SGID_BSS	5	/* PT_LOAD */
994 #if	defined(_ELF64)
995 #define	SGID_LRODATA	6	/* PT_LOAD (amd64-only) */
996 #define	SGID_LDATA	7	/* PT_LOAD (amd64-only) */
997 #endif
998 #define	SGID_TEXT_EMPTY	8	/* PT_LOAD, reserved (?E in version 1 syntax) */
999 #define	SGID_NULL_EMPTY	9	/* PT_NULL, reserved (?E in version 1 syntax) */
1000 #define	SGID_DYN	10	/* PT_DYNAMIC */
1001 #define	SGID_DTRACE	11	/* PT_SUNWDTRACE */
1002 #define	SGID_TLS	12	/* PT_TLS */
1003 #define	SGID_UNWIND	13	/* PT_SUNW_UNWIND */
1004 #define	SGID_SUNWSTACK	14	/* PT_SUNWSTACK */
1005 #define	SGID_NOTE	15	/* PT_NOTE */
1006 #define	SGID_NULL	16	/* PT_NULL,  mapfile defined empty phdr slots */
1007 				/*	for use by post processors */
1008 #define	SGID_EXTRA	17	/* PT_NULL (final catchall) */
1009 
1010 typedef Half sg_flags_t;
1011 struct sg_desc {			/* output segment descriptor */
1012 	Word		sg_id;		/* segment identifier (for sorting) */
1013 	Phdr		sg_phdr;	/* segment header for output file */
1014 	const char	*sg_name;	/* segment name for PT_LOAD, PT_NOTE, */
1015 					/*	and PT_NULL, otherwise NULL */
1016 	Xword		sg_round;	/* data rounding required (mapfile) */
1017 	Xword		sg_length;	/* maximum segment length; if 0 */
1018 					/*	segment is not specified */
1019 	APlist		*sg_osdescs;	/* list of output section descriptors */
1020 	APlist		*sg_is_order;	/* list of entry criteria */
1021 					/*	giving input section order */
1022 	Alist		*sg_os_order;	/* list specifying output section */
1023 					/*	ordering for the segment */
1024 	sg_flags_t	sg_flags;
1025 	APlist		*sg_sizesym;	/* size symbols for this segment */
1026 	Xword		sg_align;	/* LCM of sh_addralign */
1027 	Elf_Scn		*sg_fscn;	/* the SCN of the first section. */
1028 	avl_node_t	sg_avlnode;	/* AVL book-keeping */
1029 };
1030 
1031 #define	FLG_SG_P_VADDR		0x0001	/* p_vaddr segment attribute set */
1032 #define	FLG_SG_P_PADDR		0x0002	/* p_paddr segment attribute set */
1033 #define	FLG_SG_LENGTH		0x0004	/* length segment attribute set */
1034 #define	FLG_SG_P_ALIGN		0x0008	/* p_align segment attribute set */
1035 #define	FLG_SG_ROUND		0x0010	/* round segment attribute set */
1036 #define	FLG_SG_P_FLAGS		0x0020	/* p_flags segment attribute set */
1037 #define	FLG_SG_P_TYPE		0x0040	/* p_type segment attribute set */
1038 #define	FLG_SG_IS_ORDER		0x0080	/* input section ordering is required */
1039 					/* 	for this segment. */
1040 #define	FLG_SG_NOHDR		0x0100	/* don't map ELF or phdrs into */
1041 					/*	this segment */
1042 #define	FLG_SG_EMPTY		0x0200	/* an empty segment specification */
1043 					/*	no input sections will be */
1044 					/*	associated to this section */
1045 #define	FLG_SG_KEY		0x0400	/* segment requires sort keys */
1046 #define	FLG_SG_NODISABLE	0x0800	/* FLG_SG_DISABLED is not allowed on */
1047 					/*	this segment */
1048 #define	FLG_SG_DISABLED		0x1000	/* this segment is disabled */
1049 #define	FLG_SG_PHREQ		0x2000	/* this segment requires a program */
1050 					/* header */
1051 #define	FLG_SG_ORDERED		0x4000	/* SEGMENT_ORDER segment */
1052 
1053 struct sec_order {
1054 	const char	*sco_secname;	/* section name to be ordered */
1055 	Half		sco_flags;
1056 };
1057 
1058 #define	FLG_SGO_USED	0x0001		/* was ordering used? */
1059 
1060 typedef Half ec_flags_t;
1061 struct ent_desc {			/* input section entrance criteria */
1062 	const char	*ec_name;	/* entrace criteria name, or NULL */
1063 	Alist		*ec_files;	/* files from which to accept */
1064 					/*	sections */
1065 	const char	*ec_is_name;	/* input section name to match */
1066 					/*	(NULL if none) */
1067 	Word		ec_type;	/* section type */
1068 	Word		ec_attrmask;	/* section attribute mask (AWX) */
1069 	Word		ec_attrbits;	/* sections attribute bits */
1070 	Sg_desc		*ec_segment;	/* output segment to enter if matched */
1071 	Word		ec_ordndx;	/* index to determine where section */
1072 					/*	meeting this criteria should */
1073 					/*	inserted. Used for reordering */
1074 					/*	of sections. */
1075 	ec_flags_t	ec_flags;
1076 	avl_node_t	ec_avlnode;	/* AVL book-keeping */
1077 };
1078 
1079 #define	FLG_EC_BUILTIN	0x0001		/* built in descriptor */
1080 #define	FLG_EC_USED	0x0002		/* entrance criteria met? */
1081 #define	FLG_EC_CATCHALL	0x0004		/* Catches any section */
1082 
1083 /*
1084  * Ent_desc_file is the type of element maintained in the ec_files Alist
1085  * of an entrance criteria descriptor. Each item maintains one file
1086  * path, and a set of flags that specify the type of comparison it implies,
1087  * and other information about it. The comparison type is maintained in
1088  * the bottom byte of the flags.
1089  */
1090 #define	TYP_ECF_MASK		0x00ff  /* Comparison type mask */
1091 #define	TYP_ECF_PATH		0	/* Compare to file path */
1092 #define	TYP_ECF_BASENAME	1	/* Compare to file basename */
1093 #define	TYP_ECF_OBJNAME		2	/* Compare to regular file basename, */
1094 					/*	 or to archive member name */
1095 #define	TYP_ECF_NUM		3
1096 
1097 #define	FLG_ECF_ARMEMBER	0x0100	/* name includes archive member */
1098 
1099 typedef struct {
1100 	Word		edf_flags;	/* Type of comparison */
1101 	const char	*edf_name;	/* String to compare to */
1102 	size_t		edf_name_len;	/* strlen(edf_name) */
1103 } Ent_desc_file;
1104 
1105 /*
1106  * One structure is allocated for a move entry, and associated to the symbol
1107  * against which a move is targeted.
1108  */
1109 typedef struct {
1110 	Move		*md_move;	/* original Move entry */
1111 	Xword		md_start;	/* start position */
1112 	Xword		md_len;		/* length of initialization */
1113 	Word 		md_oidx;	/* output Move entry index */
1114 } Mv_desc;
1115 
1116 /*
1117  * Symbol descriptor.
1118  */
1119 typedef	Lword		sd_flag_t;
1120 struct sym_desc {
1121 	Alist		*sd_GOTndxs;	/* list of associated GOT entries */
1122 	Sym		*sd_sym;	/* pointer to symbol table entry */
1123 	Sym		*sd_osym;	/* copy of the original symbol entry */
1124 					/*	used only for local partial */
1125 	Alist		*sd_move;	/* move information associated with a */
1126 					/*	partially initialized symbol */
1127 	const char	*sd_name;	/* symbols name */
1128 	Ifl_desc	*sd_file;	/* file where symbol is taken */
1129 	Is_desc		*sd_isc;	/* input section of symbol definition */
1130 	Sym_aux		*sd_aux;	/* auxiliary global symbol info. */
1131 	Word		sd_symndx;	/* index in output symbol table */
1132 	Word		sd_shndx;	/* sect. index sym is associated w/ */
1133 	sd_flag_t	sd_flags;	/* state flags */
1134 	Half		sd_ref;		/* reference definition of symbol */
1135 };
1136 
1137 /*
1138  * The auxiliary symbol descriptor contains the additional information (beyond
1139  * the symbol descriptor) required to process global symbols.  These symbols are
1140  * accessed via an internal symbol hash table where locality of reference is
1141  * important for performance.
1142  */
1143 struct sym_aux {
1144 	APlist 		*sa_dfiles;	/* files where symbol is defined */
1145 	Sym		sa_sym;		/* copy of symtab entry */
1146 	const char	*sa_vfile;	/* first unavailable definition */
1147 	const char	*sa_rfile;	/* file with first symbol referenced */
1148 	Word		sa_hash;	/* the pure hash value of symbol */
1149 	Word		sa_PLTndx;	/* index into PLT for symbol */
1150 	Word		sa_PLTGOTndx;	/* GOT entry indx for PLT indirection */
1151 	Word		sa_linkndx;	/* index of associated symbol from */
1152 					/*	ET_DYN file */
1153 	Half		sa_symspec;	/* special symbol ids */
1154 	Half		sa_overndx;	/* output file versioning index */
1155 	Half		sa_dverndx;	/* dependency versioning index */
1156 };
1157 
1158 /*
1159  * Nodes used to track symbols in the global AVL symbol dictionary.
1160  */
1161 struct sym_avlnode {
1162 	avl_node_t	sav_node;	/* AVL node */
1163 	Word		sav_hash;	/* symbol hash value */
1164 	const char	*sav_name;	/* symbol name */
1165 	Sym_desc	*sav_sdp;	/* symbol descriptor */
1166 };
1167 
1168 /*
1169  * These are the ids for processing of `Special symbols'.  They are used
1170  * to set the sym->sd_aux->sa_symspec field.
1171  */
1172 #define	SDAUX_ID_ETEXT	1		/* etext && _etext symbol */
1173 #define	SDAUX_ID_EDATA	2		/* edata && _edata symbol */
1174 #define	SDAUX_ID_END	3		/* end, _end, && _END_ symbol */
1175 #define	SDAUX_ID_DYN	4		/* DYNAMIC && _DYNAMIC symbol */
1176 #define	SDAUX_ID_PLT	5		/* _PROCEDURE_LINKAGE_TABLE_ symbol */
1177 #define	SDAUX_ID_GOT	6		/* _GLOBAL_OFFSET_TABLE_ symbol */
1178 #define	SDAUX_ID_START	7		/* START_ && _START_ symbol */
1179 
1180 /*
1181  * Flags for sym_desc.sd_flags
1182  */
1183 #define	FLG_SY_MVTOCOMM	0x00000001	/* assign symbol to common (.bss) */
1184 					/*	this is a result of a */
1185 					/*	copy reloc against sym */
1186 #define	FLG_SY_GLOBREF	0x00000002	/* a global reference has been seen */
1187 #define	FLG_SY_WEAKDEF	0x00000004	/* a weak definition has been used */
1188 #define	FLG_SY_CLEAN	0x00000008	/* `Sym' entry points to original */
1189 					/*	input file (read-only). */
1190 #define	FLG_SY_UPREQD	0x00000010	/* symbol value update is required, */
1191 					/*	either it's used as an entry */
1192 					/*	point or for relocation, but */
1193 					/*	it must be updated even if */
1194 					/*	the -s flag is in effect */
1195 #define	FLG_SY_NOTAVAIL	0x00000020	/* symbol is not available to the */
1196 					/*	application either because it */
1197 					/*	originates from an implicitly */
1198 					/* 	referenced shared object, or */
1199 					/*	because it is not part of a */
1200 					/*	specified version. */
1201 #define	FLG_SY_REDUCED	0x00000040	/* a global is reduced to local */
1202 #define	FLG_SY_VERSPROM	0x00000080	/* version definition has been */
1203 					/*	promoted to output file */
1204 #define	FLG_SY_PROT	0x00000100	/* stv_protected visibility seen */
1205 #define	FLG_SY_MAPREF	0x00000200	/* symbol reference generated by user */
1206 					/*	from mapfile */
1207 #define	FLG_SY_REFRSD	0x00000400	/* symbols sd_ref has been raised */
1208 					/* 	due to a copy-relocs */
1209 					/*	weak-strong pairing */
1210 #define	FLG_SY_INTPOSE	0x00000800	/* symbol defines an interposer */
1211 #define	FLG_SY_INVALID	0x00001000	/* unwanted/erroneous symbol */
1212 #define	FLG_SY_SMGOT	0x00002000	/* small got index assigned to symbol */
1213 					/*	sparc only */
1214 #define	FLG_SY_PARENT	0x00004000	/* symbol to be found in parent */
1215 					/*    only used with direct bindings */
1216 #define	FLG_SY_LAZYLD	0x00008000	/* symbol to cause lazyloading of */
1217 					/*	parent object */
1218 #define	FLG_SY_ISDISC	0x00010000	/* symbol is a member of a DISCARDED */
1219 					/*	section (COMDAT) */
1220 #define	FLG_SY_PAREXPN	0x00020000	/* partially init. symbol to be */
1221 					/*	expanded */
1222 #define	FLG_SY_PLTPAD	0x00040000	/* pltpadding has been allocated for */
1223 					/*	this symbol */
1224 #define	FLG_SY_REGSYM	0x00080000	/* REGISTER symbol (sparc only) */
1225 #define	FLG_SY_SOFOUND	0x00100000	/* compared against an SO definition */
1226 #define	FLG_SY_EXTERN	0x00200000	/* symbol is external, allows -zdefs */
1227 					/*    error suppression */
1228 #define	FLG_SY_MAPUSED	0x00400000	/* mapfile symbol used (occurred */
1229 					/*    within a relocatable object) */
1230 #define	FLG_SY_COMMEXP	0x00800000	/* COMMON symbol which has been */
1231 					/*	allocated */
1232 #define	FLG_SY_CMDREF	0x01000000	/* symbol was referenced from the */
1233 					/*	command line.  (ld -u <>, */
1234 					/*	ld -zrtldinfo=<>, ...) */
1235 #define	FLG_SY_SPECSEC	0x02000000	/* section index is reserved value */
1236 					/*	ABS, COMMON, ... */
1237 #define	FLG_SY_TENTSYM	0x04000000	/* tentative symbol */
1238 #define	FLG_SY_VISIBLE	0x08000000	/* symbols visibility determined */
1239 #define	FLG_SY_STDFLTR	0x10000000	/* symbol is a standard filter */
1240 #define	FLG_SY_AUXFLTR	0x20000000	/* symbol is an auxiliary filter */
1241 #define	FLG_SY_DYNSORT	0x40000000	/* req. in dyn[sym|tls]sort section */
1242 #define	FLG_SY_NODYNSORT 0x80000000	/* excluded from dyn[sym_tls]sort sec */
1243 
1244 #define	FLG_SY_DEFAULT	0x0000100000000	/* global symbol, default */
1245 #define	FLG_SY_SINGLE	0x0000200000000	/* global symbol, singleton defined */
1246 #define	FLG_SY_PROTECT	0x0000400000000	/* global symbol, protected defined */
1247 #define	FLG_SY_EXPORT	0x0000800000000	/* global symbol, exported defined */
1248 
1249 #define	MSK_SY_GLOBAL \
1250 	(FLG_SY_DEFAULT | FLG_SY_SINGLE | FLG_SY_PROTECT | FLG_SY_EXPORT)
1251 					/* this mask indicates that the */
1252 					/*    symbol has been explicitly */
1253 					/*    defined within a mapfile */
1254 					/*    definition, and is a candidate */
1255 					/*    for versioning */
1256 
1257 #define	FLG_SY_HIDDEN	0x0001000000000	/* global symbol, reduce to local */
1258 #define	FLG_SY_ELIM	0x0002000000000	/* global symbol, eliminate */
1259 #define	FLG_SY_IGNORE	0x0004000000000	/* global symbol, ignored */
1260 
1261 #define	MSK_SY_LOCAL	(FLG_SY_HIDDEN | FLG_SY_ELIM | FLG_SY_IGNORE)
1262 					/* this mask allows all local state */
1263 					/*    flags to be removed when the */
1264 					/*    symbol is copy relocated */
1265 
1266 #define	FLG_SY_EXPDEF	0x0008000000000	/* symbol visibility defined */
1267 					/*    explicitly */
1268 
1269 #define	MSK_SY_NOAUTO	(FLG_SY_SINGLE | FLG_SY_EXPORT | FLG_SY_EXPDEF)
1270 					/* this mask indicates that the */
1271 					/*    symbol is not a candidate for */
1272 					/*    auto-reduction/elimination */
1273 
1274 #define	FLG_SY_MAPFILE	0x0010000000000	/* symbol attribute defined in a */
1275 					/*    mapfile */
1276 #define	FLG_SY_DIR	0x0020000000000	/* global symbol, direct bindings */
1277 #define	FLG_SY_NDIR	0x0040000000000	/* global symbol, nondirect bindings */
1278 #define	FLG_SY_OVERLAP	0x0080000000000	/* move entry overlap detected */
1279 #define	FLG_SY_CAP	0x0100000000000	/* symbol is associated with */
1280 					/*    capabilities */
1281 #define	FLG_SY_DEFERRED	0x0200000000000	/* symbol should not be bound to */
1282 					/*	during BIND_NOW relocations */
1283 
1284 /*
1285  * A symbol can only be truly hidden if it is not a capabilities symbol.
1286  */
1287 #define	SYM_IS_HIDDEN(_sdp) \
1288 	(((_sdp)->sd_flags & (FLG_SY_HIDDEN | FLG_SY_CAP)) == FLG_SY_HIDDEN)
1289 
1290 /*
1291  * Create a mask for (sym.st_other & visibility) since the gABI does not yet
1292  * define a ELF*_ST_OTHER macro.
1293  */
1294 #define	MSK_SYM_VISIBILITY	0x7
1295 
1296 /*
1297  * Structure to manage the shared object definition lists.  There are two lists
1298  * that use this structure:
1299  *
1300  *  -	ofl_soneed; maintain the list of implicitly required dependencies
1301  *	(ie. shared objects needed by other shared objects).  These definitions
1302  *	may include RPATH's required to locate the dependencies, and any
1303  *	version requirements.
1304  *
1305  *  -	ofl_socntl; maintains the shared object control definitions.  These are
1306  *	provided by the user (via a mapfile) and are used to indicate any
1307  *	version control requirements.
1308  */
1309 struct	sdf_desc {
1310 	const char	*sdf_name;	/* the shared objects file name */
1311 	char		*sdf_rpath;	/* library search path DT_RPATH */
1312 	const char	*sdf_rfile;	/* referencing file for diagnostics */
1313 	Ifl_desc	*sdf_file;	/* the final input file descriptor */
1314 	Alist		*sdf_vers;	/* list of versions that are required */
1315 					/*	from this object */
1316 	Alist		*sdf_verneed;	/* list of VERNEEDS to create for */
1317 					/*	object via mapfile ADDVERS */
1318 	Word		sdf_flags;
1319 };
1320 
1321 #define	FLG_SDF_SELECT	0x01		/* version control selection required */
1322 #define	FLG_SDF_VERIFY	0x02		/* version definition verification */
1323 					/*	required */
1324 #define	FLG_SDF_ADDVER	0x04		/* add VERNEED references */
1325 
1326 /*
1327  * Structure to manage shared object version usage requirements.
1328  */
1329 struct	sdv_desc {
1330 	const char	*sdv_name;	/* version name */
1331 	const char	*sdv_ref;	/* versions reference */
1332 	Word		sdv_flags;	/* flags */
1333 };
1334 
1335 #define	FLG_SDV_MATCHED	0x01		/* VERDEF found and matched */
1336 
1337 /*
1338  * Structures to manage versioning information.  Two versioning structures are
1339  * defined:
1340  *
1341  *   -	a version descriptor maintains a linked list of versions and their
1342  *	associated dependencies.  This is used to build the version definitions
1343  *	for an image being created (see map_symbol), and to determine the
1344  *	version dependency graph for any input files that are versioned.
1345  *
1346  *   -	a version index array contains each version of an input file that is
1347  *	being processed.  It informs us which versions are available for
1348  *	binding, and is used to generate any version dependency information.
1349  */
1350 struct	ver_desc {
1351 	const char	*vd_name;	/* version name */
1352 	Ifl_desc	*vd_file;	/* file that defined version */
1353 	Word		vd_hash;	/* hash value of name */
1354 	Half		vd_ndx;		/* coordinates with symbol index */
1355 	Half		vd_flags;	/* version information */
1356 	APlist		*vd_deps;	/* version dependencies */
1357 	Ver_desc	*vd_ref;	/* dependency's first reference */
1358 };
1359 
1360 struct	ver_index {
1361 	const char	*vi_name;	/* dependency version name */
1362 	Half		vi_flags;	/* communicates availability */
1363 	Half		vi_overndx;	/* index assigned to this version in */
1364 					/*	output object Verneed section */
1365 	Ver_desc	*vi_desc;	/* cross reference to descriptor */
1366 };
1367 
1368 /*
1369  * Define any internal version descriptor flags ([vd|vi]_flags).  Note that the
1370  * first byte is reserved for user visible flags (refer VER_FLG's in link.h).
1371  */
1372 #define	MSK_VER_USER	0x0f		/* mask for user visible flags */
1373 
1374 #define	FLG_VER_AVAIL	0x10		/* version is available for binding */
1375 #define	FLG_VER_REFER	0x20		/* version has been referenced */
1376 #define	FLG_VER_CYCLIC	0x40		/* a member of cyclic dependency */
1377 
1378 /*
1379  * isalist(1) descriptor - used to break an isalist string into its component
1380  * options.
1381  */
1382 struct	isa_opt {
1383 	char		*isa_name;	/* individual isa option name */
1384 	size_t		isa_namesz;	/*	and associated size */
1385 };
1386 
1387 struct	isa_desc {
1388 	char		*isa_list;	/* sysinfo(SI_ISALIST) list */
1389 	size_t		isa_listsz;	/*	and associated size */
1390 	Isa_opt		*isa_opt;	/* table of individual isa options */
1391 	size_t		isa_optno;	/*	and associated number */
1392 };
1393 
1394 /*
1395  * uname(2) descriptor - used to break a utsname structure into its component
1396  * options (at least those that we're interested in).
1397  */
1398 struct	uts_desc {
1399 	char		*uts_osname;	/* operating system name */
1400 	size_t		uts_osnamesz;	/*	and associated size */
1401 	char		*uts_osrel;	/* operating system release */
1402 	size_t		uts_osrelsz;	/*	and associated size */
1403 };
1404 
1405 /*
1406  * SHT_GROUP descriptor - used to track group sections at the global
1407  * level to resolve conflicts and determine which to keep.
1408  */
1409 struct group_desc {
1410 	Is_desc		*gd_isc;	/* input section descriptor */
1411 	Is_desc		*gd_oisc;	/* overriding input section */
1412 					/*	descriptor when discarded */
1413 	const char	*gd_name;	/* group name (signature symbol) */
1414 	Word		*gd_data;	/* data for group section */
1415 	size_t		gd_cnt;		/* number of entries in group data */
1416 };
1417 
1418 /*
1419  * Indexes into the ld_support_funcs[] table.
1420  */
1421 typedef enum {
1422 	LDS_VERSION = 0,	/* Must be first and have value 0 */
1423 	LDS_INPUT_DONE,
1424 	LDS_START,
1425 	LDS_ATEXIT,
1426 	LDS_OPEN,
1427 	LDS_FILE,
1428 	LDS_INSEC,
1429 	LDS_SEC,
1430 	LDS_NUM
1431 } Support_ndx;
1432 
1433 /*
1434  * Structure to manage archive member caching.  Each archive has an archive
1435  * descriptor (Ar_desc) associated with it.  This contains pointers to the
1436  * archive symbol table (obtained by elf_getarsyms(3e)) and an auxiliary
1437  * structure (Ar_uax[]) that parallels this symbol table.  The member element
1438  * of this auxiliary table indicates whether the archive member associated with
1439  * the symbol offset has already been extracted (AREXTRACTED) or partially
1440  * processed (refer process_member()).
1441  */
1442 typedef struct ar_mem {
1443 	Elf		*am_elf;	/* elf descriptor for this member */
1444 	const char	*am_name;	/* members name */
1445 	const char	*am_path;	/* path (ie. lib(foo.o)) */
1446 	Sym		*am_syms;	/* start of global symbols */
1447 	char		*am_strs;	/* associated string table start */
1448 	Xword		am_symn;	/* no. of global symbols */
1449 } Ar_mem;
1450 
1451 typedef struct ar_aux {
1452 	Sym_desc	*au_syms;	/* internal symbol descriptor */
1453 	Ar_mem		*au_mem;	/* associated member */
1454 } Ar_aux;
1455 
1456 #define	FLG_ARMEM_PROC	(Ar_mem *)-1
1457 
1458 typedef struct ar_desc {
1459 	const char	*ad_name;	/* archive file name */
1460 	Elf		*ad_elf;	/* elf descriptor for the archive */
1461 	Elf_Arsym	*ad_start;	/* archive symbol table start */
1462 	Ar_aux		*ad_aux;	/* auxiliary symbol information */
1463 	dev_t		ad_stdev;	/* device id and inode number for */
1464 	ino_t		ad_stino;	/*	multiple inclusion checks */
1465 	ofl_flag_t	ad_flags;	/* archive specific cmd line flags */
1466 } Ar_desc;
1467 
1468 /*
1469  * Define any archive descriptor flags.  NOTE, make sure they do not clash with
1470  * any output file descriptor archive extraction flags, as these are saved in
1471  * the same entry (see MSK_OF1_ARCHIVE).
1472  */
1473 #define	FLG_ARD_EXTRACT	0x00010000	/* archive member has been extracted */
1474 
1475 /* Mapfile versions supported by libld */
1476 #define	MFV_NONE	0	/* Not a valid version */
1477 #define	MFV_SYSV	1	/* Original System V syntax */
1478 #define	MFV_SOLARIS	2	/* Solaris mapfile syntax */
1479 #define	MFV_NUM		3	/* # of mapfile versions */
1480 
1481 
1482 /*
1483  * Function Declarations.
1484  */
1485 #if	defined(_ELF64)
1486 
1487 #define	ld_create_outfile	ld64_create_outfile
1488 #define	ld_ent_setup		ld64_ent_setup
1489 #define	ld_init_strings		ld64_init_strings
1490 #define	ld_init_target		ld64_init_target
1491 #define	ld_make_sections	ld64_make_sections
1492 #define	ld_main			ld64_main
1493 #define	ld_ofl_cleanup		ld64_ofl_cleanup
1494 #define	ld_process_mem		ld64_process_mem
1495 #define	ld_reloc_init		ld64_reloc_init
1496 #define	ld_reloc_process	ld64_reloc_process
1497 #define	ld_sym_validate		ld64_sym_validate
1498 #define	ld_update_outfile	ld64_update_outfile
1499 
1500 #else
1501 
1502 #define	ld_create_outfile	ld32_create_outfile
1503 #define	ld_ent_setup		ld32_ent_setup
1504 #define	ld_init_strings		ld32_init_strings
1505 #define	ld_init_target		ld32_init_target
1506 #define	ld_make_sections	ld32_make_sections
1507 #define	ld_main			ld32_main
1508 #define	ld_ofl_cleanup		ld32_ofl_cleanup
1509 #define	ld_process_mem		ld32_process_mem
1510 #define	ld_reloc_init		ld32_reloc_init
1511 #define	ld_reloc_process	ld32_reloc_process
1512 #define	ld_sym_validate		ld32_sym_validate
1513 #define	ld_update_outfile	ld32_update_outfile
1514 
1515 #endif
1516 
1517 extern int		ld_getopt(Lm_list *, int, int, char **);
1518 
1519 extern int		ld32_main(int, char **, Half);
1520 extern int		ld64_main(int, char **, Half);
1521 
1522 extern uintptr_t	ld_create_outfile(Ofl_desc *);
1523 extern uintptr_t	ld_ent_setup(Ofl_desc *, Xword);
1524 extern uintptr_t	ld_init_strings(Ofl_desc *);
1525 extern int		ld_init_target(Lm_list *, Half mach);
1526 extern uintptr_t	ld_make_sections(Ofl_desc *);
1527 extern void		ld_ofl_cleanup(Ofl_desc *);
1528 extern Ifl_desc		*ld_process_mem(const char *, const char *, char *,
1529 			    size_t, Ofl_desc *, Rej_desc *);
1530 extern uintptr_t	ld_reloc_init(Ofl_desc *);
1531 extern uintptr_t	ld_reloc_process(Ofl_desc *);
1532 extern uintptr_t	ld_sym_validate(Ofl_desc *);
1533 extern uintptr_t	ld_update_outfile(Ofl_desc *);
1534 
1535 #ifdef	__cplusplus
1536 }
1537 #endif
1538 
1539 #endif	/* _LIBLD_H */
1540