summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/libld/common/map.c
blob: e7711bb64dd74540cad420ca9a70c2b287c6db8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 *	Copyright (c) 1988 AT&T
 *	  All Rights Reserved
 *
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Map file parsing (Original SysV syntax).
 */
#include	<string.h>
#include	<strings.h>
#include	<stdio.h>
#include	<unistd.h>
#include	<errno.h>
#include	<limits.h>
#include	<ctype.h>
#include	<elfcap.h>
#include	<debug.h>
#include	"msg.h"
#include	"_libld.h"
#include	"_map.h"

/*
 * Process a hardware/software capabilities segment declaration definition.
 *	hwcap_1	= val,... [ OVERRIDE ]
 *	sfcap_1	= val,... [ OVERRIDE ]
 *	hwcap_2	= val,... [ OVERRIDE ]
 *	platcap	= name,... [ OVERRIDE ]
 *	machcap	= name,... [ OVERRIDE ]
 *
 * The values can be defined as a list of machine specify tokens, or numerics.
 * Tokens are representations of the sys/auxv_$MACH.h capabilities, for example:
 *
 *	#define AV_386_FPU 0x0001	is represented as	FPU
 *	#define AV_386_TSC 0x0002	"    "	  "     "	TSC
 *
 * Or, the above two capabilities could be represented as V0x3.  Note, the
 * OVERRIDE flag is used to ensure that only those values provided via this
 * mapfile entry are recorded in the final image, ie. this overrides any
 * hardware capabilities that may be defined in the objects read as part of
 * this link-edit.  Specifying:
 *
 *	V0x0 OVERRIDE
 *
 * effectively removes any capabilities information from the final image.
 */
static Boolean
map_cap(Mapfile *mf, Word type, Capmask *capmask)
{
	Token		tok;		/* Current token. */
	Xword		number;
	int		used = 0;
	Ofl_desc	*ofl = mf->mf_ofl;
	ld_map_tkval_t	tkv;		/* Value of token */
	elfcap_mask_t	value = 0;

	if (DBG_ENABLED) {
		Dbg_cap_mapfile_title(ofl->ofl_lml, mf->mf_lineno);
		Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_HW_1,
		    capmask->cm_val, ld_targ.t_m.m_mach);
	}

	while ((tok = ld_map_gettoken(mf, TK_F_STRLC, &tkv)) !=
	    TK_SEMICOLON) {
		if (tok != TK_STRING) {
			if (tok != TK_ERROR)
				mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSEGATT));
			return (FALSE);
		}

		/*
		 * First, determine if the token represents the reserved
		 * OVERRIDE keyword.
		 */
		if (strncmp(tkv.tkv_str, MSG_ORIG(MSG_MAP_OVERRIDE),
		    MSG_MAP_OVERRIDE_SIZE) == 0) {
			ld_map_cap_set_ovflag(mf, type);
			used++;
			continue;
		}

		/* Is the token a symbolic capability name? */
		if ((number = (Xword)elfcap_tag_from_str(ELFCAP_STYLE_LC,
		    type, tkv.tkv_str, ld_targ.t_m.m_mach)) != 0) {
			value |= number;
			used++;
			continue;
		}

		/*
		 * Is the token a numeric value?
		 */
		if (tkv.tkv_str[0] == 'v') {
			if (ld_map_strtoxword(&tkv.tkv_str[1], NULL,
			    &number) != STRTOXWORD_OK) {
				mf_fatal(mf, MSG_INTL(MSG_MAP_BADCAPVAL),
				    tkv.tkv_str);
				return (FALSE);
			}
			value |= number;
			used++;
			continue;
		}

		/*
		 * We have an unknown token.
		 */
		used++;
		mf_fatal(mf, MSG_INTL(MSG_MAP_UNKCAPATTR), tkv.tkv_str);
		return (FALSE);
	}

	/* Catch empty declarations */
	if (used == 0) {
		mf_warn0(mf, MSG_INTL(MSG_MAP_EMPTYCAP));
		return (TRUE);
	}

	DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, type, value,
	    ld_targ.t_m.m_mach));
	capmask->cm_val |= value;

	/* Sanity check the resulting bits */
	if (!ld_map_cap_sanitize(mf, type, capmask))
		return (FALSE);

	return (TRUE);
}

/*
 * Parse the flags for a segment definition. Called by map_equal().
 *
 * entry:
 *	mf - Mapfile descriptor
 *	sgp - Segment being defined
 *	b_flags - Address of b_flags variable from map_equal().
 *		*bflags is TRUE if flags have already been seen in, the
 *		current segment definition directive, and FALSE otherwise.
 *	flag_tok - Flags string, starting with the '?' character.
 *
 * exit:
 *	On success, the flags have been parsed and the segment updated,
 *	*b_flags is set to TRUE, and TRUE is returned. On error, FALSE
 *	is returned.
 */
static Boolean
map_equal_flags(Mapfile *mf, Sg_desc *sgp, Boolean *b_flags,
    const char *flag_tok)
{
	Word	tmp_flags = 0;

	if (*b_flags) {
		mf_fatal(mf, MSG_INTL(MSG_MAP_MOREONCE),
		    MSG_INTL(MSG_MAP_SEGFLAG));
		return (FALSE);
	}

	/* Skip over the leading '?' character */
	flag_tok++;

	/*
	 * If ? has nothing following leave the flags cleared,
	 * otherwise OR in any flags specified.
	 */
	while (*flag_tok) {
		switch (*flag_tok) {
		case 'r':
			tmp_flags |= PF_R;
			break;
		case 'w':
			tmp_flags |= PF_W;
			break;
		case 'x':
			tmp_flags |= PF_X;
			break;
		case 'e':
			sgp->sg_flags |= FLG_SG_EMPTY;
			break;
		case 'o':
			/*
			 * The version 1 ?O option is incompatible with
			 * the version 2 SEGMENT IS_ORDER attribute.
			 */
			if (aplist_nitems(sgp->sg_is_order) > 0) {
				mf_fatal(mf, MSG_INTL(MSG_MAP_ISORDVER),
				    sgp->sg_name);
				return (FALSE);
			}

			/*
			 * Set FLG_SG_IS_ORDER to indicate that segment has
			 * had the ?O flag set by a version 1 mapfile.
			 */
			sgp->sg_flags |= FLG_SG_IS_ORDER;
			break;
		case 'n':
			/*
			 * If segment ends up as the first loadable segment,
			 * it will not include the the ELF and program headers.
			 */
			sgp->sg_flags |= FLG_SG_NOHDR;
			break;
		default:
			mf_fatal(mf, MSG_INTL(MSG_MAP_UNKSEGFLG), *flag_tok);
			return (FALSE);
		}
		flag_tok++;
	}

	/*
	 * Warn when changing flags except when we're adding or removing "X"
	 * from a RW PT_LOAD segment.
	 */
	if ((sgp->sg_flags & FLG_SG_P_FLAGS) &&
	    (sgp->sg_phdr.p_flags != tmp_flags) &&
	    !(sgp->sg_phdr.p_type == PT_LOAD &&
	    (tmp_flags & (PF_R|PF_W)) == (PF_R|PF_W) &&
	    (tmp_flags ^ sgp->sg_phdr.p_flags) == PF_X))
		mf_warn(mf, MSG_INTL(MSG_MAP_REDEFATT),
		    MSG_INTL(MSG_MAP_SEGFLAG), sgp->sg_name);

	sgp->sg_flags |= FLG_SG_P_FLAGS;
	sgp->sg_phdr.p_flags = tmp_flags;
	*b_flags = TRUE;

	return (TRUE);
}

