xref: /titanic_44/usr/src/cmd/sgs/libld/common/map.c (revision 6b2bcd8e40cb530c97e59f351ceccb5c807ac34a)
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 |=
1604 					    (FLG_OF1_NDIRECT | FLG_OF1_NGLBDIR);
1605 				} else if (strcmp(Start_tok,
1606 				    MSG_ORIG(MSG_MAP_FILTER)) == 0) {
1607 					dftflag = filter = FLG_SY_STDFLTR;
1608 					sym_flags |= FLG_SY_STDFLTR;
1609 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1610 					continue;
1611 				} else if (strcmp(Start_tok,
1612 				    MSG_ORIG(MSG_MAP_AUXILIARY)) == 0) {
1613 					dftflag = filter = FLG_SY_AUXFLTR;
1614 					sym_flags |= FLG_SY_AUXFLTR;
1615 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1616 					continue;
1617 				} else if (strcmp(Start_tok,
1618 				    MSG_ORIG(MSG_MAP_INTERPOSE)) == 0) {
1619 					if (!(ofl->ofl_flags & FLG_OF_EXEC)) {
1620 						eprintf(ofl->ofl_lml, ERR_FATAL,
1621 						    MSG_INTL(MSG_MAP_NOINTPOSE),
1622 						    mapfile,
1623 						    EC_XWORD(Line_num));
1624 						errcnt++;
1625 						continue;
1626 					}
1627 					sym_flags |= FLG_SY_INTPOSE;
1628 					ofl->ofl_flags |= FLG_OF_SYMINFO;
1629 					ofl->ofl_dtflags_1 |= DF_1_SYMINTPOSE;
1630 					continue;
1631 				} else if (strcmp(Start_tok,
1632 				    MSG_ORIG(MSG_MAP_DYNSORT)) == 0) {
1633 					sym_flags |= FLG_SY_DYNSORT;
1634 					sym_flags &= ~FLG_SY_NODYNSORT;
1635 					continue;
1636 				} else if (strcmp(Start_tok,
1637 				    MSG_ORIG(MSG_MAP_NODYNSORT)) == 0) {
1638 					sym_flags &= ~FLG_SY_DYNSORT;
1639 					sym_flags |= FLG_SY_NODYNSORT;
1640 					continue;
1641 				} else {
1642 					eprintf(ofl->ofl_lml, ERR_FATAL,
1643 					    MSG_INTL(MSG_MAP_UNKSYMDEF),
1644 					    mapfile, EC_XWORD(Line_num),
1645 					    Start_tok);
1646 					errcnt++;
1647 					continue;
1648 				}
1649 			}
1650 			/* FALLTHROUGH */
1651 
1652 		case TK_SEMICOLON:
1653 			/*
1654 			 * The special auto-reduction directive `*' can be
1655 			 * specified in hidden/local, and eliminate scope.  This
1656 			 * directive indicates that all symbols processed that
1657 			 * are not explicitly defined to be global are to be
1658 			 * reduced to hidden/local scope in, or eliminated from,
1659 			 * the output image.
1660 			 *
1661 			 * An auto-reduction directive also implies that a
1662 			 * version definition be created, as the user has
1663 			 * effectively defined an interface.
1664 			 */
1665 			if (*_name == '*') {
1666 				if (scope == FLG_SCOPE_HIDD)
1667 					ofl->ofl_flags |=
1668 					    (FLG_OF_VERDEF | FLG_OF_AUTOLCL);
1669 				else if (scope == FLG_SCOPE_ELIM) {
1670 					ofl->ofl_flags |=
1671 					    (FLG_OF_VERDEF | FLG_OF_AUTOELM);
1672 				}
1673 				continue;
1674 			}
1675 
1676 			/*
1677 			 * Add the new symbol.  It should be noted that all
1678 			 * symbols added by the mapfile start out with global
1679 			 * scope, thus they will fall through the normal symbol
1680 			 * resolution process.  Symbols defined as locals will
1681 			 * be reduced in scope after all input file processing.
1682 			 */
1683 			/* LINTED */
1684 			hash = (Word)elf_hash(_name);
1685 			DBG_CALL(Dbg_map_version(ofl->ofl_lml, name, _name,
1686 			    scope));
1687 			if ((sdp = ld_sym_find(_name, hash, &where,
1688 			    ofl)) == NULL) {
1689 				if ((sym =
1690 				    libld_calloc(sizeof (Sym), 1)) == NULL)
1691 					return (S_ERROR);
1692 
1693 				/*
1694 				 * Make sure any parent or external declarations
1695 				 * fall back to references.
1696 				 */
1697 				if (sym_flags &
1698 				    (FLG_SY_PARENT | FLG_SY_EXTERN)) {
1699 					/*
1700 					 * Turn it into a reference by setting
1701 					 * the section index to UNDEF.
1702 					 */
1703 					sym->st_shndx = shndx = SHN_UNDEF;
1704 
1705 					/*
1706 					 * It is wrong to to specify size
1707 					 * or value for an external symbol.
1708 					 */
1709 					if ((novalue == 0) || (size != 0)) {
1710 						eprintf(ofl->ofl_lml, ERR_FATAL,
1711 						    MSG_INTL(MSG_MAP_NOEXVLSZ),
1712 						    mapfile,
1713 						    EC_XWORD(Line_num));
1714 						errcnt++;
1715 						continue;
1716 					}
1717 				} else {
1718 					sym->st_shndx = (Half)shndx;
1719 				}
1720 
1721 				sym->st_value = value;
1722 				sym->st_size = size;
1723 				sym->st_info = ELF_ST_INFO(STB_GLOBAL, type);
1724 
1725 				if ((sdp = ld_sym_enter(_name, sym, hash, ifl,
1726 				    ofl, 0, shndx, sym_flags, sym_flags1,
1727 				    &where)) == (Sym_desc *)S_ERROR)
1728 					return (S_ERROR);
1729 
1730 				sdp->sd_flags &= ~FLG_SY_CLEAN;
1731 
1732 				/*
1733 				 * Identify any references.  FLG_SY_MAPREF is
1734 				 * turned off once a relocatable object with
1735 				 * the same symbol is found, thus the existence
1736 				 * of FLG_SY_MAPREF at symbol validation is
1737 				 * used to flag undefined/misspelled entries.
1738 				 */
1739 				if (sym->st_shndx == SHN_UNDEF)
1740 					sdp->sd_flags |=
1741 					    (FLG_SY_MAPREF | FLG_SY_GLOBREF);
1742 
1743 			} else {
1744 				conflict = NULL;
1745 				sym = sdp->sd_sym;
1746 
1747 				/*
1748 				 * If this symbol already exists, make sure this
1749 				 * definition doesn't conflict with the former.
1750 				 * Provided it doesn't, multiple definitions
1751 				 * from different mapfiles can augment each
1752 				 * other.
1753 				 */
1754 				/* BEGIN CSTYLED */
1755 				if (sym->st_value) {
1756 				    if (value && (sym->st_value != value))
1757 					conflict =
1758 					    MSG_INTL(MSG_MAP_DIFF_SYMVAL);
1759 				} else {
1760 					sym->st_value = value;
1761 				}
1762 				if (sym->st_size) {
1763 				    if (size && (sym->st_size != size))
1764 					conflict = MSG_INTL(MSG_MAP_DIFF_SYMSZ);
1765 				} else {
1766 					sym->st_size = size;
1767 				}
1768 				if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) {
1769 				    if ((type != STT_NOTYPE) &&
1770 					(ELF_ST_TYPE(sym->st_info) != type))
1771 					    conflict =
1772 						MSG_INTL(MSG_MAP_DIFF_SYMTYP);
1773 				} else {
1774 					sym->st_info =
1775 					    ELF_ST_INFO(STB_GLOBAL, type);
1776 				}
1777 				if (sym->st_shndx != SHN_UNDEF) {
1778 				    if ((shndx != SHN_UNDEF) &&
1779 					(sym->st_shndx != shndx))
1780 					    conflict =
1781 						MSG_INTL(MSG_MAP_DIFF_SYMNDX);
1782 				} else {
1783 					sdp->sd_shndx = sym->st_shndx = shndx;
1784 				}
1785 				/* END CSTYLED */
1786 
1787 				if ((sdp->sd_flags1 & MSK_SY1_GLOBAL) &&
1788 				    (sdp->sd_aux->sa_overndx !=
1789 				    VER_NDX_GLOBAL) &&
1790 				    (vdp->vd_ndx != VER_NDX_GLOBAL) &&
1791 				    (sdp->sd_aux->sa_overndx != vdp->vd_ndx)) {
1792 					conflict =
1793 					    MSG_INTL(MSG_MAP_DIFF_SYMVER);
1794 				}
1795 
1796 				if (conflict) {
1797 					eprintf(ofl->ofl_lml, ERR_FATAL,
1798 					    MSG_INTL(MSG_MAP_SYMDEF1), mapfile,
1799 					    EC_XWORD(Line_num), demangle(_name),
1800 					    sdp->sd_file->ifl_name, conflict);
1801 					errcnt++;
1802 					continue;
1803 				}
1804 
1805 				/*
1806 				 * If this mapfile entry supplies a definition,
1807 				 * indicate that the symbol is now used.
1808 				 */
1809 				if (shndx != SHN_UNDEF)
1810 					sdp->sd_flags |= FLG_SY_MAPUSED;
1811 			}
1812 
1813 			/*
1814 			 * A symbol declaration that defines a size but no
1815 			 * value is processed as a request to create an
1816 			 * associated backing section.  The intent behind this
1817 			 * functionality is to provide OBJT definitions within
1818 			 * filters that are not ABS.  ABS symbols don't allow
1819 			 * copy-relocations to be established to filter OBJT
1820 			 * definitions.
1821 			 */
1822 			if ((shndx == SHN_ABS) && size && novalue) {
1823 				/* Create backing section if not there */
1824 				if (sdp->sd_isc == NULL) {
1825 					Is_desc	*isp;
1826 
1827 					if (type == STT_OBJECT) {
1828 						if ((isp = ld_make_data(ofl,
1829 						    size)) ==
1830 						    (Is_desc *)S_ERROR)
1831 							return (S_ERROR);
1832 					} else {
1833 						if ((isp = ld_make_text(ofl,
1834 						    size)) ==
1835 						    (Is_desc *)S_ERROR)
1836 							return (S_ERROR);
1837 					}
1838 
1839 					sdp->sd_isc = isp;
1840 					isp->is_file = ifl;
1841 				}
1842 
1843 				/*
1844 				 * Now that backing storage has been created,
1845 				 * associate the symbol descriptor.  Remove the
1846 				 * symbols special section tag so that it will
1847 				 * be assigned the correct section index as part
1848 				 * of update symbol processing.
1849 				 */
1850 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1851 				sym_flags &= ~FLG_SY_SPECSEC;
1852 			}
1853 
1854 			/*
1855 			 * Indicate the new symbols scope.  Although the
1856 			 * symbols st_other field will eventually be updated as
1857 			 * part of writing out the final symbol, update the
1858 			 * st_other field here to trigger better diagnostics
1859 			 * during symbol validation (for example, undefined
1860 			 * references that are defined symbolic in a mapfile).
1861 			 */
1862 			if (scope == FLG_SCOPE_HIDD) {
1863 				/*
1864 				 * This symbol needs to be reduced to local.
1865 				 */
1866 				if (ofl->ofl_flags & FLG_OF_REDLSYM) {
1867 					sdp->sd_flags1 |=
1868 					    (FLG_SY1_HIDDEN | FLG_SY1_ELIM);
1869 					sdp->sd_sym->st_other = STV_ELIMINATE;
1870 				} else {
1871 					sdp->sd_flags1 |= FLG_SY1_HIDDEN;
1872 					sdp->sd_sym->st_other = STV_HIDDEN;
1873 				}
1874 			} else if (scope == FLG_SCOPE_ELIM) {
1875 				/*
1876 				 * This symbol needs to be eliminated.  Note,
1877 				 * the symbol is also tagged as local to trigger
1878 				 * any necessary relocation processing prior
1879 				 * to the symbol being eliminated.
1880 				 */
1881 				sdp->sd_flags1 |=
1882 				    (FLG_SY1_HIDDEN | FLG_SY1_ELIM);
1883 				sdp->sd_sym->st_other = STV_ELIMINATE;
1884 
1885 			} else {
1886 				/*
1887 				 * This symbol is explicitly defined to remain
1888 				 * global.
1889 				 */
1890 				sdp->sd_flags |= sym_flags;
1891 				sdp->sd_flags1 |= sym_flags1;
1892 
1893 				/*
1894 				 * Qualify any global scope.
1895 				 */
1896 				if (scope == FLG_SCOPE_SNGL) {
1897 					sdp->sd_flags1 |=
1898 					    (FLG_SY1_SINGLE | FLG_SY1_NDIR);
1899 					sdp->sd_sym->st_other = STV_SINGLETON;
1900 				} else if (scope == FLG_SCOPE_PROT) {
1901 					sdp->sd_flags1 |= FLG_SY1_PROTECT;
1902 					sdp->sd_sym->st_other = STV_PROTECTED;
1903 				} else if (scope == FLG_SCOPE_EXPT) {
1904 					sdp->sd_flags1 |= FLG_SY1_EXPORT;
1905 					sdp->sd_sym->st_other = STV_EXPORTED;
1906 				} else
1907 					sdp->sd_flags1 |= FLG_SY1_DEFAULT;
1908 
1909 				/*
1910 				 * Record the present version index for later
1911 				 * potential versioning.
1912 				 */
1913 				if ((sdp->sd_aux->sa_overndx == 0) ||
1914 				    (sdp->sd_aux->sa_overndx == VER_NDX_GLOBAL))
1915 					sdp->sd_aux->sa_overndx = vdp->vd_ndx;
1916 				vdp->vd_flags |= FLG_VER_REFER;
1917 			}
1918 
1919 			conflict = NULL;
1920 
1921 			/*
1922 			 * Carry out some validity checks to ensure incompatible
1923 			 * symbol characteristics have not been defined.
1924 			 * These checks are carried out after symbols are added
1925 			 * or resolved, to catch single instance, and
1926 			 * multi-instance definition inconsistencies.
1927 			 */
1928 			if ((sdp->sd_flags1 &
1929 			    (FLG_SY1_HIDDEN | FLG_SY1_ELIM)) &&
1930 			    ((scope != FLG_SCOPE_HIDD) &&
1931 			    (scope != FLG_SCOPE_ELIM))) {
1932 				conflict = MSG_INTL(MSG_MAP_DIFF_SYMLCL);
1933 
1934 			} else if (((sdp->sd_flags1 & FLG_SY1_SINGLE) ||
1935 			    (sdp->sd_flags1 & FLG_SY1_EXPORT)) &&
1936 			    ((scope != FLG_SCOPE_DFLT) &&
1937 			    (scope != FLG_SCOPE_EXPT) &&
1938 			    (scope != FLG_SCOPE_SNGL))) {
1939 				conflict = MSG_INTL(MSG_MAP_DIFF_SYMGLOB);
1940 
1941 			} else if ((sdp->sd_flags1 & FLG_SY1_PROTECT) &&
1942 			    ((scope != FLG_SCOPE_DFLT) &&
1943 			    (scope != FLG_SCOPE_PROT))) {
1944 				conflict = MSG_INTL(MSG_MAP_DIFF_SYMPROT);
1945 
1946 			} else if ((sdp->sd_flags1 & FLG_SY1_NDIR) &&
1947 			    (scope == FLG_SCOPE_PROT)) {
1948 				conflict = MSG_INTL(MSG_MAP_DIFF_PROTNDIR);
1949 
1950 			} else if ((sdp->sd_flags1 & FLG_SY1_DIR) &&
1951 			    (scope == FLG_SCOPE_SNGL)) {
1952 				conflict = MSG_INTL(MSG_MAP_DIFF_SNGLDIR);
1953 			}
1954 
1955 			if (conflict) {
1956 				/*
1957 				 * Select the conflict message from either a
1958 				 * single instance or multi-instance definition.
1959 				 */
1960 				if (sdp->sd_file->ifl_name == mapfile) {
1961 					eprintf(ofl->ofl_lml, ERR_FATAL,
1962 					    MSG_INTL(MSG_MAP_SYMDEF2), mapfile,
1963 					    EC_XWORD(Line_num), demangle(_name),
1964 					    conflict);
1965 				} else {
1966 					eprintf(ofl->ofl_lml, ERR_FATAL,
1967 					    MSG_INTL(MSG_MAP_SYMDEF1), mapfile,
1968 					    EC_XWORD(Line_num), demangle(_name),
1969 					    sdp->sd_file->ifl_name, conflict);
1970 				}
1971 				errcnt++;
1972 				continue;
1973 			}
1974 
1975 			/*
1976 			 * Indicate that this symbol has been explicitly
1977 			 * contributed from a mapfile.
1978 			 */
1979 			sdp->sd_flags1 |= (FLG_SY1_MAPFILE | FLG_SY1_EXPDEF);
1980 
1981 			/*
1982 			 * If we've encountered a symbol definition simulate
1983 			 * that an input file has been processed - this allows
1984 			 * things like filters to be created purely from a
1985 			 * mapfile.
1986 			 */
1987 			if (type != STT_NOTYPE)
1988 				ofl->ofl_objscnt++;
1989 			DBG_CALL(Dbg_map_symbol(ofl, sdp));
1990 
1991 			/*
1992 			 * If this symbol has an associated filtee, record the
1993 			 * filtee string and associate the string index with the
1994 			 * symbol.  This is used later to associate the syminfo
1995 			 * information with the necessary .dynamic entry.
1996 			 */
1997 			if (filter && (filtee == NULL)) {
1998 				eprintf(ofl->ofl_lml, ERR_FATAL,
1999 				    MSG_INTL(MSG_MAP_NOFILTER), mapfile,
2000 				    EC_XWORD(Line_num), _name);
2001 				errcnt++;
2002 				continue;
2003 			}
2004 
2005 			if (filtee) {
2006 				Dfltr_desc *	dftp;
2007 				Sfltr_desc	sft;
2008 				Aliste		idx, _idx, nitems;
2009 
2010 				/*
2011 				 * Make sure we don't duplicate any filtee
2012 				 * strings, and create a new descriptor if
2013 				 * necessary.
2014 				 */
2015 				idx = nitems = alist_nitems(ofl->ofl_dtsfltrs);
2016 				for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, _idx,
2017 				    dftp)) {
2018 					if ((dftflag != dftp->dft_flag) ||
2019 					    (strcmp(dftp->dft_str, filtee)))
2020 						continue;
2021 					idx = _idx;
2022 					break;
2023 				}
2024 				if (idx == nitems) {
2025 					Dfltr_desc	dft;
2026 
2027 					dft.dft_str = filtee;
2028 					dft.dft_flag = dftflag;
2029 					dft.dft_ndx = 0;
2030 
2031 					/*
2032 					 * The following append puts the new
2033 					 * item at the offset contained in
2034 					 * idx, because we know idx contains
2035 					 * the index of the next available slot.
2036 					 */
2037 					if (alist_append(&ofl->ofl_dtsfltrs,
2038 					    &dft, sizeof (Dfltr_desc),
2039 					    AL_CNT_OFL_DTSFLTRS) == NULL)
2040 						return (S_ERROR);
2041 				}
2042 
2043 				/*
2044 				 * Create a new filter descriptor for this
2045 				 * symbol.
2046 				 */
2047 				sft.sft_sdp = sdp;
2048 				sft.sft_idx = idx;
2049 
2050 				if (alist_append(&ofl->ofl_symfltrs,
2051 				    &sft, sizeof (Sfltr_desc),
2052 				    AL_CNT_OFL_SYMFLTRS) == NULL)
2053 					return (S_ERROR);
2054 			}
2055 			break;
2056 
2057 		default:
2058 			eprintf(ofl->ofl_lml, ERR_FATAL,
2059 			    MSG_INTL(MSG_MAP_EXPSCOL), mapfile,
2060 			    EC_XWORD(Line_num));
2061 			errcnt++;
2062 			continue;
2063 		}
2064 	}
2065 
2066 	if (errcnt)
2067 		return (S_ERROR);
2068 
2069 	/*
2070 	 * Determine if any version references are provided after the close
2071 	 * bracket.
2072 	 */
2073 	while ((tok = gettoken(ofl, mapfile, 0)) != TK_SEMICOLON) {
2074 		Ver_desc	*_vdp;
2075 		char		*_name;
2076 
2077 		if (tok != TK_STRING) {
2078 			if (tok != TK_ERROR)
2079 				eprintf(ofl->ofl_lml, ERR_FATAL,
2080 				    MSG_INTL(MSG_MAP_EXPVERS), mapfile,
2081 				    EC_XWORD(Line_num));
2082 			return (S_ERROR);
2083 		}
2084 
2085 		name = Start_tok;
2086 		if (vdp->vd_ndx == VER_NDX_GLOBAL) {
2087 			eprintf(ofl->ofl_lml, ERR_WARNING,
2088 			    MSG_INTL(MSG_MAP_UNEXDEP), mapfile,
2089 			    EC_XWORD(Line_num), name);
2090 			continue;
2091 		}
2092 
2093 		/*
2094 		 * Generate a new version descriptor if it doesn't already
2095 		 * exist.
2096 		 */
2097 		/* LINTED */
2098 		hash = (Word)elf_hash(name);
2099 		if ((_vdp = ld_vers_find(name, hash,
2100 		    ofl->ofl_verdesc)) == NULL) {
2101 			if ((_name = libld_malloc(strlen(name) + 1)) == NULL)
2102 				return (S_ERROR);
2103 			(void) strcpy(_name, name);
2104 
2105 			if ((_vdp = ld_vers_desc(_name, hash,
2106 			    &ofl->ofl_verdesc)) == (Ver_desc *)S_ERROR)
2107 				return (S_ERROR);
2108 		}
2109 
2110 		/*
2111 		 * Add the new version descriptor to the parent version
2112 		 * descriptors reference list.  Indicate the version descriptors
2113 		 * first reference (used for error disgnostics if undefined
2114 		 * version dependencies remain).
2115 		 */
2116 		if (ld_vers_find(name, hash, vdp->vd_deps) == NULL)
2117 			if (aplist_append(&vdp->vd_deps, _vdp,
2118 			    AL_CNT_VERDESCS) == NULL)
2119 				return (S_ERROR);
2120 
2121 		if (_vdp->vd_ref == NULL)
2122 			_vdp->vd_ref = vdp;
2123 	}
2124 	return (1);
2125 }
2126 
2127 /*
2128  * If a user has provided segment definitions via a mapfile, and these segments
2129  * have been assigned virtual addresses, sort the associated segments by
2130  * increasing virtual address.
2131  *
2132  * Only PT_LOAD segments can be assigned a virtual address.  These segments can
2133  * be one of two types:
2134  *
2135  *  -	Standard segments for text, data or bss.  These segments will have been
2136  *	inserted before the default text (first PT_LOAD) segment.
2137  *
2138  *  -	Empty (reservation) segments.  These segment will have been inserted at
2139  *	the end of any default PT_LOAD segments.
2140  *
2141  * Any standard segments that are assigned a virtual address will be sorted,
2142  * and as their definitions precede any default PT_LOAD segments, these segments
2143  * will be assigned sections before any defaults.
2144  *
2145  * Any reservation segments are also sorted amoung themselves, as these segments
2146  * must still follow the standard default segments.
2147  */
2148 uintptr_t
2149 ld_sort_seg_list(Ofl_desc *ofl)
2150 {
2151 	APlist	*seg1 = NULL, *seg2 = NULL;
2152 	Sg_desc	*sgp1;
2153 	Aliste	idx1;
2154 
2155 #define	FIRST_SEGMENT(type) \
2156 	((type == PT_PHDR) || (type == PT_INTERP) || (type == PT_SUNWCAP))
2157 
2158 	/*
2159 	 * Add the .phdr and .interp segments to our list.  These segments must
2160 	 * occur before any PT_LOAD segments (refer exec/elf/elf.c).  Also add
2161 	 * the capabilities segment.  This isn't essential, but the capabilities
2162 	 * section is one of the first in an object.
2163 	 */
2164 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) {
2165 		Word	type = sgp1->sg_phdr.p_type;
2166 
2167 		if (FIRST_SEGMENT(type)) {
2168 			if (aplist_append(&seg1, sgp1, AL_CNT_SEGMENTS) == NULL)
2169 				return (S_ERROR);
2170 		}
2171 	}
2172 
2173 	/*
2174 	 * Add the loadable segments to another list in sorted order.
2175 	 */
2176 	DBG_CALL(Dbg_map_sort(ofl->ofl_lml));
2177 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) {
2178 		DBG_CALL(Dbg_map_sort_seg(ofl->ofl_lml, sgp1, 1));
2179 
2180 		if (sgp1->sg_phdr.p_type != PT_LOAD)
2181 			continue;
2182 
2183 		/*
2184 		 * If the loadable segment does not contain a vaddr, simply
2185 		 * append it to the new list.
2186 		 */
2187 		if ((sgp1->sg_flags & FLG_SG_VADDR) == 0) {
2188 			if (aplist_append(&seg2, sgp1, AL_CNT_SEGMENTS) == NULL)
2189 				return (S_ERROR);
2190 
2191 		} else {
2192 			Aliste		idx2;
2193 			Sg_desc		*sgp2;
2194 			int		inserted = 0;
2195 
2196 			/*
2197 			 * Traverse the segment list we are creating, looking
2198 			 * for a segment that defines a vaddr.
2199 			 */
2200 			for (APLIST_TRAVERSE(seg2, idx2, sgp2)) {
2201 				/*
2202 				 * Any real segments that contain vaddr's need
2203 				 * to be sorted.  Any reservation segments also
2204 				 * need to be sorted.  However, any reservation
2205 				 * segments should be placed after any real
2206 				 * segments.
2207 				 */
2208 				if (((sgp2->sg_flags &
2209 				    (FLG_SG_VADDR | FLG_SG_EMPTY)) == 0) &&
2210 				    (sgp1->sg_flags & FLG_SG_EMPTY))
2211 					continue;
2212 
2213 				if ((sgp2->sg_flags & FLG_SG_VADDR) &&
2214 				    ((sgp2->sg_flags & FLG_SG_EMPTY) ==
2215 				    (sgp1->sg_flags & FLG_SG_EMPTY))) {
2216 					if (sgp1->sg_phdr.p_vaddr ==
2217 					    sgp2->sg_phdr.p_vaddr) {
2218 						eprintf(ofl->ofl_lml, ERR_FATAL,
2219 						    MSG_INTL(MSG_MAP_SEGSAME),
2220 						    sgp1->sg_name,
2221 						    sgp2->sg_name);
2222 						return (S_ERROR);
2223 					}
2224 
2225 					if (sgp1->sg_phdr.p_vaddr >
2226 					    sgp2->sg_phdr.p_vaddr)
2227 						continue;
2228 				}
2229 
2230 				/*
2231 				 * Insert this segment before the segment on
2232 				 * the seg2 list.
2233 				 */
2234 				if (aplist_insert(&seg2, sgp1, AL_CNT_SEGMENTS,
2235 				    idx2) == NULL)
2236 					return (S_ERROR);
2237 				inserted = 1;
2238 				break;
2239 			}
2240 
2241 			/*
2242 			 * If the segment being inspected has not been inserted
2243 			 * in the segment list, simply append it to the list.
2244 			 */
2245 			if ((inserted == 0) && (aplist_append(&seg2,
2246 			    sgp1, AL_CNT_SEGMENTS) == NULL))
2247 				return (S_ERROR);
2248 		}
2249 	}
2250 
2251 	/*
2252 	 * Add the sorted loadable segments to our initial segment list.
2253 	 */
2254 	for (APLIST_TRAVERSE(seg2, idx1, sgp1)) {
2255 		if (aplist_append(&seg1, sgp1, AL_CNT_SEGMENTS) == NULL)
2256 			return (S_ERROR);
2257 	}
2258 
2259 	/*
2260 	 * Add all other segments to our list.
2261 	 */
2262 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp1)) {
2263 		Word	type = sgp1->sg_phdr.p_type;
2264 
2265 		if (!FIRST_SEGMENT(type) && (type != PT_LOAD)) {
2266 			if (aplist_append(&seg1, sgp1, AL_CNT_SEGMENTS) == NULL)
2267 				return (S_ERROR);
2268 		}
2269 	}
2270 	free((void *)ofl->ofl_segs);
2271 	ofl->ofl_segs = NULL;
2272 
2273 	/*
2274 	 * Now rebuild the original list and process all of the
2275 	 * segment/section ordering information if present.
2276 	 */
2277 	for (APLIST_TRAVERSE(seg1, idx1, sgp1)) {
2278 		DBG_CALL(Dbg_map_sort_seg(ofl->ofl_lml, sgp1, 0));
2279 		if (aplist_append(&ofl->ofl_segs, sgp1,
2280 		    AL_CNT_SEGMENTS) == NULL)
2281 			return (S_ERROR);
2282 	}
2283 
2284 #undef	FIRST_SEGMENT
2285 
2286 	return (1);
2287 }
2288 
2289 /*
2290  * Parse the mapfile.
2291  */
2292 uintptr_t
2293 ld_map_parse(const char *mapfile, Ofl_desc *ofl)
2294 {
2295 	struct stat	stat_buf;	/* stat of mapfile */
2296 	int		mapfile_fd;	/* descriptor for mapfile */
2297 	Sg_desc		*sgp1;		/* seg descriptor being manipulated */
2298 	Sg_desc		*sgp2;		/* temp segment descriptor pointer */
2299 	Ent_desc	*enp;		/* Segment entrance criteria. */
2300 	Token		tok;		/* current token. */
2301 	Aliste		endx = 0;	/* next place for entrance criterion */
2302 	Boolean		new_segment;	/* If true, defines new segment. */
2303 	char		*name;
2304 	static	int	num_stack = 0;	/* number of stack segment */
2305 	int		err;
2306 
2307 	DBG_CALL(Dbg_map_parse(ofl->ofl_lml, mapfile));
2308 
2309 	/*
2310 	 * Determine if we're dealing with a file or a directory.
2311 	 */
2312 	if (stat(mapfile, &stat_buf) == -1) {
2313 		err = errno;
2314 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_STAT),
2315 		    mapfile, strerror(err));
2316 		return (S_ERROR);
2317 	}
2318 	if (S_ISDIR(stat_buf.st_mode)) {
2319 		DIR		*dirp;
2320 		struct dirent	*denp;
2321 
2322 		/*
2323 		 * Open the directory and interpret each visible file as a
2324 		 * mapfile.
2325 		 */
2326 		if ((dirp = opendir(mapfile)) == NULL)
2327 			return (1);
2328 
2329 		while ((denp = readdir(dirp)) != NULL) {
2330 			char	path[PATH_MAX];
2331 
2332 			/*
2333 			 * Ignore any hidden filenames.  Construct the full
2334 			 * pathname to the new mapfile.
2335 			 */
2336 			if (*denp->d_name == '.')
2337 				continue;
2338 			(void) snprintf(path, PATH_MAX, MSG_ORIG(MSG_STR_PATH),
2339 			    mapfile, denp->d_name);
2340 			if (ld_map_parse(path, ofl) == S_ERROR)
2341 				return (S_ERROR);
2342 		}
2343 		(void) closedir(dirp);
2344 		return (1);
2345 	} else if (!S_ISREG(stat_buf.st_mode)) {
2346 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_NOTREG),
2347 		    mapfile);
2348 		return (S_ERROR);
2349 	}
2350 
2351 	/*
2352 	 * We read the entire mapfile into memory.
2353 	 */
2354 	if ((Mapspace = libld_malloc(stat_buf.st_size + 1)) == NULL)
2355 		return (S_ERROR);
2356 	if ((mapfile_fd = open(mapfile, O_RDONLY)) == -1) {
2357 		err = errno;
2358 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
2359 		    mapfile, strerror(err));
2360 		return (S_ERROR);
2361 	}
2362 
2363 	if (read(mapfile_fd, Mapspace, stat_buf.st_size) != stat_buf.st_size) {
2364 		err = errno;
2365 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_SYS_READ),
2366 		    mapfile, strerror(err));
2367 		return (S_ERROR);
2368 	}
2369 	Mapspace[stat_buf.st_size] = '\0';
2370 	nextchr = Mapspace;
2371 
2372 	/*
2373 	 * Set up any global variables, the line number counter and file name.
2374 	 */
2375 	Line_num = 1;
2376 
2377 	/*
2378 	 * We now parse the mapfile until the gettoken routine returns EOF.
2379 	 */
2380 	while ((tok = gettoken(ofl, mapfile, 1)) != TK_EOF) {
2381 		Aliste	idx;
2382 		int	ndx;
2383 
2384 		/*
2385 		 * Don't know which segment yet.
2386 		 */
2387 		sgp1 = NULL;
2388 
2389 		/*
2390 		 * At this point we are at the beginning of a line, and the
2391 		 * variable `Start_tok' points to the first string on the line.
2392 		 * All mapfile entries start with some string token except it
2393 		 * is possible for a scoping definition to start with `{'.
2394 		 */
2395 		if (tok == TK_LEFTBKT) {
2396 			if (map_version(mapfile, (char *)0, ofl) == S_ERROR)
2397 				return (S_ERROR);
2398 			continue;
2399 		}
2400 		if (tok != TK_STRING) {
2401 			if (tok != TK_ERROR)
2402 				eprintf(ofl->ofl_lml, ERR_FATAL,
2403 				    MSG_INTL(MSG_MAP_EXPSEGNAM), mapfile,
2404 				    EC_XWORD(Line_num));
2405 			return (S_ERROR);
2406 		}
2407 
2408 		/*
2409 		 * Save the initial token.
2410 		 */
2411 		if ((name = libld_malloc(strlen(Start_tok) + 1)) == NULL)
2412 			return (S_ERROR);
2413 		(void) strcpy(name, Start_tok);
2414 
2415 		/*
2416 		 * Now check the second character on the line.  The special `-'
2417 		 * and `{' characters do not involve any segment manipulation so
2418 		 * we handle them first.
2419 		 */
2420 		tok = gettoken(ofl, mapfile, 0);
2421 		if ((tok == TK_ERROR) || (tok == TK_EOF))
2422 			return (S_ERROR);
2423 		if (tok == TK_DASH) {
2424 			if (map_dash(mapfile, name, ofl) == S_ERROR)
2425 				return (S_ERROR);
2426 			continue;
2427 		}
2428 		if (tok == TK_LEFTBKT) {
2429 			if (map_version(mapfile, name, ofl) == S_ERROR)
2430 				return (S_ERROR);
2431 			continue;
2432 		}
2433 
2434 		/*
2435 		 * If we're here we need to interpret the first string as a
2436 		 * segment name.  Find the segment named in the token.
2437 		 */
2438 		ndx = 0;
2439 		for (APLIST_TRAVERSE(ofl->ofl_segs, idx, sgp2)) {
2440 			if (strcmp(sgp2->sg_name, name) == 0) {
2441 				sgp1 = sgp2;
2442 				sgp2->sg_flags &= ~FLG_SG_DISABLED;
2443 				new_segment = FALSE;
2444 				break;
2445 			}
2446 			ndx++;
2447 		}
2448 
2449 		/*
2450 		 * If the second token is a '|' then we had better
2451 		 * of found a segment.  It is illegal to perform
2452 		 * section within segment ordering before the segment
2453 		 * has been declared.
2454 		 */
2455 		if (tok == TK_PIPE) {
2456 			if (sgp1 == NULL) {
2457 				eprintf(ofl->ofl_lml, ERR_FATAL,
2458 				    MSG_INTL(MSG_MAP_SECINSEG), mapfile,
2459 				    EC_XWORD(Line_num), name);
2460 				return (S_ERROR);
2461 			} else {
2462 				if (map_pipe(ofl, mapfile, sgp1) == S_ERROR)
2463 					return (S_ERROR);
2464 				continue;
2465 			}
2466 		}
2467 
2468 		/*
2469 		 * If segment is still NULL then it does not exist.  Create a
2470 		 * new segment, and leave its values as 0 so that map_equal()
2471 		 * can detect changing attributes.
2472 		 */
2473 		if (sgp1 == NULL) {
2474 			if ((sgp1 =
2475 			    libld_calloc(sizeof (Sg_desc), 1)) == NULL)
2476 				return (S_ERROR);
2477 			sgp1->sg_phdr.p_type = PT_NULL;
2478 			sgp1->sg_name = name;
2479 			new_segment = TRUE;
2480 		}
2481 
2482 		if ((strcmp(sgp1->sg_name, MSG_ORIG(MSG_STR_INTERP)) == 0) ||
2483 		    (strcmp(sgp1->sg_name, MSG_ORIG(MSG_STR_LD_DYNAMIC)) ==
2484 		    0)) {
2485 			eprintf(ofl->ofl_lml, ERR_FATAL,
2486 			    MSG_INTL(MSG_MAP_SEGRESV), mapfile,
2487 			    EC_XWORD(Line_num));
2488 			return (S_ERROR);
2489 		}
2490 
2491 		/*
2492 		 * Now check the second token from the input line.
2493 		 */
2494 		if (tok == TK_EQUAL) {
2495 			if (strcmp(sgp1->sg_name,
2496 			    MSG_ORIG(MSG_STR_HWCAP_1)) == 0) {
2497 				if (map_cap(mapfile, CA_SUNW_HW_1,
2498 				    ofl) == S_ERROR)
2499 					return (S_ERROR);
2500 				DBG_CALL(Dbg_cap_mapfile(ofl->ofl_lml,
2501 				    CA_SUNW_HW_1, ofl->ofl_hwcap_1,
2502 				    ld_targ.t_m.m_mach));
2503 				continue;
2504 
2505 			} else if (strcmp(sgp1->sg_name,
2506 			    MSG_ORIG(MSG_STR_SFCAP_1)) == 0) {
2507 				if (map_cap(mapfile, CA_SUNW_SF_1,
2508 				    ofl) == S_ERROR)
2509 					return (S_ERROR);
2510 				DBG_CALL(Dbg_cap_mapfile(ofl->ofl_lml,
2511 				    CA_SUNW_SF_1, ofl->ofl_sfcap_1,
2512 				    ld_targ.t_m.m_mach));
2513 				continue;
2514 
2515 			} else {
2516 				if (map_equal(mapfile, sgp1, ofl) == S_ERROR)
2517 					return (S_ERROR);
2518 				DBG_CALL(Dbg_map_set_equal(new_segment));
2519 			}
2520 		} else if (tok == TK_COLON) {
2521 			/*
2522 			 * If this is an existing segment reservation, sections
2523 			 * can't be assigned to it.
2524 			 */
2525 			if ((new_segment == FALSE) &&
2526 			    (sgp1->sg_flags & FLG_SG_EMPTY)) {
2527 				eprintf(ofl->ofl_lml, ERR_FATAL,
2528 				    MSG_INTL(MSG_MAP_SEGEMPSEC), mapfile,
2529 				    EC_XWORD(Line_num));
2530 				return (S_ERROR);
2531 			}
2532 
2533 			/*
2534 			 * We are looking at a new entrance criteria line.
2535 			 * Note that entrance criteria are added in the order
2536 			 * they are found in the mapfile, but are placed before
2537 			 * any default criteria.
2538 			 */
2539 			if ((enp = alist_insert(&(ofl->ofl_ents), NULL,
2540 			    sizeof (Ent_desc), AL_CNT_OFL_ENTRANCE,
2541 			    endx)) == NULL)
2542 				return (S_ERROR);
2543 
2544 			enp->ec_segment = sgp1;
2545 			endx++;
2546 
2547 			if (map_colon(ofl, mapfile, enp) == S_ERROR)
2548 				return (S_ERROR);
2549 			DBG_CALL(Dbg_map_ent(ofl->ofl_lml, new_segment,
2550 			    enp, ofl));
2551 		} else if (tok == TK_ATSIGN) {
2552 			if (map_atsign(mapfile, sgp1, ofl) == S_ERROR)
2553 				return (S_ERROR);
2554 			DBG_CALL(Dbg_map_set_atsign(new_segment));
2555 		} else if (tok != TK_ERROR) {
2556 			eprintf(ofl->ofl_lml, ERR_FATAL,
2557 			    MSG_INTL(MSG_MAP_EXPEQU), mapfile,
2558 			    EC_XWORD(Line_num));
2559 			return (S_ERROR);
2560 		}
2561 
2562 		/*
2563 		 * Having completed parsing an entry in the mapfile determine
2564 		 * if the segment to which it applies is new.
2565 		 */
2566 		if (new_segment) {
2567 			/*
2568 			 * If specific fields have not been supplied via
2569 			 * map_equal(), make sure defaults are supplied.
2570 			 */
2571 			if (((sgp1->sg_flags & FLG_SG_TYPE) == 0) &&
2572 			    (sgp1->sg_phdr.p_type == PT_NULL)) {
2573 				/*
2574 				 * Default to a loadable segment.
2575 				 */
2576 				sgp1->sg_phdr.p_type = PT_LOAD;
2577 				sgp1->sg_flags |= FLG_SG_TYPE;
2578 			}
2579 			if (sgp1->sg_phdr.p_type == PT_LOAD) {
2580 				if ((sgp1->sg_flags & FLG_SG_FLAGS) == 0) {
2581 					/*
2582 					 * Default to read/write and execute.
2583 					 */
2584 					sgp1->sg_phdr.p_flags =
2585 					    PF_R + PF_W + PF_X;
2586 					sgp1->sg_flags |= FLG_SG_FLAGS;
2587 				}
2588 				if ((sgp1->sg_flags & FLG_SG_ALIGN) == 0) {
2589 					/*
2590 					 * Default to segment alignment
2591 					 */
2592 					sgp1->sg_phdr.p_align =
2593 					    ld_targ.t_m.m_segm_align;
2594 					sgp1->sg_flags |= FLG_SG_ALIGN;
2595 				}
2596 			}
2597 
2598 			/*
2599 			 * Determine where the new item should be inserted in
2600 			 * the segment descriptor list.  Presently the user can
2601 			 * only add the following:
2602 			 *
2603 			 *  PT_LOAD	added before the text segment.
2604 			 *  PT_NULL/empty PT_LOAD
2605 			 *		added after the data/bss segments, thus
2606 			 *		we add before the dynamic segment.
2607 			 *  PT_SUNWSTACK
2608 			 *		added before the final note segment.
2609 			 *  PT_NOTE	added before the final note segment.
2610 			 *
2611 			 * Note that any new segments must always be added
2612 			 * after any PT_PHDR and PT_INTERP (refer Generic ABI,
2613 			 * Page 5-4).
2614 			 */
2615 			switch (sgp1->sg_phdr.p_type) {
2616 			case PT_LOAD:
2617 			case PT_NULL:
2618 				if (sgp1->sg_flags & FLG_SG_EMPTY)
2619 					sgp1->sg_id = LD_DYN;
2620 				else
2621 					sgp1->sg_id = LD_TEXT;
2622 				break;
2623 			case PT_SUNWSTACK:
2624 				sgp1->sg_id = LD_NOTE;
2625 				if (++num_stack >= 2) {
2626 					/*
2627 					 * Currently the number of sunw_stack
2628 					 * segment is limited to 1.
2629 					 */
2630 					eprintf(ofl->ofl_lml, ERR_WARNING,
2631 					    MSG_INTL(MSG_MAP_NOSTACK2),
2632 					    mapfile, EC_XWORD(Line_num));
2633 					continue;
2634 				}
2635 				break;
2636 			case PT_NOTE:
2637 				sgp1->sg_id = LD_NOTE;
2638 				break;
2639 			default:
2640 				eprintf(ofl->ofl_lml, ERR_FATAL,
2641 				    MSG_INTL(MSG_MAP_UNKSEGTYP), mapfile,
2642 				    EC_XWORD(Line_num),
2643 				    EC_WORD(sgp1->sg_phdr.p_type));
2644 				return (S_ERROR);
2645 			}
2646 
2647 			ndx = 0;
2648 			for (APLIST_TRAVERSE(ofl->ofl_segs, idx, sgp2)) {
2649 				if (sgp1->sg_id > sgp2->sg_id) {
2650 					ndx++;
2651 					continue;
2652 				}
2653 
2654 				if (aplist_insert(&ofl->ofl_segs, sgp1,
2655 				    AL_CNT_SEGMENTS, idx) == NULL)
2656 					return (S_ERROR);
2657 				break;
2658 			}
2659 		}
2660 		DBG_CALL(Dbg_map_seg(ofl, ndx, sgp1));
2661 	}
2662 
2663 	/*
2664 	 * If the output file is a static file without an interpreter, and
2665 	 * if any virtual address is specified, then set the ?N flag for
2666 	 * backward compatibility.
2667 	 */
2668 	if (!(ofl->ofl_flags & FLG_OF_DYNAMIC) &&
2669 	    !(ofl->ofl_flags & FLG_OF_RELOBJ) &&
2670 	    !(ofl->ofl_osinterp) &&
2671 	    (ofl->ofl_flags1 & FLG_OF1_VADDR))
2672 		ofl->ofl_dtflags_1 |= DF_1_NOHDR;
2673 
2674 	/*
2675 	 * If the output file is a relocatable file, then ?N has no effect.
2676 	 * Make sure this flag isn't set.
2677 	 */
2678 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
2679 		ofl->ofl_dtflags_1 &= ~DF_1_NOHDR;
2680 
2681 	return (1);
2682 }
2683