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