/*
 * Read an address (value) or size Xword from a TK_STRING token value
 * where the first letter of the string is a letter ('v', 'l', 's', ...)
 * followed by the numeric value.
 *
 * entry:
 *	mf - Mapfile descriptor
 *	tkv - TK_STRING token to parse
 *	value - Address of variable to receive the resulting value.
 *
 * exit:
 *	Returns TRUE for success. On failure, issues an error message
 *	and returns FALSE.
 */
static Boolean
valuetoxword(Mapfile *mf, ld_map_tkval_t *tkv, Xword *value)
{
	switch (ld_map_strtoxword(&tkv->tkv_str[1], NULL, value)) {
	case STRTOXWORD_OK:
		return (TRUE);

	case STRTOXWORD_TOOBIG:
		mf_fatal(mf, MSG_INTL(MSG_MAP_SEGADDR), tkv->tkv_str,
		    MSG_INTL(MSG_MAP_EXCLIMIT));
		break;
	default:
		mf_fatal(mf, MSG_INTL(MSG_MAP_SEGADDR), tkv->tkv_str,
		    MSG_INTL(MSG_MAP_NOBADFRM));
		break;
	}

	return (FALSE);
}

/*
 * Process a mapfile segment declaration definition.
 *	segment_name	= segment_attribute;
 *	segment_attribute : segment_type  segment_flags	 virtual_addr
 *			    physical_addr  length alignment
 */
