xref: /titanic_50/usr/src/cmd/sgs/include/libld.h (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
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 2007 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #ifndef	_LIBLD_H
31 #define	_LIBLD_H
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #include <stdlib.h>
36 #include <libelf.h>
37 #include <sgs.h>
38 #include <machdep.h>
39 #include <string_table.h>
40 #include <sys/avl.h>
41 #include <alist.h>
42 
43 #ifdef	__cplusplus
44 extern "C" {
45 #endif
46 
47 /*
48  * Default directory search path manipulation for the link-editor.  YLDIR
49  * indicates which directory in LIBPATH is replaced by the -YL option to cc
50  * and ld.  YUDIR indicates which directory is replaced by -YU.
51  */
52 #define	YLDIR	1
53 #define	YUDIR	2
54 
55 /*
56  * Define a hash value that can never be returned from elf_hash().
57  */
58 #define	SYM_NOHASH	(~(Word)0)
59 
60 /*
61  * Macro that can be used to represent both ORDER flags
62  * in a section header.
63  */
64 #define	ALL_SHF_ORDER	(SHF_ORDERED | SHF_LINK_ORDER)
65 
66 /*
67  * The linker merges (concatenates) sections with the same name and
68  * compatible section header flags. When comparing these flags,
69  * there are some that should not be included in the decision.
70  * The ALL_SHF_IGNORE constant defines these flags.
71  *
72  * NOTE: SHF_MERGE|SHF_STRINGS:
73  * The compiler is allowed to set the SHF_MERGE|SHF_STRINGS flags in
74  * order to tell the linker that:
75  *
76  *      1) There is nothing in the section except null terminated strings.
77  *      2) If two compatible sections both have these flags set, it is
78  *		OK to combine identical strings into single instances.
79  *		In this case, the two sections would be modified to both
80  *		reference a single string copy.
81  *
82  * This is a different meaning than the simple concatenating of sections
83  * that the linker always does. It is a hint that an additional optimization
84  * is possible, but not required. This means that sections that do not
85  * share the same SHF_MERGE|SHF_STRINGS values can be merged (concatenated),
86  * but cannot have their duplicate strings combined. Hence, the values
87  * of SHF_MERGE|SHF_STRINGS should be ignored when deciding whether two
88  * sections can be merged (concatenated).
89  *
90  * We do not currently implement the SHF_MERGE|SHF_STRINGS optimization,
91  * but it is possible to add it. If we did, the procedure would be to
92  * first combine the compatible sections that have these flag bits set,
93  * and then to concatenate any others to the result.
94  */
95 #define	ALL_SHF_IGNORE	(ALL_SHF_ORDER | SHF_GROUP | SHF_MERGE | SHF_STRINGS)
96 
97 /*
98  * Define symbol reference types for use in symbol resolution.
99  */
100 typedef enum {
101 	REF_DYN_SEEN,			/* a .so symbol has been seen */
102 	REF_DYN_NEED,			/* a .so symbol satisfies a .o symbol */
103 	REF_REL_NEED,			/* a .o symbol */
104 	REF_NUM				/* the number of symbol references */
105 } Symref;
106 
107 
108 /*
109  * GOT reference models
110  */
111 typedef enum {
112 	GOT_REF_GENERIC,	/* generic symbol reference */
113 	GOT_REF_TLSIE,		/* TLS initial exec (gnu) reference */
114 	GOT_REF_TLSLD,		/* TLS local dynamic reference */
115 	GOT_REF_TLSGD		/* TLS general dynamic reference */
116 } Gotref;
117 
118 typedef struct {
119 	Xword		gn_addend;	/* addend associated with GOT entry */
120 	Sword		gn_gotndx;	/* GOT table index */
121 	Gotref		gn_gotref;
122 } Gotndx;
123 
124 /*
125  * Got debugging structure.  The got index is defined as a signed value as we
126  * do so much mucking around with negative and positive gots on SPARC, and sign
127  * extension is necessary when building 64-bit objects.  On intel we explicitly
128  * cast this variable to an unsigned value.
129  */
130 typedef struct {
131 	Sym_desc *	gt_sym;
132 	Gotndx		gt_gndx;
133 } Gottable;
134 
135 /*
136  * Output file processing structure
137  */
138 struct ofl_desc {
139 	char		*ofl_sgsid;	/* link-editor identification */
140 	const char	*ofl_name;	/* full file name */
141 	Elf		*ofl_elf;	/* elf_memory() elf descriptor */
142 	Elf		*ofl_welf;	/* ELF_C_WRITE elf descriptor */
143 	Ehdr		*ofl_dehdr;	/* default elf header, and new elf */
144 	Ehdr		*ofl_nehdr;	/*	header describing this file */
145 	Phdr		*ofl_phdr;	/* program header descriptor */
146 	Phdr		*ofl_tlsphdr;	/* TLS phdr */
147 	int		ofl_fd;		/* file descriptor */
148 	size_t		ofl_size;	/* image size */
149 	List		ofl_maps;	/* list of input mapfiles */
150 	List		ofl_segs;	/* list of segments */
151 	List		ofl_ents;	/* list of entrance descriptors */
152 	List		ofl_objs;	/* relocatable object file list */
153 	Word		ofl_objscnt;	/* 	and count */
154 	List		ofl_ars;	/* archive library list */
155 	Word		ofl_arscnt;	/* 	and count */
156 	List		ofl_sos;	/* shared object list */
157 	Word		ofl_soscnt;	/* 	and count */
158 	List		ofl_soneed;	/* list of implicitly required .so's */
159 	List		ofl_socntl;	/* list of .so control definitions */
160 	List		ofl_outrels;	/* list of output relocations */
161 	Word		ofl_outrelscnt;	/* 	and count */
162 	List		ofl_actrels;	/* list of relocations to perform */
163 	Word		ofl_actrelscnt;	/* 	and count */
164 	Word		ofl_entrelscnt;	/* no of relocations entered */
165 	List		ofl_copyrels;	/* list of copy relocations */
166 	List		ofl_ordered;	/* list of shf_ordered sections */
167 	List		ofl_syminfsyms;	/* list of interesting syms */
168 					/*	for syminfo processing */
169 	List		ofl_ismove;	/* list of .SUNW_move sections */
170 	List		ofl_mvrelisdescs; /* list of relocation input section */
171 					/* targeting to expanded area */
172 	List		ofl_parsym; 	/* list of Parsym_info */
173 	List		ofl_extrarels;	/* relocation sections which have */
174 					/*    a NULL sh_info */
175 	avl_tree_t	*ofl_groups;	/* pointer to head of Groups AVL tree */
176 	List		ofl_initarray;	/* list of init array func names */
177 	List		ofl_finiarray;	/* list of fini array func names */
178 	List		ofl_preiarray;	/* list of preinit array func names */
179 	List		ofl_rtldinfo;	/* list of rtldinfo syms */
180 	List		ofl_osgroups;	/* list of output GROUP sections */
181 	List		ofl_ostlsseg;	/* pointer to sections in TLS segment */
182 #if	defined(__x86) && defined(_ELF64)
183 	List		ofl_unwind;	/* list of unwind output sections */
184 	Os_desc		*ofl_unwindhdr;	/* Unwind hdr */
185 #endif
186 	avl_tree_t	ofl_symavl;	/* pointer to head of Syms AVL tree */
187 	Sym_desc	**ofl_regsyms;	/* array of potential register */
188 	Word		ofl_regsymsno;	/*    symbols and array count */
189 	Word		ofl_regsymcnt;	/* no. of output register symbols */
190 	Word		ofl_lregsymcnt;	/* no. of local register symbols */
191 	Sym_desc	*ofl_dtracesym;	/* ld -zdtrace= */
192 	Lword		ofl_flags;	/* various state bits, args etc. */
193 	Lword		ofl_flags1;	/*	more flags */
194 	Xword		ofl_segorigin;	/* segment origin (start) */
195 	void		*ofl_entry;	/* entry point (-e and Sym_desc *) */
196 	char		*ofl_filtees;	/* shared objects we are a filter for */
197 	const char	*ofl_soname;	/* (-h option) output file name for */
198 					/*	dynamic structure */
199 	const char	*ofl_interp;	/* interpreter name used by exec() */
200 	char		*ofl_rpath;	/* run path to store in .dynamic */
201 	char		*ofl_config;	/* config path to store in .dynamic */
202 	List		ofl_ulibdirs;	/* user supplied library search list */
203 	List		ofl_dlibdirs;	/* default library search list */
204 	Word		ofl_vercnt;	/* number of versions to generate */
205 	List		ofl_verdesc;	/* list of version descriptors */
206 	size_t		ofl_verdefsz;	/* size of version definition section */
207 	size_t		ofl_verneedsz;	/* size of version needed section */
208 	Word		ofl_entercnt;	/* no. of global symbols entered */
209 	Word		ofl_globcnt;	/* no. of global symbols to output */
210 	Word		ofl_scopecnt;	/* no. of scoped symbols to output */
211 	Word		ofl_dynscopecnt; /* no. scoped syms in .SUNW_ldynsym */
212 	Word		ofl_elimcnt;	/* no. of eliminated symbols */
213 	Word		ofl_locscnt;	/* no. of local symbols in .symtab */
214 	Word		ofl_dynlocscnt;	/* no. local symbols in .SUNW_ldynsym */
215 	Word		ofl_dynsymsortcnt; /* no. ndx in .SUNW_dynsymsort */
216 	Word		ofl_dyntlssortcnt; /* no. ndx in .SUNW_dyntlssort */
217 	Word		ofl_dynshdrcnt;	/* no. of output section in .dynsym */
218 	Word		ofl_shdrcnt;	/* no. of output sections */
219 	Str_tbl		*ofl_shdrsttab;	/* Str_tbl for shdr strtab */
220 	Str_tbl		*ofl_strtab;	/* Str_tbl for symtab strtab */
221 	Str_tbl		*ofl_dynstrtab;	/* Str_tbl for dymsym strtab */
222 	Gotndx		*ofl_tlsldgotndx; /* index to LD TLS_index structure */
223 	Xword		ofl_relocsz;	/* size of output relocations */
224 	Xword		ofl_relocgotsz;	/* size of .got relocations */
225 	Xword		ofl_relocpltsz;	/* size of .plt relocations */
226 	Xword		ofl_relocbsssz;	/* size of .bss (copy) relocations */
227 	Xword		ofl_relocrelsz;	/* size of .rel[a] relocations */
228 	Word		ofl_relocincnt;	/* no. of input relocations */
229 	Word		ofl_reloccnt;	/* tot number of output relocations */
230 	Word		ofl_reloccntsub; /* tot numb of output relocations to */
231 					/*	skip (-zignore) */
232 	Word		ofl_relocrelcnt; /* tot number of relative */
233 					/*	relocations */
234 	Word		ofl_gotcnt;	/* no. of .got entries */
235 	Word		ofl_pltcnt;	/* no. of .plt entries */
236 	Word		ofl_pltpad;	/* no. of .plt padd entries */
237 	Word		ofl_hashbkts;	/* no. of hash buckets required */
238 	Is_desc		*ofl_isbss;	/* .bss input section (globals) */
239 	Is_desc		*ofl_islbss;	/* .lbss input section (globals) */
240 	Is_desc		*ofl_istlsbss;	/* .tlsbss input section (globals) */
241 	Is_desc		*ofl_issunwdata1; /* .data input section */
242 					/* 	partially expanded. */
243 	Is_desc		*ofl_issunwbss;	/* .SUNW_bss input section (globals) */
244 	Os_desc		*ofl_osdynamic;	/* .dynamic output section */
245 	Os_desc		*ofl_osdynsym;	/* .dynsym output section */
246 	Os_desc		*ofl_osldynsym;	/* .SUNW_ldynsym output section */
247 	Os_desc		*ofl_osdynstr;	/* .dynstr output section */
248 	Os_desc		*ofl_osdynsymsort; /* .SUNW_dynsymsort output section */
249 	Os_desc		*ofl_osdyntlssort; /* .SUNW_dyntlssort output section */
250 	Os_desc		*ofl_osgot;	/* .got output section */
251 	Os_desc		*ofl_oshash;	/* .hash output section */
252 	Os_desc		*ofl_osinitarray; /* .initarray output section */
253 	Os_desc		*ofl_osfiniarray; /* .finiarray output section */
254 	Os_desc		*ofl_ospreinitarray; /* .preinitarray output section */
255 	Os_desc		*ofl_osinterp;	/* .interp output section */
256 	Os_desc		*ofl_oscap;	/* .SUNW_cap output section */
257 	Os_desc		*ofl_osplt;	/* .plt output section */
258 	Os_desc		*ofl_osmove;	/* .SUNW_move output section */
259 	Os_desc		*ofl_osrelhead;	/* first relocation section */
260 	Os_desc		*ofl_osrel;	/* .rel[a] relocation section */
261 	Os_desc		*ofl_osshstrtab; /* .shstrtab output section */
262 	Os_desc		*ofl_osstrtab;	/* .strtab output section */
263 	Os_desc		*ofl_ossymtab;	/* .symtab output section */
264 	Os_desc		*ofl_ossymshndx; /* .symtab_shndx output section */
265 	Os_desc		*ofl_osdynshndx; /* .dynsym_shndx output section */
266 	Os_desc		*ofl_osldynshndx; /* .SUNW_ldynsym_shndx output sec */
267 	Os_desc		*ofl_osverdef;	/* .version definition output section */
268 	Os_desc		*ofl_osverneed;	/* .version needed output section */
269 	Os_desc		*ofl_osversym;	/* .version symbol ndx output section */
270 	Word		ofl_dtflags_1;	/* DT_FLAGS_1 entries */
271 	Word		ofl_dtflags;	/* DT_FLAGS entries */
272 	Os_desc		*ofl_ossyminfo;	/* .SUNW_syminfo output section */
273 	Half		ofl_sunwdata1ndx; /* section index for sunwdata1  */
274 					/* Ref. at perform_outreloc() in */
275 					/* libld/{mach}/machrel.c */
276 	Xword		*ofl_checksum;	/* DT_CHECKSUM value address */
277 	char		*ofl_depaudit;	/* dependency auditing required (-P) */
278 	char		*ofl_audit;	/* object auditing required (-p) */
279 	Alist		*ofl_symfltrs;	/* per-symbol filtees and their */
280 	Alist		*ofl_dtsfltrs;	/*	associated .dynamic/.dynstrs */
281 	Xword		ofl_hwcap_1;	/* hardware capabilities */
282 	Xword		ofl_sfcap_1;	/* software capabilities */
283 	Lm_list		*ofl_lml;	/* runtime link-map list */
284 	Gottable	*ofl_gottable;	/* debugging got information */
285 };
286 
287 #define	FLG_OF_DYNAMIC	0x00000001	/* generate dynamic output module */
288 #define	FLG_OF_STATIC	0x00000002	/* generate static output module */
289 #define	FLG_OF_EXEC	0x00000004	/* generate an executable */
290 #define	FLG_OF_RELOBJ	0x00000008	/* generate a relocatable object */
291 #define	FLG_OF_SHAROBJ	0x00000010	/* generate a shared object */
292 #define	FLG_OF_BFLAG	0x00000020	/* do no special plt building: -b */
293 #define	FLG_OF_IGNENV	0x00000040	/* ignore LD_LIBRARY_PATH: -i */
294 #define	FLG_OF_STRIP	0x00000080	/* strip output: -s */
295 #define	FLG_OF_NOWARN	0x00000100	/* disable symbol warnings: -t */
296 #define	FLG_OF_NOUNDEF	0x00000200	/* allow no undefined symbols: -zdefs */
297 #define	FLG_OF_PURETXT	0x00000400	/* allow no text relocations: -ztext  */
298 #define	FLG_OF_GENMAP	0x00000800	/* generate a memory map: -m */
299 #define	FLG_OF_DYNLIBS	0x00001000	/* dynamic input allowed: -Bdynamic */
300 #define	FLG_OF_SYMBOLIC	0x00002000	/* bind global symbols: -Bsymbolic */
301 #define	FLG_OF_ADDVERS	0x00004000	/* add version stamp: -Qy */
302 #define	FLG_OF_NOLDYNSYM 0x00008000	/* -znoldynsym set */
303 #define	FLG_OF_SEGORDER	0x00010000	/* segment ordering is required */
304 #define	FLG_OF_SEGSORT	0x00020000	/* segment sorting is required */
305 #define	FLG_OF_TEXTREL	0x00040000	/* text relocations have been found */
306 #define	FLG_OF_MULDEFS	0x00080000	/* multiple symbols are allowed */
307 #define	FLG_OF_TLSPHDR	0x00100000	/* a TLS program header is required */
308 #define	FLG_OF_BLDGOT	0x00200000	/* build GOT table */
309 #define	FLG_OF_VERDEF	0x00400000	/* record version definitions */
310 #define	FLG_OF_VERNEED	0x00800000	/* record version dependencies */
311 #define	FLG_OF_NOVERSEC 0x01000000	/* don't record version sections */
312 #define	FLG_OF_AUTOLCL	0x02000000	/* automatically reduce unspecified */
313 					/*	global symbols to locals */
314 #define	FLG_OF_PROCRED	0x04000000	/* process any symbol reductions by */
315 					/*	effecting the symbol table */
316 					/*	output and relocations */
317 #define	FLG_OF_SYMINFO	0x08000000	/* create a syminfo section */
318 #define	FLG_OF_AUX	0x10000000	/* ofl_filter is an auxiliary filter */
319 #define	FLG_OF_FATAL	0x20000000	/* fatal error during input */
320 #define	FLG_OF_WARN	0x40000000	/* warning during input processing. */
321 #define	FLG_OF_VERBOSE	0x80000000	/* -z verbose flag set */
322 
323 #define	FLG_OF_MAPSYMB	0x000100000000	/* symbolic scope definition seen */
324 #define	FLG_OF_MAPGLOB	0x000200000000	/* global scope definition seen */
325 
326 /*
327  * In the flags1 arena, establish any options that are applicable to archive
328  * extraction first, and associate a mask.  These values are recorded with any
329  * archive descriptor so that they may be reset should the archive require a
330  * rescan to try and resolve undefined symbols.
331  */
332 #define	FLG_OF1_ALLEXRT	0x00000001	/* extract all members from an */
333 					/*	archive file */
334 #define	FLG_OF1_WEAKEXT	0x00000002	/* allow archive extraction to */
335 					/*	resolve weak references */
336 #define	MSK_OF1_ARCHIVE	0x00000003	/* archive flags mask */
337 
338 #define	FLG_OF1_NOINTRP	0x00000008	/* -z nointerp flag set */
339 #define	FLG_OF1_ZDIRECT	0x00000010	/* -z direct flag set */
340 #define	FLG_OF1_NDIRECT	0x00000020	/* no-direct bindings specified */
341 #define	FLG_OF1_OVHWCAP	0x00000040	/* override any input hardware or */
342 #define	FLG_OF1_OVSFCAP	0x00000080	/*	software capabilities */
343 #define	FLG_OF1_RELDYN	0x00000100	/* process .dynamic in rel obj */
344 #define	FLG_OF1_REDLSYM	0x00000200	/* reduce local symbols */
345 #define	FLG_OF1_AUTOELM	0x00000400	/* automatically eliminate  */
346 					/*	unspecified global symbols */
347 #define	FLG_OF1_IGNORE	0x00000800	/* ignore unused dependencies */
348 #define	FLG_OF1_RELCNT	0x00001000	/* enable DT_RELACNT tracking */
349 #define	FLG_OF1_TEXTOFF 0x00002000	/* text relocations are ok */
350 #define	FLG_OF1_ABSEXEC	0x00004000	/* -zabsexec set */
351 #define	FLG_OF1_LAZYLD	0x00008000	/* lazy loading of objects enabled */
352 #define	FLG_OF1_GRPPRM	0x00010000	/* dependencies are to have */
353 					/*	GROUPPERM enabled */
354 #define	FLG_OF1_OVRFLW	0x00020000	/* size exceeds 32-bit limitation */
355 					/*	of 32-bit libld */
356 #define	FLG_OF1_NOPARTI	0x00040000	/* -znopartial set */
357 #define	FLG_OF1_BSSOREL	0x00080000	/* output relocation against bss */
358 					/*	section */
359 #define	FLG_OF1_TLSOREL	0x00100000	/* output relocation against .tlsbss */
360 					/*	section */
361 #define	FLG_OF1_MEMORY	0x00200000	/* produce a memory model */
362 #define	FLG_OF1_RLXREL	0x00400000	/* -z relaxreloc flag set */
363 
364 #define	FLG_OF1_VADDR	0x01000000	/* vaddr was explicitly set */
365 #define	FLG_OF1_EXTRACT	0x02000000	/* archive member has been extracted */
366 #define	FLG_OF1_RESCAN	0x04000000	/* any archives should be rescanned */
367 #define	FLG_OF1_IGNPRC	0x08000000	/* ignore processing required */
368 #define	FLG_OF1_NCSTTAB	0x10000000	/* -znocompstrtab set */
369 #define	FLG_OF1_DONE	0x20000000	/* link-editor processing complete */
370 #define	FLG_OF1_NONREG	0x40000000	/* non-regular file specified as */
371 					/*	the output file */
372 #define	FLG_OF1_ALNODIR	0x80000000	/* establish NODIRECT for all */
373 					/*	exported interfaces. */
374 
375 /*
376  * Test to see if the output file would allow the presence of
377  * a .dynsym section.
378  */
379 #define	OFL_ALLOW_DYNSYM(ofl) ((ofl->ofl_flags & \
380 	(FLG_OF_DYNAMIC | FLG_OF_RELOBJ)) == FLG_OF_DYNAMIC)
381 
382 /*
383  * Test to see if the output file would allow the presence of
384  * a .SUNW_ldynsym section. The requirements are that a .dynsym
385  * is allowed, and -znoldynsym has not been specified. Note that
386  * even if the answer is True (1), we will only generate one if there
387  * are local symbols that require it.
388  */
389 #define	OFL_ALLOW_LDYNSYM(ofl) ((ofl->ofl_flags & \
390 	(FLG_OF_DYNAMIC | FLG_OF_RELOBJ | FLG_OF_NOLDYNSYM)) == FLG_OF_DYNAMIC)
391 
392 /*
393  * Relocation (active & output) processing structure - transparent to common
394  * code.
395  */
396 struct rel_desc {
397 	Os_desc		*rel_osdesc;	/* output section reloc is against */
398 	Is_desc		*rel_isdesc;	/* input section reloc is against */
399 	const char	*rel_sname;	/* symbol name (may be "unknown") */
400 	Sym_desc	*rel_sym;	/* sym relocation is against */
401 	Sym_desc	*rel_usym;	/* strong sym if this is a weak pair */
402 	Mv_desc		*rel_move;	/* move table information */
403 	Word		rel_flags;	/* misc. flags for relocations */
404 	Word		rel_rtype;	/* relocation type */
405 	Xword		rel_roffset;	/* relocation offset */
406 	Sxword		rel_raddend;	/* addend from input relocation */
407 	Word		rel_typedata;	/* ELF_R_TYPE_DATA(info) */
408 };
409 
410 /*
411  * common flags used on the Rel_desc structure (defined in machrel.h).
412  */
413 #define	FLG_REL_GOT	0x00000001	/* relocation against GOT */
414 #define	FLG_REL_PLT	0x00000002	/* relocation against PLT */
415 #define	FLG_REL_BSS	0x00000004	/* relocation against BSS */
416 #define	FLG_REL_LOAD	0x00000008	/* section loadable */
417 #define	FLG_REL_SCNNDX	0x00000010	/* use section index for symbol ndx */
418 #define	FLG_REL_CLVAL	0x00000020	/* clear VALUE for active relocation */
419 #define	FLG_REL_ADVAL	0x00000040	/* add VALUE for output relocation, */
420 					/*	only relevant to SPARC and */
421 					/*	R_SPARC_RELATIVE */
422 #define	FLG_REL_GOTCL	0x00000080	/* clear the GOT entry.  This is */
423 					/* relevant to RELA relocations, */
424 					/* not REL (i386) relocations */
425 #define	FLG_REL_MOVETAB	0x00000100	/* Relocation against .SUNW_move */
426 					/*	adjustments required before */
427 					/*	actual relocation */
428 #define	FLG_REL_NOINFO	0x00000200	/* Relocation comes from a section */
429 					/*	with a null sh_info field */
430 #define	FLG_REL_REG	0x00000400	/* Relocation target is reg sym */
431 #define	FLG_REL_FPTR	0x00000800	/* relocation against func. desc. */
432 #define	FLG_REL_RFPTR1	0x00001000	/* Relative relocation against */
433 					/*   1st part of FD */
434 #define	FLG_REL_RFPTR2	0x00002000	/* Relative relocation against */
435 					/*   2nd part of FD */
436 #define	FLG_REL_DISP	0x00004000	/* *disp* relocation */
437 #define	FLG_REL_STLS	0x00008000	/* IE TLS reference to */
438 					/*	static TLS GOT index */
439 #define	FLG_REL_DTLS	0x00010000	/* GD TLS reference relative to */
440 					/*	dynamic TLS GOT index */
441 #define	FLG_REL_MTLS	0x00020000	/* LD TLS reference against GOT */
442 #define	FLG_REL_STTLS	0x00040000	/* LE TLS reference directly */
443 					/*	to static tls index */
444 #define	FLG_REL_TLSFIX	0x00080000	/* relocation points to TLS instr. */
445 					/*	which needs updating */
446 #define	FLG_REL_RELA	0x00100000	/* descripter captures a Rela */
447 #define	FLG_REL_GOTFIX	0x00200000	/* relocation points to GOTOP instr. */
448 					/*	which needs updating */
449 
450 /*
451  * Structure to hold a cache of Relocations.
452  */
453 struct rel_cache {
454 	Rel_desc	*rc_end;
455 	Rel_desc	*rc_free;
456 };
457 
458 /*
459  * Symbol value descriptor.  For relocatable objects, each symbols value is
460  * its offset within its associated section.  Therefore, to uniquely define
461  * each symbol within a reloctable object, record and sort the sh_offset and
462  * symbol value.  This information is used to seach for displacement
463  * relocations as part of copy relocation validation.
464  */
465 typedef struct {
466 	Addr		ssv_value;
467 	Sym_desc	*ssv_sdp;
468 } Ssv_desc;
469 
470 /*
471  * Input file processing structures.
472  */
473 struct ifl_desc {			/* input file descriptor */
474 	const char	*ifl_name;	/* full file name */
475 	const char	*ifl_soname;	/* shared object name */
476 	dev_t		ifl_stdev;	/* device id and inode number for .so */
477 	ino_t		ifl_stino;	/*	multiple inclusion checks */
478 	Ehdr		*ifl_ehdr;	/* elf header describing this file */
479 	Elf		*ifl_elf;	/* elf descriptor for this file */
480 	Sym_desc	**ifl_oldndx;	/* original symbol table indices */
481 	Sym_desc	*ifl_locs;	/* symbol desc version of locals */
482 	Ssv_desc	*ifl_sortsyms;	/* sorted list of symbols by value */
483 	Word		ifl_locscnt;	/* no. of local symbols to process */
484 	Word		ifl_symscnt;	/* total no. of symbols to process */
485 	Word		ifl_sortcnt;	/* no. of sorted symbols to process */
486 	Word		ifl_shnum;	/* number of sections in file */
487 	Word		ifl_shstrndx;	/* index to .shstrtab */
488 	Word		ifl_vercnt;	/* number of versions in file */
489 	Is_desc		**ifl_isdesc;	/* isdesc[scn ndx] = Is_desc ptr */
490 	Sdf_desc	*ifl_sdfdesc;	/* control definition */
491 	Versym		*ifl_versym;	/* version symbol table array */
492 	Ver_index	*ifl_verndx;	/* verndx[ver ndx] = Ver_index */
493 	List		ifl_verdesc;	/* version descriptor list */
494 	List		ifl_relsect;	/* relocation section list */
495 	Alist		*ifl_groups;	/* SHT_GROUP section list */
496 	Half		ifl_neededndx;	/* index to NEEDED in .dyn section */
497 	Half		ifl_flags;	/* Explicit/implicit reference */
498 };
499 
500 #define	FLG_IF_CMDLINE	0x00001		/* full filename specified from the */
501 					/*	command line (no -l) */
502 #define	FLG_IF_NEEDED	0x00002		/* shared object should be recorded */
503 #define	FLG_IF_DIRECT	0x00004		/* establish direct bindings to this */
504 					/*	object */
505 #define	FLG_IF_EXTRACT	0x00008		/* file extracted from an archive */
506 #define	FLG_IF_VERNEED	0x00010		/* version dependency information is */
507 					/*	required */
508 #define	FLG_IF_DEPREQD	0x00020		/* dependency is required to satisfy */
509 					/*	symbol references */
510 #define	FLG_IF_NEEDSTR	0x00040		/* dependency specified by -Nn */
511 					/*	flag */
512 #define	FLG_IF_IGNORE	0x00080		/* ignore unused dependencies */
513 #define	FLG_IF_NODIRECT	0x00100		/* object contains symbols that */
514 					/*	cannot be directly bound to. */
515 #define	FLG_IF_LAZYLD	0x00200		/* bindings to this object should be */
516 					/*	lazy loaded */
517 #define	FLG_IF_GRPPRM	0x00400		/* this dependency should have the */
518 					/*	DF_P1_GROUPPERM flag set */
519 #define	FLG_IF_DISPPEND 0x00800		/* displacement relocation done */
520 					/*	in the ld time. */
521 #define	FLG_IF_DISPDONE 0x01000		/* displacement relocation done */
522 					/* 	at the run time */
523 #define	FLG_IF_MAPFILE	0x02000		/* file is a mapfile */
524 #define	FLG_IF_HSTRTAB	0x04000		/* file has a string section */
525 #define	FLG_IF_FILEREF	0x08000		/* file contains a section which */
526 					/*	is included in the output */
527 					/*	allocatable image */
528 
529 struct is_desc {			/* input section descriptor */
530 	const char	*is_name;	/* the section name */
531 	const char	*is_basename;	/* original section name (without */
532 					/*	.<sect>%<func> munging */
533 	Shdr		*is_shdr;	/* the elf section header */
534 	Ifl_desc	*is_file;	/* infile desc for this section */
535 	Os_desc		*is_osdesc;	/* new output section for this */
536 					/*	input section */
537 	Elf_Data	*is_indata;	/* input sections raw data */
538 	Is_desc		*is_symshndx;	/* related SHT_SYM_SHNDX section */
539 	Word		is_scnndx;	/* original section index in file */
540 	Word		is_txtndx;	/* Index for section.  Used to decide */
541 					/*	where to insert section when */
542 					/* 	reordering sections */
543 	Word		is_ident;	/* preserved IDENT used for ordered */
544 					/*	sections. */
545 	uint_t		is_namehash;	/* hash on section name */
546 	Half		is_key;		/* Used for SHF_ORDERED */
547 	Half		is_flags;	/* Various flags */
548 };
549 
550 #define	FLG_IS_ORDERED	0x0001		/* This is a SHF_ORDERED section */
551 #define	FLG_IS_KEY	0x0002		/* This is a section pointed by */
552 					/* sh_info of a SHF_ORDERED section */
553 #define	FLG_IS_DISCARD	0x0004		/* section is to be discarded */
554 #define	FLG_IS_RELUPD	0x0008		/* symbol defined here may have moved */
555 #define	FLG_IS_SECTREF	0x0010		/* section has been referenced */
556 #define	FLG_IS_GDATADEF	0x0020		/* section contains global data sym */
557 #define	FLG_IS_EXTERNAL	0x0040		/* isp from an user file */
558 
559 
560 /*
561  * Map file and output file processing structures
562  */
563 struct os_desc {			/* Output section descriptor */
564 	const char	*os_name;	/* the section name */
565 	Elf_Scn		*os_scn;	/* the elf section descriptor */
566 	Shdr		*os_shdr;	/* the elf section header */
567 	Os_desc		*os_relosdesc;	/* the output relocation section */
568 	List		os_relisdescs;	/* reloc input section descriptors */
569 					/*	for this output section */
570 	List		os_isdescs;	/* list of input sections in output */
571 	Sort_desc	*os_sort;	/* used for sorting sections */
572 	Sg_desc		*os_sgdesc;	/* segment os_desc is placed on */
573 	Elf_Data	*os_outdata;	/* output sections raw data */
574 	List		os_comdats;	/* list of COMDAT sections present */
575 					/*	in current output section */
576 	Word		os_scnsymndx;	/* index in output symtab of section */
577 					/*	symbol for this section */
578 	Word		os_txtndx;	/* Index for section.  Used to decide */
579 					/*	where to insert section when */
580 					/* 	reordering sections */
581 	Xword		os_szoutrels;	/* size of output relocation section */
582 	uint_t		os_namehash;	/* hash on section name */
583 	uchar_t		os_flags;	/* various flags */
584 };
585 
586 #define	FLG_OS_ORDER_KEY	0x01	/* include a sort key section */
587 #define	FLG_OS_OUTREL		0x02	/* output rel against this section */
588 #define	FLG_OS_SECTREF		0x04	/* isps are not affected by -zignore */
589 
590 /*
591  * For sorting sections.
592  */
593 struct sort_desc {
594 	Is_desc		**st_order;
595 	Word		st_ordercnt;
596 	Is_desc		**st_before;
597 	Word		st_beforecnt;
598 	Is_desc		**st_after;
599 	Word		st_aftercnt;
600 };
601 
602 struct sg_desc {			/* output segment descriptor */
603 	Phdr		sg_phdr;	/* segment header for output file */
604 	const char	*sg_name;	/* segment name */
605 	Xword		sg_round;	/* data rounding required (mapfile) */
606 	Xword		sg_length;	/* maximum segment length; if 0 */
607 					/*	segment is not specified */
608 	Alist		*sg_osdescs;	/* list of output section descriptors */
609 	Alist		*sg_secorder;	/* list specifying section ordering */
610 					/*	for the segment */
611 	Half		sg_flags;
612 	Sym_desc	*sg_sizesym;	/* size symbol for this segment */
613 	Xword		sg_addralign;	/* LCM of sh_addralign */
614 	Elf_Scn		*sg_fscn;	/* the SCN of the first section. */
615 };
616 
617 
618 #define	FLG_SG_VADDR	0x0001		/* vaddr segment attribute set */
619 #define	FLG_SG_PADDR	0x0002		/* paddr segment attribute set */
620 #define	FLG_SG_LENGTH	0x0004		/* length segment attribute set */
621 #define	FLG_SG_ALIGN	0x0008		/* align segment attribute set */
622 #define	FLG_SG_ROUND	0x0010		/* round segment attribute set */
623 #define	FLG_SG_FLAGS	0x0020		/* flags segment attribute set */
624 #define	FLG_SG_TYPE	0x0040		/* type segment attribute set */
625 #define	FLG_SG_ORDER	0x0080		/* has ordering been turned on for */
626 					/* 	this segment. */
627 					/*	i.e. ?[O] option in mapfile */
628 #define	FLG_SG_NOHDR	0x0100		/* don't map ELF or phdrs into */
629 					/* 	this segment */
630 #define	FLG_SG_EMPTY	0x0200		/* an empty segment specification */
631 					/*	no input sections will be */
632 					/*	associated to this section */
633 #define	FLG_SG_KEY	0x0400		/* include a key section */
634 #define	FLG_SG_DISABLED	0x0800		/* this segment is disabled */
635 #define	FLG_SG_PHREQ	0x1000		/* this segment requires a program */
636 					/* header */
637 
638 struct sec_order {
639 	const char	*sco_secname;	/* section name to be ordered */
640 	Word		sco_index;	/* ordering index for section */
641 	Half		sco_flags;
642 };
643 
644 #define	FLG_SGO_USED	0x0001		/* was ordering used? */
645 
646 struct ent_desc {			/* input section entrance criteria */
647 	List		ec_files;	/* files from which to accept */
648 					/*	sections */
649 	const char	*ec_name;	/* name to match (NULL if none) */
650 	Word		ec_type;	/* section type */
651 	Word		ec_attrmask;	/* section attribute mask (AWX) */
652 	Word		ec_attrbits;	/* sections attribute bits */
653 	Sg_desc		*ec_segment;	/* output segment to enter if matched */
654 	Word		ec_ndx;		/* index to determine where section */
655 					/*	meeting this criteria should */
656 					/*	inserted. Used for reordering */
657 					/*	of sections. */
658 	Half		ec_flags;
659 };
660 
661 #define	FLG_EC_USED	0x0001		/* entrance criteria met? */
662 
663 /*
664  *  Move supplementary structures
665  *	Sorted by symbol local/global and then by name.
666  */
667 typedef struct psym_info {
668 	Sym_desc	*psym_symd;	/* partially initialized symbol */
669 	Word 		psym_num;	/* number of move entires */
670 	Half 		psym_flag;	/* various flag */
671 	List 		psym_mvs;	/* the list of move entries */
672 } Psym_info;
673 
674 #define	FLG_PSYM_OVERLAP	0x01	/* Overlapping */
675 
676 /*
677  * One structure is allocated for a move entry.
678  */
679 typedef struct mv_itm {
680 	Xword		mv_start;	/* start position */
681 	Xword		mv_length;	/* The length of initialization */
682 	Half		mv_flag;	/* various flags */
683 	Is_desc		*mv_isp;	/* input desc. this entry is from */
684 	Move		*mv_ientry;	/* Input Move_entry */
685 	Word 		mv_oidx;	/* Output Move_entry index */
686 } Mv_itm;
687 
688 #define	FLG_MV_OUTSECT	0x01	/* Will be in move section */
689 
690 /*
691  * Define a move descripter used within relocation structures.
692  */
693 struct mv_desc {
694 	Move		*mvd_move;
695 	Sym_desc	*mvd_sym;
696 };
697 
698 struct sym_desc {
699 	List		sd_GOTndxs;	/* list of associated GOT entries */
700 	Sym		*sd_sym;	/* pointer to symbol table entry */
701 	Sym		*sd_osym;	/* copy of the original symbol entry */
702 					/*	used only for local partial */
703 	Psym_info	*sd_psyminfo;	/* for partial symbols, maintain a */
704 					/*	pointer to parsym_info */
705 	const char	*sd_name;	/* symbols name */
706 	Ifl_desc	*sd_file;	/* file where symbol is taken */
707 	Is_desc		*sd_isc;	/* input section of symbol definition */
708 	Sym_aux		*sd_aux;	/* auxiliary global symbol info. */
709 	Word		sd_symndx;	/* index in output symbol table */
710 	Word		sd_shndx;	/* sect. index sym is associated w/ */
711 	Word		sd_flags;	/* state flags */
712 	Half		sd_flags1;	/* more symbol flags */
713 	Half		sd_ref;		/* reference definition of symbol */
714 };
715 
716 /*
717  * The auxiliary symbol descriptor contains the additional information (beyond
718  * the symbol descriptor) required to process global symbols.  These symbols are
719  * accessed via an internal symbol hash table where locality of reference is
720  * important for performance.
721  */
722 struct sym_aux {
723 	List 		sa_dfiles;	/* files where symbol is defined */
724 	Sym		sa_sym;		/* copy of symtab entry */
725 	const char	*sa_vfile;	/* first unavailable definition */
726 	Ifl_desc	*sa_bindto;	/* symbol to bind to - for translator */
727 	const char	*sa_rfile;	/* file with first symbol referenced */
728 	Word		sa_hash;	/* the pure hash value of symbol */
729 	Word		sa_PLTndx;	/* index into PLT for symbol */
730 	Word		sa_PLTGOTndx;	/* GOT entry indx for PLT indirection */
731 	Word		sa_linkndx;	/* index of associated symbol from */
732 					/*	ET_DYN file */
733 	Half		sa_symspec;	/* special symbol ids */
734 	Half		sa_overndx;	/* output file versioning index */
735 	Half		sa_dverndx;	/* dependency versioning index */
736 };
737 
738 
739 /*
740  * Nodes used to track symbols in the global AVL symbol dictionary.
741  */
742 struct sym_avlnode {
743 	avl_node_t	sav_node;	/* AVL node */
744 	Word		sav_hash;	/* symbol hash value */
745 	const char	*sav_name;	/* symbol name */
746 	Sym_desc	*sav_symdesc;	/* SymDesc entry */
747 };
748 
749 /*
750  * These are the ids for processing of `Special symbols'.  They are used
751  * to set the sym->sd_aux->sa_symspec field.
752  */
753 #define	SDAUX_ID_ETEXT	1		/* etext && _etext symbol */
754 #define	SDAUX_ID_EDATA	2		/* edata && _edata symbol */
755 #define	SDAUX_ID_END	3		/* end, _end, && _END_ symbol */
756 #define	SDAUX_ID_DYN	4		/* DYNAMIC && _DYNAMIC symbol */
757 #define	SDAUX_ID_PLT	5		/* _PROCEDURE_LINKAGE_TABLE_ symbol */
758 #define	SDAUX_ID_GOT	6		/* _GLOBAL_OFFSET_TABLE_ symbol */
759 #define	SDAUX_ID_START	7		/* START_ && _START_ symbol */
760 
761 /*
762  * Flags for sym_desc.sd_flags
763  */
764 #define	FLG_SY_MVTOCOMM	0x00000001	/* assign symbol to common (.bss) */
765 					/*	this is a result of a */
766 					/*	copy reloc against sym */
767 #define	FLG_SY_GLOBREF	0x00000002	/* a global reference has been seen */
768 #define	FLG_SY_WEAKDEF	0x00000004	/* a weak definition has been used */
769 #define	FLG_SY_CLEAN	0x00000008	/* `Sym' entry points to original */
770 					/*	input file (read-only). */
771 #define	FLG_SY_UPREQD	0x00000010	/* symbol value update is required, */
772 					/*	either it's used as an entry */
773 					/*	point or for relocation, but */
774 					/*	it must be updated even if */
775 					/*	the -s flag is in effect */
776 #define	FLG_SY_NOTAVAIL	0x00000020	/* symbol is not available to the */
777 					/*	application either because it */
778 					/*	originates from an implicitly */
779 					/* 	referenced shared object, or */
780 					/*	because it is not part of a */
781 					/*	specified version. */
782 #define	FLG_SY_REDUCED	0x00000040	/* a global is reduced to local */
783 #define	FLG_SY_VERSPROM	0x00000080	/* version definition has been */
784 					/*	promoted to output file */
785 #define	FLG_SY_PROT	0x00000100	/* stv_protected visibility seen */
786 
787 #define	FLG_SY_MAPREF	0x00000200	/* symbol reference generated by user */
788 					/*	from mapfile */
789 #define	FLG_SY_REFRSD	0x00000400	/* symbols sd_ref has been raised */
790 					/* 	due to a copy-relocs */
791 					/*	weak-strong pairing */
792 #define	FLG_SY_INTPOSE	0x00000800	/* symbol defines an interposer */
793 #define	FLG_SY_INVALID	0x00001000	/* unwanted/erroneous symbol */
794 #define	FLG_SY_SMGOT	0x00002000	/* small got index assigned to symbol */
795 					/*	sparc only */
796 #define	FLG_SY_PARENT	0x00004000	/* symbol to be found in parent */
797 					/*    only used with direct bindings */
798 #define	FLG_SY_LAZYLD	0x00008000	/* symbol to cause lazyloading of */
799 					/*	parent object */
800 #define	FLG_SY_ISDISC	0x00010000	/* symbol is a member of a DISCARDED */
801 					/*	section (COMDAT) */
802 #define	FLG_SY_PAREXPN	0x00020000	/* partially init. symbol to be */
803 					/*	expanded */
804 #define	FLG_SY_PLTPAD	0x00040000	/* pltpadding has been allocated for */
805 					/*	this symbol */
806 #define	FLG_SY_REGSYM	0x00080000	/* REGISTER symbol (sparc only) */
807 #define	FLG_SY_SOFOUND	0x00100000	/* compared against an SO definition */
808 #define	FLG_SY_EXTERN	0x00200000	/* symbol is external, allows -zdefs */
809 					/*    error suppression */
810 #define	FLG_SY_MAPUSED	0x00400000	/* mapfile symbol used (occurred */
811 					/*    within a relocatable object) */
812 #define	FLG_SY_COMMEXP	0x00800000	/* COMMON symbol which has been */
813 					/*	allocated */
814 #define	FLG_SY_CMDREF	0x01000000	/* symbol was referenced from the */
815 					/*	command line.  (ld -u <>, */
816 					/*	ld -zrtldinfo=<>, ...) */
817 #define	FLG_SY_SPECSEC	0x02000000	/* section index is reserved value */
818 					/*	ABS, COMMON, ... */
819 #define	FLG_SY_TENTSYM	0x04000000	/* tentative symbol */
820 #define	FLG_SY_VISIBLE	0x08000000	/* symbols visibility determined */
821 #define	FLG_SY_STDFLTR	0x10000000	/* symbol is a standard filter */
822 #define	FLG_SY_AUXFLTR	0x20000000	/* symbol is an auxiliary filter */
823 #define	FLG_SY_DYNSORT	0x40000000	/* req. in dyn[sym|tls]sort section */
824 #define	FLG_SY_NODYNSORT 0x80000000	/* excluded from dyn[sym_tls]sort sec */
825 
826 /*
827  * Sym_desc.sd_flags1
828  */
829 #define	FLG_SY1_GLOB	0x00000001	/* global symbol, remain global */
830 #define	FLG_SY1_PROT	0x00000002	/* global symbol, reduce to protected */
831 #define	FLG_SY1_LOCL	0x00000004	/* global symbol, reduce to local */
832 #define	FLG_SY1_DIR	0x00000008	/* global symbol, direct bindings */
833 #define	FLG_SY1_NDIR	0x00000010	/* global symbol, nondirect bindings */
834 #define	FLG_SY1_ELIM	0x00000020	/* global symbol, eliminate */
835 #define	FLG_SY1_IGNORE	0x00000040	/* symbol should be ignored */
836 
837 #define	MSK_SY1_DEFINED	(FLG_SY1_GLOB | FLG_SY1_PROT | FLG_SY1_LOCL)
838 					/* The above mask indicates that */
839 					/*    a symbol has been explicitly */
840 					/*    scoped, and therefore is not */
841 					/*    a candidate for auto-reduction */
842 
843 /*
844  * create a mask for (Sym.St_other & visibility) since the
845  * gABI does not yet define a ELF*_ST_OTHER macro.
846  */
847 #define	MSK_SYM_VISIBILITY	0x03
848 
849 /*
850  * Structure to manage the shared object definition lists.  There are two lists
851  * that use this structure:
852  *
853  *  o	ofl_soneed; maintain the list of implicitly required dependencies
854  *	(ie. shared objects needed by other shared objects).  These definitions
855  *	may include RPATH's required to locate the dependencies, and any
856  *	version requirements.
857  *
858  *  o	ofl_socntl; maintains the shared object control definitions.  These are
859  *	provided by the user (via a mapfile) and are used to indicate any
860  *	SONAME translations and verion control requirements.
861  */
862 struct	sdf_desc {
863 	const char	*sdf_name;	/* the shared objects file name */
864 	const char	*sdf_soname;	/* the shared objects SONAME */
865 	char		*sdf_rpath;	/* library search path DT_RPATH */
866 	const char	*sdf_rfile;	/* referencing file for diagnostics */
867 	Ifl_desc	*sdf_file;	/* the final input file descriptor */
868 	List		sdf_vers;	/* list of versions that are required */
869 					/*	from this object */
870 	List		sdf_verneed;	/* list of VERNEEDS to create for */
871 					/*	this object (via SPECVERS or */
872 					/*	ADDVERS) */
873 	Word		sdf_flags;
874 };
875 
876 #define	FLG_SDF_SONAME	0x02		/* An alternative SONAME is supplied */
877 #define	FLG_SDF_SELECT	0x04		/* version control selection required */
878 #define	FLG_SDF_VERIFY	0x08		/* version definition verification */
879 					/*	required */
880 #define	FLG_SDF_SPECVER	0x10		/* specify VERNEEDS */
881 #define	FLG_SDF_ADDVER	0x20		/* add VERNEED references */
882 
883 /*
884  * Structure to manage shared object version usage requirements.
885  */
886 struct	sdv_desc {
887 	const char	*sdv_name;	/* version name */
888 	const char	*sdv_ref;	/* versions reference */
889 	Word		sdv_flags;	/* flags */
890 };
891 
892 #define	FLG_SDV_MATCHED	0x01		/* VERDEF found and matched */
893 
894 /*
895  * Structures to manage versioning information.  Two versioning structures are
896  * defined:
897  *
898  *   o	a version descriptor maintains a linked list of versions and their
899  *	associated dependencies.  This is used to build the version definitions
900  *	for an image being created (see map_symbol), and to determine the
901  *	version dependency graph for any input files that are versioned.
902  *
903  *   o	a version index array contains each version of an input file that is
904  *	being processed.  It informs us which versions are available for
905  *	binding, and is used to generate any version dependency information.
906  */
907 struct	ver_desc {
908 	const char	*vd_name;	/* version name */
909 	Word		vd_hash;	/* hash value of name */
910 	Ifl_desc	*vd_file;	/* file that defined version */
911 	Half		vd_ndx;		/* coordinates with symbol index */
912 	Half		vd_flags;	/* version information */
913 	List		vd_deps;	/* version dependencies */
914 	Ver_desc	*vd_ref;	/* dependency's first reference */
915 };
916 
917 struct	ver_index {
918 	const char	*vi_name;	/* dependency version name */
919 	Half		vi_flags;	/* communicates availability */
920 	Ver_desc	*vi_desc;	/* cross reference to descriptor */
921 };
922 
923 /*
924  * Define any internal version descriptor flags ([vd|vi]_flags).  Note that the
925  * first byte is reserved for user visible flags (refer VER_FLG's in link.h).
926  */
927 #define	MSK_VER_USER	0x0f		/* mask for user visible flags */
928 
929 #define	FLG_VER_AVAIL	0x10		/* version is available for binding */
930 #define	FLG_VER_REFER	0x20		/* version has been referenced */
931 #define	FLG_VER_SELECT	0x40		/* version has been selected by user */
932 #define	FLG_VER_CYCLIC	0x80		/* a member of cyclic dependency */
933 
934 
935 /*
936  * Test to see if a symbol verson index is outside of the valid range
937  * of version indexes.
938  *
939  * entry:
940  *	_shndx - Symbol section index
941  *	_vercnt - # of versions defined by object containing symbol
942  *		in its verdef section. 0 implies that there is no verdef.
943  *	_versym - Pointer to versym array, or NULL if there isn't one.
944  *	_symndx - Index of symbol within containing symbol table
945  *		Used to index _versym, but only if _versym is non-NULL.
946  *
947  * note:
948  *	_vercnt and _versym[_symndx] are evaluated more than once.
949  *	Beware of expensive computations, or computations with side effects.
950  *
951  * There are two versions of this macro:
952  *
953  *	VERNDX_INVALID - Used by ld and rtld
954  *	VERNDX_INVALID_DIAG - Used by diagnostic tools such as elfdump.
955  *
956  * The difference is that VERNDX_INVALID does not examine the versym
957  * array if the number of defined versions is 0. This allows us to
958  * run GNU binaries (See below for details). However, the diagnostic
959  * tools will report them.
960  *
961  * If we encounter a defined symbol with a version that is outside the
962  * range of the valid versions supplied by the file, then we quietly
963  * ignore that symbol. This can't  happen in a native Solaris object.
964  * However, the GNU 'ld' uses the top bit (0x8000) of the version as a
965  * flag to the system that says the symbol should be treated as if it
966  * doesn't exist. The Solaris linker does not support this mechanism,
967  * or the model of interface evolution that it allows. However, we do
968  * wish to be able to link against objects produced that way. Ignoring
969  * such symbols gives the desired GNU-compatible "ignore" behavior,
970  * without committing us to their model, while reserving the ability
971  * to adopt it or do something different at a later date.
972  *
973  * Note that it is possible to see "version 1" in a file that contains 0
974  * version definitions. This happens when a mapfile is used to reduce
975  * global symbols to local. For instance:
976  *
977  *	{
978  *		global:
979  *			main;
980  *			eprintf;
981  *		local:
982  *			*;
983  *	};
984  *
985  * In this case, there will be a versym section (containing version
986  * indexes 0 and 1). However, there are no versions defined, and
987  * hence no corresponding verdef section.
988  *
989  * We have also seen objects produced by the GNU ld in which an object
990  * with no verdef section contains symbols with version symbols greater
991  * than 1. For both of these reasons, we allow and ignore arbitrary
992  * versions in the case where there is a versym section but the vesion
993  * count (_vercnt) is 0.
994  */
995 #define	VERNDX_INVALID_DIAG(_shndx, _vercnt, _versym, _symndx) \
996 	(((_shndx) != SHN_UNDEF) && (_versym != NULL) && \
997 	((_versym)[_symndx] > ((_vercnt == 0) ? 1 : _vercnt)) && \
998 	((_versym)[_symndx] < VER_NDX_LORESERVE))
999 #define	VERNDX_INVALID(_shndx, _vercnt, _versym, _symndx) \
1000 	(((_shndx) != SHN_UNDEF) && (_versym != NULL) && \
1001 	((_versym)[_symndx] > _vercnt) && ((_vercnt) > 0) && \
1002 	((_versym)[_symndx] < VER_NDX_LORESERVE))
1003 
1004 
1005 
1006 /*
1007  * isalist(1) descriptor - used to break an isalist string into its component
1008  * options.
1009  */
1010 struct	isa_opt {
1011 	char		*isa_name;	/* individual isa option name */
1012 	size_t		isa_namesz;	/*	and associated size */
1013 };
1014 
1015 struct	isa_desc {
1016 	char		*isa_list;	/* sysinfo(SI_ISALIST) list */
1017 	size_t		isa_listsz;	/*	and associated size */
1018 	Isa_opt		*isa_opt;	/* table of individual isa options */
1019 	size_t		isa_optno;	/*	and associated number */
1020 };
1021 
1022 /*
1023  * uname(2) descriptor - used to break a utsname structure into its component
1024  * options (at least those that we're interested in).
1025  */
1026 struct	uts_desc {
1027 	char		*uts_osname;	/* operating system name */
1028 	size_t		uts_osnamesz;	/*	and associated size */
1029 	char		*uts_osrel;	/* operating system release */
1030 	size_t		uts_osrelsz;	/*	and associated size */
1031 };
1032 
1033 
1034 /*
1035  * SHT_GROUP descriptor - used to track group sections at the global
1036  * level to resolve conflicts/determine which to keep.
1037  */
1038 struct group_desc {
1039 	const char	*gd_gsectname;	/* group section name */
1040 	const char	*gd_symname;	/* symbol name */
1041 	Word		*gd_data;	/* data for group section */
1042 	size_t		gd_scnndx;	/* group section index */
1043 	size_t		gd_cnt;		/* number of entries in group data */
1044 	Word		gd_flags;
1045 };
1046 
1047 #define	GRP_FLG_DISCARD	0x0001		/* group is to be discarded */
1048 
1049 /*
1050  * Indexes into the ld_support_funcs[] table.
1051  */
1052 typedef enum {
1053 	LDS_VERSION = 0,
1054 	LDS_INPUT_DONE,
1055 	LDS_START,
1056 	LDS_ATEXIT,
1057 	LDS_OPEN,
1058 	LDS_FILE,
1059 	LDS_INSEC,
1060 	LDS_SEC,
1061 	LDS_NUM
1062 } Support_ndx;
1063 
1064 
1065 /*
1066  * Structure to manage archive member caching.  Each archive has an archive
1067  * descriptor (Ar_desc) associated with it.  This contains pointers to the
1068  * archive symbol table (obtained by elf_getarsyms(3e)) and an auxiliary
1069  * structure (Ar_uax[]) that parallels this symbol table.  The member element
1070  * of this auxiliary table indicates whether the archive member associated with
1071  * the symbol offset has already been extracted (AREXTRACTED) or partially
1072  * processed (refer process_member()).
1073  */
1074 typedef struct ar_mem {
1075 	Elf		*am_elf;	/* elf descriptor for this member */
1076 	char		*am_name;	/* members name */
1077 	char		*am_path;	/* path (ie. lib(foo.o)) */
1078 	Sym		*am_syms;	/* start of global symbols */
1079 	char		*am_strs;	/* associated string table start */
1080 	Xword		am_symn;	/* no. of global symbols */
1081 } Ar_mem;
1082 
1083 typedef struct ar_aux {
1084 	Sym_desc	*au_syms;	/* internal symbol descriptor */
1085 	Ar_mem		*au_mem;	/* associated member */
1086 } Ar_aux;
1087 
1088 #define	FLG_ARMEM_PROC	(Ar_mem *)-1
1089 
1090 typedef struct ar_desc {
1091 	const char	*ad_name;	/* archive file name */
1092 	Elf		*ad_elf;	/* elf descriptor for the archive */
1093 	Elf_Arsym	*ad_start;	/* archive symbol table start */
1094 	Ar_aux		*ad_aux;	/* auxiliary symbol information */
1095 	dev_t		ad_stdev;	/* device id and inode number for */
1096 	ino_t		ad_stino;	/*	multiple inclusion checks */
1097 	Word		ad_flags;	/* archive specific cmd line flags */
1098 } Ar_desc;
1099 
1100 /*
1101  * Define any archive descriptor flags.  NOTE, make sure they do not clash with
1102  * any output file descriptor archive extraction flags, as these are saved in
1103  * the same entry (see MSK_OF1_ARCHIVE).
1104  */
1105 #define	FLG_ARD_EXTRACT	0x00010000	/* archive member has been extracted */
1106 
1107 /*
1108  * Function Declarations.
1109  */
1110 #if	defined(_ELF64)
1111 
1112 #define	ld_create_outfile	ld64_create_outfile
1113 #define	ld_ent_setup		ld64_ent_setup
1114 #define	ld_init_strings		ld64_init_strings
1115 #define	ld_make_sections	ld64_make_sections
1116 #define	ld_main			ld64_main
1117 #define	ld_ofl_cleanup		ld64_ofl_cleanup
1118 #define	ld_process_open		ld64_process_open
1119 #define	ld_reloc_init		ld64_reloc_init
1120 #define	ld_reloc_process	ld64_reloc_process
1121 #define	ld_sym_validate		ld64_sym_validate
1122 #define	ld_update_outfile	ld64_update_outfile
1123 
1124 #else
1125 
1126 #define	ld_create_outfile	ld32_create_outfile
1127 #define	ld_ent_setup		ld32_ent_setup
1128 #define	ld_init_strings		ld32_init_strings
1129 #define	ld_make_sections	ld32_make_sections
1130 #define	ld_main			ld32_main
1131 #define	ld_ofl_cleanup		ld32_ofl_cleanup
1132 #define	ld_process_open		ld32_process_open
1133 #define	ld_reloc_init		ld32_reloc_init
1134 #define	ld_reloc_process	ld32_reloc_process
1135 #define	ld_sym_validate		ld32_sym_validate
1136 #define	ld_update_outfile	ld32_update_outfile
1137 
1138 #endif
1139 
1140 extern int		ld32_main(int, char **);
1141 extern int		ld64_main(int, char **);
1142 
1143 extern uintptr_t	ld_create_outfile(Ofl_desc *);
1144 extern uintptr_t	ld_ent_setup(Ofl_desc *, Xword);
1145 extern uintptr_t	ld_init_strings(Ofl_desc *);
1146 extern uintptr_t	ld_make_sections(Ofl_desc *);
1147 extern void		ld_ofl_cleanup(Ofl_desc *);
1148 extern Ifl_desc		*ld_process_open(const char *, const char *, int *,
1149 			    Ofl_desc *, Half, Rej_desc *);
1150 extern uintptr_t	ld_reloc_init(Ofl_desc *);
1151 extern uintptr_t	ld_reloc_process(Ofl_desc *);
1152 extern uintptr_t	ld_sym_validate(Ofl_desc *);
1153 extern uintptr_t	ld_update_outfile(Ofl_desc *);
1154 
1155 #ifdef	__cplusplus
1156 }
1157 #endif
1158 
1159 #endif	/* _LIBLD_H */
1160