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 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
|
/*
* Oracle Linux DTrace.
* Copyright (c) 2009, 2026, Oracle and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <memory.h>
#include <errno.h>
#include <dirent.h>
#include <signal.h>
#include <limits.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/ptrace.h>
#include <port.h>
#include <setjmp.h>
#include <pthread.h>
#include <rtld_db.h>
#include "libproc.h"
#include "Pcontrol.h"
static map_info_t *object_to_map(struct ps_prochandle *, Lmid_t, const char *);
static map_info_t *object_name_to_map(struct ps_prochandle *,
Lmid_t, const char *);
static prmap_file_t *Pfilename_to_file_map(struct ps_prochandle *P,
const char *name);
static GElf_Sym *sym_by_name(sym_tbl_t *, const char *, GElf_Sym *, uint_t *);
static file_info_t *file_info_new(struct ps_prochandle *, map_info_t *);
static int byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname);
static void optimize_symtab(sym_tbl_t *);
static void Pbuild_file_symtab(struct ps_prochandle *, file_info_t *);
static map_info_t *Paddr2mptr(struct ps_prochandle *P, uintptr_t addr);
static int Pxlookup_by_name_internal(struct ps_prochandle *P, Lmid_t lmid,
const char *oname, const char *sname, int fixup_load_addr, GElf_Sym *symp,
prsyminfo_t *sip);
#define DATA_TYPES \
((1 << STT_OBJECT) | (1 << STT_FUNC) | \
(1 << STT_COMMON) | (1 << STT_TLS))
#define IS_DATA_TYPE(tp) (((1 << (tp)) & DATA_TYPES) != 0)
static char *
dezerostr(char *str)
{
size_t i;
if (str[0] != '0')
return str;
i = strspn(str, "0");
memmove(str, str + i, strlen(str) - i + 1);
return str;
}
static ulong_t
string_hash(const char *key)
{
ulong_t g, h = 0;
const char *p;
for (p = key; *p != '\0'; p++) {
h = (h << 4) + *p;
if ((g = (h & 0xf0000000)) != 0) {
h ^= (g >> 24);
h ^= g;
}
}
return h;
}
/*
* This is Pelle Evensen's NASAM mixer. Since we are only mixing in one value
* we don't need anything but a mixing step.
*/
#define ror(x,y) ((x) >> (y) | (x) << (64 - (y)))
static uint64_t
nasam_mix(uint64_t h)
{
h ^= ror(h, 25) ^ ror(h, 47);
h *= 0x9E6C63D0676A9A99UL;
h ^= h >> 23 ^ h >> 51;
h *= 0x9E6D62D06F6A9A9BUL;
h ^= h >> 23 ^ h >> 51;
return h;
}
#undef ror
static ino_t
dev_inum_hash(dev_t dev, ino_t inum)
{
uint64_t h;
h = nasam_mix(inum);
h += dev;
h = nasam_mix(h);
return (ino_t) h;
}
/*
* Allocation function for a new file_info_t.
*/
static file_info_t *
file_info_new(struct ps_prochandle *P, map_info_t *mptr)
{
file_info_t *fptr;
if ((fptr = calloc(1, sizeof(file_info_t))) == NULL)
return NULL;
dt_list_append(&P->file_list, fptr);
fptr->file_pname = strdup(mptr->map_pmap->pr_file->prf_mapname);
fptr->file_dev = mptr->map_pmap->pr_dev;
fptr->file_inum = mptr->map_pmap->pr_inum;
mptr->map_file = fptr;
fptr->file_map = -1;
fptr->file_ref = 1;
P->num_files++;
return fptr;
}
/*
* Deallocation function for a zero-refcount file_info_t.
*
* (Not the inverse of file_info_new(), hence the _del() name rather than
* _free().)
*/
static void
file_info_del(struct ps_prochandle *P, file_info_t *fptr)
{
_dprintf("%s: dropping file info with zero refcount\n",
fptr->file_pname);
dt_list_delete(&P->file_list, fptr);
free(fptr->file_symtab.sym_byname);
free(fptr->file_symtab.sym_byaddr);
free(fptr->file_dynsym.sym_byname);
free(fptr->file_dynsym.sym_byaddr);
if (fptr->file_lo)
free(fptr->file_lo->rl_scope);
free(fptr->file_lo);
free(fptr->file_lname);
free(fptr->file_pname);
free(fptr->file_symsearch);
elf_end(fptr->file_elf);
free(fptr);
P->num_files--;
}
/*
* Deallocate all file_info_t's with zero reference count.
*/
static void
file_info_purge(struct ps_prochandle *P)
{
uint_t i, num_files = P->num_files;
file_info_t *fptr;
file_info_t *old_fptr = NULL;
for (i = 0, fptr = dt_list_next(&P->file_list);
i < num_files; i++, old_fptr = fptr,
fptr = dt_list_next(fptr)) {
if (old_fptr && old_fptr->file_ref == 0)
file_info_del(P, old_fptr);
}
if (old_fptr && old_fptr->file_ref == 0)
file_info_del(P, old_fptr);
}
/*
* Deallocates all map_info_t, prmap_file_t, prmaps, and search paths from a
* prochandle. Does not free file_info_t's, except for their search path
* components; use file_info_purge() for that. Does not free the mapping array
* itself because it will often be reused immediately.
*/
static void
mapping_purge(struct ps_prochandle *P)
{
file_info_t *fptr;
size_t i;
for (i = 0; i < P->num_mappings; i++) {
if ((fptr = P->mappings[i].map_file) != NULL) {
fptr->file_ref--;
fptr->file_map = -1;
}
free(P->mappings[i].map_pmap->pr_mapaddrname);
free(P->mappings[i].map_pmap);
P->mappings[i].map_pmap = NULL;
}
P->num_mappings = 0;
for (i = 0; i < MAP_HASH_BUCKETS; i++) {
prmap_file_t *prf;
prmap_file_t *old_prf = NULL;
for (prf = P->map_files[i]; prf != NULL;
old_prf = prf, prf = prf->prf_name_next) {
free(old_prf);
free(prf->prf_mappings);
free(prf->prf_mapname);
}
free(old_prf);
}
memset(P->map_files, 0, sizeof(struct prmap_file *) * MAP_HASH_BUCKETS);
memset(P->map_inum, 0, sizeof(struct prmap_file *) * MAP_HASH_BUCKETS);
for (i = 0, fptr = dt_list_next(&P->file_list);
i < P->num_files; i++, fptr = dt_list_next(fptr)) {
if (fptr->file_symsearch) {
free(fptr->file_symsearch);
fptr->file_symsearch = NULL;
fptr->file_nsymsearch = 0;
}
}
P->map_exec = -1;
P->map_ldso = -1;
}
/*
* Called from Pcreate() and Pgrab() to initialize the symbol table state in the
* new ps_prochandle.
*/
int
Psym_init(struct ps_prochandle *P)
{
P->map_files = calloc(MAP_HASH_BUCKETS, sizeof(struct prmap_file_t *));
if (!P->map_files) {
_dprintf("Out of memory initializing map_files hash\n");
return -ENOMEM;
}
P->map_inum = calloc(MAP_HASH_BUCKETS, sizeof(struct prmap_file_t *));
if (!P->map_inum) {
_dprintf("Out of memory initializing map_inum hash\n");
return -ENOMEM;
}
return 0;
}
/*
* The opposite of Psym_init().
*/
void
Psym_free(struct ps_prochandle *P)
{
free(P->map_files);
P->map_files = NULL;
free(P->map_inum);
P->map_inum = NULL;
}
/*
* Call-back function for librtld_db to iterate through all of its shared
* libraries and determine their load object names and lmids.
*/
static int
map_iter(const rd_loadobj_t *lop, size_t num, void *prochandle)
{
char buf[PATH_MAX];
struct ps_prochandle *P = prochandle;
map_info_t *mptr;
file_info_t *fptr;
size_t scopes_size;
_dprintf("encountered rd object with dyn at %p\n", (void *)lop->rl_dyn);
/*
* The first mptr is the executable itself: the second is the vdso.
*/
if (num == 0) {
if (P->map_exec != -1)
mptr = &P->mappings[P->map_exec];
else {
_dprintf("map_iter: executable mapping, but not found "
"in /proc/%i/maps\n", P->pid);
return 1;
}
}
else if ((num == 1) && (lop->rl_nscopes == 1)) {
_dprintf("map_iter: skipping vdso\n");
return 1;
} else if (lop->rl_dyn == 0)
/*
* No dynamic section: this cannot be anything we are interested
* in.
*/
return 1;
else if ((mptr = Paddr2mptr(P, lop->rl_dyn)) == NULL) {
_dprintf("map_iter: base address of %lu doesn't match any mapping\n", lop->rl_dyn);
return 1;
}
if (mptr->map_file == NULL) {
_dprintf("map_iter: no file_info_t for this mapping\n");
return 1;
}
fptr = mptr->map_file;
/*
* Allocate a new file_lo, or free its dynamically allocated interior
* structure if already allocated.
*/
if (fptr->file_lo != NULL) {
free(fptr->file_lo->rl_scope);
} else if ((fptr->file_lo = malloc(sizeof(rd_loadobj_t))) == NULL) {
_dprintf("map_iter: failed to allocate rd_loadobj_t\n");
return 1;
}
*fptr->file_lo = *lop;
if (num == 0) {
/*
* The load object name is populated as an empty string by
* the C library, so we use the file name from the mapping.
*/
if (fptr->file_lname == NULL) {
fptr->file_lbase = NULL;
if ((fptr->file_lname =
strdup(fptr->file_pname)) != NULL)
fptr->file_lbase = basename(fptr->file_lname);
}
} else if (Pread_string(P, buf, sizeof(buf), lop->rl_nameaddr) >= 0) {
if ((fptr->file_lname == NULL) ||
(strcmp(fptr->file_lname, buf) != 0) ||
(buf[0] != '\0')) {
free(fptr->file_lname);
fptr->file_lbase = NULL;
if ((fptr->file_lname = strdup(buf)) != NULL)
fptr->file_lbase = basename(fptr->file_lname);
}
} else {
_dprintf("map_iter: failed to read string at %p\n",
(void *)lop->rl_nameaddr);
}
/*
* We must take a copy of rl_scope, since it is aggressively reused by
* rtld_db.
*/
scopes_size = lop->rl_nscopes * sizeof(uintptr_t);
fptr->file_lo->rl_scope = malloc(scopes_size);
if (!fptr->file_lo->rl_scope) {
_dprintf("map_iter: failed to allocate raw symbol search "
"path\n");
return 1;
}
memcpy(fptr->file_lo->rl_scope, lop->rl_scope, scopes_size);
_dprintf("loaded rd object %s lmid %lx\n",
fptr->file_lname ? fptr->file_lname : "<NULL>", lop->rl_lmident);
return 1;
}
/*
* Compute the file_symsearch for a given file_info.
*
* This is expensive to construct, and is only needed for a few file_info
* instances, so is lazily constructed here.
*/
static void
Pupdate_symsearch(struct ps_prochandle *P, struct file_info *fptr)
{
volatile rd_loadobj_t scope_lo = {0};
jmp_buf * volatile old_exec_jmp;
jmp_buf **jmp_pad, this_exec_jmp;
size_t i = 0;
if (fptr->file_symsearch != NULL ||
fptr->file_lo == NULL ||
P == NULL)
return;
fptr->file_symsearch = calloc(fptr->file_lo->rl_nscopes,
sizeof(struct file_info *));
/*
* Failure to allocate here is not as serious as it seems: symbol
* lookups will fall back to an inaccurate linear lookup (as inaccurate
* as GDB!).
*/
if (!fptr->file_symsearch) {
_dprintf("Cannot allocate %li bytes for symbol search "
"path for library with soname %s\n",
fptr->file_lo->rl_nscopes * sizeof(struct file_info *),
fptr->file_lbase);
return;
}
/*
* If we spot an exec() in this function, free the scopes array and
* rethrow (or just return, alternatively).
*/
jmp_pad = libproc_unwinder_pad(P);
old_exec_jmp = *jmp_pad;
if (setjmp(this_exec_jmp)) {
free(scope_lo.rl_scope);
if (old_exec_jmp)
longjmp(*old_exec_jmp, 1);
*jmp_pad = old_exec_jmp;
return;
}
*jmp_pad = &this_exec_jmp;
/*
* Simply skip scopes we can't read out. There's no point making a fuss
* if we can't a few of them, since the target process may have mutated
* them since we read them in, and aborting is excessive. This means
* we'll often allocate a little too much space. That's not a problem.
*/
for (i = 0; i < fptr->file_lo->rl_nscopes; i++) {
map_info_t *mptr;
if (rd_get_scope(P->rap, (rd_loadobj_t *)&scope_lo, fptr->file_lo,
i) == NULL) {
_dprintf("Cannot read scope %lu in symbol search path "
"for library with soname %s\n", i,
fptr->file_lbase);
continue;
}
/*
* In map_iter(), we have special cases for the vdso (skipping
* it) and for the executable, because we wanted to get info on
* it even if it had no dynamic section (and thus no rl_dyn).
* Here, we will simply skip any scope we cannot find, so a
* special case for the vdso is redundant; and a special case
* for the executable is pointless because if it has no dynamic
* section we cannot look up symbols in it anyway.
*
* TODO: if/when we get .ldynsym support, this needs rethinking,
* since that will not affect rl_dyn but will nonetheless
* permit symbol lookup in the executable.
*/
if (((mptr = Paddr2mptr(P, fptr->file_lo->rl_dyn)) == NULL) ||
mptr->map_file == NULL)
continue;
fptr->file_symsearch[fptr->file_nsymsearch++] = mptr->map_file;
}
free(scope_lo.rl_scope);
*jmp_pad = old_exec_jmp;
}
/*
* Go through all the address space mappings, validating or updating
* the information already gathered, or gathering new information.
*
* This function is only called when we suspect that the mappings have changed
* because of rtld activity, or when a process is referenced for the first time.
*/
void
Pupdate_maps(struct ps_prochandle *P)
{
char mapfile[PATH_MAX + MAXLEN_PID + strlen("/maps") + 1];
char exefilesym[PATH_MAX + MAXLEN_PID + strlen("/exe") + 1];
char exefile[PATH_MAX + 10] = ""; /* strlen(" (deleted)") */
FILE *fp;
size_t old_num_mappings = P->num_mappings;
size_t i = 0;
char *fn = NULL;
char *mapaddrname = NULL;
char *line = NULL;
size_t len;
if (P->info_valid)
return;
if (P->state == PS_DEAD)
return;
_dprintf("Updating mappings for PID %i\n", P->pid);
/*
* For now, just throw away and reconstruct all the mappings from
* scratch. This is theoretically inefficient, as a dlopen() or
* dlclose() will normally change only a few mappings: but it is much
* simpler to implement than the alternatives, and it ends up being
* cheaper than expected because we can avoid having to resort any of
* the mappings arrays (since /proc/$pid/maps is always sorted by
* address). We are left with allocator overhead and nothing else.
*
* Because it is much more expensive to recompute the file_info_t, we
* preserve them (with zero reference count) and reuse them where
* possible.
*/
mapping_purge(P);
snprintf(mapfile, sizeof(mapfile), "%s/%d/maps",
procfs_path, (int)P->pid);
if ((fp = fopen(mapfile, "r")) == NULL) {
Preset_maps(P);
return;
}
snprintf(exefilesym, sizeof(exefilesym), "%s/%d/exe", procfs_path,
(int)P->pid);
if ((len = readlink(exefilesym, exefile, sizeof(exefile))) > 0)
exefile[len] = '\0';
while (getline(&line, &len, fp) >= 0) {
unsigned long laddr, haddr, offset;
ino_t inode;
unsigned int major;
unsigned int minor;
char perms[5];
const char *first_space;
char *dash;
map_info_t *mptr;
prmap_file_t *prf;
prmap_t *pmptr;
struct prmap **new_prf_mappings;
/*
* gcc complains:
* warning: ISO C does not support the 'm' scanf flag
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
sscanf(line, "%lx-%lx %s %lx %x:%x %lu %ms",
&laddr, &haddr, perms, &offset, &major, &minor, &inode,
&fn);
#pragma GCC diagnostic pop
/*
* Skip anonymous mappings, and special mappings like the stack,
* heap, and vdso; also skip on OOM. Drop leading zeroes from
* the addresses as well (map_files entries lack them).
*/
first_space = strchr(line, ' ');
mapaddrname = strndup(line, first_space - line);
dezerostr(mapaddrname);
dash = strchr(mapaddrname, '-');
if (dash) {
dash++;
dezerostr(dash);
}
if ((fn == NULL) || (mapaddrname == NULL) || (fn[0] == '[')) {
free(fn);
free(mapaddrname);
continue;
}
/*
* Allocate a new map_info, and see if we need to allocate a new
* map_file. Expand the map_info region only if necessary.
* (This makes multiple sequential unmaps cheaper, though
* nothing we can do can make multiple sequential maps cheap.)
*/
if (P->num_mappings >= old_num_mappings) {
map_info_t *mappings = realloc(P->mappings,
sizeof(struct map_info) * (P->num_mappings + 1));
if (!mappings)
goto err;
P->mappings = mappings;
}
mptr = &P->mappings[P->num_mappings];
memset(mptr, 0, sizeof(struct map_info));
mptr->map_pmap = malloc(sizeof(struct prmap));
if (!mptr->map_pmap)
goto err;
pmptr = mptr->map_pmap;
memset(pmptr, 0, sizeof(struct prmap));
pmptr->pr_dev = makedev(major, minor);
pmptr->pr_inum = inode;
if ((prf = Pfilename_to_file_map(P, fn)) == NULL) {
uint_t fn_h = string_hash(fn) % MAP_HASH_BUCKETS;
uint_t inum_h = dev_inum_hash(pmptr->pr_dev, pmptr->pr_inum) %
MAP_HASH_BUCKETS;
if ((prf = malloc(sizeof(struct prmap_file))) == NULL) {
free(mptr->map_pmap);
goto err;
}
memset(prf, 0, sizeof(struct prmap_file));
prf->prf_mapname = fn;
prf->prf_name_next = P->map_files[fn_h];
P->map_files[fn_h] = prf;
prf->prf_inum_next = P->map_inum[inum_h];
P->map_inum[inum_h] = prf;
} else {
free(fn);
fn = NULL;
}
new_prf_mappings = realloc(prf->prf_mappings,
(prf->prf_num_mappings + 1) * sizeof(struct prmap_t *));
if (new_prf_mappings == NULL) {
free(mptr->map_pmap);
goto err;
}
prf->prf_mappings = new_prf_mappings;
prf->prf_mappings[prf->prf_num_mappings] = pmptr;
prf->prf_num_mappings++;
pmptr->pr_vaddr = laddr;
pmptr->pr_size = haddr - laddr;
pmptr->pr_mapaddrname = mapaddrname;
/*
* Note down the first mapping encountered: this is the base for
* segment/offset calculations. Also note the mapping that
* likely corresponds to the place code will be executed out of.
*/
if (prf->first_segment == NULL)
prf->first_segment = pmptr;
if (perms[0] == 'r')
pmptr->pr_mflags |= MA_READ;
if (perms[1] == 'w') {
pmptr->pr_mflags |= MA_WRITE;
}
if (perms[2] == 'x') {
char *basename = strrchr(prf->prf_mapname, '/');
char *suffix = strrchr(prf->prf_mapname, '.');
pmptr->pr_mflags |= MA_EXEC;
/*
* The primary text mapping must correspond to an
* on-disk mapping somewhere (since we cannot mmap()
* nor create a file_info for anonymous mappings).
* This is universally true in any case.
*/
if ((prf->prf_text_map == NULL) &&
(prf->prf_mapname[0] == '/'))
prf->prf_text_map = pmptr;
/*
* Heuristic to recognize the dynamic linker. Works
* for /lib, /lib64, and Debian multiarch as well as
* conventional /lib/ld-2.13.so style systems. (All
* versions of glibc 2.x name their dynamic linker
* something like ld-*.so.)
*
* If this heuristic fails, object_name_to_map() can use
* the AT_BASE auxv entry to come up with another guess
* (though this is likely to be stymied by dynamic
* linker relocation for non-statically-linked
* programs).
*/
if (basename && suffix && P->map_ldso == -1 &&
!P->no_dyn &&
(strncmp(prf->prf_mapname, "/lib", 4) == 0) &&
(strncmp(basename, "/ld-", 4) == 0) &&
(strcmp(suffix, ".so") == 0))
P->map_ldso = P->num_mappings;
/*
* Recognize the executable mapping.
*/
if (exefile[0] != '\0' && P->map_exec == -1 &&
(strcmp(prf->prf_mapname, exefile) == 0))
P->map_exec = P->num_mappings;
}
pmptr->pr_file = prf;
/*
* We try to merge any file information we may have for existing
* mappings, to avoid having to rebuild the file info.
*
* This is quite expensive if we have a lot of mappings, so we
* avoid doing it for those mappings that cannot possibly
* correspond to on-disk files. (It is still not guaranteed
* that all our file_info_t's correspond to ELF files.)
*/
if (prf->prf_mapname[0] == '/') {
file_info_t *fptr;
for (i = 0, fptr = dt_list_next(&P->file_list);
i < P->num_files; i++, fptr = dt_list_next(fptr)) {
if (fptr->file_dev == pmptr->pr_dev &&
fptr->file_inum == pmptr->pr_inum &&
(strcmp(fptr->file_pname,
prf->prf_mapname) == 0)) {
/*
* This mapping matches. Revive it.
*/
fptr->file_ref++;
mptr->map_file = fptr;
break;
}
}
if ((mptr->map_file == NULL) &&
(mptr->map_file = file_info_new(P, mptr)) == NULL) {
_dprintf("failed to allocate a new "
"file_info_t for %s\n", prf->prf_mapname);
/*
* Keep going: we can still work out other
* mappings.
*/
}
if (mptr->map_file &&
mptr->map_file->file_map == -1 &&
mptr->map_pmap->pr_file->prf_text_map == mptr->map_pmap) {
mptr->map_file->file_map = P->num_mappings;
if (mptr->map_file->file_etype == ET_DYN)
mptr->map_pmap->pr_mflags |= MA_PIC;
}
}
_dprintf("Added mapping for %s: %lx:%lx %lx(%lx)\n",
prf->prf_mapname, pmptr->pr_dev, pmptr->pr_inum,
pmptr->pr_vaddr, pmptr->pr_size);
P->num_mappings++;
}
/*
* Drop file_info_t's corresponding to closed mappings, which will still
* have a zero refcount.
*/
file_info_purge(P);
/*
* Note that we need to iterate across all mappings and recompute their
* lmids and sonames when we do a lookup that might depend on any such
* thing. (That is, pretty much all lookups other than PR_OBJ_LDSO.)
*
* We do this lazily not for efficiency's sake but because rtld_db
* itself does a name lookup of _rtld_global inside ld.so to determine
* whether link maps are consistent, and iteration across load objects
* requires link map consistency.
*/
P->info_valid = 1;
if (!P->no_dyn)
P->lmids_valid = 0;
fclose(fp);
free(line);
return;
err:
fclose(fp);
free(fn);
free(mapaddrname);
free(line);
Preset_maps(P);
return;
}
/*
* Iterate across all mappings and recompute their lmids and sonames.
* (Mappings which the dynamic linker does not know about, such as that
* for the dynamic linker itself, will be left in lmid 0 by default.
* This is almost certainly correct, since lmid use is vanishingly rare
* on Linux.)
*
* Don't do this if we know this is a statically linked binary, since
* they have no analogue of lmids at all. (If we are not sure, rd_new()
* will compute it. The rd_new() initialization process itself can call
* back into symbol lookup if this turns out to be a statically linked
* binary, to prevent unnecessary recursion.)
*/
static void
Pupdate_lmids(struct ps_prochandle *P)
{
if (P->info_valid && !P->noninvasive &&
!P->no_dyn && !P->lmids_valid) {
if (P->rap == NULL)
P->rap = rd_new(P);
if (P->rap != NULL)
rd_loadobj_iter(P->rap, map_iter, P);
P->lmids_valid = 1;
}
}
/*
* Update all of the mappings as if by Pupdate_maps(), and then forcibly cache
* all of the symbol tables associated with all object files.
*/
void
Pupdate_syms(struct ps_prochandle *P)
{
file_info_t *fptr;
int i;
if (P->state == PS_DEAD)
return;
P->info_valid = 0;
Pupdate_maps(P);
Pupdate_lmids(P);
/*
* The process might have died while updating maps or lmids, both of
* which can be quite time-consuming.
*/
if (P->state == PS_DEAD)
return;
for (i = 0, fptr = dt_list_next(&P->file_list);
i < P->num_files; i++, fptr = dt_list_next(fptr))
Pbuild_file_symtab(P, fptr);
}
/*
* Return the librtld_db agent handle for the victim process.
* The handle will become invalid at the next successful exec() and the
* client (caller of proc_rd_agent()) must not use it beyond that point.
* If the process is already dead or noninvasively grabbed, there's
* nothing we can do: just return the rd_agent if already available.
*/
rd_agent_t *
Prd_agent(struct ps_prochandle *P)
{
if (P->state == PS_DEAD)
return P->rap;
if (P->rap == NULL && !P->noninvasive) {
Pupdate_maps(P);
Pupdate_lmids(P);
}
return P->rap;
}
/*
* Return the prmap_t structure containing 'addr' (no restrictions on
* the type of mapping).
*/
const prmap_t *
Paddr_to_map(struct ps_prochandle *P, uintptr_t addr)
{
map_info_t *mptr;
if (P->state == PS_DEAD)
return NULL;
if (!P->info_valid) {
Pupdate_maps(P);
Pupdate_lmids(P);
}
if ((mptr = Paddr2mptr(P, addr)) != NULL)
return mptr->map_pmap;
return NULL;
}
/*
* Convert a full or partial load object name to the prmap_t for its
* corresponding primary text mapping.
*/
const prmap_t *
Plmid_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
{
map_info_t *mptr;
if (P->state == PS_DEAD)
return NULL;
if (name == PR_OBJ_EVERY)
return NULL; /* A reasonable mistake */
if ((mptr = object_name_to_map(P, lmid, name)) != NULL)
return mptr->map_pmap;
return NULL;
}
/*
* Given a process handle, find a corresponding mapping by hunting across all lmids.
*/
const prmap_t *
Pname_to_map(struct ps_prochandle *P, const char *name)
{
return Plmid_to_map(P, PR_LMID_EVERY, name);
}
/*
* Given a process handle, find a corresponding prmap_file by file name, or NULL
* if none.
*/
static prmap_file_t *Pfilename_to_file_map(struct ps_prochandle *P,
const char *name)
{
uint_t h = string_hash(name) % MAP_HASH_BUCKETS;
prmap_file_t *prf;
for (prf = P->map_files[h]; prf != NULL; prf = prf->prf_name_next)
if (strcmp(prf->prf_mapname, name) == 0)
return prf;
return NULL;
}
/*
* Given a process handle, find a corresponding prmap_file by (dev_t, inode
* number), or NULL if none.
*/
const prmap_file_t *
Pinode_to_file_map(struct ps_prochandle *P, dev_t dev, ino_t inum)
{
uint_t h = dev_inum_hash(dev, inum) % MAP_HASH_BUCKETS;
prmap_file_t *prf;
for (prf = P->map_inum[h]; prf != NULL; prf = prf->prf_inum_next)
if (prf->first_segment->pr_inum == inum &&
prf->first_segment->pr_dev == dev)
return prf;
return NULL;
}
/*
* Return the full path to a given mapping in /proc/$pid/map_files, as a new
* dynamically-allocated string, or NULL if none.
*/
char *
Pmap_mapfile_name(struct ps_prochandle *P, const prmap_t *mapp)
{
char *fn;
if (P->state == PS_DEAD)
return NULL;
Pupdate_maps(P);
if (mapp->pr_mapaddrname == NULL)
return NULL;
if (asprintf(&fn, "%s/%d/map_files/%s", procfs_path,
(int)P->pid, mapp->pr_mapaddrname) < 0)
return NULL;
return fn;
}
/*
* We wouldn't need these if qsort(3C) took an argument for the callback...
*/
static pthread_mutex_t sort_mtx = PTHREAD_MUTEX_INITIALIZER;
static char *sort_strs;
static GElf_Sym *sort_syms;
static int
byaddr_cmp_common(GElf_Sym *a, char *aname, GElf_Sym *b, char *bname)
{
if (a->st_value < b->st_value)
return -1;
if (a->st_value > b->st_value)
return 1;
/*
* Prefer the function to the non-function.
*/
if (GELF_ST_TYPE(a->st_info) != GELF_ST_TYPE(b->st_info)) {
if (GELF_ST_TYPE(a->st_info) == STT_FUNC)
return -1;
if (GELF_ST_TYPE(b->st_info) == STT_FUNC)
return 1;
}
/*
* Prefer the weak or strong global symbol to the local symbol.
*/
if (GELF_ST_BIND(a->st_info) != GELF_ST_BIND(b->st_info)) {
if (GELF_ST_BIND(b->st_info) == STB_LOCAL)
return -1;
if (GELF_ST_BIND(a->st_info) == STB_LOCAL)
return 1;
}
/*
* Prefer the symbol that doesn't begin with a '$' since compilers and
* other symbol generators often use it as a prefix.
*/
if (*bname == '$')
return -1;
if (*aname == '$')
return 1;
/*
* Prefer the name with fewer leading underscores.
*/
while (*aname == '_' && *bname == '_') {
aname++;
bname++;
}
if (*bname == '_')
return -1;
if (*aname == '_')
return 1;
/*
* Prefer the symbol with the smaller size.
*/
if (a->st_size < b->st_size)
return -1;
if (a->st_size > b->st_size)
return 1;
/*
* All other factors being equal, fall back to lexicographic order.
*/
return strcmp(aname, bname);
}
static int
byaddr_cmp(const void *aa, const void *bb)
{
GElf_Sym *a = &sort_syms[*(uint_t *)aa];
GElf_Sym *b = &sort_syms[*(uint_t *)bb];
char *aname = sort_strs + a->st_name;
char *bname = sort_strs + b->st_name;
return byaddr_cmp_common(a, aname, b, bname);
}
static int
byname_cmp(const void *aa, const void *bb)
{
GElf_Sym *a = &sort_syms[*(uint_t *)aa];
GElf_Sym *b = &sort_syms[*(uint_t *)bb];
char *aname = sort_strs + a->st_name;
char *bname = sort_strs + b->st_name;
return strcmp(aname, bname);
}
/*
* Given a symbol index, look up the corresponding symbol from the
* given symbol table.
*
* This function allows the caller to treat the symbol table as a single
* logical entity even though there may be 2 actual ELF symbol tables
* involved. See the comments in Pcontrol.h for details.
*/
static GElf_Sym *
symtab_getsym(sym_tbl_t *symtab, int ndx, GElf_Sym *dst)
{
/* If index is in range of primary symtab, look it up there */
if (ndx >= symtab->sym_symn_aux)
return gelf_getsym(symtab->sym_data_pri,
ndx - symtab->sym_symn_aux, dst);
/* Not in primary: Look it up in the auxiliary symtab */
return gelf_getsym(symtab->sym_data_aux, ndx, dst);
}
void __attribute__((__used__))
debug_dump_symtab(sym_tbl_t *symtab, const char *description)
{
uint_t i;
_dprintf("Symbol table dump of %s:\n", description);
for (i = 0; i < symtab->sym_symn; i++) {
GElf_Sym sym;
if (symtab_getsym(symtab, i, &sym) != NULL) {
_dprintf("%i %s %lx(%li), section %i\n", i,
sym.st_name < symtab->sym_strsz ?
symtab->sym_strs + sym.st_name :
"[unnamed]", sym.st_value, sym.st_size,
sym.st_shndx);
}
}
}
void __attribute__((__used__))
debug_dump_status(struct ps_prochandle *P)
{
char *status = Pget_proc_status(P->pid, "State");
if (status)
_dprintf("State:\t%s", status);
free(status);
}
static void
optimize_symtab(sym_tbl_t *symtab)
{
GElf_Sym *symp, *syms;
uint_t i, *indexa, *indexb;
size_t symn, strsz, count;
if (symtab == NULL || symtab->sym_data_pri == NULL ||
symtab->sym_byaddr != NULL)
return;
symn = symtab->sym_symn;
strsz = symtab->sym_strsz;
symp = syms = malloc(sizeof(GElf_Sym) * symn);
if (symp == NULL) {
_dprintf("optimize_symtab: failed to malloc symbol array");
return;
}
/*
* First record all the symbols into a table and count up the ones
* that we're interested in. We mark symbols as invalid by setting
* the st_name to an illegal value.
*/
for (i = 0, count = 0; i < symn; i++, symp++) {
if (symtab_getsym(symtab, i, symp) != NULL &&
symp->st_name < strsz &&
IS_DATA_TYPE(GELF_ST_TYPE(symp->st_info)))
count++;
else
symp->st_name = strsz;
}
/*
* Allocate sufficient space for both tables and populate them
* with the same symbols we just counted.
*/
symtab->sym_count = count;
indexa = symtab->sym_byaddr = calloc(sizeof(uint_t), count);
indexb = symtab->sym_byname = calloc(sizeof(uint_t), count);
if (indexa == NULL || indexb == NULL) {
_dprintf(
"optimize_symtab: failed to malloc symbol index arrays");
symtab->sym_count = 0;
if (indexa != NULL) { /* First alloc succeeded. Free it */
free(indexa);
symtab->sym_byaddr = NULL;
}
free(syms);
return;
}
for (i = 0, symp = syms; i < symn; i++, symp++) {
if (symp->st_name < strsz)
*indexa++ = *indexb++ = i;
}
/*
* Sort the two tables according to the appropriate criteria.
*/
pthread_mutex_lock(&sort_mtx);
sort_strs = symtab->sym_strs;
sort_syms = syms;
qsort(symtab->sym_byaddr, count, sizeof(uint_t), byaddr_cmp);
qsort(symtab->sym_byname, count, sizeof(uint_t), byname_cmp);
sort_strs = NULL;
sort_syms = NULL;
pthread_mutex_unlock(&sort_mtx);
free(syms);
}
/*
* Build the symbol table for the given mapped file.
*/
static void
Pbuild_file_symtab(struct ps_prochandle *P, file_info_t *fptr)
{
size_t i;
GElf_Ehdr ehdr;
volatile int fd = -1;
Elf_Data *shdata;
Elf_Scn *scn;
Elf *elf = NULL;
volatile Elf *velf = NULL;
size_t nshdrs, shstrndx;
int mapfilefd;
int err;
jmp_buf * volatile old_exec_jmp;
jmp_buf **jmp_pad, this_exec_jmp;
struct {
GElf_Shdr c_shdr;
Elf_Data *c_data;
const char *c_name;
} *cp, *cache = NULL;
if (!fptr) /* no file */
return;
if (fptr->file_init)
return; /* We've already processed this file */
/*
* Mark the file_info struct as having the symbol table initialized
* even if we fail below. We tried once; we don't try again.
*/
fptr->file_init = 1;
if (elf_version(EV_CURRENT) == EV_NONE) {
_dprintf("libproc ELF version is more recent than libelf\n");
return;
}
/*
* If we spot an exec() in this function, free everything and rethrow.
*/
jmp_pad = libproc_unwinder_pad(P);
old_exec_jmp = *jmp_pad;
if (setjmp(this_exec_jmp)) {
if (fd > -1)
close(fd);
if (velf)
elf_end((Elf *)velf);
fptr->file_dyn_base = 0;
free(cache);
fptr->file_elf = NULL;
if (old_exec_jmp)
longjmp(*old_exec_jmp, 1);
*jmp_pad = old_exec_jmp;
return;
}
*jmp_pad = &this_exec_jmp;
if (P->state == PS_DEAD) {
/*
* If we're not live, there is nothing we can do. (This should
* never happen in DTrace anyway.)
*/
_dprintf("cannot work over dead process\n");
goto bad;
}
if (fptr->file_map == -1) {
/*
* No primary text mapping for this file? OK, we can't use it
* for symbol lookup yet.
*/
_dprintf("no primary text mapping for %s yet\n", fptr->file_pname);
goto bad;
}
/*
* Acquire an fd to this mapping. This may require ptrace()ing, but
* first, try to use the upstream /proc/$pid/map_files interface.
* Then create the elf file and get the elf header and .shstrtab data
* buffer so we can process sections by name. If we're 'grabbing'
* without ptracing, without a monitor thread, and without map_files
* support, try to open the ELF object directly, though this is less
* reliable and can fail in the presence of deleted files and running
* executables for which we don't have read permission. If we can't
* even do that, there is no way for us to read any kind of symbol table
* out of this executable.
*
* Note: This Ptrace() call may trigger breakpoint handlers, which can
* look up addresses, which can call this function: so temporarily mark
* this file as not initialized, in case of such a recursive call, and
* drop out immediately afterwards if it is marked as done now. The
* same is true of the Puntrace(). There is no danger of infinite
* recursion because once a breakpoint is being processed, the victim is
* no longer running and thus cannot trigger another breakpoint.
*/
if ((mapfilefd = Pmapfilefd(P)) > -1)
fd = openat(mapfilefd,
P->mappings[fptr->file_map].map_pmap->pr_mapaddrname,
O_RDONLY);
if ((!P->noninvasive) && (fd < 0)) {
long pfd;
fptr->file_init = 0;
err = Ptrace(P, 1);
if (fptr->file_init == 1) {
if (err >= 0)
Puntrace(P, 0);
goto ret;
}
fptr->file_init = 1;
/*
* Even this can fail: fd table overflow, for instance.
* Fall back, if so.
*/
if (err >= 0) {
if (wrapped_ptrace(P, PTRACE_GETMAPFD, P->pid,
P->mappings[fptr->file_map].map_pmap->pr_vaddr,
&pfd) < 0)
fd = -1;
else
fd = pfd;
}
fptr->file_init = 0;
Puntrace(P, 0);
if (fptr->file_init == 1) {
close(fd);
goto ret;
}
fptr->file_init = 1;
}
if (fd < 0) {
struct stat s;
if ((stat(fptr->file_pname, &s) < 0) ||
s.st_dev != fptr->file_dev ||
s.st_ino != fptr->file_inum ||
((fd = open(fptr->file_pname, O_RDONLY)) < 0)) {
_dprintf("%i: cannot open %s in non-ptrace()d "
"process: replaced file or open() error\n",
P->pid, fptr->file_pname);
goto bad;
}
}
/*
* Don't hold the fd open forever. (ELF_C_READ followed by
* elf_cntl(..., ELF_C_FDREAD) triggers assertion failures in elfutils
* at gelf_getshdr() time: ELF_C_READ_MMAP works around this.)
*/
if ((elf = elf_begin(fd, ELF_C_READ_MMAP, NULL)) == NULL ||
elf_cntl(elf, ELF_C_FDREAD) == -1 ||
elf_kind(elf) != ELF_K_ELF ||
gelf_getehdr(elf, &ehdr) == NULL ||
elf_getshdrnum(elf, &nshdrs) == -1 ||
elf_getshdrstrndx(elf, &shstrndx) == -1 ||
(scn = elf_getscn(elf, shstrndx)) == NULL ||
(shdata = elf_getdata(scn, NULL)) == NULL) {
err = elf_errno();
close(fd);
_dprintf("failed to process ELF file %s: %s\n",
fptr->file_pname, (err == 0) ? "<null>" : elf_errmsg(err));
goto bad;
}
velf = elf;
close(fd);
if ((cache = malloc(nshdrs * sizeof(*cache))) == NULL) {
_dprintf("failed to malloc section cache for mapping of %s\n",
fptr->file_pname);
goto bad;
}
_dprintf("processing ELF file %s\n", fptr->file_pname);
fptr->file_etype = ehdr.e_type;
fptr->file_elf = elf;
fptr->file_shstrs = shdata->d_buf;
fptr->file_shstrsz = shdata->d_size;
/*
* Iterate through each section, caching its section header, data
* pointer, and name. We use this for handling sh_link values below.
*/
for (cp = cache + 1, scn = NULL; (scn = elf_nextscn(elf, scn)) != NULL;
cp++) {
if (gelf_getshdr(scn, &cp->c_shdr) == NULL) {
_dprintf("Pbuild_file_symtab: Failed to get section "
"header\n");
goto bad; /* Failed to get section header */
}
if ((cp->c_data = elf_getdata(scn, NULL)) == NULL) {
_dprintf("Pbuild_file_symtab: Failed to get section "
"data\n");
goto bad; /* Failed to get section data */
}
if (cp->c_shdr.sh_name >= shdata->d_size) {
_dprintf("Pbuild_file_symtab: corrupt section name");
goto bad; /* Corrupt section name */
}
cp->c_name = (const char *)shdata->d_buf + cp->c_shdr.sh_name;
}
/*
* Now iterate through the section cache in order to locate info
* for the .symtab, .dynsym and .SUNW_ldynsym sections.
*/
for (i = 1, cp = cache + 1; i < nshdrs; i++, cp++) {
GElf_Shdr *shp = &cp->c_shdr;
if (shp->sh_type == SHT_SYMTAB || shp->sh_type == SHT_DYNSYM) {
sym_tbl_t *symp = shp->sh_type == SHT_SYMTAB ?
&fptr->file_symtab : &fptr->file_dynsym;
/*
* It's possible that the we already got the symbol
* table from the core file itself. We'll just be
* replacing the symbol table we pulled out of the core
* file with an equivalent one. In either case, this
* check isn't essential, but it's a good idea.
*/
if (symp->sym_data_pri == NULL) {
_dprintf("Symbol table found for %s\n",
fptr->file_pname);
symp->sym_data_pri = cp->c_data;
symp->sym_symn +=
shp->sh_size / shp->sh_entsize;
symp->sym_strs =
cache[shp->sh_link].c_data->d_buf;
symp->sym_strsz =
cache[shp->sh_link].c_data->d_size;
symp->sym_hdr_pri = cp->c_shdr;
symp->sym_strhdr = cache[shp->sh_link].c_shdr;
} else {
_dprintf("Symbol table already there for %s\n",
fptr->file_pname);
}
#ifdef LATER
} else if (shp->sh_type == SHT_SUNW_LDYNSYM) {
/* .SUNW_ldynsym section is auxiliary to .dynsym */
if (fptr->file_dynsym.sym_data_aux == NULL) {
_dprintf(".SUNW_ldynsym symbol table"
" found for %s\n",
fptr->file_pname);
fptr->file_dynsym.sym_data_aux = cp->c_data;
fptr->file_dynsym.sym_symn_aux =
shp->sh_size / shp->sh_entsize;
fptr->file_dynsym.sym_symn +=
fptr->file_dynsym.sym_symn_aux;
fptr->file_dynsym.sym_hdr_aux = cp->c_shdr;
} else {
_dprintf(".SUNW_ldynsym symbol table already"
" there for %s\n", fptr->file_pname);
}
#endif
}
}
/*
* At this point, we've found all the symbol tables we're ever going
* to find: the ones in the loop above and possibly the symtab that
* was included in the core file. Before we perform any lookups, we
* create sorted versions to optimize for lookups.
*/
optimize_symtab(&fptr->file_symtab);
optimize_symtab(&fptr->file_dynsym);
free(cache);
/*
* Fill in the base address of the text mapping and entry point address
* for relocatable objects.
*
* If the object is a shared library or other dynamic object, set
* file_dyn_base to the dynamic linker's idea of its load bias, rather
* than engaging in a tiresome search through the object file. ld.so
* itself never gets this treatment, since it relocates itself without
* setting its rl_diff_addr: we must always compute its base address the
* tiresome way.
*/
if (fptr->file_etype == ET_DYN &&
fptr->file_lo != NULL &&
fptr->file_map != P->map_ldso) {
fptr->file_dyn_base = fptr->file_lo->rl_diff_addr;
_dprintf("%s: ld.so says file_dyn_base is %lx\n",
fptr->file_pname, fptr->file_dyn_base);
goto ret;
}
/*
* ET_EXEC executables have a load bias of zero.
*/
if (fptr->file_etype == ET_EXEC) {
fptr->file_dyn_base = 0;
goto ret;
}
size_t nphdrs;
GElf_Phdr hdr;
GElf_Phdr *phdr = &hdr;
prmap_file_t *prf = Pfilename_to_file_map(P, fptr->file_pname);
if (!prf) {
_dprintf("%s: mapping not known (internal error)\n.",
fptr->file_pname);
goto elf_bad_noaddr;
}
if (elf_getphdrnum(elf, &nphdrs) < 0) {
_dprintf("%s: can't get phdrs.\n", fptr->file_pname);
goto elf_bad_noaddr;
}
if (nphdrs == 0) {
_dprintf("%s: no loadable sections.\n", fptr->file_pname);
goto elf_bad_noaddr;
}
for (i = 0; i < nphdrs; i++) {
phdr = gelf_getphdr(elf, i, &hdr);
if (!phdr)
goto elf_bad_noaddr;
if (phdr->p_type == PT_LOAD)
break;
}
fptr->file_dyn_base = prf->first_segment->pr_vaddr -
(phdr->p_vaddr & (phdr->p_align - 1));
_dprintf("setting file_dyn_base for %s to %lx, "
"from load address of %lx and phdr vaddr of %lx\n",
fptr->file_pname, (long)fptr->file_dyn_base,
prf->first_segment->pr_vaddr,
(phdr->p_vaddr & (phdr->p_align - 1)));
ret:
*jmp_pad = old_exec_jmp;
return;
elf_bad_noaddr:
/*
* Can't reliably derive the base address. Symbol lookup is quite
* unlikely to work, but we can still try with a zero base address.
*/
err = elf_errno();
_dprintf("failed to get base address for ELF file %s, symbol lookup "
"likely broken: %s\n", fptr->file_pname, (err == 0) ? "<null>" :
elf_errmsg(err));
fptr->file_dyn_base = 0;
*jmp_pad = old_exec_jmp;
return;
bad:
free(cache);
elf_end(elf);
fptr->file_elf = NULL;
*jmp_pad = old_exec_jmp;
}
/*
* Given a process virtual address, return the index of the map_info_t
* containing it. If none found, return -1.
*/
static ssize_t
Paddr2idx(struct ps_prochandle *P, uintptr_t addr)
{
int lo = 0;
int hi = P->num_mappings - 1;
int mid;
map_info_t *mp;
while (lo <= hi) {
mid = (lo + hi) / 2;
mp = &P->mappings[mid];
/* check that addr is in [vaddr, vaddr + size) */
if ((addr - mp->map_pmap->pr_vaddr) < mp->map_pmap->pr_size)
return mid;
if (addr < mp->map_pmap->pr_vaddr)
hi = mid - 1;
else
lo = mid + 1;
}
return -1;
}
/*
* Given a process virtual address, return the map_info_t containing it.
* If none found, return NULL.
*
* These addresses are not stable across calls to Pupdate_maps()!
*/
static map_info_t *
Paddr2mptr(struct ps_prochandle *P, uintptr_t addr)
{
ssize_t mpidx = Paddr2idx(P, addr);
if (mpidx == -1)
return NULL;
return &P->mappings[mpidx];
}
/*
* Given a shared object name, return the map_info_t for it. If no matching
* object is found, return NULL. Normally, the link maps contain the full
* object pathname, e.g. /usr/lib/libc.so.1. We allow the object name to
* take one of the following forms:
*
* 1. An exact match (i.e. a full pathname): "/usr/lib/libc.so.1"
* 2. An exact basename match: "libc.so.1"
* 3. An initial basename match up to a '.' suffix: "libc.so" or "libc"
* 4. The literal string "a.out" is an alias for the executable mapping
*
* The third case is a convenience for callers and may not be necessary.
*
* As the exact same object name may be loaded on different link maps (see
* dlmopen(3DL)), we also allow the caller to resolve the object name by
* specifying a particular link map id. If lmid is PR_LMID_EVERY, the
* first matching name will be returned, regardless of the link map id.
*/
static map_info_t *
object_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *objname)
{
map_info_t *mp;
file_info_t *fp;
size_t objlen;
uint_t i;
/*
* If we have no rtld_db, then always treat a request as one for all
* link maps.
*/
if (P->rap == NULL)
lmid = PR_LMID_EVERY;
/*
* First pass: look for exact matches of the entire pathname, or
* basename (cases 1 and 2 above):
*/
for (i = 0, mp = P->mappings; i < P->num_mappings; i++, mp++) {
if (mp->map_pmap->pr_file->prf_mapname[0] != '/' ||
(fp = mp->map_file) == NULL)
continue;
if (lmid != PR_LMID_EVERY &&
(fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
continue;
/*
* If we match, return the primary text mapping, if there is
* one; if none (unlikely), just return the mapping we matched.
*/
if ((fp->file_pname && strcmp(fp->file_pname, objname) == 0) ||
(fp->file_lbase && strcmp(fp->file_lbase, objname) == 0) ||
(fp->file_lname && strcmp(fp->file_lname, objname) == 0))
return fp->file_map != -1 ? &P->mappings[fp->file_map] : mp;
}
objlen = strlen(objname);
/*
* Second pass: look for partial matches (case 3 above):
*/
for (i = 0, mp = P->mappings; i < P->num_mappings; i++, mp++) {
if (mp->map_pmap->pr_file->prf_mapname[0] != '/' ||
(fp = mp->map_file) == NULL)
continue;
if (lmid != PR_LMID_EVERY &&
(fp->file_lo == NULL || lmid != fp->file_lo->rl_lmident))
continue;
/*
* If we match, return the primary text mapping, if there is
* one; if none (unlikely), just return the mapping we matched.
*/
if (fp->file_lbase &&
(strncmp(fp->file_lbase, objname, objlen) == 0) &&
(fp->file_lbase[objlen] == '.'))
return fp->file_map != -1 ? &P->mappings[fp->file_map] : mp;
}
/*
* We allow "a.out" to always alias the executable, assuming this name
* was not in use for something else.
*/
if ((lmid == PR_LMID_EVERY || lmid == LM_ID_BASE) &&
(strcmp(objname, "a.out") == 0) && (P->map_exec != -1))
return &P->mappings[P->map_exec];
return NULL;
}
static map_info_t *
object_name_to_map(struct ps_prochandle *P, Lmid_t lmid, const char *name)
{
map_info_t *mptr;
if (!P->info_valid) {
Pupdate_maps(P);
if (name != PR_OBJ_LDSO)
Pupdate_lmids(P);
}
if (P->map_ldso == -1)
P->map_ldso = Paddr2idx(P, Pgetauxval(P, AT_BASE));
if (name == PR_OBJ_EXEC) {
if (P->map_exec != -1)
mptr = &P->mappings[P->map_exec];
else
mptr = NULL;
} else if (name == PR_OBJ_LDSO) {
if (P->map_ldso != -1)
mptr = &P->mappings[P->map_ldso];
else
mptr = NULL;
} else
mptr = object_to_map(P, lmid, name);
return mptr;
}
/*
* When two symbols are found by address, decide which one is to be preferred.
*/
static GElf_Sym *
sym_prefer(GElf_Sym *sym1, char *name1, GElf_Sym *sym2, char *name2)
{
/*
* Prefer the non-NULL symbol.
*/
if (sym1 == NULL)
return sym2;
if (sym2 == NULL)
return sym1;
/*
* Defer to the sort ordering...
*/
return byaddr_cmp_common(sym1, name1, sym2, name2) <= 0 ? sym1 : sym2;
}
/*
* Look up a symbol by address in the specified symbol table, using a binary
* search.
*
* Adjustment to 'addr' must already have been made for the
* offset of the symbol if this is a dynamic library symbol table.
*/
static GElf_Sym *
sym_by_addr(sym_tbl_t *symtab, GElf_Addr addr, GElf_Sym *symp, uint_t *idp)
{
GElf_Sym sym, osym;
uint_t i, oid = 0, *byaddr = symtab->sym_byaddr;
int min, max, mid, omid = 0, found = 0;
if (symtab->sym_data_pri == NULL || symtab->sym_count == 0)
return NULL;
min = 0;
max = symtab->sym_count - 1;
osym.st_value = 0;
/*
* We can't return when we've found a match, we have to continue
* searching for the closest matching symbol.
*/
while (min <= max) {
mid = (max + min) / 2;
i = byaddr[mid];
if ((symtab_getsym(symtab, i, &sym)) &&
(addr >= sym.st_value &&
addr < sym.st_value + sym.st_size &&
(!found || sym.st_value > osym.st_value))) {
osym = sym;
omid = mid;
oid = i;
found = 1;
}
if (addr < sym.st_value)
max = mid - 1;
else
min = mid + 1;
}
if (!found)
return NULL;
/*
* There may be many symbols with identical values so we walk
* backward in the byaddr table to find the best match.
*/
do {
sym = osym;
i = oid;
if (omid == 0)
break;
oid = byaddr[--omid];
symtab_getsym(symtab, oid, &osym);
} while (addr >= osym.st_value &&
addr < sym.st_value + osym.st_size &&
osym.st_value == sym.st_value);
*symp = sym;
if (idp != NULL)
*idp = i;
return symp;
}
/*
* Look up a symbol by name in the specified symbol table, using a binary
* search.
*/
static GElf_Sym *
sym_by_name(sym_tbl_t *symtab, const char *name, GElf_Sym *symp, uint_t *idp)
{
char *strs = symtab->sym_strs;
uint_t i, *byname = symtab->sym_byname;
int min, mid, max, cmp;
if (symtab->sym_data_pri == NULL || strs == NULL ||
symtab->sym_count == 0)
return NULL;
min = 0;
max = symtab->sym_count - 1;
while (min <= max) {
GElf_Sym *sym;
mid = (max + min) / 2;
i = byname[mid];
sym = symtab_getsym(symtab, i, symp);
if (sym == NULL)
_dprintf("null sym %i!\n", i);
if ((cmp = strcmp(name, strs + symp->st_name)) == 0) {
if (idp != NULL)
*idp = i;
return symp;
}
if (cmp < 0)
max = mid - 1;
else
min = mid + 1;
}
return NULL;
}
/*
* State for the symbol search iterator.
*/
typedef struct sym_search_iter {
enum { SSI_START, SSI_PATH, SSI_START_LINEAR, SSI_LINEAR, SSI_END } ssi_state;
size_t path_index;
file_info_t *fptr;
} sym_search_iter_t;
/*
* An iterator over file_info_t's, scanning the symbol search path from a given
* point, then every library in turn. Symbols may be returned more than once.
*/
static file_info_t *
sym_search_next(struct ps_prochandle *P, file_info_t *fptr,
sym_search_iter_t *state, int just_one)
{
/*
* Trivial case first: just_one alternates between returning what is
* passed in, and returning NULL (to end the loop).
*/
if (just_one) {
if (state->ssi_state == SSI_START) {
state->ssi_state = SSI_END;
return fptr;
} else {
state->ssi_state = SSI_START;
return NULL;
}
}
/*
* Start: first hit is always ourself. Subsequently, use the path
* search, if possible, or a linear search otherwise.
*/
if (state->ssi_state == SSI_START) {
state->fptr = fptr;
state->path_index = 0;
if (fptr->file_nsymsearch != 0)
state->ssi_state = SSI_PATH;
else
state->ssi_state = SSI_START_LINEAR;
return fptr;
}
switch (state->ssi_state) {
case SSI_START: /* can never happen: quash a warning */
case SSI_PATH: {
file_info_t *ret = fptr->file_symsearch[state->path_index++];
if (state->path_index >= state->fptr->file_nsymsearch) {
state->ssi_state = SSI_START_LINEAR;
}
return ret;
}
case SSI_START_LINEAR:
state->path_index = 0;
state->fptr = (file_info_t *)&P->file_list;
state->ssi_state = SSI_LINEAR;
/* Fall through */
case SSI_LINEAR:
if (state->path_index++ < P->num_files) {
state->fptr = dt_list_next(state->fptr);
} else {
state->ssi_state = SSI_END;
state->fptr = NULL;
}
return state->fptr;
case SSI_END:
state->ssi_state = SSI_START;
state->path_index = 0;
return NULL;
}
/*
* This can never happen.
*/
return NULL;
}
/*
* Search the process symbol tables looking for a symbol whose
* value to value+size contain the address specified by addr.
* Return values are:
* sym_name copy of the symbol name
* GElf_Sym symbol table entry
* Returns 0 on success, -1 on failure.
*/
int
Plookup_by_addr(struct ps_prochandle *P, uintptr_t addr, const char **sym_name,
GElf_Sym *symbolp)
{
GElf_Sym *symp;
GElf_Sym sym1, *sym1p = NULL;
GElf_Sym sym2, *sym2p = NULL;
char *name1 = NULL;
char *name2 = NULL;
uint_t i1;
uint_t i2;
map_info_t *mptr;
file_info_t *fptr;
if (P->state == PS_DEAD)
return -1;
Pupdate_maps(P);
Pupdate_lmids(P);
if ((mptr = Paddr2mptr(P, addr)) == NULL) /* no such address */
return -1;
fptr = mptr->map_file;
Pbuild_file_symtab(P, fptr);
if (fptr->file_elf == NULL) /* not an ELF file */
return -1;
/*
* Adjust the address by the load object base address in case the
* address turns out to be in a shared library. (This will likely fail
* or work only erratically for noninvasive grabs, since we cannot
* determine the runtime value of file_dyn_base in that case.)
*/
addr -= fptr->file_dyn_base;
/*
* Search both symbol tables, symtab first, then dynsym.
*/
if ((sym1p = sym_by_addr(&fptr->file_symtab, addr, &sym1, &i1)) != NULL)
name1 = fptr->file_symtab.sym_strs + sym1.st_name;
if ((sym2p = sym_by_addr(&fptr->file_dynsym, addr, &sym2, &i2)) != NULL)
name2 = fptr->file_dynsym.sym_strs + sym2.st_name;
if ((symp = sym_prefer(sym1p, name1, sym2p, name2)) == NULL)
return -1;
if (sym_name)
*sym_name = strdup((symp == sym1p) ? name1 : name2);
*symbolp = *symp;
if (GELF_ST_TYPE(symbolp->st_info) != STT_TLS)
symbolp->st_value += fptr->file_dyn_base;
return 0;
}
/*
* Search a specific symbol table looking for a symbol whose name matches the
* specified name and whose object and link map optionally match the specified
* parameters. On success, the function returns 0 and fills in the GElf_Sym
* symbol table entry, optionally without applying any form of compensation for
* the load object's load address. On failure, -1 is returned.
*/
static int
Pxlookup_by_name_internal(
struct ps_prochandle *P,
Lmid_t lmid, /* link map to match, or -1 for any */
const char *oname, /* load object name, PR_OBJ_EVERY, or
PR_OBJ_LDSO */
const char *sname, /* symbol name */
int fixup_load_addr, /* compensate for load address */
GElf_Sym *symp, /* returned symbol table entry */
prsyminfo_t *sip) /* returned symbol info */
{
file_info_t *fptr;
int just_one = 0;
sym_search_iter_t state = {0};
GElf_Sym sym;
prsyminfo_t si;
int rv = -1;
uint_t id;
if (P->state == PS_DEAD)
return -1;
memset(&sym, 0, sizeof(GElf_Sym));
memset(&si, 0, sizeof(prsyminfo_t));
if (oname == PR_OBJ_EVERY) {
/* create all the file_info_t's for all the mappings */
Pupdate_maps(P);
Pupdate_lmids(P);
if (!P->info_valid)
return -1;
/*
* Start from the executable mapping, if known.
*/
if ((P->map_exec != -1) &&
(P->mappings[P->map_exec].map_file != NULL))
fptr = P->mappings[P->map_exec].map_file;
else
fptr = dt_list_next(&P->file_list);
Pupdate_symsearch(P, fptr);
} else {
map_info_t *mptr;
just_one = 1;
if ((mptr = object_name_to_map(P, lmid, oname)) == NULL)
return -1;
fptr = mptr->map_file;
Pbuild_file_symtab(P, fptr);
}
/*
* Iterate through the loaded object files and look for the symbol
* name in the .symtab and .dynsym of each. If we encounter a match
* with SHN_UNDEF, keep looking in hopes of finding a better match.
* This means that a name such as "puts" will match the puts function
* in libc instead of matching the puts PLT entry in the a.out file.
*/
while ((fptr = sym_search_next(P, fptr, &state, just_one)) != NULL) {
Pbuild_file_symtab(P, fptr);
if (fptr->file_elf == NULL)
continue;
if (lmid != PR_LMID_EVERY && fptr->file_lo != NULL &&
lmid != fptr->file_lo->rl_lmident)
continue;
if (fptr->file_symtab.sym_data_pri != NULL &&
sym_by_name(&fptr->file_symtab, sname, symp, &id)) {
if (sip != NULL) {
sip->prs_id = id;
sip->prs_table = PR_SYMTAB;
sip->prs_object = oname;
sip->prs_name = sname;
sip->prs_lmid = fptr->file_lo == NULL ?
LM_ID_BASE : fptr->file_lo->rl_lmident;
}
} else if (fptr->file_dynsym.sym_data_pri != NULL &&
sym_by_name(&fptr->file_dynsym, sname, symp, &id)) {
if (sip != NULL) {
sip->prs_id = id;
sip->prs_table = PR_DYNSYM;
sip->prs_object = oname;
sip->prs_name = sname;
sip->prs_lmid = fptr->file_lo == NULL ?
LM_ID_BASE : fptr->file_lo->rl_lmident;
}
} else
continue;
if (fixup_load_addr && GELF_ST_TYPE(symp->st_info) != STT_TLS)
symp->st_value += fptr->file_dyn_base;
if (sym.st_shndx != SHN_UNDEF)
return 0;
if (rv != 0 ||
sym.st_shndx == SHN_UNDEF) {
if (sip != NULL)
si = *sip;
sym = *symp;
rv = 0;
}
}
if (rv == 0) {
if (sip != NULL)
*sip = si;
*symp = sym;
}
return rv;
}
/*
* Search the process symbol tables looking for a symbol whose name matches the
* specified name and whose object and link map optionally match the specified
* parameters. On success, the function returns 0 and fills in the GElf_Sym
* symbol table entry. On failure, -1 is returned.
*/
int
Pxlookup_by_name(
struct ps_prochandle *P,
Lmid_t lmid, /* link map to match, or -1 for any */
const char *oname, /* load object name, PR_OBJ_EVERY, or
PR_OBJ_LDSO */
const char *sname, /* symbol name */
GElf_Sym *symp, /* returned symbol table entry */
prsyminfo_t *sip) /* returned symbol info */
{
return Pxlookup_by_name_internal (P, lmid, oname, sname, TRUE,
symp, sip);
}
/*
* Iterate over the text mappings of the process's mapped objects.
*/
int
Pobject_iter(struct ps_prochandle *P, proc_map_f *func, void *cd)
{
map_info_t *mptr;
file_info_t *fptr;
uint_t cnt;
int rc = 0;
if (P->state == PS_DEAD)
return -1;
Pupdate_maps(P);
Pupdate_lmids(P);
if (!P->info_valid)
return -1;
for (cnt = P->num_files, fptr = dt_list_next(&P->file_list);
cnt; cnt--, fptr = dt_list_next(fptr)) {
const char *lname;
if (fptr->file_lname != NULL)
lname = fptr->file_lname;
else
lname = "";
if (fptr->file_map == -1)
continue;
mptr = &P->mappings[fptr->file_map];
if ((rc = func(cd, mptr->map_pmap, lname)) != 0)
return rc;
if (!P->info_valid) {
Pupdate_maps(P);
Pupdate_lmids(P);
}
}
return 0;
}
/*
* Given a virtual address, return the name of the underlying
* mapped object (file) as provided by the dynamic linker.
* Failing that, return the basename of its name on disk.
*
* Return NULL if we can't find any name information for the object.
*/
char *
Pobjname(struct ps_prochandle *P, uintptr_t addr,
char *buffer, size_t bufsize)
{
map_info_t *mptr;
file_info_t *fptr;
if (P->state == PS_DEAD)
return NULL;
Pupdate_maps(P);
Pupdate_lmids(P);
if ((mptr = Paddr2mptr(P, addr)) == NULL)
return NULL;
if ((fptr = mptr->map_file) == NULL)
return NULL;
if (fptr->file_lname != NULL)
strlcpy(buffer, fptr->file_lname, bufsize);
else if (fptr->file_pname != NULL)
strlcpy(buffer, fptr->file_pname, bufsize);
else
return NULL;
return buffer;
}
/*
* Given a virtual address, return the link map id of the underlying mapped
* object (file), as provided by the dynamic linker. Return -1 on failure.
*/
int
Plmid(struct ps_prochandle *P, uintptr_t addr, Lmid_t *lmidp)
{
map_info_t *mptr;
file_info_t *fptr;
if (P->state == PS_DEAD)
return -1;
/* create all the file_info_t's for all the mappings */
Pupdate_maps(P);
Pupdate_lmids(P);
if ((mptr = Paddr2mptr(P, addr)) != NULL &&
(fptr = mptr->map_file) != NULL && fptr->file_lo != NULL) {
*lmidp = fptr->file_lo->rl_lmident;
return 0;
}
return -1;
}
/*
* Given an object name, iterate over the object's symbols.
* If which == PR_SYMTAB, search the normal symbol table.
* If which == PR_DYNSYM, search the dynamic symbol table.
*/
int
Psymbol_iter_by_addr(struct ps_prochandle *P,
const char *object_name, int which, int mask, proc_sym_f *func, void *cd)
{
#if STT_NUM != (STT_TLS + 1)
#error "STT_NUM has grown. update Psymbol_iter_com()"
#endif
GElf_Sym sym;
GElf_Shdr shdr;
map_info_t *mptr;
file_info_t *fptr;
sym_tbl_t *symtab;
size_t count;
const char *strs;
size_t strsz;
int rv;
uint_t *map, i, ndx;
if (P->state == PS_DEAD)
return -1;
if ((mptr = object_name_to_map(P, PR_LMID_EVERY, object_name)) == NULL)
return -1;
fptr = mptr->map_file;
Pbuild_file_symtab(P, fptr);
if (fptr->file_elf == NULL) /* not an ELF file */
return -1;
/*
* Search the specified symbol table.
*/
switch (which) {
case PR_SYMTAB:
symtab = &fptr->file_symtab;
break;
case PR_DYNSYM:
symtab = &fptr->file_dynsym;
break;
default:
return -1;
}
strs = symtab->sym_strs;
strsz = symtab->sym_strsz;
map = symtab->sym_byaddr;
count = symtab->sym_count;
if (symtab->sym_data_pri == NULL || strs == NULL || count == 0)
return -1;
rv = 0;
for (i = 0; i < count; i++) {
ndx = map == NULL ? i : map[i];
if (symtab_getsym(symtab, ndx, &sym) != NULL) {
uint_t s_bind, s_type, type;
const char *prs_name;
if (sym.st_name >= strsz) /* invalid st_name */
continue;
s_bind = GELF_ST_BIND(sym.st_info);
s_type = GELF_ST_TYPE(sym.st_info);
/*
* In case you haven't already guessed, this relies on
* the bitmask used in <libproc.h> for encoding symbol
* type and binding matching the order of STB and STT
* constants in <sys/elf.h>. Changes to ELF must
* maintain binary compatibility, so I think this is
* reasonably fair game.
*/
if (s_bind < STB_NUM && s_type < STT_NUM) {
type = (1 << (s_type + 8)) | (1 << s_bind);
if ((type & ~mask) != 0)
continue;
} else
continue; /* Invalid type or binding */
if (GELF_ST_TYPE(sym.st_info) != STT_TLS)
sym.st_value += fptr->file_dyn_base;
prs_name = strs + sym.st_name;
/*
* If symbol's type is STT_SECTION, then try to lookup
* the name of the corresponding section.
*/
if (GELF_ST_TYPE(sym.st_info) == STT_SECTION &&
fptr->file_shstrs != NULL &&
gelf_getshdr(elf_getscn(fptr->file_elf,
sym.st_shndx), &shdr) != NULL &&
shdr.sh_name != 0 &&
shdr.sh_name < fptr->file_shstrsz)
prs_name = fptr->file_shstrs + shdr.sh_name;
if ((rv = func(cd, &sym, prs_name)) != 0)
break;
}
}
return rv;
}
/*
* Return 1 if this address is within a valid mapping.
*/
int
Pvalid_mapping(struct ps_prochandle *P, uintptr_t addr)
{
if (P->state == PS_DEAD)
return -1;
Pupdate_maps(P);
Pupdate_lmids(P);
return Paddr2mptr(P, addr) != NULL;
}
/*
* Return 1 if this address is within a file-backed mapping.
*/
int
Pfile_mapping(struct ps_prochandle *P, uintptr_t addr)
{
map_info_t *mptr;
if (P->state == PS_DEAD)
return -1;
Pupdate_maps(P);
Pupdate_lmids(P);
if ((mptr = Paddr2mptr(P, addr)) != NULL)
return mptr->map_file != NULL;
return 0;
}
/*
* Return 1 if this address is within a writable mapping.
*/
int
Pwritable_mapping(struct ps_prochandle *P, uintptr_t addr)
{
map_info_t *mptr;
if (P->state == PS_DEAD)
return -1;
Pupdate_maps(P);
Pupdate_lmids(P);
if ((mptr = Paddr2mptr(P, addr)) == NULL)
return 0;
return (mptr->map_pmap->pr_mflags & MA_WRITE) != 0;
}
/*
* Called to destroy the symbol tables. They will be recreated later as needed.
*
* Must be called by the client after an exec() in the victim process.
*/
void
Preset_maps(struct ps_prochandle *P)
{
mapping_purge(P);
free(P->mappings);
P->mappings = NULL;
file_info_purge(P);
P->info_valid = 0;
}
|