static Boolean
map_equal(Mapfile *mf, Sg_desc *sgp)
{
	/*
	 * Segment type.  Users are permitted to define PT_LOAD,
	 * PT_NOTE, PT_SUNWSTACK and PT_NULL segments.  Other segment
	 * types are only defined in seg_desc[].
	 */
	typedef struct {
		const char	*name;	/* Name for segment type  */
		Word		p_type;	/* PT_ constant corresponding to name */
		sg_flags_t	sg_flags; /* Seg descriptor flags to apply */
	} seg_types_t;

	static seg_types_t seg_type_arr[] = {
		{ MSG_ORIG(MSG_MAP_LOAD),	PT_LOAD,	FLG_SG_P_TYPE },
		{ MSG_ORIG(MSG_MAP_STACK),	PT_SUNWSTACK,
		    FLG_SG_P_TYPE | FLG_SG_EMPTY },
		{ MSG_ORIG(MSG_MAP_NULL),	PT_NULL,	FLG_SG_P_TYPE },
		{ MSG_ORIG(MSG_MAP_NOTE),	PT_NOTE,	FLG_SG_P_TYPE },

		/* Array must be NULL terminated */
		{ NULL }
	};


	seg_types_t	*seg_type;
	Token	tok;			/* Current token. */
	ld_map_tkval_t	tkv;		/* Value of token */
	Boolean	b_type  = FALSE;	/* True if seg types found. */
	Boolean	b_flags = FALSE;	/* True if seg flags found. */
	Boolean	b_len   = FALSE;	/* True if seg length found. */
	Boolean	b_round = FALSE;	/* True if seg rounding found. */
	Boolean	b_vaddr = FALSE;	/* True if seg virtual addr found. */
	Boolean	b_paddr = FALSE;	/* True if seg physical addr found. */
	Boolean	b_align = FALSE;	/* True if seg alignment found. */

	while ((tok = ld_map_gettoken(mf, TK_F_STRLC, &tkv)) !=
	    TK_SEMICOLON) {
		if (tok != TK_STRING) {
			if (tok != TK_ERROR)
				mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSEGATT));
			return (FALSE);
		}

		/*
		 * If it is the name of a segment type, set the type
		 * and flags fields in the descriptor.
		 */
		for (seg_type = seg_type_arr; seg_type->name; seg_type++) {
			if (strcmp(tkv.tkv_str, seg_type->name) == 0) {
				if (b_type) {
					mf_fatal(mf, MSG_INTL(MSG_MAP_MOREONCE),
					    MSG_INTL(MSG_MAP_SEGTYP));
					return (FALSE);
				}
				if ((sgp->sg_flags & FLG_SG_P_TYPE) &&
				    (sgp->sg_phdr.p_type != seg_type->p_type)) {
					mf_warn(mf, MSG_INTL(MSG_MAP_REDEFATT),
					    MSG_INTL(MSG_MAP_SEGTYP),
					    sgp->sg_name);
				}

				sgp->sg_phdr.p_type = seg_type->p_type;
				sgp->sg_flags |= seg_type->sg_flags;
				break;
			}
		}
		if (seg_type->name != NULL)	/* Matched segment type */
			continue;		/* next token */

		/* Segment Flags */
		if (*tkv.tkv_str == '?') {
			if (!map_equal_flags(mf, sgp, &b_flags, tkv.tkv_str))
				return (FALSE);
			continue;		/* next token */
		}


		/* Segment address, length, alignment or rounding number */
		if ((tkv.tkv_str[0] == 'l') || (tkv.tkv_str[0] == 'v') ||
		    (tkv.tkv_str[0] == 'a') || (tkv.tkv_str[0] == 'p') ||
		    (tkv.tkv_str[0] == 'r')) {
			Xword	number;

			if (!valuetoxword(mf, &tkv, &number))
				return (FALSE);

			switch (*tkv.tkv_str) {
			case 'l':
				if (b_len) {
					mf_fatal(mf,
					    MSG_INTL(MSG_MAP_MOREONCE),
					    MSG_INTL(MSG_MAP_SEGLEN));
					return (FALSE);
				}
				if ((sgp->sg_flags & FLG_SG_LENGTH) &&
				    (sgp->sg_length != number))
					mf_warn(mf,
					    MSG_INTL(MSG_MAP_REDEFATT),
					    MSG_INTL(MSG_MAP_SEGLEN),
					    sgp->sg_name);
				sgp->sg_length = number;
				sgp->sg_flags |= FLG_SG_LENGTH;
				b_len = TRUE;
				break;
			case 'r':
				if (b_round) {
					mf_fatal(mf,
					    MSG_INTL(MSG_MAP_MOREONCE),
					    MSG_INTL(MSG_MAP_SEGROUND));
					return (FALSE);
				}
				if ((sgp->sg_flags & FLG_SG_ROUND) &&
				    (sgp->sg_round != number))
					mf_warn(mf,
					    MSG_INTL(MSG_MAP_REDEFATT),
					    MSG_INTL(MSG_MAP_SEGROUND),
					    sgp->sg_name);
				sgp->sg_round = number;
				sgp->sg_flags |= FLG_SG_ROUND;
				b_round = TRUE;
				break;
			case 'v':
				if (b_vaddr) {
					mf_fatal(mf,
					    MSG_INTL(MSG_MAP_MOREONCE),
					    MSG_INTL(MSG_MAP_SEGVADDR));
					return (FALSE);
				}
				if ((sgp->sg_flags & FLG_SG_P_VADDR) &&
				    (sgp->sg_phdr.p_vaddr != number))
					mf_warn(mf,
					    MSG_INTL(MSG_MAP_REDEFATT),
					    MSG_INTL(MSG_MAP_SEGVADDR),
					    sgp->sg_name);
				/* LINTED */
				sgp->sg_phdr.p_vaddr = (Addr)number;
				sgp->sg_flags |= FLG_SG_P_VADDR;
				b_vaddr = TRUE;
				break;
			case 'p':
				if (b_paddr) {
					mf_fatal(mf,
					    MSG_INTL(MSG_MAP_MOREONCE),
					    MSG_INTL(MSG_MAP_SEGPHYS));
					return (FALSE);
				}
				if ((sgp->sg_flags & FLG_SG_P_PADDR) &&
				    (sgp->sg_phdr.p_paddr != number))
					mf_warn(mf,
					    MSG_INTL(MSG_MAP_REDEFATT),
					    MSG_INTL(MSG_MAP_SEGPHYS),
					    sgp->sg_name);
				/* LINTED */
				sgp->sg_phdr.p_paddr = (Addr)number;
				sgp->sg_flags |= FLG_SG_P_PADDR;
				b_paddr = TRUE;
				break;
			case 'a':
				if (b_align) {
					mf_fatal(mf,
					    MSG_INTL(MSG_MAP_MOREONCE),
					    MSG_INTL(MSG_MAP_SEGALIGN));
					return (FALSE);
				}
				if ((sgp->sg_flags & FLG_SG_P_ALIGN) &&
				    (sgp->sg_phdr.p_align != number))
					mf_warn(mf,
					    MSG_INTL(MSG_MAP_REDEFATT),
					    MSG_INTL(MSG_MAP_SEGALIGN),
					    sgp->sg_name);
				/* LINTED */
				sgp->sg_phdr.p_align = (Xword)number;
				sgp->sg_flags |= FLG_SG_P_ALIGN;
				b_align = TRUE;
				break;
			}

			continue;		/* next token */
		}

		/*
		 * If we reach the bottom of this loop, we have an
		 * unrecognized token.
		 */
		mf_fatal(mf, MSG_INTL(MSG_MAP_UNKSEGATT), tkv.tkv_str);
		return (FALSE);
	}

	/*
	 * Empty segments can be used to define PT_LOAD segment reservations, or
	 * to reserve PT_NULL program headers.
	 *
	 * PT_LOAD reservations are only allowed within executables, as the
	 * reservation must be established through exec() as part of initial
	 * process loading.  In addition, PT_LOAD reservations must have an
	 * associated address and size. Note: This is an obsolete feature,
	 * not supported by the newer mapfile syntax.
	 *
	 * PT_NULL program headers are established for later use by applications
	 * such as the post-optimizer.  PT_NULL headers should have no other
	 * attributes assigned.
	 */
	if ((sgp->sg_flags & FLG_SG_EMPTY) &&
	    (sgp->sg_phdr.p_type != PT_SUNWSTACK)) {

		/*
		 * Any style of empty segment should have no permissions.
		 */
		if (sgp->sg_phdr.p_flags != 0) {
			mf_fatal(mf, MSG_INTL(MSG_MAP_SEGEMNOPERM),
			    EC_WORD(sgp->sg_phdr.p_flags));
			return (FALSE);
		}

		if (sgp->sg_phdr.p_type == PT_LOAD) {
			if ((mf->mf_ofl->ofl_flags & FLG_OF_EXEC) == 0) {
				mf_fatal0(mf, MSG_INTL(MSG_MAP_SEGEMPEXE));
				return (FALSE);
			}
			if ((sgp->sg_flags &
			    (FLG_SG_LENGTH | FLG_SG_P_VADDR)) !=
			    (FLG_SG_LENGTH | FLG_SG_P_VADDR)) {
				mf_fatal0(mf, MSG_INTL(MSG_MAP_SEGEMPATT));
				return (FALSE);
			}
		} else if (sgp->sg_phdr.p_type == PT_NULL) {
			if ((sgp->sg_flags &
			    (FLG_SG_LENGTH | FLG_SG_P_VADDR)) &&
			    ((sgp->sg_length != 0) ||
			    (sgp->sg_phdr.p_vaddr != 0))) {
				mf_fatal0(mf, MSG_INTL(MSG_MAP_SEGEMPNOATT));
				return (FALSE);
			}
		} else {
			mf_warn0(mf, MSG_INTL(MSG_MAP_SEGEMPLOAD));
			sgp->sg_phdr.p_type = PT_LOAD;
		}
	}

	/*
	 * All segment attributes have now been scanned.  Certain flags do not
	 * make sense if this is not a loadable segment, fix if necessary.
	 * Note, if the segment is of type PT_NULL it must be new, and any
	 * defaults will be applied by ld_map_seg_insert(). When clearing an
	 * attribute leave the flag set as an indicator for later entries
	 * re-specifying the same segment.
	 */
	if ((sgp->sg_phdr.p_type != PT_NULL) &&
	    (sgp->sg_phdr.p_type != PT_LOAD)) {
		const char	*fmt;

		if (sgp->sg_phdr.p_type == PT_SUNWSTACK)
			fmt = MSG_INTL(MSG_MAP_NOSTACK1);
		else
			fmt = MSG_INTL(MSG_MAP_NONLOAD);

		if ((sgp->sg_flags & FLG_SG_P_FLAGS) &&
		    (sgp->sg_phdr.p_type != PT_SUNWSTACK)) {
			if (sgp->sg_phdr.p_flags != 0) {
				mf_warn(mf, MSG_INTL(MSG_MAP_NONLOAD),
				    MSG_INTL(MSG_MAP_SEGFLAG));
				sgp->sg_phdr.p_flags = 0;
			}
		}
		if (sgp->sg_flags & FLG_SG_LENGTH)
			if (sgp->sg_length != 0) {
				mf_warn(mf, fmt, MSG_INTL(MSG_MAP_SEGLEN));
				sgp->sg_length = 0;
			}
		if (sgp->sg_flags & FLG_SG_ROUND)
			if (sgp->sg_round != 0) {
				mf_warn(mf, fmt, MSG_INTL(MSG_MAP_SEGROUND));
				sgp->sg_round = 0;
			}
		if (sgp->sg_flags & FLG_SG_P_VADDR) {
			if (sgp->sg_phdr.p_vaddr != 0) {
				mf_warn(mf, fmt, MSG_INTL(MSG_MAP_SEGVADDR));
				sgp->sg_phdr.p_vaddr = 0;
			}
		}
		if (sgp->sg_flags & FLG_SG_P_PADDR)
			if (sgp->sg_phdr.p_paddr != 0) {
				mf_warn(mf, fmt, MSG_INTL(MSG_MAP_SEGPHYS));
				sgp->sg_phdr.p_paddr = 0;
			}
		if (sgp->sg_flags & FLG_SG_P_ALIGN)
			if (sgp->sg_phdr.p_align != 0) {
				mf_warn(mf, fmt, MSG_INTL(MSG_MAP_SEGALIGN));
				sgp->sg_phdr.p_align = 0;
			}
	}
	return (TRUE);
}


