|
root / base / usr / src / cmd / sgs / librtld_db / common
common Plain Text 1684 lines 45.3 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
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
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	__RTLD_DB_H
#define	__RTLD_DB_H

#include <proc_service.h>
#include <thread.h>
#include <synch.h>
#include <sgs.h>
#include <machdep.h>

#ifdef	__cplusplus
extern "C" {
#endif

/*
 * Brand helper libraries must name their ops vector using this macro.
 */
#ifdef _LP64
#ifdef _ELF64
#define	RTLD_DB_BRAND_OPS rtld_db_brand_ops64
#else /* !_ELF64 */
#define	RTLD_DB_BRAND_OPS rtld_db_brand_ops32
#endif /* !_ELF64 */
#else /* !_LP64 */
#define	RTLD_DB_BRAND_OPS rtld_db_brand_ops32
#endif /* !_LP64 */

/*
 * State kept for brand helper libraries
 *
 * All librtld_db brand plugin libraries need to specify a Lmid_t value
 * that controls how link map ids are assigned to native solaris objects
 * (as pointed to by the processes aux vectors) which are enumerated by
 * librtld_db.  In most cases this value will either be LM_ID_NONE or
 * LM_ID_BRAND.
 *
 * If LM_ID_NONE is specified in the structure below, then when native solaris
 * objects are enumerated by librtld_db, their link map id values will match
 * the link map ids assigned to those objects by the solaris linker within
 * the target process.
 *
 * If LM_ID_BRAND is specified in the structure below, then when native solaris
 * objects are enumerated by librtld_db, their link map id value will be
 * explicity set to LM_ID_BRAND, regardless of the link map ids assigned to
 * those objects by the solaris linker within the target process.
 *
 * In all cases the librtld_db brand plugin library can report any link
 * map id value that it wants for objects that it enumerates via it's
 * rho_loadobj_iter() entry point.
 */
typedef struct __rd_helper_data	*rd_helper_data_t;
typedef struct rd_helper_ops {
	Lmid_t			rho_lmid;
	rd_helper_data_t	(*rho_init)(rd_agent_t *,
				    struct ps_prochandle *);
	void			(*rho_fini)(rd_helper_data_t);
	int			(*rho_loadobj_iter)(rd_helper_data_t,
				    rl_iter_f *, void *);
	rd_err_e		(*rho_get_dyns)(rd_helper_data_t,
				    psaddr_t, void **, size_t *);
} rd_helper_ops_t;

typedef struct rd_helper {
	void			*rh_dlhandle;
	rd_helper_ops_t		*rh_ops;
	rd_helper_data_t	rh_data;
} rd_helper_t;

struct rd_agent {
	mutex_t				rd_mutex;
	struct ps_prochandle		*rd_psp;	/* prochandle pointer */
	psaddr_t			rd_rdebug;	/* rtld r_debug */
	psaddr_t			rd_preinit;	/* rtld_db_preinit */
	psaddr_t			rd_postinit;	/* rtld_db_postinit */
	psaddr_t			rd_dlact;	/* rtld_db_dlact */
	psaddr_t			rd_tbinder;	/* tail of binder */
	psaddr_t			rd_rtlddbpriv;	/* rtld rtld_db_priv */
	ulong_t				rd_flags;	/* flags */
	ulong_t				rd_rdebugvers;	/* rtld_db_priv.vers */
	int				rd_dmodel;	/* data model */
	rd_helper_t			rd_helper;	/* private to helper */
};

/*
 * Values for rd_flags
 */
#define	RDF_FL_COREFILE		0x0001		/* client is core file image */



#define	RDAGLOCK(x)	(void) mutex_lock(&(x->rd_mutex));
#define	RDAGUNLOCK(x)	(void) mutex_unlock(&(x->rd_mutex));
#define	LOG(func)	{						\
				(void) mutex_lock(&glob_mutex);		\
				if (rtld_db_logging)			\
					func;				\
				(void) mutex_unlock(&glob_mutex);	\
			}

extern mutex_t		glob_mutex;
extern int		rtld_db_version;
extern int		rtld_db_logging;

extern rd_err_e		rd_binder_exit_addr(struct rd_agent *, const char *,
				psaddr_t *);

extern rd_err_e		_rd_event_enable32(rd_agent_t *, int);
extern rd_err_e		_rd_event_getmsg32(rd_agent_t *, rd_event_msg_t *);
extern rd_err_e		_rd_get_dyns32(struct rd_agent *,
			    psaddr_t, Dyn **, size_t *);
extern rd_err_e		_rd_get_ehdr32(struct rd_agent *,
			    psaddr_t, Ehdr *, uint_t *);
extern rd_err_e		_rd_objpad_enable32(struct rd_agent *, size_t);
extern rd_err_e		_rd_loadobj_iter32(rd_agent_t *, rl_iter_f *, void *);
extern rd_err_e		_rd_reset32(struct rd_agent *);
extern rd_err_e		find_dynamic_ent32(struct rd_agent *, psaddr_t,
			    Xword, Dyn *);
extern rd_err_e		validate_rdebug32(struct rd_agent *rap);
#ifdef _LP64
extern rd_err_e		_rd_event_enable64(rd_agent_t *, int);
extern rd_err_e		_rd_event_getmsg64(rd_agent_t *, rd_event_msg_t *);
extern rd_err_e		_rd_get_dyns64(struct rd_agent *,
			    psaddr_t, Elf64_Dyn **, size_t *);
extern rd_err_e		_rd_get_ehdr64(struct rd_agent *,
			    psaddr_t, Elf64_Ehdr *, uint_t *);
extern rd_err_e		_rd_objpad_enable64(struct rd_agent *, size_t);
extern rd_err_e		_rd_loadobj_iter64(rd_agent_t *, rl_iter_f *, void *);
extern rd_err_e		_rd_reset64(struct rd_agent *);
extern rd_err_e		find_dynamic_ent64(struct rd_agent *, psaddr_t,
			    Xword, Elf64_Dyn *);
extern rd_err_e		plt64_resolution(rd_agent_t *, psaddr_t, lwpid_t,
			    psaddr_t, rd_plt_info_t *);
extern rd_err_e		validate_rdebug64(struct rd_agent *rap);
#endif

#ifdef	__cplusplus
}
#endif

#endif /* __RTLD_DB_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 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"%Z%%M%	%I%	%E% SMI"

# Message file for cmd/sgs/librtld_db - intel specific.

@ _END_
#
# 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 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# Message file for cmd/sgs/librtld_db.

@ MSG_ID_LIBRTLD_DB

# There presently seem little point in internationalizing these messages
# as they provide debugging information for dbx developers and shouldn't
# be see by anyone else.

@ _END_

# Basic strings

@ MSG_DB_NOLDDATA	"rtld_db: rr: no LDDATA found in aux vector - \
			 falling back to symbol lookup"
@ MSG_DB_FLDDATA	"rtld_db: rl: found LDDATA auxv ld.so.1 data seg \
			 at: 0x%p"
