|
root / base / usr / src / cmd / mdb / tools
tools Plain Text 1402 lines 37.1 KB
   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
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Copyright 2022 Garrett D'Amore
#

include $(SRC)/Makefile.master

SUBDIRS = scripts setdynflag

.PARALLEL: $(SUBDIRS)

include ../Makefile.subdirs
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"%Z%%M%	%I%	%E% SMI"
#

%.o: ../../common/%.c
	$(COMPILE.c) $<
	$(POST_PROCESS_O)

%.ln: ../../common/%.c
	$(LINT.c) -c $<
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <elf.h>

#include <util.h>

void
die(char *format, ...)
{
	va_list ap;
	int err = errno;

	(void) fprintf(stderr, "%s: ", progname);

	va_start(ap, format);
	/* LINTED - variable format specifier */
	(void) vfprintf(stderr, format, ap);
	va_end(ap);

	if (format[strlen(format) - 1] != '\n')
		(void) fprintf(stderr, ": %s\n", strerror(err));

	exit(1);
}

void
elfdie(char *format, ...)
{
	va_list ap;

	(void) fprintf(stderr, "%s: ", progname);

	va_start(ap, format);
	/* LINTED - variable format specifier */
	(void) vfprintf(stderr, format, ap);
	va_end(ap);

	if (format[strlen(format) - 1] != '\n')
		(void) fprintf(stderr, ": %s\n", elf_errmsg(elf_errno()));

	exit(1);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <string.h>
#include <libelf.h>
#include <gelf.h>

#include <util.h>

int
findelfsecidx(Elf *elf, char *tofind)
{
	Elf_Scn *scn = NULL;
	GElf_Ehdr ehdr;
	GElf_Shdr shdr;

	if (gelf_getehdr(elf, &ehdr) == NULL)
		elfdie("failed to get ELF header");

	while ((scn = elf_nextscn(elf, scn)) != NULL) {
		char *name;

		if (gelf_getshdr(scn, &shdr) == NULL ||
		    (name = elf_strptr(elf, ehdr.e_shstrndx,
		    (size_t)shdr.sh_name)) == NULL)
			elfdie("failed to get section header");

		if (strcmp(name, tofind) == 0)
			return (elf_ndxscn(scn));
	}

	return (-1);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _UTIL_H
#define	_UTIL_H

#include <libelf.h>

#ifdef __cplusplus
extern "C" {
#endif

extern int findelfsecidx(Elf *, char *);

extern void die(char *, ...) __NORETURN;
extern void elfdie(char *, ...) __NORETURN;

extern const char *progname;

#ifdef __cplusplus
}
#endif

#endif /* _UTIL_H */
#
# 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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
#
#ident	"%Z%%M%	%I%	%E% SMI"

.KEEP_STATE:
.SUFFIXES: .pl

include ../../../Makefile.cmd

PKGSCRIPTS = mdb mkmodules
BLDSCRIPTS = tigen map2linktest hdr2map

ROOTOPTPKGBIN = $(ROOT)/opt/SUNWonmdb/bin
ROOTPKGSCRIPTS = $(PKGSCRIPTS:%=$(ROOTOPTPKGBIN)/%)

all install: $(BLDSCRIPTS) $(PKGSCRIPTS)

clean.lint dmods install_h lint:

clobber: clean

clean:
	$(RM) $(BLDSCRIPTS) $(PKGSCRIPTS)

pkg: $(ROOTPKGSCRIPTS)

$(ROOTOPTPKGBIN):
	$(INS.dir)

$(ROOTPKGSCRIPTS): $(ROOTOPTPKGBIN)

$(ROOTOPTPKGBIN)/%: %.sh
	$(INS.rename)
#!/bin/ksh
#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"%Z%%M%	%I%	%E% SMI"
#

#
# Given a header file, extract function prototypes and global variable
# declarations in a form that can be used in a mapfile.  The list of extracted
# functions and variables will be combined with a user-specified template to
# create a complete mapfile.
#
# Template
# --------
#
# The template contains two sections - the prologue, and the epilogue.  These
# sections are used, verbatim, as the beginning and the end of the mapfile.
# Sections begin and end with single-line comments whose sole contents are
# "/* BEGIN $section */" and "/* END $section */".
#
# Template example:
#
# /* BEGIN PROLOGUE */
# [ ... prologue goes here ... ]
# /* END PROLOGUE */
# /* BEGIN EPILOGUE */
# [ ... epilogue goes here ... ]
# /* END EPILOGUE */
#
# Selective Exportation
# ---------------------
#
# Some header files will have a public/private interface mix that is strongly
# biased towards private interfaces.  That is, of the interfaces declared by
# a given header file, the majority of them are private.  Only a small subset
# of interfaces are to be exported publicly.  Using Selective Exportation, a
# special comment is included in the header file, declaring to this script that
# only a subset of interfaces - those with a marking declared in the comment -
# should be included in the mapfile.  The marking is itself a special comment,
# whose format is declared using a directive like this:
#
# 	MAPFILE: export "Driver OK"
#
# Using the above directive, only those function prototypes and variable
# declarations with "/* Driver OK */" comments included in the mapfile.  Note
# that the comment must be at the end of the first line.  If the declaration
# spans multiple lines, the exportation comment must appear on the first line.
#
# Examples of functions selected for exportation:
#
# MAPFILE: export "Driver OK"
#
# extern int foo(int);		/* Driver OK */
# extern void bar(int, int,	/* Driver OK */
#     int, void *);
#
# Selective Exportation may not be used in the same file as Selective Exclusion.
#
# Selective Exclusion
# -------------------
#
# Selective Exclusion is to be used in cases where the public/private interface
# mix is reversed - where public interfaces greatly outnumber the private ones.
# In this case, we want to be able to mark the private ones, thus telling this
# script that the marked interfaces are to be excluded from the mapfile.
# Marking is accomplished via a process similar to that used for Selective
# Exportation.  A directive is included in a comment, and is formatted like
# this:
#
#	MAPFILE: exclude "Internal"
#
# Using the above directive, function prototypes and variable declarations with
# "/* Internal */" comments would be excluded.  Note that the comment must be at
# the end of the first line.  If the declaration spans multiple lines, the
# exclusion comment must appear on the first line.
#
# Examples of functions excluded from exportation:
#
# MAPFILE: exclude "Internal"
#
# extern int foo(int);		/* Internal */
# extern void bar(int, int,	/* Internal */
#	int, void *);
#
# Selective Exclusion may not be used in the same file as Selective Exportation.
#

function extract_prototypes
{
	typeset header="$1"
	typeset prefix="$2"

	nawk -v prefix="$prefix" <$header '
		/^.*MAPFILE: export \"[^\"]*\"$/ {
			if (protoexclude) {
				print "ERROR: export after exclude\n";
				exit(1);
			}
		
			sub(/^[^\"]*\"/, "");
			sub(/\"$/, "");

			exportmark=sprintf("/* %s */", $0);
			next;
		}

		/^.*MAPFILE: exclude \"[^\"]*\"$/ {
			if (protomatch) {
				print "ERROR: exclude after export";
				exit(1);
			}

			sub(/^[^\"]*\"/, "");
			sub(/\"$/, "");

			excludemark=sprintf("/* %s */", $0);
			next;
		}

		exportmark {
			# Selective Exportation has been selected (exportmark is
			# set), so exclude this line if it does not have the
			# magic export mark.
			if (length($0) < length(exportmark) ||
			    substr($0, length($0) - length(exportmark) + 1) != \
			    exportmark)
				next;
		}

		excludemark {
			# Selective Exclusion has been selected (excludemark is
			# set), so exclude this line only if it has the magic
			# exclude mark.
			if (length($0) > length(excludemark) &&
			    substr($0, \
			    length($0) - length(excludemark) + 1) == \
			    excludemark)
				next;
		}

		# Functions
		/^extern.*\(/ {
			for (i = 1; i <= NF; i++) {
				if (sub(/\(.*$/, "", $i)) {
					sub(/^\*/, "", $i);
					if (!seenfn[$i]) {
						printf("%s%s;\n", prefix, $i);
						seenfn[$i] = 1;
					}
					break;
				}
			}
			next;
		}

		# Global variables
		/^extern[^\(\)]*;/ {
			for (i = 1; i <= NF; i++) {
				if (match($i, /;$/)) {
					printf("%s%s; /* variable */\n", prefix,
					    substr($i, 1, length($i) - 1));
					break;
				}
			}
			next;
		}
	' || die "Extraction failed"
}

function extract_section
{
	typeset skel="$1"
	typeset secname="$2"

	nawk <$skel -v name=$secname -v skel=$skel '
	    /\/\* [^ ]* [^ ]* \*\// && $3 == name {
		if ($2 == "BEGIN") {
			printing = 1;
		} else {
			printing = 0;
		}
		next;
	    }

	    printing != 0 { print; }
	'
}

function die
{
	echo "$PROGNAME: $@" >&2
	exit 1
}

function usage
{
	echo "Usage: $PROGNAME -t tmplfile header [header ...]" >&2
	exit 2
}

PROGNAME=$(basename "$0")

while getopts t: c ; do
	case $c in
	    t)
		mapfile_skel=$OPTARG
		;;
	    ?)
		usage
	esac
done

[[ -z "$mapfile_skel" ]] && usage
[[ ! -f $mapfile_skel ]] && die "Couldn't open template $tmplfile"

shift $(($OPTIND - 1))

[[ $# -lt 1 ]] && usage

for file in $@ ; do
	[[ ! -f $file ]] && die "Can't open input file $file"
done

extract_section $mapfile_skel PROLOGUE

for file in $@ ; do
	echo "\t\t/*"
	echo "\t\t * Exported functions and variables from:"
	echo "\t\t *  $file"
	echo "\t\t */"
	extract_prototypes $file "\t\t"
	echo
done

extract_section $mapfile_skel EPILOGUE
#!/bin/ksh
#
# 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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"%Z%%M%	%I%	%E% SMI"
#

#
# Create dummy functions for each of the functions in the module API.  We can
# then link a module against an object file created from the output of this
# script to determine whether or not that module restricts itself to the API.
# If the module uses functions outside of the module API, then it cannot be
# used as a kmdb module.
#
nawk '
	/^[ 	]*global:[ 	]*$/ {
		printing = 1;
		next;
	}

	/^[	]*local:[ 	]*$/ {
		printing = 0;
		next;
	}

	# Skip blank lines and comments
	/^$/ { next; }
	/^[ 	]*#/ { next;}

	# Print globals only
	printing == 0 { next; }

	# Symbols beginning with "kmdb_" are not in the module API - they are
	# private to kmdb.
	$1 ~ /^kmdb_/ { next; }

	# Symbols which have the token "variable" are seen as an int
	$3 ~ /variable/ {
		if (seen[$1]) {
			next;
		}

		seen[$1] = 1;

		printf("int %s = 0;\n", substr($1, 1, length($1) - 1));
		next;
	}

	$1 !~ /;$/ { next; }

	# Print everything else that we have not already seen as a function
	# definition so we can create our filter.
	{
		if (seen[$1]) {
			next;
		}

		seen[$1] = 1;

		printf("void %s(void) {}\n", substr($1, 1, length($1) - 1));
	}
'

#
# kmdb modules cannot have their own _init, _fini, or _info routines.  By
# creating dummies for them here, a link against an object file created from
# the output of this script will fail if the module defines one of them.
#
echo "void _init(void) {}"
echo "void _info(void) {}"
echo "void _fini(void) {}"
#
# The SunStudio compiler may generate calls to _memcpy and so we
# need to make sure that the correct symbol exists for these calls.
#
echo "void _memcpy(void) {}"
#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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) 1998-2001 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"%Z%%M%	%I%	%E% SMI"

mdb_lib=/net/mdb.eng/mdb/archives	# Archive library path
mdb_ws=/net/mdb.eng/mdb/snapshot/latest	# Snapshot of latest workspace
mdb_args=				# Debugger argument string

os_name='s81'				# Default OS name prefix
os_rel='5.9'				# Default OS release number

mach=`/bin/uname -p`		# Machine type
unset mdb_exec build root		# Local variables

#
# Attempt to locate a suitable mdb binary to execute, first on the local
# machine, then in the user's workspace, and finally on the MDB server.
# If we select the user's workspace, we also set $root to their proto area
# to force MDB to use shared libraries installed there as well.
#
if [ -n "$CODEMGR_WS" -a -x $CODEMGR_WS/proto/root_$mach/usr/bin/mdb ]; then
	mdb_exec=$CODEMGR_WS/proto/root_$mach/usr/bin/mdb
	root=$CODEMGR_WS/proto/root_$mach
elif [ -x /usr/bin/mdb -a ! -d /mdb ]; then
	mdb_exec=/usr/bin/mdb
	root=$mdb_lib/$mach/%R/%V
elif [ -x /usr/bin/mdb -a -d /mdb ]; then
	for isa in `isalist`; do
		if [ -x /usr/bin/$isa/mdb ]; then
			mdb_exec=/usr/bin/$isa/mdb
			break
		fi
	done
	if [ -z "$mdb_exec" ]; then
		echo "$0: cannot find mdb binary in ISA subdirectories" >& 2
		exit 1
	fi
	root=$mdb_lib/$mach/%R/%V
elif [ -x $mdb_ws/proto/root_$mach/usr/bin/mdb ]; then
	mdb_exec=$mdb_ws/proto/root_$mach/usr/bin/mdb
	root=$mdb_lib/$mach/%R/%V
fi

#
# Abort if we were not able to locate a copy of mdb to execute.
#
if [ -z "$mdb_exec" ]; then
	echo "$0: failed to locate mdb executable" >& 2
	exit 1
fi

#
# The wrapper script handles several special command-line arguments that are
# used to select a desired set of MDB macros, modules, and a libkvm binary.
#
if [ $# -gt 0 ]; then
	case "$1" in
	-s[0-9]*)
		build=`echo "$1" | tr -d -`
		shift
		;;

	-[0-9]|-[0-9][0-9])
		build=`echo "$1" | tr -d -`
		if [ $build -lt 10 ]; then
			build=${os_name}_0$build
		else
			build=${os_name}_$build
		fi
		shift
		;;

	-[0-9][0-9]-|-[0-9][0-9][A-Za-z])
		build=${os_name}_`echo "$1" | cut -c2- | tr '[A-Z]' '[a-z]'`
		shift
		;;

	-B) build=$os_rel/Beta; shift ;;
	-U) build=$os_rel/Beta_Update; shift ;;
	-G) build=$os_rel/Generic; shift ;;

	-\?)
		echo "Usage: $0" \
		     "[ -s<rel> | -s<bld> | -[0-9]+ | -B | -G | -U ] args ..."

		echo "\t-s<rel>  Use proto area for specified release"
		echo "\t         e.g. -${os_name}"
		echo "\t-s<bld>  Use proto area for specified build"
		echo "\t         e.g. -${os_name}_01"
		echo "\t-[0-9]+  Use proto area for specified build of $os_name"
		echo "\t-B       Use proto area for $os_rel Beta build"
		echo "\t-G       Use proto area for $os_rel Generic build\n"
		echo "\t-U       Use proto area for $os_rel Beta_Update build"
		;;
	esac
fi

#
# If a build was specified, using the corresponding proto area from $mdb_lib.
# Note that this will override the $root setting determined above.
#
[ -n "$build" ] && root=$mdb_lib/$mach/$build

#
# If a proto area was set either by specifying a build number, or by using
# mdb from $CODEMGR_WS, set LD_LIBRARY_PATH accordingly.  This allows mdb to
# pick up the appropriate libkvm.so to examine dumps from that build.
# We also add the -R flag to the mdb command line so that mdb will modify
# its default macro include and module library paths to use the build root.
#
if [ -n "$build" -o "$root" = "$CODEMGR_WS/proto/root_$mach" ]; then
	if [ -n "$build" -a ! -d $root ]; then
		echo "mdb: $root is missing or not a directory" >& 2
		exit 1
	fi

	[ -n "$LD_LIBRARY_PATH" ] && LD_LIBRARY_PATH=$LD_LIBRARY_PATH:
	LD_LIBRARY_PATH="$LD_LIBRARY_PATH$root/usr/lib"

	[ -n "$LD_LIBRARY_PATH_64" ] && LD_LIBRARY_PATH_64=$LD_LIBRARY_PATH_64:
	LD_LIBRARY_PATH_64="$LD_LIBRARY_PATH_64$root/usr/lib/sparcv9"

	export LD_LIBRARY_PATH LD_LIBRARY_PATH_64

elif [ $mdb_exec = $mdb_ws/proto/root_$mach/usr/bin/mdb ]; then
	#
	# We also need to set LD_LIBRARY_PATH if we're using mdb.eng's mdb
	# binary -- it requires the new libproc.so to work properly.
	#
	usrlib=$mdb_ws/proto/root_$mach/usr/lib

	[ -n "$LD_LIBRARY_PATH" ] && LD_LIBRARY_PATH=$LD_LIBRARY_PATH:
	LD_LIBRARY_PATH="$LD_LIBRARY_PATH$usrlib"

	[ -n "$LD_LIBRARY_PATH_64" ] && LD_LIBRARY_PATH_64=$LD_LIBRARY_PATH_64:
	LD_LIBRARY_PATH_64="$LD_LIBRARY_PATH_64$usrlib/sparcv9"

	export LD_LIBRARY_PATH LD_LIBRARY_PATH_64
fi

exec $mdb_exec -R $root $mdb_args "$@"
#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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) 1998-2001 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"%Z%%M%	%I%	%E% SMI"

#
# Script to build the MDB modules present in a workspace against the set of
# include files saved in an MDB "root" (created with mkroot.sh), and install
# resulting modules into this tree so that it can be used as the argument
# to ``mdb -R'' or by mdb's auto-detect code, used by the mdb.sh wrapper.
# We use the ``make dmods'' target to do the build -- this is equivalent to
# make install, but does not build the debugger itself, and pass a special
# define (_MDB_BLDID) into the compilation environment so that the module
# source can detect the various changes in header files, etc.  This is only
# used for module source kept on mdb.eng that needs to compile against
# legacy builds of the operating system.
#

PNAME=`basename $0`
opt_R=false
umask 022

if [ $# -ne 0 -a "x$1" = x-R ]; then
	opt_R=true
	shift
fi

if [ $# -ne 1 -o -z "${INCROOT:=$1}" -o ! -d "$INCROOT" ]; then
	echo "Usage: $PNAME [-R] root-dir"
	exit 2
fi

if $opt_R; then
	ROOT="$INCROOT"
	export ROOT
fi

if [ -z "$SRC" -a -z "$CODEMGR_WS" ]; then
	echo "$PNAME: \$SRC or \$CODEMGR_WS must be set" >& 2
	exit 1
fi

if [ -z "$ROOT" ]; then
	echo "$PNAME: \$ROOT must be set" >& 2
	exit 1
fi

if [ -z "$SRC" ]; then
	SRC="$CODEMGR_WS/usr/src"
	export SRC
fi

#
# Derive a build-id based on the name of the $ROOT directory.  The build-id
# is passed into the compilation environment as _MDB_BLDID, and consists of
# a 4-digit hexadecimal number whose first two digits are the release number
# (e.g. 0x27XX for Solaris 2.7) and whose last two digits are the build number.
#
case "`basename $INCROOT`" in
s297_fcs) BLDID=0x2637 ;;
s998_fcs) BLDID=0x2721 ;;
 s28_fcs) BLDID=0x2838 ;;
  s399_*) BLDID=0x27FF ;;
  s599_*) BLDID=0x27FF ;;
  s899_*) BLDID=0x27FF ;;
 s1199_*) BLDID=0x27FF ;;
   s81_*) BLDID=0x81`basename $INCROOT | sed 's/s81_//' | tr -cd '[0-9]'` ;;
       *) echo "$PNAME: cannot derive _MDB_BLDID for $INCROOT" >& 2; exit 1 ;;