/*
 * Process a mapfile mapping directives definition.
 *
 *	segment_name : section_attribute [ : file_name ]
 *
 * Where segment_attribute is one of: section_name section_type section_flags;
 */
static Boolean
map_colon(Mapfile *mf, Ent_desc *enp)
{
	Token		tok;
	ld_map_tkval_t	tkv;
	Boolean		b_name = FALSE;
	Boolean		b_type = FALSE;
	Boolean		b_attr = FALSE;
	Boolean		b_bang = FALSE;


	/*
	 * Start out assuming that this entrance criteria will be empty,
	 * and therefore match anything. We clear the CATCHALL flag below
	 * if this turns out not to be the case.
	 */
	enp->ec_flags |= FLG_EC_CATCHALL;

	while (((tok = ld_map_gettoken(mf, 0, &tkv)) != TK_COLON) &&
	    (tok != TK_SEMICOLON)) {
		if (tok == TK_ERROR)
			return (FALSE);
		if (tok != TK_STRING) {
			mf_fatal0(mf, MSG_INTL(MSG_MAP_MALFORM));
			return (FALSE);
		}

		/* Segment type. */

		if (*tkv.tkv_str == '$') {
			if (b_type) {
				mf_fatal(mf, MSG_INTL(MSG_MAP_MOREONCE),
				    MSG_INTL(MSG_MAP_SECTYP));
				return (FALSE);
			}
			b_type = TRUE;
			tkv.tkv_str++;
			ld_map_lowercase(tkv.tkv_str);
			if (strcmp(tkv.tkv_str, MSG_ORIG(MSG_STR_PROGBITS)) ==
			    0)
				enp->ec_type = SHT_PROGBITS;
			else if (strcmp(tkv.tkv_str,
			    MSG_ORIG(MSG_STR_SYMTAB)) == 0)
				enp->ec_type = SHT_SYMTAB;
			else if (strcmp(tkv.tkv_str,
			    MSG_ORIG(MSG_STR_DYNSYM)) == 0)
				enp->ec_type = SHT_DYNSYM;
			else if (strcmp(tkv.tkv_str,
			    MSG_ORIG(MSG_STR_STRTAB)) == 0)
				enp->ec_type = SHT_STRTAB;
			else if ((strcmp(tkv.tkv_str,
			    MSG_ORIG(MSG_STR_REL)) == 0) ||
			    (strcmp(tkv.tkv_str, MSG_ORIG(MSG_STR_RELA)) == 0))
				enp->ec_type = ld_targ.t_m.m_rel_sht_type;
			else if (strcmp(tkv.tkv_str, MSG_ORIG(MSG_STR_HASH)) ==
			    0)
				enp->ec_type = SHT_HASH;
			else if (strcmp(tkv.tkv_str, MSG_ORIG(MSG_STR_LIB)) ==
			    0)
				enp->ec_type = SHT_SHLIB;
			else if (strcmp(tkv.tkv_str,
			    MSG_ORIG(MSG_STR_LD_DYNAMIC)) == 0)
				enp->ec_type = SHT_DYNAMIC;
			else if (strcmp(tkv.tkv_str, MSG_ORIG(MSG_STR_NOTE)) ==
			    0)
				enp->ec_type = SHT_NOTE;
			else if (strcmp(tkv.tkv_str,
			    MSG_ORIG(MSG_STR_NOBITS)) == 0)
				enp->ec_type = SHT_NOBITS;
			else {
				mf_fatal(mf, MSG_INTL(MSG_MAP_UNKSECTYP),
				    tkv.tkv_str);
				return (FALSE);
			}

			enp->ec_flags &= ~FLG_EC_CATCHALL;

		/*
		 * Segment flags.
		 * If a segment flag is specified then the appropriate bit is
		 * set in the ec_attrmask, the ec_attrbits fields determine
		 * whether the attrmask fields must be tested true or false
		 * ie.	for  ?A the attrmask is set and the attrbit is set,
		 *	for ?!A the attrmask is set and the attrbit is clear.
		 */
		} else if (*tkv.tkv_str == '?') {
			if (b_attr) {
				mf_fatal(mf, MSG_INTL(MSG_MAP_MOREONCE),
				    MSG_INTL(MSG_MAP_SECFLAG));
				return (FALSE);
			}
			b_attr = TRUE;
			b_bang = FALSE;
			tkv.tkv_str++;
			ld_map_lowercase(tkv.tkv_str);
			for (; *tkv.tkv_str != '\0'; tkv.tkv_str++)
				switch (*tkv.tkv_str) {
				case '!':
					if (b_bang) {
						mf_fatal(mf,
						    MSG_INTL(MSG_MAP_BADFLAG),
						    tkv.tkv_str);
						return (FALSE);
					}
					b_bang = TRUE;
					break;
				case 'a':
					if (enp->ec_attrmask & SHF_ALLOC) {
						mf_fatal(mf,
						    MSG_INTL(MSG_MAP_BADFLAG),
						    tkv.tkv_str);
						return (FALSE);
					}
					enp->ec_attrmask |= SHF_ALLOC;
					if (!b_bang)
						enp->ec_attrbits |= SHF_ALLOC;
					b_bang = FALSE;
					break;
				case 'w':
					if (enp->ec_attrmask & SHF_WRITE) {
						mf_fatal(mf,
						    MSG_INTL(MSG_MAP_BADFLAG),
						    tkv.tkv_str);
						return (FALSE);
					}
					enp->ec_attrmask |= SHF_WRITE;
					if (!b_bang)
						enp->ec_attrbits |= SHF_WRITE;
					b_bang = FALSE;
					break;
				case 'x':
					if (enp->ec_attrmask & SHF_EXECINSTR) {
						mf_fatal(mf,
						    MSG_INTL(MSG_MAP_BADFLAG),
						    tkv.tkv_str);
						return (FALSE);
					}
					enp->ec_attrmask |= SHF_EXECINSTR;
					if (!b_bang)
						enp->ec_attrbits |=
						    SHF_EXECINSTR;
					b_bang = FALSE;
					break;
				default:
					mf_fatal(mf,
					    MSG_INTL(MSG_MAP_BADFLAG),
					    tkv.tkv_str);
					return (FALSE);
				}
			if (enp->ec_attrmask != 0)
				enp->ec_flags &= ~FLG_EC_CATCHALL;

		/*
		 * Section name.
		 */
		} else {
			if (b_name) {
				mf_fatal(mf, MSG_INTL(MSG_MAP_MOREONCE),
				    MSG_INTL(MSG_MAP_SECNAME));
				return (FALSE);
			}
			b_name = TRUE;
			enp->ec_is_name = tkv.tkv_str;
			enp->ec_flags &= ~FLG_EC_CATCHALL;
		}
	}
	if (tok == TK_COLON) {
		/*
		 * File names.
		 */
		while ((tok = ld_map_gettoken(mf, 0, &tkv)) != TK_SEMICOLON) {
			Word	ecf_type;

			if (tok != TK_STRING) {
				if (tok != TK_ERROR)
					mf_fatal0(mf,
					    MSG_INTL(MSG_MAP_MALFORM));
				return (FALSE);
			}

			/*
			 * A leading '*' means that this should be a basename
			 * comparison rather than a full path. It's not a glob
			 * wildcard, although it looks like one.
			 */
			if (tkv.tkv_str[0] == '*') {
				ecf_type = TYP_ECF_BASENAME;
				tkv.tkv_str++;
			} else {
				ecf_type = TYP_ECF_PATH;
			}
			if (!ld_map_seg_ent_files(mf, enp, ecf_type,
			    tkv.tkv_str))
				return (FALSE);
			enp->ec_flags &= ~FLG_EC_CATCHALL;
		}
	}
	return (TRUE);
}

