xref: /illumos-gate/usr/src/cmd/sgs/libld/common/args.c (revision 66597161e2ba69a84fa138bce7ac02a1e6b9746c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27  */
28 /*
29  * Copyright (c) 2012, Joyent, Inc.  All rights reserved.
30  * Copyright 2017 RackTop Systems.
31  */
32 
33 /*
34  * Publicly available flags are defined in ld(1).   The following flags are
35  * private, and may be removed at any time.
36  *
37  *    OPTION			MEANING
38  *
39  *    -z dtrace=symbol		assigns symbol to PT_SUNWDTRACE segment,
40  *				providing scratch area for dtrace processing.
41  *
42  *    -z noreloc		suppress relocation processing.  This provides
43  *				a mechanism for validating kernel module symbol
44  *				resolution that would normally incur fatal
45  *				relocation errors.
46  *
47  *    -z rtldinfo=symbol	assigns symbol to SUNW_RTLDINF dynamic tag,
48  *				providing pre-initialization specific routines
49  *				for TLS initialization.
50  *
51  *    -z nointerp		suppress the addition of an interpreter
52  *				section.  This is used to generate the kernel,
53  *				but makes no sense to be used by anyone else.
54  *
55  *    -z norelaxreloc		suppress the automatic addition of relaxed
56  *				relocations to GNU linkonce/COMDAT sections.
57  *
58  *    -z nosighandler		suppress the registration of the signal handler
59  *				used to manage SIGBUS.
60  */
61 
62 /*
63  * The following flags are committed, and will not be removed, but are
64  * not publically documented, either because they are obsolete, or because
65  * they exist to work around defects in other software and are not of
66  * sufficient interest otherwise.
67  *
68  *    OPTION			MEANING
69  *
70  *    -Wl,...			compiler drivers and configuration tools
71  *				have been known to pass this compiler option
72  *				to ld(1).  Strip off the "-Wl," prefix and
73  *			        process the remainder (...) as a normal option.
74  */
75 
76 #include	<sys/link.h>
77 #include	<stdio.h>
78 #include	<fcntl.h>
79 #include	<string.h>
80 #include	<errno.h>
81 #include	<elf.h>
82 #include	<unistd.h>
83 #include	<debug.h>
84 #include	"msg.h"
85 #include	"_libld.h"
86 
87 /*
88  * Define a set of local argument flags, the settings of these will be
89  * verified in check_flags() and lead to the appropriate output file flags
90  * being initialized.
91  */
92 typedef	enum {
93 	SET_UNKNOWN = -1,
94 	SET_FALSE = 0,
95 	SET_TRUE = 1
96 } Setstate;
97 
98 static Setstate	dflag	= SET_UNKNOWN;
99 static Setstate	zdflag	= SET_UNKNOWN;
100 static Setstate	Qflag	= SET_UNKNOWN;
101 static Setstate	Bdflag	= SET_UNKNOWN;
102 static Setstate zfwflag	= SET_UNKNOWN;
103 
104 static Boolean	aflag	= FALSE;
105 static Boolean	bflag	= FALSE;
106 static Boolean	sflag	= FALSE;
107 static Boolean	zinflag = FALSE;
108 static Boolean	zlflag	= FALSE;
109 static Boolean	Bgflag	= FALSE;
110 static Boolean	Blflag	= FALSE;
111 static Boolean	Beflag	= FALSE;
112 static Boolean	Bsflag	= FALSE;
113 static Boolean	Dflag	= FALSE;
114 static Boolean	Vflag	= FALSE;
115 
116 enum output_type {
117 	OT_RELOC,		/* relocatable object */
118 	OT_SHARED,		/* shared object */
119 	OT_EXEC,		/* dynamic executable */
120 	OT_KMOD,		/* kernel module */
121 };
122 
123 static enum output_type	otype = OT_EXEC;
124 
125 /*
126  * ztflag's state is set by pointing it to the matching string:
127  *	text | textoff | textwarn
128  */
129 static const char	*ztflag = NULL;
130 
131 /*
132  * Remember the guidance flags that result from the initial -z guidance
133  * option, so that they can be compared to any that follow. We only want
134  * to issue a warning when they differ.
135  */
136 static ofl_guideflag_t	initial_guidance_flags	= 0;
137 
138 static uintptr_t process_files_com(Ofl_desc *, int, char **);
139 static uintptr_t process_flags_com(Ofl_desc *, int, char **, int *);
140 
141 /*
142  * Print usage message to stderr - 2 modes, summary message only,
143  * and full usage message.
144  */
145 static void
146 usage_mesg(Boolean detail)
147 {
148 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE),
149 	    MSG_ORIG(MSG_STR_OPTIONS));
150 
151 	if (detail == FALSE)
152 		return;
153 
154 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_3));
155 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_6));
156 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_A));
157 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_B));
158 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBDR));
159 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBDY));
160 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBE));
161 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBG));
162 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBL));
163 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBR));
164 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBS));
165 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_C));
166 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CC));
167 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_D));
168 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CD));
169 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_E));
170 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_F));
171 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CF));
172 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CG));
173 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_H));
174 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_I));
175 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CI));
176 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_L));
177 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CL));
178 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_M));
179 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CM));
180 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CN));
181 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_O));
182 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_P));
183 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CP));
184 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CQ));
185 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_R));
186 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CR));
187 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_S));
188 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CS));
189 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_T));
190 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_U));
191 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CV));
192 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CY));
193 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZA));
194 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZAE));
195 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZAL));
196 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZADLIB));
197 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZC));
198 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZDEF));
199 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZDFS));
200 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZDRS));
201 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZE));
202 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZFATW));
203 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZFA));
204 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZGP));
205 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZGUIDE));
206 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZH));
207 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZIG));
208 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZINA));
209 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZINI));
210 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZINT));
211 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZLAZY));
212 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZLD32));
213 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZLD64));
214 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZLO));
215 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZM));
216 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNC));
217 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDFS));
218 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDEF));
219 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDEL));
220 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDLO));
221 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDU));
222 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNLD));
223 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNOW));
224 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNPA));
225 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNV));
226 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZO));
227 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZPIA));
228 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRL));
229 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRREL));
230 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRS));
231 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRSN));
232 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRSGRP));
233 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZSCAP));
234 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZTARG));
235 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZT));
236 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZTO));
237 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZTW));
238 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZTY));
239 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZWRAP));
240 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZVER));
241 }
242 
243 /*
244  * Rescan the archives seen on the command line in order
245  * to handle circularly dependent archives, stopping when
246  * no further member extraction occurs.
247  *
248  * entry:
249  *	ofl - Output file descriptor
250  *	isgrp - True if this is a an archive group search, False
251  *		to search starting with argv[1] through end_arg_ndx
252  *	end_arg_ndx - Index of final argv element to consider.
253  */
254 static uintptr_t
255 ld_rescan_archives(Ofl_desc *ofl, int isgrp, int end_arg_ndx)
256 {
257 	ofl->ofl_flags1 |= FLG_OF1_EXTRACT;
258 
259 	while (ofl->ofl_flags1 & FLG_OF1_EXTRACT) {
260 		Aliste		idx;
261 		Ar_desc		*adp;
262 		Word		start_ndx = isgrp ? ofl->ofl_ars_gsndx : 0;
263 		Word		ndx = 0;
264 
265 		ofl->ofl_flags1 &= ~FLG_OF1_EXTRACT;
266 
267 		DBG_CALL(Dbg_file_ar_rescan(ofl->ofl_lml,
268 		    isgrp ? ofl->ofl_ars_gsandx : 1, end_arg_ndx));
269 
270 		for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
271 			/* If not to starting index yet, skip it */
272 			if (ndx++ < start_ndx)
273 				continue;
274 
275 			/*
276 			 * If this archive was processed with -z allextract,
277 			 * then all members have already been extracted.
278 			 */
279 			if (adp->ad_elf == NULL)
280 				continue;
281 
282 			/*
283 			 * Reestablish any archive specific command line flags.
284 			 */
285 			ofl->ofl_flags1 &= ~MSK_OF1_ARCHIVE;
286 			ofl->ofl_flags1 |= (adp->ad_flags & MSK_OF1_ARCHIVE);
287 
288 			/*
289 			 * Re-process the archive.  Note that a file descriptor
290 			 * is unnecessary, as the file is already available in
291 			 * memory.
292 			 */
293 			if (!ld_process_archive(adp->ad_name, -1, adp, ofl))
294 				return (S_ERROR);
295 			if (ofl->ofl_flags & FLG_OF_FATAL)
296 				return (1);
297 		}
298 	}
299 
300 	return (1);
301 }
302 
303 /*
304  * Checks the command line option flags for consistency.
305  */
306 static uintptr_t
307 check_flags(Ofl_desc * ofl, int argc)
308 {
309 	/*
310 	 * If the user specified -zguidance=noall, then we can safely disable
311 	 * the entire feature. The purpose of -zguidance=noall is to allow
312 	 * the user to override guidance specified from a makefile via
313 	 * the LD_OPTIONS environment variable, and so, we want to behave
314 	 * in exactly the same manner we would have if no option were present.
315 	 */
316 	if ((ofl->ofl_guideflags & (FLG_OFG_ENABLE | FLG_OFG_NO_ALL)) ==
317 	    (FLG_OFG_ENABLE | FLG_OFG_NO_ALL))
318 		ofl->ofl_guideflags &= ~FLG_OFG_ENABLE;
319 
320 	if (Plibpath && (Llibdir || Ulibdir))
321 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_YP),
322 		    Llibdir ? 'L' : 'U');
323 
324 	if ((otype == OT_RELOC) || (otype == OT_KMOD)) {
325 		if (otype == OT_RELOC) {
326 			if (dflag == SET_UNKNOWN)
327 				dflag = SET_FALSE;
328 		} else if (otype == OT_KMOD) {
329 			if (dflag != SET_UNKNOWN) {
330 				ld_eprintf(ofl, ERR_FATAL,
331 				    MSG_INTL(MSG_MARG_INCOMP),
332 				    MSG_INTL(MSG_MARG_TYPE_KMOD),
333 				    MSG_ORIG(MSG_ARG_D));
334 			}
335 
336 			dflag = SET_TRUE;
337 		}
338 
339 		/*
340 		 * Combining relocations when building a relocatable
341 		 * object isn't allowed.  Warn the user, but proceed.
342 		 */
343 		if (ofl->ofl_flags & FLG_OF_COMREL) {
344 			const char *msg;
345 
346 			if (otype == OT_RELOC) {
347 				msg = MSG_INTL(MSG_MARG_REL);
348 			} else {
349 				msg = MSG_INTL(MSG_MARG_TYPE_KMOD);
350 			}
351 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_MARG_INCOMP),
352 			    msg,
353 			    MSG_ORIG(MSG_ARG_ZCOMBRELOC));
354 		}
355 		ofl->ofl_flags |= FLG_OF_RELOBJ;
356 
357 		if (otype == OT_KMOD)
358 			ofl->ofl_flags |= FLG_OF_KMOD;
359 	} else {
360 		/*
361 		 * Translating object capabilities to symbol capabilities is
362 		 * only meaningful when creating a relocatable object.
363 		 */
364 		if (ofl->ofl_flags & FLG_OF_OTOSCAP)
365 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_MARG_ONLY),
366 			    MSG_ORIG(MSG_ARG_ZSYMBOLCAP),
367 			    MSG_INTL(MSG_MARG_REL));
368 
369 		/*
370 		 * If the user hasn't explicitly requested that relocations
371 		 * not be combined, combine them by default.
372 		 */
373 		if ((ofl->ofl_flags & FLG_OF_NOCOMREL) == 0)
374 			ofl->ofl_flags |= FLG_OF_COMREL;
375 	}
376 
377 	if (zdflag == SET_TRUE)
378 		ofl->ofl_flags |= FLG_OF_NOUNDEF;
379 
380 	if (zinflag)
381 		ofl->ofl_dtflags_1 |= DF_1_INTERPOSE;
382 
383 	if (sflag)
384 		ofl->ofl_flags |= FLG_OF_STRIP;
385 
386 	if (Qflag == SET_TRUE)
387 		ofl->ofl_flags |= FLG_OF_ADDVERS;
388 
389 	if (Blflag)
390 		ofl->ofl_flags |= FLG_OF_AUTOLCL;
391 
392 	if (Beflag)
393 		ofl->ofl_flags |= FLG_OF_AUTOELM;
394 
395 	if (Blflag && Beflag)
396 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_INCOMP),
397 		    MSG_ORIG(MSG_ARG_BELIMINATE), MSG_ORIG(MSG_ARG_BLOCAL));
398 
399 	if (ofl->ofl_interp && (ofl->ofl_flags1 & FLG_OF1_NOINTRP))
400 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_INCOMP),
401 		    MSG_ORIG(MSG_ARG_CI), MSG_ORIG(MSG_ARG_ZNOINTERP));
402 
403 	if ((ofl->ofl_flags1 & (FLG_OF1_NRLXREL | FLG_OF1_RLXREL)) ==
404 	    (FLG_OF1_NRLXREL | FLG_OF1_RLXREL))
405 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_INCOMP),
406 		    MSG_ORIG(MSG_ARG_ZRELAXRELOC),
407 		    MSG_ORIG(MSG_ARG_ZNORELAXRELOC));
408 
409 	if (ofl->ofl_filtees && (otype != OT_SHARED))
410 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_MARG_ST_ONLYAVL),
411 		    ((ofl->ofl_flags & FLG_OF_AUX) ?
412 		    MSG_INTL(MSG_MARG_FILTER_AUX) : MSG_INTL(MSG_MARG_FILTER)));
413 
414 	if (dflag != SET_FALSE) {
415 		/*
416 		 * Set -Bdynamic on by default, setting is rechecked as input
417 		 * files are processed.
418 		 */
419 		ofl->ofl_flags |=
420 		    (FLG_OF_DYNAMIC | FLG_OF_DYNLIBS | FLG_OF_PROCRED);
421 
422 		if (aflag)
423 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_INCOMP),
424 			    MSG_ORIG(MSG_ARG_DY), MSG_ORIG(MSG_ARG_A));
425 
426 		if (bflag)
427 			ofl->ofl_flags |= FLG_OF_BFLAG;
428 
429 		if (Bgflag == TRUE) {
430 			if (zdflag == SET_FALSE)
431 				ld_eprintf(ofl, ERR_FATAL,
432 				    MSG_INTL(MSG_ARG_INCOMP),
433 				    MSG_ORIG(MSG_ARG_BGROUP),
434 				    MSG_ORIG(MSG_ARG_ZNODEF));
435 			ofl->ofl_dtflags_1 |= DF_1_GROUP;
436 			ofl->ofl_flags |= FLG_OF_NOUNDEF;
437 		}
438 
439 		/*
440 		 * If the use of default library searching has been suppressed
441 		 * but no runpaths have been provided we're going to have a hard
442 		 * job running this object.
443 		 */
444 		if ((ofl->ofl_dtflags_1 & DF_1_NODEFLIB) && !ofl->ofl_rpath)
445 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_ARG_NODEFLIB),
446 			    MSG_INTL(MSG_MARG_RPATH));
447 
448 		/*
449 		 * By default, text relocation warnings are given when building
450 		 * an executable unless the -b flag is specified.  This option
451 		 * implies that unclean text can be created, so no warnings are
452 		 * generated unless specifically asked for.
453 		 */
454 		if ((ztflag == MSG_ORIG(MSG_ARG_ZTEXTOFF)) ||
455 		    ((ztflag == NULL) && bflag)) {
456 			ofl->ofl_flags1 |= FLG_OF1_TEXTOFF;
457 			ofl->ofl_guideflags |= FLG_OFG_NO_TEXT;
458 		} else if (ztflag == MSG_ORIG(MSG_ARG_ZTEXT)) {
459 			ofl->ofl_flags |= FLG_OF_PURETXT;
460 			ofl->ofl_guideflags |= FLG_OFG_NO_TEXT;
461 		}
462 
463 		if ((otype == OT_SHARED) || (otype == OT_EXEC)) {
464 			/*
465 			 * Create a dynamic object.  -Bdirect indicates that all
466 			 * references should be bound directly.  This also
467 			 * enables lazyloading.  Individual symbols can be
468 			 * bound directly (or not) using mapfiles and the
469 			 * DIRECT (NODIRECT) qualifier.  With this capability,
470 			 * each syminfo entry is tagged SYMINFO_FLG_DIRECTBIND.
471 			 * Prior to this per-symbol direct binding, runtime
472 			 * direct binding was controlled via the DF_1_DIRECT
473 			 * flag.  This flag affected all references from the
474 			 * object.  -Bdirect continues to set this flag, and
475 			 * thus provides a means of taking a newly built
476 			 * direct binding object back to older systems.
477 			 *
478 			 * NOTE, any use of per-symbol NODIRECT bindings, or
479 			 * -znodirect, will disable the creation of the
480 			 * DF_1_DIRECT flag.  Older runtime linkers do not
481 			 * have the capability to do per-symbol direct bindings.
482 			 */
483 			if (Bdflag == SET_TRUE) {
484 				ofl->ofl_dtflags_1 |= DF_1_DIRECT;
485 				ofl->ofl_flags1 |= FLG_OF1_LAZYLD;
486 				ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
487 				ofl->ofl_flags |= FLG_OF_SYMINFO;
488 			}
489 
490 			/*
491 			 * -Bnodirect disables directly binding to any symbols
492 			 * exported from the object being created.  Individual
493 			 * references to external objects can still be affected
494 			 * by -zdirect or mapfile DIRECT directives.
495 			 */
496 			if (Bdflag == SET_FALSE) {
497 				ofl->ofl_flags1 |= (FLG_OF1_NDIRECT |
498 				    FLG_OF1_NGLBDIR | FLG_OF1_ALNODIR);
499 				ofl->ofl_flags |= FLG_OF_SYMINFO;
500 			}
501 		}
502 
503 		if (otype == OT_EXEC) {
504 			/*
505 			 * Dynamically linked executable.
506 			 */
507 			ofl->ofl_flags |= FLG_OF_EXEC;
508 
509 			if (zdflag != SET_FALSE)
510 				ofl->ofl_flags |= FLG_OF_NOUNDEF;
511 
512 			/*
513 			 * -z textwarn is the default for executables, and
514 			 * only an explicit -z text* option can change that,
515 			 * so there's no need to provide additional guidance.
516 			 */
517 			ofl->ofl_guideflags |= FLG_OFG_NO_TEXT;
518 
519 			if (Bsflag)
520 				ld_eprintf(ofl, ERR_FATAL,
521 				    MSG_INTL(MSG_ARG_DY_INCOMP),
522 				    MSG_ORIG(MSG_ARG_BSYMBOLIC));
523 			if (ofl->ofl_soname)
524 				ld_eprintf(ofl, ERR_FATAL,
525 				    MSG_INTL(MSG_MARG_DY_INCOMP),
526 				    MSG_INTL(MSG_MARG_SONAME));
527 		} else if (otype == OT_SHARED) {
528 			/*
529 			 * Shared library.
530 			 */
531 			ofl->ofl_flags |= FLG_OF_SHAROBJ;
532 
533 			/*
534 			 * By default, print text relocation warnings for
535 			 * executables but *not* for shared objects. However,
536 			 * if -z guidance is on, issue warnings for shared
537 			 * objects as well.
538 			 *
539 			 * If -z textwarn is explicitly specified, also issue
540 			 * guidance messages if -z guidance is on, but not
541 			 * for -z text or -z textoff.
542 			 */
543 			if (ztflag == NULL) {
544 				if (!OFL_GUIDANCE(ofl, FLG_OFG_NO_TEXT))
545 					ofl->ofl_flags1 |= FLG_OF1_TEXTOFF;
546 			} else if ((ofl->ofl_flags & FLG_OF_PURETXT) ||
547 			    (ofl->ofl_flags1 & FLG_OF1_TEXTOFF)) {
548 				ofl->ofl_guideflags |= FLG_OFG_NO_TEXT;
549 			}
550 
551 			if (Bsflag) {
552 				/*
553 				 * -Bsymbolic, and -Bnodirect make no sense.
554 				 */
555 				if (Bdflag == SET_FALSE)
556 					ld_eprintf(ofl, ERR_FATAL,
557 					    MSG_INTL(MSG_ARG_INCOMP),
558 					    MSG_ORIG(MSG_ARG_BSYMBOLIC),
559 					    MSG_ORIG(MSG_ARG_BNODIRECT));
560 				ofl->ofl_flags |= FLG_OF_SYMBOLIC;
561 				ofl->ofl_dtflags |= DF_SYMBOLIC;
562 			}
563 		} else {
564 			/*
565 			 * Dynamic relocatable object.
566 			 */
567 			if (ztflag == NULL)
568 				ofl->ofl_flags1 |= FLG_OF1_TEXTOFF;
569 			ofl->ofl_guideflags |= FLG_OFG_NO_TEXT;
570 
571 			if (ofl->ofl_interp)
572 				ld_eprintf(ofl, ERR_FATAL,
573 				    MSG_INTL(MSG_MARG_INCOMP),
574 				    MSG_INTL(MSG_MARG_REL),
575 				    MSG_ORIG(MSG_ARG_CI));
576 		}
577 
578 		assert((ofl->ofl_flags & (FLG_OF_SHAROBJ|FLG_OF_EXEC)) !=
579 		    (FLG_OF_SHAROBJ|FLG_OF_EXEC));
580 	} else {
581 		ofl->ofl_flags |= FLG_OF_STATIC;
582 
583 		if (bflag)
584 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_ST_INCOMP),
585 			    MSG_ORIG(MSG_ARG_B));
586 		if (ofl->ofl_soname)
587 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_MARG_ST_INCOMP),
588 			    MSG_INTL(MSG_MARG_SONAME));
589 		if (ofl->ofl_depaudit)
590 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_ST_INCOMP),
591 			    MSG_ORIG(MSG_ARG_CP));
592 		if (ofl->ofl_audit)
593 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_ST_INCOMP),
594 			    MSG_ORIG(MSG_ARG_P));
595 		if (ofl->ofl_config)
596 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_ST_INCOMP),
597 			    MSG_ORIG(MSG_ARG_C));
598 		if (ztflag)
599 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_ST_INCOMP),
600 			    MSG_ORIG(MSG_ARG_ZTEXTALL));
601 		if (otype == OT_SHARED)
602 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_MARG_ST_INCOMP),
603 			    MSG_INTL(MSG_MARG_SO));
604 		if (aflag && (otype == OT_RELOC))
605 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_MARG_INCOMP),
606 			    MSG_ORIG(MSG_ARG_A), MSG_INTL(MSG_MARG_REL));
607 
608 		if (otype == OT_RELOC) {
609 			/*
610 			 * We can only strip the symbol table and string table
611 			 * if no output relocations will refer to them.
612 			 */
613 			if (sflag)
614 				ld_eprintf(ofl, ERR_WARNING,
615 				    MSG_INTL(MSG_ARG_STRIP),
616 				    MSG_INTL(MSG_MARG_REL),
617 				    MSG_INTL(MSG_MARG_STRIP));
618 
619 			if (ztflag == NULL)
620 				ofl->ofl_flags1 |= FLG_OF1_TEXTOFF;
621 			ofl->ofl_guideflags |= FLG_OFG_NO_TEXT;
622 
623 			if (ofl->ofl_interp)
624 				ld_eprintf(ofl, ERR_FATAL,
625 				    MSG_INTL(MSG_MARG_INCOMP),
626 				    MSG_INTL(MSG_MARG_REL),
627 				    MSG_ORIG(MSG_ARG_CI));
628 		} else {
629 			/*
630 			 * Static executable.
631 			 */
632 			ofl->ofl_flags |= FLG_OF_EXEC | FLG_OF_PROCRED;
633 
634 			if (zdflag != SET_FALSE)
635 				ofl->ofl_flags |= FLG_OF_NOUNDEF;
636 		}
637 	}
638 
639 	/*
640 	 * If the user didn't supply an output file name supply a default.
641 	 */
642 	if (ofl->ofl_name == NULL)
643 		ofl->ofl_name = MSG_ORIG(MSG_STR_AOUT);
644 
645 	/*
646 	 * We set the entrance criteria after all input argument processing as
647 	 * it is only at this point we're sure what the output image will be
648 	 * (static or dynamic).
649 	 */
650 	if (ld_ent_setup(ofl, ld_targ.t_m.m_segm_align) == S_ERROR)
651 		return (S_ERROR);
652 
653 	/*
654 	 * Does the host currently running the linker have the same
655 	 * byte order as the target for which the object is being produced?
656 	 * If not, set FLG_OF1_ENCDIFF so relocation code will know
657 	 * to check.
658 	 */
659 	if (_elf_sys_encoding() != ld_targ.t_m.m_data)
660 		ofl->ofl_flags1 |= FLG_OF1_ENCDIFF;
661 
662 	/*
663 	 * If the target has special executable section filling requirements,
664 	 * register the fill function with libelf
665 	 */
666 	if (ld_targ.t_ff.ff_execfill != NULL)
667 		_elf_execfill(ld_targ.t_ff.ff_execfill);
668 
669 	/*
670 	 * Initialize string tables.  Symbol definitions within mapfiles can
671 	 * result in the creation of input sections.
672 	 */
673 	if (ld_init_strings(ofl) == S_ERROR)
674 		return (S_ERROR);
675 
676 	/*
677 	 * Process mapfiles. Mapfile can redefine or add sections/segments,
678 	 * so this must come after the default entrance criteria are established
679 	 * (above).
680 	 */
681 	if (ofl->ofl_maps) {
682 		const char	*name;
683 		Aliste		idx;
684 
685 		for (APLIST_TRAVERSE(ofl->ofl_maps, idx, name))
686 			if (!ld_map_parse(name, ofl))
687 				return (S_ERROR);
688 
689 		if (!ld_map_post_process(ofl))
690 			return (S_ERROR);
691 	}
692 
693 	/*
694 	 * If a mapfile has been used to define a single symbolic scope of
695 	 * interfaces, -Bsymbolic is established.  This global setting goes
696 	 * beyond individual symbol protection, and ensures all relocations
697 	 * (even those that reference section symbols) are processed within
698 	 * the object being built.
699 	 */
700 	if (((ofl->ofl_flags &
701 	    (FLG_OF_MAPSYMB | FLG_OF_MAPGLOB)) == FLG_OF_MAPSYMB) &&
702 	    (ofl->ofl_flags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM))) {
703 		ofl->ofl_flags |= FLG_OF_SYMBOLIC;
704 		ofl->ofl_dtflags |= DF_SYMBOLIC;
705 	}
706 
707 	/*
708 	 * If -zloadfltr is set, verify that filtering is in effect.  Filters
709 	 * are either established from the command line, and affect the whole
710 	 * object, or are set on a per-symbol basis from a mapfile.
711 	 */
712 	if (zlflag) {
713 		if ((ofl->ofl_filtees == NULL) && (ofl->ofl_dtsfltrs == NULL))
714 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_NOFLTR),
715 			    MSG_ORIG(MSG_ARG_ZLOADFLTR));
716 		ofl->ofl_dtflags_1 |= DF_1_LOADFLTR;
717 	}
718 
719 	/*
720 	 * Check that we have something to work with. This check is carried out
721 	 * after mapfile processing as its possible a mapfile is being used to
722 	 * define symbols, in which case it would be sufficient to build the
723 	 * output file purely from the mapfile.
724 	 */
725 	if ((ofl->ofl_objscnt == 0) && (ofl->ofl_soscnt == 0)) {
726 		if ((Vflag ||
727 		    (Dflag && (dbg_desc->d_extra & DBG_E_HELP_EXIT))) &&
728 		    (argc == 2)) {
729 			ofl->ofl_flags1 |= FLG_OF1_DONE;
730 		} else {
731 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_NOFILES));
732 			return (S_ERROR);
733 		}
734 	}
735 	return (1);
736 }
737 
738 /*
739  * Decompose the string pointed by optarg into argv[][] so that argv[][] can be
740  * used as an argument to getopt().
741  *
742  * If the second argument 'usage' is not NULL, then this is called from the
743  * first pass. Else this is called from the second pass.
744  */
745 static uintptr_t
746 createargv(Ofl_desc *ofl, int *usage)
747 {
748 	int		argc = 0, idx = 0, ooptind;
749 	uintptr_t	ret;
750 	char		**argv, *p0;
751 
752 	/*
753 	 * The argument being examined is either:
754 	 *	ld32=	or
755 	 *	ld64=
756 	 */
757 #if	defined(_LP64)
758 	if (optarg[2] == '3')
759 		return (0);
760 #else
761 	if (optarg[2] == '6')
762 		return (0);
763 #endif
764 
765 	p0 = &optarg[5];
766 
767 	/*
768 	 * Count the number of arguments.
769 	 */
770 	while (*p0) {
771 		/*
772 		 * Pointing at non-separator character.
773 		 */
774 		if (*p0 != ',') {
775 			argc++;
776 			while (*p0 && (*p0 != ','))
777 				p0++;
778 			continue;
779 		}
780 
781 		/*
782 		 * Pointing at a separator character.
783 		 */
784 		if (*p0 == ',') {
785 			while (*p0 == ',')
786 				p0++;
787 			continue;
788 		}
789 	}
790 
791 	if (argc == 0)
792 		return (0);
793 
794 	/*
795 	 * Allocate argument vector.
796 	 */
797 	if ((p0 = (char *)strdup(&optarg[5])) == NULL)
798 		return (S_ERROR);
799 	if ((argv = libld_malloc((sizeof (char *)) * (argc + 1))) == NULL)
800 		return (S_ERROR);
801 
802 	while (*p0) {
803 		char *p;
804 
805 		/*
806 		 * Pointing at the beginning of non-separator character string.
807 		 */
808 		if (*p0 != ',') {
809 			p = p0;
810 			while (*p0 && (*p0 != ','))
811 				p0++;
812 			argv[idx++] = p;
813 			if (*p0) {
814 				*p0 = '\0';
815 				p0++;
816 			}
817 			continue;
818 		}
819 
820 		/*
821 		 * Pointing at the beginining of separator character string.
822 		 */
823 		if (*p0 == ',') {
824 			while (*p0 == ',')
825 				p0++;
826 			continue;
827 		}
828 	}
829 	argv[idx] = 0;
830 	ooptind = optind;
831 	optind = 0;
832 
833 	/*
834 	 * Dispatch to pass1 or pass2
835 	 */
836 	if (usage)
837 		ret = process_flags_com(ofl, argc, argv, usage);
838 	else
839 		ret = process_files_com(ofl, argc, argv);
840 
841 	optind = ooptind;
842 	return (ret);
843 }
844 
845 /*
846  * Parse the items in a '-z guidance' value, and set the ofl_guideflags.
847  * A guidance option looks like this:
848  *
849  *	-z guidance[=item1,item2,...]
850  *
851  * Where each item specifies categories of guidance messages to suppress,
852  * each starting with the prefix 'no'. We allow arbitrary whitespace between
853  * the items, allow multiple ',' delimiters without an intervening item, and
854  * quietly ignore any items we don't recognize.
855  *
856  * -	Such items are likely to be known to newer versions of the linker,
857  *	and we do not want an older version of the linker to
858  *	complain about them.
859  *
860  * -	Times and standards can change, and so we wish to reserve the
861  *	right to make an old item that no longer makes sense go away.
862  *	Quietly ignoring unrecognized items facilitates this.
863  *
864  * However, we always display unrecognized items in debug output.
865  *
866  * entry:
867  *	ofl - Output descriptor
868  *	optarg - option string to be processed. This will either be a NULL
869  *		terminated 'guidance', or it will be 'guidance=' followed
870  *		by the item tokens as described above.
871  *
872  * exit:
873  *	Returns TRUE (1) on success, FALSE (0) on failure.
874  *
875  */
876 static Boolean
877 guidance_parse(Ofl_desc *ofl, char *optarg)
878 {
879 	typedef struct {
880 		const char	*name;
881 		ofl_guideflag_t	flag;
882 	} item_desc;
883 
884 	static  item_desc items[] = {
885 		{ MSG_ORIG(MSG_ARG_GUIDE_NO_ALL),	FLG_OFG_NO_ALL },
886 
887 		{ MSG_ORIG(MSG_ARG_GUIDE_NO_DEFS),	FLG_OFG_NO_DEFS },
888 		{ MSG_ORIG(MSG_ARG_GUIDE_NO_DIRECT),	FLG_OFG_NO_DB },
889 		{ MSG_ORIG(MSG_ARG_GUIDE_NO_LAZYLOAD),	FLG_OFG_NO_LAZY },
890 		{ MSG_ORIG(MSG_ARG_GUIDE_NO_MAPFILE),	FLG_OFG_NO_MF },
891 		{ MSG_ORIG(MSG_ARG_GUIDE_NO_TEXT),	FLG_OFG_NO_TEXT },
892 		{ MSG_ORIG(MSG_ARG_GUIDE_NO_UNUSED),	FLG_OFG_NO_UNUSED },
893 		{ MSG_ORIG(MSG_ARG_GUIDE_NO_ASSERTS),	FLG_OFG_NO_ASSERTS },
894 		{ NULL,					0 }
895 	};
896 
897 	char		*lasts, *name;
898 	item_desc	*item;
899 	ofl_guideflag_t	ofl_guideflags = FLG_OFG_ENABLE;
900 
901 	/*
902 	 * Skip the 'guidance' prefix. If NULL terminated, there are no
903 	 * item values to parse. Otherwise, skip the '=' and parse the items.
904 	 */
905 	optarg += MSG_ARG_GUIDE_SIZE;
906 	if (*optarg == '=') {
907 		optarg++;
908 
909 		if ((name = libld_malloc(strlen(optarg) + 1)) == NULL)
910 			return (FALSE);
911 		(void) strcpy(name, optarg);
912 
913 		if ((name = strtok_r(name, MSG_ORIG(MSG_ARG_GUIDE_DELIM),
914 		    &lasts)) != NULL) {
915 			do {
916 				for (item = items; item->name != NULL; item++)
917 					if (strcasecmp(name, item->name) == 0)
918 						break;
919 				if (item->name == NULL) {
920 					ld_eprintf(ofl, ERR_GUIDANCE,
921 					    MSG_INTL(MSG_GUIDE_UNKNOWN), name);
922 					continue;
923 				}
924 				ofl_guideflags |= item->flag;
925 			} while ((name = strtok_r(NULL,
926 			    MSG_ORIG(MSG_ARG_GUIDE_DELIM), &lasts)) != NULL);
927 		}
928 	}
929 
930 	/*
931 	 * If -zguidance is used more than once, we take the first one. We
932 	 * do this quietly if they have identical options, and with a warning
933 	 * otherwise.
934 	 */
935 	if ((initial_guidance_flags & FLG_OFG_ENABLE) &&
936 	    (ofl_guideflags != initial_guidance_flags)) {
937 		ld_eprintf(ofl, ERR_WARNING_NF, MSG_INTL(MSG_ARG_MTONCE),
938 		    MSG_ORIG(MSG_ARG_ZGUIDE));
939 		return (TRUE);
940 	}
941 
942 	/*
943 	 * First time: Save the flags for comparison to any subsequent
944 	 * -z guidance that comes along, and OR the resulting flags into
945 	 * the flags kept in the output descriptor.
946 	 */
947 	initial_guidance_flags = ofl_guideflags;
948 	ofl->ofl_guideflags |= ofl_guideflags;
949 	return (TRUE);
950 }
951 
952 /*
953  * Parse the -z assert-deflib option. This option can appear in two different
954  * forms:
955  *	-z assert-deflib
956  *	-z assert-deflib=libfred.so
957  *
958  * Either form enables this option, the latter form marks libfred.so as an
959  * exempt library from the check. It is valid to have multiple invocations of
960  * the second form. We silently ignore mulitple occurrences of the first form
961  * and multiple invocations of the first form when the second form also occurs.
962  *
963  * We only return false when we have an internal error, such as the failure of
964  * aplist_append. Every other time we return true, but we have the appropriate
965  * fatal flags set beacuse of the ld_eprintf.
966  */
967 static int
968 assdeflib_parse(Ofl_desc *ofl, char *optarg)
969 {
970 	size_t olen, mlen;
971 	ofl->ofl_flags |= FLG_OF_ADEFLIB;
972 
973 	olen = strlen(optarg);
974 	/* Minimum size of assert-deflib=lib%s.so */
975 	mlen = MSG_ARG_ASSDEFLIB_SIZE + 1 + MSG_STR_LIB_SIZE +
976 	    MSG_STR_SOEXT_SIZE;
977 	if (olen > MSG_ARG_ASSDEFLIB_SIZE) {
978 		if (optarg[MSG_ARG_ASSDEFLIB_SIZE] != '=') {
979 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_ILLEGAL),
980 			    MSG_ORIG(MSG_ARG_ASSDEFLIB), optarg);
981 			return (TRUE);
982 		}
983 
984 		if (strncmp(optarg + MSG_ARG_ASSDEFLIB_SIZE + 1,
985 		    MSG_ORIG(MSG_STR_LIB), MSG_STR_LIB_SIZE) != 0 ||
986 		    strcmp(optarg + olen - MSG_STR_SOEXT_SIZE,
987 		    MSG_ORIG(MSG_STR_SOEXT)) != 0 || olen <= mlen) {
988 			ld_eprintf(ofl, ERR_FATAL,
989 			    MSG_INTL(MSG_ARG_ASSDEFLIB_MALFORMED), optarg);
990 			return (TRUE);
991 		}
992 
993 		if (aplist_append(&ofl->ofl_assdeflib, optarg +
994 		    MSG_ARG_ASSDEFLIB_SIZE + 1, AL_CNT_ASSDEFLIB) == NULL)
995 			return (FALSE);
996 	}
997 
998 	return (TRUE);
999 }
1000 
1001 static int	optitle = 0;
1002 /*
1003  * Parsing options pass1 for process_flags().
1004  */
1005 static uintptr_t
1006 parseopt_pass1(Ofl_desc *ofl, int argc, char **argv, int *usage)
1007 {
1008 	int	c, ndx = optind;
1009 
1010 	/*
1011 	 * The -32, -64 and -ztarget options are special, in that we validate
1012 	 * them, but otherwise ignore them. libld.so (this code) is called
1013 	 * from the ld front end program. ld has already examined the
1014 	 * arguments to determine the output class and machine type of the
1015 	 * output object, as reflected in the version (32/64) of ld_main()
1016 	 * that was called and the value of the 'mach' argument passed.
1017 	 * By time execution reaches this point, these options have already
1018 	 * been seen and acted on.
1019 	 */
1020 	while ((c = ld_getopt(ofl->ofl_lml, ndx, argc, argv)) != -1) {
1021 
1022 		switch (c) {
1023 		case '3':
1024 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1025 
1026 			/*
1027 			 * -32 is processed by ld to determine the output class.
1028 			 * Here we sanity check the option incase some other
1029 			 * -3* option is mistakenly passed to us.
1030 			 */
1031 			if (optarg[0] != '2')
1032 				ld_eprintf(ofl, ERR_FATAL,
1033 				    MSG_INTL(MSG_ARG_ILLEGAL),
1034 				    MSG_ORIG(MSG_ARG_3), optarg);
1035 			continue;
1036 
1037 		case '6':
1038 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1039 
1040 			/*
1041 			 * -64 is processed by ld to determine the output class.
1042 			 * Here we sanity check the option incase some other
1043 			 * -6* option is mistakenly passed to us.
1044 			 */
1045 			if (optarg[0] != '4')
1046 				ld_eprintf(ofl, ERR_FATAL,
1047 				    MSG_INTL(MSG_ARG_ILLEGAL),
1048 				    MSG_ORIG(MSG_ARG_6), optarg);
1049 			continue;
1050 
1051 		case 'a':
1052 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1053 			aflag = TRUE;
1054 			break;
1055 
1056 		case 'b':
1057 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1058 			bflag = TRUE;
1059 
1060 			/*
1061 			 * This is a hack, and may be undone later.
1062 			 * The -b option is only used to build the Unix
1063 			 * kernel and its related kernel-mode modules.
1064 			 * We do not want those files to get a .SUNW_ldynsym
1065 			 * section. At least for now, the kernel makes no
1066 			 * use of .SUNW_ldynsym, and we do not want to use
1067 			 * the space to hold it. Therefore, we overload
1068 			 * the use of -b to also imply -znoldynsym.
1069 			 */
1070 			ofl->ofl_flags |= FLG_OF_NOLDYNSYM;
1071 			break;
1072 
1073 		case 'c':
1074 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1075 			if (ofl->ofl_config)
1076 				ld_eprintf(ofl, ERR_WARNING_NF,
1077 				    MSG_INTL(MSG_ARG_MTONCE),
1078 				    MSG_ORIG(MSG_ARG_C));
1079 			else
1080 				ofl->ofl_config = optarg;
1081 			break;
1082 
1083 		case 'C':
1084 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1085 			demangle_flag = 1;
1086 			break;
1087 
1088 		case 'd':
1089 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1090 			if ((optarg[0] == 'n') && (optarg[1] == '\0')) {
1091 				if (dflag != SET_UNKNOWN)
1092 					ld_eprintf(ofl, ERR_WARNING_NF,
1093 					    MSG_INTL(MSG_ARG_MTONCE),
1094 					    MSG_ORIG(MSG_ARG_D));
1095 				else
1096 					dflag = SET_FALSE;
1097 			} else if ((optarg[0] == 'y') && (optarg[1] == '\0')) {
1098 				if (dflag != SET_UNKNOWN)
1099 					ld_eprintf(ofl, ERR_WARNING_NF,
1100 					    MSG_INTL(MSG_ARG_MTONCE),
1101 					    MSG_ORIG(MSG_ARG_D));
1102 				else
1103 					dflag = SET_TRUE;
1104 			} else {
1105 				ld_eprintf(ofl, ERR_FATAL,
1106 				    MSG_INTL(MSG_ARG_ILLEGAL),
1107 				    MSG_ORIG(MSG_ARG_D), optarg);
1108 			}
1109 			break;
1110 
1111 		case 'e':
1112 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1113 			if (ofl->ofl_entry)
1114 				ld_eprintf(ofl, ERR_WARNING_NF,
1115 				    MSG_INTL(MSG_MARG_MTONCE),
1116 				    MSG_INTL(MSG_MARG_ENTRY));
1117 			else
1118 				ofl->ofl_entry = (void *)optarg;
1119 			break;
1120 
1121 		case 'f':
1122 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1123 			if (ofl->ofl_filtees &&
1124 			    (!(ofl->ofl_flags & FLG_OF_AUX))) {
1125 				ld_eprintf(ofl, ERR_FATAL,
1126 				    MSG_INTL(MSG_MARG_INCOMP),
1127 				    MSG_INTL(MSG_MARG_FILTER_AUX),
1128 				    MSG_INTL(MSG_MARG_FILTER));
1129 			} else {
1130 				if ((ofl->ofl_filtees =
1131 				    add_string(ofl->ofl_filtees, optarg)) ==
1132 				    (const char *)S_ERROR)
1133 					return (S_ERROR);
1134 				ofl->ofl_flags |= FLG_OF_AUX;
1135 			}
1136 			break;
1137 
1138 		case 'F':
1139 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1140 			if (ofl->ofl_filtees &&
1141 			    (ofl->ofl_flags & FLG_OF_AUX)) {
1142 				ld_eprintf(ofl, ERR_FATAL,
1143 				    MSG_INTL(MSG_MARG_INCOMP),
1144 				    MSG_INTL(MSG_MARG_FILTER),
1145 				    MSG_INTL(MSG_MARG_FILTER_AUX));
1146 			} else {
1147 				if ((ofl->ofl_filtees =
1148 				    add_string(ofl->ofl_filtees, optarg)) ==
1149 				    (const char *)S_ERROR)
1150 					return (S_ERROR);
1151 			}
1152 			break;
1153 
1154 		case 'h':
1155 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1156 			if (ofl->ofl_soname)
1157 				ld_eprintf(ofl, ERR_WARNING_NF,
1158 				    MSG_INTL(MSG_MARG_MTONCE),
1159 				    MSG_INTL(MSG_MARG_SONAME));
1160 			else
1161 				ofl->ofl_soname = (const char *)optarg;
1162 			break;
1163 
1164 		case 'i':
1165 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1166 			ofl->ofl_flags |= FLG_OF_IGNENV;
1167 			break;
1168 
1169 		case 'I':
1170 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1171 			if (ofl->ofl_interp)
1172 				ld_eprintf(ofl, ERR_WARNING_NF,
1173 				    MSG_INTL(MSG_ARG_MTONCE),
1174 				    MSG_ORIG(MSG_ARG_CI));
1175 			else
1176 				ofl->ofl_interp = (const char *)optarg;
1177 			break;
1178 
1179 		case 'l':
1180 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1181 			/*
1182 			 * For now, count any library as a shared object.  This
1183 			 * is used to size the internal symbol cache.  This
1184 			 * value is recalculated later on actual file processing
1185 			 * to get an accurate shared object count.
1186 			 */
1187 			ofl->ofl_soscnt++;
1188 			break;
1189 
1190 		case 'm':
1191 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1192 			ofl->ofl_flags |= FLG_OF_GENMAP;
1193 			break;
1194 
1195 		case 'o':
1196 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1197 			if (ofl->ofl_name)
1198 				ld_eprintf(ofl, ERR_WARNING_NF,
1199 				    MSG_INTL(MSG_MARG_MTONCE),
1200 				    MSG_INTL(MSG_MARG_OUTFILE));
1201 			else
1202 				ofl->ofl_name = (const char *)optarg;
1203 			break;
1204 
1205 		case 'p':
1206 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1207 
1208 			/*
1209 			 * Multiple instances of this option may occur.  Each
1210 			 * additional instance is effectively concatenated to
1211 			 * the previous separated by a colon.
1212 			 */
1213 			if (*optarg != '\0') {
1214 				if ((ofl->ofl_audit =
1215 				    add_string(ofl->ofl_audit,
1216 				    optarg)) == (const char *)S_ERROR)
1217 					return (S_ERROR);
1218 			}
1219 			break;
1220 
1221 		case 'P':
1222 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1223 
1224 			/*
1225 			 * Multiple instances of this option may occur.  Each
1226 			 * additional instance is effectively concatenated to
1227 			 * the previous separated by a colon.
1228 			 */
1229 			if (*optarg != '\0') {
1230 				if ((ofl->ofl_depaudit =
1231 				    add_string(ofl->ofl_depaudit,
1232 				    optarg)) == (const char *)S_ERROR)
1233 					return (S_ERROR);
1234 			}
1235 			break;
1236 
1237 		case 'r':
1238 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1239 			otype = OT_RELOC;
1240 			break;
1241 
1242 		case 'R':
1243 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1244 
1245 			/*
1246 			 * Multiple instances of this option may occur.  Each
1247 			 * additional instance is effectively concatenated to
1248 			 * the previous separated by a colon.
1249 			 */
1250 			if (*optarg != '\0') {
1251 				if ((ofl->ofl_rpath =
1252 				    add_string(ofl->ofl_rpath,
1253 				    optarg)) == (const char *)S_ERROR)
1254 					return (S_ERROR);
1255 			}
1256 			break;
1257 
1258 		case 's':
1259 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1260 			sflag = TRUE;
1261 			break;
1262 
1263 		case 't':
1264 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1265 			ofl->ofl_flags |= FLG_OF_NOWARN;
1266 			break;
1267 
1268 		case 'u':
1269 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1270 			break;
1271 
1272 		case 'z':
1273 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1274 
1275 			/*
1276 			 * Skip comma that might be present between -z and its
1277 			 * argument (e.g. if -Wl,-z,assert-deflib was passed).
1278 			 */
1279 			if (strncmp(optarg, MSG_ORIG(MSG_STR_COMMA),
1280 			    MSG_STR_COMMA_SIZE) == 0)
1281 				optarg++;
1282 
1283 			/*
1284 			 * For specific help, print our usage message and exit
1285 			 * immediately to ensure a 0 return code.
1286 			 */
1287 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_HELP),
1288 			    MSG_ARG_HELP_SIZE) == 0) {
1289 				usage_mesg(TRUE);
1290 				exit(0);
1291 			}
1292 
1293 			/*
1294 			 * For some options set a flag - further consistancy
1295 			 * checks will be carried out in check_flags().
1296 			 */
1297 			if ((strncmp(optarg, MSG_ORIG(MSG_ARG_LD32),
1298 			    MSG_ARG_LD32_SIZE) == 0) ||
1299 			    (strncmp(optarg, MSG_ORIG(MSG_ARG_LD64),
1300 			    MSG_ARG_LD64_SIZE) == 0)) {
1301 				if (createargv(ofl, usage) == S_ERROR)
1302 					return (S_ERROR);
1303 
1304 			} else if (
1305 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DEFS)) == 0) {
1306 				if (zdflag != SET_UNKNOWN)
1307 					ld_eprintf(ofl, ERR_WARNING_NF,
1308 					    MSG_INTL(MSG_ARG_MTONCE),
1309 					    MSG_ORIG(MSG_ARG_ZDEFNODEF));
1310 				else
1311 					zdflag = SET_TRUE;
1312 				ofl->ofl_guideflags |= FLG_OFG_NO_DEFS;
1313 			} else if (strcmp(optarg,
1314 			    MSG_ORIG(MSG_ARG_NODEFS)) == 0) {
1315 				if (zdflag != SET_UNKNOWN)
1316 					ld_eprintf(ofl, ERR_WARNING_NF,
1317 					    MSG_INTL(MSG_ARG_MTONCE),
1318 					    MSG_ORIG(MSG_ARG_ZDEFNODEF));
1319 				else
1320 					zdflag = SET_FALSE;
1321 				ofl->ofl_guideflags |= FLG_OFG_NO_DEFS;
1322 			} else if (strcmp(optarg,
1323 			    MSG_ORIG(MSG_ARG_TEXT)) == 0) {
1324 				if (ztflag &&
1325 				    (ztflag != MSG_ORIG(MSG_ARG_ZTEXT)))
1326 					ld_eprintf(ofl, ERR_FATAL,
1327 					    MSG_INTL(MSG_ARG_INCOMP),
1328 					    MSG_ORIG(MSG_ARG_ZTEXT),
1329 					    ztflag);
1330 				ztflag = MSG_ORIG(MSG_ARG_ZTEXT);
1331 			} else if (strcmp(optarg,
1332 			    MSG_ORIG(MSG_ARG_TEXTOFF)) == 0) {
1333 				if (ztflag &&
1334 				    (ztflag != MSG_ORIG(MSG_ARG_ZTEXTOFF)))
1335 					ld_eprintf(ofl, ERR_FATAL,
1336 					    MSG_INTL(MSG_ARG_INCOMP),
1337 					    MSG_ORIG(MSG_ARG_ZTEXTOFF),
1338 					    ztflag);
1339 				ztflag = MSG_ORIG(MSG_ARG_ZTEXTOFF);
1340 			} else if (strcmp(optarg,
1341 			    MSG_ORIG(MSG_ARG_TEXTWARN)) == 0) {
1342 				if (ztflag &&
1343 				    (ztflag != MSG_ORIG(MSG_ARG_ZTEXTWARN)))
1344 					ld_eprintf(ofl, ERR_FATAL,
1345 					    MSG_INTL(MSG_ARG_INCOMP),
1346 					    MSG_ORIG(MSG_ARG_ZTEXTWARN),
1347 					    ztflag);
1348 				ztflag = MSG_ORIG(MSG_ARG_ZTEXTWARN);
1349 
1350 			/*
1351 			 * For other options simply set the ofl flags directly.
1352 			 */
1353 			} else if (strcmp(optarg,
1354 			    MSG_ORIG(MSG_ARG_RESCAN)) == 0) {
1355 				ofl->ofl_flags1 |= FLG_OF1_RESCAN;
1356 			} else if (strcmp(optarg,
1357 			    MSG_ORIG(MSG_ARG_ABSEXEC)) == 0) {
1358 				ofl->ofl_flags1 |= FLG_OF1_ABSEXEC;
1359 			} else if (strcmp(optarg,
1360 			    MSG_ORIG(MSG_ARG_LOADFLTR)) == 0) {
1361 				zlflag = TRUE;
1362 			} else if (strcmp(optarg,
1363 			    MSG_ORIG(MSG_ARG_NORELOC)) == 0) {
1364 				ofl->ofl_dtflags_1 |= DF_1_NORELOC;
1365 			} else if (strcmp(optarg,
1366 			    MSG_ORIG(MSG_ARG_NOVERSION)) == 0) {
1367 				ofl->ofl_flags |= FLG_OF_NOVERSEC;
1368 			} else if (strcmp(optarg,
1369 			    MSG_ORIG(MSG_ARG_MULDEFS)) == 0) {
1370 				ofl->ofl_flags |= FLG_OF_MULDEFS;
1371 			} else if (strcmp(optarg,
1372 			    MSG_ORIG(MSG_ARG_REDLOCSYM)) == 0) {
1373 				ofl->ofl_flags |= FLG_OF_REDLSYM;
1374 			} else if (strcmp(optarg,
1375 			    MSG_ORIG(MSG_ARG_INITFIRST)) == 0) {
1376 				ofl->ofl_dtflags_1 |= DF_1_INITFIRST;
1377 			} else if (strcmp(optarg,
1378 			    MSG_ORIG(MSG_ARG_NODELETE)) == 0) {
1379 				ofl->ofl_dtflags_1 |= DF_1_NODELETE;
1380 			} else if (strcmp(optarg,
1381 			    MSG_ORIG(MSG_ARG_NOPARTIAL)) == 0) {
1382 				ofl->ofl_flags1 |= FLG_OF1_NOPARTI;
1383 			} else if (strcmp(optarg,
1384 			    MSG_ORIG(MSG_ARG_NOOPEN)) == 0) {
1385 				ofl->ofl_dtflags_1 |= DF_1_NOOPEN;
1386 			} else if (strcmp(optarg,
1387 			    MSG_ORIG(MSG_ARG_NOW)) == 0) {
1388 				ofl->ofl_dtflags_1 |= DF_1_NOW;
1389 				ofl->ofl_dtflags |= DF_BIND_NOW;
1390 			} else if (strcmp(optarg,
1391 			    MSG_ORIG(MSG_ARG_ORIGIN)) == 0) {
1392 				ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1393 				ofl->ofl_dtflags |= DF_ORIGIN;
1394 			} else if (strcmp(optarg,
1395 			    MSG_ORIG(MSG_ARG_NODEFAULTLIB)) == 0) {
1396 				ofl->ofl_dtflags_1 |= DF_1_NODEFLIB;
1397 			} else if (strcmp(optarg,
1398 			    MSG_ORIG(MSG_ARG_NODUMP)) == 0) {
1399 				ofl->ofl_dtflags_1 |= DF_1_NODUMP;
1400 			} else if (strcmp(optarg,
1401 			    MSG_ORIG(MSG_ARG_ENDFILTEE)) == 0) {
1402 				ofl->ofl_dtflags_1 |= DF_1_ENDFILTEE;
1403 			} else if (strcmp(optarg,
1404 			    MSG_ORIG(MSG_ARG_VERBOSE)) == 0) {
1405 				ofl->ofl_flags |= FLG_OF_VERBOSE;
1406 			} else if (strcmp(optarg,
1407 			    MSG_ORIG(MSG_ARG_COMBRELOC)) == 0) {
1408 				ofl->ofl_flags |= FLG_OF_COMREL;
1409 			} else if (strcmp(optarg,
1410 			    MSG_ORIG(MSG_ARG_NOCOMBRELOC)) == 0) {
1411 				ofl->ofl_flags |= FLG_OF_NOCOMREL;
1412 			} else if (strcmp(optarg,
1413 			    MSG_ORIG(MSG_ARG_NOCOMPSTRTAB)) == 0) {
1414 				ofl->ofl_flags1 |= FLG_OF1_NCSTTAB;
1415 			} else if (strcmp(optarg,
1416 			    MSG_ORIG(MSG_ARG_NOINTERP)) == 0) {
1417 				ofl->ofl_flags1 |= FLG_OF1_NOINTRP;
1418 			} else if (strcmp(optarg,
1419 			    MSG_ORIG(MSG_ARG_INTERPOSE)) == 0) {
1420 				zinflag = TRUE;
1421 			} else if (strcmp(optarg,
1422 			    MSG_ORIG(MSG_ARG_IGNORE)) == 0) {
1423 				ofl->ofl_flags1 |= FLG_OF1_IGNPRC;
1424 			} else if (strcmp(optarg,
1425 			    MSG_ORIG(MSG_ARG_RELAXRELOC)) == 0) {
1426 				ofl->ofl_flags1 |= FLG_OF1_RLXREL;
1427 			} else if (strcmp(optarg,
1428 			    MSG_ORIG(MSG_ARG_NORELAXRELOC)) == 0) {
1429 				ofl->ofl_flags1 |= FLG_OF1_NRLXREL;
1430 			} else if (strcmp(optarg,
1431 			    MSG_ORIG(MSG_ARG_NOLDYNSYM)) == 0) {
1432 				ofl->ofl_flags |= FLG_OF_NOLDYNSYM;
1433 			} else if (strcmp(optarg,
1434 			    MSG_ORIG(MSG_ARG_GLOBAUDIT)) == 0) {
1435 				ofl->ofl_dtflags_1 |= DF_1_GLOBAUDIT;
1436 			} else if (strcmp(optarg,
1437 			    MSG_ORIG(MSG_ARG_NOSIGHANDLER)) == 0) {
1438 				ofl->ofl_flags1 |= FLG_OF1_NOSGHND;
1439 			} else if (strcmp(optarg,
1440 			    MSG_ORIG(MSG_ARG_SYMBOLCAP)) == 0) {
1441 				ofl->ofl_flags |= FLG_OF_OTOSCAP;
1442 
1443 			/*
1444 			 * Check archive group usage
1445 			 *	-z rescan-start ... -z rescan-end
1446 			 * to ensure they don't overlap and are well formed.
1447 			 */
1448 			} else if (strcmp(optarg,
1449 			    MSG_ORIG(MSG_ARG_RESCAN_START)) == 0) {
1450 				if (ofl->ofl_ars_gsandx == 0) {
1451 					ofl->ofl_ars_gsandx = ndx;
1452 				} else if (ofl->ofl_ars_gsandx > 0) {
1453 					/* Another group is still open */
1454 					ld_eprintf(ofl, ERR_FATAL,
1455 					    MSG_INTL(MSG_ARG_AR_GRP_OLAP),
1456 					    MSG_INTL(MSG_MARG_AR_GRPS));
1457 					/* Don't report cascading errors */
1458 					ofl->ofl_ars_gsandx = -1;
1459 				}
1460 			} else if (strcmp(optarg,
1461 			    MSG_ORIG(MSG_ARG_RESCAN_END)) == 0) {
1462 				if (ofl->ofl_ars_gsandx > 0) {
1463 					ofl->ofl_ars_gsandx = 0;
1464 				} else if (ofl->ofl_ars_gsandx == 0) {
1465 					/* There was no matching begin */
1466 					ld_eprintf(ofl, ERR_FATAL,
1467 					    MSG_INTL(MSG_ARG_AR_GRP_BAD),
1468 					    MSG_INTL(MSG_MARG_AR_GRP_END),
1469 					    MSG_INTL(MSG_MARG_AR_GRP_START));
1470 					/* Don't report cascading errors */
1471 					ofl->ofl_ars_gsandx = -1;
1472 				}
1473 
1474 			/*
1475 			 * If -z wrap is seen, enter the symbol to be wrapped
1476 			 * into the wrap AVL tree.
1477 			 */
1478 			} else if (strncmp(optarg, MSG_ORIG(MSG_ARG_WRAP),
1479 			    MSG_ARG_WRAP_SIZE) == 0) {
1480 				if (ld_wrap_enter(ofl,
1481 				    optarg + MSG_ARG_WRAP_SIZE) == NULL)
1482 					return (S_ERROR);
1483 			} else if (strncmp(optarg, MSG_ORIG(MSG_ARG_ASLR),
1484 			    MSG_ARG_ASLR_SIZE) == 0) {
1485 				char *p = optarg + MSG_ARG_ASLR_SIZE;
1486 				if (*p == '\0') {
1487 					ofl->ofl_aslr = 1;
1488 				} else if (*p == '=') {
1489 					p++;
1490 
1491 					if ((strcmp(p,
1492 					    MSG_ORIG(MSG_ARG_ENABLED)) == 0) ||
1493 					    (strcmp(p,
1494 					    MSG_ORIG(MSG_ARG_ENABLE)) == 0)) {
1495 						ofl->ofl_aslr = 1;
1496 					} else if ((strcmp(p,
1497 					    MSG_ORIG(MSG_ARG_DISABLED)) == 0) ||
1498 					    (strcmp(p,
1499 					    MSG_ORIG(MSG_ARG_DISABLE)) == 0)) {
1500 						ofl->ofl_aslr = -1;
1501 					} else {
1502 						ld_eprintf(ofl, ERR_FATAL,
1503 						    MSG_INTL(MSG_ARG_ILLEGAL),
1504 						    MSG_ORIG(MSG_ARG_ZASLR), p);
1505 						return (S_ERROR);
1506 					}
1507 				} else {
1508 					ld_eprintf(ofl, ERR_FATAL,
1509 					    MSG_INTL(MSG_ARG_ILLEGAL),
1510 					    MSG_ORIG(MSG_ARG_Z), optarg);
1511 					return (S_ERROR);
1512 				}
1513 			} else if ((strncmp(optarg, MSG_ORIG(MSG_ARG_GUIDE),
1514 			    MSG_ARG_GUIDE_SIZE) == 0) &&
1515 			    ((optarg[MSG_ARG_GUIDE_SIZE] == '=') ||
1516 			    (optarg[MSG_ARG_GUIDE_SIZE] == '\0'))) {
1517 				if (!guidance_parse(ofl, optarg))
1518 					return (S_ERROR);
1519 			} else if (strcmp(optarg,
1520 			    MSG_ORIG(MSG_ARG_FATWARN)) == 0) {
1521 				if (zfwflag  == SET_FALSE) {
1522 					ld_eprintf(ofl, ERR_WARNING_NF,
1523 					    MSG_INTL(MSG_ARG_MTONCE),
1524 					    MSG_ORIG(MSG_ARG_ZFATWNOFATW));
1525 				} else {
1526 					zfwflag = SET_TRUE;
1527 					ofl->ofl_flags |= FLG_OF_FATWARN;
1528 				}
1529 			} else if (strcmp(optarg,
1530 			    MSG_ORIG(MSG_ARG_NOFATWARN)) == 0) {
1531 				if (zfwflag  == SET_TRUE)
1532 					ld_eprintf(ofl, ERR_WARNING_NF,
1533 					    MSG_INTL(MSG_ARG_MTONCE),
1534 					    MSG_ORIG(MSG_ARG_ZFATWNOFATW));
1535 				else
1536 					zfwflag = SET_FALSE;
1537 
1538 			/*
1539 			 * Process everything related to -z assert-deflib. This
1540 			 * must be done in pass 1 because it gets used in pass
1541 			 * 2.
1542 			 */
1543 			} else if (strncmp(optarg, MSG_ORIG(MSG_ARG_ASSDEFLIB),
1544 			    MSG_ARG_ASSDEFLIB_SIZE) == 0) {
1545 				if (assdeflib_parse(ofl, optarg) != TRUE)
1546 					return (S_ERROR);
1547 
1548 			/*
1549 			 * Process new-style output type specification, which
1550 			 * we'll use in pass 2 and throughout.
1551 			 */
1552 			} else if (strncmp(optarg, MSG_ORIG(MSG_ARG_TYPE),
1553 			    MSG_ARG_TYPE_SIZE) == 0) {
1554 				char *p = optarg + MSG_ARG_TYPE_SIZE;
1555 				if (*p != '=') {
1556 					ld_eprintf(ofl, ERR_FATAL,
1557 					    MSG_INTL(MSG_ARG_ILLEGAL),
1558 					    MSG_ORIG(MSG_ARG_Z), optarg);
1559 					return (S_ERROR);
1560 				}
1561 
1562 				p++;
1563 				if (strcmp(p,
1564 				    MSG_ORIG(MSG_ARG_TYPE_RELOC)) == 0) {
1565 					otype = OT_RELOC;
1566 				} else if (strcmp(p,
1567 				    MSG_ORIG(MSG_ARG_TYPE_EXEC)) == 0) {
1568 					otype = OT_EXEC;
1569 				} else if (strcmp(p,
1570 				    MSG_ORIG(MSG_ARG_TYPE_SHARED)) == 0) {
1571 					otype = OT_SHARED;
1572 				} else if (strcmp(p,
1573 				    MSG_ORIG(MSG_ARG_TYPE_KMOD)) == 0) {
1574 					otype = OT_KMOD;
1575 				} else {
1576 					ld_eprintf(ofl, ERR_FATAL,
1577 					    MSG_INTL(MSG_ARG_ILLEGAL),
1578 					    MSG_ORIG(MSG_ARG_Z), optarg);
1579 					return (S_ERROR);
1580 				}
1581 			/*
1582 			 * The following options just need validation as they
1583 			 * are interpreted on the second pass through the
1584 			 * command line arguments.
1585 			 */
1586 			} else if (
1587 			    strncmp(optarg, MSG_ORIG(MSG_ARG_INITARRAY),
1588 			    MSG_ARG_INITARRAY_SIZE) &&
1589 			    strncmp(optarg, MSG_ORIG(MSG_ARG_FINIARRAY),
1590 			    MSG_ARG_FINIARRAY_SIZE) &&
1591 			    strncmp(optarg, MSG_ORIG(MSG_ARG_PREINITARRAY),
1592 			    MSG_ARG_PREINITARRAY_SIZE) &&
1593 			    strncmp(optarg, MSG_ORIG(MSG_ARG_RTLDINFO),
1594 			    MSG_ARG_RTLDINFO_SIZE) &&
1595 			    strncmp(optarg, MSG_ORIG(MSG_ARG_DTRACE),
1596 			    MSG_ARG_DTRACE_SIZE) &&
1597 			    strcmp(optarg, MSG_ORIG(MSG_ARG_ALLEXTRT)) &&
1598 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DFLEXTRT)) &&
1599 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DIRECT)) &&
1600 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NODIRECT)) &&
1601 			    strcmp(optarg, MSG_ORIG(MSG_ARG_GROUPPERM)) &&
1602 			    strcmp(optarg, MSG_ORIG(MSG_ARG_LAZYLOAD)) &&
1603 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NOGROUPPERM)) &&
1604 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NOLAZYLOAD)) &&
1605 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NODEFERRED)) &&
1606 			    strcmp(optarg, MSG_ORIG(MSG_ARG_RECORD)) &&
1607 			    strcmp(optarg, MSG_ORIG(MSG_ARG_ALTEXEC64)) &&
1608 			    strcmp(optarg, MSG_ORIG(MSG_ARG_WEAKEXT)) &&
1609 			    strncmp(optarg, MSG_ORIG(MSG_ARG_TARGET),
1610 			    MSG_ARG_TARGET_SIZE) &&
1611 			    strcmp(optarg, MSG_ORIG(MSG_ARG_RESCAN_NOW)) &&
1612 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DEFERRED))) {
1613 				ld_eprintf(ofl, ERR_FATAL,
1614 				    MSG_INTL(MSG_ARG_ILLEGAL),
1615 				    MSG_ORIG(MSG_ARG_Z), optarg);
1616 			}
1617 
1618 			break;
1619 
1620 		case 'D':
1621 			/*
1622 			 * If we have not yet read any input files go ahead
1623 			 * and process any debugging options (this allows any
1624 			 * argument processing, entrance criteria and library
1625 			 * initialization to be displayed).  Otherwise, if an
1626 			 * input file has been seen, skip interpretation until
1627 			 * process_files (this allows debugging to be turned
1628 			 * on and off around individual groups of files).
1629 			 */
1630 			Dflag = 1;
1631 			if (ofl->ofl_objscnt == 0) {
1632 				if (dbg_setup(ofl, optarg, 2) == 0)
1633 					return (S_ERROR);
1634 			}
1635 
1636 			/*
1637 			 * A diagnostic can only be provided after dbg_setup().
1638 			 * As this is the first diagnostic that can be produced
1639 			 * by ld(1), issue a title for timing and basic output.
1640 			 */
1641 			if ((optitle == 0) && DBG_ENABLED) {
1642 				optitle++;
1643 				DBG_CALL(Dbg_basic_options(ofl->ofl_lml));
1644 			}
1645 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1646 			break;
1647 
1648 		case 'B':
1649 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1650 			if (strcmp(optarg, MSG_ORIG(MSG_ARG_DIRECT)) == 0) {
1651 				if (Bdflag == SET_FALSE) {
1652 					ld_eprintf(ofl, ERR_FATAL,
1653 					    MSG_INTL(MSG_ARG_INCOMP),
1654 					    MSG_ORIG(MSG_ARG_BNODIRECT),
1655 					    MSG_ORIG(MSG_ARG_BDIRECT));
1656 				} else {
1657 					Bdflag = SET_TRUE;
1658 					ofl->ofl_guideflags |= FLG_OFG_NO_DB;
1659 				}
1660 			} else if (strcmp(optarg,
1661 			    MSG_ORIG(MSG_ARG_NODIRECT)) == 0) {
1662 				if (Bdflag == SET_TRUE) {
1663 					ld_eprintf(ofl, ERR_FATAL,
1664 					    MSG_INTL(MSG_ARG_INCOMP),
1665 					    MSG_ORIG(MSG_ARG_BDIRECT),
1666 					    MSG_ORIG(MSG_ARG_BNODIRECT));
1667 				} else {
1668 					Bdflag = SET_FALSE;
1669 					ofl->ofl_guideflags |= FLG_OFG_NO_DB;
1670 				}
1671 			} else if (strcmp(optarg,
1672 			    MSG_ORIG(MSG_STR_SYMBOLIC)) == 0)
1673 				Bsflag = TRUE;
1674 			else if (strcmp(optarg, MSG_ORIG(MSG_ARG_REDUCE)) == 0)
1675 				ofl->ofl_flags |= FLG_OF_PROCRED;
1676 			else if (strcmp(optarg, MSG_ORIG(MSG_STR_LOCAL)) == 0)
1677 				Blflag = TRUE;
1678 			else if (strcmp(optarg, MSG_ORIG(MSG_ARG_GROUP)) == 0)
1679 				Bgflag = TRUE;
1680 			else if (strcmp(optarg,
1681 			    MSG_ORIG(MSG_STR_ELIMINATE)) == 0)
1682 				Beflag = TRUE;
1683 			else if (strcmp(optarg,
1684 			    MSG_ORIG(MSG_ARG_TRANSLATOR)) == 0) {
1685 				ld_eprintf(ofl, ERR_WARNING,
1686 				    MSG_INTL(MSG_ARG_UNSUPPORTED),
1687 				    MSG_ORIG(MSG_ARG_BTRANSLATOR));
1688 			} else if (strcmp(optarg,
1689 			    MSG_ORIG(MSG_STR_LD_DYNAMIC)) &&
1690 			    strcmp(optarg, MSG_ORIG(MSG_ARG_STATIC))) {
1691 				ld_eprintf(ofl, ERR_FATAL,
1692 				    MSG_INTL(MSG_ARG_ILLEGAL),
1693 				    MSG_ORIG(MSG_ARG_CB), optarg);
1694 			}
1695 			break;
1696 
1697 		case 'G':
1698 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1699 			otype = OT_SHARED;
1700 			break;
1701 
1702 		case 'L':
1703 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1704 			break;
1705 
1706 		case 'M':
1707 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1708 			if (aplist_append(&(ofl->ofl_maps), optarg,
1709 			    AL_CNT_OFL_MAPFILES) == NULL)
1710 				return (S_ERROR);
1711 			break;
1712 
1713 		case 'N':
1714 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1715 			break;
1716 
1717 		case 'Q':
1718 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1719 			if ((optarg[0] == 'n') && (optarg[1] == '\0')) {
1720 				if (Qflag != SET_UNKNOWN)
1721 					ld_eprintf(ofl, ERR_WARNING_NF,
1722 					    MSG_INTL(MSG_ARG_MTONCE),
1723 					    MSG_ORIG(MSG_ARG_CQ));
1724 				else
1725 					Qflag = SET_FALSE;
1726 			} else if ((optarg[0] == 'y') && (optarg[1] == '\0')) {
1727 				if (Qflag != SET_UNKNOWN)
1728 					ld_eprintf(ofl, ERR_WARNING_NF,
1729 					    MSG_INTL(MSG_ARG_MTONCE),
1730 					    MSG_ORIG(MSG_ARG_CQ));
1731 				else
1732 					Qflag = SET_TRUE;
1733 			} else {
1734 				ld_eprintf(ofl, ERR_FATAL,
1735 				    MSG_INTL(MSG_ARG_ILLEGAL),
1736 				    MSG_ORIG(MSG_ARG_CQ), optarg);
1737 			}
1738 			break;
1739 
1740 		case 'S':
1741 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1742 			if (aplist_append(&lib_support, optarg,
1743 			    AL_CNT_SUPPORT) == NULL)
1744 				return (S_ERROR);
1745 			break;
1746 
1747 		case 'V':
1748 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1749 			if (!Vflag)
1750 				(void) fprintf(stderr, MSG_ORIG(MSG_STR_STRNL),
1751 				    ofl->ofl_sgsid);
1752 			Vflag = TRUE;
1753 			break;
1754 
1755 		case 'Y':
1756 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1757 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_LCOM), 2) == 0) {
1758 				if (Llibdir)
1759 					ld_eprintf(ofl, ERR_WARNING_NF,
1760 					    MSG_INTL(MSG_ARG_MTONCE),
1761 					    MSG_ORIG(MSG_ARG_CYL));
1762 				else
1763 					Llibdir = optarg + 2;
1764 			} else if (strncmp(optarg,
1765 			    MSG_ORIG(MSG_ARG_UCOM), 2) == 0) {
1766 				if (Ulibdir)
1767 					ld_eprintf(ofl, ERR_WARNING_NF,
1768 					    MSG_INTL(MSG_ARG_MTONCE),
1769 					    MSG_ORIG(MSG_ARG_CYU));
1770 				else
1771 					Ulibdir = optarg + 2;
1772 			} else if (strncmp(optarg,
1773 			    MSG_ORIG(MSG_ARG_PCOM), 2) == 0) {
1774 				if (Plibpath)
1775 					ld_eprintf(ofl, ERR_WARNING_NF,
1776 					    MSG_INTL(MSG_ARG_MTONCE),
1777 					    MSG_ORIG(MSG_ARG_CYP));
1778 				else
1779 					Plibpath = optarg + 2;
1780 			} else {
1781 				ld_eprintf(ofl, ERR_FATAL,
1782 				    MSG_INTL(MSG_ARG_ILLEGAL),
1783 				    MSG_ORIG(MSG_ARG_CY), optarg);
1784 			}
1785 			break;
1786 
1787 		case '?':
1788 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1789 			/*
1790 			 * If the option character is '-', we're looking at a
1791 			 * long option which couldn't be translated, display a
1792 			 * more useful error.
1793 			 */
1794 			if (optopt == '-') {
1795 				eprintf(ofl->ofl_lml, ERR_FATAL,
1796 				    MSG_INTL(MSG_ARG_LONG_UNKNOWN),
1797 				    argv[optind-1]);
1798 			} else {
1799 				eprintf(ofl->ofl_lml, ERR_FATAL,
1800 				    MSG_INTL(MSG_ARG_UNKNOWN), optopt);
1801 			}
1802 			(*usage)++;
1803 			break;
1804 
1805 		default:
1806 			break;
1807 		}
1808 
1809 		/*
1810 		 * Update the argument index for the next getopt() iteration.
1811 		 */
1812 		ndx = optind;
1813 	}
1814 	return (1);
1815 }
1816 
1817 /*
1818  * Parsing options pass2 for
1819  */
1820 static uintptr_t
1821 parseopt_pass2(Ofl_desc *ofl, int argc, char **argv)
1822 {
1823 	int	c, ndx = optind;
1824 
1825 	while ((c = ld_getopt(ofl->ofl_lml, ndx, argc, argv)) != -1) {
1826 		Ifl_desc	*ifl;
1827 		Sym_desc	*sdp;
1828 
1829 		switch (c) {
1830 			case 'l':
1831 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1832 				    optarg));
1833 				if (ld_find_library(optarg, ofl) == S_ERROR)
1834 					return (S_ERROR);
1835 				break;
1836 			case 'B':
1837 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1838 				    optarg));
1839 				if (strcmp(optarg,
1840 				    MSG_ORIG(MSG_STR_LD_DYNAMIC)) == 0) {
1841 					if (ofl->ofl_flags & FLG_OF_DYNAMIC)
1842 						ofl->ofl_flags |=
1843 						    FLG_OF_DYNLIBS;
1844 					else {
1845 						ld_eprintf(ofl, ERR_FATAL,
1846 						    MSG_INTL(MSG_ARG_ST_INCOMP),
1847 						    MSG_ORIG(MSG_ARG_BDYNAMIC));
1848 					}
1849 				} else if (strcmp(optarg,
1850 				    MSG_ORIG(MSG_ARG_STATIC)) == 0)
1851 					ofl->ofl_flags &= ~FLG_OF_DYNLIBS;
1852 				break;
1853 			case 'L':
1854 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1855 				    optarg));
1856 				if (ld_add_libdir(ofl, optarg) == S_ERROR)
1857 					return (S_ERROR);
1858 				break;
1859 			case 'N':
1860 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1861 				    optarg));
1862 				/*
1863 				 * Record DT_NEEDED string
1864 				 */
1865 				if (!(ofl->ofl_flags & FLG_OF_DYNAMIC))
1866 					ld_eprintf(ofl, ERR_FATAL,
1867 					    MSG_INTL(MSG_ARG_ST_INCOMP),
1868 					    MSG_ORIG(MSG_ARG_CN));
1869 				if (((ifl = libld_calloc(1,
1870 				    sizeof (Ifl_desc))) == NULL) ||
1871 				    (aplist_append(&ofl->ofl_sos, ifl,
1872 				    AL_CNT_OFL_LIBS) == NULL))
1873 					return (S_ERROR);
1874 
1875 				ifl->ifl_name = MSG_INTL(MSG_STR_COMMAND);
1876 				ifl->ifl_soname = optarg;
1877 				ifl->ifl_flags = (FLG_IF_NEEDSTR |
1878 				    FLG_IF_FILEREF | FLG_IF_DEPREQD);
1879 
1880 				break;
1881 			case 'D':
1882 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1883 				    optarg));
1884 				(void) dbg_setup(ofl, optarg, 3);
1885 				break;
1886 			case 'u':
1887 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1888 				    optarg));
1889 				if (ld_sym_add_u(optarg, ofl,
1890 				    MSG_STR_COMMAND) == (Sym_desc *)S_ERROR)
1891 					return (S_ERROR);
1892 				break;
1893 			case 'z':
1894 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1895 				    optarg));
1896 				if ((strncmp(optarg, MSG_ORIG(MSG_ARG_LD32),
1897 				    MSG_ARG_LD32_SIZE) == 0) ||
1898 				    (strncmp(optarg, MSG_ORIG(MSG_ARG_LD64),
1899 				    MSG_ARG_LD64_SIZE) == 0)) {
1900 					if (createargv(ofl, 0) == S_ERROR)
1901 						return (S_ERROR);
1902 				} else if (strcmp(optarg,
1903 				    MSG_ORIG(MSG_ARG_ALLEXTRT)) == 0) {
1904 					ofl->ofl_flags1 |= FLG_OF1_ALLEXRT;
1905 					ofl->ofl_flags1 &= ~FLG_OF1_WEAKEXT;
1906 				} else if (strcmp(optarg,
1907 				    MSG_ORIG(MSG_ARG_WEAKEXT)) == 0) {
1908 					ofl->ofl_flags1 |= FLG_OF1_WEAKEXT;
1909 					ofl->ofl_flags1 &= ~FLG_OF1_ALLEXRT;
1910 				} else if (strcmp(optarg,
1911 				    MSG_ORIG(MSG_ARG_DFLEXTRT)) == 0) {
1912 					ofl->ofl_flags1 &=
1913 					    ~(FLG_OF1_ALLEXRT |
1914 					    FLG_OF1_WEAKEXT);
1915 				} else if (strcmp(optarg,
1916 				    MSG_ORIG(MSG_ARG_DIRECT)) == 0) {
1917 					ofl->ofl_flags1 |= FLG_OF1_ZDIRECT;
1918 					ofl->ofl_guideflags |= FLG_OFG_NO_DB;
1919 				} else if (strcmp(optarg,
1920 				    MSG_ORIG(MSG_ARG_NODIRECT)) == 0) {
1921 					ofl->ofl_flags1 &= ~FLG_OF1_ZDIRECT;
1922 					ofl->ofl_guideflags |= FLG_OFG_NO_DB;
1923 				} else if (strcmp(optarg,
1924 				    MSG_ORIG(MSG_ARG_IGNORE)) == 0) {
1925 					ofl->ofl_flags1 |= FLG_OF1_IGNORE;
1926 				} else if (strcmp(optarg,
1927 				    MSG_ORIG(MSG_ARG_RECORD)) == 0) {
1928 					ofl->ofl_flags1 &= ~FLG_OF1_IGNORE;
1929 				} else if (strcmp(optarg,
1930 				    MSG_ORIG(MSG_ARG_LAZYLOAD)) == 0) {
1931 					ofl->ofl_flags1 |= FLG_OF1_LAZYLD;
1932 					ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
1933 				} else if (strcmp(optarg,
1934 				    MSG_ORIG(MSG_ARG_NOLAZYLOAD)) == 0) {
1935 					ofl->ofl_flags1 &= ~ FLG_OF1_LAZYLD;
1936 					ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
1937 				} else if (strcmp(optarg,
1938 				    MSG_ORIG(MSG_ARG_GROUPPERM)) == 0) {
1939 					ofl->ofl_flags1 |= FLG_OF1_GRPPRM;
1940 				} else if (strcmp(optarg,
1941 				    MSG_ORIG(MSG_ARG_NOGROUPPERM)) == 0) {
1942 					ofl->ofl_flags1 &= ~FLG_OF1_GRPPRM;
1943 				} else if (strncmp(optarg,
1944 				    MSG_ORIG(MSG_ARG_INITARRAY),
1945 				    MSG_ARG_INITARRAY_SIZE) == 0) {
1946 					if (((sdp = ld_sym_add_u(optarg +
1947 					    MSG_ARG_INITARRAY_SIZE, ofl,
1948 					    MSG_STR_COMMAND)) ==
1949 					    (Sym_desc *)S_ERROR) ||
1950 					    (aplist_append(&ofl->ofl_initarray,
1951 					    sdp, AL_CNT_OFL_ARRAYS) == NULL))
1952 						return (S_ERROR);
1953 				} else if (strncmp(optarg,
1954 				    MSG_ORIG(MSG_ARG_FINIARRAY),
1955 				    MSG_ARG_FINIARRAY_SIZE) == 0) {
1956 					if (((sdp = ld_sym_add_u(optarg +
1957 					    MSG_ARG_FINIARRAY_SIZE, ofl,
1958 					    MSG_STR_COMMAND)) ==
1959 					    (Sym_desc *)S_ERROR) ||
1960 					    (aplist_append(&ofl->ofl_finiarray,
1961 					    sdp, AL_CNT_OFL_ARRAYS) == NULL))
1962 						return (S_ERROR);
1963 				} else if (strncmp(optarg,
1964 				    MSG_ORIG(MSG_ARG_PREINITARRAY),
1965 				    MSG_ARG_PREINITARRAY_SIZE) == 0) {
1966 					if (((sdp = ld_sym_add_u(optarg +
1967 					    MSG_ARG_PREINITARRAY_SIZE, ofl,
1968 					    MSG_STR_COMMAND)) ==
1969 					    (Sym_desc *)S_ERROR) ||
1970 					    (aplist_append(&ofl->ofl_preiarray,
1971 					    sdp, AL_CNT_OFL_ARRAYS) == NULL))
1972 						return (S_ERROR);
1973 				} else if (strncmp(optarg,
1974 				    MSG_ORIG(MSG_ARG_RTLDINFO),
1975 				    MSG_ARG_RTLDINFO_SIZE) == 0) {
1976 					if (((sdp = ld_sym_add_u(optarg +
1977 					    MSG_ARG_RTLDINFO_SIZE, ofl,
1978 					    MSG_STR_COMMAND)) ==
1979 					    (Sym_desc *)S_ERROR) ||
1980 					    (aplist_append(&ofl->ofl_rtldinfo,
1981 					    sdp, AL_CNT_OFL_ARRAYS) == NULL))
1982 						return (S_ERROR);
1983 				} else if (strncmp(optarg,
1984 				    MSG_ORIG(MSG_ARG_DTRACE),
1985 				    MSG_ARG_DTRACE_SIZE) == 0) {
1986 					if ((sdp = ld_sym_add_u(optarg +
1987 					    MSG_ARG_DTRACE_SIZE, ofl,
1988 					    MSG_STR_COMMAND)) ==
1989 					    (Sym_desc *)S_ERROR)
1990 						return (S_ERROR);
1991 					ofl->ofl_dtracesym = sdp;
1992 				} else if (strcmp(optarg,
1993 				    MSG_ORIG(MSG_ARG_RESCAN_NOW)) == 0) {
1994 					if (ld_rescan_archives(ofl, 0, ndx) ==
1995 					    S_ERROR)
1996 						return (S_ERROR);
1997 				} else if (strcmp(optarg,
1998 				    MSG_ORIG(MSG_ARG_RESCAN_START)) == 0) {
1999 					ofl->ofl_ars_gsndx = ofl->ofl_arscnt;
2000 					ofl->ofl_ars_gsandx = ndx;
2001 				} else if (strcmp(optarg,
2002 				    MSG_ORIG(MSG_ARG_RESCAN_END)) == 0) {
2003 					if (ld_rescan_archives(ofl, 1, ndx) ==
2004 					    S_ERROR)
2005 						return (S_ERROR);
2006 				} else if (strcmp(optarg,
2007 				    MSG_ORIG(MSG_ARG_DEFERRED)) == 0) {
2008 					ofl->ofl_flags1 |= FLG_OF1_DEFERRED;
2009 				} else if (strcmp(optarg,
2010 				    MSG_ORIG(MSG_ARG_NODEFERRED)) == 0) {
2011 					ofl->ofl_flags1 &= ~FLG_OF1_DEFERRED;
2012 				}
2013 			default:
2014 				break;
2015 		}
2016 
2017 		/*
2018 		 * Update the argument index for the next getopt() iteration.
2019 		 */
2020 		ndx = optind;
2021 	}
2022 	return (1);
2023 }
2024 
2025 /*
2026  *
2027  * Pass 1 -- process_flags: collects all options and sets flags
2028  */
2029 static uintptr_t
2030 process_flags_com(Ofl_desc *ofl, int argc, char **argv, int *usage)
2031 {
2032 	for (; optind < argc; optind++) {
2033 		/*
2034 		 * If we detect some more options return to getopt().
2035 		 * Checking argv[optind][1] against null prevents a forever
2036 		 * loop if an unadorned `-' argument is passed to us.
2037 		 */
2038 		while ((optind < argc) && (argv[optind][0] == '-')) {
2039 			if (argv[optind][1] != '\0') {
2040 				if (parseopt_pass1(ofl, argc, argv,
2041 				    usage) == S_ERROR)
2042 					return (S_ERROR);
2043 			} else if (++optind < argc)
2044 				continue;
2045 		}
2046 		if (optind >= argc)
2047 			break;
2048 		ofl->ofl_objscnt++;
2049 	}
2050 
2051 	/* Did an unterminated archive group run off the end? */
2052 	if (ofl->ofl_ars_gsandx > 0) {
2053 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_AR_GRP_BAD),
2054 		    MSG_INTL(MSG_MARG_AR_GRP_START),
2055 		    MSG_INTL(MSG_MARG_AR_GRP_END));
2056 		return (S_ERROR);
2057 	}
2058 
2059 	return (1);
2060 }
2061 
2062 uintptr_t
2063 ld_process_flags(Ofl_desc *ofl, int argc, char **argv)
2064 {
2065 	int	usage = 0;	/* Collect all argument errors before exit */
2066 
2067 	if (argc < 2) {
2068 		usage_mesg(FALSE);
2069 		return (S_ERROR);
2070 	}
2071 
2072 	/*
2073 	 * Option handling
2074 	 */
2075 	opterr = 0;
2076 	optind = 1;
2077 	if (process_flags_com(ofl, argc, argv, &usage) == S_ERROR)
2078 		return (S_ERROR);
2079 
2080 	/*
2081 	 * Having parsed everything, did we have any usage errors.
2082 	 */
2083 	if (usage) {
2084 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_USEHELP));
2085 		return (S_ERROR);
2086 	}
2087 
2088 	return (check_flags(ofl, argc));
2089 }
2090 
2091 /*
2092  * Pass 2 -- process_files: skips the flags collected in pass 1 and processes
2093  * files.
2094  */
2095 static uintptr_t
2096 process_files_com(Ofl_desc *ofl, int argc, char **argv)
2097 {
2098 	for (; optind < argc; optind++) {
2099 		int		fd;
2100 		uintptr_t	open_ret;
2101 		char		*path;
2102 		Rej_desc	rej = { 0 };
2103 
2104 		/*
2105 		 * If we detect some more options return to getopt().
2106 		 * Checking argv[optind][1] against null prevents a forever
2107 		 * loop if an unadorned `-' argument is passed to us.
2108 		 */
2109 		while ((optind < argc) && (argv[optind][0] == '-')) {
2110 			if (argv[optind][1] != '\0') {
2111 				if (parseopt_pass2(ofl, argc, argv) == S_ERROR)
2112 					return (S_ERROR);
2113 			} else if (++optind < argc)
2114 				continue;
2115 		}
2116 		if (optind >= argc)
2117 			break;
2118 
2119 		path = argv[optind];
2120 		if ((fd = open(path, O_RDONLY)) == -1) {
2121 			int err = errno;
2122 
2123 			ld_eprintf(ofl, ERR_FATAL,
2124 			    MSG_INTL(MSG_SYS_OPEN), path, strerror(err));
2125 			continue;
2126 		}
2127 
2128 		DBG_CALL(Dbg_args_file(ofl->ofl_lml, optind, path));
2129 
2130 		open_ret = ld_process_open(path, path, &fd, ofl,
2131 		    (FLG_IF_CMDLINE | FLG_IF_NEEDED), &rej, NULL);
2132 		if (fd != -1)
2133 			(void) close(fd);
2134 		if (open_ret == S_ERROR)
2135 			return (S_ERROR);
2136 
2137 		/*
2138 		 * Check for mismatched input.
2139 		 */
2140 		if (rej.rej_type) {
2141 			Conv_reject_desc_buf_t rej_buf;
2142 
2143 			ld_eprintf(ofl, ERR_FATAL,
2144 			    MSG_INTL(reject[rej.rej_type]),
2145 			    rej.rej_name ? rej.rej_name :
2146 			    MSG_INTL(MSG_STR_UNKNOWN),
2147 			    conv_reject_desc(&rej, &rej_buf,
2148 			    ld_targ.t_m.m_mach));
2149 			return (1);
2150 		}
2151 	}
2152 	return (1);
2153 }
2154 
2155 uintptr_t
2156 ld_process_files(Ofl_desc *ofl, int argc, char **argv)
2157 {
2158 	DBG_CALL(Dbg_basic_files(ofl->ofl_lml));
2159 
2160 	/*
2161 	 * Process command line files (taking into account any applicable
2162 	 * preceding flags).  Return if any fatal errors have occurred.
2163 	 */
2164 	opterr = 0;
2165 	optind = 1;
2166 	if (process_files_com(ofl, argc, argv) == S_ERROR)
2167 		return (S_ERROR);
2168 	if (ofl->ofl_flags & FLG_OF_FATAL)
2169 		return (1);
2170 
2171 	/*
2172 	 * Guidance: Use -B direct/nodirect or -z direct/nodirect.
2173 	 *
2174 	 * This is a backstop for the case where the link had no dependencies.
2175 	 * Otherwise, it will get caught by ld_process_ifl(). We need both,
2176 	 * because -z direct is positional, and its value at the time where
2177 	 * the first dependency is seen might be different than it is now.
2178 	 */
2179 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) &&
2180 	    OFL_GUIDANCE(ofl, FLG_OFG_NO_DB)) {
2181 		ld_eprintf(ofl, ERR_GUIDANCE, MSG_INTL(MSG_GUIDE_DIRECT));
2182 		ofl->ofl_guideflags |= FLG_OFG_NO_DB;
2183 	}
2184 
2185 	/*
2186 	 * Now that all command line files have been processed see if there are
2187 	 * any additional `needed' shared object dependencies.
2188 	 */
2189 	if (ofl->ofl_soneed)
2190 		if (ld_finish_libs(ofl) == S_ERROR)
2191 			return (S_ERROR);
2192 
2193 	/*
2194 	 * If rescanning archives is enabled, do so now to determine whether
2195 	 * there might still be members extracted to satisfy references from any
2196 	 * explicit objects.  Continue until no new objects are extracted.  Note
2197 	 * that this pass is carried out *after* processing any implicit objects
2198 	 * (above) as they may already have resolved any undefined references
2199 	 * from any explicit dependencies.
2200 	 */
2201 	if (ofl->ofl_flags1 & FLG_OF1_RESCAN) {
2202 		if (ld_rescan_archives(ofl, 0, argc) == S_ERROR)
2203 			return (S_ERROR);
2204 		if (ofl->ofl_flags & FLG_OF_FATAL)
2205 			return (1);
2206 	}
2207 
2208 	/*
2209 	 * If debugging, provide statistics on each archives extraction, or flag
2210 	 * any archive that has provided no members.  Note that this could be a
2211 	 * nice place to free up much of the archive infrastructure, as we've
2212 	 * extracted any members we need.  However, as we presently don't free
2213 	 * anything under ld(1) there's not much point in proceeding further.
2214 	 */
2215 	DBG_CALL(Dbg_statistics_ar(ofl));
2216 
2217 	/*
2218 	 * If any version definitions have been established, either via input
2219 	 * from a mapfile or from the input relocatable objects, make sure any
2220 	 * version dependencies are satisfied, and version symbols created.
2221 	 */
2222 	if (ofl->ofl_verdesc)
2223 		if (ld_vers_check_defs(ofl) == S_ERROR)
2224 			return (S_ERROR);
2225 
2226 	/*
2227 	 * If input section ordering was specified within some segment
2228 	 * using a mapfile, verify that the expected sections were seen.
2229 	 */
2230 	if (ofl->ofl_flags & FLG_OF_IS_ORDER)
2231 		ld_ent_check(ofl);
2232 
2233 	return (1);
2234 }
2235 
2236 uintptr_t
2237 ld_init_strings(Ofl_desc *ofl)
2238 {
2239 	uint_t	stflags;
2240 
2241 	if (ofl->ofl_flags1 & FLG_OF1_NCSTTAB)
2242 		stflags = 0;
2243 	else
2244 		stflags = FLG_STNEW_COMPRESS;
2245 
2246 	if (((ofl->ofl_shdrsttab = st_new(stflags)) == NULL) ||
2247 	    ((ofl->ofl_strtab = st_new(stflags)) == NULL) ||
2248 	    ((ofl->ofl_dynstrtab = st_new(stflags)) == NULL))
2249 		return (S_ERROR);
2250 
2251 	return (0);
2252 }
2253