@ MSG_DB_SYMRDEBUG	"rtld_db: rr: found ld.so.1::rdebug address: 0x%llx"
@ MSG_DB_LOOKFAIL	"rtld_db: rr: lookup of %s failed"
@ MSG_DB_NOAUXV		"rtld_db: rr: call to ps_pauxv() failed"
@ MSG_DB_DMLOOKFAIL	"rtld_db: rr: lookup of data model failed"
@ MSG_DB_NODYNAMIC	"rtld_db: rr: unable to find _DYNAMIC in exec"
@ MSG_DB_INITFAILED	"rtld_db: rr: can't find r_debug, rtld_db init failed"
@ MSG_DB_RESETFAIL	"rtld_db: rn: rd_reset failed"
@ MSG_DB_LKMAPFAIL	"rtld_db: im: failed in reading link_maps"
@ MSG_DB_CALLBACKR0	"rtld_db: im: itermap() callback returned 0 - \
			 terminating link-map traversal"
@ MSG_DB_ITERMAP	"rtld_db: im: itermap_cb(cb=0x%p, data=0x%p,\n\
			 rtld_db: im:      objbase=0x%llx, ident=0x%llx)"
@ MSG_DB_READDBGFAIL_1	"rtld_db: rli: failed to read rtld_db_priv: 0x%llx"
@ MSG_DB_READDBGFAIL_2	"rtld_db: reg: failed to read rdebug: 0x%llx"
@ MSG_DB_READDBGFAIL_3	"rtld_db: rli: failed to read rtd_dynlmlst: 0x%llx"
@ MSG_DB_READDBGFAIL_4	"rtld_db: rli: failed to read dynlm_list: 0x%llx"
@ MSG_DB_READDBGFAIL_5	"rtld_db: rli: failed to read dynlm_list->ap_data[]: \
			 0x%llx"
@ MSG_DB_READDBGFAIL_6	"rtld_db: rli: failed to read Lm_list: 0x%llx"
@ MSG_DB_READPRIVFAIL_1	"rtld_db: rr: fail to read rtld_db_priv: 0x%llx"
@ MSG_DB_BADPVERS	"rtld_db: ve: rtld vs. rtld_db version mismatch: \
			 %d != %d"
@ MSG_DB_VALIDRDEBUG	"rtld_db: ve: found valid r_debug structure. \
			 Addr: 0x%llx\n\
			 rtld_db: ve:	rtld_dbVers: %d rtldVers: %d \
			 Corefile: %d"
@ MSG_DB_LKMAPNOINIT	"rtld_db: rli: link maps are not yet initialized: \
			 rtd_dynlmlst: 0x%llx"
@ MSG_DB_LKMAPNOINIT_1	"rtld_db: rli: dynlm_list is not yet initialized: \
			 dynlm_list: 0x%llx"
@ MSG_DB_NULLITER	"rtld_db: rli: called with null iterator"
@ MSG_DB_UNEXPEVENT	"rtld_db: rea: unexpected event num: %d"
@ MSG_DB_READFAIL_1	"rtld_db: vr: read of 0x%llx failed"
@ MSG_DB_READFAIL_2	"rtld_db: rpr: read of 0x%llx failed"
@ MSG_DB_READFAIL_3	"rtld_db: roe: read of 0x%llx failed"
@ MSG_DB_READFAIL_4	"rtld_db: fde: read of 0x%llx failed"
@ MSG_DB_READFAIL_5	"rtld_db: ge: read of 0x%llx failed"
@ MSG_DB_READFAIL_6	"rtld_db: gd: read of 0x%llx failed"
@ MSG_DB_WRITEFAIL_1	"rtld_db: ree: write of 0x%llx failed"
@ MSG_DB_WRITEFAIL_2	"rtld_db: roe: write of 0x%llx failed"
@ MSG_DB_UNFNDSYM	"rtld_db: rbea: unable to find sym: %s"
@ MSG_DB_NODYNDEBUG	"rtld_db: fde: no %lld found in .dynamic"
@ MSG_DB_FINDDYNAMIC	"rtld_db: fde: DYNAMIC entry found tag: %d found. \
			 val: 0x%llx"
@ MSG_DB_HELPER_PREFIX	"/usr/lib/brand"



# Diagnostic messages

@ MSG_DB_LOGENABLE	"rtld_db: logging enabled!"
@ MSG_DB_RDINIT		"rtld_db: rd_init(%d)"
@ MSG_DB_RDNEW		"rtld_db: rd_new(0x%p)"
@ MSG_DB_RDDELETE	"rtld_db: rd_delete(0x%p)"
@ MSG_DB_LOADOBJITER	"rtld_db: rd_loadobj_iter32(dmodel=%d, cb=0x%p, \
			 d=0x%p)"
@ MSG_DB_RDEVENTADDR	"rtld_db: rd_event_addr(event=%d, addr=0x%llx)"
@ MSG_DB_RDRESET	"rtld_db: rd_reset(dmodel=%d)"
@ MSG_DB_RDEVENTENABLE	"rtld_db: rd_event_enable(dmodel=%d, onoff=%d)"
@ MSG_DB_RDEVENTGETMSG	"rtld_db: rd_event_getmsg(dmodel=%d, type=%d, \
			 state=%d)"
@ MSG_DB_RDOBJPADE	"rtld_db: rd_objpad_enable(padsize=0x%llx)"
@ MSG_DB_64BIT_PREFIX	"64/"
@ MSG_DB_BRAND_HELPERPATH_PREFIX "%s/%s/%s/%s%s_librtld_db.so.1"
@ MSG_DB_BRAND_HELPERPATH "%s/%s/%s%s_librtld_db.so.1"
@ MSG_DB_HELPERNOOPS	"rtld_db: helper lib loaded but ops not preset"
@ MSG_DB_HELPERLOADED	"rtld_db: helper library loaded for brand \"%s\""
@ MSG_DB_HELPERLOADFAILED "rtld_db: couldn't load brand helper library %s"
@ MSG_DB_HELPERINITFAILED "rtld_db: brand helper library initialization failed"

@ MSG_ER_OK		"no error"
@ MSG_ER_ERR		"generic rtld_db.so error"
@ MSG_ER_DBERR		"debugger service failed"
@ MSG_ER_NOCAPAB	"capability not available"
@ MSG_ER_NODYNAM	"couldn't find '_DYNAMIC'"
@ MSG_ER_NOBASE		"couldn't find auxv tag 'AT_BASE'"
@ MSG_ER_NOMAPS		"link-maps are not initialized"
@ MSG_ER_DEFAULT	"unknown rtld_db.so error code"

@ MSG_SYM_DEBUG		"r_debug"
@ MSG_SYM_PREINIT	"rtld_db_preinit"
@ MSG_SYM_POSTINIT	"rtld_db_postinit"
@ MSG_SYM_DLACT		"rtld_db_dlactivity"
@ MSG_SYM_RTBIND	"elf_rtbndr"
@ MSG_SYM_DYNAMIC	"_DYNAMIC"
@ MSG_SYM_BRANDOPS_32	"rtld_db_brand_ops32"
@ MSG_SYM_BRANDOPS_64	"rtld_db_brand_ops64"
#
# 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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
#

#
# MAPFILE HEADER START
#
# WARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
#	usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#

$mapfile_version 2

SYMBOL_VERSION SUNW_1.1 {
    global:
	rd_delete;
	rd_errstr;
	rd_event_addr;
	rd_event_enable;
	rd_event_getmsg;
	rd_init;
	rd_ctl;
	rd_loadobj_iter;
	rd_log;
	rd_new;
	rd_objpad_enable;
	rd_plt_resolution;
	rd_reset;
    local:
	*;
};