/*
 * Process a mapfile size symbol definition.
 *	segment_name @ symbol_name;
 */
static Boolean
map_atsign(Mapfile *mf, Sg_desc *sgp)
{
	Token		tok;		/* Current token. */
	ld_map_tkval_t	tkv;		/* Value of token */

	if ((tok = ld_map_gettoken(mf, 0, &tkv)) != TK_STRING) {
		if (tok != TK_ERROR)
			mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSYM_1));
		return (FALSE);
	}

	/* Add the symbol to the segment */
	if (!ld_map_seg_size_symbol(mf, sgp, TK_PLUSEQ, tkv.tkv_str))
		return (FALSE);


	if (ld_map_gettoken(mf, 0, &tkv) != TK_SEMICOLON) {
		if (tok != TK_ERROR)
			mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSCOL));
		return (FALSE);
	}

	return (TRUE);
}


static Boolean
map_pipe(Mapfile *mf, Sg_desc *sgp)
{
	Token		tok;		/* current token. */
	ld_map_tkval_t	tkv;		/* Value of token */

	if ((tok = ld_map_gettoken(mf, 0, &tkv)) != TK_STRING) {
		if (tok != TK_ERROR)
			mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSEC));
		return (FALSE);
	}

	if (!ld_map_seg_os_order_add(mf, sgp, tkv.tkv_str))
		return (FALSE);

	if ((tok = ld_map_gettoken(mf, 0, &tkv)) != TK_SEMICOLON) {
		if (tok != TK_ERROR)
			mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSCOL));
		return (FALSE);
	}

	return (TRUE);
}

/*
 * Process a mapfile library specification definition.
 *	shared_object_name - shared object definition
 *	shared object definition : [ shared object type [ = SONAME ]]
 *					[ versions ];
 */
