xref: /illumos-gate/usr/src/cmd/sgs/libld/common/args.c (revision 3bbba396f8599b5a3225ae7d8bb0119abdfaa427)
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 		{ NULL,					0 }
894 	};
895 
896 	char		*lasts, *name;
897 	item_desc	*item;
898 	ofl_guideflag_t	ofl_guideflags = FLG_OFG_ENABLE;
899 
900 	/*
901 	 * Skip the 'guidance' prefix. If NULL terminated, there are no
902 	 * item values to parse. Otherwise, skip the '=' and parse the items.
903 	 */
904 	optarg += MSG_ARG_GUIDE_SIZE;
905 	if (*optarg == '=') {
906 		optarg++;
907 
908 		if ((name = libld_malloc(strlen(optarg) + 1)) == NULL)
909 			return (FALSE);
910 		(void) strcpy(name, optarg);
911 
912 		if ((name = strtok_r(name, MSG_ORIG(MSG_ARG_GUIDE_DELIM),
913 		    &lasts)) != NULL) {
914 			do {
915 				for (item = items; item->name != NULL; item++)
916 					if (strcasecmp(name, item->name) == 0)
917 						break;
918 				if (item->name == NULL) {
919 					ld_eprintf(ofl, ERR_GUIDANCE,
920 					    MSG_INTL(MSG_GUIDE_UNKNOWN), name);
921 					continue;
922 				}
923 				ofl_guideflags |= item->flag;
924 			} while ((name = strtok_r(NULL,
925 			    MSG_ORIG(MSG_ARG_GUIDE_DELIM), &lasts)) != NULL);
926 		}
927 	}
928 
929 	/*
930 	 * If -zguidance is used more than once, we take the first one. We
931 	 * do this quietly if they have identical options, and with a warning
932 	 * otherwise.
933 	 */
934 	if ((initial_guidance_flags & FLG_OFG_ENABLE) &&
935 	    (ofl_guideflags != initial_guidance_flags)) {
936 		ld_eprintf(ofl, ERR_WARNING_NF, MSG_INTL(MSG_ARG_MTONCE),
937 		    MSG_ORIG(MSG_ARG_ZGUIDE));
938 		return (TRUE);
939 	}
940 
941 	/*
942 	 * First time: Save the flags for comparison to any subsequent
943 	 * -z guidance that comes along, and OR the resulting flags into
944 	 * the flags kept in the output descriptor.
945 	 */
946 	initial_guidance_flags = ofl_guideflags;
947 	ofl->ofl_guideflags |= ofl_guideflags;
948 	return (TRUE);
949 }
950 
951 /*
952  * Parse the -z assert-deflib option. This option can appear in two different
953  * forms:
954  *	-z assert-deflib
955  *	-z assert-deflib=libfred.so
956  *
957  * Either form enables this option, the latter form marks libfred.so as an
958  * exempt library from the check. It is valid to have multiple invocations of
959  * the second form. We silently ignore mulitple occurrences of the first form
960  * and multiple invocations of the first form when the second form also occurs.
961  *
962  * We only return false when we have an internal error, such as the failure of
963  * aplist_append. Every other time we return true, but we have the appropriate
964  * fatal flags set beacuse of the ld_eprintf.
965  */
966 static int
967 assdeflib_parse(Ofl_desc *ofl, char *optarg)
968 {
969 	size_t olen, mlen;
970 	ofl->ofl_flags |= FLG_OF_ADEFLIB;
971 
972 	olen = strlen(optarg);
973 	/* Minimum size of assert-deflib=lib%s.so */
974 	mlen = MSG_ARG_ASSDEFLIB_SIZE + 1 + MSG_STR_LIB_SIZE +
975 	    MSG_STR_SOEXT_SIZE;
976 	if (olen > MSG_ARG_ASSDEFLIB_SIZE) {
977 		if (optarg[MSG_ARG_ASSDEFLIB_SIZE] != '=') {
978 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_ILLEGAL),
979 			    MSG_ORIG(MSG_ARG_ASSDEFLIB), optarg);
980 			return (TRUE);
981 		}
982 
983 		if (strncmp(optarg + MSG_ARG_ASSDEFLIB_SIZE + 1,
984 		    MSG_ORIG(MSG_STR_LIB), MSG_STR_LIB_SIZE) != 0 ||
985 		    strcmp(optarg + olen - MSG_STR_SOEXT_SIZE,
986 		    MSG_ORIG(MSG_STR_SOEXT)) != 0 || olen <= mlen) {
987 			ld_eprintf(ofl, ERR_FATAL,
988 			    MSG_INTL(MSG_ARG_ASSDEFLIB_MALFORMED), optarg);
989 			return (TRUE);
990 		}
991 
992 		if (aplist_append(&ofl->ofl_assdeflib, optarg +
993 		    MSG_ARG_ASSDEFLIB_SIZE + 1, AL_CNT_ASSDEFLIB) == NULL)
994 			return (FALSE);
995 	}
996 
997 	return (TRUE);
998 }
999 
1000 static int	optitle = 0;
1001 /*
1002  * Parsing options pass1 for process_flags().
1003  */
1004 static uintptr_t
1005 parseopt_pass1(Ofl_desc *ofl, int argc, char **argv, int *usage)
1006 {
1007 	int	c, ndx = optind;
1008 
1009 	/*
1010 	 * The -32, -64 and -ztarget options are special, in that we validate
1011 	 * them, but otherwise ignore them. libld.so (this code) is called
1012 	 * from the ld front end program. ld has already examined the
1013 	 * arguments to determine the output class and machine type of the
1014 	 * output object, as reflected in the version (32/64) of ld_main()
1015 	 * that was called and the value of the 'mach' argument passed.
1016 	 * By time execution reaches this point, these options have already
1017 	 * been seen and acted on.
1018 	 */
1019 	while ((c = ld_getopt(ofl->ofl_lml, ndx, argc, argv)) != -1) {
1020 
1021 		switch (c) {
1022 		case '3':
1023 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1024 
1025 			/*
1026 			 * -32 is processed by ld to determine the output class.
1027 			 * Here we sanity check the option incase some other
1028 			 * -3* option is mistakenly passed to us.
1029 			 */
1030 			if (optarg[0] != '2')
1031 				ld_eprintf(ofl, ERR_FATAL,
1032 				    MSG_INTL(MSG_ARG_ILLEGAL),
1033 				    MSG_ORIG(MSG_ARG_3), optarg);
1034 			continue;
1035 
1036 		case '6':
1037 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1038 
1039 			/*
1040 			 * -64 is processed by ld to determine the output class.
1041 			 * Here we sanity check the option incase some other
1042 			 * -6* option is mistakenly passed to us.
1043 			 */
1044 			if (optarg[0] != '4')
1045 				ld_eprintf(ofl, ERR_FATAL,
1046 				    MSG_INTL(MSG_ARG_ILLEGAL),
1047 				    MSG_ORIG(MSG_ARG_6), optarg);
1048 			continue;
1049 
1050 		case 'a':
1051 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1052 			aflag = TRUE;
1053 			break;
1054 
1055 		case 'b':
1056 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1057 			bflag = TRUE;
1058 
1059 			/*
1060 			 * This is a hack, and may be undone later.
1061 			 * The -b option is only used to build the Unix
1062 			 * kernel and its related kernel-mode modules.
1063 			 * We do not want those files to get a .SUNW_ldynsym
1064 			 * section. At least for now, the kernel makes no
1065 			 * use of .SUNW_ldynsym, and we do not want to use
1066 			 * the space to hold it. Therefore, we overload
1067 			 * the use of -b to also imply -znoldynsym.
1068 			 */
1069 			ofl->ofl_flags |= FLG_OF_NOLDYNSYM;
1070 			break;
1071 
1072 		case 'c':
1073 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1074 			if (ofl->ofl_config)
1075 				ld_eprintf(ofl, ERR_WARNING_NF,
1076 				    MSG_INTL(MSG_ARG_MTONCE),
1077 				    MSG_ORIG(MSG_ARG_C));
1078 			else
1079 				ofl->ofl_config = optarg;
1080 			break;
1081 
1082 		case 'C':
1083 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1084 			demangle_flag = 1;
1085 			break;
1086 
1087 		case 'd':
1088 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1089 			if ((optarg[0] == 'n') && (optarg[1] == '\0')) {
1090 				if (dflag != SET_UNKNOWN)
1091 					ld_eprintf(ofl, ERR_WARNING_NF,
1092 					    MSG_INTL(MSG_ARG_MTONCE),
1093 					    MSG_ORIG(MSG_ARG_D));
1094 				else
1095 					dflag = SET_FALSE;
1096 			} else if ((optarg[0] == 'y') && (optarg[1] == '\0')) {
1097 				if (dflag != SET_UNKNOWN)
1098 					ld_eprintf(ofl, ERR_WARNING_NF,
1099 					    MSG_INTL(MSG_ARG_MTONCE),
1100 					    MSG_ORIG(MSG_ARG_D));
1101 				else
1102 					dflag = SET_TRUE;
1103 			} else {
1104 				ld_eprintf(ofl, ERR_FATAL,
1105 				    MSG_INTL(MSG_ARG_ILLEGAL),
1106 				    MSG_ORIG(MSG_ARG_D), optarg);
1107 			}
1108 			break;
1109 
1110 		case 'e':
1111 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1112 			if (ofl->ofl_entry)
1113 				ld_eprintf(ofl, ERR_WARNING_NF,
1114 				    MSG_INTL(MSG_MARG_MTONCE),
1115 				    MSG_INTL(MSG_MARG_ENTRY));
1116 			else
1117 				ofl->ofl_entry = (void *)optarg;
1118 			break;
1119 
1120 		case 'f':
1121 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1122 			if (ofl->ofl_filtees &&
1123 			    (!(ofl->ofl_flags & FLG_OF_AUX))) {
1124 				ld_eprintf(ofl, ERR_FATAL,
1125 				    MSG_INTL(MSG_MARG_INCOMP),
1126 				    MSG_INTL(MSG_MARG_FILTER_AUX),
1127 				    MSG_INTL(MSG_MARG_FILTER));
1128 			} else {
1129 				if ((ofl->ofl_filtees =
1130 				    add_string(ofl->ofl_filtees, optarg)) ==
1131 				    (const char *)S_ERROR)
1132 					return (S_ERROR);
1133 				ofl->ofl_flags |= FLG_OF_AUX;
1134 			}
1135 			break;
1136 
1137 		case 'F':
1138 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1139 			if (ofl->ofl_filtees &&
1140 			    (ofl->ofl_flags & FLG_OF_AUX)) {
1141 				ld_eprintf(ofl, ERR_FATAL,
1142 				    MSG_INTL(MSG_MARG_INCOMP),
1143 				    MSG_INTL(MSG_MARG_FILTER),
1144 				    MSG_INTL(MSG_MARG_FILTER_AUX));
1145 			} else {
1146 				if ((ofl->ofl_filtees =
1147 				    add_string(ofl->ofl_filtees, optarg)) ==
1148 				    (const char *)S_ERROR)
1149 					return (S_ERROR);
1150 			}
1151 			break;
1152 
1153 		case 'h':
1154 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1155 			if (ofl->ofl_soname)
1156 				ld_eprintf(ofl, ERR_WARNING_NF,
1157 				    MSG_INTL(MSG_MARG_MTONCE),
1158 				    MSG_INTL(MSG_MARG_SONAME));
1159 			else
1160 				ofl->ofl_soname = (const char *)optarg;
1161 			break;
1162 
1163 		case 'i':
1164 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1165 			ofl->ofl_flags |= FLG_OF_IGNENV;
1166 			break;
1167 
1168 		case 'I':
1169 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1170 			if (ofl->ofl_interp)
1171 				ld_eprintf(ofl, ERR_WARNING_NF,
1172 				    MSG_INTL(MSG_ARG_MTONCE),
1173 				    MSG_ORIG(MSG_ARG_CI));
1174 			else
1175 				ofl->ofl_interp = (const char *)optarg;
1176 			break;
1177 
1178 		case 'l':
1179 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1180 			/*
1181 			 * For now, count any library as a shared object.  This
1182 			 * is used to size the internal symbol cache.  This
1183 			 * value is recalculated later on actual file processing
1184 			 * to get an accurate shared object count.
1185 			 */
1186 			ofl->ofl_soscnt++;
1187 			break;
1188 
1189 		case 'm':
1190 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1191 			ofl->ofl_flags |= FLG_OF_GENMAP;
1192 			break;
1193 
1194 		case 'o':
1195 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1196 			if (ofl->ofl_name)
1197 				ld_eprintf(ofl, ERR_WARNING_NF,
1198 				    MSG_INTL(MSG_MARG_MTONCE),
1199 				    MSG_INTL(MSG_MARG_OUTFILE));
1200 			else
1201 				ofl->ofl_name = (const char *)optarg;
1202 			break;
1203 
1204 		case 'p':
1205 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1206 
1207 			/*
1208 			 * Multiple instances of this option may occur.  Each
1209 			 * additional instance is effectively concatenated to
1210 			 * the previous separated by a colon.
1211 			 */
1212 			if (*optarg != '\0') {
1213 				if ((ofl->ofl_audit =
1214 				    add_string(ofl->ofl_audit,
1215 				    optarg)) == (const char *)S_ERROR)
1216 					return (S_ERROR);
1217 			}
1218 			break;
1219 
1220 		case 'P':
1221 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1222 
1223 			/*
1224 			 * Multiple instances of this option may occur.  Each
1225 			 * additional instance is effectively concatenated to
1226 			 * the previous separated by a colon.
1227 			 */
1228 			if (*optarg != '\0') {
1229 				if ((ofl->ofl_depaudit =
1230 				    add_string(ofl->ofl_depaudit,
1231 				    optarg)) == (const char *)S_ERROR)
1232 					return (S_ERROR);
1233 			}
1234 			break;
1235 
1236 		case 'r':
1237 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1238 			otype = OT_RELOC;
1239 			break;
1240 
1241 		case 'R':
1242 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1243 
1244 			/*
1245 			 * Multiple instances of this option may occur.  Each
1246 			 * additional instance is effectively concatenated to
1247 			 * the previous separated by a colon.
1248 			 */
1249 			if (*optarg != '\0') {
1250 				if ((ofl->ofl_rpath =
1251 				    add_string(ofl->ofl_rpath,
1252 				    optarg)) == (const char *)S_ERROR)
1253 					return (S_ERROR);
1254 			}
1255 			break;
1256 
1257 		case 's':
1258 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1259 			sflag = TRUE;
1260 			break;
1261 
1262 		case 't':
1263 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1264 			ofl->ofl_flags |= FLG_OF_NOWARN;
1265 			break;
1266 
1267 		case 'u':
1268 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1269 			break;
1270 
1271 		case 'z':
1272 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1273 
1274 			/*
1275 			 * Skip comma that might be present between -z and its
1276 			 * argument (e.g. if -Wl,-z,assert-deflib was passed).
1277 			 */
1278 			if (strncmp(optarg, MSG_ORIG(MSG_STR_COMMA),
1279 			    MSG_STR_COMMA_SIZE) == 0)
1280 				optarg++;
1281 
1282 			/*
1283 			 * For specific help, print our usage message and exit
1284 			 * immediately to ensure a 0 return code.
1285 			 */
1286 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_HELP),
1287 			    MSG_ARG_HELP_SIZE) == 0) {
1288 				usage_mesg(TRUE);
1289 				exit(0);
1290 			}
1291 
1292 			/*
1293 			 * For some options set a flag - further consistancy
1294 			 * checks will be carried out in check_flags().
1295 			 */
1296 			if ((strncmp(optarg, MSG_ORIG(MSG_ARG_LD32),
1297 			    MSG_ARG_LD32_SIZE) == 0) ||
1298 			    (strncmp(optarg, MSG_ORIG(MSG_ARG_LD64),
1299 			    MSG_ARG_LD64_SIZE) == 0)) {
1300 				if (createargv(ofl, usage) == S_ERROR)
1301 					return (S_ERROR);
1302 
1303 			} else if (
1304 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DEFS)) == 0) {
1305 				if (zdflag != SET_UNKNOWN)
1306 					ld_eprintf(ofl, ERR_WARNING_NF,
1307 					    MSG_INTL(MSG_ARG_MTONCE),
1308 					    MSG_ORIG(MSG_ARG_ZDEFNODEF));
1309 				else
1310 					zdflag = SET_TRUE;
1311 				ofl->ofl_guideflags |= FLG_OFG_NO_DEFS;
1312 			} else if (strcmp(optarg,
1313 			    MSG_ORIG(MSG_ARG_NODEFS)) == 0) {
1314 				if (zdflag != SET_UNKNOWN)
1315 					ld_eprintf(ofl, ERR_WARNING_NF,
1316 					    MSG_INTL(MSG_ARG_MTONCE),
1317 					    MSG_ORIG(MSG_ARG_ZDEFNODEF));
1318 				else
1319 					zdflag = SET_FALSE;
1320 				ofl->ofl_guideflags |= FLG_OFG_NO_DEFS;
1321 			} else if (strcmp(optarg,
1322 			    MSG_ORIG(MSG_ARG_TEXT)) == 0) {
1323 				if (ztflag &&
1324 				    (ztflag != MSG_ORIG(MSG_ARG_ZTEXT)))
1325 					ld_eprintf(ofl, ERR_FATAL,
1326 					    MSG_INTL(MSG_ARG_INCOMP),
1327 					    MSG_ORIG(MSG_ARG_ZTEXT),
1328 					    ztflag);
1329 				ztflag = MSG_ORIG(MSG_ARG_ZTEXT);
1330 			} else if (strcmp(optarg,
1331 			    MSG_ORIG(MSG_ARG_TEXTOFF)) == 0) {
1332 				if (ztflag &&
1333 				    (ztflag != MSG_ORIG(MSG_ARG_ZTEXTOFF)))
1334 					ld_eprintf(ofl, ERR_FATAL,
1335 					    MSG_INTL(MSG_ARG_INCOMP),
1336 					    MSG_ORIG(MSG_ARG_ZTEXTOFF),
1337 					    ztflag);
1338 				ztflag = MSG_ORIG(MSG_ARG_ZTEXTOFF);
1339 			} else if (strcmp(optarg,
1340 			    MSG_ORIG(MSG_ARG_TEXTWARN)) == 0) {
1341 				if (ztflag &&
1342 				    (ztflag != MSG_ORIG(MSG_ARG_ZTEXTWARN)))
1343 					ld_eprintf(ofl, ERR_FATAL,
1344 					    MSG_INTL(MSG_ARG_INCOMP),
1345 					    MSG_ORIG(MSG_ARG_ZTEXTWARN),
1346 					    ztflag);
1347 				ztflag = MSG_ORIG(MSG_ARG_ZTEXTWARN);
1348 
1349 			/*
1350 			 * For other options simply set the ofl flags directly.
1351 			 */
1352 			} else if (strcmp(optarg,
1353 			    MSG_ORIG(MSG_ARG_RESCAN)) == 0) {
1354 				ofl->ofl_flags1 |= FLG_OF1_RESCAN;
1355 			} else if (strcmp(optarg,
1356 			    MSG_ORIG(MSG_ARG_ABSEXEC)) == 0) {
1357 				ofl->ofl_flags1 |= FLG_OF1_ABSEXEC;
1358 			} else if (strcmp(optarg,
1359 			    MSG_ORIG(MSG_ARG_LOADFLTR)) == 0) {
1360 				zlflag = TRUE;
1361 			} else if (strcmp(optarg,
1362 			    MSG_ORIG(MSG_ARG_NORELOC)) == 0) {
1363 				ofl->ofl_dtflags_1 |= DF_1_NORELOC;
1364 			} else if (strcmp(optarg,
1365 			    MSG_ORIG(MSG_ARG_NOVERSION)) == 0) {
1366 				ofl->ofl_flags |= FLG_OF_NOVERSEC;
1367 			} else if (strcmp(optarg,
1368 			    MSG_ORIG(MSG_ARG_MULDEFS)) == 0) {
1369 				ofl->ofl_flags |= FLG_OF_MULDEFS;
1370 			} else if (strcmp(optarg,
1371 			    MSG_ORIG(MSG_ARG_REDLOCSYM)) == 0) {
1372 				ofl->ofl_flags |= FLG_OF_REDLSYM;
1373 			} else if (strcmp(optarg,
1374 			    MSG_ORIG(MSG_ARG_INITFIRST)) == 0) {
1375 				ofl->ofl_dtflags_1 |= DF_1_INITFIRST;
1376 			} else if (strcmp(optarg,
1377 			    MSG_ORIG(MSG_ARG_NODELETE)) == 0) {
1378 				ofl->ofl_dtflags_1 |= DF_1_NODELETE;
1379 			} else if (strcmp(optarg,
1380 			    MSG_ORIG(MSG_ARG_NOPARTIAL)) == 0) {
1381 				ofl->ofl_flags1 |= FLG_OF1_NOPARTI;
1382 			} else if (strcmp(optarg,
1383 			    MSG_ORIG(MSG_ARG_NOOPEN)) == 0) {
1384 				ofl->ofl_dtflags_1 |= DF_1_NOOPEN;
1385 			} else if (strcmp(optarg,
1386 			    MSG_ORIG(MSG_ARG_NOW)) == 0) {
1387 				ofl->ofl_dtflags_1 |= DF_1_NOW;
1388 				ofl->ofl_dtflags |= DF_BIND_NOW;
1389 			} else if (strcmp(optarg,
1390 			    MSG_ORIG(MSG_ARG_ORIGIN)) == 0) {
1391 				ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1392 				ofl->ofl_dtflags |= DF_ORIGIN;
1393 			} else if (strcmp(optarg,
1394 			    MSG_ORIG(MSG_ARG_NODEFAULTLIB)) == 0) {
1395 				ofl->ofl_dtflags_1 |= DF_1_NODEFLIB;
1396 			} else if (strcmp(optarg,
1397 			    MSG_ORIG(MSG_ARG_NODUMP)) == 0) {
1398 				ofl->ofl_dtflags_1 |= DF_1_NODUMP;
1399 			} else if (strcmp(optarg,
1400 			    MSG_ORIG(MSG_ARG_ENDFILTEE)) == 0) {
1401 				ofl->ofl_dtflags_1 |= DF_1_ENDFILTEE;
1402 			} else if (strcmp(optarg,
1403 			    MSG_ORIG(MSG_ARG_VERBOSE)) == 0) {
1404 				ofl->ofl_flags |= FLG_OF_VERBOSE;
1405 			} else if (strcmp(optarg,
1406 			    MSG_ORIG(MSG_ARG_COMBRELOC)) == 0) {
1407 				ofl->ofl_flags |= FLG_OF_COMREL;
1408 			} else if (strcmp(optarg,
1409 			    MSG_ORIG(MSG_ARG_NOCOMBRELOC)) == 0) {
1410 				ofl->ofl_flags |= FLG_OF_NOCOMREL;
1411 			} else if (strcmp(optarg,
1412 			    MSG_ORIG(MSG_ARG_NOCOMPSTRTAB)) == 0) {
1413 				ofl->ofl_flags1 |= FLG_OF1_NCSTTAB;
1414 			} else if (strcmp(optarg,
1415 			    MSG_ORIG(MSG_ARG_NOINTERP)) == 0) {
1416 				ofl->ofl_flags1 |= FLG_OF1_NOINTRP;
1417 			} else if (strcmp(optarg,
1418 			    MSG_ORIG(MSG_ARG_INTERPOSE)) == 0) {
1419 				zinflag = TRUE;
1420 			} else if (strcmp(optarg,
1421 			    MSG_ORIG(MSG_ARG_IGNORE)) == 0) {
1422 				ofl->ofl_flags1 |= FLG_OF1_IGNPRC;
1423 			} else if (strcmp(optarg,
1424 			    MSG_ORIG(MSG_ARG_RELAXRELOC)) == 0) {
1425 				ofl->ofl_flags1 |= FLG_OF1_RLXREL;
1426 			} else if (strcmp(optarg,
1427 			    MSG_ORIG(MSG_ARG_NORELAXRELOC)) == 0) {
1428 				ofl->ofl_flags1 |= FLG_OF1_NRLXREL;
1429 			} else if (strcmp(optarg,
1430 			    MSG_ORIG(MSG_ARG_NOLDYNSYM)) == 0) {
1431 				ofl->ofl_flags |= FLG_OF_NOLDYNSYM;
1432 			} else if (strcmp(optarg,
1433 			    MSG_ORIG(MSG_ARG_GLOBAUDIT)) == 0) {
1434 				ofl->ofl_dtflags_1 |= DF_1_GLOBAUDIT;
1435 			} else if (strcmp(optarg,
1436 			    MSG_ORIG(MSG_ARG_NOSIGHANDLER)) == 0) {
1437 				ofl->ofl_flags1 |= FLG_OF1_NOSGHND;
1438 			} else if (strcmp(optarg,
1439 			    MSG_ORIG(MSG_ARG_SYMBOLCAP)) == 0) {
1440 				ofl->ofl_flags |= FLG_OF_OTOSCAP;
1441 
1442 			/*
1443 			 * Check archive group usage
1444 			 *	-z rescan-start ... -z rescan-end
1445 			 * to ensure they don't overlap and are well formed.
1446 			 */
1447 			} else if (strcmp(optarg,
1448 			    MSG_ORIG(MSG_ARG_RESCAN_START)) == 0) {
1449 				if (ofl->ofl_ars_gsandx == 0) {
1450 					ofl->ofl_ars_gsandx = ndx;
1451 				} else if (ofl->ofl_ars_gsandx > 0) {
1452 					/* Another group is still open */
1453 					ld_eprintf(ofl, ERR_FATAL,
1454 					    MSG_INTL(MSG_ARG_AR_GRP_OLAP),
1455 					    MSG_INTL(MSG_MARG_AR_GRPS));
1456 					/* Don't report cascading errors */
1457 					ofl->ofl_ars_gsandx = -1;
1458 				}
1459 			} else if (strcmp(optarg,
1460 			    MSG_ORIG(MSG_ARG_RESCAN_END)) == 0) {
1461 				if (ofl->ofl_ars_gsandx > 0) {
1462 					ofl->ofl_ars_gsandx = 0;
1463 				} else if (ofl->ofl_ars_gsandx == 0) {
1464 					/* There was no matching begin */
1465 					ld_eprintf(ofl, ERR_FATAL,
1466 					    MSG_INTL(MSG_ARG_AR_GRP_BAD),
1467 					    MSG_INTL(MSG_MARG_AR_GRP_END),
1468 					    MSG_INTL(MSG_MARG_AR_GRP_START));
1469 					/* Don't report cascading errors */
1470 					ofl->ofl_ars_gsandx = -1;
1471 				}
1472 
1473 			/*
1474 			 * If -z wrap is seen, enter the symbol to be wrapped
1475 			 * into the wrap AVL tree.
1476 			 */
1477 			} else if (strncmp(optarg, MSG_ORIG(MSG_ARG_WRAP),
1478 			    MSG_ARG_WRAP_SIZE) == 0) {
1479 				if (ld_wrap_enter(ofl,
1480 				    optarg + MSG_ARG_WRAP_SIZE) == NULL)
1481 					return (S_ERROR);
1482 			} else if (strncmp(optarg, MSG_ORIG(MSG_ARG_ASLR),
1483 			    MSG_ARG_ASLR_SIZE) == 0) {
1484 				char *p = optarg + MSG_ARG_ASLR_SIZE;
1485 				if (*p == '\0') {
1486 					ofl->ofl_aslr = 1;
1487 				} else if (*p == '=') {
1488 					p++;
1489 
1490 					if ((strcmp(p,
1491 					    MSG_ORIG(MSG_ARG_ENABLED)) == 0) ||
1492 					    (strcmp(p,
1493 					    MSG_ORIG(MSG_ARG_ENABLE)) == 0)) {
1494 						ofl->ofl_aslr = 1;
1495 					} else if ((strcmp(p,
1496 					    MSG_ORIG(MSG_ARG_DISABLED)) == 0) ||
1497 					    (strcmp(p,
1498 					    MSG_ORIG(MSG_ARG_DISABLE)) == 0)) {
1499 						ofl->ofl_aslr = -1;
1500 					} else {
1501 						ld_eprintf(ofl, ERR_FATAL,
1502 						    MSG_INTL(MSG_ARG_ILLEGAL),
1503 						    MSG_ORIG(MSG_ARG_ZASLR), p);
1504 						return (S_ERROR);
1505 					}
1506 				} else {
1507 					ld_eprintf(ofl, ERR_FATAL,
1508 					    MSG_INTL(MSG_ARG_ILLEGAL),
1509 					    MSG_ORIG(MSG_ARG_Z), optarg);
1510 					return (S_ERROR);
1511 				}
1512 			} else if ((strncmp(optarg, MSG_ORIG(MSG_ARG_GUIDE),
1513 			    MSG_ARG_GUIDE_SIZE) == 0) &&
1514 			    ((optarg[MSG_ARG_GUIDE_SIZE] == '=') ||
1515 			    (optarg[MSG_ARG_GUIDE_SIZE] == '\0'))) {
1516 				if (!guidance_parse(ofl, optarg))
1517 					return (S_ERROR);
1518 			} else if (strcmp(optarg,
1519 			    MSG_ORIG(MSG_ARG_FATWARN)) == 0) {
1520 				if (zfwflag  == SET_FALSE) {
1521 					ld_eprintf(ofl, ERR_WARNING_NF,
1522 					    MSG_INTL(MSG_ARG_MTONCE),
1523 					    MSG_ORIG(MSG_ARG_ZFATWNOFATW));
1524 				} else {
1525 					zfwflag = SET_TRUE;
1526 					ofl->ofl_flags |= FLG_OF_FATWARN;
1527 				}
1528 			} else if (strcmp(optarg,
1529 			    MSG_ORIG(MSG_ARG_NOFATWARN)) == 0) {
1530 				if (zfwflag  == SET_TRUE)
1531 					ld_eprintf(ofl, ERR_WARNING_NF,
1532 					    MSG_INTL(MSG_ARG_MTONCE),
1533 					    MSG_ORIG(MSG_ARG_ZFATWNOFATW));
1534 				else
1535 					zfwflag = SET_FALSE;
1536 
1537 			/*
1538 			 * Process everything related to -z assert-deflib. This
1539 			 * must be done in pass 1 because it gets used in pass
1540 			 * 2.
1541 			 */
1542 			} else if (strncmp(optarg, MSG_ORIG(MSG_ARG_ASSDEFLIB),
1543 			    MSG_ARG_ASSDEFLIB_SIZE) == 0) {
1544 				if (assdeflib_parse(ofl, optarg) != TRUE)
1545 					return (S_ERROR);
1546 
1547 			/*
1548 			 * Process new-style output type specification, which
1549 			 * we'll use in pass 2 and throughout.
1550 			 */
1551 			} else if (strncmp(optarg, MSG_ORIG(MSG_ARG_TYPE),
1552 			    MSG_ARG_TYPE_SIZE) == 0) {
1553 				char *p = optarg + MSG_ARG_TYPE_SIZE;
1554 				if (*p != '=') {
1555 					ld_eprintf(ofl, ERR_FATAL,
1556 					    MSG_INTL(MSG_ARG_ILLEGAL),
1557 					    MSG_ORIG(MSG_ARG_Z), optarg);
1558 					return (S_ERROR);
1559 				}
1560 
1561 				p++;
1562 				if (strcmp(p,
1563 				    MSG_ORIG(MSG_ARG_TYPE_RELOC)) == 0) {
1564 					otype = OT_RELOC;
1565 				} else if (strcmp(p,
1566 				    MSG_ORIG(MSG_ARG_TYPE_EXEC)) == 0) {
1567 					otype = OT_EXEC;
1568 				} else if (strcmp(p,
1569 				    MSG_ORIG(MSG_ARG_TYPE_SHARED)) == 0) {
1570 					otype = OT_SHARED;
1571 				} else if (strcmp(p,
1572 				    MSG_ORIG(MSG_ARG_TYPE_KMOD)) == 0) {
1573 					otype = OT_KMOD;
1574 				} else {
1575 					ld_eprintf(ofl, ERR_FATAL,
1576 					    MSG_INTL(MSG_ARG_ILLEGAL),
1577 					    MSG_ORIG(MSG_ARG_Z), optarg);
1578 					return (S_ERROR);
1579 				}
1580 			/*
1581 			 * The following options just need validation as they
1582 			 * are interpreted on the second pass through the
1583 			 * command line arguments.
1584 			 */
1585 			} else if (
1586 			    strncmp(optarg, MSG_ORIG(MSG_ARG_INITARRAY),
1587 			    MSG_ARG_INITARRAY_SIZE) &&
1588 			    strncmp(optarg, MSG_ORIG(MSG_ARG_FINIARRAY),
1589 			    MSG_ARG_FINIARRAY_SIZE) &&
1590 			    strncmp(optarg, MSG_ORIG(MSG_ARG_PREINITARRAY),
1591 			    MSG_ARG_PREINITARRAY_SIZE) &&
1592 			    strncmp(optarg, MSG_ORIG(MSG_ARG_RTLDINFO),
1593 			    MSG_ARG_RTLDINFO_SIZE) &&
1594 			    strncmp(optarg, MSG_ORIG(MSG_ARG_DTRACE),
1595 			    MSG_ARG_DTRACE_SIZE) &&
1596 			    strcmp(optarg, MSG_ORIG(MSG_ARG_ALLEXTRT)) &&
1597 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DFLEXTRT)) &&
1598 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DIRECT)) &&
1599 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NODIRECT)) &&
1600 			    strcmp(optarg, MSG_ORIG(MSG_ARG_GROUPPERM)) &&
1601 			    strcmp(optarg, MSG_ORIG(MSG_ARG_LAZYLOAD)) &&
1602 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NOGROUPPERM)) &&
1603 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NOLAZYLOAD)) &&
1604 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NODEFERRED)) &&
1605 			    strcmp(optarg, MSG_ORIG(MSG_ARG_RECORD)) &&
1606 			    strcmp(optarg, MSG_ORIG(MSG_ARG_ALTEXEC64)) &&
1607 			    strcmp(optarg, MSG_ORIG(MSG_ARG_WEAKEXT)) &&
1608 			    strncmp(optarg, MSG_ORIG(MSG_ARG_TARGET),
1609 			    MSG_ARG_TARGET_SIZE) &&
1610 			    strcmp(optarg, MSG_ORIG(MSG_ARG_RESCAN_NOW)) &&
1611 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DEFERRED))) {
1612 				ld_eprintf(ofl, ERR_FATAL,
1613 				    MSG_INTL(MSG_ARG_ILLEGAL),
1614 				    MSG_ORIG(MSG_ARG_Z), optarg);
1615 			}
1616 
1617 			break;
1618 
1619 		case 'D':
1620 			/*
1621 			 * If we have not yet read any input files go ahead
1622 			 * and process any debugging options (this allows any
1623 			 * argument processing, entrance criteria and library
1624 			 * initialization to be displayed).  Otherwise, if an
1625 			 * input file has been seen, skip interpretation until
1626 			 * process_files (this allows debugging to be turned
1627 			 * on and off around individual groups of files).
1628 			 */
1629 			Dflag = 1;
1630 			if (ofl->ofl_objscnt == 0) {
1631 				if (dbg_setup(ofl, optarg, 2) == 0)
1632 					return (S_ERROR);
1633 			}
1634 
1635 			/*
1636 			 * A diagnostic can only be provided after dbg_setup().
1637 			 * As this is the first diagnostic that can be produced
1638 			 * by ld(1), issue a title for timing and basic output.
1639 			 */
1640 			if ((optitle == 0) && DBG_ENABLED) {
1641 				optitle++;
1642 				DBG_CALL(Dbg_basic_options(ofl->ofl_lml));
1643 			}
1644 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1645 			break;
1646 
1647 		case 'B':
1648 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1649 			if (strcmp(optarg, MSG_ORIG(MSG_ARG_DIRECT)) == 0) {
1650 				if (Bdflag == SET_FALSE) {
1651 					ld_eprintf(ofl, ERR_FATAL,
1652 					    MSG_INTL(MSG_ARG_INCOMP),
1653 					    MSG_ORIG(MSG_ARG_BNODIRECT),
1654 					    MSG_ORIG(MSG_ARG_BDIRECT));
1655 				} else {
1656 					Bdflag = SET_TRUE;
1657 					ofl->ofl_guideflags |= FLG_OFG_NO_DB;
1658 				}
1659 			} else if (strcmp(optarg,
1660 			    MSG_ORIG(MSG_ARG_NODIRECT)) == 0) {
1661 				if (Bdflag == SET_TRUE) {
1662 					ld_eprintf(ofl, ERR_FATAL,
1663 					    MSG_INTL(MSG_ARG_INCOMP),
1664 					    MSG_ORIG(MSG_ARG_BDIRECT),
1665 					    MSG_ORIG(MSG_ARG_BNODIRECT));
1666 				} else {
1667 					Bdflag = SET_FALSE;
1668 					ofl->ofl_guideflags |= FLG_OFG_NO_DB;
1669 				}
1670 			} else if (strcmp(optarg,
1671 			    MSG_ORIG(MSG_STR_SYMBOLIC)) == 0)
1672 				Bsflag = TRUE;
1673 			else if (strcmp(optarg, MSG_ORIG(MSG_ARG_REDUCE)) == 0)
1674 				ofl->ofl_flags |= FLG_OF_PROCRED;
1675 			else if (strcmp(optarg, MSG_ORIG(MSG_STR_LOCAL)) == 0)
1676 				Blflag = TRUE;
1677 			else if (strcmp(optarg, MSG_ORIG(MSG_ARG_GROUP)) == 0)
1678 				Bgflag = TRUE;
1679 			else if (strcmp(optarg,
1680 			    MSG_ORIG(MSG_STR_ELIMINATE)) == 0)
1681 				Beflag = TRUE;
1682 			else if (strcmp(optarg,
1683 			    MSG_ORIG(MSG_ARG_TRANSLATOR)) == 0) {
1684 				ld_eprintf(ofl, ERR_WARNING,
1685 				    MSG_INTL(MSG_ARG_UNSUPPORTED),
1686 				    MSG_ORIG(MSG_ARG_BTRANSLATOR));
1687 			} else if (strcmp(optarg,
1688 			    MSG_ORIG(MSG_STR_LD_DYNAMIC)) &&
1689 			    strcmp(optarg, MSG_ORIG(MSG_ARG_STATIC))) {
1690 				ld_eprintf(ofl, ERR_FATAL,
1691 				    MSG_INTL(MSG_ARG_ILLEGAL),
1692 				    MSG_ORIG(MSG_ARG_CB), optarg);
1693 			}
1694 			break;
1695 
1696 		case 'G':
1697 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1698 			otype = OT_SHARED;
1699 			break;
1700 
1701 		case 'L':
1702 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1703 			break;
1704 
1705 		case 'M':
1706 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1707 			if (aplist_append(&(ofl->ofl_maps), optarg,
1708 			    AL_CNT_OFL_MAPFILES) == NULL)
1709 				return (S_ERROR);
1710 			break;
1711 
1712 		case 'N':
1713 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1714 			break;
1715 
1716 		case 'Q':
1717 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1718 			if ((optarg[0] == 'n') && (optarg[1] == '\0')) {
1719 				if (Qflag != SET_UNKNOWN)
1720 					ld_eprintf(ofl, ERR_WARNING_NF,
1721 					    MSG_INTL(MSG_ARG_MTONCE),
1722 					    MSG_ORIG(MSG_ARG_CQ));
1723 				else
1724 					Qflag = SET_FALSE;
1725 			} else if ((optarg[0] == 'y') && (optarg[1] == '\0')) {
1726 				if (Qflag != SET_UNKNOWN)
1727 					ld_eprintf(ofl, ERR_WARNING_NF,
1728 					    MSG_INTL(MSG_ARG_MTONCE),
1729 					    MSG_ORIG(MSG_ARG_CQ));
1730 				else
1731 					Qflag = SET_TRUE;
1732 			} else {
1733 				ld_eprintf(ofl, ERR_FATAL,
1734 				    MSG_INTL(MSG_ARG_ILLEGAL),
1735 				    MSG_ORIG(MSG_ARG_CQ), optarg);
1736 			}
1737 			break;
1738 
1739 		case 'S':
1740 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1741 			if (aplist_append(&lib_support, optarg,
1742 			    AL_CNT_SUPPORT) == NULL)
1743 				return (S_ERROR);
1744 			break;
1745 
1746 		case 'V':
1747 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1748 			if (!Vflag)
1749 				(void) fprintf(stderr, MSG_ORIG(MSG_STR_STRNL),
1750 				    ofl->ofl_sgsid);
1751 			Vflag = TRUE;
1752 			break;
1753 
1754 		case 'Y':
1755 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, optarg));
1756 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_LCOM), 2) == 0) {
1757 				if (Llibdir)
1758 					ld_eprintf(ofl, ERR_WARNING_NF,
1759 					    MSG_INTL(MSG_ARG_MTONCE),
1760 					    MSG_ORIG(MSG_ARG_CYL));
1761 				else
1762 					Llibdir = optarg + 2;
1763 			} else if (strncmp(optarg,
1764 			    MSG_ORIG(MSG_ARG_UCOM), 2) == 0) {
1765 				if (Ulibdir)
1766 					ld_eprintf(ofl, ERR_WARNING_NF,
1767 					    MSG_INTL(MSG_ARG_MTONCE),
1768 					    MSG_ORIG(MSG_ARG_CYU));
1769 				else
1770 					Ulibdir = optarg + 2;
1771 			} else if (strncmp(optarg,
1772 			    MSG_ORIG(MSG_ARG_PCOM), 2) == 0) {
1773 				if (Plibpath)
1774 					ld_eprintf(ofl, ERR_WARNING_NF,
1775 					    MSG_INTL(MSG_ARG_MTONCE),
1776 					    MSG_ORIG(MSG_ARG_CYP));
1777 				else
1778 					Plibpath = optarg + 2;
1779 			} else {
1780 				ld_eprintf(ofl, ERR_FATAL,
1781 				    MSG_INTL(MSG_ARG_ILLEGAL),
1782 				    MSG_ORIG(MSG_ARG_CY), optarg);
1783 			}
1784 			break;
1785 
1786 		case '?':
1787 			DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c, NULL));
1788 			/*
1789 			 * If the option character is '-', we're looking at a
1790 			 * long option which couldn't be translated, display a
1791 			 * more useful error.
1792 			 */
1793 			if (optopt == '-') {
1794 				eprintf(ofl->ofl_lml, ERR_FATAL,
1795 				    MSG_INTL(MSG_ARG_LONG_UNKNOWN),
1796 				    argv[optind-1]);
1797 			} else {
1798 				eprintf(ofl->ofl_lml, ERR_FATAL,
1799 				    MSG_INTL(MSG_ARG_UNKNOWN), optopt);
1800 			}
1801 			(*usage)++;
1802 			break;
1803 
1804 		default:
1805 			break;
1806 		}
1807 
1808 		/*
1809 		 * Update the argument index for the next getopt() iteration.
1810 		 */
1811 		ndx = optind;
1812 	}
1813 	return (1);
1814 }
1815 
1816 /*
1817  * Parsing options pass2 for
1818  */
1819 static uintptr_t
1820 parseopt_pass2(Ofl_desc *ofl, int argc, char **argv)
1821 {
1822 	int	c, ndx = optind;
1823 
1824 	while ((c = ld_getopt(ofl->ofl_lml, ndx, argc, argv)) != -1) {
1825 		Ifl_desc	*ifl;
1826 		Sym_desc	*sdp;
1827 
1828 		switch (c) {
1829 			case 'l':
1830 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1831 				    optarg));
1832 				if (ld_find_library(optarg, ofl) == S_ERROR)
1833 					return (S_ERROR);
1834 				break;
1835 			case 'B':
1836 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1837 				    optarg));
1838 				if (strcmp(optarg,
1839 				    MSG_ORIG(MSG_STR_LD_DYNAMIC)) == 0) {
1840 					if (ofl->ofl_flags & FLG_OF_DYNAMIC)
1841 						ofl->ofl_flags |=
1842 						    FLG_OF_DYNLIBS;
1843 					else {
1844 						ld_eprintf(ofl, ERR_FATAL,
1845 						    MSG_INTL(MSG_ARG_ST_INCOMP),
1846 						    MSG_ORIG(MSG_ARG_BDYNAMIC));
1847 					}
1848 				} else if (strcmp(optarg,
1849 				    MSG_ORIG(MSG_ARG_STATIC)) == 0)
1850 					ofl->ofl_flags &= ~FLG_OF_DYNLIBS;
1851 				break;
1852 			case 'L':
1853 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1854 				    optarg));
1855 				if (ld_add_libdir(ofl, optarg) == S_ERROR)
1856 					return (S_ERROR);
1857 				break;
1858 			case 'N':
1859 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1860 				    optarg));
1861 				/*
1862 				 * Record DT_NEEDED string
1863 				 */
1864 				if (!(ofl->ofl_flags & FLG_OF_DYNAMIC))
1865 					ld_eprintf(ofl, ERR_FATAL,
1866 					    MSG_INTL(MSG_ARG_ST_INCOMP),
1867 					    MSG_ORIG(MSG_ARG_CN));
1868 				if (((ifl = libld_calloc(1,
1869 				    sizeof (Ifl_desc))) == NULL) ||
1870 				    (aplist_append(&ofl->ofl_sos, ifl,
1871 				    AL_CNT_OFL_LIBS) == NULL))
1872 					return (S_ERROR);
1873 
1874 				ifl->ifl_name = MSG_INTL(MSG_STR_COMMAND);
1875 				ifl->ifl_soname = optarg;
1876 				ifl->ifl_flags = (FLG_IF_NEEDSTR |
1877 				    FLG_IF_FILEREF | FLG_IF_DEPREQD);
1878 
1879 				break;
1880 			case 'D':
1881 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1882 				    optarg));
1883 				(void) dbg_setup(ofl, optarg, 3);
1884 				break;
1885 			case 'u':
1886 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1887 				    optarg));
1888 				if (ld_sym_add_u(optarg, ofl,
1889 				    MSG_STR_COMMAND) == (Sym_desc *)S_ERROR)
1890 					return (S_ERROR);
1891 				break;
1892 			case 'z':
1893 				DBG_CALL(Dbg_args_option(ofl->ofl_lml, ndx, c,
1894 				    optarg));
1895 				if ((strncmp(optarg, MSG_ORIG(MSG_ARG_LD32),
1896 				    MSG_ARG_LD32_SIZE) == 0) ||
1897 				    (strncmp(optarg, MSG_ORIG(MSG_ARG_LD64),
1898 				    MSG_ARG_LD64_SIZE) == 0)) {
1899 					if (createargv(ofl, 0) == S_ERROR)
1900 						return (S_ERROR);
1901 				} else if (strcmp(optarg,
1902 				    MSG_ORIG(MSG_ARG_ALLEXTRT)) == 0) {
1903 					ofl->ofl_flags1 |= FLG_OF1_ALLEXRT;
1904 					ofl->ofl_flags1 &= ~FLG_OF1_WEAKEXT;
1905 				} else if (strcmp(optarg,
1906 				    MSG_ORIG(MSG_ARG_WEAKEXT)) == 0) {
1907 					ofl->ofl_flags1 |= FLG_OF1_WEAKEXT;
1908 					ofl->ofl_flags1 &= ~FLG_OF1_ALLEXRT;
1909 				} else if (strcmp(optarg,
1910 				    MSG_ORIG(MSG_ARG_DFLEXTRT)) == 0) {
1911 					ofl->ofl_flags1 &=
1912 					    ~(FLG_OF1_ALLEXRT |
1913 					    FLG_OF1_WEAKEXT);
1914 				} else if (strcmp(optarg,
1915 				    MSG_ORIG(MSG_ARG_DIRECT)) == 0) {
1916 					ofl->ofl_flags1 |= FLG_OF1_ZDIRECT;
1917 					ofl->ofl_guideflags |= FLG_OFG_NO_DB;
1918 				} else if (strcmp(optarg,
1919 				    MSG_ORIG(MSG_ARG_NODIRECT)) == 0) {
1920 					ofl->ofl_flags1 &= ~FLG_OF1_ZDIRECT;
1921 					ofl->ofl_guideflags |= FLG_OFG_NO_DB;
1922 				} else if (strcmp(optarg,
1923 				    MSG_ORIG(MSG_ARG_IGNORE)) == 0) {
1924 					ofl->ofl_flags1 |= FLG_OF1_IGNORE;
1925 				} else if (strcmp(optarg,
1926 				    MSG_ORIG(MSG_ARG_RECORD)) == 0) {
1927 					ofl->ofl_flags1 &= ~FLG_OF1_IGNORE;
1928 				} else if (strcmp(optarg,
1929 				    MSG_ORIG(MSG_ARG_LAZYLOAD)) == 0) {
1930 					ofl->ofl_flags1 |= FLG_OF1_LAZYLD;
1931 					ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
1932 				} else if (strcmp(optarg,
1933 				    MSG_ORIG(MSG_ARG_NOLAZYLOAD)) == 0) {
1934 					ofl->ofl_flags1 &= ~ FLG_OF1_LAZYLD;
1935 					ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
1936 				} else if (strcmp(optarg,
1937 				    MSG_ORIG(MSG_ARG_GROUPPERM)) == 0) {
1938 					ofl->ofl_flags1 |= FLG_OF1_GRPPRM;
1939 				} else if (strcmp(optarg,
1940 				    MSG_ORIG(MSG_ARG_NOGROUPPERM)) == 0) {
1941 					ofl->ofl_flags1 &= ~FLG_OF1_GRPPRM;
1942 				} else if (strncmp(optarg,
1943 				    MSG_ORIG(MSG_ARG_INITARRAY),
1944 				    MSG_ARG_INITARRAY_SIZE) == 0) {
1945 					if (((sdp = ld_sym_add_u(optarg +
1946 					    MSG_ARG_INITARRAY_SIZE, ofl,
1947 					    MSG_STR_COMMAND)) ==
1948 					    (Sym_desc *)S_ERROR) ||
1949 					    (aplist_append(&ofl->ofl_initarray,
1950 					    sdp, AL_CNT_OFL_ARRAYS) == NULL))
1951 						return (S_ERROR);
1952 				} else if (strncmp(optarg,
1953 				    MSG_ORIG(MSG_ARG_FINIARRAY),
1954 				    MSG_ARG_FINIARRAY_SIZE) == 0) {
1955 					if (((sdp = ld_sym_add_u(optarg +
1956 					    MSG_ARG_FINIARRAY_SIZE, ofl,
1957 					    MSG_STR_COMMAND)) ==
1958 					    (Sym_desc *)S_ERROR) ||
1959 					    (aplist_append(&ofl->ofl_finiarray,
1960 					    sdp, AL_CNT_OFL_ARRAYS) == NULL))
1961 						return (S_ERROR);
1962 				} else if (strncmp(optarg,
1963 				    MSG_ORIG(MSG_ARG_PREINITARRAY),
1964 				    MSG_ARG_PREINITARRAY_SIZE) == 0) {
1965 					if (((sdp = ld_sym_add_u(optarg +
1966 					    MSG_ARG_PREINITARRAY_SIZE, ofl,
1967 					    MSG_STR_COMMAND)) ==
1968 					    (Sym_desc *)S_ERROR) ||
1969 					    (aplist_append(&ofl->ofl_preiarray,
1970 					    sdp, AL_CNT_OFL_ARRAYS) == NULL))
1971 						return (S_ERROR);
1972 				} else if (strncmp(optarg,
1973 				    MSG_ORIG(MSG_ARG_RTLDINFO),
1974 				    MSG_ARG_RTLDINFO_SIZE) == 0) {
1975 					if (((sdp = ld_sym_add_u(optarg +
1976 					    MSG_ARG_RTLDINFO_SIZE, ofl,
1977 					    MSG_STR_COMMAND)) ==
1978 					    (Sym_desc *)S_ERROR) ||
1979 					    (aplist_append(&ofl->ofl_rtldinfo,
1980 					    sdp, AL_CNT_OFL_ARRAYS) == NULL))
1981 						return (S_ERROR);
1982 				} else if (strncmp(optarg,
1983 				    MSG_ORIG(MSG_ARG_DTRACE),
1984 				    MSG_ARG_DTRACE_SIZE) == 0) {
1985 					if ((sdp = ld_sym_add_u(optarg +
1986 					    MSG_ARG_DTRACE_SIZE, ofl,
1987 					    MSG_STR_COMMAND)) ==
1988 					    (Sym_desc *)S_ERROR)
1989 						return (S_ERROR);
1990 					ofl->ofl_dtracesym = sdp;
1991 				} else if (strcmp(optarg,
1992 				    MSG_ORIG(MSG_ARG_RESCAN_NOW)) == 0) {
1993 					if (ld_rescan_archives(ofl, 0, ndx) ==
1994 					    S_ERROR)
1995 						return (S_ERROR);
1996 				} else if (strcmp(optarg,
1997 				    MSG_ORIG(MSG_ARG_RESCAN_START)) == 0) {
1998 					ofl->ofl_ars_gsndx = ofl->ofl_arscnt;
1999 					ofl->ofl_ars_gsandx = ndx;
2000 				} else if (strcmp(optarg,
2001 				    MSG_ORIG(MSG_ARG_RESCAN_END)) == 0) {
2002 					if (ld_rescan_archives(ofl, 1, ndx) ==
2003 					    S_ERROR)
2004 						return (S_ERROR);
2005 				} else if (strcmp(optarg,
2006 				    MSG_ORIG(MSG_ARG_DEFERRED)) == 0) {
2007 					ofl->ofl_flags1 |= FLG_OF1_DEFERRED;
2008 				} else if (strcmp(optarg,
2009 				    MSG_ORIG(MSG_ARG_NODEFERRED)) == 0) {
2010 					ofl->ofl_flags1 &= ~FLG_OF1_DEFERRED;
2011 				}
2012 			default:
2013 				break;
2014 		}
2015 
2016 		/*
2017 		 * Update the argument index for the next getopt() iteration.
2018 		 */
2019 		ndx = optind;
2020 	}
2021 	return (1);
2022 }
2023 
2024 /*
2025  *
2026  * Pass 1 -- process_flags: collects all options and sets flags
2027  */
2028 static uintptr_t
2029 process_flags_com(Ofl_desc *ofl, int argc, char **argv, int *usage)
2030 {
2031 	for (; optind < argc; optind++) {
2032 		/*
2033 		 * If we detect some more options return to getopt().
2034 		 * Checking argv[optind][1] against null prevents a forever
2035 		 * loop if an unadorned `-' argument is passed to us.
2036 		 */
2037 		while ((optind < argc) && (argv[optind][0] == '-')) {
2038 			if (argv[optind][1] != '\0') {
2039 				if (parseopt_pass1(ofl, argc, argv,
2040 				    usage) == S_ERROR)
2041 					return (S_ERROR);
2042 			} else if (++optind < argc)
2043 				continue;
2044 		}
2045 		if (optind >= argc)
2046 			break;
2047 		ofl->ofl_objscnt++;
2048 	}
2049 
2050 	/* Did an unterminated archive group run off the end? */
2051 	if (ofl->ofl_ars_gsandx > 0) {
2052 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_AR_GRP_BAD),
2053 		    MSG_INTL(MSG_MARG_AR_GRP_START),
2054 		    MSG_INTL(MSG_MARG_AR_GRP_END));
2055 		return (S_ERROR);
2056 	}
2057 
2058 	return (1);
2059 }
2060 
2061 uintptr_t
2062 ld_process_flags(Ofl_desc *ofl, int argc, char **argv)
2063 {
2064 	int	usage = 0;	/* Collect all argument errors before exit */
2065 
2066 	if (argc < 2) {
2067 		usage_mesg(FALSE);
2068 		return (S_ERROR);
2069 	}
2070 
2071 	/*
2072 	 * Option handling
2073 	 */
2074 	opterr = 0;
2075 	optind = 1;
2076 	if (process_flags_com(ofl, argc, argv, &usage) == S_ERROR)
2077 		return (S_ERROR);
2078 
2079 	/*
2080 	 * Having parsed everything, did we have any usage errors.
2081 	 */
2082 	if (usage) {
2083 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_USEHELP));
2084 		return (S_ERROR);
2085 	}
2086 
2087 	return (check_flags(ofl, argc));
2088 }
2089 
2090 /*
2091  * Pass 2 -- process_files: skips the flags collected in pass 1 and processes
2092  * files.
2093  */
2094 static uintptr_t
2095 process_files_com(Ofl_desc *ofl, int argc, char **argv)
2096 {
2097 	for (; optind < argc; optind++) {
2098 		int		fd;
2099 		uintptr_t	open_ret;
2100 		char		*path;
2101 		Rej_desc	rej = { 0 };
2102 
2103 		/*
2104 		 * If we detect some more options return to getopt().
2105 		 * Checking argv[optind][1] against null prevents a forever
2106 		 * loop if an unadorned `-' argument is passed to us.
2107 		 */
2108 		while ((optind < argc) && (argv[optind][0] == '-')) {
2109 			if (argv[optind][1] != '\0') {
2110 				if (parseopt_pass2(ofl, argc, argv) == S_ERROR)
2111 					return (S_ERROR);
2112 			} else if (++optind < argc)
2113 				continue;
2114 		}
2115 		if (optind >= argc)
2116 			break;
2117 
2118 		path = argv[optind];
2119 		if ((fd = open(path, O_RDONLY)) == -1) {
2120 			int err = errno;
2121 
2122 			ld_eprintf(ofl, ERR_FATAL,
2123 			    MSG_INTL(MSG_SYS_OPEN), path, strerror(err));
2124 			continue;
2125 		}
2126 
2127 		DBG_CALL(Dbg_args_file(ofl->ofl_lml, optind, path));
2128 
2129 		open_ret = ld_process_open(path, path, &fd, ofl,
2130 		    (FLG_IF_CMDLINE | FLG_IF_NEEDED), &rej, NULL);
2131 		if (fd != -1)
2132 			(void) close(fd);
2133 		if (open_ret == S_ERROR)
2134 			return (S_ERROR);
2135 
2136 		/*
2137 		 * Check for mismatched input.
2138 		 */
2139 		if (rej.rej_type) {
2140 			Conv_reject_desc_buf_t rej_buf;
2141 
2142 			ld_eprintf(ofl, ERR_FATAL,
2143 			    MSG_INTL(reject[rej.rej_type]),
2144 			    rej.rej_name ? rej.rej_name :
2145 			    MSG_INTL(MSG_STR_UNKNOWN),
2146 			    conv_reject_desc(&rej, &rej_buf,
2147 			    ld_targ.t_m.m_mach));
2148 			return (1);
2149 		}
2150 	}
2151 	return (1);
2152 }
2153 
2154 uintptr_t
2155 ld_process_files(Ofl_desc *ofl, int argc, char **argv)
2156 {
2157 	DBG_CALL(Dbg_basic_files(ofl->ofl_lml));
2158 
2159 	/*
2160 	 * Process command line files (taking into account any applicable
2161 	 * preceding flags).  Return if any fatal errors have occurred.
2162 	 */
2163 	opterr = 0;
2164 	optind = 1;
2165 	if (process_files_com(ofl, argc, argv) == S_ERROR)
2166 		return (S_ERROR);
2167 	if (ofl->ofl_flags & FLG_OF_FATAL)
2168 		return (1);
2169 
2170 	/*
2171 	 * Guidance: Use -B direct/nodirect or -z direct/nodirect.
2172 	 *
2173 	 * This is a backstop for the case where the link had no dependencies.
2174 	 * Otherwise, it will get caught by ld_process_ifl(). We need both,
2175 	 * because -z direct is positional, and its value at the time where
2176 	 * the first dependency is seen might be different than it is now.
2177 	 */
2178 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) &&
2179 	    OFL_GUIDANCE(ofl, FLG_OFG_NO_DB)) {
2180 		ld_eprintf(ofl, ERR_GUIDANCE, MSG_INTL(MSG_GUIDE_DIRECT));
2181 		ofl->ofl_guideflags |= FLG_OFG_NO_DB;
2182 	}
2183 
2184 	/*
2185 	 * Now that all command line files have been processed see if there are
2186 	 * any additional `needed' shared object dependencies.
2187 	 */
2188 	if (ofl->ofl_soneed)
2189 		if (ld_finish_libs(ofl) == S_ERROR)
2190 			return (S_ERROR);
2191 
2192 	/*
2193 	 * If rescanning archives is enabled, do so now to determine whether
2194 	 * there might still be members extracted to satisfy references from any
2195 	 * explicit objects.  Continue until no new objects are extracted.  Note
2196 	 * that this pass is carried out *after* processing any implicit objects
2197 	 * (above) as they may already have resolved any undefined references
2198 	 * from any explicit dependencies.
2199 	 */
2200 	if (ofl->ofl_flags1 & FLG_OF1_RESCAN) {
2201 		if (ld_rescan_archives(ofl, 0, argc) == S_ERROR)
2202 			return (S_ERROR);
2203 		if (ofl->ofl_flags & FLG_OF_FATAL)
2204 			return (1);
2205 	}
2206 
2207 	/*
2208 	 * If debugging, provide statistics on each archives extraction, or flag
2209 	 * any archive that has provided no members.  Note that this could be a
2210 	 * nice place to free up much of the archive infrastructure, as we've
2211 	 * extracted any members we need.  However, as we presently don't free
2212 	 * anything under ld(1) there's not much point in proceeding further.
2213 	 */
2214 	DBG_CALL(Dbg_statistics_ar(ofl));
2215 
2216 	/*
2217 	 * If any version definitions have been established, either via input
2218 	 * from a mapfile or from the input relocatable objects, make sure any
2219 	 * version dependencies are satisfied, and version symbols created.
2220 	 */
2221 	if (ofl->ofl_verdesc)
2222 		if (ld_vers_check_defs(ofl) == S_ERROR)
2223 			return (S_ERROR);
2224 
2225 	/*
2226 	 * If input section ordering was specified within some segment
2227 	 * using a mapfile, verify that the expected sections were seen.
2228 	 */
2229 	if (ofl->ofl_flags & FLG_OF_IS_ORDER)
2230 		ld_ent_check(ofl);
2231 
2232 	return (1);
2233 }
2234 
2235 uintptr_t
2236 ld_init_strings(Ofl_desc *ofl)
2237 {
2238 	uint_t	stflags;
2239 
2240 	if (ofl->ofl_flags1 & FLG_OF1_NCSTTAB)
2241 		stflags = 0;
2242 	else
2243 		stflags = FLG_STNEW_COMPRESS;
2244 
2245 	if (((ofl->ofl_shdrsttab = st_new(stflags)) == NULL) ||
2246 	    ((ofl->ofl_strtab = st_new(stflags)) == NULL) ||
2247 	    ((ofl->ofl_dynstrtab = st_new(stflags)) == NULL))
2248 		return (S_ERROR);
2249 
2250 	return (0);
2251 }
2252