SYMBOL_VERSION SUNWprivate_1.1 {
    global:
	_rd_get_dyns32;
	_rd_get_ehdr32;
	_rd_loadobj_iter32;
	rd_get_dyns;
	validate_rdebug32;

$if _ELF64
	_rd_get_dyns64;
	_rd_get_ehdr64;
	_rd_loadobj_iter64;
	validate_rdebug64;
$endif
};

#Externally defined symbols
SYMBOL_SCOPE {
    global:
	ps_pauxv		{ FLAGS = NODIRECT PARENT; };
	ps_pdmodel		{ FLAGS = NODIRECT PARENT; };
	ps_pglobal_lookup	{ FLAGS = NODIRECT PARENT; };
	ps_pglobal_sym		{ FLAGS = NODIRECT PARENT; };
	ps_plog		{ FLAGS = NODIRECT PARENT; };
	ps_pread		{ FLAGS = NODIRECT PARENT; };
	ps_pwrite		{ FLAGS = NODIRECT PARENT; };
};
/*
 * 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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include	<stdlib.h>
#include	<stdio.h>
#include	<proc_service.h>
#include	<link.h>
#include	<rtld_db.h>
#include	<rtld.h>
#include	<alist.h>
#include	<list.h>
#include	<_rtld_db.h>
#include	<msg.h>
#include	<limits.h>
#include	<string.h>
#include	<sys/param.h>

/*
 * 64-bit builds are going to compile this module twice, the
 * second time with _ELF64 defined.  These defines should make
 * all the necessary adjustments to the code.
 */
#ifdef _LP64
#ifdef _ELF64
#define	_rd_event_enable32	_rd_event_enable64
#define	_rd_event_getmsg32	_rd_event_getmsg64
#define	_rd_get_dyns32		_rd_get_dyns64
#define	_rd_get_ehdr32		_rd_get_ehdr64
#define	_rd_objpad_enable32	_rd_objpad_enable64
#define	_rd_loadobj_iter32	_rd_loadobj_iter64
#define	_rd_reset32		_rd_reset64
#define	find_dynamic_ent32	find_dynamic_ent64
#define	validate_rdebug32	validate_rdebug64
#define	TAPlist			APlist
#define	TLm_list		Lm_list
#define	TList			List
#define	TListnode		Listnode
#define	MSG_SYM_BRANDOPS	MSG_SYM_BRANDOPS_64
#else	/* ELF32 */
#define	Rt_map			Rt_map32
#define	Rtld_db_priv		Rtld_db_priv32
#define	TAPlist			APlist32
#define	TLm_list		Lm_list32
#define	TList			List32
#define	TListnode		Listnode32
#define	Lm_list			Lm_list32
#define	MSG_SYM_BRANDOPS	MSG_SYM_BRANDOPS_32
#endif	/* _ELF64 */
#else	/* _LP64 */
#define	TAPlist			APlist
#define	TLm_list		Lm_list
#define	TList			List
#define	TListnode		Listnode
#define	MSG_SYM_BRANDOPS	MSG_SYM_BRANDOPS_32
#endif	/* _LP64 */

/*
 * BrandZ added ps_pbrandname().  Many debuggers that link directly
 * against librtld_db.so may not implement this interface.  Hence
 * we won't call the function directly, instead we'll try to look it
 * up using the linker first and only invoke it if we find it.
 */
typedef ps_err_e (*ps_pbrandname_fp_t)(struct ps_prochandle *,
    char *, size_t);

rd_err_e
validate_rdebug32(struct rd_agent *rap)
{
	struct ps_prochandle	*php = rap->rd_psp;
	psaddr_t		db_privp;
	Rtld_db_priv		db_priv;

	if (rap->rd_rdebug == 0)
		return (RD_ERR);

	/*
	 * The rtld_db_priv structure contains both the traditional (exposed)
	 * r_debug structure as well as private data only available to
	 * this library.
	 */
	db_privp = rap->rd_rdebug;

	/*
	 * Verify that librtld_db & rtld are at the proper revision
	 * levels.
	 */
	if (ps_pread(php, db_privp, (char *)&db_priv,
	    sizeof (Rtld_db_priv)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_READPRIVFAIL_1),
		    EC_ADDR(db_privp)));
		return (RD_DBERR);
	}

	if ((db_priv.rtd_version < R_RTLDDB_VERSION1) ||
	    (db_priv.rtd_version > R_RTLDDB_VERSION)) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_BADPVERS),
		    db_priv.rtd_version, R_RTLDDB_VERSION));
		return (RD_NOCAPAB);
	}

	/*
	 * Is the image being examined from a core file or not.
	 * If it is a core file then the following write will fail.
	 */
	if (ps_pwrite(php, db_privp, (char *)&db_priv,
	    sizeof (Rtld_db_priv)) != PS_OK)
		rap->rd_flags |= RDF_FL_COREFILE;

	rap->rd_rdebugvers = db_priv.rtd_version;
	rap->rd_rtlddbpriv = db_privp;

	LOG(ps_plog(MSG_ORIG(MSG_DB_VALIDRDEBUG), EC_ADDR(rap->rd_rdebug),
	    R_RTLDDB_VERSION, rap->rd_rdebugvers,
	    rap->rd_flags & RDF_FL_COREFILE));
	return (RD_OK);
}


rd_err_e
find_dynamic_ent32(struct rd_agent *rap, psaddr_t dynaddr,
    Xword dyntag, Dyn *dyn)
{
	struct ps_prochandle	*php = rap->rd_psp;
	Dyn			d;

	d.d_tag = DT_NULL;
	do {
		if (ps_pread(php, dynaddr, (void *)(&d), sizeof (d)) !=
		    PS_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_4),
			    EC_ADDR(dynaddr)));
			return (RD_DBERR);
		}
		dynaddr += sizeof (d);
		if (d.d_tag == dyntag)
			break;
	} while (d.d_tag != DT_NULL);
	if (d.d_tag == dyntag) {
		*dyn = d;
		LOG(ps_plog(MSG_ORIG(MSG_DB_FINDDYNAMIC), EC_ADDR(dyntag),
		    EC_ADDR(d.d_un.d_val)));
		return (RD_OK);
	}
	LOG(ps_plog(MSG_ORIG(MSG_DB_NODYNDEBUG), EC_ADDR(dyntag)));
	return (RD_DBERR);
}

extern char rtld_db_helper_path[MAXPATHLEN];