static Boolean
map_dash(Mapfile *mf, char *name)
{
	Token		tok;
	Sdf_desc	*sdf;
	ld_map_tkval_t	tkv;		/* Value of token */
	enum {
	    MD_NONE = 0,
	    MD_ADDVERS,
	}		dolkey = MD_NONE;

	/* Get descriptor for dependency */
	if ((sdf = ld_map_dv(mf, name)) == NULL)
		return (FALSE);

	/*
	 * Get the shared object descriptor string.
	 */
	while ((tok = ld_map_gettoken(mf, 0, &tkv)) != TK_SEMICOLON) {
		if ((tok != TK_STRING) && (tok != TK_EQUAL)) {
			if (tok != TK_ERROR)
				mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSO));
			return (FALSE);
		}

		/*
		 * Determine if the library type is accompanied with a SONAME
		 * definition.
		 */
		if (tok == TK_EQUAL) {
			if ((tok = ld_map_gettoken(mf, 0, &tkv)) !=
			    TK_STRING) {
				if (tok != TK_ERROR)
					mf_fatal0(mf,
					    MSG_INTL(MSG_MAP_EXPSO));
				return (FALSE);
			}
			switch (dolkey) {
			case MD_ADDVERS:
				if (!ld_map_dv_entry(mf, sdf, TRUE,
				    tkv.tkv_str))
					return (FALSE);
				break;
			case MD_NONE:
				mf_fatal(mf, MSG_INTL(MSG_MAP_UNEXTOK), '=');
				return (FALSE);
			}
			dolkey = MD_NONE;
			continue;
		}

		/*
		 * A shared object type has been specified.  This may also be
		 * accompanied by an SONAME redefinition (see above).
		 */
		if (*tkv.tkv_str == '$') {
			if (dolkey != MD_NONE) {
				mf_fatal(mf, MSG_INTL(MSG_MAP_UNEXTOK), '$');
				return (FALSE);
			}
			tkv.tkv_str++;
			ld_map_lowercase(tkv.tkv_str);
			if (strcmp(tkv.tkv_str, MSG_ORIG(MSG_MAP_ADDVERS)) ==
			    0) {
				dolkey = MD_ADDVERS;
			} else {
				mf_fatal(mf, MSG_INTL(MSG_MAP_UNKSOTYP),
				    tkv.tkv_str);
				return (FALSE);
			}
			continue;
		}

		/*
		 * shared object version requirement.
		 */
		if (!ld_map_dv_entry(mf, sdf, FALSE, tkv.tkv_str))
			return (FALSE);
	}

	return (TRUE);
}


/*
 * Process a symbol definition.  Historically, this originated from processing
 * a version definition.  However, this has evolved into a generic means of
 * defining symbol references and definitions (see Defining Additional Symbols
 * in the Linker and Libraries guide for the complete syntax).
 *
 * [ name ] {
 *	scope:
 *		 symbol [ = [ type ] [ value ] [ size ] [ attribute ] ];
 * } [ dependency ];
 *
 */
