xref: /titanic_41/usr/src/cmd/sgs/libld/common/util.c (revision ecd7f3e388a11eae4ace93c308218a3c541f6c05)
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) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  *	Copyright (c) 1988 AT&T
28  *	  All Rights Reserved
29  */
30 
31 /*
32  * Utility functions
33  */
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <sys/types.h>
40 #include <sys/mman.h>
41 #include <errno.h>
42 #include <sgs.h>
43 #include <libintl.h>
44 #include <debug.h>
45 #include "msg.h"
46 #include "_libld.h"
47 
48 /*
49  * libld_malloc() and dz_map() are used for both performance and for ease of
50  * programming:
51  *
52  * Performance:
53  *	The link-edit is a short lived process which doesn't really free much
54  *	of the dynamic memory that it requests.  Because of this, it is more
55  *	important to optimize for quick memory allocations than the
56  *	re-usability of the memory.
57  *
58  *	By also mmaping blocks of pages in from /dev/zero we don't need to
59  *	waste the overhead of zeroing out these pages for calloc() requests.
60  *
61  * Memory Management:
62  *	By doing all libld memory management through the ld_malloc routine
63  *	it's much easier to free up all memory at the end by simply unmaping
64  *	all of the blocks that were mapped in through dz_map().  This is much
65  *	simpler then trying to track all of the libld structures that were
66  *	dynamically allocate and are actually pointers into the ELF files.
67  *
68  *	It's important that we can free up all of our dynamic memory because
69  *	libld is used by ld.so.1 when it performs dlopen()'s of relocatable
70  *	objects.
71  *
72  * Format:
73  *	The memory blocks for each allocation store the size of the allocation
74  *	in the first 8 bytes of the block.  The pointer that is returned by
75  *	libld_malloc() is actually the address of (block + 8):
76  *
77  *		(addr - 8)	block_size
78  *		(addr)		<allocated block>
79  *
80  *	The size is retained in order to implement realloc(), and to perform
81  *	the required memcpy().  8 bytes are uses, as the memory area returned
82  *	by libld_malloc() must be 8 byte-aligned.  Even in a 32-bit environment,
83  *	u_longlog_t pointers are employed.
84  *
85  * Map anonymous memory via MAP_ANON (added in Solaris 8).
86  */
87 static void *
88 dz_map(size_t size)
89 {
90 	void	*addr;
91 
92 	if ((addr = mmap(0, size, (PROT_READ | PROT_WRITE | PROT_EXEC),
93 	    (MAP_PRIVATE | MAP_ANON), -1, 0)) == MAP_FAILED) {
94 		int	err = errno;
95 		eprintf(NULL, ERR_FATAL, MSG_INTL(MSG_SYS_MMAPANON),
96 		    strerror(err));
97 		return (MAP_FAILED);
98 	}
99 	return (addr);
100 }
101 
102 void *
103 libld_malloc(size_t size)
104 {
105 	Ld_heap		*chp = ld_heap;
106 	void		*vptr;
107 	size_t		asize = size + HEAPALIGN;
108 
109 	/*
110 	 * If this is the first allocation, or the allocation request is greater
111 	 * than the current free space available, allocate a new heap.
112 	 */
113 	if ((chp == NULL) ||
114 	    (((size_t)chp->lh_end - (size_t)chp->lh_free) <= asize)) {
115 		Ld_heap	*nhp;
116 		size_t	hsize = (size_t)S_ROUND(sizeof (Ld_heap), HEAPALIGN);
117 		size_t	tsize = (size_t)S_ROUND((asize + hsize), HEAPALIGN);
118 
119 		/*
120 		 * Allocate a block that is at minimum 'HEAPBLOCK' size
121 		 */
122 		if (tsize < HEAPBLOCK)
123 			tsize = HEAPBLOCK;
124 
125 		if ((nhp = dz_map(tsize)) == MAP_FAILED)
126 			return (NULL);
127 
128 		nhp->lh_next = chp;
129 		nhp->lh_free = (void *)((size_t)nhp + hsize);
130 		nhp->lh_end = (void *)((size_t)nhp + tsize);
131 
132 		ld_heap = chp = nhp;
133 	}
134 	vptr = chp->lh_free;
135 
136 	/*
137 	 * Assign size to head of allocated block (used by realloc), and
138 	 * memory arena as then next 8-byte aligned offset.
139 	 */
140 	*((size_t *)vptr) = size;
141 	vptr = (void *)((size_t)vptr + HEAPALIGN);
142 
143 	/*
144 	 * Increment free to point to next available block
145 	 */
146 	chp->lh_free = (void *)S_ROUND((size_t)chp->lh_free + asize,
147 	    HEAPALIGN);
148 
149 	return (vptr);
150 }
151 
152 void *
153 libld_realloc(void *ptr, size_t size)
154 {
155 	size_t	psize;
156 	void	*vptr;
157 
158 	if (ptr == NULL)
159 		return (libld_malloc(size));
160 
161 	/*
162 	 * Size of the allocated blocks is stored *just* before the blocks
163 	 * address.
164 	 */
165 	psize = *((size_t *)((size_t)ptr - HEAPALIGN));
166 
167 	/*
168 	 * If the block actually fits then just return.
169 	 */
170 	if (size <= psize)
171 		return (ptr);
172 
173 	if ((vptr = libld_malloc(size)) != NULL)
174 		(void) memcpy(vptr, ptr, psize);
175 
176 	return (vptr);
177 }
178 
179 void
180 /* ARGSUSED 0 */
181 libld_free(void *ptr)
182 {
183 }
184 
185 /*
186  * Determine if a shared object definition structure already exists and if
187  * not create one.  These definitions provide for recording information
188  * regarding shared objects that are still to be processed.  Once processed
189  * shared objects are maintained on the ofl_sos list.  The information
190  * recorded in this structure includes:
191  *
192  *  o	DT_USED requirements.  In these cases definitions are added during
193  *	mapfile processing of `-' entries (see map_dash()).
194  *
195  *  o	implicit NEEDED entries.  As shared objects are processed from the
196  *	command line so any of their dependencies are recorded in these
197  *	structures for later processing (see process_dynamic()).
198  *
199  *  o	version requirements.  Any explicit shared objects that have version
200  *	dependencies on other objects have their version requirements recorded.
201  *	In these cases definitions are added during mapfile processing of `-'
202  *	entries (see map_dash()).  Also, shared objects may have versioning
203  *	requirements on their NEEDED entries.  These cases are added during
204  *	their version processing (see vers_need_process()).
205  *
206  *	Note: Both process_dynamic() and vers_need_process() may generate the
207  *	initial version definition structure because you can't rely on what
208  *	section (.dynamic or .SUNW_version) may be processed first from	any
209  *	input file.
210  */
211 Sdf_desc *
212 sdf_find(const char *name, APlist *alp)
213 {
214 	Aliste		idx;
215 	Sdf_desc	*sdf;
216 
217 	for (APLIST_TRAVERSE(alp, idx, sdf))
218 		if (strcmp(name, sdf->sdf_name) == 0)
219 			return (sdf);
220 
221 	return (NULL);
222 }
223 
224 Sdf_desc *
225 sdf_add(const char *name, APlist **alpp)
226 {
227 	Sdf_desc	*sdf;
228 
229 	if ((sdf = libld_calloc(sizeof (Sdf_desc), 1)) == NULL)
230 		return ((Sdf_desc *)S_ERROR);
231 
232 	sdf->sdf_name = name;
233 
234 	if (aplist_append(alpp, sdf, AL_CNT_OFL_LIBS) == NULL)
235 		return ((Sdf_desc *)S_ERROR);
236 
237 	return (sdf);
238 }
239 
240 /*
241  * Add a string, separated by a colon, to an existing string.  Typically used
242  * to maintain filter, rpath and audit names, of which there is normally only
243  * one string supplied anyway.
244  */
245 char *
246 add_string(char *old, char *str)
247 {
248 	char	*new;
249 
250 	if (old) {
251 		char	*_str;
252 		size_t	len;
253 
254 		/*
255 		 * If an original string exists, make sure this new string
256 		 * doesn't get duplicated.
257 		 */
258 		if ((_str = strstr(old, str)) != NULL) {
259 			if (((_str == old) ||
260 			    (*(_str - 1) == *(MSG_ORIG(MSG_STR_COLON)))) &&
261 			    (_str += strlen(str)) &&
262 			    ((*_str == '\0') ||
263 			    (*_str == *(MSG_ORIG(MSG_STR_COLON)))))
264 				return (old);
265 		}
266 
267 		len = strlen(old) + strlen(str) + 2;
268 		if ((new = libld_calloc(1, len)) == NULL)
269 			return ((char *)S_ERROR);
270 		(void) snprintf(new, len, MSG_ORIG(MSG_FMT_COLPATH), old, str);
271 	} else {
272 		if ((new = libld_malloc(strlen(str) + 1)) == NULL)
273 			return ((char *)S_ERROR);
274 		(void) strcpy(new, str);
275 	}
276 
277 	return (new);
278 }
279 
280 /*
281  * The GNU ld '-wrap=XXX' and '--wrap=XXX' options correspond to our
282  * '-z wrap=XXX'. When str2chr() does this conversion, we end up with
283  * the return character set to 'z' and optarg set to 'XXX'. This callback
284  * changes optarg to include the missing wrap= prefix.
285  *
286  * exit:
287  *	Returns c on success, or '?' on error.
288  */
289 static int
290 str2chr_wrap_cb(int c)
291 {
292 	char    *str;
293 	size_t  len = MSG_ARG_WRAP_SIZE + strlen(optarg) + 1;
294 
295 	if ((str = libld_malloc(len)) == NULL)
296 		return ('?');
297 	(void) snprintf(str, len, MSG_ORIG(MSG_FMT_STRCAT),
298 	    MSG_ORIG(MSG_ARG_WRAP), optarg);
299 	optarg = str;
300 	return (c);
301 }
302 
303 /*
304  * Determine whether this string, possibly with an associated option, should be
305  * translated to an option character.  If so, update the optind and optarg
306  * as described for short options in getopt(3c).
307  *
308  * entry:
309  *	lml - Link map list for debug messages
310  *	ndx - Starting optind for current item
311  *	argc, argv - Command line arguments
312  *	arg - Option to be examined
313  *	c, opt - Option character (c) and corresponding long name (opt)
314  *	optsz - 0 if option does not accept a value. If option does
315  *		accept a value, strlen(opt), giving the offset to the
316  *		value if the option and value are combined in one string.
317  *	cbfunc - NULL, or pointer to function to call if a translation is
318  *		successful.
319  */
320 static int
321 str2chr(Lm_list *lml, int ndx, int argc, char **argv, char *arg, int c,
322     const char *opt, size_t optsz, int cbfunc(int))
323 {
324 	if (optsz == 0) {
325 		/*
326 		 * Compare a single option (ie. there's no associated option
327 		 * argument).
328 		 */
329 		if (strcmp(arg, opt) == 0) {
330 			DBG_CALL(Dbg_args_str2chr(lml, ndx, opt, c));
331 			optind += 1;
332 			return (c);
333 		}
334 	} else if ((strcmp(arg, opt) == 0) ||
335 	    ((arg[optsz] == '=') && strncmp(arg, opt, optsz) == 0)) {
336 		/*
337 		 * Otherwise, compare the option name, which may be
338 		 * concatenated with the option argument.
339 		 */
340 		DBG_CALL(Dbg_args_str2chr(lml, ndx, opt, c));
341 
342 		if (arg[optsz] == '\0') {
343 			/*
344 			 * Optarg is the next argument (white space separated).
345 			 * Make sure an optarg is available, and if not return
346 			 * a failure to prevent any fall-through to the generic
347 			 * getopt() processing.
348 			 */
349 			if ((++optind + 1) > argc) {
350 				return ('?');
351 			}
352 			optarg = argv[optind];
353 			optind++;
354 		} else {
355 			/*
356 			 * GNU option/option argument pairs can be represented
357 			 * with a "=" separator.  If this is the case, remove
358 			 * the separator.
359 			 */
360 			optarg = &arg[optsz];
361 			optind++;
362 			if (*optarg == '=') {
363 				if (*(++optarg) == '\0')
364 					return ('?');
365 			}
366 		}
367 
368 		if (cbfunc != NULL)
369 			c = (*cbfunc)(c);
370 
371 		return (c);
372 	}
373 	return (0);
374 }
375 
376 /*
377  * Parse an individual option.  The intent of this function is to determine if
378  * any known, non-Solaris options have been passed to ld(1).  This condition
379  * can occur as a result of build configuration tools, because of users
380  * familiarity with other systems, or simply the users preferences.  If a known
381  * non-Solaris option can be determined, translate that option into the Solaris
382  * counterpart.
383  *
384  * This function will probably never be a complete solution, as new, non-Solaris
385  * options are discovered, their translation will have to be added.  Other
386  * non-Solaris options are incompatible with the Solaris link-editor, and will
387  * never be recognized.  We support what we can.
388  */
389 int
390 ld_getopt(Lm_list *lml, int ndx, int argc, char **argv)
391 {
392 	int	c;
393 
394 	if ((optind < argc) && argv[optind] && (argv[optind][0] == '-')) {
395 		char	*arg = &argv[optind][1];
396 
397 		switch (*arg) {
398 		case 'r':
399 			/* Translate -rpath <optarg> to -R <optarg> */
400 			if ((c = str2chr(lml, ndx, argc, argv, arg, 'R',
401 			    MSG_ORIG(MSG_ARG_T_RPATH),
402 			    MSG_ARG_T_RPATH_SIZE, NULL)) != 0) {
403 				return (c);
404 			}
405 			break;
406 		case 's':
407 			/* Translate -shared to -G */
408 			if ((c = str2chr(lml, ndx, argc, argv, arg, 'G',
409 			    MSG_ORIG(MSG_ARG_T_SHARED), 0, NULL)) != 0) {
410 				return (c);
411 
412 			/* Translate -soname <optarg> to -h <optarg> */
413 			} else if ((c = str2chr(lml, ndx, argc, argv, arg, 'h',
414 			    MSG_ORIG(MSG_ARG_T_SONAME),
415 			    MSG_ARG_T_SONAME_SIZE, NULL)) != 0) {
416 				return (c);
417 			}
418 			break;
419 		case 'w':
420 			/* Translate -wrap to -z wrap= */
421 			if ((c = str2chr(lml, ndx, argc, argv, arg, 'z',
422 			    MSG_ORIG(MSG_ARG_T_WRAP) + 1,
423 			    MSG_ARG_T_WRAP_SIZE - 1, str2chr_wrap_cb)) != 0) {
424 				return (c);
425 			}
426 			break;
427 		case '(':
428 			/*
429 			 * Translate -( to -z rescan-start
430 			 */
431 			if ((c = str2chr(lml, ndx, argc, argv,
432 			    arg, 'z', MSG_ORIG(MSG_ARG_T_OPAR), 0, NULL)) !=
433 			    0) {
434 				optarg = (char *)MSG_ORIG(MSG_ARG_RESCAN_START);
435 				return (c);
436 			}
437 			break;
438 		case ')':
439 			/*
440 			 * Translate -) to -z rescan-end
441 			 */
442 			if ((c = str2chr(lml, ndx, argc, argv,
443 			    arg, 'z', MSG_ORIG(MSG_ARG_T_CPAR), 0, NULL)) !=
444 			    0) {
445 				optarg = (char *)MSG_ORIG(MSG_ARG_RESCAN_END);
446 				return (c);
447 			}
448 			break;
449 		case '-':
450 			switch (*(arg + 1)) {
451 			case 'a':
452 				/*
453 				 * Translate --allow-multiple-definition to
454 				 * -zmuldefs
455 				 */
456 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'z',
457 				    MSG_ORIG(MSG_ARG_T_MULDEFS), 0, NULL)) !=
458 				    0) {
459 					optarg =
460 					    (char *)MSG_ORIG(MSG_ARG_MULDEFS);
461 					return (c);
462 
463 				/*
464 				 * Translate --auxiliary <optarg> to
465 				 * -f <optarg>
466 				 */
467 				} else if ((c = str2chr(lml, argc, ndx, argv,
468 				    arg, 'f', MSG_ORIG(MSG_ARG_T_AUXFLTR),
469 				    MSG_ARG_T_AUXFLTR_SIZE, NULL)) != 0) {
470 					return (c);
471 				}
472 				break;
473 			case 'd':
474 				/*
475 				 * Translate --dynamic-linker <optarg> to
476 				 * -I <optarg>
477 				 */
478 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'I',
479 				    MSG_ORIG(MSG_ARG_T_INTERP),
480 				    MSG_ARG_T_INTERP_SIZE, NULL)) != 0) {
481 					return (c);
482 				}
483 				break;
484 			case 'e':
485 				/* Translate --entry <optarg> to -e <optarg> */
486 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'e',
487 				    MSG_ORIG(MSG_ARG_T_ENTRY),
488 				    MSG_ARG_T_ENTRY_SIZE, NULL)) != 0) {
489 					return (c);
490 				}
491 				/*
492 				 * Translate --end-group to -z rescan-end
493 				 */
494 				if ((c = str2chr(lml, ndx, argc, argv,
495 				    arg, 'z', MSG_ORIG(MSG_ARG_T_ENDGROUP),
496 				    0, NULL)) != 0) {
497 					optarg = (char *)
498 					    MSG_ORIG(MSG_ARG_RESCAN_END);
499 					return (c);
500 				}
501 				break;
502 			case 'f':
503 				/*
504 				 * Translate --fatal-warnings to
505 				 * -z fatal-warnings.
506 				 */
507 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'z',
508 				    MSG_ORIG(MSG_ARG_T_FATWARN),
509 				    0, NULL)) != 0) {
510 					optarg = (char *)
511 					    MSG_ORIG(MSG_ARG_FATWARN);
512 					return (c);
513 				}
514 				/* Translate --filter <optarg> to -F <optarg> */
515 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'F',
516 				    MSG_ORIG(MSG_ARG_T_STDFLTR),
517 				    MSG_ARG_T_STDFLTR_SIZE, NULL)) != 0) {
518 					return (c);
519 				}
520 				break;
521 			case 'h':
522 				/* Translate --help to -zhelp */
523 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'z',
524 				    MSG_ORIG(MSG_ARG_T_HELP), 0, NULL)) !=
525 				    0) {
526 					optarg = (char *)MSG_ORIG(MSG_ARG_HELP);
527 					return (c);
528 				}
529 				break;
530 			case 'l':
531 				/*
532 				 * Translate --library <optarg> to -l <optarg>
533 				 */
534 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'l',
535 				    MSG_ORIG(MSG_ARG_T_LIBRARY),
536 				    MSG_ARG_T_LIBRARY_SIZE, NULL)) != 0) {
537 					return (c);
538 
539 				/*
540 				 * Translate --library-path <optarg> to
541 				 * -L <optarg>
542 				 */
543 				} else if ((c = str2chr(lml, ndx, argc, argv,
544 				    arg, 'L', MSG_ORIG(MSG_ARG_T_LIBPATH),
545 				    MSG_ARG_T_LIBPATH_SIZE, NULL)) != 0) {
546 					return (c);
547 				}
548 				break;
549 			case 'n':
550 				/*
551 				 * Translate --no-fatal-warnings to
552 				 * -z nofatal-warnings.
553 				 */
554 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'z',
555 				    MSG_ORIG(MSG_ARG_T_NOFATWARN),
556 				    0, NULL)) != 0) {
557 					optarg = (char *)
558 					    MSG_ORIG(MSG_ARG_NOFATWARN);
559 					return (c);
560 				}
561 
562 				/* Translate --no-undefined to -zdefs */
563 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'z',
564 				    MSG_ORIG(MSG_ARG_T_NOUNDEF), 0, NULL)) !=
565 				    0) {
566 					optarg = (char *)MSG_ORIG(MSG_ARG_DEFS);
567 					return (c);
568 
569 				/*
570 				 * Translate --no-whole-archive to
571 				 * -z defaultextract
572 				 */
573 				} else if ((c = str2chr(lml, ndx, argc, argv,
574 				    arg, 'z', MSG_ORIG(MSG_ARG_T_NOWHOLEARC),
575 				    0, NULL)) != 0) {
576 					optarg =
577 					    (char *)MSG_ORIG(MSG_ARG_DFLEXTRT);
578 					return (c);
579 				}
580 				break;
581 			case 'o':
582 				/* Translate --output <optarg> to -o <optarg> */
583 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'o',
584 				    MSG_ORIG(MSG_ARG_T_OUTPUT),
585 				    MSG_ARG_T_OUTPUT_SIZE, NULL)) != 0) {
586 					return (c);
587 				}
588 				break;
589 			case 'r':
590 				/* Translate --relocatable to -r */
591 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'r',
592 				    MSG_ORIG(MSG_ARG_T_RELOCATABLE), 0,
593 				    NULL)) != 0) {
594 					return (c);
595 				}
596 				break;
597 			case 's':
598 				/* Translate --strip-all to -s */
599 				if ((c = str2chr(lml, ndx, argc, argv, arg, 's',
600 				    MSG_ORIG(MSG_ARG_T_STRIP), 0, NULL)) !=
601 				    0) {
602 					return (c);
603 				}
604 				/*
605 				 * Translate --start-group to -z rescan-start
606 				 */
607 				if ((c = str2chr(lml, ndx, argc, argv,
608 				    arg, 'z', MSG_ORIG(MSG_ARG_T_STARTGROUP),
609 				    0, NULL)) != 0) {
610 					optarg = (char *)
611 					    MSG_ORIG(MSG_ARG_RESCAN_START);
612 					return (c);
613 				}
614 				break;
615 			case 'u':
616 				/*
617 				 * Translate --undefined <optarg> to
618 				 * -u <optarg>
619 				 */
620 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'u',
621 				    MSG_ORIG(MSG_ARG_T_UNDEF),
622 				    MSG_ARG_T_UNDEF_SIZE, NULL)) != 0) {
623 					return (c);
624 				}
625 				break;
626 			case 'v':
627 				/* Translate --version to -V */
628 				if ((c = str2chr(lml, ndx, argc, argv, arg, 'V',
629 				    MSG_ORIG(MSG_ARG_T_VERSION), 0, NULL)) !=
630 				    0) {
631 					return (c);
632 				}
633 				break;
634 			case 'w':
635 				/*
636 				 * Translate --whole-archive to -z alltextract
637 				 */
638 				if ((c = str2chr(lml, ndx, argc, argv,
639 				    arg, 'z', MSG_ORIG(MSG_ARG_T_WHOLEARC),
640 				    0, NULL)) != 0) {
641 					optarg =
642 					    (char *)MSG_ORIG(MSG_ARG_ALLEXTRT);
643 					return (c);
644 				}
645 				/*
646 				 * Translate --wrap to -z wrap=
647 				 */
648 				if ((c = str2chr(lml, ndx, argc, argv,
649 				    arg, 'z', MSG_ORIG(MSG_ARG_T_WRAP),
650 				    MSG_ARG_T_WRAP_SIZE, str2chr_wrap_cb)) !=
651 				    0) {
652 					return (c);
653 				}
654 				break;
655 			}
656 			break;
657 		}
658 	}
659 
660 	if ((c = getopt(argc, argv, MSG_ORIG(MSG_STR_OPTIONS))) != -1) {
661 		/*
662 		 * It is possible that a "-Wl," argument has been used to
663 		 * specify an option.  This isn't advertized ld(1) syntax, but
664 		 * compiler drivers and configuration tools, have been known to
665 		 * pass this compiler option to ld(1).  Strip off the "-Wl,"
666 		 * prefix and pass the option through.
667 		 */
668 		if ((c == 'W') && (strncmp(optarg,
669 		    MSG_ORIG(MSG_ARG_T_WL), MSG_ARG_T_WL_SIZE) == 0)) {
670 			DBG_CALL(Dbg_args_Wldel(lml, ndx, optarg));
671 			c = optarg[MSG_ARG_T_WL_SIZE];
672 			optarg += MSG_ARG_T_WL_SIZE + 1;
673 		}
674 	}
675 
676 	return (c);
677 }
678 
679 /*
680  * A compare routine for Isd_node AVL trees.
681  */
682 int
683 isdavl_compare(const void *n1, const void *n2)
684 {
685 	uint_t		hash1, hash2;
686 	const char	*st1, *st2;
687 	int		rc;
688 
689 	hash1 = ((Isd_node *)n1)->isd_hash;
690 	hash2 = ((Isd_node *)n2)->isd_hash;
691 
692 	if (hash1 > hash2)
693 		return (1);
694 	if (hash1 < hash2)
695 		return (-1);
696 
697 	st1 = ((Isd_node *)n1)->isd_name;
698 	st2 = ((Isd_node *)n2)->isd_name;
699 
700 	rc = strcmp(st1, st2);
701 	if (rc > 0)
702 		return (1);
703 	if (rc < 0)
704 		return (-1);
705 	return (0);
706 }
707 
708 /*
709  * Messaging support - funnel everything through dgettext().
710  */
711 const char *
712 _libld_msg(Msg mid)
713 {
714 	return (dgettext(MSG_ORIG(MSG_SUNW_OST_SGS), MSG_ORIG(mid)));
715 }
716 
717 /*
718  * Determine whether a symbol name should be demangled.
719  */
720 const char *
721 demangle(const char *name)
722 {
723 	if (demangle_flag)
724 		return (Elf_demangle_name(name));
725 	else
726 		return (name);
727 }
728 
729 /*
730  * Compare a series of platform or machine hardware names.
731  */
732 int
733 cap_names_match(Alist *alp1, Alist *alp2)
734 {
735 	Capstr		*capstr1;
736 	Aliste		idx1;
737 	int		match = 0;
738 	Word		nitems;
739 
740 	if ((nitems = alist_nitems(alp1)) != alist_nitems(alp2))
741 		return (1);
742 
743 	for (ALIST_TRAVERSE(alp1, idx1, capstr1)) {
744 		Capstr		*capstr2;
745 		Aliste 		idx2;
746 
747 		for (ALIST_TRAVERSE(alp2, idx2, capstr2)) {
748 			if (strcmp(capstr1->cs_str, capstr2->cs_str))
749 				continue;
750 
751 			match++;
752 			break;
753 		}
754 	}
755 
756 	if (nitems == match)
757 		return (0);
758 
759 	return (1);
760 }
761