rd_err_e
_rd_reset32(struct rd_agent *rap)
{
	psaddr_t		symaddr;
	struct ps_prochandle	*php = rap->rd_psp;
	const auxv_t		*auxvp = NULL;
	rd_err_e		rc = RD_OK;
	char			brandname[MAXPATHLEN];
	char			brandlib[MAXPATHLEN];
	ps_pbrandname_fp_t	ps_pbrandname;

	/*
	 * librtld_db attempts three different methods to find
	 * the r_debug structure which is required to
	 * initialize itself.  The methods are:
	 *	method1:
	 *		entirely independent of any text segment
	 *		and relies on the AT_SUN_LDDATA auxvector
	 *		to find the ld.so.1::rdebug structure.
	 *	method2:
	 *		lookup symbols in ld.so.1's symbol table
	 *		to find the r_debug symbol.
	 *	method3:
	 *		(old dbx method) dependent upon the
	 *		text segment/symbol table of the
	 *		executable and not ld.so.1.  We lookup the
	 *		_DYNAMIC symbol in the executable and look for
	 *		the DT_DEBUG entry in the .dynamic table.  This
	 *		points to rdebug.
	 *
	 * If none of that works - we fail.
	 */
	LOG(ps_plog(MSG_ORIG(MSG_DB_RDRESET), rap->rd_dmodel));
	/*
	 * Method1
	 *
	 * Scan the aux vector looking for AT_BASE & AT_SUN_LDDATA
	 */

	if (ps_pauxv(php, &auxvp) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_NOAUXV)));
		rc = RD_ERR;
	}

	rap->rd_rdebug = 0;

	if (auxvp != NULL) {
		rc = RD_ERR;
		while (auxvp->a_type != AT_NULL) {
			if (auxvp->a_type == AT_SUN_LDDATA) {
				/* LINTED */
				rap->rd_rdebug = (uintptr_t)auxvp->a_un.a_ptr;
				LOG(ps_plog(MSG_ORIG(MSG_DB_FLDDATA),
				    rap->rd_rdebug));
				rc = validate_rdebug32(rap);
				break;
			}
			auxvp++;
		}
	}

	/*
	 * method2 - look for r_rdebug symbol in ld.so.1
	 */
	if (rc != RD_OK) {
		/*
		 * If the AT_SUN_LDDATA auxv vector is not present
		 * fall back on doing a symlookup of
		 * the r_debug symbol.  This is for backward
		 * compatiblity with older OS's
		 */
		LOG(ps_plog(MSG_ORIG(MSG_DB_NOLDDATA)));
		if (ps_pglobal_lookup(php, PS_OBJ_LDSO, MSG_ORIG(MSG_SYM_DEBUG),
		    &symaddr) != PS_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_LOOKFAIL),
			    MSG_ORIG(MSG_SYM_DEBUG)));
			rc = RD_DBERR;
		} else {
			rap->rd_rdebug = symaddr;
			LOG(ps_plog(MSG_ORIG(MSG_DB_SYMRDEBUG),
			    EC_ADDR(symaddr)));
			rc = validate_rdebug32(rap);
		}
	}


	/*
	 * method3 - find DT_DEBUG in the executables .dynamic section.
	 */
	if (rc != RD_OK) {
		Dyn	dyn;
		if (ps_pglobal_lookup(php, PS_OBJ_EXEC,
		    MSG_ORIG(MSG_SYM_DYNAMIC), &symaddr) != PS_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_NODYNAMIC)));
			LOG(ps_plog(MSG_ORIG(MSG_DB_INITFAILED)));
			return (rc);
		}
		rc = find_dynamic_ent32(rap, symaddr, DT_DEBUG, &dyn);
		if (rc != RD_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_INITFAILED)));
			return (rc);
		}
		rap->rd_rdebug = dyn.d_un.d_ptr;
		rc = validate_rdebug32(rap);
		if (rc != RD_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_INITFAILED)));
			return (rc);
		}
	}

	/*
	 * If we are debugging a branded executable, load the appropriate
	 * helper library, and call its initialization routine.  Being unable
	 * to load the helper library is not a critical error.  (Hopefully
	 * we'll still be able to access some objects in the target.)
	 */
	ps_pbrandname = (ps_pbrandname_fp_t)dlsym(RTLD_PROBE, "ps_pbrandname");
	while ((ps_pbrandname != NULL) &&
	    (ps_pbrandname(php, brandname, MAXPATHLEN) == PS_OK)) {
		const char *isa = "";

#ifdef _LP64
		isa = MSG_ORIG(MSG_DB_64BIT_PREFIX);
#endif /* _LP64 */

		if (rtld_db_helper_path[0] != '\0')
			(void) snprintf(brandlib, MAXPATHLEN,
			    MSG_ORIG(MSG_DB_BRAND_HELPERPATH_PREFIX),
			    rtld_db_helper_path,
			    MSG_ORIG(MSG_DB_HELPER_PREFIX), brandname, isa,
			    brandname);
		else
			(void) snprintf(brandlib, MAXPATHLEN,
			    MSG_ORIG(MSG_DB_BRAND_HELPERPATH),
			    MSG_ORIG(MSG_DB_HELPER_PREFIX), brandname, isa,
			    brandname);

		rap->rd_helper.rh_dlhandle = dlopen(brandlib,
		    RTLD_LAZY | RTLD_LOCAL);
		if (rap->rd_helper.rh_dlhandle == NULL) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_HELPERLOADFAILED),
			    brandlib));
			break;
		}

		rap->rd_helper.rh_ops = dlsym(rap->rd_helper.rh_dlhandle,
		    MSG_ORIG(MSG_SYM_BRANDOPS));
		if (rap->rd_helper.rh_ops == NULL) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_HELPERNOOPS),
			    brandlib));
			(void) dlclose(rap->rd_helper.rh_dlhandle);
			rap->rd_helper.rh_dlhandle = NULL;
			break;
		}

		rap->rd_helper.rh_data = rap->rd_helper.rh_ops->rho_init(rap,
		    php);
		if (rap->rd_helper.rh_data == NULL) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_HELPERINITFAILED)));
			(void) dlclose(rap->rd_helper.rh_dlhandle);
			rap->rd_helper.rh_dlhandle = NULL;
			rap->rd_helper.rh_ops = NULL;
			break;
		}

		LOG(ps_plog(MSG_ORIG(MSG_DB_HELPERLOADED), brandname));
		break;

		/* NOTREACHED */
	}

	if ((rap->rd_flags & RDF_FL_COREFILE) == 0) {
		if (ps_pglobal_lookup(php, PS_OBJ_LDSO,
		    MSG_ORIG(MSG_SYM_PREINIT), &symaddr) != PS_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_LOOKFAIL),
			    MSG_ORIG(MSG_SYM_PREINIT)));
			return (RD_DBERR);
		}
		rap->rd_preinit = symaddr;

		if (ps_pglobal_lookup(php, PS_OBJ_LDSO,
		    MSG_ORIG(MSG_SYM_POSTINIT), &symaddr) != PS_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_LOOKFAIL),
			    MSG_ORIG(MSG_SYM_POSTINIT)));
			return (RD_DBERR);
		}
		rap->rd_postinit = symaddr;

		if (ps_pglobal_lookup(php, PS_OBJ_LDSO,
		    MSG_ORIG(MSG_SYM_DLACT), &symaddr) != PS_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_LOOKFAIL),
			    MSG_ORIG(MSG_SYM_DLACT)));
			return (RD_DBERR);
		}
		rap->rd_dlact = symaddr;
		rap->rd_tbinder = 0;
	}

	return (RD_OK);
}

rd_err_e
_rd_get_ehdr32(struct rd_agent *rap,
    psaddr_t addr, Ehdr *ehdr, uint_t *phnum)
{
	struct ps_prochandle	*php = rap->rd_psp;
	Shdr			shdr;

	if (ps_pread(php, addr, ehdr, sizeof (*ehdr)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_5), EC_ADDR(addr)));
		return (RD_ERR);
	}
	if (phnum == NULL)
		return (RD_OK);

	if (ehdr->e_phnum != PN_XNUM) {
		*phnum = ehdr->e_phnum;
		return (RD_OK);
	}

	/* deal with elf extended program headers */
	if ((ehdr->e_shoff == 0) || (ehdr->e_shentsize < sizeof (shdr)))
		return (RD_ERR);

	addr += ehdr->e_shoff;
	if (ps_pread(php, addr, &shdr, sizeof (shdr)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_5), EC_ADDR(addr)));
		return (RD_ERR);
	}

	if (shdr.sh_info == 0)
		return (RD_ERR);

	*phnum = shdr.sh_info;
	return (RD_OK);
}