esac

#
# Set up the necessary environment variables to perform a build.  Basically
# we need to do the same stuff as bld_env or bfmenv.
#
[ `id | cut -d'(' -f1` != 'uid=0' ] && CH='#' || CH=; export CH
VERSION=${VERSION:-"`basename $INCROOT`:`date '+%m/%d/%y'`"}; export VERSION
MACH=`uname -p`; export MACH
TMPDIR=/tmp; export TMPDIR
NODENAME=`uname -n`; export NODENAME
PATH="$PUBLIC/bin:$PUBLIC/bin/$MACH:/opt/onbld/bin:/opt/onbld/bin/$MACH:/bin:/sbin:/usr/bin:/usr/sbin:."; export PATH
INS=/opt/onbld/bin/$MACH/install.bin; export INS
MAKEFLAGS=e; export MAKEFLAGS

#
# We need to export $BLDID into the compilation environment, and make sure
# to remap the default include path from /usr/include to $INCROOT/usr/include.
#
ENVCPPFLAGS1="-YI,$INCROOT/usr/include"; export ENVCPPFLAGS1
ENVCPPFLAGS2="-D_MDB_BLDID=$BLDID"; export ENVCPPFLAGS2
ENVCPPFLAGS3=; export ENVCPPFLAGS3
ENVCPPFLAGS4=; export ENVCPPFLAGS4

