xref: /titanic_50/usr/src/cmd/sgs/libld/common/map.c (revision 4a8d0ea71c9a4e51c6a916a083ced6b499eb207f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*
31  * Map file parsing.
32  */
33 #include	<fcntl.h>
34 #include	<string.h>
35 #include	<stdio.h>
36 #include	<unistd.h>
37 #include	<sys/stat.h>
38 #include	<errno.h>
39 #include	<limits.h>
40 #include	<dirent.h>
41 #include	<ctype.h>
42 #include	<elfcap.h>
43 #include	<debug.h>
44 #include	"msg.h"
45 #include	"_libld.h"
46 
47 #if	defined(_ELF64)
48 #define	STRTOADDR	strtoull
49 #define	XWORD_MAX	ULLONG_MAX
50 #else	/* Elf32 */
51 #define	STRTOADDR	strtoul
52 #define	XWORD_MAX	UINT_MAX
53 #endif	/* _ELF64 */
54 
55 /* Possible return values from gettoken */
56 typedef enum {
57 	TK_ERROR =	-1,	/* Error in lexical analysis */
58 	TK_STRING =	0,
59 	TK_COLON =	1,
60 	TK_SEMICOLON =	2,
61 	TK_EQUAL =	3,
62 	TK_ATSIGN =	4,
63 	TK_DASH =	5,
64 	TK_LEFTBKT =	6,
65 	TK_RIGHTBKT =	7,
66 	TK_PIPE =	8,
67 	TK_EOF =	9
68 } Token;
69 
70 
71 static char	*Mapspace;	/* Malloc space holding mapfile. */
72 static ulong_t	Line_num;	/* Current mapfile line number. */
73 static char	*Start_tok;	/* First character of current token. */
74 static char	*nextchr;	/* Next char in mapfile to examine. */
75 
76 /*
77  * Convert a string to lowercase.
78  */
79 static void
80 lowercase(char *str)
81 {
82 	while (*str = tolower(*str))
83 		str++;
84 }
85 
86 /*
87  * Get a token from the mapfile.
88  *
89  * entry:
90  *	ofl - Output file descriptor
91  *	mapfile - Name of mapfile
92  *	eof_ok - If False, end of file causes a premature EOF error to be
93  *		issued. If True, TK_EOF is returned quietly.
94  */
95 static Token
96 gettoken(Ofl_desc *ofl, const char *mapfile, int eof_ok)
97 {
98 	static char	oldchr = '\0';	/* Char at end of current token. */
99 	char		*end;		/* End of the current token. */
100 
101 	/* Cycle through the characters looking for tokens. */
102 	for (;;) {
103 		if (oldchr != '\0') {
104 			*nextchr = oldchr;
105 			oldchr = '\0';
106 		}
107 		if (!isascii(*nextchr) ||
108 		    (!isprint(*nextchr) && !isspace(*nextchr) &&
109 		    (*nextchr != '\0'))) {
110 			eprintf(ofl->ofl_lml, ERR_FATAL,
111 			    MSG_INTL(MSG_MAP_ILLCHAR), mapfile,
112 			    EC_XWORD(Line_num), *((uchar_t *)nextchr));
113 			return (TK_ERROR);
114 		}
115 		switch (*nextchr) {
116 		case '\0':	/* End of file. */
117 			if (!eof_ok)
118 				eprintf(ofl->ofl_lml, ERR_FATAL,
119 				    MSG_INTL(MSG_MAP_PREMEOF), mapfile,
120 				    EC_XWORD(Line_num));
121 			return (TK_EOF);
122 
123 		case ' ':	/* White space. */
124 		case '\t':
125 			nextchr++;
126 			break;
127 		case '\n':	/* White space too, but bump line number. */
128 			nextchr++;
129 			Line_num++;
130 			break;
131 		case '#':	/* Comment. */
132 			while (*nextchr != '\n' && *nextchr != '\0')
133 				nextchr++;
134 			break;
135 		case ':':
136 			nextchr++;
137 			return (TK_COLON);
138 		case ';':
139 			nextchr++;
140 			return (TK_SEMICOLON);
141 		case '=':
142 			nextchr++;
143 			return (TK_EQUAL);
144 		case '@':
145 			nextchr++;
146 			return (TK_ATSIGN);
147 		case '-':
148 			nextchr++;
149 			return (TK_DASH);
150 		case '|':
151 			nextchr++;
152 			return (TK_PIPE);
153 		case '{':
154 			nextchr++;
155 			return (TK_LEFTBKT);
156 		case '}':
157 			nextchr++;
158 			return (TK_RIGHTBKT);
159 		case '"':
160 			Start_tok = ++nextchr;
161 			if (((end = strpbrk(nextchr,
162 			    MSG_ORIG(MSG_MAP_TOK_1))) == NULL) ||
163 			    (*end != '"')) {
164 				eprintf(ofl->ofl_lml, ERR_FATAL,
165 				    MSG_INTL(MSG_MAP_NOTERM), mapfile,
166 				    EC_XWORD(Line_num));
167 				return (TK_ERROR);
168 			}
169 			*end = '\0';
170 			nextchr = end + 1;
171 			return (TK_STRING);
172 		default:	/* string. */
173 			Start_tok = nextchr;		/* CSTYLED */
174 			end = strpbrk(nextchr, MSG_ORIG(MSG_MAP_TOK_2));
175 			if (end == NULL)
176 				nextchr = Start_tok + strlen(Start_tok);
177 			else {
178 				nextchr = end;
179 				oldchr = *nextchr;
180 				*nextchr = '\0';
181 			}
182 			return (TK_STRING);
183 		}
184 	}
185 }
186 
187 /*
188  * Process a hardware/software capabilities segment declaration definition.
189  *	hwcap_1	= val,... [ OVERRIDE ]
190  *	sfcap_1	= val,... [ OVERRIDE ]
191  *
192  * The values can be defined as a list of machine specify tokens, or numerics.
193  * Tokens are representations of the sys/auxv_$MACH.h capabilities, for example:
194  *
195  *	#define AV_386_FPU 0x0001	is represented as	FPU
196  *	#define AV_386_TSC 0x0002	 "    "    "   " 	TSC
197  *
198  * Or, the above two capabilities could be represented as V0x3.  Note, the
199  * OVERRIDE flag is used to insure that only those values provided via this
200  * mapfile entry are recorded in the final image, ie. this overrides any
201  * hardware capabilities that may be defined in the objects read as part of this
202  * link-edit.  Specifying:
203  *
204  *	V0x0 OVERRIDE
205  *
206  * effectively removes any capabilities information from the final image.
207  */
208 static uintptr_t
209 map_cap(const char *mapfile, Word type, Ofl_desc *ofl)
210 {
211 	Token	tok;			/* Current token. */
212 	Xword	number;
213 	int	used = 0;
214 
215 	while ((tok = gettoken(ofl, mapfile, 0)) != TK_SEMICOLON) {
216 		if (tok != TK_STRING) {
217 			if (tok != TK_ERROR)
218 				eprintf(ofl->ofl_lml, ERR_FATAL,
219 				    MSG_INTL(MSG_MAP_EXPSEGATT), mapfile,
220 				    EC_XWORD(Line_num));
221 			return (S_ERROR);
222 		}
223 
224 		lowercase(Start_tok);
225 
226 		/*
227 		 * First, determine if the token represents the reserved
228 		 * OVERRIDE keyword.
229 		 */
230 		if (strncmp(Start_tok, MSG_ORIG(MSG_MAP_OVERRIDE),
231 		    MSG_MAP_OVERRIDE_SIZE) == 0) {
232 			if (type == CA_SUNW_HW_1)
233 				ofl->ofl_flags1 |= FLG_OF1_OVHWCAP;
234 			else
235 				ofl->ofl_flags1 |= FLG_OF1_OVSFCAP;
236 			used++;
237 			continue;
238 		}
239 
240 		/*
241 		 * Next, determine if the token represents a machine specific
242 		 * hardware capability, or a generic software capability.
243 		 */
244 		if (type == CA_SUNW_HW_1) {
245 			if ((number = (Xword)elfcap_hw1_from_str(
246 			    ELFCAP_STYLE_LC, Start_tok,
247 			    ld_targ.t_m.m_mach)) != 0) {
248 				ofl->ofl_hwcap_1 |= number;
249 				used++;
250 				continue;
251 			}
252 		} else {
253 			if ((number = (Xword)elfcap_sf1_from_str(
254 			    ELFCAP_STYLE_LC, Start_tok,
255 			    ld_targ.t_m.m_mach)) != 0) {
256 				ofl->ofl_sfcap_1 |= number;
257 				used++;
258 				continue;
259 			}
260 		}
261 
262 		/*
263 		 * Next, determine if the token represents a numeric value.
264 		 */
265 		if (Start_tok[0] == 'v') {
266 			char		*end_tok;
267 
268 			errno = 0;
269 			number = (Xword)strtoul(&Start_tok[1], &end_tok, 0);
270 			if (errno) {
271 				int	err = errno;
272 				eprintf(ofl->ofl_lml, ERR_FATAL,
273 				    MSG_INTL(MSG_MAP_BADCAPVAL),
274 				    mapfile, EC_XWORD(Line_num), Start_tok,
275 				    strerror(err));
276 				return (S_ERROR);
277 			}
278 			if (end_tok != strchr(Start_tok, '\0')) {
279 				eprintf(ofl->ofl_lml, ERR_FATAL,
280 				    MSG_INTL(MSG_MAP_BADCAPVAL), mapfile,
281 				    EC_XWORD(Line_num), Start_tok,
282 				    MSG_INTL(MSG_MAP_NOBADFRM));
283 				return (S_ERROR);
284 			}
285 
286 			if (type == CA_SUNW_HW_1)
287 				ofl->ofl_hwcap_1 |= number;
288 			else
289 				ofl->ofl_sfcap_1 |= number;
290 			used++;
291 			continue;
292 		}
293 
294 		/*
295 		 * We have an unknown token.
296 		 */
297 		used++;
298 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_MAP_UNKCAPATTR),
299 		    mapfile, EC_XWORD(Line_num), Start_tok);
300 		return (S_ERROR);
301 	}
302 
303 	/*
304 	 * Catch any empty declarations, and indicate any software capabilities
305 	 * have been initialized if necessary.
306 	 */
307 	if (used == 0) {
308 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_MAP_EMPTYCAP),
309 		    mapfile, EC_XWORD(Line_num));
310 	} else if (type == CA_SUNW_SF_1) {
311 		Lword	badsf1;
312 
313 		/*
314 		 * Note, hardware capabilities, beyond the tokens that are
315 		 * presently known, can be accepted using the V0xXXX notation,
316 		 * and as these simply get or'd into the output image, we allow
317 		 * any values to be supplied.  Software capability tokens
318 		 * however, have an algorithm of acceptance and update (see
319 		 * sf1_cap() in files.c).  Therefore only allow software
320 		 * capabilities that are known.
321 		 */
322 		if ((badsf1 = (ofl->ofl_sfcap_1 & ~SF1_SUNW_MASK)) != 0) {
323 			eprintf(ofl->ofl_lml, ERR_WARNING,
324 			    MSG_INTL(MSG_MAP_BADSF1), mapfile,
325 			    EC_XWORD(Line_num), EC_LWORD(badsf1));
326 			ofl->ofl_sfcap_1 &= SF1_SUNW_MASK;
327 		}
328 		if ((ofl->ofl_sfcap_1 &
329 		    (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) == SF1_SUNW_FPUSED) {
330 			eprintf(ofl->ofl_lml, ERR_WARNING,
331 			    MSG_INTL(MSG_MAPFIL_BADSF1), mapfile,
332 			    EC_XWORD(Line_num), EC_LWORD(SF1_SUNW_FPUSED));
333 			ofl->ofl_sfcap_1 &= ~SF1_SUNW_FPUSED;
334 		}
335 #if	!defined(_ELF64)
336 		/*
337 		 * The SF1_SUNW_ADDR32 software capability is only meaningful
338 		 * when building a 64-bit object.  Warn the user, and remove the
339 		 * setting, if we're building a 32-bit object.
340 		 */
341 		if (ofl->ofl_sfcap_1 & SF1_SUNW_ADDR32) {
342 			eprintf(ofl->ofl_lml, ERR_WARNING,
343 			    MSG_INTL(MSG_MAP_INADDR32SF1), mapfile,
344 			    EC_XWORD(Line_num));
345 			ofl->ofl_sfcap_1 &= ~SF1_SUNW_ADDR32;
346 		}
347 #endif
348 	}
349 	return (1);
350 }
351 
352 /*
353  * Common segment error checking.
354  */
355 static Boolean
356 seg_check(const char *mapfile, Sg_desc *sgp, Ofl_desc *ofl, Boolean b_type,
357     Word p_type)
358 {
359 	if (b_type) {
360 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_MAP_MOREONCE),
361 		    mapfile, EC_XWORD(Line_num), MSG_INTL(MSG_MAP_SEGTYP));
362 		return (FALSE);
363 	}
364 	if ((sgp->sg_flags & FLG_SG_TYPE) && (sgp->sg_phdr.p_type != p_type)) {
365 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_MAP_REDEFATT),
366 		    mapfile, EC_XWORD(Line_num), MSG_INTL(MSG_MAP_SEGTYP),
367 		    sgp->sg_name);
368 	}
369 	return (TRUE);
370 }
371 
372 /*
373  * Process a mapfile segment declaration definition.
374  *	segment_name	= segment_attribute;
375  * 	segment_attribute : segment_type  segment_flags  virtual_addr
376  *			    physical_addr  length alignment
377  */
378 static uintptr_t
379 map_equal(const char *mapfile, Sg_desc *sgp, Ofl_desc *ofl)
380 {
381 	Token	tok;			/* Current token. */
382 	Boolean	b_type  = FALSE;	/* True if seg types found. */
383 	Boolean	b_flags = FALSE;	/* True if seg flags found. */
384 	Boolean	b_len   = FALSE;	/* True if seg length found. */
385 	Boolean	b_round = FALSE;	/* True if seg rounding found. */
386 	Boolean	b_vaddr = FALSE;	/* True if seg virtual addr found. */
387 	Boolean	b_paddr = FALSE;	/* True if seg physical addr found. */
388 	Boolean	b_align = FALSE;	/* True if seg alignment found. */
389 
390 	while ((tok = gettoken(ofl, mapfile, 0)) != TK_SEMICOLON) {
391 		if (tok != TK_STRING) {
392 			if (tok != TK_ERROR)
393 				eprintf(ofl->ofl_lml, ERR_FATAL,
394 				    MSG_INTL(MSG_MAP_EXPSEGATT), mapfile,
395 				    EC_XWORD(Line_num));
396 			return (S_ERROR);
397 		}
398 
399 		lowercase(Start_tok);
400 
401 		/*
402 		 * Segment type.  Users are permitted to define PT_LOAD,
403 		 * PT_NOTE, PT_STACK and PT_NULL segments.  Other segment types
404 		 * are only defined in seg_desc[].
405 		 */
406 		if (strcmp(Start_tok, MSG_ORIG(MSG_MAP_LOAD)) == 0) {
407 			if ((b_type = seg_check(mapfile, sgp, ofl, b_type,
408 			    PT_LOAD)) == FALSE)
409 				return (S_ERROR);
410 
411 			sgp->sg_phdr.p_type = PT_LOAD;
412 			sgp->sg_flags |= FLG_SG_TYPE;
413 
414 		} else if (strcmp(Start_tok, MSG_ORIG(MSG_MAP_STACK)) == 0) {
415 			if ((b_type = seg_check(mapfile, sgp, ofl, b_type,
416 			    PT_SUNWSTACK)) == FALSE)
417 				return (S_ERROR);
418 
419 			sgp->sg_phdr.p_type = PT_SUNWSTACK;
420 			sgp->sg_flags |= (FLG_SG_TYPE | FLG_SG_EMPTY);
421 
422 		} else if (strcmp(Start_tok, MSG_ORIG(MSG_MAP_NULL)) == 0) {
423 			if ((b_type = seg_check(mapfile, sgp, ofl, b_type,
424 			    PT_NULL)) == FALSE)
425 				return (S_ERROR);
426 
427 			sgp->sg_phdr.p_type = PT_NULL;
428 			sgp->sg_flags |= FLG_SG_TYPE;
429 
430 		} else if (strcmp(Start_tok, MSG_ORIG(MSG_MAP_NOTE)) == 0) {
431 			if ((b_type = seg_check(mapfile, sgp, ofl, b_type,
432 			    PT_NOTE)) == FALSE)
433 				return (S_ERROR);
434 
435 			sgp->sg_phdr.p_type = PT_NOTE;
436 			sgp->sg_flags |= FLG_SG_TYPE;
437 		}
438 
439 		/* Segment Flags. */
440 
441 		else if (*Start_tok == '?') {
442 			Word	tmp_flags = 0;
443 			char	*flag_tok = Start_tok + 1;
444 
445 			if (b_flags) {
446 				eprintf(ofl->ofl_lml, ERR_FATAL,
447 				    MSG_INTL(MSG_MAP_MOREONCE), mapfile,
448 				    EC_XWORD(Line_num),
449 				    MSG_INTL(MSG_MAP_SEGFLAG));
450 				return (S_ERROR);
451 			}
452 
453 			/*
454 			 * If ? has nothing following leave the flags cleared,
455 			 * otherwise or in any flags specified.
456 			 */
457 			if (*flag_tok) {
458 				while (*flag_tok) {
459 					switch (*flag_tok) {
460 					case 'r':
461 						tmp_flags |= PF_R;
462 						break;
463 					case 'w':
464 						tmp_flags |= PF_W;
465 						break;
466 					case 'x':
467 						tmp_flags |= PF_X;
468 						break;
469 					case 'e':
470 						sgp->sg_flags |= FLG_SG_EMPTY;
471 						break;
472 					case 'o':
473 						sgp->sg_flags |= FLG_SG_ORDER;
474 						ofl->ofl_flags |=
475 						    FLG_OF_SEGORDER;
476 						break;
477 					case 'n':
478 						sgp->sg_flags |= FLG_SG_NOHDR;
479 						break;
480 					default:
481 						eprintf(ofl->ofl_lml, ERR_FATAL,
482 						    MSG_INTL(MSG_MAP_UNKSEGFLG),
483 						    mapfile, EC_XWORD(Line_num),
484 						    *flag_tok);
485 						return (S_ERROR);
486 					}
487 					flag_tok++;
488 				}
489 			}
490 			/*
491 			 * Warn when changing flags except when we're
492 			 * adding or removing "X" from a RW PT_LOAD
493 			 * segment.
494 			 */
495 			if ((sgp->sg_flags & FLG_SG_FLAGS) &&
496 			    (sgp->sg_phdr.p_flags != tmp_flags) &&
497 			    !(sgp->sg_phdr.p_type == PT_LOAD &&
498 			    (tmp_flags & (PF_R|PF_W)) == (PF_R|PF_W) &&
499 			    (tmp_flags ^ sgp->sg_phdr.p_flags) == PF_X)) {
500 				eprintf(ofl->ofl_lml, ERR_WARNING,
501 				    MSG_INTL(MSG_MAP_REDEFATT), mapfile,
502 				    EC_XWORD(Line_num),
503 				    MSG_INTL(MSG_MAP_SEGFLAG), sgp->sg_name);
504 			}
505 			sgp->sg_flags |= FLG_SG_FLAGS;
506 			sgp->sg_phdr.p_flags = tmp_flags;
507 			b_flags = TRUE;
508 		}
509 
510 
511 		/* Segment address, length, alignment or rounding number. */
512 
513 		else if ((Start_tok[0] == 'l') || (Start_tok[0] == 'v') ||
514 		    (Start_tok[0] == 'a') || (Start_tok[0] == 'p') ||
515 		    (Start_tok[0] == 'r')) {
516 			char		*end_tok;
517 			Xword		number;
518 
519 			if ((number = (Xword)STRTOADDR(&Start_tok[1], &end_tok,
520 			    0))	>= XWORD_MAX) {
521 				eprintf(ofl->ofl_lml, ERR_FATAL,
522 				    MSG_INTL(MSG_MAP_SEGADDR), mapfile,
523 				    EC_XWORD(Line_num), Start_tok,
524 				    MSG_INTL(MSG_MAP_EXCLIMIT));
525 				return (S_ERROR);
526 			}
527 
528 			if (end_tok != strchr(Start_tok, '\0')) {
529 				eprintf(ofl->ofl_lml, ERR_FATAL,
530 				    MSG_INTL(MSG_MAP_SEGADDR), mapfile,
531 				    EC_XWORD(Line_num), Start_tok,
532 				    MSG_INTL(MSG_MAP_NOBADFRM));
533 				return (S_ERROR);
534 			}
535 
536 			switch (*Start_tok) {
537 			case 'l':
538 				if (b_len) {
539 					eprintf(ofl->ofl_lml, ERR_FATAL,
540 					    MSG_INTL(MSG_MAP_MOREONCE),
541 					    mapfile, EC_XWORD(Line_num),
542 					    MSG_INTL(MSG_MAP_SEGLEN));
543 					return (S_ERROR);
544 				}
545 				if ((sgp->sg_flags & FLG_SG_LENGTH) &&
546 				    (sgp->sg_length != number))
547 					eprintf(ofl->ofl_lml, ERR_WARNING,
548 					    MSG_INTL(MSG_MAP_REDEFATT),
549 					    mapfile, EC_XWORD(Line_num),
550 					    MSG_INTL(MSG_MAP_SEGLEN),
551 					    sgp->sg_name);
552 				sgp->sg_length = number;
553 				sgp->sg_flags |= FLG_SG_LENGTH;
554 				b_len = TRUE;
555 				break;
556 			case 'r':
557 				if (b_round) {
558 					eprintf(ofl->ofl_lml, ERR_FATAL,
559 					    MSG_INTL(MSG_MAP_MOREONCE),
560 					    mapfile, EC_XWORD(Line_num),
561 					    MSG_INTL(MSG_MAP_SEGROUND));
562 					return (S_ERROR);
563 				}
564 				if ((sgp->sg_flags & FLG_SG_ROUND) &&
565 				    (sgp->sg_round != number))
566 					eprintf(ofl->ofl_lml, ERR_WARNING,
567 					    MSG_INTL(MSG_MAP_REDEFATT),
568 					    mapfile, EC_XWORD(Line_num),
569 					    MSG_INTL(MSG_MAP_SEGROUND),
570 					    sgp->sg_name);
571 				sgp->sg_round = number;
572 				sgp->sg_flags |= FLG_SG_ROUND;
573 				b_round = TRUE;
574 				break;
575 			case 'v':
576 				if (b_vaddr) {
577 					eprintf(ofl->ofl_lml, ERR_FATAL,
578 					    MSG_INTL(MSG_MAP_MOREONCE),
579 					    mapfile, EC_XWORD(Line_num),
580 					    MSG_INTL(MSG_MAP_SEGVADDR));
581 					return (S_ERROR);
582 				}
583 				if ((sgp->sg_flags & FLG_SG_VADDR) &&
584 				    (sgp->sg_phdr.p_vaddr != number))
585 					eprintf(ofl->ofl_lml, ERR_WARNING,
586 					    MSG_INTL(MSG_MAP_REDEFATT),
587 					    mapfile, EC_XWORD(Line_num),
588 					    MSG_INTL(MSG_MAP_SEGVADDR),
589 					    sgp->sg_name);
590 				/* LINTED */
591 				sgp->sg_phdr.p_vaddr = (Addr)number;
592 				sgp->sg_flags |= FLG_SG_VADDR;
593 				ofl->ofl_flags1 |= FLG_OF1_VADDR;
594 				ofl->ofl_flags |= FLG_OF_SEGSORT;
595 				b_vaddr = TRUE;
596 				break;
597 			case 'p':
598 				if (b_paddr) {
599 					eprintf(ofl->ofl_lml, ERR_FATAL,
600 					    MSG_INTL(MSG_MAP_MOREONCE),
601 					    mapfile, EC_XWORD(Line_num),
602 					    MSG_INTL(MSG_MAP_SEGPHYS));
603 					return (S_ERROR);
604 				}
605 				if ((sgp->sg_flags & FLG_SG_PADDR) &&
606 				    (sgp->sg_phdr.p_paddr != number))
607 					eprintf(ofl->ofl_lml, ERR_WARNING,
608 					    MSG_INTL(MSG_MAP_REDEFATT),
609 					    mapfile, EC_XWORD(Line_num),
610 					    MSG_INTL(MSG_MAP_SEGPHYS),
611 					    sgp->sg_name);
612 				/* LINTED */
613 				sgp->sg_phdr.p_paddr = (Addr)number;
614 				sgp->sg_flags |= FLG_SG_PADDR;
615 				b_paddr = TRUE;
616 				break;
617 			case 'a':
618 				if (b_align) {
619 					eprintf(ofl->ofl_lml, ERR_FATAL,
620 					    MSG_INTL(MSG_MAP_MOREONCE),
621 					    mapfile, EC_XWORD(Line_num),
622 					    MSG_INTL(MSG_MAP_SEGALIGN));
623 					return (S_ERROR);
624 				}
625 				if ((sgp->sg_flags & FLG_SG_ALIGN) &&
626 				    (sgp->sg_phdr.p_align != number))
627 					eprintf(ofl->ofl_lml, ERR_WARNING,
628 					    MSG_INTL(MSG_MAP_REDEFATT),
629 					    mapfile, EC_XWORD(Line_num),
630 					    MSG_INTL(MSG_MAP_SEGALIGN),
631 					    sgp->sg_name);
632 				/* LINTED */
633 				sgp->sg_phdr.p_align = (Xword)number;
634 				sgp->sg_flags |= FLG_SG_ALIGN;
635 				b_align = TRUE;
636 				break;
637 			}
638 		} else {
639 			eprintf(ofl->ofl_lml, ERR_FATAL,
640 			    MSG_INTL(MSG_MAP_UNKSEGATT), mapfile,
641 			    EC_XWORD(Line_num), Start_tok);
642 			return (S_ERROR);
643 		}
644 	}
645 
646 	/*
647 	 * Empty segments can be used to define PT_LOAD segment reservations, or
648 	 * to reserve PT_NULL program headers.
649 	 *
650 	 * PT_LOAD reservations are only allowed within executables, as the
651 	 * reservation must be established through exec() as part of initial
652 	 * process loading.  In addition, PT_LOAD reservations must have an
653 	 * associated address and size.
654 	 *
655 	 * PT_NULL program headers are established for later use by applications
656 	 * such as the post-optimizer.  PT_NULL headers should have no other
657 	 * attributes assigned.
658 	 */
659 	if ((sgp->sg_flags & FLG_SG_EMPTY) &&
660 	    (sgp->sg_phdr.p_type != PT_SUNWSTACK)) {
661 
662 		/*
663 		 * Any style of empty segment should have no permissions.
664 		 */
665 		if (sgp->sg_phdr.p_flags != 0) {
666 			eprintf(ofl->ofl_lml, ERR_FATAL,
667 			    MSG_INTL(MSG_MAP_SEGEMNOPERM), mapfile,
668 			    EC_XWORD(Line_num),
669 			    EC_WORD(sgp->sg_phdr.p_flags));
670 			return (S_ERROR);
671 		}
672 
673 		if (sgp->sg_phdr.p_type == PT_LOAD) {
674 			if ((ofl->ofl_flags & FLG_OF_EXEC) == 0) {
675 				eprintf(ofl->ofl_lml, ERR_FATAL,
676 				    MSG_INTL(MSG_MAP_SEGEMPEXE), mapfile,
677 				    EC_XWORD(Line_num));
678 				return (S_ERROR);
679 			}
680 			if ((sgp->sg_flags & (FLG_SG_LENGTH | FLG_SG_VADDR)) !=
681 			    (FLG_SG_LENGTH | FLG_SG_VADDR)) {
682 				eprintf(ofl->ofl_lml, ERR_FATAL,
683 				    MSG_INTL(MSG_MAP_SEGEMPATT), mapfile,
684 				    EC_XWORD(Line_num));
685 				return (S_ERROR);
686 			}
687 		} else if (sgp->sg_phdr.p_type == PT_NULL) {
688 			if ((sgp->sg_flags & (FLG_SG_LENGTH | FLG_SG_VADDR)) &&
689 			    ((sgp->sg_length != 0) ||
690 			    (sgp->sg_phdr.p_vaddr != 0))) {
691 				eprintf(ofl->ofl_lml, ERR_FATAL,
692 				    MSG_INTL(MSG_MAP_SEGEMPNOATT), mapfile,
693 				    EC_XWORD(Line_num));
694 				return (S_ERROR);
695 			}
696 		} else {
697 			eprintf(ofl->ofl_lml, ERR_WARNING,
698 			    MSG_INTL(MSG_MAP_SEGEMPLOAD), mapfile,
699 			    EC_XWORD(Line_num));
700 			sgp->sg_phdr.p_type = PT_LOAD;
701 		}
702 	}
703 
704 	/*
705 	 * All segment attributes have now been scanned.  Certain flags do not
706 	 * make sense if this is not a loadable segment, fix if necessary.
707 	 * Note, if the segment is of type PT_NULL it must be new, and any
708 	 * defaults will be applied back in ld_map_parse().
709 	 * When clearing an attribute leave the flag set as an indicator for
710 	 * later entries re-specifying the same segment.
711 	 */
712 	if ((sgp->sg_phdr.p_type != PT_NULL) &&
713 	    (sgp->sg_phdr.p_type != PT_LOAD)) {
714 		const char	*fmt;
715 
716 		if (sgp->sg_phdr.p_type == PT_SUNWSTACK)
717 			fmt = MSG_INTL(MSG_MAP_NOSTACK1);
718 		else
719 			fmt = MSG_INTL(MSG_MAP_NONLOAD);
720 
721 		if ((sgp->sg_flags & FLG_SG_FLAGS) &&
722 		    (sgp->sg_phdr.p_type != PT_SUNWSTACK)) {
723 			if (sgp->sg_phdr.p_flags != 0) {
724 				eprintf(ofl->ofl_lml, ERR_WARNING,
725 				    MSG_INTL(MSG_MAP_NONLOAD), mapfile,
726 				    EC_XWORD(Line_num),
727 				    MSG_INTL(MSG_MAP_SEGFLAG));
728 				sgp->sg_phdr.p_flags = 0;
729 			}
730 		}
731 		if (sgp->sg_flags & FLG_SG_LENGTH)
732 			if (sgp->sg_length != 0) {
733 				eprintf(ofl->ofl_lml, ERR_WARNING,
734 				    fmt, mapfile, EC_XWORD(Line_num),
735 				    MSG_INTL(MSG_MAP_SEGLEN));
736 				sgp->sg_length = 0;
737 			}
738 		if (sgp->sg_flags & FLG_SG_ROUND)
739 			if (sgp->sg_round != 0) {
740 				eprintf(ofl->ofl_lml, ERR_WARNING,
741 				    fmt, mapfile, EC_XWORD(Line_num),
742 				    MSG_INTL(MSG_MAP_SEGROUND));
743 				sgp->sg_round = 0;
744 			}
745 		if (sgp->sg_flags & FLG_SG_VADDR) {
746 			if (sgp->sg_phdr.p_vaddr != 0) {
747 				eprintf(ofl->ofl_lml, ERR_WARNING,
748 				    fmt, mapfile, EC_XWORD(Line_num),
749 				    MSG_INTL(MSG_MAP_SEGVADDR));
750 				sgp->sg_phdr.p_vaddr = 0;
751 			}
752 		}
753 		if (sgp->sg_flags & FLG_SG_PADDR)
754 			if (sgp->sg_phdr.p_paddr != 0) {
755 				eprintf(ofl->ofl_lml, ERR_WARNING,
756 				    fmt, mapfile, EC_XWORD(Line_num),
757 				    MSG_INTL(MSG_MAP_SEGPHYS));
758 				sgp->sg_phdr.p_paddr = 0;
759 			}
760 		if (sgp->sg_flags & FLG_SG_ALIGN)
761 			if (sgp->sg_phdr.p_align != 0) {
762 				eprintf(ofl->ofl_lml, ERR_WARNING,
763 				    fmt, mapfile, EC_XWORD(Line_num),
764 				    MSG_INTL(MSG_MAP_SEGALIGN));
765 				sgp->sg_phdr.p_align = 0;
766 			}
767 	}
768 	return (1);
769 }
770 
771 
772 /*
773  * Process a mapfile mapping directives definition.
774  * 	segment_name : section_attribute [ : file_name ]
775  * 	segment_attribute : section_name section_type section_flags;
776  */
777 static uintptr_t
778 map_colon(Ofl_desc *ofl, const char *mapfile, Ent_desc *enp)
779 {
780 	Token		tok;		/* Current token. */
781 
782 	Boolean		b_name = FALSE;
783 	Boolean		b_type = FALSE;
784 	Boolean		b_attr = FALSE;
785 	Boolean		b_bang = FALSE;
786 	static	Xword	index = 0;
787 
788 
789 	while (((tok = gettoken(ofl, mapfile, 0)) != TK_COLON) &&
790 	    (tok != TK_SEMICOLON)) {
791 		if ((tok == TK_ERROR) || (tok == TK_EOF))
792 			return (S_ERROR);
793 
794 		/* Segment type. */
795 
796 		if (*Start_tok == '$') {
797 			if (b_type) {
798 				eprintf(ofl->ofl_lml, ERR_FATAL,
799 				    MSG_INTL(MSG_MAP_MOREONCE), mapfile,
800 				    EC_XWORD(Line_num),
801 				    MSG_INTL(MSG_MAP_SECTYP));
802 				return (S_ERROR);
803 			}
804 			b_type = TRUE;
805 			Start_tok++;
806 			lowercase(Start_tok);
807 			if (strcmp(Start_tok, MSG_ORIG(MSG_STR_PROGBITS)) == 0)
808 				enp->ec_type = SHT_PROGBITS;
809 			else if (strcmp(Start_tok,
810 			    MSG_ORIG(MSG_STR_SYMTAB)) == 0)
811 				enp->ec_type = SHT_SYMTAB;
812 			else if (strcmp(Start_tok,
813 			    MSG_ORIG(MSG_STR_DYNSYM)) == 0)
814 				enp->ec_type = SHT_DYNSYM;
815 			else if (strcmp(Start_tok,
816 			    MSG_ORIG(MSG_STR_STRTAB)) == 0)
817 				enp->ec_type = SHT_STRTAB;
818 			else if ((strcmp(Start_tok,
819 			    MSG_ORIG(MSG_STR_REL)) == 0) ||
820 			    (strcmp(Start_tok, MSG_ORIG(MSG_STR_RELA)) == 0))
821 				enp->ec_type = ld_targ.t_m.m_rel_sht_type;
822 			else if (strcmp(Start_tok, MSG_ORIG(MSG_STR_HASH)) == 0)
823 				enp->ec_type = SHT_HASH;
824 			else if (strcmp(Start_tok, MSG_ORIG(MSG_STR_LIB)) == 0)
825 				enp->ec_type = SHT_SHLIB;
826 			else if (strcmp(Start_tok,
827 			    MSG_ORIG(MSG_STR_LD_DYNAMIC)) == 0)
828 				enp->ec_type = SHT_DYNAMIC;
829 			else if (strcmp(Start_tok, MSG_ORIG(MSG_STR_NOTE)) == 0)
830 				enp->ec_type = SHT_NOTE;
831 			else if (strcmp(Start_tok,
832 			    MSG_ORIG(MSG_STR_NOBITS)) == 0)
833 				enp->ec_type = SHT_NOBITS;
834 			else {
835 				eprintf(ofl->ofl_lml, ERR_FATAL,
836 				    MSG_INTL(MSG_MAP_UNKSECTYP), mapfile,
837 				    EC_XWORD(Line_num), Start_tok);
838 				return (S_ERROR);
839 			}
840 
841 		/*
842 		 * Segment flags.
843 		 * If a segment flag is specified then the appropriate bit is
844 		 * set in the ec_attrmask, the ec_attrbits fields determine
845 		 * whether the attrmask fields must be tested true or false
846 		 * ie.	for  ?A the attrmask is set and the attrbit is set,
847 		 *	for ?!A the attrmask is set and the attrbit is clear.
848 		 */
849 		} else if (*Start_tok == '?') {
850 			if (b_attr) {
851 				eprintf(ofl->ofl_lml, ERR_FATAL,
852 				    MSG_INTL(MSG_MAP_MOREONCE), mapfile,
853 				    EC_XWORD(Line_num),
854 				    MSG_INTL(MSG_MAP_SECFLAG));
855 				return (S_ERROR);
856 			}
857 			b_attr = TRUE;
858 			b_bang = FALSE;
859 			Start_tok++;
860 			lowercase(Start_tok);
861 			for (; *Start_tok != '\0'; Start_tok++)
862 				switch (*Start_tok) {
863 				case '!':
864 					if (b_bang) {
865 						eprintf(ofl->ofl_lml, ERR_FATAL,
866 						    MSG_INTL(MSG_MAP_BADFLAG),
867 						    mapfile, EC_XWORD(Line_num),
868 						    Start_tok);
869 						return (S_ERROR);
870 					}
871 					b_bang = TRUE;
872 					break;
873 				case 'a':
874 					if (enp->ec_attrmask & SHF_ALLOC) {
875 						eprintf(ofl->ofl_lml, ERR_FATAL,
876 						    MSG_INTL(MSG_MAP_BADFLAG),
877 						    mapfile, EC_XWORD(Line_num),
878 						    Start_tok);
879 						return (S_ERROR);
880 					}
881 					enp->ec_attrmask |= SHF_ALLOC;
882 					if (!b_bang)
883 						enp->ec_attrbits |= SHF_ALLOC;
884 					b_bang = FALSE;
885 					break;
886 				case 'w':
887 					if (enp->ec_attrmask & SHF_WRITE) {
888 						eprintf(ofl->ofl_lml, ERR_FATAL,
889 						    MSG_INTL(MSG_MAP_BADFLAG),
890 						    mapfile, EC_XWORD(Line_num),
891 						    Start_tok);
892 						return (S_ERROR);
893 					}
894 					enp->ec_attrmask |= SHF_WRITE;
895 					if (!b_bang)
896 						enp->ec_attrbits |= SHF_WRITE;
897 					b_bang = FALSE;
898 					break;
899 				case 'x':
900 					if (enp->ec_attrmask & SHF_EXECINSTR) {
901 						eprintf(ofl->ofl_lml, ERR_FATAL,
902 						    MSG_INTL(MSG_MAP_BADFLAG),
903 						    mapfile, EC_XWORD(Line_num),
904 						    Start_tok);
905 						return (S_ERROR);
906 					}
907 					enp->ec_attrmask |= SHF_EXECINSTR;
908 					if (!b_bang)
909 						enp->ec_attrbits |=
910 						    SHF_EXECINSTR;
911 					b_bang = FALSE;
912 					break;
913 				default:
914 					eprintf(ofl->ofl_lml, ERR_FATAL,
915 					    MSG_INTL(MSG_MAP_BADFLAG),
916 					    mapfile, EC_XWORD(Line_num),
917 					    Start_tok);
918 					return (S_ERROR);
919 				}
920 		/*
921 		 * Section name.
922 		 */
923 		} else {
924 			if (b_name) {
925 				eprintf(ofl->ofl_lml, ERR_FATAL,
926 				    MSG_INTL(MSG_MAP_MOREONCE), mapfile,
927 				    EC_XWORD(Line_num),
928 				    MSG_INTL(MSG_MAP_SECNAME));
929 				return (S_ERROR);
930 			}
931 			b_name = TRUE;
932 			if ((enp->ec_name =
933 			    libld_malloc(strlen(Start_tok) + 1)) == NULL)
934 				return (S_ERROR);
935 			(void) strcpy((char *)enp->ec_name, Start_tok);
936 			/*
937 			 * Set the index for text reordering.
938 			 */
939 			enp->ec_ordndx = ++index;
940 		}
941 	}
942 	if (tok == TK_COLON) {
943 		/*
944 		 * File names.
945 		 */
946 		while ((tok = gettoken(ofl, mapfile, 0)) != TK_SEMICOLON) {
947 			char	*file;
948 
949 			if (tok != TK_STRING) {
950 				if (tok != TK_ERROR)
951 					eprintf(ofl->ofl_lml, ERR_FATAL,
952 					    MSG_INTL(MSG_MAP_MALFORM), mapfile,
953 					    EC_XWORD(Line_num));
954 				return (S_ERROR);
955 			}
956 			if ((file =
957 			    libld_malloc(strlen(Start_tok) + 1)) == NULL)
958 				return (S_ERROR);
959 			(void) strcpy(file, Start_tok);
960 
961 			if (aplist_append(&(enp->ec_files), file,
962 			    AL_CNT_EC_FILES) == NULL)
963 				return (S_ERROR);
964 		}
965 	}
966 	return (1);
967 }
968 
969 /*
970  * Obtain a pseudo input file descriptor to assign to a mapfile.  This is
971  * required any time a symbol is generated.  First traverse the input file
972  * descriptors looking for a match.  As all mapfile processing occurs before
973  * any real input file processing this list is going to be small and we don't
974  * need to do any filename clash checking.
975  */
976 static Ifl_desc *
977 map_ifl(const char *mapfile, Ofl_desc *ofl)
978 {
979 	Ifl_desc	*ifl;
980 	Aliste		idx;
981 
982 	for (APLIST_TRAVERSE(ofl->ofl_objs, idx, ifl))
983 		if (strcmp(ifl->ifl_name, mapfile) == 0)
984 			return (ifl);
985 
986 	if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == NULL)
987 		return ((Ifl_desc *)S_ERROR);
988 	ifl->ifl_name = mapfile;
989 	ifl->ifl_flags = (FLG_IF_MAPFILE | FLG_IF_NEEDED | FLG_IF_FILEREF);
990 	if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 1)) == NULL)
991 		return ((Ifl_desc *)S_ERROR);
992 	ifl->ifl_ehdr->e_type = ET_REL;
993 
994 	if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
995 		return ((Ifl_desc *)S_ERROR);
996 	else
997 		return (ifl);
998 }
999 
1000 /*
1001  * Process a mapfile size symbol definition.
1002  * 	segment_name @ symbol_name;
1003  */
1004 static uintptr_t
1005 map_atsign(const char *mapfile, Sg_desc *sgp, Ofl_desc *ofl)
1006 {
1007 	Sym		*sym;		/* New symbol pointer */
1008 	Sym_desc	*sdp;		/* New symbol node pointer */
1009 	Ifl_desc	*ifl;		/* Dummy input file structure */
1010 	Token		tok;		/* Current token. */
1011 	avl_index_t	where;
1012 
1013 	if ((tok = gettoken(ofl, mapfile, 0)) != TK_STRING) {
1014 		if (tok != TK_ERROR)
1015 			eprintf(ofl->ofl_lml, ERR_FATAL,
1016 			    MSG_INTL(MSG_MAP_EXPSYM_1), mapfile,
1017 			    EC_XWORD(Line_num));
1018 		return (S_ERROR);
1019 	}
1020 
1021 	if (sgp->sg_sizesym != NULL) {
1022 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_MAP_SEGSIZE),
1023 		    mapfile, EC_XWORD(Line_num), sgp->sg_name);
1024 		return (S_ERROR);
1025 	}
1026 
1027 	/*
1028 	 * Make sure we have a pseudo file descriptor to associate to the
1029 	 * symbol.
1030 	 */
1031 	if ((ifl = map_ifl(mapfile, ofl)) == (Ifl_desc *)S_ERROR)
1032 		return (S_ERROR);
1033 
1034 	/*
1035 	 * Make sure the symbol doesn't already exist.  It is possible that the
1036 	 * symbol has been scoped or versioned, in which case it does exist
1037 	 * but we can freely update it here.
1038 	 */
1039 	if ((sdp = ld_sym_find(Start_tok, SYM_NOHASH, &where, ofl)) == NULL) {
1040 		char	*name;
1041 		Word hval;
1042 
1043 		if ((name = libld_malloc(strlen(Start_tok) + 1)) == NULL)
1044 			return (S_ERROR);
1045 		(void) strcpy(name, Start_tok);
1046 
1047 		if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
1048 			return (S_ERROR);
1049 		sym->st_shndx = SHN_ABS;
1050 		sym->st_size = 0;
1051 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
1052 
1053 		DBG_CALL(Dbg_map_size_new(ofl->ofl_lml, name));
1054 		/* LINTED */
1055 		hval = (Word)elf_hash(name);
1056 		if ((sdp = ld_sym_enter(name, sym, hval, ifl, ofl, 0, SHN_ABS,
1057 		    (FLG_SY_SPECSEC | FLG_SY_GLOBREF), 0, &where)) ==
1058 		    (Sym_desc *)S_ERROR)
1059 			return (S_ERROR);
1060 		sdp->sd_flags &= ~FLG_SY_CLEAN;
1061 		DBG_CALL(Dbg_map_symbol(ofl, sdp));
1062 	} else {
1063 		sym = sdp->sd_sym;
1064 
1065 		if (sym->st_shndx == SHN_UNDEF) {
1066 			sdp->sd_shndx = sym->st_shndx = SHN_ABS;
1067 			sdp->sd_flags |= FLG_SY_SPECSEC;
1068 			sym->st_size = 0;
1069 			sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
1070 
1071 			sdp->sd_flags &= ~FLG_SY_MAPREF;
1072 
1073 			DBG_CALL(Dbg_map_size_old(ofl, sdp));
1074 		} else {
1075 			eprintf(ofl->ofl_lml, ERR_FATAL,
1076 			    MSG_INTL(MSG_MAP_SYMDEF1), mapfile,
1077 			    EC_XWORD(Line_num), demangle(sdp->sd_name),
1078 			    sdp->sd_file->ifl_name,
1079 			    MSG_INTL(MSG_MAP_DIFF_SYMMUL));
1080 			return (S_ERROR);
1081 		}
1082 	}
1083 
1084 	/*
1085 	 * Assign the symbol to the segment.
1086 	 */
1087 	sgp->sg_sizesym = sdp;
1088 
1089 	if (gettoken(ofl, mapfile, 0) != TK_SEMICOLON) {
1090 		if (tok != TK_ERROR)
1091 			eprintf(ofl->ofl_lml, ERR_FATAL,
1092 			    MSG_INTL(MSG_MAP_EXPSCOL), mapfile,
1093 			    EC_XWORD(Line_num));
1094 		return (S_ERROR);
1095 	}
1096 
1097 	return (1);
1098 }
1099 
1100 
1101 static uintptr_t
1102 map_pipe(Ofl_desc *ofl, const char *mapfile, Sg_desc *sgp)
1103 {
1104 	char		*sec_name;	/* section name */
1105 	Token		tok;		/* current token. */
1106 	Sec_order	*sc_order;
1107 	static Word	index = 0;	/* used to maintain a increasing */
1108 					/* 	index for section ordering. */
1109 
1110 	if ((tok = gettoken(ofl, mapfile, 0)) != TK_STRING) {
1111 		if (tok != TK_ERROR)
1112 			eprintf(ofl->ofl_lml, ERR_FATAL,
1113 			    MSG_INTL(MSG_MAP_EXPSEC), mapfile,
1114 			    EC_XWORD(Line_num));
1115 		return (S_ERROR);
1116 	}
1117 
1118 	if ((sec_name = libld_malloc(strlen(Start_tok) + 1)) == NULL)
1119 		return (S_ERROR);
1120 	(void) strcpy(sec_name, Start_tok);
1121 
1122 	if ((sc_order = libld_malloc(sizeof (Sec_order))) == NULL)
1123 		return (S_ERROR);
1124 
1125 	sc_order->sco_secname = sec_name;
1126 	sc_order->sco_index = ++index;
1127 
1128 	if (aplist_append(&sgp->sg_secorder, sc_order,
1129 	    AL_CNT_SG_SECORDER) == NULL)
1130 		return (S_ERROR);
1131 
1132 	ofl->ofl_flags |= FLG_OF_SECORDER;
1133 	DBG_CALL(Dbg_map_pipe(ofl->ofl_lml, sgp, sec_name, index));
1134 
1135 	if ((tok = gettoken(ofl, mapfile, 0)) != TK_SEMICOLON) {
1136 		if (tok != TK_ERROR)
1137 			eprintf(ofl->ofl_lml, ERR_FATAL,
1138 			    MSG_INTL(MSG_MAP_EXPSCOL), mapfile,
1139 			    EC_XWORD(Line_num));
1140 		return (S_ERROR);
1141 	}
1142 
1143 	return (1);
1144 }
1145 
1146 /*
1147  * Process a mapfile library specification definition.
1148  * 	shared_object_name - shared object definition
1149  *	shared object definition : [ shared object type [ = SONAME ]]
1150  *					[ versions ];
1151  */
1152 static uintptr_t
1153 map_dash(const char *mapfile, char *name, Ofl_desc *ofl)
1154 {
1155 	char		*version;
1156 	Token		tok;
1157 	Sdf_desc	*sdf;
1158 	Sdv_desc	sdv;
1159 	enum {
1160 	    MD_NONE = 0,
1161 	    MD_ADDVERS,
1162 	}		dolkey = MD_NONE;
1163 
1164 
1165 	/*
1166 	 * If a shared object definition for this file already exists use it,
1167 	 * otherwise allocate a new descriptor.
1168 	 */
1169 	if ((sdf = sdf_find(name, ofl->ofl_socntl)) == NULL) {
1170 		if ((sdf = sdf_add(name, &ofl->ofl_socntl)) ==
1171 		    (Sdf_desc *)S_ERROR)
1172 			return (S_ERROR);
1173 		sdf->sdf_rfile = mapfile;
1174 	}
1175 
1176 	/*
1177 	 * Get the shared object descriptor string.
1178 	 */
1179 	while ((tok = gettoken(ofl, mapfile, 0)) != TK_SEMICOLON) {
1180 		if ((tok != TK_STRING) && (tok != TK_EQUAL)) {
1181 			if (tok != TK_ERROR)
1182 				eprintf(ofl->ofl_lml, ERR_FATAL,
1183 				    MSG_INTL(MSG_MAP_EXPSO), mapfile,
1184 				    EC_XWORD(Line_num));
1185 			return (S_ERROR);
1186 		}
1187 
1188 		/*
1189 		 * Determine if the library type is accompanied with a SONAME
1190 		 * definition.
1191 		 */
1192 		if (tok == TK_EQUAL) {
1193 			if ((tok = gettoken(ofl, mapfile, 0)) != TK_STRING) {
1194 				if (tok != TK_ERROR)
1195 					eprintf(ofl->ofl_lml, ERR_FATAL,
1196 					    MSG_INTL(MSG_MAP_EXPSO), mapfile,
1197 					    EC_XWORD(Line_num));
1198 				return (S_ERROR);
1199 			}
1200 			switch (dolkey) {
1201 			case MD_ADDVERS:
1202 				sdf->sdf_flags |= FLG_SDF_ADDVER;
1203 
1204 				if ((version = libld_malloc(
1205 				    strlen(Start_tok) + 1)) == NULL)
1206 					return (S_ERROR);
1207 				(void) strcpy(version, Start_tok);
1208 
1209 				sdv.sdv_name = version;
1210 				sdv.sdv_ref = mapfile;
1211 				sdv.sdv_flags = 0;
1212 
1213 				if (alist_append(&sdf->sdf_verneed, &sdv,
1214 				    sizeof (Sdv_desc),
1215 				    AL_CNT_SDF_VERSIONS) == NULL)
1216 					return (S_ERROR);
1217 				break;
1218 			case MD_NONE:
1219 				eprintf(ofl->ofl_lml, ERR_FATAL,
1220 				    MSG_INTL(MSG_MAP_UNEXTOK), mapfile,
1221 				    EC_XWORD(Line_num), '=');
1222 				return (S_ERROR);
1223 			}
1224 			dolkey = MD_NONE;
1225 			continue;
1226 		}
1227 
1228 		/*
1229 		 * A shared object type has been specified.  This may also be
1230 		 * accompanied by an SONAME redefinition (see above).
1231 		 */
1232 		if (*Start_tok == '$') {
1233 			if (dolkey != MD_NONE) {
1234 				eprintf(ofl->ofl_lml, ERR_FATAL,
1235 				    MSG_INTL(MSG_MAP_UNEXTOK), mapfile,
1236 				    EC_XWORD(Line_num), '$');
1237 				return (S_ERROR);
1238 			}
1239 			Start_tok++;
1240 			lowercase(Start_tok);
1241 			if (strcmp(Start_tok,
1242 			    MSG_ORIG(MSG_MAP_ADDVERS)) == 0)
1243 				dolkey = MD_ADDVERS;
1244 			else {
1245 				eprintf(ofl->ofl_lml, ERR_FATAL,
1246 				    MSG_INTL(MSG_MAP_UNKSOTYP), mapfile,
1247 				    EC_XWORD(Line_num), Start_tok);
1248 				return (S_ERROR);
1249 			}
1250 			continue;
1251 		}
1252 
1253 		/*
1254 		 * shared object version requirement.
1255 		 */
1256 		if ((version = libld_malloc(strlen(Start_tok) + 1)) == NULL)
1257 			return (S_ERROR);
1258 		(void) strcpy(version, Start_tok);
1259 
1260 		sdf->sdf_flags |= FLG_SDF_SELECT;
1261 
1262 		sdv.sdv_name = version;
1263 		sdv.sdv_ref = mapfile;
1264 		sdv.sdv_flags = 0;
1265 
1266 		if (alist_append(&sdf->sdf_vers, &sdv, sizeof (Sdv_desc),
1267 		    AL_CNT_SDF_VERSIONS) == NULL)
1268 			return (S_ERROR);
1269 	}
1270 
1271 	DBG_CALL(Dbg_map_dash(ofl->ofl_lml, name));
1272 	return (1);
1273 }
1274 
1275 
1276 /*
1277  * Process a symbol definition.  Historically, this originated from processing
1278  * a version definition.  However, this has evolved into a generic means of
1279  * defining symbol references and definitions (see Defining Additional Symbols
1280  * in the Linker and Libraries guide for the complete syntax).
1281  *
1282  * [ name ] {
1283  *	scope:
1284  *		 symbol [ = [ type ] [ value ] [ size ] [ attribute ] ];
1285  * } [ dependency ];
1286  *
1287  */
1288 #define	FLG_SCOPE_HIDD	0		/* symbol defined hidden/local */
1289 #define	FLG_SCOPE_DFLT	1		/* symbol defined default/global */
1290 #define	FLG_SCOPE_PROT	2		/* symbol defined protected/symbolic */
1291 #define	FLG_SCOPE_EXPT	3		/* symbol defined exported */
1292 #define	FLG_SCOPE_SNGL	4		/* symbol defined singleton */
1293 #define	FLG_SCOPE_ELIM	5		/* symbol defined eliminate */
1294 
1295 static uintptr_t
1296 map_version(const char *mapfile, char *name, Ofl_desc *ofl)
1297 {
1298 	Token		tok;
1299 	Sym		*sym;
1300 	int		scope = FLG_SCOPE_DFLT, errcnt = 0;
1301 	Ver_desc	*vdp;
1302 	Word		hash;
1303 	Ifl_desc	*ifl;
1304 	avl_index_t	where;
1305 
1306 	/*
1307 	 * If we're generating segments within the image then any symbol
1308 	 * reductions will be processed (ie. applied to relocations and symbol
1309 	 * table entries).  Otherwise (when creating a relocatable object) any
1310 	 * versioning information is simply recorded for use in a later
1311 	 * (segment generating) link-edit.
1312 	 */
1313 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
1314 		ofl->ofl_flags |= FLG_OF_VERDEF;
1315 
1316 	/*
1317 	 * If this is a new mapfile reference generate an input file descriptor
1318 	 * to represent it.  Otherwise this must simply be a new version within
1319 	 * the mapfile we've previously been processing, in this case continue
1320 	 * to use the original input file descriptor.
1321 	 */
1322 	if ((ifl = map_ifl(mapfile, ofl)) == (Ifl_desc *)S_ERROR)
1323 		return (S_ERROR);
1324 
1325 	/*
1326 	 * If no version descriptors have yet been set up, initialize a base
1327 	 * version to represent the output file itself.  This `base' version
1328 	 * catches any internally generated symbols (_end, _etext, etc.) and
1329 	 * serves to initialize the output version descriptor count.
1330 	 */
1331 	if (ofl->ofl_vercnt == 0) {
1332 		if (ld_vers_base(ofl) == (Ver_desc *)S_ERROR)
1333 			return (S_ERROR);
1334 	}
1335 
1336 	/*
1337 	 * If this definition has an associated version name then generate a
1338 	 * new version descriptor and an associated version symbol index table.
1339 	 */
1340 	if (name) {
1341 		ofl->ofl_flags |= FLG_OF_VERDEF;
1342 
1343 		/*
1344 		 * Traverse the present version descriptor list to see if there
1345 		 * is already one of the same name, otherwise create a new one.
1346 		 */
1347 		/* LINTED */
1348 		hash = (Word)elf_hash(name);
1349 		if (((vdp = ld_vers_find(name, hash,
1350 		    ofl->ofl_verdesc)) == NULL) &&
1351 		    ((vdp = ld_vers_desc(name, hash,
1352 		    &ofl->ofl_verdesc)) == (Ver_desc *)S_ERROR))
1353 			return (S_ERROR);
1354 
1355 		/*
1356 		 * Initialize any new version with an index, the file from which
1357 		 * it was first referenced, and a WEAK flag (indicates that
1358 		 * there are no symbols assigned to it yet).
1359 		 */
1360 		if (vdp->vd_ndx == 0) {
1361 			/* LINTED */
1362 			vdp->vd_ndx = (Half)++ofl->ofl_vercnt;
1363 			vdp->vd_file = ifl;
1364 			vdp->vd_flags = VER_FLG_WEAK;
1365 		}
1366 	} else {
1367 		/*
1368 		 * If a version definition hasn't been specified assign any
1369 		 * symbols to the base version.
1370 		 */
1371 		vdp = (Ver_desc *)ofl->ofl_verdesc->apl_data[0];
1372 	}
1373 
1374 	/*
1375 	 * Scan the mapfile entry picking out scoping and symbol definitions.
1376 	 */
1377 	while ((tok = gettoken(ofl, mapfile, 0)) != TK_RIGHTBKT) {
1378 		Sym_desc	*sdp;
1379 		Word		shndx = SHN_UNDEF;
1380 		uchar_t 	type = STT_NOTYPE;
1381 		Addr		value = 0, size = 0;
1382 		char		*_name, *filtee = NULL;
1383 		Word		sym_flags = 0;
1384 		Half		sym_flags1 = 0;
1385 		uint_t		filter = 0, novalue = 1, dftflag;
1386 		const char	*conflict;
1387 
1388 		if ((tok != TK_STRING) && (tok != TK_COLON)) {
1389 			if (tok == TK_ERROR)
1390 				eprintf(ofl->ofl_lml, ERR_FATAL,
1391 				    MSG_INTL(MSG_MAP_EXPSYM_2), mapfile,
1392 				    EC_XWORD(Line_num));
1393 			if ((tok == TK_ERROR) || (tok == TK_EOF))
1394 				return (S_ERROR);
1395 			errcnt++;
1396 			continue;
1397 		}
1398 
1399 		if ((_name = libld_malloc(strlen(Start_tok) + 1)) == NULL)
1400 			return (S_ERROR);
1401 		(void) strcpy(_name, Start_tok);
1402 
1403 		if (tok != TK_COLON) {
1404 			tok = gettoken(ofl, mapfile, 0);
1405 			if ((tok == TK_ERROR) || (tok == TK_EOF)) {
1406 				errcnt++;
1407 				continue;
1408 			}
1409 		}
1410 
1411 		/*
1412 		 * Turn off the WEAK flag to indicate that definitions are
1413 		 * associated with this version.  It would probably be more
1414 		 * accurate to only remove this flag with the specification of
1415 		 * global symbols, however setting it here allows enough slop
1416 		 * to compensate for the various user inputs we've seen so far.
1417 		 * Only if a closed version is specified (i.e., "SUNW_1.x {};")
1418 		 * will a user get a weak version (which is how we document the
1419 		 * creation of weak versions).
1420 		 */
1421 		vdp->vd_flags &= ~VER_FLG_WEAK;
1422 
1423 		switch (tok) {
1424 		case TK_COLON:
1425 			/*
1426 			 * Establish a new scope.  All symbols added by this
1427 			 * mapfile are actually global entries, and are assigned
1428 			 * the scope that is presently in effect.
1429 			 *
1430 			 * If a protected/symbolic scope is detected, remember
1431 			 * this.  If a protected/symbolic scope is the only
1432 			 * scope defined in this (or any other mapfiles), then
1433 			 * the mode -Bsymbolic is established.
1434 			 */
1435 			if ((strcmp(MSG_ORIG(MSG_MAP_DEFAULT), _name) == 0) ||
1436 			    (strcmp(MSG_ORIG(MSG_MAP_GLOBAL), _name) == 0)) {
1437 				scope = FLG_SCOPE_DFLT;
1438 				ofl->ofl_flags |= FLG_OF_MAPGLOB;
1439 
1440 			} else if ((strcmp(MSG_ORIG(MSG_MAP_HIDDEN),
1441 			    _name) == 0) ||
1442 			    (strcmp(MSG_ORIG(MSG_STR_LOCAL), _name) == 0)) {
1443 				scope = FLG_SCOPE_HIDD;
1444 
1445 			} else if ((strcmp(MSG_ORIG(MSG_MAP_PROTECTED),
1446 			    _name) == 0) ||
1447 			    (strcmp(MSG_ORIG(MSG_STR_SYMBOLIC), _name) == 0)) {
1448 				scope = FLG_SCOPE_PROT;
1449 				ofl->ofl_flags |= FLG_OF_MAPSYMB;
1450 
1451 			} else if (strcmp(MSG_ORIG(MSG_STR_EXPORTED),
1452 			    _name) == 0) {
1453 				scope = FLG_SCOPE_EXPT;
1454 
1455 			} else if (strcmp(MSG_ORIG(MSG_STR_SINGLETON),
1456 			    _name) == 0) {
1457 				scope = FLG_SCOPE_SNGL;
1458 				ofl->ofl_flags |= FLG_OF_MAPGLOB;
1459 
1460 			} else if (strcmp(MSG_ORIG(MSG_STR_ELIMINATE),
1461 			    _name) == 0) {
1462 				scope = FLG_SCOPE_ELIM;
1463 
1464 			} else {
1465 				eprintf(ofl->ofl_lml, ERR_FATAL,
1466 				    MSG_INTL(MSG_MAP_UNKSYMSCO), mapfile,
1467 				    EC_XWORD(Line_num), _name);
1468 				errcnt++;
1469 			}
1470 			continue;
1471 
1472 		case TK_EQUAL:
1473 			/*
1474 			 * A full blown symbol definition follows.
1475 			 * Determine the symbol type and any virtual address or
1476 			 * alignment specified and then fall through to process
1477 			 * the entire symbols information.
1478 			 */
1479 			while ((tok = gettoken(ofl, mapfile, 0)) !=
1480 			    TK_SEMICOLON) {
1481 				if ((tok == TK_ERROR) || (tok == TK_EOF))
1482 					return (S_ERROR);
1483 				/*
1484 				 * If we had previously seen a filter or
1485 				 * auxiliary filter requirement, the next string
1486 				 * is the filtee itself.
1487 				 */
1488 				if (filter) {
1489 					if (filtee) {
1490 					    /* BEGIN CSTYLED */
1491 					    eprintf(ofl->ofl_lml, ERR_FATAL,
1492 						MSG_INTL(MSG_MAP_MULTFILTEE),
1493 						mapfile, EC_XWORD(Line_num),
1494 						_name);
1495 					    errcnt++;
1496 					    continue;
1497 					    /* END CSTYLED */
1498 					}
1499 					if ((filtee = libld_malloc(
1500 					    strlen(Start_tok) + 1)) == NULL)
1501 						return (S_ERROR);
1502 					(void) strcpy(filtee, Start_tok);
1503 					filter = 0;
1504 					continue;
1505 				}
1506 
1507 				/*
1508 				 * Determine any Value or Size attributes.
1509 				 */
1510 				lowercase(Start_tok);
1511 
1512 				if (Start_tok[0] == 'v' ||
1513 				    Start_tok[0] == 's') {
1514 					char		*end_tok;
1515 					Lword		number;
1516 
1517 					if ((number = (Lword)STRTOADDR(
1518 					    &Start_tok[1], &end_tok, 0)) ==
1519 					    XWORD_MAX) {
1520 						eprintf(ofl->ofl_lml, ERR_FATAL,
1521 						    MSG_INTL(MSG_MAP_SEGADDR),
1522 						    mapfile, EC_XWORD(Line_num),
1523 						    Start_tok,
1524 						    MSG_INTL(MSG_MAP_EXCLIMIT));
1525 						errcnt++;
1526 						continue;
1527 					}
1528 
1529 					if (end_tok !=
1530 					    strchr(Start_tok, '\0')) {
1531 						eprintf(ofl->ofl_lml, ERR_FATAL,
1532 						    MSG_INTL(MSG_MAP_SEGADDR),
1533 						    mapfile, EC_XWORD(Line_num),
1534 						    Start_tok,
1535 						    MSG_INTL(MSG_MAP_NOBADFRM));
1536 						errcnt++;
1537 						continue;
1538 					}
1539 
1540 					switch (*Start_tok) {
1541 					case 'v':
1542 					    /* BEGIN CSTYLED */
1543 					    if (value) {
1544 						eprintf(ofl->ofl_lml, ERR_FATAL,
1545 						    MSG_INTL(MSG_MAP_MOREONCE),
1546 						    mapfile, EC_XWORD(Line_num),
1547 						    MSG_INTL(MSG_MAP_SYMVAL));
1548 						errcnt++;
1549 						continue;
1550 					    }
1551 					    /* LINTED */
1552 					    value = (Addr)number;
1553 					    novalue = 0;
1554 					    break;
1555 					    /* END CSTYLED */
1556 					case 's':
1557 					    /* BEGIN CSTYLED */
1558 					    if (size) {
1559 						eprintf(ofl->ofl_lml, ERR_FATAL,
1560 						    MSG_INTL(MSG_MAP_MOREONCE),
1561 						    mapfile, EC_XWORD(Line_num),
1562 						    MSG_INTL(MSG_MAP_SYMSIZE));
1563 						errcnt++;
1564 						continue;
1565 					    }
1566 					    /* LINTED */
1567 					    size = (Addr)number;
1568 					    break;
1569 					    /* END CSTYLED */
1570 					}
1571 
1572 				} else if (strcmp(Start_tok,
1573 				    MSG_ORIG(MSG_MAP_FUNCTION)) == 0) {
1574 					shndx = SHN_ABS;
1575 					sym_flags |= FLG_SY_SPECSEC;
1576 					type = STT_FUNC;
1577 				} else if (strcmp(Start_tok,
1578 				    MSG_ORIG(MSG_MAP_DATA)) == 0) {
1579 					shndx = SHN_ABS;
1580 					sym_flags |= FLG_SY_SPECSEC;
1581 					type = STT_OBJECT;
1582 				} else if (strcmp(Start_tok,
1583 				    MSG_ORIG(MSG_MAP_COMMON)) == 0) {
1584 					shndx = SHN_COMMON;
1585 					sym_flags |= FLG_SY_SPECSEC;
1586 					type = STT_OBJECT;
1587 				} else if (strcmp(Start_tok,
1588 				    MSG_ORIG(MSG_MAP_PARENT)) == 0) {
1589 					sym_flags |= FLG_SY_PARENT;
1590 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1591 				} else if (strcmp(Start_tok,
1592 				    MSG_ORIG(MSG_MAP_EXTERN)) == 0) {
1593 					sym_flags |= FLG_SY_EXTERN;
1594 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1595 				} else if (strcmp(Start_tok,
1596 				    MSG_ORIG(MSG_MAP_DIRECT)) == 0) {
1597 					sym_flags1 |= FLG_SY1_DIR;
1598 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1599 				} else if (strcmp(Start_tok,
1600 				    MSG_ORIG(MSG_MAP_NODIRECT)) == 0) {
1601 					sym_flags1 |= FLG_SY1_NDIR;
1602 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1603 					ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
1604 				} else if (strcmp(Start_tok,
1605 				    MSG_ORIG(MSG_MAP_FILTER)) == 0) {
1606 					dftflag = filter = FLG_SY_STDFLTR;
1607 					sym_flags |= FLG_SY_STDFLTR;
1608 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1609 					continue;
1610 				} else if (strcmp(Start_tok,
1611 				    MSG_ORIG(MSG_MAP_AUXILIARY)) == 0) {
1612 					dftflag = filter = FLG_SY_AUXFLTR;
1613 					sym_flags |= FLG_SY_AUXFLTR;
1614 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1615 					continue;
1616 				} else if (strcmp(Start_tok,
1617 				    MSG_ORIG(MSG_MAP_INTERPOSE)) == 0) {
1618 					if (!(ofl->ofl_flags & FLG_OF_EXEC)) {
1619 						eprintf(ofl->ofl_lml, ERR_FATAL,
1620 						    MSG_INTL(MSG_MAP_NOINTPOSE),
1621 						    mapfile,
1622 						    EC_XWORD(Line_num));
1623 						errcnt++;
1624 						continue;
1625 					}
1626 					sym_flags |= FLG_SY_INTPOSE;
1627 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1628 					ofl->ofl_dtflags_1 |= DF_1_SYMINTPOSE;
1629 					continue;
1630 				} else if (strcmp(Start_tok,
1631 				    MSG_ORIG(MSG_MAP_DYNSORT)) == 0) {
1632 					sym_flags |= FLG_SY_DYNSORT;
1633 					sym_flags &= ~FLG_SY_NODYNSORT;
1634 					continue;
1635 				} else if (strcmp(Start_tok,
1636 				    MSG_ORIG(MSG_MAP_NODYNSORT)) == 0) {
1637 					sym_flags &= ~FLG_SY_DYNSORT;
1638 					sym_flags |= FLG_SY_NODYNSORT;
1639 					continue;
1640 				} else {
1641 					eprintf(ofl->ofl_lml, ERR_FATAL,
1642 					    MSG_INTL(MSG_MAP_UNKSYMDEF),
1643 					    mapfile, EC_XWORD(Line_num),
1644 					    Start_tok);
1645 					errcnt++;
1646 					continue;
1647 				}
1648 			}
1649 			/* FALLTHROUGH */
1650 
1651 		case TK_SEMICOLON:
1652 			/*
1653 			 * The special auto-reduction directive `*' can be
1654 			 * specified in hidden/local, and eliminate scope.  This
1655 			 * directive indicates that all symbols processed that
1656 			 * are not explicitly defined to be global are to be
1657 			 * reduced to hidden/local scope in, or eliminated from,
1658 			 * the output image.
1659 			 *
1660 			 * An auto-reduction directive also implies that a
1661 			 * version definition be created, as the user has
1662 			 * effectively defined an interface.
1663 			 */
1664 			if (*_name == '*') {
1665 				if (scope == FLG_SCOPE_HIDD)
1666 					ofl->ofl_flags |=
1667 					    (FLG_OF_VERDEF | FLG_OF_AUTOLCL);
1668 				else if (scope == FLG_SCOPE_ELIM) {
1669 					ofl->ofl_flags |=
1670 					    (FLG_OF_VERDEF | FLG_OF_AUTOELM);
1671 				}
1672 				continue;
1673 			}
1674 
1675 			/*
1676 			 * Add the new symbol.  It should be noted that all
1677 			 * symbols added by the mapfile start out with global
1678 			 * scope, thus they will fall through the normal symbol
1679 			 * resolution process.  Symbols defined as locals will
1680 			 * be reduced in scope after all input file processing.
1681 			 */
1682 			/* LINTED */
1683 			hash = (Word)elf_hash(_name);
1684 			DBG_CALL(Dbg_map_version(ofl->ofl_lml, name, _name,
1685 			    scope));
1686 			if ((sdp = ld_sym_find(_name, hash, &where,
1687 			    ofl)) == NULL) {
1688 				if ((sym =
1689 				    libld_calloc(sizeof (Sym), 1)) == NULL)
1690 					return (S_ERROR);
1691 
1692 				/*
1693 				 * Make sure any parent or external declarations
1694 				 * fall back to references.
1695 				 */
1696 				if (sym_flags &
1697 				    (FLG_SY_PARENT | FLG_SY_EXTERN)) {
1698 					/*
1699 					 * Turn it into a reference by setting
1700 					 * the section index to UNDEF.
1701 					 */
1702 					sym->st_shndx = shndx = SHN_UNDEF;
1703 
1704 					/*
1705 					 * It is wrong to to specify size
1706 					 * or value for an external symbol.
1707 					 */
1708 					if ((novalue == 0) || (size != 0)) {
1709 						eprintf(ofl->ofl_lml, ERR_FATAL,
1710 						    MSG_INTL(MSG_MAP_NOEXVLSZ),
1711 						    mapfile,
1712 						    EC_XWORD(Line_num));
1713 						errcnt++;
1714 						continue;
1715 					}
1716 				} else {
1717 					sym->st_shndx = (Half)shndx;
1718 				}
1719 
1720 				sym->st_value = value;
1721 				sym->st_size = size;
1722 				sym->st_info = ELF_ST_INFO(STB_GLOBAL, type);
1723 
1724 				if ((sdp = ld_sym_enter(_name, sym, hash, ifl,
1725 				    ofl, 0, shndx, sym_flags, sym_flags1,
1726 				    &where)) == (Sym_desc *)S_ERROR)
1727 					return (S_ERROR);
1728 
1729 				sdp->sd_flags &= ~FLG_SY_CLEAN;
1730 
1731 				/*
1732 				 * Identify any references.  FLG_SY_MAPREF is
1733 				 * turned off once a relocatable object with
1734 				 * the same symbol is found, thus the existence
1735 				 * of FLG_SY_MAPREF at symbol validation is
1736 				 * used to flag undefined/misspelled entries.
1737 				 */
1738 				if (sym->st_shndx == SHN_UNDEF)
1739 					sdp->sd_flags |=
1740 					    (FLG_SY_MAPREF | FLG_SY_GLOBREF);
1741 
1742 			} else {
1743 				conflict = NULL;
1744 				sym = sdp->sd_sym;
1745 
1746 				/*
1747 				 * If this symbol already exists, make sure this
1748 				 * definition doesn't conflict with the former.
1749 				 * Provided it doesn't, multiple definitions
1750 				 * from different mapfiles can augment each
1751 				 * other.
1752 				 */
1753 				/* BEGIN CSTYLED */
1754 				if (sym->st_value) {
1755 				    if (value && (sym->st_value != value))
1756 					conflict =
1757 					    MSG_INTL(MSG_MAP_DIFF_SYMVAL);
1758 				} else {
1759 					sym->st_value = value;
1760 				}
1761 				if (sym->st_size) {
1762 				    if (size && (sym->st_size != size))
1763 					conflict = MSG_INTL(MSG_MAP_DIFF_SYMSZ);
1764 				} else {
1765 					sym->st_size = size;
1766 				}
1767 				if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) {
1768 				    if ((type != STT_NOTYPE) &&
1769 					(ELF_ST_TYPE(sym->st_info) != type))
1770 					    conflict =
1771 						MSG_INTL(MSG_MAP_DIFF_SYMTYP);
1772 				} else {
1773 					sym->st_info =
1774 					    ELF_ST_INFO(STB_GLOBAL, type);
1775 				}
1776 				if (sym->st_shndx != SHN_UNDEF) {
1777 				    if ((shndx != SHN_UNDEF) &&
1778 					(sym->st_shndx != shndx))
1779 					    conflict =
1780 						MSG_INTL(MSG_MAP_DIFF_SYMNDX);
1781 				} else {
1782 					sdp->sd_shndx = sym->st_shndx = shndx;
1783 				}
1784 				/* END CSTYLED */
1785 
1786 				if ((sdp->sd_flags1 & MSK_SY1_GLOBAL) &&
1787 				    (sdp->sd_aux->sa_overndx !=
1788 				    VER_NDX_GLOBAL) &&
1789 				    (vdp->vd_ndx != VER_NDX_GLOBAL) &&
1790 				    (sdp->sd_aux->sa_overndx != vdp->vd_ndx)) {
1791 					conflict =
1792 					    MSG_INTL(MSG_MAP_DIFF_SYMVER);
1793 				}
1794 
1795 				if (conflict) {
1796 					eprintf(ofl->ofl_lml, ERR_FATAL,
1797 					    MSG_INTL(MSG_MAP_SYMDEF1), mapfile,
1798 					    EC_XWORD(Line_num), demangle(_name),
1799 					    sdp->sd_file->ifl_name, conflict);
1800 					errcnt++;
1801 					continue;
1802 				}
1803 
1804 				/*
1805 				 * If this mapfile entry supplies a definition,
1806 				 * indicate that the symbol is now used.
1807 				 */
1808 				if (shndx != SHN_UNDEF)
1809 					sdp->sd_flags |= FLG_SY_MAPUSED;
1810 			}
1811 
1812 			/*
1813 			 * A symbol declaration that defines a size but no
1814 			 * value is processed as a request to create an
1815 			 * associated backing section.  The intent behind this
1816 			 * functionality is to provide OBJT definitions within
1817 			 * filters that are not ABS.  ABS symbols don't allow
1818 			 * copy-relocations to be established to filter OBJT
1819 			 * definitions.
1820 			 */
1821 			if ((shndx == SHN_ABS) && size && novalue) {
1822 				/* Create backing section if not there */
1823 				if (sdp->sd_isc == NULL) {
1824 					Is_desc	*isp;
1825 
1826 					if (type == STT_OBJECT) {
1827 						if ((isp = ld_make_data(ofl,
1828 						    size)) ==
1829 						    (Is_desc *)S_ERROR)
1830 							return (S_ERROR);
1831 					} else {
1832 						if ((isp = ld_make_text(ofl,
1833 						    size)) ==
1834 						    (Is_desc *)S_ERROR)
1835 							return (S_ERROR);
1836 					}
1837 
1838 					sdp->sd_isc = isp;
1839 					isp->is_file = ifl;
1840 				}
1841 
1842 				/*
1843 				 * Now that backing storage has been created,
1844 				 * associate the symbol descriptor.  Remove the
1845 				 * symbols special section tag so that it will
1846 				 * be assigned the correct section index as part
1847 				 * of update symbol processing.
1848 				 */
1849 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1850 				sym_flags &= ~FLG_SY_SPECSEC;
1851 			}
1852 
1853 			/*
1854 			 * Indicate the new symbols scope.  Although the
1855 			 * symbols st_other field will eventually be updated as
1856 			 * part of writing out the final symbol, update the
1857 			 * st_other field here to trigger better diagnostics
1858 			 * during symbol validation (for example, undefined
1859 			 * references that are defined symbolic in a mapfile).
1860 			 */
1861 			if (scope == FLG_SCOPE_HIDD) {
1862 				/*
1863 				 * This symbol needs to be reduced to local.
1864 				 */
1865 				if (ofl->ofl_flags & FLG_OF_REDLSYM) {
1866 					sdp->sd_flags1 |=
1867 					    (FLG_SY1_HIDDEN | FLG_SY1_ELIM);
1868 					sdp->sd_sym->st_other = STV_ELIMINATE;
1869 				} else {
1870 					sdp->sd_flags1 |= FLG_SY1_HIDDEN;
1871 					sdp->sd_sym->st_other = STV_HIDDEN;
1872 				}
1873 			} else if (scope == FLG_SCOPE_ELIM) {
1874 				/*
1875 				 * This symbol needs to be eliminated.  Note,
1876 				 * the symbol is also tagged as local to trigger
1877 				 * any necessary relocation processing prior
1878 				 * to the symbol being eliminated.
1879 				 */
1880 				sdp->sd_flags1 |=
1881 				    (FLG_SY1_HIDDEN | FLG_SY1_ELIM);
1882 				sdp->sd_sym->st_other = STV_ELIMINATE;
1883 
1884 			} else {
1885 				/*
1886 				 * This symbol is explicitly defined to remain
1887 				 * global.
1888 				 */
1889 				sdp->sd_flags |= sym_flags;
1890 				sdp->sd_flags1 |= sym_flags1;
1891 
1892 				/*
1893 				 * Qualify any global scope.
1894 				 */
1895 				if (scope == FLG_SCOPE_SNGL) {
1896 					sdp->sd_flags1 |=
1897 					    (FLG_SY1_SINGLE | FLG_SY1_NDIR);
1898 					sdp->sd_sym->st_other = STV_SINGLETON;
1899 				} else if (scope == FLG_SCOPE_PROT) {
1900 					sdp->sd_flags1 |= FLG_SY1_PROTECT;
1901 					sdp->sd_sym->st_other = STV_PROTECTED;
1902 				} else if (scope == FLG_SCOPE_EXPT) {
1903 					sdp->sd_flags1 |= FLG_SY1_EXPORT;
1904 					sdp->sd_sym->st_other = STV_EXPORTED;
1905 				} else
1906 					sdp->sd_flags1 |= FLG_SY1_DEFAULT;
1907 
1908 				/*
1909 				 * Record the present version index for later
1910 				 * potential versioning.
1911 				 */
1912 				if ((sdp->sd_aux->sa_overndx == 0) ||
1913 				    (sdp->sd_aux->sa_overndx == VER_NDX_GLOBAL))
1914 					sdp->sd_aux->sa_overndx = vdp->vd_ndx;
1915 				vdp->vd_flags |= FLG_VER_REFER;
1916 			}
1917 
1918 			conflict = NULL;
1919 
1920 			/*
1921 			 * Carry out some validity checks to ensure incompatible
1922 			 * symbol characteristics have not been defined.
1923 			 * These checks are carried out after symbols are added
1924 			 * or resolved, to catch single instance, and
1925 			 * multi-instance definition inconsistencies.
1926 			 */
1927 			if ((sdp->sd_flags1 &
1928 			    (FLG_SY1_HIDDEN | FLG_SY1_ELIM)) &&
1929 			    ((scope != FLG_SCOPE_HIDD) &&
1930 			    (scope != FLG_SCOPE_ELIM))) {
1931 				conflict = MSG_INTL(MSG_MAP_DIFF_SYMLCL);
1932 
1933 			} else if (((sdp->sd_flags1 & FLG_SY1_SINGLE) ||
1934 			    (sdp->sd_flags1 & FLG_SY1_EXPORT)) &&
1935 			    ((scope != FLG_SCOPE_DFLT) &&
1936 			    (scope != FLG_SCOPE_EXPT) &&
1937 			    (scope != FLG_SCOPE_SNGL))) {
1938 				conflict = MSG_INTL(MSG_MAP_DIFF_SYMGLOB);
1939 
1940 			} else if ((sdp->sd_flags1 & FLG_SY1_PROTECT) &&
1941 			    ((scope != FLG_SCOPE_DFLT) &&
1942 			    (scope != FLG_SCOPE_PROT))) {
1943 				conflict = MSG_INTL(MSG_MAP_DIFF_SYMPROT);
1944 
1945 			} else if ((sdp->sd_flags1 & FLG_SY1_NDIR) &&
1946 			    (scope == FLG_SCOPE_PROT)) {
1947 				conflict = MSG_INTL(MSG_MAP_DIFF_PROTNDIR);
1948 
1949 			} else if ((sdp->sd_flags1 & FLG_SY1_DIR) &&
1950 			    (scope == FLG_SCOPE_SNGL)) {
1951 				conflict = MSG_INTL(MSG_MAP_DIFF_SNGLDIR);
1952 			}
1953 
1954 			if (conflict) {
1955 				/*
1956 				 * Select the conflict message from either a
1957 				 * single instance or multi-instance definition.
1958 				 */
1959 				if (sdp->sd_file->ifl_name == mapfile) {
1960 					eprintf(ofl->ofl_lml, ERR_FATAL,
1961 					    MSG_INTL(MSG_MAP_SYMDEF2), mapfile,
1962 					    EC_XWORD(Line_num), demangle(_name),
1963 					    conflict);
1964 				} else {
1965 					eprintf(ofl->ofl_lml, ERR_FATAL,
1966 					    MSG_INTL(MSG_MAP_SYMDEF1), mapfile,
1967 					    EC_XWORD(Line_num), demangle(_name),
1968 					    sdp->sd_file->ifl_name, conflict);
1969 				}
1970 				errcnt++;
1971 				continue;
1972 			}
1973 
1974 			/*
1975 			 * Indicate that this symbol has been explicitly
1976 			 * contributed from a mapfile.
1977 			 */
1978 			sdp->sd_flags1 |= (FLG_SY1_MAPFILE | FLG_SY1_EXPDEF);
1979 
1980 			/*
1981 			 * If we've encountered a symbol definition simulate
1982 			 * that an input file has been processed - this allows
1983 			 * things like filters to be created purely from a
1984 			 * mapfile.
1985 			 */
1986 			if (type != STT_NOTYPE)
1987 				ofl->ofl_objscnt++;
1988 			DBG_CALL(Dbg_map_symbol(ofl, sdp));
1989 
1990 			/*
1991 			 * If this symbol has an associated filtee, record the
1992 			 * filtee string and associate the string index with the
1993 			 * symbol.  This is used later to associate the syminfo
1994 			 * information with the necessary .dynamic entry.
1995 			 */
1996 			if (filter && (filtee == NULL)) {
1997 				eprintf(ofl->ofl_lml, ERR_FATAL,
1998 				    MSG_INTL(MSG_MAP_NOFILTER), mapfile,
1999 				    EC_XWORD(Line_num), _name);
2000 				errcnt++;
2001 				continue;
2002 			}
2003 
2004 			if (filtee) {
2005 				Dfltr_desc *	dftp;
2006 				Sfltr_desc	sft;
2007 				Aliste		idx, _idx, nitems;
2008 
2009 				/*
2010 				 * Make sure we don't duplicate any filtee
2011 				 * strings, and create a new descriptor if
2012 				 * necessary.
2013 				 */
2014 				idx = nitems = alist_nitems(ofl->ofl_dtsfltrs);
2015 				for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, _idx,
2016 				    dftp)) {
2017 					if ((dftflag != dftp->dft_flag) ||
2018 					    (strcmp(dftp->dft_str, filtee)))
2019 						continue;
2020 					idx = _idx;
2021 					break;
2022 				}
2023 				if (idx == nitems) {
2024 					Dfltr_desc	dft;
2025 
2026 					dft.dft_str = filtee;
2027 					dft.dft_flag = dftflag;
2028 					dft.dft_ndx = 0;
2029 
2030 					/*
2031 					 * The following append puts the new
2032 					 * item at the offset contained in
2033 					 * idx, because we know idx contains
2034 					 * the index of the next available slot.
2035 					 */
2036 					if (alist_append(&ofl->ofl_dtsfltrs,
2037 					    &dft, sizeof (Dfltr_desc),
2038 					    AL_CNT_OFL_DTSFLTRS) == NULL)
2039 						return (S_ERROR);
2040 				}
2041 
2042 				/*
2043 				 * Create a new filter descriptor for this
2044 				 * symbol.
2045 				 */
2046 				sft.sft_sdp = sdp;
2047 				sft.sft_idx = idx;
2048 
2049 				if (alist_append(&ofl->ofl_symfltrs,
2050 				    &sft, sizeof (Sfltr_desc),
2051 				    AL_CNT_OFL_SYMFLTRS) == NULL)
2052 					return (S_ERROR);
2053 			}
2054 			break;
2055 
2056 		default:
2057 			eprintf(ofl->ofl_lml, ERR_FATAL,
2058 			    MSG_INTL(MSG_MAP_EXPSCOL), mapfile,
2059 			    EC_XWORD(Line_num));
2060 			errcnt++;
2061 			continue;
2062 		}
2063 	}
2064 
2065 	if (errcnt)
2066 		return (S_ERROR);
2067 
2068 	/*
2069 	 * Determine if any version references are provided after the close
2070 	 * bracket.
2071 	 */
2072 	while ((tok = gettoken(ofl, mapfile, 0)) != TK_SEMICOLON) {
2073 		Ver_desc	*_vdp;
2074 		char		*_name;
2075 
2076 		if (tok != TK_STRING) {
2077 			if (tok != TK_ERROR)
2078 				eprintf(ofl->ofl_lml, ERR_FATAL,
2079 				    MSG_INTL(MSG_MAP_EXPVERS), mapfile,
2080 				    EC_XWORD(Line_num));
2081 			return (S_ERROR);
2082 		}
2083 
2084 		name = Start_tok;
2085 		if (vdp->vd_ndx == VER_NDX_GLOBAL) {
2086 			eprintf(ofl->ofl_lml, ERR_WARNING,
2087 			    MSG_INTL(MSG_MAP_UNEXDEP), mapfile,
2088 			    EC_XWORD(Line_num), name);
2089 			continue;
2090 		}
2091 
2092 		/*
2093 		 * Generate a new version descriptor if it doesn't already
2094 		 * exist.
2095 		 */
2096 		/* LINTED */
2097 		hash = (Word)elf_hash(name);
2098 		if ((_vdp = ld_vers_find(name, hash,
2099 		    ofl->ofl_verdesc)) == NULL) {
2100 			if ((_name = libld_malloc(strlen(name) + 1)) == NULL)
2101 				return (S_ERROR);
2102 			(void) strcpy(_name, name);
2103 
2104 			if ((_vdp = ld_vers_desc(_name, hash,
2105 			    &ofl->ofl_verdesc)) == (Ver_desc *)S_ERROR)
2106 				return (S_ERROR);
2107 		}
2108 
2109 		/*
2110 		 * Add the new version descriptor to the parent version
2111 		 * descriptors reference list.  Indicate the version descriptors
2112 		 * first reference (used for error disgnostics if undefined
2113 		 * version dependencies remain).
2114 		 */
2115 		if (ld_vers_find(name, hash, vdp->vd_deps) == NULL)
2116 			if (aplist_append(&vdp->vd_deps, _vdp,
2117 			    AL_CNT_VERDESCS) == NULL)
2118 				return (S_ERROR);
2119 
2120 		if (_vdp->vd_ref == NULL)
2121 			_vdp->vd_ref = vdp;
2122 	}
2123 	return (1);
2124 }
2125 
2126 /*
2127  * If a user has provided segment definitions via a mapfile, and these segments
2128  * have been assigned virtual addresses, sort the associated segments by
2129  * increasing virtual address.
2130  *
2131  * Only PT_LOAD segments can be assigned a virtual address.  These segments can
2132  * be one of two types:
2133  *
2134  *  -	Standard segments for text, data or bss.  These segments will have been
2135  *	inserted before the default text (first PT_LOAD) segment.
2136  *
2137  *  -	Empty (reservation) segments.  These segment will have been inserted at
2138  *	the end of any default PT_LOAD segments.
2139  *
2140  * Any standard segments that are assigned a virtual address will be sorted,
2141  * and as their definitions precede any default PT_LOAD segments, these segments
2142  * will be assigned sections before any defaults.
2143  *
2144  * Any reservation segments are also sorted amoung themselves, as these segments
2145  * must still follow the standard default segments.
2146  */
2147 uintptr_t
2148 ld_sort_seg_list(Ofl_desc *ofl)
2149 {
2150 	APlist	*seg1 = NULL, *seg2 = NULL;
2151 	Sg_desc	*sgp1;
2152 	Aliste	idx1;
2153 
2154 #define	FIRST_SEGMENT(type) \
2155 	((type == PT_PHDR) || (type == PT_INTERP) || (type == PT_SUNWCAP))
2156 
2157 	/*
2158 	 * Add the .phdr and .interp segments to our list.  These segments must
2159 	 * occur before any PT_LOAD segments (refer exec/elf/elf.c).  Also add
2160 	 * the capabilities segment.  This isn't essential, but the capabilities
2161 	 * section is one of the first in an object.
2162 	 */
2163 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) {
2164 		Word	type = sgp1->sg_phdr.p_type;
2165 
2166 		if (FIRST_SEGMENT(type)) {
2167 			if (aplist_append(&seg1, sgp1, AL_CNT_SEGMENTS) == NULL)
2168 				return (S_ERROR);
2169 		}
2170 	}
2171 
2172 	/*
2173 	 * Add the loadable segments to another list in sorted order.
2174 	 */
2175 	DBG_CALL(Dbg_map_sort(ofl->ofl_lml));
2176 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) {
2177 		DBG_CALL(Dbg_map_sort_seg(ofl->ofl_lml, sgp1, 1));
2178 
2179 		if (sgp1->sg_phdr.p_type != PT_LOAD)
2180 			continue;
2181 
2182 		/*
2183 		 * If the loadable segment does not contain a vaddr, simply
2184 		 * append it to the new list.
2185 		 */
2186 		if ((sgp1->sg_flags & FLG_SG_VADDR) == 0) {
2187 			if (aplist_append(&seg2, sgp1, AL_CNT_SEGMENTS) == NULL)
2188 				return (S_ERROR);
2189 
2190 		} else {
2191 			Aliste		idx2;
2192 			Sg_desc		*sgp2;
2193 			int		inserted = 0;
2194 
2195 			/*
2196 			 * Traverse the segment list we are creating, looking
2197 			 * for a segment that defines a vaddr.
2198 			 */
2199 			for (APLIST_TRAVERSE(seg2, idx2, sgp2)) {
2200 				/*
2201 				 * Any real segments that contain vaddr's need
2202 				 * to be sorted.  Any reservation segments also
2203 				 * need to be sorted.  However, any reservation
2204 				 * segments should be placed after any real
2205 				 * segments.
2206 				 */
2207 				if (((sgp2->sg_flags &
2208 				    (FLG_SG_VADDR | FLG_SG_EMPTY)) == 0) &&
2209 				    (sgp1->sg_flags & FLG_SG_EMPTY))
2210 					continue;
2211 
2212 				if ((sgp2->sg_flags & FLG_SG_VADDR) &&
2213 				    ((sgp2->sg_flags & FLG_SG_EMPTY) ==
2214 				    (sgp1->sg_flags & FLG_SG_EMPTY))) {
2215 					if (sgp1->sg_phdr.p_vaddr ==
2216 					    sgp2->sg_phdr.p_vaddr) {
2217 						eprintf(ofl->ofl_lml, ERR_FATAL,
2218 						    MSG_INTL(MSG_MAP_SEGSAME),
2219 						    sgp1->sg_name,
2220 						    sgp2->sg_name);
2221 						return (S_ERROR);
2222 					}
2223 
2224 					if (sgp1->sg_phdr.p_vaddr >
2225 					    sgp2->sg_phdr.p_vaddr)
2226 						continue;
2227 				}
2228 
2229 				/*
2230 				 * Insert this segment before the segment on
2231 				 * the seg2 list.
2232 				 */
2233 				if (aplist_insert(&seg2, sgp1, AL_CNT_SEGMENTS,
2234 				    idx2) == NULL)
2235 					return (S_ERROR);
2236 				inserted = 1;
2237 				break;
2238 			}
2239 
2240 			/*
2241 			 * If the segment being inspected has not been inserted
2242 			 * in the segment list, simply append it to the list.
2243 			 */
2244 			if ((inserted == 0) && (aplist_append(&seg2,
2245 			    sgp1, AL_CNT_SEGMENTS) == NULL))
2246 				return (S_ERROR);
2247 		}
2248 	}
2249 
2250 	/*
2251 	 * Add the sorted loadable segments to our initial segment list.
2252 	 */
2253 	for (APLIST_TRAVERSE(seg2, idx1, sgp1)) {
2254 		if (aplist_append(&seg1, sgp1, AL_CNT_SEGMENTS) == NULL)
2255 			return (S_ERROR);
2256 	}
2257 
2258 	/*
2259 	 * Add all other segments to our list.
2260 	 */
2261 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) {
2262 		Word	type = sgp1->sg_phdr.p_type;
2263 
2264 		if (!FIRST_SEGMENT(type) && (type != PT_LOAD)) {
2265 			if (aplist_append(&seg1, sgp1, AL_CNT_SEGMENTS) == NULL)
2266 				return (S_ERROR);
2267 		}
2268 	}
2269 	free((void *)ofl->ofl_segs);
2270 	ofl->ofl_segs = NULL;
2271 
2272 	/*
2273 	 * Now rebuild the original list and process all of the
2274 	 * segment/section ordering information if present.
2275 	 */
2276 	for (APLIST_TRAVERSE(seg1, idx1, sgp1)) {
2277 		DBG_CALL(Dbg_map_sort_seg(ofl->ofl_lml, sgp1, 0));
2278 		if (aplist_append(&ofl->ofl_segs, sgp1,
2279 		    AL_CNT_SEGMENTS) == NULL)
2280 			return (S_ERROR);
2281 	}
2282 
2283 #undef	FIRST_SEGMENT
2284 
2285 	return (1);
2286 }
2287 
2288 /*
2289  * Parse the mapfile.
2290  */
2291 uintptr_t
2292 ld_map_parse(const char *mapfile, Ofl_desc *ofl)
2293 {
2294 	struct stat	stat_buf;	/* stat of mapfile */
2295 	int		mapfile_fd;	/* descriptor for mapfile */
2296 	Sg_desc		*sgp1;		/* seg descriptor being manipulated */
2297 	Sg_desc		*sgp2;		/* temp segment descriptor pointer */
2298 	Ent_desc	*enp;		/* Segment entrance criteria. */
2299 	Token		tok;		/* current token. */
2300 	Aliste		endx = 0;	/* next place for entrance criterion */
2301 	Boolean		new_segment;	/* If true, defines new segment. */
2302 	char		*name;
2303 	static	int	num_stack = 0;	/* number of stack segment */
2304 	int		err;
2305 
2306 	DBG_CALL(Dbg_map_parse(ofl->ofl_lml, mapfile));
2307 
2308 	/*
2309 	 * Determine if we're dealing with a file or a directory.
2310 	 */
2311 	if (stat(mapfile, &stat_buf) == -1) {
2312 		err = errno;
2313 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_STAT),
2314 		    mapfile, strerror(err));
2315 		return (S_ERROR);
2316 	}
2317 	if (S_ISDIR(stat_buf.st_mode)) {
2318 		DIR		*dirp;
2319 		struct dirent	*denp;
2320 
2321 		/*
2322 		 * Open the directory and interpret each visible file as a
2323 		 * mapfile.
2324 		 */
2325 		if ((dirp = opendir(mapfile)) == NULL)
2326 			return (1);
2327 
2328 		while ((denp = readdir(dirp)) != NULL) {
2329 			char	path[PATH_MAX];
2330 
2331 			/*
2332 			 * Ignore any hidden filenames.  Construct the full
2333 			 * pathname to the new mapfile.
2334 			 */
2335 			if (*denp->d_name == '.')
2336 				continue;
2337 			(void) snprintf(path, PATH_MAX, MSG_ORIG(MSG_STR_PATH),
2338 			    mapfile, denp->d_name);
2339 			if (ld_map_parse(path, ofl) == S_ERROR)
2340 				return (S_ERROR);
2341 		}
2342 		(void) closedir(dirp);
2343 		return (1);
2344 	} else if (!S_ISREG(stat_buf.st_mode)) {
2345 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_NOTREG),
2346 		    mapfile);
2347 		return (S_ERROR);
2348 	}
2349 
2350 	/*
2351 	 * We read the entire mapfile into memory.
2352 	 */
2353 	if ((Mapspace = libld_malloc(stat_buf.st_size + 1)) == NULL)
2354 		return (S_ERROR);
2355 	if ((mapfile_fd = open(mapfile, O_RDONLY)) == -1) {
2356 		err = errno;
2357 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
2358 		    mapfile, strerror(err));
2359 		return (S_ERROR);
2360 	}
2361 
2362 	if (read(mapfile_fd, Mapspace, stat_buf.st_size) != stat_buf.st_size) {
2363 		err = errno;
2364 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_READ),
2365 		    mapfile, strerror(err));
2366 		return (S_ERROR);
2367 	}
2368 	Mapspace[stat_buf.st_size] = '\0';
2369 	nextchr = Mapspace;
2370 
2371 	/*
2372 	 * Set up any global variables, the line number counter and file name.
2373 	 */
2374 	Line_num = 1;
2375 
2376 	/*
2377 	 * We now parse the mapfile until the gettoken routine returns EOF.
2378 	 */
2379 	while ((tok = gettoken(ofl, mapfile, 1)) != TK_EOF) {
2380 		Aliste	idx;
2381 		int	ndx;
2382 
2383 		/*
2384 		 * Don't know which segment yet.
2385 		 */
2386 		sgp1 = NULL;
2387 
2388 		/*
2389 		 * At this point we are at the beginning of a line, and the
2390 		 * variable `Start_tok' points to the first string on the line.
2391 		 * All mapfile entries start with some string token except it
2392 		 * is possible for a scoping definition to start with `{'.
2393 		 */
2394 		if (tok == TK_LEFTBKT) {
2395 			if (map_version(mapfile, (char *)0, ofl) == S_ERROR)
2396 				return (S_ERROR);
2397 			continue;
2398 		}
2399 		if (tok != TK_STRING) {
2400 			if (tok != TK_ERROR)
2401 				eprintf(ofl->ofl_lml, ERR_FATAL,
2402 				    MSG_INTL(MSG_MAP_EXPSEGNAM), mapfile,
2403 				    EC_XWORD(Line_num));
2404 			return (S_ERROR);
2405 		}
2406 
2407 		/*
2408 		 * Save the initial token.
2409 		 */
2410 		if ((name = libld_malloc(strlen(Start_tok) + 1)) == NULL)
2411 			return (S_ERROR);
2412 		(void) strcpy(name, Start_tok);
2413 
2414 		/*
2415 		 * Now check the second character on the line.  The special `-'
2416 		 * and `{' characters do not involve any segment manipulation so
2417 		 * we handle them first.
2418 		 */
2419 		tok = gettoken(ofl, mapfile, 0);
2420 		if ((tok == TK_ERROR) || (tok == TK_EOF))
2421 			return (S_ERROR);
2422 		if (tok == TK_DASH) {
2423 			if (map_dash(mapfile, name, ofl) == S_ERROR)
2424 				return (S_ERROR);
2425 			continue;
2426 		}
2427 		if (tok == TK_LEFTBKT) {
2428 			if (map_version(mapfile, name, ofl) == S_ERROR)
2429 				return (S_ERROR);
2430 			continue;
2431 		}
2432 
2433 		/*
2434 		 * If we're here we need to interpret the first string as a
2435 		 * segment name.  Find the segment named in the token.
2436 		 */
2437 		ndx = 0;
2438 		for (APLIST_TRAVERSE(ofl->ofl_segs, idx, sgp2)) {
2439 			if (strcmp(sgp2->sg_name, name) == 0) {
2440 				sgp1 = sgp2;
2441 				sgp2->sg_flags &= ~FLG_SG_DISABLED;
2442 				new_segment = FALSE;
2443 				break;
2444 			}
2445 			ndx++;
2446 		}
2447 
2448 		/*
2449 		 * If the second token is a '|' then we had better
2450 		 * of found a segment.  It is illegal to perform
2451 		 * section within segment ordering before the segment
2452 		 * has been declared.
2453 		 */
2454 		if (tok == TK_PIPE) {
2455 			if (sgp1 == NULL) {
2456 				eprintf(ofl->ofl_lml, ERR_FATAL,
2457 				    MSG_INTL(MSG_MAP_SECINSEG), mapfile,
2458 				    EC_XWORD(Line_num), name);
2459 				return (S_ERROR);
2460 			} else {
2461 				if (map_pipe(ofl, mapfile, sgp1) == S_ERROR)
2462 					return (S_ERROR);
2463 				continue;
2464 			}
2465 		}
2466 
2467 		/*
2468 		 * If segment is still NULL then it does not exist.  Create a
2469 		 * new segment, and leave its values as 0 so that map_equal()
2470 		 * can detect changing attributes.
2471 		 */
2472 		if (sgp1 == NULL) {
2473 			if ((sgp1 =
2474 			    libld_calloc(sizeof (Sg_desc), 1)) == NULL)
2475 				return (S_ERROR);
2476 			sgp1->sg_phdr.p_type = PT_NULL;
2477 			sgp1->sg_name = name;
2478 			new_segment = TRUE;
2479 		}
2480 
2481 		if ((strcmp(sgp1->sg_name, MSG_ORIG(MSG_STR_INTERP)) == 0) ||
2482 		    (strcmp(sgp1->sg_name, MSG_ORIG(MSG_STR_LD_DYNAMIC)) ==
2483 		    0)) {
2484 			eprintf(ofl->ofl_lml, ERR_FATAL,
2485 			    MSG_INTL(MSG_MAP_SEGRESV), mapfile,
2486 			    EC_XWORD(Line_num));
2487 			return (S_ERROR);
2488 		}
2489 
2490 		/*
2491 		 * Now check the second token from the input line.
2492 		 */
2493 		if (tok == TK_EQUAL) {
2494 			if (strcmp(sgp1->sg_name,
2495 			    MSG_ORIG(MSG_STR_HWCAP_1)) == 0) {
2496 				if (map_cap(mapfile, CA_SUNW_HW_1,
2497 				    ofl) == S_ERROR)
2498 					return (S_ERROR);
2499 				DBG_CALL(Dbg_cap_mapfile(ofl->ofl_lml,
2500 				    CA_SUNW_HW_1, ofl->ofl_hwcap_1,
2501 				    ld_targ.t_m.m_mach));
2502 				continue;
2503 
2504 			} else if (strcmp(sgp1->sg_name,
2505 			    MSG_ORIG(MSG_STR_SFCAP_1)) == 0) {
2506 				if (map_cap(mapfile, CA_SUNW_SF_1,
2507 				    ofl) == S_ERROR)
2508 					return (S_ERROR);
2509 				DBG_CALL(Dbg_cap_mapfile(ofl->ofl_lml,
2510 				    CA_SUNW_SF_1, ofl->ofl_sfcap_1,
2511 				    ld_targ.t_m.m_mach));
2512 				continue;
2513 
2514 			} else {
2515 				if (map_equal(mapfile, sgp1, ofl) == S_ERROR)
2516 					return (S_ERROR);
2517 				DBG_CALL(Dbg_map_set_equal(new_segment));
2518 			}
2519 		} else if (tok == TK_COLON) {
2520 			/*
2521 			 * If this is an existing segment reservation, sections
2522 			 * can't be assigned to it.
2523 			 */
2524 			if ((new_segment == FALSE) &&
2525 			    (sgp1->sg_flags & FLG_SG_EMPTY)) {
2526 				eprintf(ofl->ofl_lml, ERR_FATAL,
2527 				    MSG_INTL(MSG_MAP_SEGEMPSEC), mapfile,
2528 				    EC_XWORD(Line_num));
2529 				return (S_ERROR);
2530 			}
2531 
2532 			/*
2533 			 * We are looking at a new entrance criteria line.
2534 			 * Note that entrance criteria are added in the order
2535 			 * they are found in the mapfile, but are placed before
2536 			 * any default criteria.
2537 			 */
2538 			if ((enp = alist_insert(&(ofl->ofl_ents), NULL,
2539 			    sizeof (Ent_desc), AL_CNT_OFL_ENTRANCE,
2540 			    endx)) == NULL)
2541 				return (S_ERROR);
2542 
2543 			enp->ec_segment = sgp1;
2544 			endx++;
2545 
2546 			if (map_colon(ofl, mapfile, enp) == S_ERROR)
2547 				return (S_ERROR);
2548 			DBG_CALL(Dbg_map_ent(ofl->ofl_lml, new_segment,
2549 			    enp, ofl));
2550 		} else if (tok == TK_ATSIGN) {
2551 			if (map_atsign(mapfile, sgp1, ofl) == S_ERROR)
2552 				return (S_ERROR);
2553 			DBG_CALL(Dbg_map_set_atsign(new_segment));
2554 		} else if (tok != TK_ERROR) {
2555 			eprintf(ofl->ofl_lml, ERR_FATAL,
2556 			    MSG_INTL(MSG_MAP_EXPEQU), mapfile,
2557 			    EC_XWORD(Line_num));
2558 			return (S_ERROR);
2559 		}
2560 
2561 		/*
2562 		 * Having completed parsing an entry in the mapfile determine
2563 		 * if the segment to which it applies is new.
2564 		 */
2565 		if (new_segment) {
2566 			/*
2567 			 * If specific fields have not been supplied via
2568 			 * map_equal(), make sure defaults are supplied.
2569 			 */
2570 			if (((sgp1->sg_flags & FLG_SG_TYPE) == 0) &&
2571 			    (sgp1->sg_phdr.p_type == PT_NULL)) {
2572 				/*
2573 				 * Default to a loadable segment.
2574 				 */
2575 				sgp1->sg_phdr.p_type = PT_LOAD;
2576 				sgp1->sg_flags |= FLG_SG_TYPE;
2577 			}
2578 			if (sgp1->sg_phdr.p_type == PT_LOAD) {
2579 				if ((sgp1->sg_flags & FLG_SG_FLAGS) == 0) {
2580 					/*
2581 					 * Default to read/write and execute.
2582 					 */
2583 					sgp1->sg_phdr.p_flags =
2584 					    PF_R + PF_W + PF_X;
2585 					sgp1->sg_flags |= FLG_SG_FLAGS;
2586 				}
2587 				if ((sgp1->sg_flags & FLG_SG_ALIGN) == 0) {
2588 					/*
2589 					 * Default to segment alignment
2590 					 */
2591 					sgp1->sg_phdr.p_align =
2592 					    ld_targ.t_m.m_segm_align;
2593 					sgp1->sg_flags |= FLG_SG_ALIGN;
2594 				}
2595 			}
2596 
2597 			/*
2598 			 * Determine where the new item should be inserted in
2599 			 * the segment descriptor list.  Presently the user can
2600 			 * only add the following:
2601 			 *
2602 			 *  PT_LOAD	added before the text segment.
2603 			 *  PT_NULL/empty PT_LOAD
2604 			 *		added after the data/bss segments, thus
2605 			 *		we add before the dynamic segment.
2606 			 *  PT_SUNWSTACK
2607 			 *		added before the final note segment.
2608 			 *  PT_NOTE	added before the final note segment.
2609 			 *
2610 			 * Note that any new segments must always be added
2611 			 * after any PT_PHDR and PT_INTERP (refer Generic ABI,
2612 			 * Page 5-4).
2613 			 */
2614 			switch (sgp1->sg_phdr.p_type) {
2615 			case PT_LOAD:
2616 			case PT_NULL:
2617 				if (sgp1->sg_flags & FLG_SG_EMPTY)
2618 					sgp1->sg_id = LD_DYN;
2619 				else
2620 					sgp1->sg_id = LD_TEXT;
2621 				break;
2622 			case PT_SUNWSTACK:
2623 				sgp1->sg_id = LD_NOTE;
2624 				if (++num_stack >= 2) {
2625 					/*
2626 					 * Currently the number of sunw_stack
2627 					 * segment is limited to 1.
2628 					 */
2629 					eprintf(ofl->ofl_lml, ERR_WARNING,
2630 					    MSG_INTL(MSG_MAP_NOSTACK2),
2631 					    mapfile, EC_XWORD(Line_num));
2632 					continue;
2633 				}
2634 				break;
2635 			case PT_NOTE:
2636 				sgp1->sg_id = LD_NOTE;
2637 				break;
2638 			default:
2639 				eprintf(ofl->ofl_lml, ERR_FATAL,
2640 				    MSG_INTL(MSG_MAP_UNKSEGTYP), mapfile,
2641 				    EC_XWORD(Line_num),
2642 				    EC_WORD(sgp1->sg_phdr.p_type));
2643 				return (S_ERROR);
2644 			}
2645 
2646 			ndx = 0;
2647 			for (APLIST_TRAVERSE(ofl->ofl_segs, idx, sgp2)) {
2648 				if (sgp1->sg_id > sgp2->sg_id) {
2649 					ndx++;
2650 					continue;
2651 				}
2652 
2653 				if (aplist_insert(&ofl->ofl_segs, sgp1,
2654 				    AL_CNT_SEGMENTS, idx) == NULL)
2655 					return (S_ERROR);
2656 				break;
2657 			}
2658 		}
2659 		DBG_CALL(Dbg_map_seg(ofl, ndx, sgp1));
2660 	}
2661 
2662 	/*
2663 	 * If the output file is a static file without an interpreter, and
2664 	 * if any virtual address is specified, then set the ?N flag for
2665 	 * backward compatibility.
2666 	 */
2667 	if (!(ofl->ofl_flags & FLG_OF_DYNAMIC) &&
2668 	    !(ofl->ofl_flags & FLG_OF_RELOBJ) &&
2669 	    !(ofl->ofl_osinterp) &&
2670 	    (ofl->ofl_flags1 & FLG_OF1_VADDR))
2671 		ofl->ofl_dtflags_1 |= DF_1_NOHDR;
2672 
2673 	/*
2674 	 * If the output file is a relocatable file, then ?N has no effect.
2675 	 * Make sure this flag isn't set.
2676 	 */
2677 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
2678 		ofl->ofl_dtflags_1 &= ~DF_1_NOHDR;
2679 
2680 	return (1);
2681 }
2682