rd_err_e
_rd_get_dyns32(rd_agent_t *rap, psaddr_t addr, Dyn **dynpp, size_t *dynpp_sz)
{
	struct ps_prochandle	*php = rap->rd_psp;
	rd_err_e		err;
	uint_t			phnum;
	Ehdr			ehdr;
	Phdr			phdr;
	Dyn			*dynp;
	int			i;

	/* We only need to muck with dyn elements for ET_DYN objects */
	if ((err = _rd_get_ehdr32(rap, addr, &ehdr, &phnum)) != RD_OK)
		return (err);

	for (i = 0; i < phnum; i++) {
		psaddr_t a = addr + ehdr.e_phoff + (i * ehdr.e_phentsize);
		if (ps_pread(php, a, &phdr, sizeof (phdr)) != PS_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_6), EC_ADDR(a)));
			return (RD_ERR);
		}
		if (phdr.p_type == PT_DYNAMIC)
			break;
	}
	if (i == phnum)
		return (RD_ERR);

	if ((dynp = malloc(phdr.p_filesz)) == NULL)
		return (RD_ERR);
	if (ehdr.e_type == ET_DYN)
		phdr.p_vaddr += addr;
	if (ps_pread(php, phdr.p_vaddr, dynp, phdr.p_filesz) != PS_OK) {
		free(dynp);
		LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_6),
		    EC_ADDR(phdr.p_vaddr)));
		return (RD_ERR);
	}

	*dynpp = dynp;
	if (dynpp_sz != NULL)
		*dynpp_sz = phdr.p_filesz;
	return (RD_OK);
}

rd_err_e
_rd_event_enable32(rd_agent_t *rap, int onoff)
{
	struct ps_prochandle	*php = rap->rd_psp;
	Rtld_db_priv		rdb;

	LOG(ps_plog(MSG_ORIG(MSG_DB_RDEVENTENABLE), rap->rd_dmodel, onoff));
	/*
	 * Tell the debugged process that debugging is occuring
	 * This will enable the storing of event messages so that
	 * the can be gathered by the debugger.
	 */
	if (ps_pread(php, rap->rd_rdebug, (char *)&rdb,
	    sizeof (Rtld_db_priv)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_1),
		    EC_ADDR((uintptr_t)&rdb)));
		return (RD_DBERR);
	}

	if (onoff)
		rdb.rtd_rdebug.r_flags |= RD_FL_DBG;
	else
		rdb.rtd_rdebug.r_flags &= ~RD_FL_DBG;

	if (ps_pwrite(php, rap->rd_rdebug, (char *)&rdb,
	    sizeof (Rtld_db_priv)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_WRITEFAIL_1),
		    EC_ADDR((uintptr_t)&rdb)));
		return (RD_DBERR);
	}

	return (RD_OK);
}


rd_err_e
_rd_event_getmsg32(rd_agent_t *rap, rd_event_msg_t *emsg)
{
	Rtld_db_priv	rdb;

	if (ps_pread(rap->rd_psp, rap->rd_rdebug, (char *)&rdb,
	    sizeof (Rtld_db_priv)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_READDBGFAIL_2),
		    EC_ADDR(rap->rd_rdebug)));
		return (RD_DBERR);
	}
	emsg->type = rdb.rtd_rdebug.r_rdevent;
	if (emsg->type == RD_DLACTIVITY) {
		switch (rdb.rtd_rdebug.r_state) {
			case RT_CONSISTENT:
				emsg->u.state = RD_CONSISTENT;
				break;
			case RT_ADD:
				emsg->u.state = RD_ADD;
				break;
			case RT_DELETE:
				emsg->u.state = RD_DELETE;
				break;
		}
	} else
		emsg->u.state = RD_NOSTATE;

	LOG(ps_plog(MSG_ORIG(MSG_DB_RDEVENTGETMSG), rap->rd_dmodel,
	    emsg->type, emsg->u.state));

	return (RD_OK);
}


rd_err_e
_rd_objpad_enable32(struct rd_agent *rap, size_t padsize)
{
	Rtld_db_priv		db_priv;
	struct ps_prochandle	*php = rap->rd_psp;

	LOG(ps_plog(MSG_ORIG(MSG_DB_RDOBJPADE), EC_ADDR(padsize)));

	if (ps_pread(php, rap->rd_rtlddbpriv, (char *)&db_priv,
	    sizeof (Rtld_db_priv)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_READFAIL_3),
		    EC_ADDR(rap->rd_rtlddbpriv)));
		return (RD_DBERR);
	}
#if	defined(_LP64) && !defined(_ELF64)
	/*LINTED*/
	db_priv.rtd_objpad = (uint32_t)padsize;
#else
	db_priv.rtd_objpad = padsize;
#endif
	if (ps_pwrite(php, rap->rd_rtlddbpriv, (char *)&db_priv,
	    sizeof (Rtld_db_priv)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_WRITEFAIL_2),
		    EC_ADDR(rap->rd_rtlddbpriv)));
		return (RD_DBERR);
	}
	return (RD_OK);
}