ENVLDLIBS1=; export ENVLDLIBS1
ENVLDLIBS2=; export ENVLDLIBS2
ENVLDLIBS3=; export ENVLDLIBS3

cd $SRC && make clobber && make dmods
#!/bin/ksh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#

#
# Terminal Info Generator
#
# This script generates a static terminfo database for use by mdb.  For each
# of the terminal properties used by mdb_termio.c, this script uses tput(1)
# to determine the value of the given attribute for each specified terminal
# type.  The script produces an ANSI-C source file which contains a static
# array for each terminal type storing the properties.  An additional array
# is then declared containing a list of the terminal types and pointers to
# the previous arrays.  Finally, source code for several terminfo routines
# are included that simply access the arrays and return the saved properties.
#

# Hammerhead: /bin is a separate directory (not symlinked to /usr/bin)
PATH=/bin:/usr/bin; export PATH

PROGNAME=$(basename "$0")

usage()
{
	echo "Usage: $PROGNAME -s skel -t termio [-v] term ..." >&2
	exit 2
}

extract_section()
{
	typeset skel="$1"
	typeset secname="$2"

	nawk <$skel -v name=$secname -v skel=$skel '
	    /\/\* [^ ]* [^ ]* \*\// && $3 == name {
		if ($2 == "BEGIN") {
			printing = 1;
			printf("# %d \"%s\"\n", NR + 1, skel);
		} else {
			printing = 0;
		}
		next;
	    }

	    printing != 0 { print; }
	'
}