static Boolean
map_version(Mapfile *mf, char *name)
{
	Token		tok;
	ld_map_tkval_t	tkv;		/* Value of token */
	ld_map_ver_t	mv;
	ld_map_sym_t	ms;
	Ofl_desc	*ofl = mf->mf_ofl;

	/* Establish the version descriptor and related data */
	if (!ld_map_sym_ver_init(mf, name, &mv))
		return (FALSE);

	/*
	 * Scan the mapfile entry picking out scoping and symbol definitions.
	 */
	while ((tok = ld_map_gettoken(mf, 0, &tkv)) != TK_RIGHTBKT) {
		uint_t		filter = 0;

		if (tok != TK_STRING) {
			if (tok == TK_ERROR) {
				mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSYM_2));
				return (FALSE);
			}
			mv.mv_errcnt++;
			continue;
		}

		/* The default value for all the symbol attributes is 0 */
		(void) memset(&ms, 0, sizeof (ms));
		ms.ms_name = tkv.tkv_str;

		tok = ld_map_gettoken(mf, 0, &tkv);
		if (tok == TK_ERROR) {
			mv.mv_errcnt++;
			continue;
		}

		/*
		 * Turn off the WEAK flag to indicate that definitions are
		 * associated with this version.  It would probably be more
		 * accurate to only remove this flag with the specification of
		 * global symbols, however setting it here allows enough slop
		 * to compensate for the various user inputs we've seen so far.
		 * Only if a closed version is specified (i.e., "SUNW_1.x {};")
		 * will a user get a weak version (which is how we document the
		 * creation of weak versions).
		 */
		mv.mv_vdp->vd_flags &= ~VER_FLG_WEAK;

		switch (tok) {
		case TK_COLON:
			ld_map_sym_scope(mf, ms.ms_name, &mv);
			continue;

		case TK_EQUAL:
			/*
			 * A full blown symbol definition follows.
			 * Determine the symbol type and any virtual address or
			 * alignment specified and then fall through to process
			 * the entire symbols information.
			 */
			while ((tok = ld_map_gettoken(mf, 0, &tkv)) !=
			    TK_SEMICOLON) {
				if (tok == TK_ERROR)
					return (FALSE);
				if (tok != TK_STRING) {
					mf_fatal0(mf,
					    MSG_INTL(MSG_MAP_MALFORM));
					return (FALSE);
				}

				/*
				 * If we had previously seen AUX or FILTER,
				 * the next string is the filtee itself.
				 * Add it, and clear the filter flag.
				 */
				if (filter) {
					ld_map_sym_filtee(mf, &mv, &ms,
					    filter, tkv.tkv_str);
					filter = 0;
					continue;
				}

				/*
				 * Determine any Value or Size attributes.
				 */
				ld_map_lowercase(tkv.tkv_str);

				if (tkv.tkv_str[0] == 'v' ||
				    tkv.tkv_str[0] == 's') {
					Xword	number;

					if (!valuetoxword(mf, &tkv, &number)) {
						mv.mv_errcnt++;
						return (FALSE);
					}

					switch (*tkv.tkv_str) {
					case 'v':
					    /* BEGIN CSTYLED */
					    if (ms.ms_value) {
						mf_fatal(mf,
						    MSG_INTL(MSG_MAP_MOREONCE),
						    MSG_INTL(MSG_MAP_SYMVAL));
						mv.mv_errcnt++;
						continue;
					    }
					    /* LINTED */
					    ms.ms_value = (Addr)number;
					    ms.ms_value_set = TRUE;
					    break;
					    /* END CSTYLED */
					case 's':
					    /* BEGIN CSTYLED */
					    if (ms.ms_size) {
						mf_fatal(mf,
						    MSG_INTL(MSG_MAP_MOREONCE),
						    MSG_INTL(MSG_MAP_SYMSIZE));
						mv.mv_errcnt++;
						continue;
					    }
					    /* LINTED */
					    ms.ms_size = (Addr)number;
					    ms.ms_size_set = TRUE;
					    break;
					    /* END CSTYLED */
					}

				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_FUNCTION)) == 0) {
					ms.ms_shndx = SHN_ABS;
					ms.ms_sdflags |= FLG_SY_SPECSEC;
					ms.ms_type = STT_FUNC;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_DATA)) == 0) {
					ms.ms_shndx = SHN_ABS;
					ms.ms_sdflags |= FLG_SY_SPECSEC;
					ms.ms_type = STT_OBJECT;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_COMMON)) == 0) {
					ms.ms_shndx = SHN_COMMON;
					ms.ms_sdflags |= FLG_SY_SPECSEC;
					ms.ms_type = STT_OBJECT;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_PARENT)) == 0) {
					ms.ms_sdflags |= FLG_SY_PARENT;
					ofl->ofl_flags |= FLG_OF_SYMINFO;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_EXTERN)) == 0) {
					ms.ms_sdflags |= FLG_SY_EXTERN;
					ofl->ofl_flags |= FLG_OF_SYMINFO;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_DIRECT)) == 0) {
					ms.ms_sdflags |= FLG_SY_DIR;
					ofl->ofl_flags |= FLG_OF_SYMINFO;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_NODIRECT)) == 0) {
					ms.ms_sdflags |= FLG_SY_NDIR;
					ofl->ofl_flags |= FLG_OF_SYMINFO;
					ofl->ofl_flags1 |=
					    (FLG_OF1_NDIRECT | FLG_OF1_NGLBDIR);
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_FILTER)) == 0) {
					/* Next token is the filtee */
					filter = FLG_SY_STDFLTR;
					continue;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_AUXILIARY)) == 0) {
					/* Next token is the filtee */
					filter = FLG_SY_AUXFLTR;
					continue;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_INTERPOSE)) == 0) {
					/* BEGIN CSTYLED */
					if (!(ofl->ofl_flags & FLG_OF_EXEC)) {
					    mf_fatal0(mf,
						MSG_INTL(MSG_MAP_NOINTPOSE));
					    mv.mv_errcnt++;
					    break;
					}
					/* END CSTYLED */
					ms.ms_sdflags |= FLG_SY_INTPOSE;
					ofl->ofl_flags |= FLG_OF_SYMINFO;
					ofl->ofl_dtflags_1 |= DF_1_SYMINTPOSE;
					continue;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_DYNSORT)) == 0) {
					ms.ms_sdflags |= FLG_SY_DYNSORT;
					ms.ms_sdflags &= ~FLG_SY_NODYNSORT;
					continue;
				} else if (strcmp(tkv.tkv_str,
				    MSG_ORIG(MSG_MAP_NODYNSORT)) == 0) {
					ms.ms_sdflags &= ~FLG_SY_DYNSORT;
					ms.ms_sdflags |= FLG_SY_NODYNSORT;
					continue;
				} else {
					mf_fatal(mf,
					    MSG_INTL(MSG_MAP_UNKSYMDEF),
					    tkv.tkv_str);
					mv.mv_errcnt++;
					continue;
				}
			}
			/* FALLTHROUGH */

		case TK_SEMICOLON:
			/* Auto-reduction directive ('*')? */
			if (*ms.ms_name == '*') {
				ld_map_sym_autoreduce(mf, &mv);
				continue;
			}

			/*
			 * Catch the error where the AUX or FILTER keyword
			 * was used, but the filtee wasn't supplied.
			 */
			if (filter && (ms.ms_filtee == NULL)) {
				mf_fatal(mf, MSG_INTL(MSG_MAP_NOFILTER),
				    ms.ms_name);
				mv.mv_errcnt++;
				continue;
			}

			/*
			 * Add the new symbol.  It should be noted that all
			 * symbols added by the mapfile start out with global
			 * scope, thus they will fall through the normal symbol
			 * resolution process.  Symbols defined as locals will
			 * be reduced in scope after all input file processing.
			 */
			if (!ld_map_sym_enter(mf, &mv, &ms, NULL))
				return (FALSE);
			break;

		default:
			mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSCOL));
			mv.mv_errcnt++;
			continue;
		}
	}

	if (mv.mv_errcnt)
		return (FALSE);

	/*
	 * Determine if any version references are provided after the close
	 * bracket, parsing up to the terminating ';'.
	 */
	if (!ld_map_sym_ver_fini(mf, &mv))
		return (FALSE);

	return (TRUE);
}

/*
 * Parse the mapfile --- Sysv syntax
 */