static rd_err_e
iter_map(rd_agent_t *rap, unsigned long ident, psaddr_t lmaddr,
    rl_iter_f *cb, void *client_data, uint_t *abort_iterp)
{
	while (lmaddr) {
		Rt_map		rmap;
		rd_loadobj_t	lobj;
		int		i;
		ulong_t		off;
		Ehdr		ehdr;
		Phdr		phdr;

		if (ps_pread(rap->rd_psp, lmaddr, (char *)&rmap,
		    sizeof (Rt_map)) != PS_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_LKMAPFAIL)));
			return (RD_DBERR);
		}

		/*
		 * As of 'VERSION5' we only report objects
		 * which have been fully relocated.  While the maps
		 * might be in a consistent state - if a object hasn't
		 * been relocated - it's not really ready for the debuggers
		 * to examine.  This is mostly due to the fact that we
		 * might still be mucking with the text-segment, if
		 * we are - we could conflict with any break-points
		 * the debuggers might have set.
		 */
		if (rap->rd_rdebugvers >= R_RTLDDB_VERSION5) {
			if ((FLAGS(&rmap) & FLG_RT_RELOCED) == 0) {
				lmaddr = (psaddr_t)NEXT(&rmap);
				continue;
			}
		}

		lobj.rl_base = (psaddr_t)ADDR(&rmap);
		lobj.rl_flags = 0;
		lobj.rl_refnameaddr = (psaddr_t)REFNAME(&rmap);
		if ((rap->rd_helper.rh_ops != NULL) &&
		    (rap->rd_helper.rh_ops->rho_lmid != LM_ID_NONE))
			lobj.rl_lmident =
			    rap->rd_helper.rh_ops->rho_lmid;
		else
			lobj.rl_lmident = ident;

		/*
		 * refnameaddr is only valid from a core file
		 * which is VERSION3 or greater.
		 */
		if (rap->rd_rdebugvers < R_RTLDDB_VERSION3) {
			lobj.rl_nameaddr = (psaddr_t)NAME(&rmap);
			lobj.rl_bend = 0;
			lobj.rl_padstart = 0;
			lobj.rl_padend = 0;
		} else {
			lobj.rl_nameaddr = (psaddr_t)PATHNAME(&rmap);
			lobj.rl_bend = ADDR(&rmap) + MSIZE(&rmap);
			lobj.rl_padstart = PADSTART(&rmap);
			lobj.rl_padend = PADSTART(&rmap) + PADIMLEN(&rmap);

		}

		if (rtld_db_version >= RD_VERSION2)
			if (FLAGS(&rmap) & FLG_RT_IMGALLOC)
				lobj.rl_flags |= RD_FLG_MEM_OBJECT;
		if (rtld_db_version >= RD_VERSION2) {
			lobj.rl_dynamic = (psaddr_t)DYN(&rmap);
		}

		if (rtld_db_version >= RD_VERSION4)
			lobj.rl_tlsmodid = TLSMODID(&rmap);

		/*
		 * Look for beginning of data segment.
		 *
		 * NOTE: the data segment can only be found for full
		 *	processes and not from core images.
		 */
		lobj.rl_data_base = 0;
		if (rap->rd_flags & RDF_FL_COREFILE)
			lobj.rl_data_base = 0;
		else {
			off = ADDR(&rmap);
			if (ps_pread(rap->rd_psp, off, (char *)&ehdr,
			    sizeof (Ehdr)) != PS_OK) {
				LOG(ps_plog(MSG_ORIG(MSG_DB_LKMAPFAIL)));
				return (RD_DBERR);
			}
			off += sizeof (Ehdr);
			for (i = 0; i < ehdr.e_phnum; i++) {
				if (ps_pread(rap->rd_psp, off, (char *)&phdr,
				    sizeof (Phdr)) != PS_OK) {
					LOG(ps_plog(MSG_ORIG(
					    MSG_DB_LKMAPFAIL)));
					return (RD_DBERR);
				}
				if ((phdr.p_type == PT_LOAD) &&
				    (phdr.p_flags & PF_W)) {
					lobj.rl_data_base = phdr.p_vaddr;
					if (ehdr.e_type == ET_DYN)
						lobj.rl_data_base +=
						    ADDR(&rmap);
					break;
				}
				off += ehdr.e_phentsize;
			}
		}

		/*
		 * When we transfer control to the client we free the
		 * lock and re-atain it after we've returned from the
		 * client.  This is to avoid any deadlock situations.
		 */
		LOG(ps_plog(MSG_ORIG(MSG_DB_ITERMAP), cb, client_data,
		    EC_ADDR(lobj.rl_base), EC_ADDR(lobj.rl_lmident)));
		RDAGUNLOCK(rap);
		if ((*cb)(&lobj, client_data) == 0) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_CALLBACKR0)));
			RDAGLOCK(rap);
			*abort_iterp = 1;
			break;
		}
		RDAGLOCK(rap);
		lmaddr = (psaddr_t)NEXT(&rmap);
	}
	return (RD_OK);
}


static rd_err_e
_rd_loadobj_iter32_native(rd_agent_t *rap, rl_iter_f *cb, void *client_data,
    uint_t *abort_iterp)
{
	Rtld_db_priv	db_priv;
	TAPlist		apl;
	uintptr_t	datap, nitems;
	Addr		addr;
	rd_err_e	rc;

	LOG(ps_plog(MSG_ORIG(MSG_DB_LOADOBJITER), rap->rd_dmodel, cb,
	    client_data));

	/*
	 * First, determine whether the link-map information has been
	 * established.  Some debuggers have made an initial call to this
	 * function with a null call back function (cb), but expect a
	 * RD_NOMAPS error return rather than a RD_ERR return when the
	 * link-maps aren't available.
	 */
	if (ps_pread(rap->rd_psp, rap->rd_rtlddbpriv, (char *)&db_priv,
	    sizeof (Rtld_db_priv)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_READDBGFAIL_1),
		    EC_ADDR(rap->rd_rtlddbpriv)));
		return (RD_DBERR);
	}

	if (db_priv.rtd_dynlmlst == 0) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_LKMAPNOINIT),
		    EC_ADDR((uintptr_t)db_priv.rtd_dynlmlst)));
		return (RD_NOMAPS);
	}

	if (ps_pread(rap->rd_psp, (psaddr_t)db_priv.rtd_dynlmlst, (char *)&addr,
	    sizeof (Addr)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_READDBGFAIL_3),
		    EC_ADDR((uintptr_t)db_priv.rtd_dynlmlst)));
		return (RD_DBERR);
	}

	if (addr == 0) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_LKMAPNOINIT_1),
		    EC_ADDR((uintptr_t)db_priv.rtd_dynlmlst)));
		return (RD_NOMAPS);
	}

	/*
	 * Having determined we have link-maps, ensure we have an iterator
	 * call back function.
	 */
	if (cb == NULL) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_NULLITER)));
		return (RD_ERR);
	}

	/*
	 * As of VERSION6, rtd_dynlmlst points to an APlist.  Prior to VERSION6
	 * rtd_dynlmlst pointed to a List.  But, there was a window where the
	 * version was not incremented, and this must be worked around by
	 * interpreting the APlist data.  Read the initial APlist information.
	 */
	if (ps_pread(rap->rd_psp, (psaddr_t)addr, (char *)&apl,
	    sizeof (TAPlist)) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_READDBGFAIL_4),
		    EC_ADDR((uintptr_t)addr)));
		return (RD_DBERR);
	}

	/*
	 * The rtd_dynlmlst change from a List to an APlist occurred under
	 * 6801536 in snv_112.  However, this change neglected to preserve
	 * backward compatibility by maintaining List processing and using a
	 * version increment to detect the change.  6862967, intergrated in
	 * snv_121 corrects the version detection.  However, to catch objects
	 * built between these releases, we look at the first element of the
	 * APlist.  apl_arritems indicates the number of APlist items that are
	 * available.  This was originally initialized with a AL_CNT_DYNLIST
	 * value of 2 (one entry for LM_ID_BASE and one entry for LM_ID_LDSO).
	 * It is possible that the use of an auditor results in an additional
	 * link-map list, in which case the original apl_arritems would have
	 * been doubled.
	 *
	 * Therefore, if the debugging verion is VERSION6, or the apl_arritems
	 * entry has a value less than or equal to 4 and the debugging version
	 * is VERSION5, then we process APlists.  Otherwise, fall back to List
	 * processing.
	 */
	if ((rap->rd_rdebugvers >= R_RTLDDB_VERSION6) ||
	    ((rap->rd_rdebugvers == R_RTLDDB_VERSION5) &&
	    (apl.apl_arritems <= 4))) {
		/*
		 * Iterate through each apl.ap_data[] entry.
		 */
		for (datap = (uintptr_t)((char *)(uintptr_t)addr +
		    ((size_t)(((TAPlist *)0)->apl_data))), nitems = 0;
		    nitems < apl.apl_nitems; nitems++, datap += sizeof (Addr)) {
			TLm_list	lm;
			ulong_t		ident;

			/*
			 * Obtain the Lm_list address for this apl.ap_data[]
			 * entry.
			 */
			if (ps_pread(rap->rd_psp, (psaddr_t)datap,
			    (char *)&addr, sizeof (Addr)) != PS_OK) {
				LOG(ps_plog(MSG_ORIG(MSG_DB_READDBGFAIL_5),
				    EC_ADDR(datap)));
				return (RD_DBERR);
			}

			/*
			 * Obtain the Lm_list data for this Lm_list address.
			 */
			if (ps_pread(rap->rd_psp, (psaddr_t)addr, (char *)&lm,
			    sizeof (TLm_list)) != PS_OK) {
				LOG(ps_plog(MSG_ORIG(MSG_DB_READDBGFAIL_6),
				    EC_ADDR((uintptr_t)addr)));
				return (RD_DBERR);
			}

			/*
			 * Determine IDENT of current LM_LIST
			 */
			if (lm.lm_flags & LML_FLG_BASELM)
				ident = LM_ID_BASE;
			else if (lm.lm_flags & LML_FLG_RTLDLM)
				ident = LM_ID_LDSO;
			else
				ident = (ulong_t)addr;

			if ((rc = iter_map(rap, ident, (psaddr_t)lm.lm_head,
			    cb, client_data, abort_iterp)) != RD_OK)
				return (rc);

			if (*abort_iterp != 0)
				break;
		}
	} else {
		TList		list;
		TListnode	lnode;
		Addr		lnp;

		/*
		 * Re-read the dynlmlst address to obtain a List structure.
		 */
		if (ps_pread(rap->rd_psp, (psaddr_t)db_priv.rtd_dynlmlst,
		    (char *)&list, sizeof (TList)) != PS_OK) {
			LOG(ps_plog(MSG_ORIG(MSG_DB_READDBGFAIL_3),
			    EC_ADDR((uintptr_t)db_priv.rtd_dynlmlst)));
			return (RD_DBERR);
		}

		/*
		 * Iterate through the link-map list.
		 */
		for (lnp = (Addr)list.head; lnp; lnp = (Addr)lnode.next) {
			Lm_list	lml;
			ulong_t	ident;

			/*
			 * Iterate through the List of Lm_list's.
			 */
			if (ps_pread(rap->rd_psp, (psaddr_t)lnp, (char *)&lnode,
			    sizeof (TListnode)) != PS_OK) {
				LOG(ps_plog(MSG_ORIG(MSG_DB_READDBGFAIL_4),
				    EC_ADDR(lnp)));
					return (RD_DBERR);
			}

			if (ps_pread(rap->rd_psp, (psaddr_t)lnode.data,
			    (char *)&lml, sizeof (Lm_list)) != PS_OK) {
				LOG(ps_plog(MSG_ORIG(MSG_DB_READDBGFAIL_5),
				    EC_ADDR((uintptr_t)lnode.data)));
					return (RD_DBERR);
			}

			/*
			 * Determine IDENT of current LM_LIST
			 */
			if (lml.lm_flags & LML_FLG_BASELM)
				ident = LM_ID_BASE;
			else if (lml.lm_flags & LML_FLG_RTLDLM)
				ident = LM_ID_LDSO;
			else
				ident = (unsigned long)lnode.data;

			if ((rc = iter_map(rap, ident, (psaddr_t)lml.lm_head,
			    cb, client_data, abort_iterp)) != RD_OK)
				return (rc);

			if (*abort_iterp != 0)
				break;
		}
	}

	return (rc);
}

