xref: /titanic_41/usr/src/cmd/sgs/include/libld.h (revision 96d9f183facd90dbbc2268c9a51689be0b6a0b46)
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 2009 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 #include <stdlib.h>
34 #include <libelf.h>
35 #include <sgs.h>
36 #include <_machelf.h>
37 #include <string_table.h>
38 #include <sys/avl.h>
39 #include <alist.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  * GOT reference models
105  */
106 typedef enum {
107 	GOT_REF_GENERIC,	/* generic symbol reference */
108 	GOT_REF_TLSIE,		/* TLS initial exec (gnu) reference */
109 	GOT_REF_TLSLD,		/* TLS local dynamic reference */
110 	GOT_REF_TLSGD		/* TLS general dynamic reference */
111 } Gotref;
112 
113 typedef struct {
114 	Xword		gn_addend;	/* addend associated with GOT entry */
115 	Sword		gn_gotndx;	/* GOT table index */
116 	Gotref		gn_gotref;
117 } Gotndx;
118 
119 /*
120  * Got debugging structure.  The got index is defined as a signed value as we
121  * do so much mucking around with negative and positive gots on SPARC, and sign
122  * extension is necessary when building 64-bit objects.  On intel we explicitly
123  * cast this variable to an unsigned value.
124  */
125 typedef struct {
126 	Sym_desc	*gt_sym;
127 	Gotndx		gt_gndx;
128 } Gottable;
129 
130 /*
131  * The link-editor caches the results of sloppy relocation processing
132  * in a variable of type Rlxrel_cache. Symbols come for processing in sorted
133  * order, so a single item cache suffices to eliminate duplicate lookups.
134  *
135  * When sloppy relocation processing fails, the Rlxrel_rej enum reports
136  * the underlying reason.
137  */
138 typedef enum {
139 	RLXREL_REJ_NONE = 0,	/* Replacement symbol was found */
140 	RLXREL_REJ_TARGET,	/* Target sec disallows relaxed relocations */
141 	RLXREL_REJ_SECTION,	/* Either there is no replacement section, */
142 				/* 	or its attributes are incompatible */
143 	RLXREL_REJ_SYMBOL,	/* Replacement symbol not found */
144 } Rlxrel_rej;
145 
146 typedef struct sreloc_cache {
147 	Sym_desc	*sr_osdp;	/* Original symbol */
148 	Sym_desc	*sr_rsdp;	/* Replacement symbol */
149 	Rlxrel_rej	sr_rej;		/* Reason for failure if NULL sr_rsdp */
150 } Rlxrel_cache;
151 
152 /*
153  * Nodes in an ofl_wrap AVL tree
154  *
155  * wsn_name is the name of the symbol to be wrapped. wsn_wrapname is used
156  * when we need to refer to the wrap symbol, and consists of the symbol
157  * name with a __wrap_ prefix.
158  */
159 typedef struct wrap_sym_node {
160 	avl_node_t	wsn_avlnode;	/* AVL book-keeping */
161 	const char	*wsn_name;	/* Symbol name: XXX */
162 	const char	*wsn_wrapname;	/* Wrap symbol name: __wrap_XXX */
163 } WrapSymNode;
164 
165 
166 /*
167  * Output file processing structure
168  */
169 typedef Lword ofl_flag_t;
170 struct ofl_desc {
171 	char		*ofl_sgsid;	/* link-editor identification */
172 	const char	*ofl_name;	/* full file name */
173 	Elf		*ofl_elf;	/* elf_memory() elf descriptor */
174 	Elf		*ofl_welf;	/* ELF_C_WRITE elf descriptor */
175 	Ehdr		*ofl_dehdr;	/* default elf header, and new elf */
176 	Ehdr		*ofl_nehdr;	/*	header describing this file */
177 	Phdr		*ofl_phdr;	/* program header descriptor */
178 	Phdr		*ofl_tlsphdr;	/* TLS phdr */
179 	int		ofl_fd;		/* file descriptor */
180 	size_t		ofl_size;	/* image size */
181 	APlist		*ofl_maps;	/* list of input mapfiles */
182 	APlist		*ofl_segs;	/* list of segments */
183 	Alist		*ofl_ents;	/* list of entrance descriptors */
184 	APlist		*ofl_objs;	/* relocatable object file list */
185 	Word		ofl_objscnt;	/* 	and count */
186 	APlist		*ofl_ars;	/* archive library list */
187 	Word		ofl_arscnt;	/* 	and count */
188 	int		ofl_ars_gsandx; /* archive group argv index. 0 means */
189 					/*	no current group, < 0 means */
190 					/*	error reported. >0 is cur ndx */
191 	Word		ofl_ars_gsndx;	/* current -zrescan-start ofl_ars ndx */
192 	APlist		*ofl_sos;	/* shared object list */
193 	Word		ofl_soscnt;	/* 	and count */
194 	APlist		*ofl_soneed;	/* list of implicitly required .so's */
195 	APlist		*ofl_socntl;	/* list of .so control definitions */
196 	APlist		*ofl_outrels;	/* list of output relocations */
197 	Word		ofl_outrelscnt;	/* 	and count */
198 	APlist		*ofl_actrels;	/* list of relocations to perform */
199 	Word		ofl_actrelscnt;	/* 	and count */
200 	Word		ofl_entrelscnt;	/* no of relocations entered */
201 	Alist		*ofl_copyrels;	/* list of copy relocations */
202 	APlist		*ofl_ordered;	/* list of shf_ordered sections */
203 	APlist		*ofl_symdtent;	/* list of syminfo symbols that need */
204 					/*	to reference .dynamic entries */
205 	APlist		*ofl_ismove;	/* list of .SUNW_move sections */
206 	APlist		*ofl_ismoverel;	/* list of relocation input section */
207 					/* targeting to expanded area */
208 	APlist		*ofl_parsyms; 	/* list of partially initialized */
209 					/*	symbols (ie. move symbols) */
210 	APlist		*ofl_extrarels;	/* relocation sections which have */
211 					/*    a NULL sh_info */
212 	avl_tree_t	*ofl_groups;	/* pointer to head of Groups AVL tree */
213 	APlist		*ofl_initarray;	/* list of init array func names */
214 	APlist		*ofl_finiarray;	/* list of fini array func names */
215 	APlist		*ofl_preiarray;	/* list of preinit array func names */
216 	APlist		*ofl_rtldinfo;	/* list of rtldinfo syms */
217 	APlist		*ofl_osgroups;	/* list of output GROUP sections */
218 	APlist		*ofl_ostlsseg;	/* pointer to sections in TLS segment */
219 	APlist		*ofl_unwind;	/* list of unwind output sections */
220 	Os_desc		*ofl_unwindhdr;	/* Unwind hdr */
221 	avl_tree_t	ofl_symavl;	/* pointer to head of Syms AVL tree */
222 	Sym_desc	**ofl_regsyms;	/* array of potential register */
223 	Word		ofl_regsymsno;	/*    symbols and array count */
224 	Word		ofl_regsymcnt;	/* no. of output register symbols */
225 	Word		ofl_lregsymcnt;	/* no. of local register symbols */
226 	Sym_desc	*ofl_dtracesym;	/* ld -zdtrace= */
227 	ofl_flag_t	ofl_flags;	/* various state bits, args etc. */
228 	ofl_flag_t	ofl_flags1;	/*	more flags */
229 	void		*ofl_entry;	/* entry point (-e and Sym_desc *) */
230 	char		*ofl_filtees;	/* shared objects we are a filter for */
231 	const char	*ofl_soname;	/* (-h option) output file name for */
232 					/*	dynamic structure */
233 	const char	*ofl_interp;	/* interpreter name used by exec() */
234 	char		*ofl_rpath;	/* run path to store in .dynamic */
235 	char		*ofl_config;	/* config path to store in .dynamic */
236 	APlist		*ofl_ulibdirs;	/* user supplied library search list */
237 	APlist		*ofl_dlibdirs;	/* default library search list */
238 	Word		ofl_vercnt;	/* number of versions to generate */
239 	APlist		*ofl_verdesc;	/* list of version descriptors */
240 	size_t		ofl_verdefsz;	/* size of version definition section */
241 	size_t		ofl_verneedsz;	/* size of version needed section */
242 	Word		ofl_entercnt;	/* no. of global symbols entered */
243 	Word		ofl_globcnt;	/* no. of global symbols to output */
244 	Word		ofl_scopecnt;	/* no. of scoped symbols to output */
245 	Word		ofl_dynscopecnt; /* no. scoped syms in .SUNW_ldynsym */
246 	Word		ofl_elimcnt;	/* no. of eliminated symbols */
247 	Word		ofl_locscnt;	/* no. of local symbols in .symtab */
248 	Word		ofl_dynlocscnt;	/* no. local symbols in .SUNW_ldynsym */
249 	Word		ofl_dynsymsortcnt; /* no. ndx in .SUNW_dynsymsort */
250 	Word		ofl_dyntlssortcnt; /* no. ndx in .SUNW_dyntlssort */
251 	Word		ofl_dynshdrcnt;	/* no. of output section in .dynsym */
252 	Word		ofl_shdrcnt;	/* no. of output sections */
253 	Str_tbl		*ofl_shdrsttab;	/* Str_tbl for shdr strtab */
254 	Str_tbl		*ofl_strtab;	/* Str_tbl for symtab strtab */
255 	Str_tbl		*ofl_dynstrtab;	/* Str_tbl for dymsym strtab */
256 	Gotndx		*ofl_tlsldgotndx; /* index to LD TLS_index structure */
257 	Xword		ofl_relocsz;	/* size of output relocations */
258 	Xword		ofl_relocgotsz;	/* size of .got relocations */
259 	Xword		ofl_relocpltsz;	/* size of .plt relocations */
260 	Xword		ofl_relocbsssz;	/* size of .bss (copy) relocations */
261 	Xword		ofl_relocrelsz;	/* size of .rel[a] relocations */
262 	Word		ofl_relocincnt;	/* no. of input relocations */
263 	Word		ofl_reloccnt;	/* tot number of output relocations */
264 	Word		ofl_reloccntsub; /* tot numb of output relocations to */
265 					/*	skip (-zignore) */
266 	Word		ofl_relocrelcnt; /* tot number of relative */
267 					/*	relocations */
268 	Word		ofl_gotcnt;	/* no. of .got entries */
269 	Word		ofl_pltcnt;	/* no. of .plt entries */
270 	Word		ofl_pltpad;	/* no. of .plt padd entries */
271 	Word		ofl_hashbkts;	/* no. of hash buckets required */
272 	Is_desc		*ofl_isbss;	/* .bss input section (globals) */
273 	Is_desc		*ofl_islbss;	/* .lbss input section (globals) */
274 	Is_desc		*ofl_istlsbss;	/* .tlsbss input section (globals) */
275 	Is_desc		*ofl_isparexpn;	/* -z nopartial .data input section */
276 	Os_desc		*ofl_osdynamic;	/* .dynamic output section */
277 	Os_desc		*ofl_osdynsym;	/* .dynsym output section */
278 	Os_desc		*ofl_osldynsym;	/* .SUNW_ldynsym output section */
279 	Os_desc		*ofl_osdynstr;	/* .dynstr output section */
280 	Os_desc		*ofl_osdynsymsort; /* .SUNW_dynsymsort output section */
281 	Os_desc		*ofl_osdyntlssort; /* .SUNW_dyntlssort output section */
282 	Os_desc		*ofl_osgot;	/* .got output section */
283 	Os_desc		*ofl_oshash;	/* .hash output section */
284 	Os_desc		*ofl_osinitarray; /* .initarray output section */
285 	Os_desc		*ofl_osfiniarray; /* .finiarray output section */
286 	Os_desc		*ofl_ospreinitarray; /* .preinitarray output section */
287 	Os_desc		*ofl_osinterp;	/* .interp output section */
288 	Os_desc		*ofl_oscap;	/* .SUNW_cap output section */
289 	Os_desc		*ofl_osplt;	/* .plt output section */
290 	Os_desc		*ofl_osmove;	/* .SUNW_move output section */
291 	Os_desc		*ofl_osrelhead;	/* first relocation section */
292 	Os_desc		*ofl_osrel;	/* .rel[a] relocation section */
293 	Os_desc		*ofl_osshstrtab; /* .shstrtab output section */
294 	Os_desc		*ofl_osstrtab;	/* .strtab output section */
295 	Os_desc		*ofl_ossymtab;	/* .symtab output section */
296 	Os_desc		*ofl_ossymshndx; /* .symtab_shndx output section */
297 	Os_desc		*ofl_osdynshndx; /* .dynsym_shndx output section */
298 	Os_desc		*ofl_osldynshndx; /* .SUNW_ldynsym_shndx output sec */
299 	Os_desc		*ofl_osverdef;	/* .version definition output section */
300 	Os_desc		*ofl_osverneed;	/* .version needed output section */
301 	Os_desc		*ofl_osversym;	/* .version symbol ndx output section */
302 	Word		ofl_dtflags_1;	/* DT_FLAGS_1 entries */
303 	Word		ofl_dtflags;	/* DT_FLAGS entries */
304 	Os_desc		*ofl_ossyminfo;	/* .SUNW_syminfo output section */
305 	Half		ofl_parexpnndx;	/* -z nopartial section index */
306 					/* Ref. at perform_outreloc() in */
307 					/* libld/{mach}/machrel.c */
308 	Xword		*ofl_checksum;	/* DT_CHECKSUM value address */
309 	char		*ofl_depaudit;	/* dependency auditing required (-P) */
310 	char		*ofl_audit;	/* object auditing required (-p) */
311 	Alist		*ofl_symfltrs;	/* per-symbol filtees and their */
312 	Alist		*ofl_dtsfltrs;	/*	associated .dynamic/.dynstrs */
313 	Xword		ofl_hwcap_1;	/* hardware capabilities */
314 	Xword		ofl_sfcap_1;	/* software capabilities */
315 	Lm_list		*ofl_lml;	/* runtime link-map list */
316 	Gottable	*ofl_gottable;	/* debugging got information */
317 	Rlxrel_cache	ofl_sr_cache;	/* Cache last result from */
318 					/*	sloppy_comdat_reloc() */
319 	APlist		*ofl_maptext;	/* mapfile added text sections */
320 	APlist		*ofl_mapdata;	/* mapfile added data sections */
321 	avl_tree_t	*ofl_wrap;	/* -z wrap symbols */
322 };
323 
324 #define	FLG_OF_DYNAMIC	0x00000001	/* generate dynamic output module */
325 #define	FLG_OF_STATIC	0x00000002	/* generate static output module */
326 #define	FLG_OF_EXEC	0x00000004	/* generate an executable */
327 #define	FLG_OF_RELOBJ	0x00000008	/* generate a relocatable object */
328 #define	FLG_OF_SHAROBJ	0x00000010	/* generate a shared object */
329 #define	FLG_OF_BFLAG	0x00000020	/* do no special plt building: -b */
330 #define	FLG_OF_IGNENV	0x00000040	/* ignore LD_LIBRARY_PATH: -i */
331 #define	FLG_OF_STRIP	0x00000080	/* strip output: -s */
332 #define	FLG_OF_NOWARN	0x00000100	/* disable symbol warnings: -t */
333 #define	FLG_OF_NOUNDEF	0x00000200	/* allow no undefined symbols: -zdefs */
334 #define	FLG_OF_PURETXT	0x00000400	/* allow no text relocations: -ztext */
335 #define	FLG_OF_GENMAP	0x00000800	/* generate a memory map: -m */
336 #define	FLG_OF_DYNLIBS	0x00001000	/* dynamic input allowed: -Bdynamic */
337 #define	FLG_OF_SYMBOLIC	0x00002000	/* bind global symbols: -Bsymbolic */
338 #define	FLG_OF_ADDVERS	0x00004000	/* add version stamp: -Qy */
339 #define	FLG_OF_NOLDYNSYM 0x00008000	/* -znoldynsym set */
340 #define	FLG_OF_SEGORDER	0x00010000	/* segment ordering is required */
341 #define	FLG_OF_SEGSORT	0x00020000	/* segment sorting is required */
342 #define	FLG_OF_TEXTREL	0x00040000	/* text relocations have been found */
343 #define	FLG_OF_MULDEFS	0x00080000	/* multiple symbols are allowed */
344 #define	FLG_OF_TLSPHDR	0x00100000	/* a TLS program header is required */
345 #define	FLG_OF_BLDGOT	0x00200000	/* build GOT table */
346 #define	FLG_OF_VERDEF	0x00400000	/* record version definitions */
347 #define	FLG_OF_VERNEED	0x00800000	/* record version dependencies */
348 #define	FLG_OF_NOVERSEC 0x01000000	/* don't record version sections */
349 #define	FLG_OF_KEY	0x02000000	/* file requires sort keys */
350 #define	FLG_OF_PROCRED	0x04000000	/* process any symbol reductions by */
351 					/*	effecting the symbol table */
352 					/*	output and relocations */
353 #define	FLG_OF_SYMINFO	0x08000000	/* create a syminfo section */
354 #define	FLG_OF_AUX	0x10000000	/* ofl_filter is an auxiliary filter */
355 #define	FLG_OF_FATAL	0x20000000	/* fatal error during input */
356 #define	FLG_OF_WARN	0x40000000	/* warning during input processing. */
357 #define	FLG_OF_VERBOSE	0x80000000	/* -z verbose flag set */
358 
359 #define	FLG_OF_MAPSYMB	0x000100000000	/* symbolic scope definition seen */
360 #define	FLG_OF_MAPGLOB	0x000200000000	/* global scope definition seen */
361 #define	FLG_OF_COMREL	0x000400000000	/* -z combreloc set, which enables */
362 					/*	DT_RELACNT tracking, */
363 #define	FLG_OF_NOCOMREL	0x000800000000	/* -z nocombreloc set */
364 #define	FLG_OF_AUTOLCL	0x001000000000	/* automatically reduce unspecified */
365 					/*	global symbols to locals */
366 #define	FLG_OF_AUTOELM	0x002000000000	/* automatically eliminate */
367 					/*	unspecified global symbols */
368 #define	FLG_OF_REDLSYM	0x004000000000	/* reduce local symbols */
369 #define	FLG_OF_SECORDER	0x008000000000	/* section ordering is required */
370 #define	FLG_OF_OSABI	0x010000000000	/* tag object as ELFOSABI_SOLARIS */
371 #define	FLG_OF_ADJOSCNT	0x020000000000	/* ajust ofl_shdrcnt to accommodate */
372 					/*	discarded sections */
373 
374 /*
375  * In the flags1 arena, establish any options that are applicable to archive
376  * extraction first, and associate a mask.  These values are recorded with any
377  * archive descriptor so that they may be reset should the archive require a
378  * rescan to try and resolve undefined symbols.
379  */
380 #define	FLG_OF1_ALLEXRT	0x00000001	/* extract all members from an */
381 					/*	archive file */
382 #define	FLG_OF1_WEAKEXT	0x00000002	/* allow archive extraction to */
383 					/*	resolve weak references */
384 #define	MSK_OF1_ARCHIVE	0x00000003	/* archive flags mask */
385 
386 #define	FLG_OF1_NOINTRP	0x00000008	/* -z nointerp flag set */
387 #define	FLG_OF1_ZDIRECT	0x00000010	/* -z direct flag set */
388 #define	FLG_OF1_NDIRECT	0x00000020	/* no-direct bindings specified */
389 #define	FLG_OF1_OVHWCAP	0x00000040	/* override any input hardware or */
390 #define	FLG_OF1_OVSFCAP	0x00000080	/*	software capabilities */
391 #define	FLG_OF1_RELDYN	0x00000100	/* process .dynamic in rel obj */
392 #define	FLG_OF1_NRLXREL	0x00000200	/* -z norelaxreloc flag set */
393 #define	FLG_OF1_RLXREL	0x00000400	/* -z relaxreloc flag set */
394 #define	FLG_OF1_IGNORE	0x00000800	/* ignore unused dependencies */
395 
396 #define	FLG_OF1_NOSGHND	0x00001000	/* -z nosighandler flag set */
397 #define	FLG_OF1_TEXTOFF 0x00002000	/* text relocations are ok */
398 #define	FLG_OF1_ABSEXEC	0x00004000	/* -zabsexec set */
399 #define	FLG_OF1_LAZYLD	0x00008000	/* lazy loading of objects enabled */
400 #define	FLG_OF1_GRPPRM	0x00010000	/* dependencies are to have */
401 					/*	GROUPPERM enabled */
402 #define	FLG_OF1_OVRFLW	0x00020000	/* size exceeds 32-bit limitation */
403 					/*	of 32-bit libld */
404 #define	FLG_OF1_NOPARTI	0x00040000	/* -znopartial set */
405 #define	FLG_OF1_BSSOREL	0x00080000	/* output relocation against bss */
406 					/*	section */
407 #define	FLG_OF1_TLSOREL	0x00100000	/* output relocation against .tlsbss */
408 					/*	section */
409 #define	FLG_OF1_MEMORY	0x00200000	/* produce a memory model */
410 #define	FLG_OF1_NGLBDIR	0x00400000	/* no DT_1_DIRECT flag allowed */
411 #define	FLG_OF1_ENCDIFF	0x00800000	/* Host running linker has different */
412 					/*	byte order than output object */
413 #define	FLG_OF1_VADDR	0x01000000	/* user segment defines a vaddr */
414 #define	FLG_OF1_EXTRACT	0x02000000	/* archive member has been extracted */
415 #define	FLG_OF1_RESCAN	0x04000000	/* any archives should be rescanned */
416 #define	FLG_OF1_IGNPRC	0x08000000	/* ignore processing required */
417 #define	FLG_OF1_NCSTTAB	0x10000000	/* -znocompstrtab set */
418 #define	FLG_OF1_DONE	0x20000000	/* link-editor processing complete */
419 #define	FLG_OF1_NONREG	0x40000000	/* non-regular file specified as */
420 					/*	the output file */
421 #define	FLG_OF1_ALNODIR	0x80000000	/* establish NODIRECT for all */
422 					/*	exported interfaces. */
423 
424 /*
425  * Test to see if the output file would allow the presence of
426  * a .dynsym section.
427  */
428 #define	OFL_ALLOW_DYNSYM(_ofl) (((_ofl)->ofl_flags & \
429 	(FLG_OF_DYNAMIC | FLG_OF_RELOBJ)) == FLG_OF_DYNAMIC)
430 
431 /*
432  * Test to see if the output file would allow the presence of
433  * a .SUNW_ldynsym section. The requirements are that a .dynsym
434  * is allowed, and -znoldynsym has not been specified. Note that
435  * even if the answer is True (1), we will only generate one if there
436  * are local symbols that require it.
437  */
438 #define	OFL_ALLOW_LDYNSYM(_ofl) (((_ofl)->ofl_flags & \
439 	(FLG_OF_DYNAMIC | FLG_OF_RELOBJ | FLG_OF_NOLDYNSYM)) == FLG_OF_DYNAMIC)
440 
441 /*
442  * Test to see if relocation processing should be done. This is normally
443  * true, but can be disabled via the '-z noreloc' option. Note that
444  * relocatable objects are still relocated even if '-z noreloc' is present.
445  */
446 #define	OFL_DO_RELOC(_ofl) (((_ofl)->ofl_flags & FLG_OF_RELOBJ) || \
447 	!((_ofl)->ofl_dtflags_1 & DF_1_NORELOC))
448 
449 /*
450  * Determine whether a static executable is being built.
451  */
452 #define	OFL_IS_STATIC_EXEC(_ofl) (((_ofl)->ofl_flags & \
453 	(FLG_OF_STATIC | FLG_OF_EXEC)) == (FLG_OF_STATIC | FLG_OF_EXEC))
454 
455 /*
456  * Determine whether a static object is being built.  This macro is used
457  * to select the appropriate string table, and symbol table that other
458  * sections need to reference.
459  */
460 #define	OFL_IS_STATIC_OBJ(_ofl) ((_ofl)->ofl_flags & \
461 	(FLG_OF_RELOBJ | FLG_OF_STATIC))
462 
463 /*
464  * Macros for counting symbol table entries.  These are used to size symbol
465  * tables and associated sections (.syminfo, SUNW_capinfo, .hash, etc.) and
466  * set required sh_info entries (the offset to the first global symbol).
467  */
468 #define	SYMTAB_LOC_CNT(_ofl)		/* local .symtab entries */	\
469 	(2 +				/*    NULL and STT_FILE */	\
470 	(_ofl)->ofl_shdrcnt +		/*    section symbol */		\
471 	(_ofl)->ofl_scopecnt +		/*    scoped symbols */		\
472 	(_ofl)->ofl_locscnt)		/*    standard locals */
473 #define	SYMTAB_ALL_CNT(_ofl)		/* all .symtab entries */	\
474 	(SYMTAB_LOC_CNT(_ofl) +		/*    .symtab locals */		\
475 	(_ofl)->ofl_globcnt)		/*    standard globals */
476 
477 #define	DYNSYM_LOC_CNT(_ofl)		/* local .dynsym entries */	\
478 	(1 +				/*    NULL */			\
479 	(_ofl)->ofl_dynshdrcnt +	/*    section symbols */	\
480 	(_ofl)->ofl_lregsymcnt)		/*    local register symbols */
481 #define	DYNSYM_ALL_CNT(_ofl)		/* all .dynsym entries */	\
482 	(DYNSYM_LOC_CNT(_ofl) +		/*    .dynsym locals */		\
483 	(_ofl)->ofl_globcnt)		/*    standard globals */
484 
485 /*
486  * Define a move descriptor used within relocation structures.
487  */
488 typedef struct {
489 	Move		*mr_move;
490 	Sym_desc	*mr_sym;
491 } Mv_reloc;
492 
493 /*
494  * Relocation (active & output) processing structure - transparent to common
495  * code.
496  *
497  * Note that rel_raddend is primarily only of interest to RELA relocations,
498  * and is set to 0 for REL. However, there is an exception: If FLG_REL_NADDEND
499  * is set, then rel_raddend contains a replacement value for the implicit
500  * addend found in the relocation target.
501  */
502 struct rel_desc {
503 	Os_desc		*rel_osdesc;	/* output section reloc is against */
504 	Is_desc		*rel_isdesc;	/* input section reloc is against */
505 	const char	*rel_sname;	/* symbol name (may be "unknown") */
506 	Sym_desc	*rel_sym;	/* sym relocation is against */
507 	Sym_desc	*rel_usym;	/* strong sym if this is a weak pair */
508 	Mv_reloc	*rel_move;	/* move table information */
509 	Word		rel_flags;	/* misc. flags for relocations */
510 	Word		rel_rtype;	/* relocation type */
511 	Xword		rel_roffset;	/* relocation offset */
512 	Sxword		rel_raddend;	/* addend from input relocation */
513 	Word		rel_typedata;	/* ELF_R_TYPE_DATA(info) */
514 };
515 
516 /*
517  * common flags used on the Rel_desc structure (defined in machrel.h).
518  */
519 #define	FLG_REL_GOT	0x00000001	/* relocation against GOT */
520 #define	FLG_REL_PLT	0x00000002	/* relocation against PLT */
521 #define	FLG_REL_BSS	0x00000004	/* relocation against BSS */
522 #define	FLG_REL_LOAD	0x00000008	/* section loadable */
523 #define	FLG_REL_SCNNDX	0x00000010	/* use section index for symbol ndx */
524 #define	FLG_REL_CLVAL	0x00000020	/* clear VALUE for active relocation */
525 #define	FLG_REL_ADVAL	0x00000040	/* add VALUE for output relocation, */
526 					/*	only relevant to SPARC and */
527 					/*	R_SPARC_RELATIVE */
528 #define	FLG_REL_GOTCL	0x00000080	/* clear the GOT entry.  This is */
529 					/* relevant to RELA relocations, */
530 					/* not REL (i386) relocations */
531 #define	FLG_REL_MOVETAB	0x00000100	/* Relocation against .SUNW_move */
532 					/*	adjustments required before */
533 					/*	actual relocation */
534 #define	FLG_REL_NOINFO	0x00000200	/* Relocation comes from a section */
535 					/*	with a null sh_info field */
536 #define	FLG_REL_REG	0x00000400	/* Relocation target is reg sym */
537 #define	FLG_REL_FPTR	0x00000800	/* relocation against func. desc. */
538 #define	FLG_REL_RFPTR1	0x00001000	/* Relative relocation against */
539 					/*   1st part of FD */
540 #define	FLG_REL_RFPTR2	0x00002000	/* Relative relocation against */
541 					/*   2nd part of FD */
542 #define	FLG_REL_DISP	0x00004000	/* *disp* relocation */
543 #define	FLG_REL_STLS	0x00008000	/* IE TLS reference to */
544 					/*	static TLS GOT index */
545 #define	FLG_REL_DTLS	0x00010000	/* GD TLS reference relative to */
546 					/*	dynamic TLS GOT index */
547 #define	FLG_REL_MTLS	0x00020000	/* LD TLS reference against GOT */
548 #define	FLG_REL_STTLS	0x00040000	/* LE TLS reference directly */
549 					/*	to static tls index */
550 #define	FLG_REL_TLSFIX	0x00080000	/* relocation points to TLS instr. */
551 					/*	which needs updating */
552 #define	FLG_REL_RELA	0x00100000	/* descriptor captures a Rela */
553 #define	FLG_REL_GOTFIX	0x00200000	/* relocation points to GOTOP instr. */
554 					/*	which needs updating */
555 #define	FLG_REL_NADDEND	0x00400000	/* Replace implicit addend in dest */
556 					/*	with value in rel_raddend */
557 					/*	Relevant to REL (i386) */
558 					/*	relocations, not to RELA. */
559 
560 /*
561  * Structure to hold a cache of Relocations.
562  */
563 struct rel_cache {
564 	Rel_desc	*rc_end;
565 	Rel_desc	*rc_free;
566 };
567 
568 /*
569  * Symbol value descriptor.  For relocatable objects, each symbols value is
570  * its offset within its associated section.  Therefore, to uniquely define
571  * each symbol within a reloctable object, record and sort the sh_offset and
572  * symbol value.  This information is used to seach for displacement
573  * relocations as part of copy relocation validation.
574  */
575 typedef struct {
576 	Addr		ssv_value;
577 	Sym_desc	*ssv_sdp;
578 } Ssv_desc;
579 
580 /*
581  * Input file processing structures.
582  */
583 struct ifl_desc {			/* input file descriptor */
584 	const char	*ifl_name;	/* full file name */
585 	const char	*ifl_soname;	/* shared object name */
586 	dev_t		ifl_stdev;	/* device id and inode number for .so */
587 	ino_t		ifl_stino;	/*	multiple inclusion checks */
588 	Ehdr		*ifl_ehdr;	/* elf header describing this file */
589 	Elf		*ifl_elf;	/* elf descriptor for this file */
590 	Sym_desc	**ifl_oldndx;	/* original symbol table indices */
591 	Sym_desc	*ifl_locs;	/* symbol desc version of locals */
592 	Ssv_desc	*ifl_sortsyms;	/* sorted list of symbols by value */
593 	Word		ifl_locscnt;	/* no. of local symbols to process */
594 	Word		ifl_symscnt;	/* total no. of symbols to process */
595 	Word		ifl_sortcnt;	/* no. of sorted symbols to process */
596 	Word		ifl_shnum;	/* number of sections in file */
597 	Word		ifl_shstrndx;	/* index to .shstrtab */
598 	Word		ifl_vercnt;	/* number of versions in file */
599 	Half		ifl_neededndx;	/* index to NEEDED in .dyn section */
600 	Word		ifl_flags;	/* explicit/implicit reference */
601 	Is_desc		**ifl_isdesc;	/* isdesc[scn ndx] = Is_desc ptr */
602 	Sdf_desc	*ifl_sdfdesc;	/* control definition */
603 	Versym		*ifl_versym;	/* version symbol table array */
604 	Ver_index	*ifl_verndx;	/* verndx[ver ndx] = Ver_index */
605 	APlist		*ifl_verdesc;	/* version descriptor list */
606 	APlist		*ifl_relsect;	/* relocation section list */
607 	Alist		*ifl_groups;	/* SHT_GROUP section list */
608 };
609 
610 #define	FLG_IF_CMDLINE	0x00000001	/* full filename specified from the */
611 					/*	command line (no -l) */
612 #define	FLG_IF_NEEDED	0x00000002	/* shared object should be recorded */
613 #define	FLG_IF_DIRECT	0x00000004	/* establish direct bindings to this */
614 					/*	object */
615 #define	FLG_IF_EXTRACT	0x00000008	/* file extracted from an archive */
616 #define	FLG_IF_VERNEED	0x00000010	/* version dependency information is */
617 					/*	required */
618 #define	FLG_IF_DEPREQD	0x00000020	/* dependency is required to satisfy */
619 					/*	symbol references */
620 #define	FLG_IF_NEEDSTR	0x00000040	/* dependency specified by -Nn */
621 					/*	flag */
622 #define	FLG_IF_IGNORE	0x00000080	/* ignore unused dependencies */
623 #define	FLG_IF_NODIRECT	0x00000100	/* object contains symbols that */
624 					/*	cannot be directly bound to */
625 #define	FLG_IF_LAZYLD	0x00000200	/* bindings to this object should be */
626 					/*	lazy loaded */
627 #define	FLG_IF_GRPPRM	0x00000400	/* this dependency should have the */
628 					/*	DF_P1_GROUPPERM flag set */
629 #define	FLG_IF_DISPPEND 0x00000800	/* displacement relocation done */
630 					/*	in the ld time. */
631 #define	FLG_IF_DISPDONE 0x00001000	/* displacement relocation done */
632 					/* 	at the run time */
633 #define	FLG_IF_MAPFILE	0x00002000	/* file is a mapfile */
634 #define	FLG_IF_HSTRTAB	0x00004000	/* file has a string section */
635 #define	FLG_IF_FILEREF	0x00008000	/* file contains a section which */
636 					/*	is included in the output */
637 					/*	allocatable image */
638 #define	FLG_IF_GNUVER	0x00010000	/* file used GNU-style versioning */
639 #define	FLG_IF_ORDERED	0x00020000	/* ordered section processing */
640 					/*	required */
641 
642 struct is_desc {			/* input section descriptor */
643 	const char	*is_name;	/* original section name */
644 	Shdr		*is_shdr;	/* the elf section header */
645 	Ifl_desc	*is_file;	/* infile desc for this section */
646 	Os_desc		*is_osdesc;	/* new output section for this */
647 					/*	input section */
648 	Elf_Data	*is_indata;	/* input sections raw data */
649 	Is_desc		*is_symshndx;	/* related SHT_SYM_SHNDX section */
650 	Is_desc		*is_comdatkeep;	/* If COMDAT section is discarded, */
651 					/* 	this is section that was kept */
652 	Word		is_scnndx;	/* original section index in file */
653 	Word		is_ordndx;	/* Index for section.  Used to decide */
654 					/*	where to insert section when */
655 					/* 	reordering sections */
656 	Word		is_keyident;	/* key for SHF_{ORDERED|LINK_ORDER} */
657 					/*	processing and ident used for */
658 					/*	 placing/ordering sections */
659 	Word		is_flags;	/* Various flags */
660 };
661 
662 #define	FLG_IS_ORDERED	0x0001		/* this is a SHF_ORDERED section */
663 #define	FLG_IS_KEY	0x0002		/* section requires sort keys */
664 #define	FLG_IS_DISCARD	0x0004		/* section is to be discarded */
665 #define	FLG_IS_RELUPD	0x0008		/* symbol defined here may have moved */
666 #define	FLG_IS_SECTREF	0x0010		/* section has been referenced */
667 #define	FLG_IS_GDATADEF	0x0020		/* section contains global data sym */
668 #define	FLG_IS_EXTERNAL	0x0040		/* isp from an user file */
669 #define	FLG_IS_INSTRMRG	0x0080		/* Usable SHF_MERGE|SHF_STRINGS sec */
670 #define	FLG_IS_GNSTRMRG	0x0100		/* Generated mergeable string section */
671 #define	FLG_IS_GROUPS	0x0200		/* section has groups to process */
672 #define	FLG_IS_PLACE	0x0400		/* section requires to be placed */
673 #define	FLG_IS_COMDAT	0x0800		/* section is COMDAT */
674 #define	FLG_IS_EHFRAME	0x1000		/* section is .eh_frame */
675 
676 /*
677  * Output sections contain lists of input sections that are assigned to them.
678  * These items fall into 4 categories:
679  *	BEFORE - Ordered sections that specify SHN_BEFORE, in input order.
680  *	ORDERED - Ordered sections that are sorted using unsorted sections
681  *		as the sort key.
682  *	DEFAULT - Sections that are placed into the output section
683  *		in input order.
684  *	AFTER - Ordered sections that specify SHN_AFTER, in input order.
685  */
686 #define	OS_ISD_BEFORE	0
687 #define	OS_ISD_ORDERED	1
688 #define	OS_ISD_DEFAULT	2
689 #define	OS_ISD_AFTER	3
690 #define	OS_ISD_NUM	4
691 typedef APlist *os_isdecs_arr[OS_ISD_NUM];
692 
693 /*
694  * Convenience macro for traversing every input section associated
695  * with a given output section. The primary benefit of this macro
696  * is that it preserves a precious level of code indentation in the
697  * code that uses it.
698  */
699 #define	OS_ISDESCS_TRAVERSE(_list_idx, _osp, _idx, _isp) \
700 	for (_list_idx = 0; _list_idx < OS_ISD_NUM; _list_idx++) \
701 		for (APLIST_TRAVERSE(_osp->os_isdescs[_list_idx], _idx, _isp))
702 
703 
704 /*
705  * Map file and output file processing structures
706  */
707 struct os_desc {			/* Output section descriptor */
708 	const char	*os_name;	/* the section name */
709 	Elf_Scn		*os_scn;	/* the elf section descriptor */
710 	Shdr		*os_shdr;	/* the elf section header */
711 	Os_desc		*os_relosdesc;	/* the output relocation section */
712 	APlist		*os_relisdescs;	/* reloc input section descriptors */
713 					/*	for this output section */
714 	os_isdecs_arr	os_isdescs;	/* lists of input sections in output */
715 	APlist		*os_mstrisdescs; /* FLG_IS_INSTRMRG input sections */
716 	Sg_desc		*os_sgdesc;	/* segment os_desc is placed on */
717 	Elf_Data	*os_outdata;	/* output sections raw data */
718 	avl_tree_t	*os_comdats;	/* AVL tree of COMDAT input sections */
719 					/*	associated to output section */
720 	Word		os_identndx;	/* section identifier for input */
721 					/*	section processing, followed */
722 					/*	by section symbol index */
723 	Word		os_ordndx;	/* index for section.  Used to decide */
724 					/*	where to insert section when */
725 					/* 	reordering sections */
726 	Xword		os_szoutrels;	/* size of output relocation section */
727 	uint_t		os_namehash;	/* hash on section name */
728 	uchar_t		os_flags;	/* various flags */
729 };
730 
731 #define	FLG_OS_KEY		0x01	/* section requires sort keys */
732 #define	FLG_OS_OUTREL		0x02	/* output rel against this section */
733 #define	FLG_OS_SECTREF		0x04	/* isps are not affected by -zignore */
734 
735 /*
736  * Types of segment index.
737  */
738 typedef enum {
739 	LD_PHDR,
740 	LD_INTERP,
741 	LD_SUNWCAP,
742 	LD_TEXT,
743 	LD_DATA,
744 	LD_BSS,
745 #if	defined(_ELF64)
746 	LD_LRODATA,		/* (amd64-only) */
747 	LD_LDATA,		/* (amd64-only) */
748 #endif
749 	LD_DYN,
750 	LD_DTRACE,
751 	LD_TLS,
752 	LD_UNWIND,
753 	LD_NOTE,
754 	LD_EXTRA,
755 	LD_NUM
756 } Segment_id;
757 
758 struct sg_desc {			/* output segment descriptor */
759 	Segment_id	sg_id;		/* segment identifier (for sorting) */
760 	Phdr		sg_phdr;	/* segment header for output file */
761 	const char	*sg_name;	/* segment name */
762 	Xword		sg_round;	/* data rounding required (mapfile) */
763 	Xword		sg_length;	/* maximum segment length; if 0 */
764 					/*	segment is not specified */
765 	APlist		*sg_osdescs;	/* list of output section descriptors */
766 	APlist		*sg_secorder;	/* list specifying section ordering */
767 					/*	for the segment */
768 	Half		sg_flags;
769 	Sym_desc	*sg_sizesym;	/* size symbol for this segment */
770 	Xword		sg_addralign;	/* LCM of sh_addralign */
771 	Elf_Scn		*sg_fscn;	/* the SCN of the first section. */
772 };
773 
774 #define	FLG_SG_VADDR	0x0001		/* vaddr segment attribute set */
775 #define	FLG_SG_PADDR	0x0002		/* paddr segment attribute set */
776 #define	FLG_SG_LENGTH	0x0004		/* length segment attribute set */
777 #define	FLG_SG_ALIGN	0x0008		/* align segment attribute set */
778 #define	FLG_SG_ROUND	0x0010		/* round segment attribute set */
779 #define	FLG_SG_FLAGS	0x0020		/* flags segment attribute set */
780 #define	FLG_SG_TYPE	0x0040		/* type segment attribute set */
781 #define	FLG_SG_ORDER	0x0080		/* has ordering been turned on for */
782 					/* 	this segment. */
783 					/*	i.e. ?[O] option in mapfile */
784 #define	FLG_SG_NOHDR	0x0100		/* don't map ELF or phdrs into */
785 					/* 	this segment */
786 #define	FLG_SG_EMPTY	0x0200		/* an empty segment specification */
787 					/*	no input sections will be */
788 					/*	associated to this section */
789 #define	FLG_SG_KEY	0x0400		/* segment requires sort keys */
790 #define	FLG_SG_DISABLED	0x0800		/* this segment is disabled */
791 #define	FLG_SG_PHREQ	0x1000		/* this segment requires a program */
792 					/* header */
793 
794 struct sec_order {
795 	const char	*sco_secname;	/* section name to be ordered */
796 	Word		sco_index;	/* ordering index for section */
797 	Half		sco_flags;
798 };
799 
800 #define	FLG_SGO_USED	0x0001		/* was ordering used? */
801 
802 struct ent_desc {			/* input section entrance criteria */
803 	APlist		*ec_files;	/* files from which to accept */
804 					/*	sections */
805 	const char	*ec_name;	/* name to match (NULL if none) */
806 	Word		ec_type;	/* section type */
807 	Word		ec_attrmask;	/* section attribute mask (AWX) */
808 	Word		ec_attrbits;	/* sections attribute bits */
809 	Sg_desc		*ec_segment;	/* output segment to enter if matched */
810 	Word		ec_ordndx;	/* index to determine where section */
811 					/*	meeting this criteria should */
812 					/*	inserted. Used for reordering */
813 					/*	of sections. */
814 	Half		ec_flags;
815 };
816 
817 #define	FLG_EC_BUILTIN	0x0001		/* built in descriptor */
818 #define	FLG_EC_USED	0x0002		/* entrance criteria met? */
819 
820 /*
821  * One structure is allocated for a move entry, and associated to the symbol
822  * against which a move is targeted.
823  */
824 typedef struct {
825 	Move		*md_move;	/* original Move entry */
826 	Xword		md_start;	/* start position */
827 	Xword		md_len;		/* length of initialization */
828 	Word 		md_oidx;	/* output Move entry index */
829 } Mv_desc;
830 
831 /*
832  * Symbol descriptor.
833  */
834 typedef	Lword		sd_flag_t;
835 struct sym_desc {
836 	Alist		*sd_GOTndxs;	/* list of associated GOT entries */
837 	Sym		*sd_sym;	/* pointer to symbol table entry */
838 	Sym		*sd_osym;	/* copy of the original symbol entry */
839 					/*	used only for local partial */
840 	Alist		*sd_move;	/* move information associated with a */
841 					/*	partially initialized symbol */
842 	const char	*sd_name;	/* symbols name */
843 	Ifl_desc	*sd_file;	/* file where symbol is taken */
844 	Is_desc		*sd_isc;	/* input section of symbol definition */
845 	Sym_aux		*sd_aux;	/* auxiliary global symbol info. */
846 	Word		sd_symndx;	/* index in output symbol table */
847 	Word		sd_shndx;	/* sect. index sym is associated w/ */
848 	sd_flag_t	sd_flags;	/* state flags */
849 	Half		sd_ref;		/* reference definition of symbol */
850 };
851 
852 /*
853  * The auxiliary symbol descriptor contains the additional information (beyond
854  * the symbol descriptor) required to process global symbols.  These symbols are
855  * accessed via an internal symbol hash table where locality of reference is
856  * important for performance.
857  */
858 struct sym_aux {
859 	APlist 		*sa_dfiles;	/* files where symbol is defined */
860 	Sym		sa_sym;		/* copy of symtab entry */
861 	const char	*sa_vfile;	/* first unavailable definition */
862 	const char	*sa_rfile;	/* file with first symbol referenced */
863 	Word		sa_hash;	/* the pure hash value of symbol */
864 	Word		sa_PLTndx;	/* index into PLT for symbol */
865 	Word		sa_PLTGOTndx;	/* GOT entry indx for PLT indirection */
866 	Word		sa_linkndx;	/* index of associated symbol from */
867 					/*	ET_DYN file */
868 	Half		sa_symspec;	/* special symbol ids */
869 	Half		sa_overndx;	/* output file versioning index */
870 	Half		sa_dverndx;	/* dependency versioning index */
871 };
872 
873 /*
874  * Nodes used to track symbols in the global AVL symbol dictionary.
875  */
876 struct sym_avlnode {
877 	avl_node_t	sav_node;	/* AVL node */
878 	Word		sav_hash;	/* symbol hash value */
879 	const char	*sav_name;	/* symbol name */
880 	Sym_desc	*sav_sdp;	/* symbol descriptor */
881 };
882 
883 /*
884  * These are the ids for processing of `Special symbols'.  They are used
885  * to set the sym->sd_aux->sa_symspec field.
886  */
887 #define	SDAUX_ID_ETEXT	1		/* etext && _etext symbol */
888 #define	SDAUX_ID_EDATA	2		/* edata && _edata symbol */
889 #define	SDAUX_ID_END	3		/* end, _end, && _END_ symbol */
890 #define	SDAUX_ID_DYN	4		/* DYNAMIC && _DYNAMIC symbol */
891 #define	SDAUX_ID_PLT	5		/* _PROCEDURE_LINKAGE_TABLE_ symbol */
892 #define	SDAUX_ID_GOT	6		/* _GLOBAL_OFFSET_TABLE_ symbol */
893 #define	SDAUX_ID_START	7		/* START_ && _START_ symbol */
894 
895 /*
896  * Flags for sym_desc.sd_flags
897  */
898 #define	FLG_SY_MVTOCOMM	0x00000001	/* assign symbol to common (.bss) */
899 					/*	this is a result of a */
900 					/*	copy reloc against sym */
901 #define	FLG_SY_GLOBREF	0x00000002	/* a global reference has been seen */
902 #define	FLG_SY_WEAKDEF	0x00000004	/* a weak definition has been used */
903 #define	FLG_SY_CLEAN	0x00000008	/* `Sym' entry points to original */
904 					/*	input file (read-only). */
905 #define	FLG_SY_UPREQD	0x00000010	/* symbol value update is required, */
906 					/*	either it's used as an entry */
907 					/*	point or for relocation, but */
908 					/*	it must be updated even if */
909 					/*	the -s flag is in effect */
910 #define	FLG_SY_NOTAVAIL	0x00000020	/* symbol is not available to the */
911 					/*	application either because it */
912 					/*	originates from an implicitly */
913 					/* 	referenced shared object, or */
914 					/*	because it is not part of a */
915 					/*	specified version. */
916 #define	FLG_SY_REDUCED	0x00000040	/* a global is reduced to local */
917 #define	FLG_SY_VERSPROM	0x00000080	/* version definition has been */
918 					/*	promoted to output file */
919 #define	FLG_SY_PROT	0x00000100	/* stv_protected visibility seen */
920 
921 #define	FLG_SY_MAPREF	0x00000200	/* symbol reference generated by user */
922 					/*	from mapfile */
923 #define	FLG_SY_REFRSD	0x00000400	/* symbols sd_ref has been raised */
924 					/* 	due to a copy-relocs */
925 					/*	weak-strong pairing */
926 #define	FLG_SY_INTPOSE	0x00000800	/* symbol defines an interposer */
927 #define	FLG_SY_INVALID	0x00001000	/* unwanted/erroneous symbol */
928 #define	FLG_SY_SMGOT	0x00002000	/* small got index assigned to symbol */
929 					/*	sparc only */
930 #define	FLG_SY_PARENT	0x00004000	/* symbol to be found in parent */
931 					/*    only used with direct bindings */
932 #define	FLG_SY_LAZYLD	0x00008000	/* symbol to cause lazyloading of */
933 					/*	parent object */
934 #define	FLG_SY_ISDISC	0x00010000	/* symbol is a member of a DISCARDED */
935 					/*	section (COMDAT) */
936 #define	FLG_SY_PAREXPN	0x00020000	/* partially init. symbol to be */
937 					/*	expanded */
938 #define	FLG_SY_PLTPAD	0x00040000	/* pltpadding has been allocated for */
939 					/*	this symbol */
940 #define	FLG_SY_REGSYM	0x00080000	/* REGISTER symbol (sparc only) */
941 #define	FLG_SY_SOFOUND	0x00100000	/* compared against an SO definition */
942 #define	FLG_SY_EXTERN	0x00200000	/* symbol is external, allows -zdefs */
943 					/*    error suppression */
944 #define	FLG_SY_MAPUSED	0x00400000	/* mapfile symbol used (occurred */
945 					/*    within a relocatable object) */
946 #define	FLG_SY_COMMEXP	0x00800000	/* COMMON symbol which has been */
947 					/*	allocated */
948 #define	FLG_SY_CMDREF	0x01000000	/* symbol was referenced from the */
949 					/*	command line.  (ld -u <>, */
950 					/*	ld -zrtldinfo=<>, ...) */
951 #define	FLG_SY_SPECSEC	0x02000000	/* section index is reserved value */
952 					/*	ABS, COMMON, ... */
953 #define	FLG_SY_TENTSYM	0x04000000	/* tentative symbol */
954 #define	FLG_SY_VISIBLE	0x08000000	/* symbols visibility determined */
955 #define	FLG_SY_STDFLTR	0x10000000	/* symbol is a standard filter */
956 #define	FLG_SY_AUXFLTR	0x20000000	/* symbol is an auxiliary filter */
957 #define	FLG_SY_DYNSORT	0x40000000	/* req. in dyn[sym|tls]sort section */
958 #define	FLG_SY_NODYNSORT 0x80000000	/* excluded from dyn[sym_tls]sort sec */
959 
960 #define	FLG_SY_DEFAULT	0x0000100000000	/* global symbol, default */
961 #define	FLG_SY_SINGLE	0x0000200000000	/* global symbol, singleton defined */
962 #define	FLG_SY_PROTECT	0x0000400000000	/* global symbol, protected defined */
963 #define	FLG_SY_EXPORT	0x0000800000000	/* global symbol, exported defined */
964 
965 #define	MSK_SY_GLOBAL \
966 	(FLG_SY_DEFAULT | FLG_SY_SINGLE | FLG_SY_PROTECT | FLG_SY_EXPORT)
967 					/* this mask indicates that the */
968 					/*    symbol has been explicitly */
969 					/*    defined within a mapfile */
970 					/*    definition, and is a candidate */
971 					/*    for versioning */
972 
973 #define	FLG_SY_HIDDEN	0x0001000000000	/* global symbol, reduce to local */
974 #define	FLG_SY_ELIM	0x0002000000000	/* global symbol, eliminate */
975 #define	FLG_SY_IGNORE	0x0004000000000	/* global symbol, ignored */
976 
977 #define	MSK_SY_LOCAL	(FLG_SY_HIDDEN | FLG_SY_ELIM | FLG_SY_IGNORE)
978 					/* this mask allows all local state */
979 					/*    flags to be removed when the */
980 					/*    symbol is copy relocated */
981 
982 #define	FLG_SY_EXPDEF	0x0008000000000	/* symbol visibility defined */
983 					/*    explicitly */
984 
985 #define	MSK_SY_NOAUTO	(FLG_SY_SINGLE | FLG_SY_EXPORT | FLG_SY_EXPDEF)
986 					/* this mask indicates that the */
987 					/*    symbol is not a candidate for */
988 					/*    auto-reduction/elimination */
989 
990 #define	FLG_SY_MAPFILE	0x0010000000000	/* symbol attribute defined in a */
991 					/*    mapfile */
992 #define	FLG_SY_DIR	0x0020000000000	/* global symbol, direct bindings */
993 #define	FLG_SY_NDIR	0x0040000000000	/* global symbol, nondirect bindings */
994 #define	FLG_SY_OVERLAP	0x0080000000000	/* move entry overlap detected */
995 
996 /*
997  * Create a mask for (sym.st_other & visibility) since the gABI does not yet
998  * define a ELF*_ST_OTHER macro.
999  */
1000 #define	MSK_SYM_VISIBILITY	0x7
1001 
1002 /*
1003  * Structure to manage the shared object definition lists.  There are two lists
1004  * that use this structure:
1005  *
1006  *  -	ofl_soneed; maintain the list of implicitly required dependencies
1007  *	(ie. shared objects needed by other shared objects).  These definitions
1008  *	may include RPATH's required to locate the dependencies, and any
1009  *	version requirements.
1010  *
1011  *  -	ofl_socntl; maintains the shared object control definitions.  These are
1012  *	provided by the user (via a mapfile) and are used to indicate any
1013  *	version control requirements.
1014  */
1015 struct	sdf_desc {
1016 	const char	*sdf_name;	/* the shared objects file name */
1017 	char		*sdf_rpath;	/* library search path DT_RPATH */
1018 	const char	*sdf_rfile;	/* referencing file for diagnostics */
1019 	Ifl_desc	*sdf_file;	/* the final input file descriptor */
1020 	Alist		*sdf_vers;	/* list of versions that are required */
1021 					/*	from this object */
1022 	Alist		*sdf_verneed;	/* list of VERNEEDS to create for */
1023 					/*	object via mapfile ADDVERS */
1024 	Word		sdf_flags;
1025 };
1026 
1027 #define	FLG_SDF_SELECT	0x01		/* version control selection required */
1028 #define	FLG_SDF_VERIFY	0x02		/* version definition verification */
1029 					/*	required */
1030 #define	FLG_SDF_ADDVER	0x04		/* add VERNEED references */
1031 
1032 /*
1033  * Structure to manage shared object version usage requirements.
1034  */
1035 struct	sdv_desc {
1036 	const char	*sdv_name;	/* version name */
1037 	const char	*sdv_ref;	/* versions reference */
1038 	Word		sdv_flags;	/* flags */
1039 };
1040 
1041 #define	FLG_SDV_MATCHED	0x01		/* VERDEF found and matched */
1042 
1043 /*
1044  * Structures to manage versioning information.  Two versioning structures are
1045  * defined:
1046  *
1047  *   -	a version descriptor maintains a linked list of versions and their
1048  *	associated dependencies.  This is used to build the version definitions
1049  *	for an image being created (see map_symbol), and to determine the
1050  *	version dependency graph for any input files that are versioned.
1051  *
1052  *   -	a version index array contains each version of an input file that is
1053  *	being processed.  It informs us which versions are available for
1054  *	binding, and is used to generate any version dependency information.
1055  */
1056 struct	ver_desc {
1057 	const char	*vd_name;	/* version name */
1058 	Ifl_desc	*vd_file;	/* file that defined version */
1059 	Word		vd_hash;	/* hash value of name */
1060 	Half		vd_ndx;		/* coordinates with symbol index */
1061 	Half		vd_flags;	/* version information */
1062 	APlist		*vd_deps;	/* version dependencies */
1063 	Ver_desc	*vd_ref;	/* dependency's first reference */
1064 };
1065 
1066 struct	ver_index {
1067 	const char	*vi_name;	/* dependency version name */
1068 	Half		vi_flags;	/* communicates availability */
1069 	Half		vi_overndx;	/* Index asssigned to this version in */
1070 					/*	output object Verneed section */
1071 	Ver_desc	*vi_desc;	/* cross reference to descriptor */
1072 };
1073 
1074 /*
1075  * Define any internal version descriptor flags ([vd|vi]_flags).  Note that the
1076  * first byte is reserved for user visible flags (refer VER_FLG's in link.h).
1077  */
1078 #define	MSK_VER_USER	0x0f		/* mask for user visible flags */
1079 
1080 #define	FLG_VER_AVAIL	0x10		/* version is available for binding */
1081 #define	FLG_VER_REFER	0x20		/* version has been referenced */
1082 #define	FLG_VER_CYCLIC	0x40		/* a member of cyclic dependency */
1083 
1084 /*
1085  * isalist(1) descriptor - used to break an isalist string into its component
1086  * options.
1087  */
1088 struct	isa_opt {
1089 	char		*isa_name;	/* individual isa option name */
1090 	size_t		isa_namesz;	/*	and associated size */
1091 };
1092 
1093 struct	isa_desc {
1094 	char		*isa_list;	/* sysinfo(SI_ISALIST) list */
1095 	size_t		isa_listsz;	/*	and associated size */
1096 	Isa_opt		*isa_opt;	/* table of individual isa options */
1097 	size_t		isa_optno;	/*	and associated number */
1098 };
1099 
1100 /*
1101  * uname(2) descriptor - used to break a utsname structure into its component
1102  * options (at least those that we're interested in).
1103  */
1104 struct	uts_desc {
1105 	char		*uts_osname;	/* operating system name */
1106 	size_t		uts_osnamesz;	/*	and associated size */
1107 	char		*uts_osrel;	/* operating system release */
1108 	size_t		uts_osrelsz;	/*	and associated size */
1109 };
1110 
1111 /*
1112  * SHT_GROUP descriptor - used to track group sections at the global
1113  * level to resolve conflicts and determine which to keep.
1114  */
1115 struct group_desc {
1116 	Is_desc		*gd_isc;	/* input section descriptor */
1117 	Is_desc		*gd_oisc;	/* overriding input section */
1118 					/*	descriptor when discarded */
1119 	const char	*gd_name;	/* group name (signature symbol) */
1120 	Word		*gd_data;	/* data for group section */
1121 	size_t		gd_cnt;		/* number of entries in group data */
1122 };
1123 
1124 /*
1125  * Indexes into the ld_support_funcs[] table.
1126  */
1127 typedef enum {
1128 	LDS_VERSION = 0,	/* Must be first and have value 0 */
1129 	LDS_INPUT_DONE,
1130 	LDS_START,
1131 	LDS_ATEXIT,
1132 	LDS_OPEN,
1133 	LDS_FILE,
1134 	LDS_INSEC,
1135 	LDS_SEC,
1136 	LDS_NUM
1137 } Support_ndx;
1138 
1139 /*
1140  * Structure to manage archive member caching.  Each archive has an archive
1141  * descriptor (Ar_desc) associated with it.  This contains pointers to the
1142  * archive symbol table (obtained by elf_getarsyms(3e)) and an auxiliary
1143  * structure (Ar_uax[]) that parallels this symbol table.  The member element
1144  * of this auxiliary table indicates whether the archive member associated with
1145  * the symbol offset has already been extracted (AREXTRACTED) or partially
1146  * processed (refer process_member()).
1147  */
1148 typedef struct ar_mem {
1149 	Elf		*am_elf;	/* elf descriptor for this member */
1150 	char		*am_name;	/* members name */
1151 	char		*am_path;	/* path (ie. lib(foo.o)) */
1152 	Sym		*am_syms;	/* start of global symbols */
1153 	char		*am_strs;	/* associated string table start */
1154 	Xword		am_symn;	/* no. of global symbols */
1155 } Ar_mem;
1156 
1157 typedef struct ar_aux {
1158 	Sym_desc	*au_syms;	/* internal symbol descriptor */
1159 	Ar_mem		*au_mem;	/* associated member */
1160 } Ar_aux;
1161 
1162 #define	FLG_ARMEM_PROC	(Ar_mem *)-1
1163 
1164 typedef struct ar_desc {
1165 	const char	*ad_name;	/* archive file name */
1166 	Elf		*ad_elf;	/* elf descriptor for the archive */
1167 	Elf_Arsym	*ad_start;	/* archive symbol table start */
1168 	Ar_aux		*ad_aux;	/* auxiliary symbol information */
1169 	dev_t		ad_stdev;	/* device id and inode number for */
1170 	ino_t		ad_stino;	/*	multiple inclusion checks */
1171 	ofl_flag_t	ad_flags;	/* archive specific cmd line flags */
1172 } Ar_desc;
1173 
1174 /*
1175  * Define any archive descriptor flags.  NOTE, make sure they do not clash with
1176  * any output file descriptor archive extraction flags, as these are saved in
1177  * the same entry (see MSK_OF1_ARCHIVE).
1178  */
1179 #define	FLG_ARD_EXTRACT	0x00010000	/* archive member has been extracted */
1180 
1181 /*
1182  * Function Declarations.
1183  */
1184 #if	defined(_ELF64)
1185 
1186 #define	ld_create_outfile	ld64_create_outfile
1187 #define	ld_ent_setup		ld64_ent_setup
1188 #define	ld_init_strings		ld64_init_strings
1189 #define	ld_init_target		ld64_init_target
1190 #define	ld_make_sections	ld64_make_sections
1191 #define	ld_main			ld64_main
1192 #define	ld_ofl_cleanup		ld64_ofl_cleanup
1193 #define	ld_process_mem		ld64_process_mem
1194 #define	ld_reloc_init		ld64_reloc_init
1195 #define	ld_reloc_process	ld64_reloc_process
1196 #define	ld_sym_validate		ld64_sym_validate
1197 #define	ld_update_outfile	ld64_update_outfile
1198 
1199 #else
1200 
1201 #define	ld_create_outfile	ld32_create_outfile
1202 #define	ld_ent_setup		ld32_ent_setup
1203 #define	ld_init_strings		ld32_init_strings
1204 #define	ld_init_target		ld32_init_target
1205 #define	ld_make_sections	ld32_make_sections
1206 #define	ld_main			ld32_main
1207 #define	ld_ofl_cleanup		ld32_ofl_cleanup
1208 #define	ld_process_mem		ld32_process_mem
1209 #define	ld_reloc_init		ld32_reloc_init
1210 #define	ld_reloc_process	ld32_reloc_process
1211 #define	ld_sym_validate		ld32_sym_validate
1212 #define	ld_update_outfile	ld32_update_outfile
1213 
1214 #endif
1215 
1216 extern int		ld_getopt(Lm_list *, int, int, char **);
1217 
1218 extern int		ld32_main(int, char **, Half);
1219 extern int		ld64_main(int, char **, Half);
1220 
1221 extern uintptr_t	ld_create_outfile(Ofl_desc *);
1222 extern uintptr_t	ld_ent_setup(Ofl_desc *, Xword);
1223 extern uintptr_t	ld_init_strings(Ofl_desc *);
1224 extern int		ld_init_target(Lm_list *, Half mach);
1225 extern uintptr_t	ld_make_sections(Ofl_desc *);
1226 extern void		ld_ofl_cleanup(Ofl_desc *);
1227 extern Ifl_desc		*ld_process_mem(const char *, const char *, char *,
1228 			    size_t, Ofl_desc *, Rej_desc *);
1229 extern uintptr_t	ld_reloc_init(Ofl_desc *);
1230 extern uintptr_t	ld_reloc_process(Ofl_desc *);
1231 extern uintptr_t	ld_sym_validate(Ofl_desc *);
1232 extern uintptr_t	ld_update_outfile(Ofl_desc *);
1233 
1234 #ifdef	__cplusplus
1235 }
1236 #endif
1237 
1238 #endif	/* _LIBLD_H */
1239