Boolean
ld_map_parse_v1(Mapfile *mf)
{
	Sg_desc		*sgp1;		/* seg descriptor being manipulated */
	Ent_desc	*enp;		/* segment entrance criteria. */
	Token		tok;		/* current token. */
	Boolean		new_segment;	/* If true, defines new segment */
	char		*name;
	Ofl_desc	*ofl = mf->mf_ofl;
	ld_map_tkval_t	tkv;		/* Value of token */
	avl_index_t	where;

	/*
	 * We now parse the mapfile until the gettoken routine returns EOF.
	 */
	while ((tok = ld_map_gettoken(mf, TK_F_EOFOK, &tkv)) != TK_EOF) {
		Xword	ndx;

		/*
		 * At this point we are at the beginning of a line, and the
		 * variable tkv.tkv_str points to the first string on the line.
		 * All mapfile entries start with some string token except it
		 * is possible for a scoping definition to start with `{'.
		 */
		if (tok == TK_LEFTBKT) {
			if (!map_version(mf, NULL))
				return (FALSE);
			continue;
		}
		if (tok != TK_STRING) {
			if (tok != TK_ERROR)
				mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPSEGNAM));
			return (FALSE);
		}

		/*
		 * Save the initial token.
		 */
		name = tkv.tkv_str;

		/*
		 * Now check the second character on the line.  The special `-'
		 * and `{' characters do not involve any segment manipulation so
		 * we handle them first.
		 */
		tok = ld_map_gettoken(mf, 0, &tkv);
		if (tok == TK_ERROR)
			return (FALSE);
		if (tok == TK_DASH) {
			if (!map_dash(mf, name))
				return (FALSE);
			continue;
		}
		if (tok == TK_LEFTBKT) {
			if (!map_version(mf, name))
				return (FALSE);
			continue;
		}

		/*
		 * If we're here we need to interpret the first string as a
		 * segment name.  Is this an already known segment?
		 */
		sgp1 = ld_seg_lookup(mf->mf_ofl, name, &where);
		new_segment = sgp1 == NULL;
		if (!new_segment)
			sgp1->sg_flags &= ~FLG_SG_DISABLED;

		/*
		 * If the second token is a '|' then we had better have found a
		 * segment.  It is illegal to perform section within segment
		 * ordering before the segment has been declared.
		 */
		if (tok == TK_PIPE) {
			if (sgp1 == NULL) {
				mf_fatal(mf, MSG_INTL(MSG_MAP_SECINSEG),
				    name);
				return (FALSE);
			}
			if (!map_pipe(mf, sgp1))
				return (FALSE);
			continue;
		}

		/*
		 * If segment does not exist, allocate a descriptor with
		 * its values set to 0 so that map_equal() can detect
		 * changing attributes.
		 */
		if (new_segment &&
		    ((sgp1 = ld_map_seg_alloc(name, PT_NULL, 0)) == NULL))
			return (FALSE);

		/*
		 * Now check the second token from the input line.
		 */
		switch (tok) {
		case TK_EQUAL:		/* Create/modify segment */
			/*
			 * We use the same syntax for hardware/software
			 * capabilities as we do for segments. If the
			 * "segment name" matches one of these, then
			 * process the capabilities instead of treating it
			 * as a segment. Note that no dynamic memory has
			 * been allocated for the segment descriptor yet,
			 * so we can bail without leaking memory.
			 */
			if (strcmp(sgp1->sg_name,
			    MSG_ORIG(MSG_STR_HWCAP_1)) == 0) {
				if (!map_cap(mf, CA_SUNW_HW_1,
				    &ofl->ofl_ocapset.oc_hw_1))
					return (FALSE);
				continue;
			}
			if (strcmp(sgp1->sg_name,
			    MSG_ORIG(MSG_STR_SFCAP_1)) == 0) {
				if (!map_cap(mf, CA_SUNW_SF_1,
				    &ofl->ofl_ocapset.oc_sf_1))
					return (FALSE);
				continue;
			}

			/*
			 * If not a new segment, show the initial value
			 * before modifying it.
			 */
			if (!new_segment && DBG_ENABLED) {
				ndx = ld_map_seg_index(mf, sgp1);
				Dbg_map_seg(ofl, DBG_STATE_MOD_BEFORE,
				    ndx, sgp1, mf->mf_lineno);
			}

			/* Process the segment */
			if (!map_equal(mf, sgp1))
				return (FALSE);

			/*
			 * Special case for STACK "segments":
			 *
			 * The ability to modify the stack flags was added
			 * long after this sysv syntax was designed. It was
			 * fit into the existing syntax by treating it as a
			 * segment. However, there can only be one stack program
			 * header, while segment syntax requires user to supply
			 * a name. This is confusing, and it allows the user to
			 * attempt to create more than one stack segment. The
			 * original implementation had a test to catch this.
			 *
			 * If this is a stack segment, locate the real stack
			 * descriptor and transfer the flags to it. We then
			 * free the allocated descriptor without inserting it.
			 * The end result is that all stack segments simply
			 * alter the one stack descriptor, and the segment
			 * name is ignored.
			 */
			if (sgp1->sg_phdr.p_type == PT_SUNWSTACK) {
				Sg_desc	*stack = ld_map_seg_stack(mf);

				if (sgp1->sg_flags & FLG_SG_P_FLAGS)
					stack->sg_phdr.p_flags =
					    sgp1->sg_phdr.p_flags;

				DBG_CALL(Dbg_map_seg(ofl,
				    DBG_STATE_MOD_AFTER, ndx, sgp1,
				    mf->mf_lineno));

				free(sgp1);
				break;
			}

			/*
			 * If this is a new segment, finish its initialization
			 * and insert it into the segment list.
			 */
			if (new_segment) {
				switch (ld_map_seg_insert(mf, DBG_STATE_NEW,
				    sgp1, where)) {
				case SEG_INS_SKIP:
					continue;
				case SEG_INS_FAIL:
					return (FALSE);
				}
			} else {
				/* Not new. Show what's changed */
				DBG_CALL(Dbg_map_seg(ofl,
				    DBG_STATE_MOD_AFTER, ndx, sgp1,
				    mf->mf_lineno));
			}
			break;

		case TK_COLON:		/* Section to segment mapping */
			/*
			 * If this is a new segment, finish its initialization
			 * and insert it into the segment list.
			 *
			 * If it is not a new segment, ensure that it is
			 * not an empty segment reservation, as sections
			 * cannot be assigned to those.
			 */
			if (new_segment) {
				switch (ld_map_seg_insert(mf,
				    DBG_STATE_NEW_IMPLICIT, sgp1, where)) {
				case SEG_INS_SKIP:
					continue;
				case SEG_INS_FAIL:
					return (FALSE);
				}
			} else if (sgp1->sg_flags & FLG_SG_EMPTY) {
				mf_fatal0(mf, MSG_INTL(MSG_MAP_SEGEMPSEC));
				return (FALSE);
			}

			/*
			 * Create new entrance criteria descriptor, and
			 * process the mapping directive.
			 */
			enp = ld_map_seg_ent_add(mf, sgp1, NULL);
			if ((enp == NULL) || !map_colon(mf, enp))
				return (FALSE);
			DBG_CALL(Dbg_map_ent(ofl->ofl_lml, enp, ofl,
			    mf->mf_lineno));
			break;

		case TK_ATSIGN:		/* Section size symbol */
			/*
			 * If this is a new segment, finish its initialization
			 * and insert it into the segment list.
			 */
			if (new_segment) {
				switch (ld_map_seg_insert(mf,
				    DBG_STATE_NEW_IMPLICIT, sgp1, where)) {
				case SEG_INS_SKIP:
					continue;
				case SEG_INS_FAIL:
					return (FALSE);
				}
			}
			if (!map_atsign(mf, sgp1))
				return (FALSE);
			break;

		case TK_ERROR:
			return (FALSE);		/* Error was already issued */

		default:
			mf_fatal0(mf, MSG_INTL(MSG_MAP_EXPEQU));
			return (FALSE);
		}
	}

	return (TRUE);
}