rd_err_e
_rd_loadobj_iter32(rd_agent_t *rap, rl_iter_f *cb, void *client_data)
{
	rd_err_e	rc, rc_brand = RD_OK;
	uint_t		abort_iter = 0;

	/* First iterate over the native target objects */
	rc = _rd_loadobj_iter32_native(rap, cb, client_data, &abort_iter);
	if (abort_iter != 0)
		return (rc);

	/* Then iterate over any branded objects. */
	if ((rap->rd_helper.rh_ops != NULL) &&
	    (rap->rd_helper.rh_ops->rho_loadobj_iter != NULL))
		rc_brand = rap->rd_helper.rh_ops->rho_loadobj_iter(
		    rap->rd_helper.rh_data, cb, client_data);

	rc = (rc != RD_OK) ? rc : rc_brand;
	return (rc);
}
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c) 2018, Joyent, Inc.
 */

#include	<stdlib.h>
#include	<stdio.h>
#include	<string.h>
#include	<proc_service.h>
#include	<link.h>
#include	<rtld_db.h>
#include	<rtld.h>
#include	<_rtld_db.h>
#include	<msg.h>
#include	<sys/param.h>

/*
 * Mutex to protect global data
 */
mutex_t	glob_mutex = DEFAULTMUTEX;
int	rtld_db_version = RD_VERSION1;
int	rtld_db_logging = 0;
char	rtld_db_helper_path[MAXPATHLEN];


void
rd_log(const int on_off)
{
	(void) mutex_lock(&glob_mutex);
	rtld_db_logging = on_off;
	(void) mutex_unlock(&glob_mutex);
	LOG(ps_plog(MSG_ORIG(MSG_DB_LOGENABLE)));
}

/*
 * Versioning Notes.
 *
 * The following have been added as the versions of librtld_db
 * have grown:
 *
 *	RD_VERSION1:
 *		o baseline version
 *
 *	RD_VERSION2:
 *		o added support for the use of the AT_SUN_LDBASE auxvector
 *		  to find the initialial debugging (r_debug) structures
 *		  in ld.so.1
 *		o added the rl_dynamic field to rd_loadobj_t
 *		o added the RD_FLG_MEM_OBJECT to be used with the
 *		  rl_dynamic->rl_flags field.
 *
 *	RD_VERSION3:
 *		o added the following fields/flags to the rd_plt_info_t
 *		  type:
 *			pi_baddr	- bound address of PLT (if bound)
 *			pi_flags	- flag field
 *			RD_FLG_PI_PLTBOUND	(flag for pi_flags)
 *				if set - the PLT is bound and pi_baddr
 *				is filled in with the destination of the PLT.
 *
 *	RD_VERSION4:
 *		o added the following field to the rd_loadobj_t structure:
 *			rl_tlsmodid	- module ID for TLS references
 */
rd_err_e
rd_init(int version)
{
	if ((version < RD_VERSION1) ||
	    (version > RD_VERSION))
		return (RD_NOCAPAB);
	rtld_db_version = version;
	LOG(ps_plog(MSG_ORIG(MSG_DB_RDINIT), rtld_db_version));

	return (RD_OK);
}

rd_err_e
rd_ctl(int cmd, void *arg)
{
	if (cmd != RD_CTL_SET_HELPPATH || arg == NULL ||
	    strlen((char *)arg) >= MAXPATHLEN)
		return (RD_ERR);

	(void) strcpy(rtld_db_helper_path, (char *)arg);

	return (RD_OK);
}

rd_err_e
rd_get_dyns(rd_agent_t *rap, psaddr_t addr, void **dynpp, size_t *dynpp_sz)
{
	if (rap->rd_helper.rh_ops != NULL)
		return (rap->rd_helper.rh_ops->rho_get_dyns(
		    rap->rd_helper.rh_data, addr, dynpp, dynpp_sz));

#ifdef _LP64
	if (rap->rd_dmodel == PR_MODEL_LP64)
		return (_rd_get_dyns64(rap,
		    addr, (Elf64_Dyn **)dynpp, dynpp_sz));
	else
#endif
		return (_rd_get_dyns32(rap,
		    addr, (Dyn **)dynpp, dynpp_sz));
}