verbose=false
termio_c=
terminfo_skel=

while getopts s:t:v name ; do
	case $name in
	    v)
		verbose=true
		;;
	    s)
		terminfo_skel=$OPTARG
		;;
	    t)
		termio_c=$OPTARG
		;;
	    ?)
		usage
		;;
	esac
done
shift $(($OPTIND - 1))

[[ -z "$terminfo_skel" || -z "$termio_c" || $# -eq 0 ]] && usage

termlist=$*
for term in $termlist; do
	tput -T $term init >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "`basename $0`: invalid terminal -- $term" >& 2
		exit 1
	fi
done

# Extract the prologue from the skeleton
extract_section $terminfo_skel PROLOGUE

#
# For each terminal in the terminal list, produce a property definition array
# listing each property we need in mdb_termio.c and its current value.
#
for term in $termlist; do
	#
	# We don't want the compiler to blame the skeleton if it doesn't like
	# the array we generate here, so point the finger elsewhere
	#
	echo "# 1 \"dynamic $term data from tigen\""

	cterm=$(echo "$term" |tr '-' '_')

	$verbose && echo "loading terminfo for $term ... \c" >& 2
	echo "static const termio_attr_t ${cterm}_attrs[] = {"

	sed -n '/termio_attrs\[\] = /,/^}/p' $termio_c | \
	    sed -n \ 's/{ "\([a-z0-9]*\)", \([A-Z_]*\),.*/\1 \2/p' | \
	    while read attr type; do

		case "$type" in
		TIO_ATTR_REQSTR|TIO_ATTR_STR)
			data="\"`tput -T $term $attr | od -bv |
			    sed 's/^[0-9]*//;s/ /\\\\\\\\/g;/^\$/d'`\""
			[ "$data" = '""' ] && data=NULL
			;;
		TIO_ATTR_BOOL)
			tput -T $term $attr
			data=`expr 1 - $?`
			;;
		TIO_ATTR_INT)
			data=`tput -T $term $attr`
			;;
		*)
			echo "`basename $0`: unknown type for $attr: $type" >& 2
			exit 1
		esac
		echo "\t{ \"$attr\", $type, (void *)$data },"
	done

	echo "\t{ NULL, 0, NULL }"
	printf '};\n\n'

	$verbose && echo "done" >& 2
done

#
# For each terminal in the terminal list, produce an entry in the terminal
# database array linking this terminal to its terminfo property array.
#
echo "# 1 \"dynamic array from tigen\""
echo "static const termio_desc_t termio_db[] = {"
for term in $termlist; do
	cterm=$(echo "$term" |tr '-' '_')
	echo "\t{ \"$term\", ${cterm}_attrs },"
done
printf '\t{ NULL, NULL }\n};\n'

extract_section $terminfo_skel EPILOGUE

exit 0
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include ../../../Makefile.cmd

# Hammerhead: amd64-only
SUBDIRS = $(MACH64)

include ../../Makefile.subdirs
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

.KEEP_STATE:

PROG = setdynflag

SRCS = \
	die.c \
	findscn.c \
	setdynflag.c
OBJS = $(SRCS:%.c=%.o)

include $(SRC)/cmd/Makefile.cmd
include ../../common/Makefile.util

#
# We're going to run this as part of the build, so we want it to use the
# running kernel's includes and libraries.
#
include $(SRC)/Makefile.native

CPPFLAGS = -I../../common
CFLAGS += $(CCVERBOSE)
CFLAGS64 += $(CCVERBOSE)
CERRWARN += $(CNOWARN_UNINIT)
LDFLAGS += $(ZLAZYLOAD) $(BDIRECT)
LDLIBS = -lelf
NATIVE_LIBS += libelf.so libc.so

install all: $(PROG)

clobber clean:
	$(RM) $(OBJS) $(PROG)

$(PROG): $(OBJS)
	$(LINK.c) $(OBJS) -o $@ $(LDLIBS)
	$(POST_PROCESS)

%.o: ../common/%.c
	$(COMPILE.c) $<
	$(POST_PROCESS_O)
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
# Copyright 2025 Hammerhead Project
#

include ../Makefile.com
include $(SRC)/cmd/Makefile.cmd.64

install: all $(ROOTPROG64)
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Set bits in the DT_FLAGS_1 member of the .dynamic section of an object.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <libelf.h>
#include <gelf.h>
#include <string.h>
#include <fcntl.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/link.h>

#include <util.h>

/*
 * These are here because we can't be sure (yet) that the build machine has a
 * sys/link.h that includes the following #defines.  This tool will be executed
 * on the build machine, so we have to use its headers (rather than the ones
 * in $ROOT which will, by definition, be up to date).  These #defines can be
 * removed when we're sure that all build machines have recent copies of
 * sys/link.h.
 */
#ifndef	DF_1_IGNMULDEF
#define	DF_1_IGNMULDEF	0x00040000
#endif
#ifndef	DF_1_NOKSYMS
#define	DF_1_NOKSYMS	0x00080000
#endif

struct dtflagval {
	char *fv_name;
	ulong_t fv_val;
};

static struct dtflagval dtflagvals[] = {
	{ "DF_1_IGNMULDEF", DF_1_IGNMULDEF },
	{ "DF_1_NOKSYMS", DF_1_NOKSYMS },
	{ NULL }
};

const char *progname;

static void
usage(void)
{
	(void) fprintf(stderr, "Usage: %s -f flag_val file\n", progname);
	exit(2);
}

static void
set_flag(char *ifile, ulong_t flval)
{
	Elf *elf;
	Elf_Scn *scn;
	Elf_Data *data;
	GElf_Shdr shdr;
	GElf_Dyn dyn;
	int fd, secidx, nent, i;

	(void) elf_version(EV_CURRENT);

	if ((fd = open(ifile, O_RDWR)) < 0)
		die("Can't open %s", ifile);

	if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL)
		elfdie("Can't start ELF for %s", ifile);

	if ((secidx = findelfsecidx(elf, ".dynamic")) == -1)
		die("Can't find .dynamic section in %s\n", ifile);

	if ((scn = elf_getscn(elf, secidx)) == NULL)
		elfdie("elf_getscn (%d)", secidx);

	if (gelf_getshdr(scn, &shdr) == NULL)
		elfdie("gelf_shdr");

	if ((data = elf_getdata(scn, NULL)) == NULL)
		elfdie("elf_getdata");

	nent = shdr.sh_size / shdr.sh_entsize;
	for (i = 0; i < nent; i++) {
		if (gelf_getdyn(data, i, &dyn) == NULL)
			elfdie("gelf_getdyn");

		if (dyn.d_tag == DT_FLAGS_1) {
			dyn.d_un.d_val |= (Elf64_Xword)flval;

			if (gelf_update_dyn(data, i, &dyn) == 0)
				elfdie("gelf_update_dyn");

			break;
		}
	}

	if (i == nent) {
		die("%s's .dynamic section doesn't have a DT_FLAGS_1 "
		    "field\n", ifile);
	}

	if (elf_update(elf, ELF_C_WRITE) == -1)
		elfdie("Couldn't update %s with changes", ifile);

	(void) elf_end(elf);
	(void) close(fd);
}

static ulong_t
parse_flag(char *optarg)
{
	ulong_t flval = 0L;
	char *arg;
	int i;

	for (arg = strtok(optarg, ","); arg != NULL; arg = strtok(NULL, ",")) {
		for (i = 0; dtflagvals[i].fv_name != NULL; i++) {
			if (strcmp(dtflagvals[i].fv_name, arg) == 0)
				flval |= dtflagvals[i].fv_val;
		}
	}

	return (flval);
}

int
main(int argc, char **argv)
{
	ulong_t flval = 0L;
	int c;

	progname = basename(argv[0]);

	while ((c = getopt(argc, argv, "f:")) != EOF) {
		switch (c) {
		case 'f':
			if ((flval = strtoul(optarg, NULL, 0)) == 0 &&
			    (flval = parse_flag(optarg)) == 0)
				usage();
			break;
		default:
			usage();
		}
	}

	if (flval == 0 || argc - optind != 1)
		usage();

	set_flag(argv[optind], flval);

	return (0);
}