xref: /illumos-gate/usr/src/cmd/sgs/libld/common/args.c (revision 3d393ee6c37fa10ac512ed6d36109ad616dc7c1a)
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 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*
31  * Publicly available flags are defined in ld(1).   The following flags are
32  * private, and may be removed at any time.
33  *
34  *    OPTION			MEANING
35  *
36  *    -z dtrace=symbol		assigns symbol to PT_SUNWDTRACE segment,
37  *    				providing scratch area for dtrace processing.
38  *
39  *    -z noreloc		suppress relocation processing.  This provides
40  *				a mechanism for validating kernel module symbol
41  *				resolution that would normally incur fatal
42  *				relocation errors.
43  *
44  *    -z rtldinfo=symbol	assigns symbol to SUNW_RTLDINF dynamic tag,
45  *				providing pre-initialization specific routines
46  *				for TLS initialization.
47  *
48  *    -z nointerp		suppress the addition of an interpreter
49  *				section.  This is used to generate the kernel,
50  *				but makes no sense to be used by anyone else.
51  *
52  *    -z norelaxreloc		suppress the automatic addition of relaxed
53  *				relocations to GNU linkonce/COMDAT sections.
54  */
55 #include	<sys/link.h>
56 #include	<stdio.h>
57 #include	<fcntl.h>
58 #include	<string.h>
59 #include	<errno.h>
60 #include	<elf.h>
61 #include	<unistd.h>
62 #include	<debug.h>
63 #include	"msg.h"
64 #include	"_libld.h"
65 
66 /*
67  * Define a set of local argument flags, the settings of these will be
68  * verified in check_flags() and lead to the appropriate output file flags
69  * being initialized.
70  */
71 typedef	enum {
72 	SET_UNKNOWN = -1,
73 	SET_FALSE = 0,
74 	SET_TRUE = 1
75 } Setstate;
76 
77 static Setstate	dflag	= SET_UNKNOWN;
78 static Setstate	zdflag	= SET_UNKNOWN;
79 static Setstate	Qflag	= SET_UNKNOWN;
80 static Setstate	Bdflag	= SET_UNKNOWN;
81 
82 static Boolean	aflag	= FALSE;
83 static Boolean	bflag	= FALSE;
84 static Boolean	rflag	= FALSE;
85 static Boolean	sflag	= FALSE;
86 static Boolean	zinflag = FALSE;
87 static Boolean	zlflag	= FALSE;
88 static Boolean	Bgflag	= FALSE;
89 static Boolean	Blflag	= FALSE;
90 static Boolean	Beflag	= FALSE;
91 static Boolean	Bsflag	= FALSE;
92 static Boolean	Btflag	= FALSE;
93 static Boolean	Gflag	= FALSE;
94 static Boolean	Vflag	= FALSE;
95 
96 /*
97  * ztflag's state is set by pointing it to the matching string:
98  *	text | textoff | textwarn
99  */
100 static const char	*ztflag = 0;
101 
102 static uintptr_t process_files_com(Ofl_desc *, int, char **);
103 static uintptr_t process_flags_com(Ofl_desc *, int, char **, int *);
104 
105 /*
106  * Print usage message to stderr - 2 modes, summary message only,
107  * and full usage message.
108  */
109 static void
110 usage_mesg(Boolean detail)
111 {
112 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE),
113 	    MSG_ORIG(MSG_STR_OPTIONS));
114 
115 	if (detail == FALSE)
116 		return;
117 
118 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_6));
119 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_A));
120 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_B));
121 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBDR));
122 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBDY));
123 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBE));
124 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBG));
125 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBL));
126 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBR));
127 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CBS));
128 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_C));
129 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CC));
130 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_D));
131 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CD));
132 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_E));
133 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_F));
134 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CF));
135 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CG));
136 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_H));
137 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_I));
138 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CI));
139 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_L));
140 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CL));
141 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_M));
142 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CM));
143 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CN));
144 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_O));
145 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_P));
146 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CP));
147 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CQ));
148 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_R));
149 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CR));
150 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_S));
151 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CS));
152 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_T));
153 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_U));
154 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CV));
155 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_CY));
156 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZA));
157 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZAE));
158 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZAL));
159 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZC));
160 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZDFS));
161 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZDRS));
162 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZE));
163 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZFA));
164 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZGP));
165 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZH));
166 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZIG));
167 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZINA));
168 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZINI));
169 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZINT));
170 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZLAZY));
171 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZLD32));
172 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZLD64));
173 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZLO));
174 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZM));
175 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNC));
176 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDFS));
177 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDEF));
178 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDEL));
179 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDLO));
180 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNDU));
181 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNLD));
182 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNOW));
183 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNPA));
184 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZNV));
185 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZO));
186 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZPIA));
187 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRL));
188 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRREL));
189 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRS));
190 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRSN));
191 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZRSGRP));
192 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZTARG));
193 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZT));
194 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZTO));
195 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZTW));
196 	(void) fprintf(stderr, MSG_INTL(MSG_ARG_DETAIL_ZV));
197 }
198 
199 /*
200  * Rescan the archives seen on the command line in order
201  * to handle circularly dependent archives, stopping when
202  * no further member extraction occurs.
203  *
204  * entry:
205  *	ofl - Output file descriptor
206  *	isgrp - True if this is a an archive group search, False
207  *		to search starting with argv[1] through end_arg_ndx
208  *	end_arg_ndx - Index of final argv element to consider.
209  */
210 static uintptr_t
211 ld_rescan_archives(Ofl_desc *ofl, int isgrp, int end_arg_ndx)
212 {
213 	ofl->ofl_flags1 |= FLG_OF1_EXTRACT;
214 
215 	while (ofl->ofl_flags1 & FLG_OF1_EXTRACT) {
216 		Listnode	*lnp;
217 		Ar_desc		*adp;
218 		Word		start_ndx = isgrp ? ofl->ofl_ars_gsndx : 0;
219 		Word		ndx = 0;
220 
221 		ofl->ofl_flags1 &= ~FLG_OF1_EXTRACT;
222 
223 		DBG_CALL(Dbg_file_ar_rescan(ofl->ofl_lml,
224 		    isgrp ? ofl->ofl_ars_gsandx : 1, end_arg_ndx));
225 
226 		for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) {
227 			/* If not to starting index yet, skip it */
228 			if (ndx++ < start_ndx)
229 				continue;
230 
231 			/*
232 			 * If this archive was processed with -z allextract,
233 			 * then all members have already been extracted.
234 			 */
235 			if (adp->ad_elf == NULL)
236 				continue;
237 
238 			/*
239 			 * Reestablish any archive specific command line flags.
240 			 */
241 			ofl->ofl_flags1 &= ~MSK_OF1_ARCHIVE;
242 			ofl->ofl_flags1 |= (adp->ad_flags & MSK_OF1_ARCHIVE);
243 
244 			/*
245 			 * Re-process the archive.  Note that a file descriptor
246 			 * is unnecessary, as the file is already available in
247 			 * memory.
248 			 */
249 			if (ld_process_archive(adp->ad_name, -1,
250 			    adp, ofl) == S_ERROR)
251 				return (S_ERROR);
252 			if (ofl->ofl_flags & FLG_OF_FATAL)
253 				return (1);
254 		}
255 	}
256 
257 	return (1);
258 }
259 
260 /*
261  * Checks the command line option flags for consistency.
262  */
263 static uintptr_t
264 check_flags(Ofl_desc * ofl, int argc)
265 {
266 	if (Plibpath && (Llibdir || Ulibdir)) {
267 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_YP),
268 		    Llibdir ? 'L' : 'U');
269 		ofl->ofl_flags |= FLG_OF_FATAL;
270 	}
271 
272 	if (rflag) {
273 		if (dflag == SET_UNKNOWN)
274 			dflag = SET_FALSE;
275 		if (ofl->ofl_flags & FLG_OF_COMREL) {
276 			/*
277 			 * Combining relocations when building a relocatable
278 			 * object isn't allowed.  Warn the user, but proceed.
279 			 */
280 			eprintf(ofl->ofl_lml, ERR_WARNING,
281 			    MSG_INTL(MSG_MARG_INCOMP), MSG_INTL(MSG_MARG_REL),
282 			    MSG_ORIG(MSG_ARG_ZCOMBRELOC));
283 			ofl->ofl_flags &= ~FLG_OF_COMREL;
284 		}
285 		ofl->ofl_flags |= FLG_OF_RELOBJ;
286 	} else {
287 		/*
288 		 * If the user hasn't explicitly requested that relocations
289 		 * not be combined, combine them by default.
290 		 */
291 		if ((ofl->ofl_flags & FLG_OF_NOCOMREL) == 0)
292 			ofl->ofl_flags |= FLG_OF_COMREL;
293 	}
294 
295 	if (zdflag == SET_TRUE)
296 		ofl->ofl_flags |= FLG_OF_NOUNDEF;
297 
298 	if (zinflag)
299 		ofl->ofl_dtflags_1 |= DF_1_INTERPOSE;
300 
301 	if (sflag)
302 		ofl->ofl_flags |= FLG_OF_STRIP;
303 
304 	if (Qflag == SET_TRUE)
305 		ofl->ofl_flags |= FLG_OF_ADDVERS;
306 
307 	if (Blflag)
308 		ofl->ofl_flags |= FLG_OF_AUTOLCL;
309 
310 	if (Beflag)
311 		ofl->ofl_flags |= FLG_OF_AUTOELM;
312 
313 	if (Blflag && Beflag) {
314 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_INCOMP),
315 		    MSG_ORIG(MSG_ARG_BELIMINATE), MSG_ORIG(MSG_ARG_BLOCAL));
316 		ofl->ofl_flags |= FLG_OF_FATAL;
317 	}
318 
319 	if (ofl->ofl_interp && (ofl->ofl_flags1 & FLG_OF1_NOINTRP)) {
320 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_INCOMP),
321 		    MSG_ORIG(MSG_ARG_CI), MSG_ORIG(MSG_ARG_ZNOINTERP));
322 		ofl->ofl_flags |= FLG_OF_FATAL;
323 	}
324 
325 	if ((ofl->ofl_flags1 & (FLG_OF1_NRLXREL | FLG_OF1_RLXREL)) ==
326 	    (FLG_OF1_NRLXREL | FLG_OF1_RLXREL)) {
327 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_INCOMP),
328 		    MSG_ORIG(MSG_ARG_ZRELAXRELOC),
329 		    MSG_ORIG(MSG_ARG_ZNORELAXRELOC));
330 		ofl->ofl_flags |= FLG_OF_FATAL;
331 	}
332 
333 	if (dflag != SET_FALSE) {
334 		/*
335 		 * Set -Bdynamic on by default, setting is rechecked as input
336 		 * files are processed.
337 		 */
338 		ofl->ofl_flags |=
339 		    (FLG_OF_DYNAMIC | FLG_OF_DYNLIBS | FLG_OF_PROCRED);
340 
341 		if (aflag) {
342 			eprintf(ofl->ofl_lml, ERR_FATAL,
343 			    MSG_INTL(MSG_ARG_INCOMP), MSG_ORIG(MSG_ARG_DY),
344 			    MSG_ORIG(MSG_ARG_A));
345 			ofl->ofl_flags |= FLG_OF_FATAL;
346 		}
347 
348 		if (bflag)
349 			ofl->ofl_flags |= FLG_OF_BFLAG;
350 
351 		if (Bgflag == TRUE) {
352 			if (zdflag == SET_FALSE) {
353 				eprintf(ofl->ofl_lml, ERR_FATAL,
354 				    MSG_INTL(MSG_ARG_INCOMP),
355 				    MSG_ORIG(MSG_ARG_BGROUP),
356 				    MSG_ORIG(MSG_ARG_ZNODEF));
357 				ofl->ofl_flags |= FLG_OF_FATAL;
358 			}
359 			ofl->ofl_dtflags_1 |= DF_1_GROUP;
360 			ofl->ofl_flags |= FLG_OF_NOUNDEF;
361 		}
362 
363 		/*
364 		 * If the use of default library searching has been suppressed
365 		 * but no runpaths have been provided we're going to have a hard
366 		 * job running this object.
367 		 */
368 		if ((ofl->ofl_dtflags_1 & DF_1_NODEFLIB) && !ofl->ofl_rpath)
369 			eprintf(ofl->ofl_lml, ERR_WARNING,
370 			    MSG_INTL(MSG_ARG_NODEFLIB),
371 			    MSG_INTL(MSG_MARG_RPATH));
372 
373 		/*
374 		 * By default, text relocation warnings are given when building
375 		 * an executable unless the -b flag is specified.  This option
376 		 * implies that unclean text can be created, so no warnings are
377 		 * generated unless specifically asked for.
378 		 */
379 		if ((ztflag == MSG_ORIG(MSG_ARG_ZTEXTOFF)) ||
380 		    ((ztflag == 0) && bflag))
381 			ofl->ofl_flags1 |= FLG_OF1_TEXTOFF;
382 		else if (ztflag == MSG_ORIG(MSG_ARG_ZTEXT))
383 			ofl->ofl_flags |= FLG_OF_PURETXT;
384 
385 		if (Gflag || !rflag) {
386 			/*
387 			 * Create a dynamic object.  -Bdirect indicates that all
388 			 * references should be bound directly.  This also
389 			 * enables lazyloading.  Individual symbols can be
390 			 * bound directly (or not) using mapfiles and the
391 			 * DIRECT (NODIRECT) qualifier.  With this capability,
392 			 * each syminfo entry is tagged SYMINFO_FLG_DIRECTBIND.
393 			 * Prior to this per-symbol direct binding, runtime
394 			 * direct binding was controlled via the DF_1_DIRECT
395 			 * flag.  This flag affected all references from the
396 			 * object.  -Bdirect continues to set this flag, and
397 			 * thus provides a means of taking a newly built
398 			 * direct binding object back to older systems.
399 			 *
400 			 * NOTE, any use of per-symbol NODIRECT bindings, or
401 			 * -znodirect, will disable the creation of the
402 			 * DF_1_DIRECT flag.  Older runtime linkers do not
403 			 * have the capability to do per-symbol direct bindings.
404 			 */
405 			if (Bdflag == SET_TRUE) {
406 				ofl->ofl_dtflags_1 |= DF_1_DIRECT;
407 				ofl->ofl_flags1 |= FLG_OF1_LAZYLD;
408 				ofl->ofl_flags |= FLG_OF_SYMINFO;
409 			}
410 
411 			/*
412 			 * -Bnodirect disables directly binding to any symbols
413 			 * exported from the object being created.  Individual
414 			 * references to external objects can still be affected
415 			 * by -zdirect or mapfile DIRECT directives.
416 			 */
417 			if (Bdflag == SET_FALSE) {
418 				ofl->ofl_flags1 |=
419 				    (FLG_OF1_NDIRECT | FLG_OF1_ALNODIR);
420 				ofl->ofl_flags |= FLG_OF_SYMINFO;
421 			}
422 		}
423 
424 		if (!Gflag && !rflag) {
425 			/*
426 			 * Dynamically linked executable.
427 			 */
428 			ofl->ofl_flags |= FLG_OF_EXEC;
429 
430 			if (zdflag != SET_FALSE)
431 				ofl->ofl_flags |= FLG_OF_NOUNDEF;
432 
433 			if (Bsflag) {
434 				eprintf(ofl->ofl_lml, ERR_FATAL,
435 				    MSG_INTL(MSG_ARG_DY_INCOMP),
436 				    MSG_ORIG(MSG_ARG_BSYMBOLIC));
437 				ofl->ofl_flags |= FLG_OF_FATAL;
438 			}
439 			if (ofl->ofl_soname) {
440 				eprintf(ofl->ofl_lml, ERR_FATAL,
441 				    MSG_INTL(MSG_MARG_DY_INCOMP),
442 				    MSG_INTL(MSG_MARG_SONAME));
443 				ofl->ofl_flags |= FLG_OF_FATAL;
444 			}
445 			if (Btflag) {
446 				eprintf(ofl->ofl_lml, ERR_FATAL,
447 				    MSG_INTL(MSG_ARG_DY_INCOMP),
448 				    MSG_ORIG(MSG_ARG_BTRANS));
449 				ofl->ofl_flags |= FLG_OF_FATAL;
450 			}
451 			if (ofl->ofl_filtees) {
452 				if (ofl->ofl_flags & FLG_OF_AUX) {
453 					eprintf(ofl->ofl_lml, ERR_FATAL,
454 					    MSG_INTL(MSG_MARG_DY_INCOMP),
455 					    MSG_INTL(MSG_MARG_FILTER_AUX));
456 				} else {
457 					eprintf(ofl->ofl_lml, ERR_FATAL,
458 					    MSG_INTL(MSG_MARG_DY_INCOMP),
459 					    MSG_INTL(MSG_MARG_FILTER));
460 				}
461 				ofl->ofl_flags |= FLG_OF_FATAL;
462 			}
463 
464 		} else if (!rflag) {
465 			/*
466 			 * Shared library.
467 			 */
468 			ofl->ofl_flags |= FLG_OF_SHAROBJ;
469 
470 			/*
471 			 * By default, print text relocation errors for
472 			 * executables but *not* for shared objects.
473 			 */
474 			if (ztflag == 0)
475 				ofl->ofl_flags1 |= FLG_OF1_TEXTOFF;
476 
477 			if (Bsflag) {
478 				/*
479 				 * -Bsymbolic, and -Bnodirect make no sense.
480 				 */
481 				if (Bdflag == SET_FALSE) {
482 					eprintf(ofl->ofl_lml, ERR_FATAL,
483 					    MSG_INTL(MSG_ARG_INCOMP),
484 					    MSG_ORIG(MSG_ARG_BSYMBOLIC),
485 					    MSG_ORIG(MSG_ARG_BNODIRECT));
486 					ofl->ofl_flags |= FLG_OF_FATAL;
487 				}
488 				ofl->ofl_flags |= FLG_OF_SYMBOLIC;
489 				ofl->ofl_dtflags |= DF_SYMBOLIC;
490 			}
491 
492 			if (Btflag) {
493 				ofl->ofl_dtflags_1 |=
494 				    (DF_1_TRANS | DF_1_DIRECT);
495 				ofl->ofl_flags |= FLG_OF_SYMINFO;
496 			}
497 
498 		} else {
499 			/*
500 			 * Dynamic relocatable object.
501 			 */
502 			if (ztflag == 0)
503 				ofl->ofl_flags1 |= FLG_OF1_TEXTOFF;
504 
505 			if (ofl->ofl_interp) {
506 				eprintf(ofl->ofl_lml, ERR_FATAL,
507 				    MSG_INTL(MSG_MARG_INCOMP),
508 				    MSG_INTL(MSG_MARG_REL),
509 				    MSG_ORIG(MSG_ARG_CI));
510 				ofl->ofl_flags |= FLG_OF_FATAL;
511 			}
512 		}
513 	} else {
514 		ofl->ofl_flags |= FLG_OF_STATIC;
515 
516 		if (bflag) {
517 			eprintf(ofl->ofl_lml, ERR_FATAL,
518 			    MSG_INTL(MSG_ARG_ST_INCOMP), MSG_ORIG(MSG_ARG_B));
519 			ofl->ofl_flags |= FLG_OF_FATAL;
520 		}
521 		if (ofl->ofl_soname) {
522 			eprintf(ofl->ofl_lml, ERR_FATAL,
523 			    MSG_INTL(MSG_MARG_ST_INCOMP),
524 			    MSG_INTL(MSG_MARG_SONAME));
525 			ofl->ofl_flags |= FLG_OF_FATAL;
526 		}
527 		if (ofl->ofl_depaudit) {
528 			eprintf(ofl->ofl_lml, ERR_FATAL,
529 			    MSG_INTL(MSG_ARG_ST_INCOMP), MSG_ORIG(MSG_ARG_CP));
530 			ofl->ofl_flags |= FLG_OF_FATAL;
531 		}
532 		if (ofl->ofl_audit) {
533 			eprintf(ofl->ofl_lml, ERR_FATAL,
534 			    MSG_INTL(MSG_ARG_ST_INCOMP), MSG_ORIG(MSG_ARG_P));
535 			ofl->ofl_flags |= FLG_OF_FATAL;
536 		}
537 		if (ofl->ofl_config) {
538 			eprintf(ofl->ofl_lml, ERR_FATAL,
539 			    MSG_INTL(MSG_ARG_ST_INCOMP), MSG_ORIG(MSG_ARG_C));
540 			ofl->ofl_flags |= FLG_OF_FATAL;
541 		}
542 		if (ofl->ofl_filtees) {
543 			if (ofl->ofl_flags & FLG_OF_AUX) {
544 				eprintf(ofl->ofl_lml, ERR_FATAL,
545 				    MSG_INTL(MSG_MARG_ST_INCOMP),
546 				    MSG_INTL(MSG_MARG_FILTER_AUX));
547 			} else {
548 				eprintf(ofl->ofl_lml, ERR_FATAL,
549 				    MSG_INTL(MSG_MARG_ST_INCOMP),
550 				    MSG_INTL(MSG_MARG_FILTER));
551 			}
552 			ofl->ofl_flags |= FLG_OF_FATAL;
553 		}
554 		if (ztflag) {
555 			eprintf(ofl->ofl_lml, ERR_FATAL,
556 			    MSG_INTL(MSG_ARG_ST_INCOMP),
557 			    MSG_ORIG(MSG_ARG_ZTEXTALL));
558 			ofl->ofl_flags |= FLG_OF_FATAL;
559 		}
560 		if (Gflag) {
561 			eprintf(ofl->ofl_lml, ERR_FATAL,
562 			    MSG_INTL(MSG_MARG_ST_INCOMP),
563 			    MSG_INTL(MSG_MARG_SO));
564 			ofl->ofl_flags |= FLG_OF_FATAL;
565 		}
566 		if (aflag && rflag) {
567 			eprintf(ofl->ofl_lml, ERR_FATAL,
568 			    MSG_INTL(MSG_MARG_INCOMP), MSG_ORIG(MSG_ARG_A),
569 			    MSG_INTL(MSG_MARG_REL));
570 			ofl->ofl_flags |= FLG_OF_FATAL;
571 		}
572 
573 		if (rflag) {
574 			/*
575 			 * We can only strip the symbol table and string table
576 			 * if no output relocations will refer to them.
577 			 */
578 			if (sflag) {
579 				eprintf(ofl->ofl_lml, ERR_WARNING,
580 				    MSG_INTL(MSG_ARG_STRIP),
581 				    MSG_INTL(MSG_MARG_REL),
582 				    MSG_INTL(MSG_MARG_STRIP));
583 			}
584 
585 			if (ztflag == 0)
586 				ofl->ofl_flags1 |= FLG_OF1_TEXTOFF;
587 
588 			if (ofl->ofl_interp) {
589 				eprintf(ofl->ofl_lml, ERR_FATAL,
590 				    MSG_INTL(MSG_MARG_INCOMP),
591 				    MSG_INTL(MSG_MARG_REL),
592 				    MSG_ORIG(MSG_ARG_CI));
593 				ofl->ofl_flags |= FLG_OF_FATAL;
594 			}
595 		} else {
596 			/*
597 			 * Static executable.
598 			 */
599 			ofl->ofl_flags |= FLG_OF_EXEC | FLG_OF_PROCRED;
600 
601 			if (zdflag != SET_FALSE)
602 				ofl->ofl_flags |= FLG_OF_NOUNDEF;
603 		}
604 	}
605 
606 	/*
607 	 * If the user didn't supply an output file name supply a default.
608 	 */
609 	if (ofl->ofl_name == NULL)
610 		ofl->ofl_name = MSG_ORIG(MSG_STR_AOUT);
611 
612 	/*
613 	 * We set the entrance criteria after all input argument processing as
614 	 * it is only at this point we're sure what the output image will be
615 	 * (static or dynamic).
616 	 */
617 	if (ld_ent_setup(ofl, ld_targ.t_m.m_segm_align) == S_ERROR)
618 		return (S_ERROR);
619 
620 	/*
621 	 * Does the host currently running the linker have the same
622 	 * byte order as the target for which the object is being produced?
623 	 * If not, set FLG_OF1_ENCDIFF so relocation code will know
624 	 * to check.
625 	 */
626 	if (_elf_sys_encoding() != ld_targ.t_m.m_data)
627 		ofl->ofl_flags1 |= FLG_OF1_ENCDIFF;
628 
629 	/*
630 	 * Initialize string tables.  Symbol definitions within mapfiles can
631 	 * result in the creation of input sections.
632 	 */
633 	if (ld_init_strings(ofl) == S_ERROR)
634 		return (S_ERROR);
635 
636 	/*
637 	 * Process any mapfiles after establishing the entrance criteria as
638 	 * the user may be redefining or adding sections/segments.
639 	 */
640 	if (ofl->ofl_maps.head) {
641 		Listnode	*lnp;
642 		const char	*name;
643 
644 		for (LIST_TRAVERSE(&ofl->ofl_maps, lnp, name))
645 			if (ld_map_parse(name, ofl) == S_ERROR)
646 				return (S_ERROR);
647 
648 		if (ofl->ofl_flags & FLG_OF_SEGSORT)
649 			if (ld_sort_seg_list(ofl) == S_ERROR)
650 				return (S_ERROR);
651 	}
652 
653 	/*
654 	 * If a mapfile has been used to define a single symbolic scope of
655 	 * interfaces, -Bsymbolic is established.  This global setting goes
656 	 * beyond individual symbol protection, and ensures all relocations
657 	 * (even those that reference section symbols) are processed within
658 	 * the object being built.
659 	 */
660 	if (((ofl->ofl_flags &
661 	    (FLG_OF_MAPSYMB | FLG_OF_MAPGLOB)) == FLG_OF_MAPSYMB) &&
662 	    (ofl->ofl_flags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM))) {
663 		ofl->ofl_flags |= FLG_OF_SYMBOLIC;
664 		ofl->ofl_dtflags |= DF_SYMBOLIC;
665 	}
666 
667 	/*
668 	 * If -zloadfltr is set, verify that filtering is in effect.  Filters
669 	 * are either established from the command line, and affect the whole
670 	 * object, or are set on a per-symbol basis from a mapfile.
671 	 */
672 	if (zlflag) {
673 		if ((ofl->ofl_filtees == NULL) && (ofl->ofl_dtsfltrs == NULL)) {
674 			eprintf(ofl->ofl_lml, ERR_FATAL,
675 			    MSG_INTL(MSG_ARG_NOFLTR),
676 			    MSG_ORIG(MSG_ARG_ZLOADFLTR));
677 			ofl->ofl_flags |= FLG_OF_FATAL;
678 		}
679 		ofl->ofl_dtflags_1 |= DF_1_LOADFLTR;
680 	}
681 
682 	/*
683 	 * Check that we have something to work with.  This check is carried out
684 	 * after mapfile processing as its possible a mapfile is being used to
685 	 * define symbols, in which case it would be sufficient to build the
686 	 * output file purely from the mapfile.
687 	 */
688 	if ((ofl->ofl_objscnt == 0) && (ofl->ofl_soscnt == 0)) {
689 		if (Vflag && (argc == 2))
690 			ofl->ofl_flags1 |= FLG_OF1_DONE;
691 		else {
692 			eprintf(ofl->ofl_lml, ERR_FATAL,
693 			    MSG_INTL(MSG_ARG_NOFILES));
694 			return (S_ERROR);
695 		}
696 	}
697 	return (1);
698 }
699 
700 /*
701  * Decompose the string pointed by optarg into argv[][] so that argv[][] can be
702  * used as an argument to getopt().
703  *
704  * If the second argument 'error' is not 0, then this is called from the first
705  * pass. Else this is called from the second pass.
706  */
707 static uintptr_t
708 createargv(Ofl_desc *ofl, int *error)
709 {
710 	int		argc = 0, idx = 0, ooptind;
711 	uintptr_t	ret;
712 	char		**argv, *p0;
713 
714 	/*
715 	 * The argument being examined is either:
716 	 *	ld32= 	or
717 	 *	ld64=
718 	 */
719 #if	defined(_LP64)
720 	if (optarg[2] == '3')
721 		return (0);
722 #else
723 	if (optarg[2] == '6')
724 		return (0);
725 #endif
726 
727 	p0 = &optarg[5];
728 
729 	/*
730 	 * Count the number of arguments.
731 	 */
732 	while (*p0) {
733 		/*
734 		 * Pointing at non-separator character.
735 		 */
736 		if (*p0 != ',') {
737 			argc++;
738 			while (*p0 && (*p0 != ','))
739 				p0++;
740 			continue;
741 		}
742 
743 		/*
744 		 * Pointing at a separator character.
745 		 */
746 		if (*p0 == ',') {
747 			while (*p0 == ',')
748 				p0++;
749 			continue;
750 		}
751 	}
752 
753 	if (argc == 0)
754 		return (0);
755 
756 	/*
757 	 * Allocate argument vector.
758 	 */
759 	if ((p0 = (char *)strdup(&optarg[5])) == 0)
760 		return (S_ERROR);
761 	if ((argv = libld_malloc((sizeof (char *)) * (argc + 1))) == 0)
762 		return (S_ERROR);
763 
764 	while (*p0) {
765 		char *p;
766 
767 		/*
768 		 * Pointing at the beginning of non-separator character string.
769 		 */
770 		if (*p0 != ',') {
771 			p = p0;
772 			while (*p0 && (*p0 != ','))
773 				p0++;
774 			argv[idx++] = p;
775 			if (*p0) {
776 				*p0 = '\0';
777 				p0++;
778 			}
779 			continue;
780 		}
781 
782 		/*
783 		 * Pointing at the beginining of separator character string.
784 		 */
785 		if (*p0 == ',') {
786 			while (*p0 == ',')
787 				p0++;
788 			continue;
789 		}
790 	}
791 	argv[idx] = 0;
792 	ooptind = optind;
793 	optind = 0;
794 
795 	/*
796 	 * Dispatch to pass1 or pass2
797 	 */
798 	if (error)
799 		ret = process_flags_com(ofl, argc, argv, error);
800 	else
801 		ret = process_files_com(ofl, argc, argv);
802 
803 	optind = ooptind;
804 	return (ret);
805 }
806 
807 /*
808  * Parsing options pass1 for process_flags().
809  */
810 static uintptr_t
811 parseopt_pass1(Ofl_desc *ofl, int argc, char **argv, int *error)
812 {
813 	int	c, ndx = optind;
814 
815 	/*
816 	 * The -64 and -ztarget options are special, in that we validate
817 	 * them, but otherwise ignore them. libld.so (this code) is called
818 	 * from the ld front end program. ld has already examined the
819 	 * arguments to determine the output class and machine type of the
820 	 * output object, as reflected in the version (32/64) of ld_main()
821 	 * that was called and the value of the 'mach' argument passed.
822 	 * By time execution reaches this point, these options have already
823 	 * been seen and acted on.
824 	 */
825 	while ((c = ld_getopt(ofl->ofl_lml, ndx, argc, argv)) != -1) {
826 
827 		switch (c) {
828 		case '6':
829 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
830 
831 			/*
832 			 * -64 is processed by ld to determine the output class.
833 			 * Here we sanity check the option incase some other
834 			 * -6* option is mistakenly passed to us.
835 			 */
836 			if (optarg[0] != '4') {
837 				eprintf(ofl->ofl_lml, ERR_FATAL,
838 				    MSG_INTL(MSG_ARG_ILLEGAL),
839 				    MSG_ORIG(MSG_ARG_6), optarg);
840 				ofl->ofl_flags |= FLG_OF_FATAL;
841 			}
842 			continue;
843 
844 		case 'a':
845 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
846 			aflag = TRUE;
847 			break;
848 
849 		case 'b':
850 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
851 			bflag = TRUE;
852 
853 			/*
854 			 * This is a hack, and may be undone later.
855 			 * The -b option is only used to build the Unix
856 			 * kernel and its related kernel-mode modules.
857 			 * We do not want those files to get a .SUNW_ldynsym
858 			 * section. At least for now, the kernel makes no
859 			 * use of .SUNW_ldynsym, and we do not want to use
860 			 * the space to hold it. Therefore, we overload
861 			 * the use of -b to also imply -znoldynsym.
862 			 */
863 			ofl->ofl_flags |= FLG_OF_NOLDYNSYM;
864 			break;
865 
866 		case 'c':
867 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
868 			if (ofl->ofl_config)
869 				eprintf(ofl->ofl_lml, ERR_WARNING,
870 				    MSG_INTL(MSG_ARG_MTONCE),
871 				    MSG_ORIG(MSG_ARG_C));
872 			else
873 				ofl->ofl_config = optarg;
874 			break;
875 
876 		case 'C':
877 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
878 			demangle_flag = 1;
879 			break;
880 
881 		case 'd':
882 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
883 			if ((optarg[0] == 'n') && (optarg[1] == '\0')) {
884 				if (dflag != SET_UNKNOWN)
885 					eprintf(ofl->ofl_lml, ERR_WARNING,
886 					    MSG_INTL(MSG_ARG_MTONCE),
887 					    MSG_ORIG(MSG_ARG_D));
888 				else
889 					dflag = SET_FALSE;
890 			} else if ((optarg[0] == 'y') && (optarg[1] == '\0')) {
891 				if (dflag != SET_UNKNOWN)
892 					eprintf(ofl->ofl_lml, ERR_WARNING,
893 					    MSG_INTL(MSG_ARG_MTONCE),
894 					    MSG_ORIG(MSG_ARG_D));
895 				else
896 					dflag = SET_TRUE;
897 			} else {
898 				eprintf(ofl->ofl_lml, ERR_FATAL,
899 				    MSG_INTL(MSG_ARG_ILLEGAL),
900 				    MSG_ORIG(MSG_ARG_D), optarg);
901 				ofl->ofl_flags |= FLG_OF_FATAL;
902 			}
903 			break;
904 
905 		case 'e':
906 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
907 			if (ofl->ofl_entry)
908 				eprintf(ofl->ofl_lml, ERR_WARNING,
909 				    MSG_INTL(MSG_MARG_MTONCE),
910 				    MSG_INTL(MSG_MARG_ENTRY));
911 			else
912 				ofl->ofl_entry = (void *)optarg;
913 			break;
914 
915 		case 'f':
916 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
917 			if (ofl->ofl_filtees &&
918 			    (!(ofl->ofl_flags & FLG_OF_AUX))) {
919 				eprintf(ofl->ofl_lml, ERR_FATAL,
920 				    MSG_INTL(MSG_MARG_INCOMP),
921 				    MSG_INTL(MSG_MARG_FILTER_AUX),
922 				    MSG_INTL(MSG_MARG_FILTER));
923 				ofl->ofl_flags |= FLG_OF_FATAL;
924 			} else {
925 				if ((ofl->ofl_filtees =
926 				    add_string(ofl->ofl_filtees, optarg)) ==
927 				    (const char *)S_ERROR)
928 					return (S_ERROR);
929 				ofl->ofl_flags |= FLG_OF_AUX;
930 			}
931 			break;
932 
933 		case 'F':
934 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
935 			if (ofl->ofl_filtees &&
936 			    (ofl->ofl_flags & FLG_OF_AUX)) {
937 				eprintf(ofl->ofl_lml, ERR_FATAL,
938 				    MSG_INTL(MSG_MARG_INCOMP),
939 				    MSG_INTL(MSG_MARG_FILTER),
940 				    MSG_INTL(MSG_MARG_FILTER_AUX));
941 				ofl->ofl_flags |= FLG_OF_FATAL;
942 			} else {
943 				if ((ofl->ofl_filtees =
944 				    add_string(ofl->ofl_filtees, optarg)) ==
945 				    (const char *)S_ERROR)
946 					return (S_ERROR);
947 			}
948 			break;
949 
950 		case 'h':
951 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
952 			if (ofl->ofl_soname)
953 				eprintf(ofl->ofl_lml, ERR_WARNING,
954 				    MSG_INTL(MSG_MARG_MTONCE),
955 				    MSG_INTL(MSG_MARG_SONAME));
956 			else
957 				ofl->ofl_soname = (const char *)optarg;
958 			break;
959 
960 		case 'i':
961 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
962 			ofl->ofl_flags |= FLG_OF_IGNENV;
963 			break;
964 
965 		case 'I':
966 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
967 			if (ofl->ofl_interp)
968 				eprintf(ofl->ofl_lml, ERR_WARNING,
969 				    MSG_INTL(MSG_ARG_MTONCE),
970 				    MSG_ORIG(MSG_ARG_CI));
971 			else
972 				ofl->ofl_interp = (const char *)optarg;
973 			break;
974 
975 		case 'l':
976 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
977 			/*
978 			 * For now, count any library as a shared object.  This
979 			 * is used to size the internal symbol cache.  This
980 			 * value is recalculated later on actual file processing
981 			 * to get an accurate shared object count.
982 			 */
983 			ofl->ofl_soscnt++;
984 			break;
985 
986 		case 'm':
987 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
988 			ofl->ofl_flags |= FLG_OF_GENMAP;
989 			break;
990 
991 		case 'o':
992 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
993 			if (ofl->ofl_name)
994 				eprintf(ofl->ofl_lml, ERR_WARNING,
995 				    MSG_INTL(MSG_MARG_MTONCE),
996 				    MSG_INTL(MSG_MARG_OUTFILE));
997 			else
998 				ofl->ofl_name = (const char *)optarg;
999 			break;
1000 
1001 		case 'p':
1002 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1003 
1004 			/*
1005 			 * Multiple instances of this option may occur.  Each
1006 			 * additional instance is effectively concatenated to
1007 			 * the previous separated by a colon.
1008 			 */
1009 			if (*optarg != '\0') {
1010 				if ((ofl->ofl_audit =
1011 				    add_string(ofl->ofl_audit,
1012 				    optarg)) == (const char *)S_ERROR)
1013 					return (S_ERROR);
1014 			}
1015 			break;
1016 
1017 		case 'P':
1018 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1019 
1020 			/*
1021 			 * Multiple instances of this option may occur.  Each
1022 			 * additional instance is effectively concatenated to
1023 			 * the previous separated by a colon.
1024 			 */
1025 			if (*optarg != '\0') {
1026 				if ((ofl->ofl_depaudit =
1027 				    add_string(ofl->ofl_depaudit,
1028 				    optarg)) == (const char *)S_ERROR)
1029 					return (S_ERROR);
1030 			}
1031 			break;
1032 
1033 		case 'r':
1034 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
1035 			rflag = TRUE;
1036 			break;
1037 
1038 		case 'R':
1039 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1040 
1041 			/*
1042 			 * Multiple instances of this option may occur.  Each
1043 			 * additional instance is effectively concatenated to
1044 			 * the previous separated by a colon.
1045 			 */
1046 			if (*optarg != '\0') {
1047 				if ((ofl->ofl_rpath =
1048 				    add_string(ofl->ofl_rpath,
1049 				    optarg)) == (const char *)S_ERROR)
1050 					return (S_ERROR);
1051 			}
1052 			break;
1053 
1054 		case 's':
1055 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
1056 			sflag = TRUE;
1057 			break;
1058 
1059 		case 't':
1060 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
1061 			ofl->ofl_flags |= FLG_OF_NOWARN;
1062 			break;
1063 
1064 		case 'u':
1065 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1066 			break;
1067 
1068 		case 'z':
1069 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1070 
1071 			/*
1072 			 * For specific help, print our usage message and exit
1073 			 * immediately to ensure a 0 return code.
1074 			 */
1075 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_HELP),
1076 			    MSG_ARG_HELP_SIZE) == 0) {
1077 				usage_mesg(1);
1078 				exit(0);
1079 			}
1080 
1081 			/*
1082 			 * For some options set a flag - further consistancy
1083 			 * checks will be carried out in check_flags().
1084 			 */
1085 			if ((strncmp(optarg, MSG_ORIG(MSG_ARG_LD32),
1086 			    MSG_ARG_LD32_SIZE) == 0) ||
1087 			    (strncmp(optarg, MSG_ORIG(MSG_ARG_LD64),
1088 			    MSG_ARG_LD64_SIZE) == 0)) {
1089 				if (createargv(ofl, error) == S_ERROR)
1090 					return (S_ERROR);
1091 
1092 			} else if (
1093 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DEFS)) == 0) {
1094 				if (zdflag != SET_UNKNOWN)
1095 					eprintf(ofl->ofl_lml, ERR_WARNING,
1096 					    MSG_INTL(MSG_ARG_MTONCE),
1097 					    MSG_ORIG(MSG_ARG_ZDEFNODEF));
1098 				else
1099 					zdflag = SET_TRUE;
1100 			} else if (strcmp(optarg,
1101 			    MSG_ORIG(MSG_ARG_NODEFS)) == 0) {
1102 				if (zdflag != SET_UNKNOWN)
1103 					eprintf(ofl->ofl_lml, ERR_WARNING,
1104 					    MSG_INTL(MSG_ARG_MTONCE),
1105 					    MSG_ORIG(MSG_ARG_ZDEFNODEF));
1106 				else
1107 					zdflag = SET_FALSE;
1108 			} else if (strcmp(optarg,
1109 			    MSG_ORIG(MSG_ARG_TEXT)) == 0) {
1110 				if (ztflag &&
1111 				    (ztflag != MSG_ORIG(MSG_ARG_ZTEXT))) {
1112 					eprintf(ofl->ofl_lml, ERR_FATAL,
1113 					    MSG_INTL(MSG_ARG_INCOMP),
1114 					    MSG_ORIG(MSG_ARG_ZTEXT),
1115 					    ztflag);
1116 					ofl->ofl_flags |= FLG_OF_FATAL;
1117 				}
1118 				ztflag = MSG_ORIG(MSG_ARG_ZTEXT);
1119 			} else if (strcmp(optarg,
1120 			    MSG_ORIG(MSG_ARG_TEXTOFF)) == 0) {
1121 				if (ztflag &&
1122 				    (ztflag != MSG_ORIG(MSG_ARG_ZTEXTOFF))) {
1123 					eprintf(ofl->ofl_lml, ERR_FATAL,
1124 					    MSG_INTL(MSG_ARG_INCOMP),
1125 					    MSG_ORIG(MSG_ARG_ZTEXTOFF),
1126 					    ztflag);
1127 					ofl->ofl_flags |= FLG_OF_FATAL;
1128 				}
1129 				ztflag = MSG_ORIG(MSG_ARG_ZTEXTOFF);
1130 			} else if (strcmp(optarg,
1131 			    MSG_ORIG(MSG_ARG_TEXTWARN)) == 0) {
1132 				if (ztflag &&
1133 				    (ztflag != MSG_ORIG(MSG_ARG_ZTEXTWARN))) {
1134 					eprintf(ofl->ofl_lml, ERR_FATAL,
1135 					    MSG_INTL(MSG_ARG_INCOMP),
1136 					    MSG_ORIG(MSG_ARG_ZTEXTWARN),
1137 					    ztflag);
1138 					ofl->ofl_flags |= FLG_OF_FATAL;
1139 				}
1140 				ztflag = MSG_ORIG(MSG_ARG_ZTEXTWARN);
1141 
1142 			/*
1143 			 * For other options simply set the ofl flags directly.
1144 			 */
1145 			} else if (strcmp(optarg,
1146 			    MSG_ORIG(MSG_ARG_RESCAN)) == 0) {
1147 				ofl->ofl_flags1 |= FLG_OF1_RESCAN;
1148 			} else if (strcmp(optarg,
1149 			    MSG_ORIG(MSG_ARG_ABSEXEC)) == 0) {
1150 				ofl->ofl_flags1 |= FLG_OF1_ABSEXEC;
1151 			} else if (strcmp(optarg,
1152 			    MSG_ORIG(MSG_ARG_LOADFLTR)) == 0) {
1153 				zlflag = TRUE;
1154 			} else if (strcmp(optarg,
1155 			    MSG_ORIG(MSG_ARG_NORELOC)) == 0) {
1156 				ofl->ofl_dtflags_1 |= DF_1_NORELOC;
1157 			} else if (strcmp(optarg,
1158 			    MSG_ORIG(MSG_ARG_NOVERSION)) == 0) {
1159 				ofl->ofl_flags |= FLG_OF_NOVERSEC;
1160 			} else if (strcmp(optarg,
1161 			    MSG_ORIG(MSG_ARG_MULDEFS)) == 0) {
1162 				ofl->ofl_flags |= FLG_OF_MULDEFS;
1163 			} else if (strcmp(optarg,
1164 			    MSG_ORIG(MSG_ARG_REDLOCSYM)) == 0) {
1165 				ofl->ofl_flags |= FLG_OF_REDLSYM;
1166 			} else if (strcmp(optarg,
1167 			    MSG_ORIG(MSG_ARG_INITFIRST)) == 0) {
1168 				ofl->ofl_dtflags_1 |= DF_1_INITFIRST;
1169 			} else if (strcmp(optarg,
1170 			    MSG_ORIG(MSG_ARG_NODELETE)) == 0) {
1171 				ofl->ofl_dtflags_1 |= DF_1_NODELETE;
1172 			} else if (strcmp(optarg,
1173 			    MSG_ORIG(MSG_ARG_NOPARTIAL)) == 0) {
1174 				ofl->ofl_flags1 |= FLG_OF1_NOPARTI;
1175 			} else if (strcmp(optarg,
1176 			    MSG_ORIG(MSG_ARG_NOOPEN)) == 0) {
1177 				ofl->ofl_dtflags_1 |= DF_1_NOOPEN;
1178 			} else if (strcmp(optarg,
1179 			    MSG_ORIG(MSG_ARG_NOW)) == 0) {
1180 				ofl->ofl_dtflags_1 |= DF_1_NOW;
1181 				ofl->ofl_dtflags |= DF_BIND_NOW;
1182 			} else if (strcmp(optarg,
1183 			    MSG_ORIG(MSG_ARG_ORIGIN)) == 0) {
1184 				ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1185 				ofl->ofl_dtflags |= DF_ORIGIN;
1186 			} else if (strcmp(optarg,
1187 			    MSG_ORIG(MSG_ARG_NODEFAULTLIB)) == 0) {
1188 				ofl->ofl_dtflags_1 |= DF_1_NODEFLIB;
1189 			} else if (strcmp(optarg,
1190 			    MSG_ORIG(MSG_ARG_NODUMP)) == 0) {
1191 				ofl->ofl_dtflags_1 |= DF_1_NODUMP;
1192 			} else if (strcmp(optarg,
1193 			    MSG_ORIG(MSG_ARG_ENDFILTEE)) == 0) {
1194 				ofl->ofl_dtflags_1 |= DF_1_ENDFILTEE;
1195 			} else if (strcmp(optarg,
1196 			    MSG_ORIG(MSG_ARG_VERBOSE)) == 0) {
1197 				ofl->ofl_flags |= FLG_OF_VERBOSE;
1198 			} else if (strcmp(optarg,
1199 			    MSG_ORIG(MSG_ARG_COMBRELOC)) == 0) {
1200 				ofl->ofl_flags |= FLG_OF_COMREL;
1201 			} else if (strcmp(optarg,
1202 			    MSG_ORIG(MSG_ARG_NOCOMBRELOC)) == 0) {
1203 				ofl->ofl_flags |= FLG_OF_NOCOMREL;
1204 			} else if (strcmp(optarg,
1205 			    MSG_ORIG(MSG_ARG_NOCOMPSTRTAB)) == 0) {
1206 				ofl->ofl_flags1 |= FLG_OF1_NCSTTAB;
1207 			} else if (strcmp(optarg,
1208 			    MSG_ORIG(MSG_ARG_NOINTERP)) == 0) {
1209 				ofl->ofl_flags1 |= FLG_OF1_NOINTRP;
1210 			} else if (strcmp(optarg,
1211 			    MSG_ORIG(MSG_ARG_INTERPOSE)) == 0) {
1212 				zinflag = TRUE;
1213 			} else if (strcmp(optarg,
1214 			    MSG_ORIG(MSG_ARG_IGNORE)) == 0) {
1215 				ofl->ofl_flags1 |= FLG_OF1_IGNPRC;
1216 			} else if (strcmp(optarg,
1217 			    MSG_ORIG(MSG_ARG_RELAXRELOC)) == 0) {
1218 				ofl->ofl_flags1 |= FLG_OF1_RLXREL;
1219 			} else if (strcmp(optarg,
1220 			    MSG_ORIG(MSG_ARG_NORELAXRELOC)) == 0) {
1221 				ofl->ofl_flags1 |= FLG_OF1_NRLXREL;
1222 			} else if (strcmp(optarg,
1223 			    MSG_ORIG(MSG_ARG_NOLDYNSYM)) == 0) {
1224 				ofl->ofl_flags |= FLG_OF_NOLDYNSYM;
1225 			} else if (strcmp(optarg,
1226 			    MSG_ORIG(MSG_ARG_GLOBAUDIT)) == 0) {
1227 				ofl->ofl_dtflags_1 |= DF_1_GLOBAUDIT;
1228 
1229 			/*
1230 			 * Check archive group usage
1231 			 *	-z rescan-start ... -z rescan-end
1232 			 * to ensure they don't overlap and are well formed.
1233 			 */
1234 			} else if (strcmp(optarg,
1235 			    MSG_ORIG(MSG_ARG_RESCAN_START)) == 0) {
1236 				if (ofl->ofl_ars_gsandx == 0) {
1237 					ofl->ofl_ars_gsandx = ndx;
1238 				} else if (ofl->ofl_ars_gsandx > 0) {
1239 					/* Another group is still open */
1240 					eprintf(ofl->ofl_lml, ERR_FATAL,
1241 					    MSG_INTL(MSG_ARG_AR_GRP_OLAP),
1242 					    MSG_INTL(MSG_MARG_AR_GRPS));
1243 					ofl->ofl_flags |= FLG_OF_FATAL;
1244 					/* Don't report cascading errors */
1245 					ofl->ofl_ars_gsandx = -1;
1246 				}
1247 			} else if (strcmp(optarg,
1248 			    MSG_ORIG(MSG_ARG_RESCAN_END)) == 0) {
1249 				if (ofl->ofl_ars_gsandx > 0) {
1250 					ofl->ofl_ars_gsandx = 0;
1251 				} else if (ofl->ofl_ars_gsandx == 0) {
1252 					/* There was no matching begin */
1253 					eprintf(ofl->ofl_lml, ERR_FATAL,
1254 					    MSG_INTL(MSG_ARG_AR_GRP_BAD),
1255 					    MSG_INTL(MSG_MARG_AR_GRP_END),
1256 					    MSG_INTL(MSG_MARG_AR_GRP_START));
1257 					ofl->ofl_flags |= FLG_OF_FATAL;
1258 					/* Don't report cascading errors */
1259 					ofl->ofl_ars_gsandx = -1;
1260 				}
1261 			/*
1262 			 * The following options just need validation as they
1263 			 * are interpreted on the second pass through the
1264 			 * command line arguments.
1265 			 */
1266 			} else if (
1267 			    strncmp(optarg, MSG_ORIG(MSG_ARG_INITARRAY),
1268 			    MSG_ARG_INITARRAY_SIZE) &&
1269 			    strncmp(optarg, MSG_ORIG(MSG_ARG_FINIARRAY),
1270 			    MSG_ARG_FINIARRAY_SIZE) &&
1271 			    strncmp(optarg, MSG_ORIG(MSG_ARG_PREINITARRAY),
1272 			    MSG_ARG_PREINITARRAY_SIZE) &&
1273 			    strncmp(optarg, MSG_ORIG(MSG_ARG_RTLDINFO),
1274 			    MSG_ARG_RTLDINFO_SIZE) &&
1275 			    strncmp(optarg, MSG_ORIG(MSG_ARG_DTRACE),
1276 			    MSG_ARG_DTRACE_SIZE) &&
1277 			    strcmp(optarg, MSG_ORIG(MSG_ARG_ALLEXTRT)) &&
1278 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DFLEXTRT)) &&
1279 			    strcmp(optarg, MSG_ORIG(MSG_ARG_DIRECT)) &&
1280 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NODIRECT)) &&
1281 			    strcmp(optarg, MSG_ORIG(MSG_ARG_GROUPPERM)) &&
1282 			    strcmp(optarg, MSG_ORIG(MSG_ARG_LAZYLOAD)) &&
1283 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NOGROUPPERM)) &&
1284 			    strcmp(optarg, MSG_ORIG(MSG_ARG_NOLAZYLOAD)) &&
1285 			    strcmp(optarg, MSG_ORIG(MSG_ARG_RECORD)) &&
1286 			    strcmp(optarg, MSG_ORIG(MSG_ARG_ALTEXEC64)) &&
1287 			    strcmp(optarg, MSG_ORIG(MSG_ARG_WEAKEXT)) &&
1288 			    strncmp(optarg, MSG_ORIG(MSG_ARG_TARGET),
1289 			    MSG_ARG_TARGET_SIZE) &&
1290 			    strcmp(optarg, MSG_ORIG(MSG_ARG_RESCAN_NOW))) {
1291 				eprintf(ofl->ofl_lml, ERR_FATAL,
1292 				    MSG_INTL(MSG_ARG_ILLEGAL),
1293 				    MSG_ORIG(MSG_ARG_Z), optarg);
1294 				ofl->ofl_flags |= FLG_OF_FATAL;
1295 			}
1296 
1297 			break;
1298 
1299 		case 'D':
1300 			/*
1301 			 * If we have not yet read any input files go ahead
1302 			 * and process any debugging options (this allows any
1303 			 * argument processing, entrance criteria and library
1304 			 * initialization to be displayed).  Otherwise, if an
1305 			 * input file has been seen, skip interpretation until
1306 			 * process_files (this allows debugging to be turned
1307 			 * on and off around individual groups of files).
1308 			 */
1309 			if (ofl->ofl_objscnt == 0) {
1310 				if (dbg_setup(optarg, dbg_desc,
1311 				    &ofl->ofl_name, 2) == S_ERROR)
1312 					return (S_ERROR);
1313 			}
1314 
1315 			/*
1316 			 * A diagnostic can only be provided after dbg_setup().
1317 			 */
1318 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1319 			break;
1320 
1321 		case 'B':
1322 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1323 			if (strcmp(optarg, MSG_ORIG(MSG_ARG_DIRECT)) == 0) {
1324 				if (Bdflag == SET_FALSE) {
1325 					eprintf(ofl->ofl_lml, ERR_FATAL,
1326 					    MSG_INTL(MSG_ARG_INCOMP),
1327 					    MSG_ORIG(MSG_ARG_BNODIRECT),
1328 					    MSG_ORIG(MSG_ARG_BDIRECT));
1329 					ofl->ofl_flags |= FLG_OF_FATAL;
1330 				} else
1331 					Bdflag = SET_TRUE;
1332 			} else if (strcmp(optarg,
1333 			    MSG_ORIG(MSG_ARG_NODIRECT)) == 0) {
1334 				if (Bdflag == SET_TRUE) {
1335 					eprintf(ofl->ofl_lml, ERR_FATAL,
1336 					    MSG_INTL(MSG_ARG_INCOMP),
1337 					    MSG_ORIG(MSG_ARG_BDIRECT),
1338 					    MSG_ORIG(MSG_ARG_BNODIRECT));
1339 					ofl->ofl_flags |= FLG_OF_FATAL;
1340 				} else
1341 					Bdflag = SET_FALSE;
1342 			} else if (strcmp(optarg,
1343 			    MSG_ORIG(MSG_STR_SYMBOLIC)) == 0)
1344 				Bsflag = TRUE;
1345 			else if (strcmp(optarg, MSG_ORIG(MSG_ARG_REDUCE)) == 0)
1346 				ofl->ofl_flags |= FLG_OF_PROCRED;
1347 			else if (strcmp(optarg, MSG_ORIG(MSG_STR_LOCAL)) == 0)
1348 				Blflag = TRUE;
1349 			else if (strcmp(optarg,
1350 			    MSG_ORIG(MSG_ARG_TRANSLATOR)) == 0)
1351 				Btflag = TRUE;
1352 			else if (strcmp(optarg, MSG_ORIG(MSG_ARG_GROUP)) == 0)
1353 				Bgflag = TRUE;
1354 			else if (strcmp(optarg,
1355 			    MSG_ORIG(MSG_STR_ELIMINATE)) == 0)
1356 				Beflag = TRUE;
1357 			else if (strcmp(optarg, MSG_ORIG(MSG_STR_LD_DYNAMIC)) &&
1358 			    strcmp(optarg, MSG_ORIG(MSG_ARG_STATIC))) {
1359 				eprintf(ofl->ofl_lml, ERR_FATAL,
1360 				    MSG_INTL(MSG_ARG_ILLEGAL),
1361 				    MSG_ORIG(MSG_ARG_CB), optarg);
1362 				ofl->ofl_flags |= FLG_OF_FATAL;
1363 			}
1364 			break;
1365 
1366 		case 'G':
1367 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
1368 			Gflag = TRUE;
1369 			break;
1370 
1371 		case 'L':
1372 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1373 			break;
1374 
1375 		case 'M':
1376 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1377 			if (list_appendc(&(ofl->ofl_maps), optarg) == 0)
1378 				return (S_ERROR);
1379 			break;
1380 
1381 		case 'N':
1382 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1383 			break;
1384 
1385 		case 'Q':
1386 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1387 			if ((optarg[0] == 'n') && (optarg[1] == '\0')) {
1388 				if (Qflag != SET_UNKNOWN)
1389 					eprintf(ofl->ofl_lml, ERR_WARNING,
1390 					    MSG_INTL(MSG_ARG_MTONCE),
1391 					    MSG_ORIG(MSG_ARG_CQ));
1392 				else
1393 					Qflag = SET_FALSE;
1394 			} else if ((optarg[0] == 'y') && (optarg[1] == '\0')) {
1395 				if (Qflag != SET_UNKNOWN)
1396 					eprintf(ofl->ofl_lml, ERR_WARNING,
1397 					    MSG_INTL(MSG_ARG_MTONCE),
1398 					    MSG_ORIG(MSG_ARG_CQ));
1399 				else
1400 					Qflag = SET_TRUE;
1401 			} else {
1402 				eprintf(ofl->ofl_lml, ERR_FATAL,
1403 				    MSG_INTL(MSG_ARG_ILLEGAL),
1404 				    MSG_ORIG(MSG_ARG_CQ), optarg);
1405 				ofl->ofl_flags |= FLG_OF_FATAL;
1406 			}
1407 			break;
1408 
1409 		case 'S':
1410 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1411 			if (list_appendc(&lib_support, optarg) == 0)
1412 				return (S_ERROR);
1413 			break;
1414 
1415 		case 'V':
1416 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
1417 			if (!Vflag)
1418 				(void) fprintf(stderr, MSG_ORIG(MSG_STR_STRNL),
1419 				    ofl->ofl_sgsid);
1420 			Vflag = TRUE;
1421 			break;
1422 
1423 		case 'Y':
1424 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, optarg));
1425 			if (strncmp(optarg, MSG_ORIG(MSG_ARG_LCOM), 2) == 0) {
1426 				if (Llibdir)
1427 					eprintf(ofl->ofl_lml, ERR_WARNING,
1428 					    MSG_INTL(MSG_ARG_MTONCE),
1429 					    MSG_ORIG(MSG_ARG_CYL));
1430 				else
1431 					Llibdir = optarg + 2;
1432 			} else if (strncmp(optarg,
1433 			    MSG_ORIG(MSG_ARG_UCOM), 2) == 0) {
1434 				if (Ulibdir)
1435 					eprintf(ofl->ofl_lml, ERR_WARNING,
1436 					    MSG_INTL(MSG_ARG_MTONCE),
1437 					    MSG_ORIG(MSG_ARG_CYU));
1438 				else
1439 					Ulibdir = optarg + 2;
1440 			} else if (strncmp(optarg,
1441 			    MSG_ORIG(MSG_ARG_PCOM), 2) == 0) {
1442 				if (Plibpath)
1443 					eprintf(ofl->ofl_lml, ERR_WARNING,
1444 					    MSG_INTL(MSG_ARG_MTONCE),
1445 					    MSG_ORIG(MSG_ARG_CYP));
1446 				else
1447 					Plibpath = optarg + 2;
1448 			} else {
1449 				eprintf(ofl->ofl_lml, ERR_FATAL,
1450 				    MSG_INTL(MSG_ARG_ILLEGAL),
1451 				    MSG_ORIG(MSG_ARG_CY), optarg);
1452 				ofl->ofl_flags |= FLG_OF_FATAL;
1453 			}
1454 			break;
1455 
1456 		case '?':
1457 			DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c, NULL));
1458 			(*error)++;
1459 			break;
1460 
1461 		default:
1462 			break;
1463 		}
1464 
1465 		/*
1466 		 * Update the argument index for the next getopt() iteration.
1467 		 */
1468 		ndx = optind;
1469 	}
1470 	return (1);
1471 }
1472 
1473 /*
1474  * Parsing options pass2 for
1475  */
1476 static uintptr_t
1477 parseopt_pass2(Ofl_desc *ofl, int argc, char **argv)
1478 {
1479 	int	c, ndx = optind;
1480 
1481 	while ((c = ld_getopt(ofl->ofl_lml, ndx, argc, argv)) != -1) {
1482 		Ifl_desc	*ifl;
1483 		Sym_desc	*sdp;
1484 
1485 		switch (c) {
1486 			case 'l':
1487 				DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c,
1488 				    optarg));
1489 				if (ld_find_library(optarg, ofl) == S_ERROR)
1490 					return (S_ERROR);
1491 				break;
1492 			case 'B':
1493 				DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c,
1494 				    optarg));
1495 				if (strcmp(optarg,
1496 				    MSG_ORIG(MSG_STR_LD_DYNAMIC)) == 0) {
1497 					if (ofl->ofl_flags & FLG_OF_DYNAMIC)
1498 						ofl->ofl_flags |=
1499 						    FLG_OF_DYNLIBS;
1500 					else {
1501 						eprintf(ofl->ofl_lml, ERR_FATAL,
1502 						    MSG_INTL(MSG_ARG_ST_INCOMP),
1503 						    MSG_ORIG(MSG_ARG_BDYNAMIC));
1504 						ofl->ofl_flags |= FLG_OF_FATAL;
1505 					}
1506 				} else if (strcmp(optarg,
1507 				    MSG_ORIG(MSG_ARG_STATIC)) == 0)
1508 					ofl->ofl_flags &= ~FLG_OF_DYNLIBS;
1509 				break;
1510 			case 'L':
1511 				DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c,
1512 				    optarg));
1513 				if (ld_add_libdir(ofl, optarg) == S_ERROR)
1514 					return (S_ERROR);
1515 				break;
1516 			case 'N':
1517 				DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c,
1518 				    optarg));
1519 				/*
1520 				 * Record DT_NEEDED string
1521 				 */
1522 				if (!(ofl->ofl_flags & FLG_OF_DYNAMIC)) {
1523 					eprintf(ofl->ofl_lml, ERR_FATAL,
1524 					    MSG_INTL(MSG_ARG_ST_INCOMP),
1525 					    MSG_ORIG(MSG_ARG_CN));
1526 					ofl->ofl_flags |= FLG_OF_FATAL;
1527 				}
1528 				if (((ifl =
1529 				    libld_calloc(1, sizeof (Ifl_desc))) == 0) ||
1530 				    (list_appendc(&ofl->ofl_sos, ifl) == 0))
1531 					return (S_ERROR);
1532 
1533 				ifl->ifl_name = MSG_INTL(MSG_STR_COMMAND);
1534 				ifl->ifl_soname = optarg;
1535 				ifl->ifl_flags = (FLG_IF_NEEDSTR |
1536 				    FLG_IF_FILEREF | FLG_IF_DEPREQD);
1537 
1538 				break;
1539 			case 'D':
1540 				DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c,
1541 				    optarg));
1542 				(void) dbg_setup(optarg, dbg_desc,
1543 				    &ofl->ofl_name, 3);
1544 				break;
1545 			case 'u':
1546 				DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c,
1547 				    optarg));
1548 				if (ld_sym_add_u(optarg, ofl,
1549 				    MSG_STR_COMMAND) == (Sym_desc *)S_ERROR)
1550 					return (S_ERROR);
1551 				break;
1552 			case 'z':
1553 				DBG_CALL(Dbg_args_opts(ofl->ofl_lml, ndx, c,
1554 				    optarg));
1555 				if ((strncmp(optarg, MSG_ORIG(MSG_ARG_LD32),
1556 				    MSG_ARG_LD32_SIZE) == 0) ||
1557 				    (strncmp(optarg, MSG_ORIG(MSG_ARG_LD64),
1558 				    MSG_ARG_LD64_SIZE) == 0)) {
1559 					if (createargv(ofl, 0) == S_ERROR)
1560 						return (S_ERROR);
1561 				} else if (strcmp(optarg,
1562 				    MSG_ORIG(MSG_ARG_ALLEXTRT)) == 0) {
1563 					ofl->ofl_flags1 |= FLG_OF1_ALLEXRT;
1564 					ofl->ofl_flags1 &= ~FLG_OF1_WEAKEXT;
1565 				} else if (strcmp(optarg,
1566 				    MSG_ORIG(MSG_ARG_WEAKEXT)) == 0) {
1567 					ofl->ofl_flags1 |= FLG_OF1_WEAKEXT;
1568 					ofl->ofl_flags1 &= ~FLG_OF1_ALLEXRT;
1569 				} else if (strcmp(optarg,
1570 				    MSG_ORIG(MSG_ARG_DFLEXTRT)) == 0) {
1571 					ofl->ofl_flags1 &=
1572 					    ~(FLG_OF1_ALLEXRT |
1573 					    FLG_OF1_WEAKEXT);
1574 				} else if (strcmp(optarg,
1575 				    MSG_ORIG(MSG_ARG_DIRECT)) == 0) {
1576 					ofl->ofl_flags1 |= FLG_OF1_ZDIRECT;
1577 				} else if (strcmp(optarg,
1578 				    MSG_ORIG(MSG_ARG_NODIRECT)) == 0) {
1579 					ofl->ofl_flags1 &= ~FLG_OF1_ZDIRECT;
1580 					ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
1581 				} else if (strcmp(optarg,
1582 				    MSG_ORIG(MSG_ARG_IGNORE)) == 0) {
1583 					ofl->ofl_flags1 |= FLG_OF1_IGNORE;
1584 				} else if (strcmp(optarg,
1585 				    MSG_ORIG(MSG_ARG_RECORD)) == 0) {
1586 					ofl->ofl_flags1 &= ~FLG_OF1_IGNORE;
1587 				} else if (strcmp(optarg,
1588 				    MSG_ORIG(MSG_ARG_LAZYLOAD)) == 0) {
1589 					ofl->ofl_flags1 |= FLG_OF1_LAZYLD;
1590 				} else if (strcmp(optarg,
1591 				    MSG_ORIG(MSG_ARG_NOLAZYLOAD)) == 0) {
1592 					ofl->ofl_flags1 &= ~ FLG_OF1_LAZYLD;
1593 				} else if (strcmp(optarg,
1594 				    MSG_ORIG(MSG_ARG_GROUPPERM)) == 0) {
1595 					ofl->ofl_flags1 |= FLG_OF1_GRPPRM;
1596 				} else if (strcmp(optarg,
1597 				    MSG_ORIG(MSG_ARG_NOGROUPPERM)) == 0) {
1598 					ofl->ofl_flags1 &= ~FLG_OF1_GRPPRM;
1599 				} else if (strncmp(optarg,
1600 				    MSG_ORIG(MSG_ARG_INITARRAY),
1601 				    MSG_ARG_INITARRAY_SIZE) == 0) {
1602 					if (((sdp = ld_sym_add_u(optarg +
1603 					    MSG_ARG_INITARRAY_SIZE, ofl,
1604 					    MSG_STR_COMMAND)) ==
1605 					    (Sym_desc *)S_ERROR) ||
1606 					    (list_appendc(&ofl->ofl_initarray,
1607 					    sdp) == 0))
1608 						return (S_ERROR);
1609 				} else if (strncmp(optarg,
1610 				    MSG_ORIG(MSG_ARG_FINIARRAY),
1611 				    MSG_ARG_FINIARRAY_SIZE) == 0) {
1612 					if (((sdp = ld_sym_add_u(optarg +
1613 					    MSG_ARG_FINIARRAY_SIZE, ofl,
1614 					    MSG_STR_COMMAND)) ==
1615 					    (Sym_desc *)S_ERROR) ||
1616 					    (list_appendc(&ofl->ofl_finiarray,
1617 					    sdp) == 0))
1618 						return (S_ERROR);
1619 				} else if (strncmp(optarg,
1620 				    MSG_ORIG(MSG_ARG_PREINITARRAY),
1621 				    MSG_ARG_PREINITARRAY_SIZE) == 0) {
1622 					if (((sdp = ld_sym_add_u(optarg +
1623 					    MSG_ARG_PREINITARRAY_SIZE, ofl,
1624 					    MSG_STR_COMMAND)) ==
1625 					    (Sym_desc *)S_ERROR) ||
1626 					    (list_appendc(&ofl->ofl_preiarray,
1627 					    sdp) == 0))
1628 						return (S_ERROR);
1629 				} else if (strncmp(optarg,
1630 				    MSG_ORIG(MSG_ARG_RTLDINFO),
1631 				    MSG_ARG_RTLDINFO_SIZE) == 0) {
1632 					if (((sdp = ld_sym_add_u(optarg +
1633 					    MSG_ARG_RTLDINFO_SIZE, ofl,
1634 					    MSG_STR_COMMAND)) ==
1635 					    (Sym_desc *)S_ERROR) ||
1636 					    (list_appendc(&ofl->ofl_rtldinfo,
1637 					    sdp) == 0))
1638 						return (S_ERROR);
1639 				} else if (strncmp(optarg,
1640 				    MSG_ORIG(MSG_ARG_DTRACE),
1641 				    MSG_ARG_DTRACE_SIZE) == 0) {
1642 					if ((sdp = ld_sym_add_u(optarg +
1643 					    MSG_ARG_DTRACE_SIZE, ofl,
1644 					    MSG_STR_COMMAND)) ==
1645 					    (Sym_desc *)S_ERROR)
1646 						return (S_ERROR);
1647 					ofl->ofl_dtracesym = sdp;
1648 				} else if (strcmp(optarg,
1649 				    MSG_ORIG(MSG_ARG_RESCAN_NOW)) == 0) {
1650 					if (ld_rescan_archives(ofl, 0, ndx) ==
1651 					    S_ERROR)
1652 						return (S_ERROR);
1653 				} else if (strcmp(optarg,
1654 				    MSG_ORIG(MSG_ARG_RESCAN_START)) == 0) {
1655 					ofl->ofl_ars_gsndx = ofl->ofl_arscnt;
1656 					ofl->ofl_ars_gsandx = ndx;
1657 				} else if (strcmp(optarg,
1658 				    MSG_ORIG(MSG_ARG_RESCAN_END)) == 0) {
1659 					if (ld_rescan_archives(ofl, 1, ndx) ==
1660 					    S_ERROR)
1661 						return (S_ERROR);
1662 				}
1663 			default:
1664 				break;
1665 		}
1666 
1667 		/*
1668 		 * Update the argument index for the next getopt() iteration.
1669 		 */
1670 		ndx = optind;
1671 	}
1672 	return (1);
1673 }
1674 
1675 /*
1676  *
1677  * Pass 1 -- process_flags: collects all options and sets flags
1678  */
1679 static uintptr_t
1680 process_flags_com(Ofl_desc *ofl, int argc, char **argv, int *e)
1681 {
1682 	for (; optind < argc; optind++) {
1683 		/*
1684 		 * If we detect some more options return to getopt().
1685 		 * Checking argv[optind][1] against null prevents a forever
1686 		 * loop if an unadorned `-' argument is passed to us.
1687 		 */
1688 		while ((optind < argc) && (argv[optind][0] == '-')) {
1689 			if (argv[optind][1] != '\0') {
1690 				if (parseopt_pass1(ofl, argc, argv, e) ==
1691 				    S_ERROR)
1692 					return (S_ERROR);
1693 			} else if (++optind < argc)
1694 				continue;
1695 		}
1696 		if (optind >= argc)
1697 			break;
1698 		ofl->ofl_objscnt++;
1699 	}
1700 
1701 	/* Did an unterminated archive group run off the end? */
1702 	if (ofl->ofl_ars_gsandx > 0) {
1703 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_AR_GRP_BAD),
1704 		    MSG_INTL(MSG_MARG_AR_GRP_START),
1705 		    MSG_INTL(MSG_MARG_AR_GRP_END));
1706 		ofl->ofl_flags |= FLG_OF_FATAL;
1707 		return (S_ERROR);
1708 	}
1709 
1710 	return (1);
1711 }
1712 
1713 uintptr_t
1714 ld_process_flags(Ofl_desc *ofl, int argc, char **argv)
1715 {
1716 	int	error = 0;	/* Collect all argument errors before exit */
1717 
1718 	if (argc < 2) {
1719 		usage_mesg(FALSE);
1720 		return (S_ERROR);
1721 	}
1722 
1723 	/*
1724 	 * Option handling
1725 	 */
1726 	opterr = 0;
1727 	optind = 1;
1728 	if (process_flags_com(ofl, argc, argv, &error) == S_ERROR)
1729 		return (S_ERROR);
1730 
1731 	/*
1732 	 * Having parsed everything, did we have any errors.
1733 	 */
1734 	if (error) {
1735 		usage_mesg(TRUE);
1736 		return (S_ERROR);
1737 	}
1738 
1739 	return (check_flags(ofl, argc));
1740 }
1741 
1742 /*
1743  * Pass 2 -- process_files: skips the flags collected in pass 1 and processes
1744  * files.
1745  */
1746 static uintptr_t
1747 process_files_com(Ofl_desc *ofl, int argc, char **argv)
1748 {
1749 	for (; optind < argc; optind++) {
1750 		int		fd;
1751 		Ifl_desc	*ifl;
1752 		char		*path;
1753 		Rej_desc	rej = { 0 };
1754 
1755 		/*
1756 		 * If we detect some more options return to getopt().
1757 		 * Checking argv[optind][1] against null prevents a forever
1758 		 * loop if an unadorned `-' argument is passed to us.
1759 		 */
1760 		while ((optind < argc) && (argv[optind][0] == '-')) {
1761 			if (argv[optind][1] != '\0') {
1762 				if (parseopt_pass2(ofl, argc, argv) == S_ERROR)
1763 					return (S_ERROR);
1764 			} else if (++optind < argc)
1765 				continue;
1766 		}
1767 		if (optind >= argc)
1768 			break;
1769 
1770 		path = argv[optind];
1771 		if ((fd = open(path, O_RDONLY)) == -1) {
1772 			int err = errno;
1773 
1774 			eprintf(ofl->ofl_lml, ERR_FATAL,
1775 			    MSG_INTL(MSG_SYS_OPEN), path, strerror(err));
1776 			ofl->ofl_flags |= FLG_OF_FATAL;
1777 			continue;
1778 		}
1779 
1780 		DBG_CALL(Dbg_args_files(ofl->ofl_lml, optind, path));
1781 
1782 		ifl = ld_process_open(path, path, &fd, ofl,
1783 		    (FLG_IF_CMDLINE | FLG_IF_NEEDED), &rej);
1784 		if (fd != -1)
1785 			(void) close(fd);
1786 		if (ifl == (Ifl_desc *)S_ERROR)
1787 			return (S_ERROR);
1788 
1789 		/*
1790 		 * Check for mismatched input.
1791 		 */
1792 		if (rej.rej_type) {
1793 			Conv_reject_desc_buf_t rej_buf;
1794 
1795 			eprintf(ofl->ofl_lml, ERR_FATAL,
1796 			    MSG_INTL(reject[rej.rej_type]),
1797 			    rej.rej_name ? rej.rej_name :
1798 			    MSG_INTL(MSG_STR_UNKNOWN),
1799 			    conv_reject_desc(&rej, &rej_buf,
1800 			    ld_targ.t_m.m_mach));
1801 			ofl->ofl_flags |= FLG_OF_FATAL;
1802 			return (1);
1803 		}
1804 	}
1805 	return (1);
1806 }
1807 
1808 uintptr_t
1809 ld_process_files(Ofl_desc *ofl, int argc, char **argv)
1810 {
1811 	/*
1812 	 * Process command line files (taking into account any applicable
1813 	 * preseeding flags).  Return if any fatal errors have occurred.
1814 	 */
1815 	opterr = 0;
1816 	optind = 1;
1817 	if (process_files_com(ofl, argc, argv) == S_ERROR)
1818 		return (S_ERROR);
1819 	if (ofl->ofl_flags & FLG_OF_FATAL)
1820 		return (1);
1821 
1822 	/*
1823 	 * Now that all command line files have been processed see if there are
1824 	 * any additional `needed' shared object dependencies.
1825 	 */
1826 	if (ofl->ofl_soneed.head)
1827 		if (ld_finish_libs(ofl) == S_ERROR)
1828 			return (S_ERROR);
1829 
1830 	/*
1831 	 * If rescanning archives is enabled, do so now to determine whether
1832 	 * there might still be members extracted to satisfy references from any
1833 	 * explicit objects.  Continue until no new objects are extracted.  Note
1834 	 * that this pass is carried out *after* processing any implicit objects
1835 	 * (above) as they may already have resolved any undefined references
1836 	 * from any explicit dependencies.
1837 	 */
1838 	if (ofl->ofl_flags1 & FLG_OF1_RESCAN) {
1839 		if (ld_rescan_archives(ofl, 0, argc) == S_ERROR)
1840 			return (S_ERROR);
1841 		if (ofl->ofl_flags & FLG_OF_FATAL)
1842 			return (1);
1843 	}
1844 
1845 	/*
1846 	 * If debugging, provide statistics on each archives extraction, or flag
1847 	 * any archive that has provided no members.  Note that this could be a
1848 	 * nice place to free up much of the archive infrastructure, as we've
1849 	 * extracted any members we need.  However, as we presently don't free
1850 	 * anything under ld(1) there's not much point in proceeding further.
1851 	 */
1852 	DBG_CALL(Dbg_statistics_ar(ofl));
1853 
1854 	/*
1855 	 * If any version definitions have been established, either via input
1856 	 * from a mapfile or from the input relocatable objects, make sure any
1857 	 * version dependencies are satisfied, and version symbols created.
1858 	 */
1859 	if (ofl->ofl_verdesc.head)
1860 		if (ld_vers_check_defs(ofl) == S_ERROR)
1861 			return (S_ERROR);
1862 
1863 	/*
1864 	 * If segment ordering was specified (using mapfile) verify things
1865 	 * are ok.
1866 	 */
1867 	if (ofl->ofl_flags & FLG_OF_SEGORDER)
1868 		ld_ent_check(ofl);
1869 
1870 	return (1);
1871 }
1872 
1873 uintptr_t
1874 ld_init_strings(Ofl_desc *ofl)
1875 {
1876 	uint_t	stflags;
1877 
1878 	if (ofl->ofl_flags1 & FLG_OF1_NCSTTAB)
1879 		stflags = 0;
1880 	else
1881 		stflags = FLG_STNEW_COMPRESS;
1882 
1883 	if (((ofl->ofl_shdrsttab = st_new(stflags)) == 0) ||
1884 	    ((ofl->ofl_strtab = st_new(stflags)) == 0) ||
1885 	    ((ofl->ofl_dynstrtab = st_new(stflags)) == 0))
1886 		return (S_ERROR);
1887 
1888 	return (0);
1889 }
1890