rd_err_e
rd_reset(struct rd_agent *rap)
{
	rd_err_e			err;

	RDAGLOCK(rap);

	rap->rd_flags = 0;

#ifdef _LP64
	/*
	 * Determine if client is 32-bit or 64-bit.
	 */
	if (ps_pdmodel(rap->rd_psp, &rap->rd_dmodel) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_DMLOOKFAIL)));
		RDAGUNLOCK(rap);
		return (RD_DBERR);
	}

	if (rap->rd_dmodel == PR_MODEL_LP64)
		err = _rd_reset64(rap);
	else
#endif
		err = _rd_reset32(rap);

	RDAGUNLOCK(rap);
	return (err);
}


rd_agent_t *
rd_new(struct ps_prochandle *php)
{
	rd_agent_t	*rap;

	LOG(ps_plog(MSG_ORIG(MSG_DB_RDNEW), php));
	if ((rap = (rd_agent_t *)calloc(1, sizeof (rd_agent_t))) == NULL)
		return (0);

	rap->rd_psp = php;
	(void) mutex_init(&rap->rd_mutex, USYNC_THREAD, 0);
	if (rd_reset(rap) != RD_OK) {
		if (rap->rd_helper.rh_dlhandle != NULL) {
			rap->rd_helper.rh_ops->rho_fini(rap->rd_helper.rh_data);
			(void) dlclose(rap->rd_helper.rh_dlhandle);
		}
		free(rap);
		LOG(ps_plog(MSG_ORIG(MSG_DB_RESETFAIL)));
		return ((rd_agent_t *)0);
	}

	return (rap);
}


void
rd_delete(rd_agent_t *rap)
{
	LOG(ps_plog(MSG_ORIG(MSG_DB_RDDELETE), rap));
	if (rap->rd_helper.rh_dlhandle != NULL) {
		rap->rd_helper.rh_ops->rho_fini(rap->rd_helper.rh_data);
		(void) dlclose(rap->rd_helper.rh_dlhandle);
	}
	free(rap);
}


rd_err_e
rd_loadobj_iter(rd_agent_t *rap, rl_iter_f *cb, void *client_data)
{
	rd_err_e	err;

	RDAGLOCK(rap);

#ifdef _LP64
	if (rap->rd_dmodel == PR_MODEL_LP64)
		err = _rd_loadobj_iter64(rap, cb, client_data);
	else
#endif
		err = _rd_loadobj_iter32(rap, cb, client_data);

	RDAGUNLOCK(rap);
	return (err);
}


rd_err_e
rd_plt_resolution(rd_agent_t *rap, psaddr_t pc, lwpid_t lwpid,
	psaddr_t pltbase, rd_plt_info_t *rpi)
{
	rd_err_e	err;
	RDAGLOCK(rap);
#ifdef	_LP64
	if (rap->rd_dmodel == PR_MODEL_LP64)
		err = plt64_resolution(rap, pc, lwpid, pltbase,
		    rpi);
	else
		err = RD_ERR;	/* Hammerhead: 64-bit only */
#else
		err = plt32_resolution(rap, pc, lwpid, pltbase,
		    rpi);
#endif
	RDAGUNLOCK(rap);
	return (err);
}

rd_err_e
rd_event_addr(rd_agent_t *rap, rd_event_e num, rd_notify_t *np)
{
	rd_err_e	rc = RD_OK;

	RDAGLOCK(rap);
	switch (num) {
	case RD_NONE:
		break;
	case RD_PREINIT:
		np->type = RD_NOTIFY_BPT;
		np->u.bptaddr = rap->rd_preinit;
		break;
	case RD_POSTINIT:
		np->type = RD_NOTIFY_BPT;
		np->u.bptaddr = rap->rd_postinit;
		break;
	case RD_DLACTIVITY:
		np->type = RD_NOTIFY_BPT;
		np->u.bptaddr = rap->rd_dlact;
		break;
	default:
		LOG(ps_plog(MSG_ORIG(MSG_DB_UNEXPEVENT), num));
		rc = RD_ERR;
		break;
	}
	if (rc == RD_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_RDEVENTADDR), num,
		    EC_ADDR(np->u.bptaddr)));
	}

	RDAGUNLOCK(rap);
	return (rc);
}


/* ARGSUSED 0 */
rd_err_e
rd_event_enable(rd_agent_t *rap, int onoff)
{
	rd_err_e	err;

	RDAGLOCK(rap);

#ifdef _LP64
	if (rap->rd_dmodel == PR_MODEL_LP64)
		err = _rd_event_enable64(rap, onoff);
	else
#endif
		err = _rd_event_enable32(rap, onoff);

	RDAGUNLOCK(rap);
	return (err);
}


rd_err_e
rd_event_getmsg(rd_agent_t *rap, rd_event_msg_t *emsg)
{
	rd_err_e	err;

	RDAGLOCK(rap);

#ifdef _LP64
	if (rap->rd_dmodel == PR_MODEL_LP64)
		err = _rd_event_getmsg64(rap, emsg);
	else
#endif
		err = _rd_event_getmsg32(rap, emsg);

	RDAGUNLOCK(rap);
	return (err);
}


rd_err_e
rd_binder_exit_addr(struct rd_agent *rap, const char *bname, psaddr_t *beaddr)
{
	ps_sym_t	sym;

	if (rap->rd_tbinder) {
		*beaddr = rap->rd_tbinder;
		return (RD_OK);
	}
	if (ps_pglobal_sym(rap->rd_psp, PS_OBJ_LDSO, bname, &sym) != PS_OK) {
		LOG(ps_plog(MSG_ORIG(MSG_DB_UNFNDSYM),
		    bname));
		return (RD_ERR);
	}

	rap->rd_tbinder = *beaddr = sym.st_value + sym.st_size - M_BIND_ADJ;

	return (RD_OK);
}


rd_err_e
rd_objpad_enable(struct rd_agent *rap, size_t padsize)
{
	rd_err_e	err;

	RDAGLOCK(rap);

#ifdef _LP64
	if (rap->rd_dmodel == PR_MODEL_LP64)
		err = _rd_objpad_enable64(rap, padsize);
	else
#endif
		err = _rd_objpad_enable32(rap, padsize);

	RDAGUNLOCK(rap);
	return (err);
}


char *
rd_errstr(rd_err_e rderr)
{
	/*
	 * Convert an 'rd_err_e' to a string
	 */
	switch (rderr) {
	case RD_OK:
		return ((char *)MSG_ORIG(MSG_ER_OK));
	case RD_ERR:
		return ((char *)MSG_ORIG(MSG_ER_ERR));
	case RD_DBERR:
		return ((char *)MSG_ORIG(MSG_ER_DBERR));
	case RD_NOCAPAB:
		return ((char *)MSG_ORIG(MSG_ER_NOCAPAB));
	case RD_NODYNAM:
		return ((char *)MSG_ORIG(MSG_ER_NODYNAM));
	case RD_NOBASE:
		return ((char *)MSG_ORIG(MSG_ER_NOBASE));
	case RD_NOMAPS:
		return ((char *)MSG_ORIG(MSG_ER_NOMAPS));
	default:
		return ((char *)MSG_ORIG(MSG_ER_DEFAULT));
	}
}