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
|
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <glob.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
#define VERSION "3.1.1"
#define ET_EXEC 2
#define ET_DYN 3
#define PT_NULL 0
#define PT_LOAD 1
#define PT_DYNAMIC 2
#define DT_NULL 0
#define DT_NEEDED 1
#define DT_STRTAB 5
#define DT_SONAME 14
#define DT_RPATH 15
#define DT_RUNPATH 29
#define BITS32 1
#define BITS64 2
#define ERR_INVALID_MAGIC 11
#define ERR_INVALID_CLASS 12
#define ERR_INVALID_DATA 13
#define ERR_INVALID_HEADER 14
#define ERR_INVALID_BITS 15
#define ERR_INVALID_ENDIANNESS 16
#define ERR_NO_EXEC_OR_DYN 17
#define ERR_INVALID_PHOFF 18
#define ERR_INVALID_PROG_HEADER 19
#define ERR_CANT_STAT 20
#define ERR_INVALID_DYNAMIC_SECTION 21
#define ERR_INVALID_DYNAMIC_ARRAY_ENTRY 22
#define ERR_NO_STRTAB 23
#define ERR_INVALID_SONAME 24
#define ERR_INVALID_RPATH 25
#define ERR_INVALID_RUNPATH 26
#define ERR_INVALID_NEEDED 27
#define ERR_DEPENDENCY_NOT_FOUND 28
#define ERR_NO_PT_LOAD 29
#define ERR_VADDRS_NOT_ORDERED 30
#define ERR_COULD_NOT_OPEN_FILE 31
#define ERR_INCOMPATIBLE_ISA 32
#define DT_FLAGS_1 0x6ffffffb
#define DT_1_NODEFLIB 0x800
#define MAX_OFFSET_T 0xFFFFFFFFFFFFFFFF
#define REGULAR_RED "\033[0;31m"
#define BOLD_RED "\033[1;31m"
#define CLEAR "\033[0m"
#define BOLD_YELLOW "\033[33m"
#define BOLD_CYAN "\033[1;36m"
#define REGULAR_CYAN "\033[0;36m"
#define REGULAR_MAGENTA "\033[0;35m"
#define REGULAR_BLUE "\033[0;34m"
#define BRIGHT_BLACK "\033[0;90m"
#define REGULAR "\033[0m"
// don't judge me.
#define LIGHT_HORIZONTAL "\xe2\x94\x80"
#define LIGHT_QUADRUPLE_DASH_VERTICAL "\xe2\x94\x8a"
#define LIGHT_UP_AND_RIGHT "\xe2\x94\x94"
#define LIGHT_VERTICAL "\xe2\x94\x82"
#define LIGHT_VERTICAL_AND_RIGHT "\xe2\x94\x9c"
#define JUST_INDENT " "
#define LIGHT_VERTICAL_WITH_INDENT LIGHT_VERTICAL " "
#define SMALL_VEC_SIZE 16
#define MAX_RECURSION_DEPTH 32
#define MAX_PATH_LENGTH 4096
// Libraries we do not show by default -- this reduces the verbosity quite a
// bit.
char const *exclude_list[] = {"ld-linux-aarch64.so",
"ld-linux-armhf.so",
"ld-linux-x86-64.so",
"ld-linux.so",
"ld64.so",
"libc.musl-aarch64.so",
"libc.musl-armhf.so",
"libc.musl-i386.so",
"libc.musl-x86_64.so",
"libc.so",
"libdl.so",
"libgcc_s.so",
"libm.so",
"libstdc++.so"};
struct header_64_t {
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
uint64_t e_entry;
uint64_t e_phoff;
uint64_t e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum;
uint16_t e_shentsize;
uint16_t e_shnum;
uint16_t e_shstrndx;
};
struct header_32_t {
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
uint32_t e_entry;
uint32_t e_phoff;
uint32_t e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum;
uint16_t e_shentsize;
uint16_t e_shnum;
uint16_t e_shstrndx;
};
struct prog_64_t {
uint32_t p_type;
uint32_t p_flags;
uint64_t p_offset;
uint64_t p_vaddr;
uint64_t p_paddr;
uint64_t p_filesz;
uint64_t p_memsz;
uint64_t p_align;
};
struct prog_32_t {
uint32_t p_type;
uint32_t p_offset;
uint32_t p_vaddr;
uint32_t p_paddr;
uint32_t p_filesz;
uint32_t p_memsz;
uint32_t p_flags;
uint32_t p_align;
};
struct dyn_64_t {
int64_t d_tag;
uint64_t d_val;
};
struct dyn_32_t {
int32_t d_tag;
uint32_t d_val;
};
struct compat_t {
char any; // 1 iff we don't look for libs matching a certain architecture
uint8_t class; // 32 or 64 bits?
uint16_t machine; // instruction set
};
typedef enum {
INPUT,
DIRECT,
RPATH,
LD_LIBRARY_PATH,
RUNPATH,
LD_SO_CONF,
DEFAULT
} how_t;
struct found_t {
how_t how;
// only set when found by in the rpath NOT of the direct parent. so, when
// it is found in a "special" way only rpaths allow, which is worth
// informing the user about.
size_t depth;
};
// large buffer in which to copy rpaths, needed libraries and sonames.
struct string_table_t {
char *arr;
size_t n;
size_t capacity;
};
struct visited_file_t {
dev_t st_dev;
ino_t st_ino;
};
struct visited_file_array_t {
struct visited_file_t *arr;
size_t n;
size_t capacity;
};
struct libtree_state_t {
int verbosity;
int path;
int color;
char *ld_conf_file;
unsigned long max_depth;
struct string_table_t string_table;
struct visited_file_array_t visited;
// rpath substitutions values (note: OSNAME/OSREL are FreeBSD specific, LIB
// is glibc/Linux specific -- we substitute all so we can support
// cross-compiled binaries).
char *PLATFORM;
char *LIB;
char *OSNAME;
char *OSREL;
// rpath stack: if lib_a needs lib_b needs lib_c and all have rpaths
// then first lib_c's rpaths are considered, then lib_b's, then lib_a's.
// so this data structure keeps a list of offsets into the string buffer
// where rpaths start, like [lib_a_rpath_offset, lib_b_rpath_offset,
// lib_c_rpath_offset]...
size_t rpath_offsets[MAX_RECURSION_DEPTH];
size_t ld_library_path_offset;
size_t default_paths_offset;
size_t ld_so_conf_offset;
// This is so we know we have to print a | or white space
// in the tree
char found_all_needed[MAX_RECURSION_DEPTH];
};
// Keep track of the files we've see
/**
* small_vec_u64 is an array that lives on the stack until it grows to the heap
*/
struct small_vec_u64_t {
uint64_t buf[SMALL_VEC_SIZE];
uint64_t *p;
size_t n;
size_t capacity;
};
static inline void utoa(char *str, size_t v) {
char *p = str;
do {
*p++ = '0' + (v % 10);
v /= 10;
} while (v > 0);
size_t len = p - str;
for (size_t i = 0; i < len / 2; i++) {
char tmp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = tmp;
}
str[len] = '\0';
}
static inline void small_vec_u64_init(struct small_vec_u64_t *v) {
memset(v, 0, sizeof(*v));
v->p = v->buf;
}
static void small_vec_u64_append(struct small_vec_u64_t *v, uint64_t val) {
// The hopefully likely path
if (v->n < SMALL_VEC_SIZE) {
v->p[v->n++] = val;
return;
}
// The slow fallback on the heap
if (v->n == SMALL_VEC_SIZE) {
v->capacity = 2 * SMALL_VEC_SIZE;
v->p = malloc(v->capacity * sizeof(uint64_t));
if (v->p == NULL)
exit(1);
memcpy(v->p, v->buf, SMALL_VEC_SIZE * sizeof(uint64_t));
} else if (v->n == v->capacity) {
v->capacity *= 2;
uint64_t *p = realloc(v->p, v->capacity * sizeof(uint64_t));
if (p == NULL)
exit(1);
v->p = p;
}
v->p[v->n++] = val;
}
static void small_vec_u64_free(struct small_vec_u64_t *v) {
if (v->n <= SMALL_VEC_SIZE)
return;
free(v->p);
v->p = NULL;
}
/**
* end of small_vec_u64_t
*/
static inline int host_is_little_endian() {
int test = 1;
char *bytes = (char *)&test;
return bytes[0] == 1;
}
static int is_ascending_order(uint64_t *v, size_t n) {
for (size_t j = 1; j < n; ++j)
if (v[j - 1] >= v[j])
return 0;
return 1;
}
static void string_table_maybe_grow(struct string_table_t *t, size_t n) {
// The likely case of not having to resize
if (t->n + n <= t->capacity)
return;
// Otherwise give twice the amount of required space.
t->capacity = 2 * (t->n + n);
char *arr = realloc(t->arr, t->capacity * sizeof(char));
if (arr == NULL) {
exit(1);
}
t->arr = arr;
}
static void string_table_store(struct string_table_t *t, char const *str) {
size_t n = strlen(str) + 1;
string_table_maybe_grow(t, n);
memcpy(t->arr + t->n, str, n);
t->n += n;
}
static void string_table_copy_from_file(struct string_table_t *t, FILE *fptr) {
int c;
// TODO: this could be a bit more efficient...
while ((c = getc(fptr)) != '\0' && c != EOF) {
string_table_maybe_grow(t, 1);
t->arr[t->n++] = c;
}
string_table_maybe_grow(t, 1);
t->arr[t->n++] = '\0';
}
static int is_in_exclude_list(char *soname) {
// Get to the end.
char *start = soname;
char *end = strrchr(start, '\0');
// Empty needed string, is that even possible?
if (start == end)
return 0;
--end;
// Strip "1234567890." from the right.
while (end != start && ((*end >= '0' && *end <= '9') || *end == '.')) {
--end;
}
// Check if we should skip this one.
for (size_t j = 0; j < sizeof(exclude_list) / sizeof(char *); ++j) {
size_t len = strlen(exclude_list[j]);
if (strncmp(start, exclude_list[j], len) != 0)
continue;
return 1;
}
return 0;
}
static void tree_preamble(struct libtree_state_t *s, size_t depth) {
if (depth == 0)
return;
for (size_t i = 0; i < depth - 1; ++i)
fputs(s->found_all_needed[i] ? JUST_INDENT : LIGHT_VERTICAL_WITH_INDENT,
stdout);
fputs(s->found_all_needed[depth - 1]
? LIGHT_UP_AND_RIGHT LIGHT_HORIZONTAL LIGHT_HORIZONTAL " "
: LIGHT_VERTICAL_AND_RIGHT LIGHT_HORIZONTAL LIGHT_HORIZONTAL " ",
stdout);
}
static int recurse(char *current_file, size_t depth,
struct libtree_state_t *state, struct compat_t compat,
struct found_t reason);
static void apply_exclude_list(size_t *needed_not_found,
struct small_vec_u64_t *needed_buf_offsets,
struct libtree_state_t *s) {
for (size_t i = 0; i < *needed_not_found;) {
// If in exclude list, swap to the back.
if (is_in_exclude_list(s->string_table.arr +
needed_buf_offsets->p[i])) {
size_t tmp = needed_buf_offsets->p[i];
needed_buf_offsets->p[i] =
needed_buf_offsets->p[*needed_not_found - 1];
needed_buf_offsets->p[--*needed_not_found] = tmp;
continue;
} else {
++i;
}
}
}
static int check_absolute_paths(size_t *needed_not_found,
struct small_vec_u64_t *needed_buf_offsets,
size_t depth, struct libtree_state_t *s,
struct compat_t compat) {
int exit_code = 0;
// First go over absolute paths in needed libs.
for (size_t i = 0; i < *needed_not_found;) {
struct string_table_t const *st = &s->string_table;
// Skip dt_needed that have do not contain /
if (strchr(st->arr + needed_buf_offsets->p[i], '/') == NULL) {
++i;
continue;
}
// Copy the path over.
char path[MAX_PATH_LENGTH];
size_t len = strlen(st->arr + needed_buf_offsets->p[i]);
// Unlikely to happen but good to guard against
if (len >= MAX_PATH_LENGTH)
continue;
// Include \0
memcpy(path, st->arr + needed_buf_offsets->p[i], len + 1);
s->found_all_needed[depth] = *needed_not_found <= 1;
char *err = NULL;
// If it is not an absolute path, we bail, cause it then starts to
// depend on the current working directory, which is rather
// nonsensical. This is allowed by glibc though.
if (path[0] != '/') {
err = " is not absolute";
exit_code = ERR_DEPENDENCY_NOT_FOUND;
} else {
int code = recurse(path, depth + 1, s, compat,
(struct found_t){.how = DIRECT});
if (code == ERR_DEPENDENCY_NOT_FOUND)
exit_code = ERR_DEPENDENCY_NOT_FOUND;
// Check if there was an issue with the direct dep and ignore errors
// of transient deps.
if (code != 0 && code != ERR_DEPENDENCY_NOT_FOUND) {
err = " not found";
}
}
if (err) {
tree_preamble(s, depth + 1);
if (s->color)
fputs(BOLD_RED, stdout);
fputs(path, stdout);
fputs(" is not absolute", stdout);
fputs(s->color ? CLEAR "\n" : "\n", stdout);
}
// Handled this library, so swap to the back.
size_t tmp = needed_buf_offsets->p[i];
needed_buf_offsets->p[i] = needed_buf_offsets->p[*needed_not_found - 1];
needed_buf_offsets->p[--*needed_not_found] = tmp;
}
return exit_code;
}
static int check_search_paths(struct found_t reason, size_t offset,
size_t *needed_not_found,
struct small_vec_u64_t *needed_buf_offsets,
size_t depth, struct libtree_state_t *s,
struct compat_t compat) {
int exit_code = 0;
char path[MAX_PATH_LENGTH];
char *path_end = path + MAX_PATH_LENGTH;
struct string_table_t const *st = &s->string_table;
while (st->arr[offset] != '\0') {
// First remove trailing colons
while (st->arr[offset] == ':' && st->arr[offset] != '\0')
++offset;
// Check if it was only colons
if (st->arr[offset] == '\0')
return exit_code;
// Copy the search path until the first \0 or :
char *dest = path;
while (st->arr[offset] != '\0' && st->arr[offset] != ':' &&
dest != path_end)
*dest++ = st->arr[offset++];
// Path too long... Can't handle.
if (dest + 1 >= path_end)
continue;
// Add a separator if necessary
if (*(dest - 1) != '/')
*dest++ = '/';
// Keep track of the end of the current search path.
char *search_path_end = dest;
// Try to open it -- if we've found anything, swap it with the back.
for (size_t i = 0; i < *needed_not_found;) {
size_t soname_len = strlen(st->arr + needed_buf_offsets->p[i]);
// Path too long, can't handle.
if (search_path_end + soname_len + 1 >= path_end)
continue;
// Otherwise append.
memcpy(search_path_end, st->arr + needed_buf_offsets->p[i],
soname_len + 1);
s->found_all_needed[depth] = *needed_not_found <= 1;
// And try to locate the lib.
int code = recurse(path, depth + 1, s, compat, reason);
if (code == ERR_DEPENDENCY_NOT_FOUND)
exit_code = ERR_DEPENDENCY_NOT_FOUND;
if (code == 0 || code == ERR_DEPENDENCY_NOT_FOUND) {
// Found at least the direct dependency, so swap out the current
// soname to the back and reduce the number of to be found by
// one.
size_t tmp = needed_buf_offsets->p[i];
needed_buf_offsets->p[i] =
needed_buf_offsets->p[*needed_not_found - 1];
needed_buf_offsets->p[--(*needed_not_found)] = tmp;
} else {
++i;
}
}
}
return exit_code;
}
static int interpolate_variables(struct libtree_state_t *s, size_t src,
char const *ORIGIN) {
// We do not write to dst if there is no variables to interpolate.
size_t prev_src = src;
size_t curr_src = src;
struct string_table_t *st = &s->string_table;
while (1) {
// Find the next potential variable.
char *dollar = strchr(st->arr + curr_src, '$');
if (dollar == NULL)
break;
curr_src = dollar - st->arr;
size_t bytes_to_dollar = curr_src - prev_src;
// Go past the dollar.
++curr_src;
// Remember if we have to look for matching curly braces.
int curly = 0;
if (st->arr[curr_src] == '{') {
curly = 1;
++curr_src;
}
// String to interpolate.
char const *var_val = NULL;
if (strncmp(&st->arr[curr_src], "ORIGIN", 6) == 0) {
var_val = ORIGIN;
curr_src += 6;
} else if (strncmp(&st->arr[curr_src], "LIB", 3) == 0) {
var_val = s->LIB;
curr_src += 3;
} else if (strncmp(&st->arr[curr_src], "PLATFORM", 8) == 0) {
var_val = s->PLATFORM;
curr_src += 8;
} else if (strncmp(&st->arr[curr_src], "OSNAME", 6) == 0) {
var_val = s->OSNAME;
curr_src += 6;
} else if (strncmp(&st->arr[curr_src], "OSREL", 5) == 0) {
var_val = s->OSREL;
curr_src += 5;
} else {
continue;
}
// Require matching {...}.
if (curly) {
if (st->arr[curr_src] != '}') {
continue;
}
++curr_src;
}
size_t var_len = strlen(var_val);
// Make sure we have enough space to write to.
string_table_maybe_grow(st, bytes_to_dollar + var_len);
// First copy over the string until the variable.
memcpy(&st->arr[s->string_table.n], &st->arr[prev_src],
bytes_to_dollar);
s->string_table.n += bytes_to_dollar;
// Then move prev_src until after the variable.
prev_src = curr_src;
// Then copy the variable value (without null).
memcpy(&st->arr[s->string_table.n], var_val, var_len);
s->string_table.n += var_len;
}
// Did we copy anything? That implies a variable was interpolated.
// Copy the remainder, including the \0.
if (prev_src != src) {
size_t n = strlen(st->arr + prev_src) + 1;
string_table_maybe_grow(st, n);
// note: we're copying from within the string table, so we
// should not store st->arr + prev_src.
memcpy(st->arr + st->n, st->arr + prev_src, n);
st->n += n;
return 1;
}
return 0;
}
static void print_colon_delimited_paths(char const *start, char const *indent) {
while (1) {
// Don't print empty string
if (*start == '\0')
break;
// Find the next delimiter after start
char *next = strchr(start, ':');
// Don't print empty strings
if (start == next) {
++start;
continue;
}
fputs(indent, stdout);
fputs(JUST_INDENT, stdout);
// Print up to but not including : or \0, followed by a newline.
if (next == NULL) {
puts(start);
} else {
fwrite(start, 1, next - start, stdout);
putchar('\n');
}
// We done yet?
if (next == NULL)
break;
// Otherwise put the : back in place and continue.
start = next + 1;
}
}
static void print_line(size_t depth, char *name, char *color_bold,
char *color_regular, int highlight,
struct found_t reason, struct libtree_state_t *s) {
tree_preamble(s, depth);
// Color the filename different than the path name, if we have a path.
char *slash = NULL;
if (s->color && highlight && (slash = strrchr(name, '/')) != NULL) {
fputs(color_regular, stdout);
fwrite(name, 1, slash + 1 - name, stdout);
fputs(color_bold, stdout);
fputs(slash + 1, stdout);
} else {
if (s->color)
fputs(color_bold, stdout);
fputs(name, stdout);
}
if (s->color && highlight)
fputs(CLEAR " " BOLD_YELLOW, stdout);
else
putchar(' ');
switch (reason.how) {
case RPATH:
if (reason.depth + 1 >= depth) {
fputs("[rpath]", stdout);
} else {
char num[8];
utoa(num, reason.depth + 1);
fputs("[rpath of ", stdout);
fputs(num, stdout);
putchar(']');
}
break;
case LD_LIBRARY_PATH:
fputs("[LD_LIBRARY_PATH]", stdout);
break;
case RUNPATH:
fputs("[runpath]", stdout);
break;
case LD_SO_CONF:
putchar('[');
char *conf_name = strrchr(s->ld_conf_file, '/');
conf_name = conf_name == NULL ? s->ld_conf_file : conf_name + 1;
fputs(conf_name, stdout);
putchar(']');
break;
case DIRECT:
fputs("[direct]", stdout);
break;
case DEFAULT:
fputs("[default path]", stdout);
break;
default:
break;
}
if (s->color)
fputs(CLEAR "\n", stdout);
else
putchar('\n');
}
static void print_error(size_t depth, size_t needed_not_found,
struct small_vec_u64_t *needed_buf_offsets,
char *runpath, struct libtree_state_t *s,
int no_def_lib) {
for (size_t i = 0; i < needed_not_found; ++i) {
s->found_all_needed[depth] = i + 1 >= needed_not_found;
tree_preamble(s, depth + 1);
if (s->color)
fputs(BOLD_RED, stdout);
fputs(s->string_table.arr + needed_buf_offsets->p[i], stdout);
fputs(" not found\n", stdout);
if (s->color)
fputs(CLEAR, stdout);
}
// If anything was not found, we print the search paths in order they
// are considered.
char *box_vertical =
s->color ? JUST_INDENT REGULAR_RED LIGHT_QUADRUPLE_DASH_VERTICAL CLEAR
: JUST_INDENT LIGHT_QUADRUPLE_DASH_VERTICAL;
char *indent = malloc(sizeof(LIGHT_VERTICAL_WITH_INDENT) * depth +
strlen(box_vertical) + 1);
char *p = indent;
for (size_t i = 0; i < depth; ++i) {
if (s->found_all_needed[i]) {
int len = sizeof(JUST_INDENT) - 1;
memcpy(p, JUST_INDENT, len);
p += len;
} else {
int len = sizeof(LIGHT_VERTICAL_WITH_INDENT) - 1;
memcpy(p, LIGHT_VERTICAL_WITH_INDENT, len);
p += len;
}
}
// dotted | in red
strcpy(p, box_vertical);
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(" Paths considered in this order:\n", stdout);
if (s->color)
fputs(CLEAR, stdout);
// Consider rpaths only when runpath is empty
fputs(indent, stdout);
if (runpath != NULL) {
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(" 1. rpath is skipped because runpath was set\n", stdout);
if (s->color)
fputs(CLEAR, stdout);
} else {
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(" 1. rpath:\n", stdout);
if (s->color)
fputs(CLEAR, stdout);
for (int j = depth; j >= 0; --j) {
if (s->rpath_offsets[j] != SIZE_MAX) {
char num[8];
utoa(num, j + 1);
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(" depth ", stdout);
fputs(num, stdout);
if (s->color)
fputs(CLEAR, stdout);
putchar('\n');
print_colon_delimited_paths(
s->string_table.arr + s->rpath_offsets[j], indent);
}
}
}
// Environment variables
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(s->ld_library_path_offset == SIZE_MAX
? " 2. LD_LIBRARY_PATH was not set\n"
: " 2. LD_LIBRARY_PATH:\n",
stdout);
if (s->color)
fputs(CLEAR, stdout);
if (s->ld_library_path_offset != SIZE_MAX)
print_colon_delimited_paths(
s->string_table.arr + s->ld_library_path_offset, indent);
// runpath
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(runpath == NULL ? " 3. runpath was not set\n" : " 3. runpath:\n",
stdout);
if (s->color)
fputs(CLEAR, stdout);
if (runpath != NULL)
print_colon_delimited_paths(runpath, indent);
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(no_def_lib
? " 4. ld config files not considered due to NODEFLIB flag\n"
: " 4. ld config files:\n",
stdout);
if (s->color)
fputs(CLEAR, stdout);
print_colon_delimited_paths(s->string_table.arr + s->ld_so_conf_offset,
indent);
fputs(indent, stdout);
if (s->color)
fputs(BRIGHT_BLACK, stdout);
fputs(no_def_lib
? " 5. Standard paths not considered due to NODEFLIB flag\n"
: " 5. Standard paths:\n",
stdout);
if (s->color)
fputs(CLEAR, stdout);
print_colon_delimited_paths(s->string_table.arr + s->default_paths_offset,
indent);
free(indent);
}
static int visited_files_contains(struct visited_file_array_t *files,
struct stat *needle) {
for (size_t i = 0; i < files->n; ++i) {
struct visited_file_t *f = &files->arr[i];
if (f->st_dev == needle->st_dev && f->st_ino == needle->st_ino)
return 1;
}
return 0;
}
static void visited_files_append(struct visited_file_array_t *files,
struct stat *new) {
if (files->n == files->capacity) {
files->capacity *= 2;
files->arr = realloc(files->arr,
files->capacity * sizeof(struct visited_file_t));
if (files->arr == NULL)
exit(1);
}
files->arr[files->n].st_dev = new->st_dev;
files->arr[files->n].st_ino = new->st_ino;
++files->n;
}
static int recurse(char *current_file, size_t depth, struct libtree_state_t *s,
struct compat_t compat, struct found_t reason) {
FILE *fptr = fopen(current_file, "rb");
if (fptr == NULL)
return ERR_COULD_NOT_OPEN_FILE;
// When we're done recursing, we should give back the memory we've claimed.
size_t old_buf_size = s->string_table.n;
// Parse the header
char e_ident[16];
if (fread(&e_ident, 16, 1, fptr) != 1) {
fclose(fptr);
return ERR_INVALID_MAGIC;
}
// Find magic elfs
if (e_ident[0] != 0x7f || e_ident[1] != 'E' || e_ident[2] != 'L' ||
e_ident[3] != 'F') {
fclose(fptr);
return ERR_INVALID_MAGIC;
}
// Do at least *some* header validation
if (e_ident[4] != BITS32 && e_ident[4] != BITS64) {
fclose(fptr);
return ERR_INVALID_CLASS;
}
if (e_ident[5] != '\x01' && e_ident[5] != '\x02') {
fclose(fptr);
return ERR_INVALID_DATA;
}
struct compat_t curr_type = {.any = 0, .class = e_ident[4]};
int is_little_endian = e_ident[5] == '\x01';
// Make sure that we have matching bits with parent
if (!compat.any && compat.class != curr_type.class) {
fclose(fptr);
return ERR_INVALID_BITS;
}
// Make sure that the elf file has a the host's endianness
// Byte swapping is on the TODO list
if (is_little_endian ^ host_is_little_endian()) {
fclose(fptr);
return ERR_INVALID_ENDIANNESS;
}
// And get the type
union {
struct header_64_t h64;
struct header_32_t h32;
} header;
// Read the (rest of the) elf header
if (curr_type.class == BITS64) {
if (fread(&header.h64, sizeof(struct header_64_t), 1, fptr) != 1) {
fclose(fptr);
return ERR_INVALID_HEADER;
}
if (header.h64.e_type != ET_EXEC && header.h64.e_type != ET_DYN) {
fclose(fptr);
return ERR_NO_EXEC_OR_DYN;
}
curr_type.machine = header.h64.e_machine;
if (!compat.any && compat.machine != curr_type.machine) {
fclose(fptr);
return ERR_INCOMPATIBLE_ISA;
}
if (fseek(fptr, header.h64.e_phoff, SEEK_SET) != 0) {
fclose(fptr);
return ERR_INVALID_PHOFF;
}
} else {
if (fread(&header.h32, sizeof(struct header_32_t), 1, fptr) != 1) {
fclose(fptr);
return ERR_INVALID_HEADER;
}
if (header.h32.e_type != ET_EXEC && header.h32.e_type != ET_DYN) {
fclose(fptr);
return ERR_NO_EXEC_OR_DYN;
}
curr_type.machine = header.h32.e_machine;
if (!compat.any && compat.machine != curr_type.machine) {
fclose(fptr);
return ERR_INCOMPATIBLE_ISA;
}
if (fseek(fptr, header.h32.e_phoff, SEEK_SET) != 0) {
fclose(fptr);
return ERR_INVALID_PHOFF;
}
}
// Make sure it's an executable or library
union {
struct prog_64_t p64;
struct prog_32_t p32;
} prog;
// map vaddr to file offset (we don't mmap the file, but directly seek in
// the file which means that we have to translate vaddr to file offset)
struct small_vec_u64_t pt_load_offset;
struct small_vec_u64_t pt_load_vaddr;
small_vec_u64_init(&pt_load_offset);
small_vec_u64_init(&pt_load_vaddr);
// Read the program header.
uint64_t p_offset = MAX_OFFSET_T;
if (curr_type.class == BITS64) {
for (uint64_t i = 0; i < header.h64.e_phnum; ++i) {
if (fread(&prog.p64, sizeof(struct prog_64_t), 1, fptr) != 1) {
fclose(fptr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&pt_load_vaddr);
return ERR_INVALID_PROG_HEADER;
}
if (prog.p64.p_type == PT_LOAD) {
small_vec_u64_append(&pt_load_offset, prog.p64.p_offset);
small_vec_u64_append(&pt_load_vaddr, prog.p64.p_vaddr);
} else if (prog.p64.p_type == PT_DYNAMIC) {
p_offset = prog.p64.p_offset;
}
}
} else {
for (uint32_t i = 0; i < header.h32.e_phnum; ++i) {
if (fread(&prog.p32, sizeof(struct prog_32_t), 1, fptr) != 1) {
fclose(fptr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&pt_load_vaddr);
return ERR_INVALID_PROG_HEADER;
}
if (prog.p32.p_type == PT_LOAD) {
small_vec_u64_append(&pt_load_offset, prog.p32.p_offset);
small_vec_u64_append(&pt_load_vaddr, prog.p32.p_vaddr);
} else if (prog.p32.p_type == PT_DYNAMIC) {
p_offset = prog.p32.p_offset;
}
}
}
// At this point we're going to store the file as "success"
struct stat finfo;
if (stat(current_file, &finfo) != 0) {
fclose(fptr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&pt_load_vaddr);
return ERR_CANT_STAT;
}
int seen_before = visited_files_contains(&s->visited, &finfo);
if (!seen_before)
visited_files_append(&s->visited, &finfo);
// No dynamic section?
if (p_offset == MAX_OFFSET_T) {
print_line(depth, current_file, BOLD_CYAN, REGULAR_CYAN, 1, reason, s);
fclose(fptr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&pt_load_vaddr);
return 0;
}
// I guess you always have to load at least a string
// table, so if there are not PT_LOAD sections, then
// it is an error.
if (pt_load_offset.n == 0) {
fclose(fptr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&pt_load_vaddr);
return ERR_NO_PT_LOAD;
}
// Go to the dynamic section
if (fseek(fptr, p_offset, SEEK_SET) != 0) {
fclose(fptr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&pt_load_vaddr);
return ERR_INVALID_DYNAMIC_SECTION;
}
// Shared libraries can disable searching in
// "default" search paths, aka ld.so.conf and
// /usr/lib etc. At least glibc respects this.
int no_def_lib = 0;
uint64_t strtab = MAX_OFFSET_T;
uint64_t rpath = MAX_OFFSET_T;
uint64_t runpath = MAX_OFFSET_T;
uint64_t soname = MAX_OFFSET_T;
// Offsets in strtab
struct small_vec_u64_t needed;
small_vec_u64_init(&needed);
for (int cont = 1; cont;) {
uint64_t d_tag;
uint64_t d_val;
if (curr_type.class == BITS64) {
struct dyn_64_t dyn;
if (fread(&dyn, sizeof(struct dyn_64_t), 1, fptr) != 1) {
fclose(fptr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&pt_load_vaddr);
small_vec_u64_free(&needed);
return ERR_INVALID_DYNAMIC_ARRAY_ENTRY;
}
d_tag = dyn.d_tag;
d_val = dyn.d_val;
} else {
struct dyn_32_t dyn;
if (fread(&dyn, sizeof(struct dyn_32_t), 1, fptr) != 1) {
fclose(fptr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&pt_load_vaddr);
small_vec_u64_free(&needed);
return ERR_INVALID_DYNAMIC_ARRAY_ENTRY;
}
d_tag = dyn.d_tag;
d_val = dyn.d_val;
}
// Store strtab / rpath / runpath / needed / soname info.
switch (d_tag) {
case DT_NULL:
cont = 0;
break;
case DT_STRTAB:
strtab = d_val;
break;
case DT_RPATH:
rpath = d_val;
break;
case DT_RUNPATH:
runpath = d_val;
break;
case DT_NEEDED:
small_vec_u64_append(&needed, d_val);
break;
case DT_SONAME:
soname = d_val;
break;
case DT_FLAGS_1:
no_def_lib |= (DT_1_NODEFLIB & d_val) == DT_1_NODEFLIB;
break;
}
}
if (strtab == MAX_OFFSET_T) {
fclose(fptr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&pt_load_vaddr);
small_vec_u64_free(&needed);
return ERR_NO_STRTAB;
}
// Let's verify just to be sure that the offsets are
// ordered.
if (!is_ascending_order(pt_load_vaddr.p, pt_load_vaddr.n)) {
fclose(fptr);
small_vec_u64_free(&pt_load_vaddr);
small_vec_u64_free(&pt_load_offset);
small_vec_u64_free(&needed);
return ERR_VADDRS_NOT_ORDERED;
}
// Find the file offset corresponding to the strtab virtual address
size_t vaddr_idx = 0;
while (vaddr_idx + 1 != pt_load_vaddr.n &&
strtab >= pt_load_vaddr.p[vaddr_idx + 1]) {
++vaddr_idx;
}
uint64_t strtab_offset =
pt_load_offset.p[vaddr_idx] + strtab - pt_load_vaddr.p[vaddr_idx];
small_vec_u64_free(&pt_load_vaddr);
small_vec_u64_free(&pt_load_offset);
// From this point on we actually copy strings from the ELF file into our
// own string buffer.
// Copy the current soname
size_t soname_buf_offset = s->string_table.n;
if (soname != MAX_OFFSET_T) {
if (fseek(fptr, strtab_offset + soname, SEEK_SET) != 0) {
s->string_table.n = old_buf_size;
fclose(fptr);
small_vec_u64_free(&needed);
return ERR_INVALID_SONAME;
}
string_table_copy_from_file(&s->string_table, fptr);
}
int in_exclude_list =
soname != MAX_OFFSET_T &&
is_in_exclude_list(s->string_table.arr + soname_buf_offset);
// No need to recurse deeper when we aren't in very verbose mode.
int should_recurse =
depth < s->max_depth &&
((!seen_before && !in_exclude_list) ||
(!seen_before && in_exclude_list && s->verbosity >= 2) ||
s->verbosity >= 3);
// Just print the library and return
if (!should_recurse) {
char *print_name = soname == MAX_OFFSET_T || s->path
? current_file
: (s->string_table.arr + soname_buf_offset);
char *bold_color = in_exclude_list
? REGULAR_MAGENTA
: seen_before ? REGULAR_BLUE : BOLD_CYAN;
char *regular_color = in_exclude_list
? REGULAR_MAGENTA
: seen_before ? REGULAR_BLUE : REGULAR_CYAN;
int highlight = !seen_before && !in_exclude_list;
print_line(depth, print_name, bold_color, regular_color, highlight,
reason, s);
s->string_table.n = old_buf_size;
fclose(fptr);
small_vec_u64_free(&needed);
return 0;
}
// Store the ORIGIN string.
char origin[MAX_PATH_LENGTH];
char *last_slash = strrchr(current_file, '/');
if (last_slash != NULL) {
// Exclude the last slash
size_t bytes = last_slash - current_file;
memcpy(origin, current_file, bytes);
origin[bytes] = '\0';
} else {
// this only happens when the input is relative (e.g. in current dir)
memcpy(origin, "./", 3);
}
// Copy DT_PRATH
if (rpath == MAX_OFFSET_T) {
s->rpath_offsets[depth] = SIZE_MAX;
} else {
s->rpath_offsets[depth] = s->string_table.n;
if (fseek(fptr, strtab_offset + rpath, SEEK_SET) != 0) {
s->string_table.n = old_buf_size;
fclose(fptr);
small_vec_u64_free(&needed);
return ERR_INVALID_RPATH;
}
string_table_copy_from_file(&s->string_table, fptr);
// We store the interpolated string right after the literal copy.
size_t curr_buf_size = s->string_table.n;
if (interpolate_variables(s, s->rpath_offsets[depth], origin))
s->rpath_offsets[depth] = curr_buf_size;
}
// Copy DT_RUNPATH
size_t runpath_buf_offset = s->string_table.n;
if (runpath != MAX_OFFSET_T) {
if (fseek(fptr, strtab_offset + runpath, SEEK_SET) != 0) {
s->string_table.n = old_buf_size;
fclose(fptr);
small_vec_u64_free(&needed);
return ERR_INVALID_RUNPATH;
}
string_table_copy_from_file(&s->string_table, fptr);
// We store the interpolated string right after the literal copy.
size_t curr_buf_size = s->string_table.n;
if (interpolate_variables(s, runpath_buf_offset, origin))
runpath_buf_offset = curr_buf_size;
}
// Copy needed libraries.
struct small_vec_u64_t needed_buf_offsets;
small_vec_u64_init(&needed_buf_offsets);
for (size_t i = 0; i < needed.n; ++i) {
small_vec_u64_append(&needed_buf_offsets, s->string_table.n);
if (fseek(fptr, strtab_offset + needed.p[i], SEEK_SET) != 0) {
s->string_table.n = old_buf_size;
fclose(fptr);
small_vec_u64_free(&needed_buf_offsets);
small_vec_u64_free(&needed);
return ERR_INVALID_NEEDED;
}
string_table_copy_from_file(&s->string_table, fptr);
}
fclose(fptr);
char *print_name = soname == MAX_OFFSET_T || s->path
? current_file
: (s->string_table.arr + soname_buf_offset);
char *bold_color = in_exclude_list ? REGULAR_MAGENTA
: seen_before ? REGULAR_BLUE : BOLD_CYAN;
char *regular_color = in_exclude_list
? REGULAR_MAGENTA
: seen_before ? REGULAR_BLUE : REGULAR_CYAN;
int highlight = !seen_before && !in_exclude_list;
print_line(depth, print_name, bold_color, regular_color, highlight, reason,
s);
// Finally start searching.
int exit_code = 0;
size_t needed_not_found = needed_buf_offsets.n;
// Skip common libraries if not verbose
if (needed_not_found && s->verbosity == 0)
apply_exclude_list(&needed_not_found, &needed_buf_offsets, s);
if (needed_not_found)
exit_code |= check_absolute_paths(
&needed_not_found, &needed_buf_offsets, depth, s, curr_type);
// Consider rpaths only when runpath is empty
if (runpath == MAX_OFFSET_T) {
// We have a stack of rpaths, try them all, starting with one set at
// this lib, then the parents.
for (int j = depth; j >= 0 && needed_not_found; --j) {
if (s->rpath_offsets[j] == SIZE_MAX)
continue;
exit_code |= check_search_paths(
(struct found_t){.how = RPATH, .depth = j}, s->rpath_offsets[j],
&needed_not_found, &needed_buf_offsets, depth, s, curr_type);
}
}
// Then try LD_LIBRARY_PATH, if we have it.
if (needed_not_found && s->ld_library_path_offset != SIZE_MAX) {
exit_code |= check_search_paths(
(struct found_t){.how = LD_LIBRARY_PATH}, s->ld_library_path_offset,
&needed_not_found, &needed_buf_offsets, depth, s, curr_type);
}
// Then consider runpaths
if (needed_not_found && runpath != MAX_OFFSET_T) {
exit_code |= check_search_paths(
(struct found_t){.how = RUNPATH}, runpath_buf_offset,
&needed_not_found, &needed_buf_offsets, depth, s, curr_type);
}
// Check ld.so.conf paths
if (needed_not_found && !no_def_lib) {
exit_code |= check_search_paths(
(struct found_t){.how = LD_SO_CONF}, s->ld_so_conf_offset,
&needed_not_found, &needed_buf_offsets, depth, s, curr_type);
}
// Then consider standard paths
if (needed_not_found && !no_def_lib) {
exit_code |= check_search_paths(
(struct found_t){.how = DEFAULT}, s->default_paths_offset,
&needed_not_found, &needed_buf_offsets, depth, s, curr_type);
}
// Finally summarize those that could not be found.
if (needed_not_found) {
print_error(depth, needed_not_found, &needed_buf_offsets,
runpath == MAX_OFFSET_T
? NULL
: s->string_table.arr + runpath_buf_offset,
s, no_def_lib);
s->string_table.n = old_buf_size;
small_vec_u64_free(&needed_buf_offsets);
small_vec_u64_free(&needed);
return ERR_DEPENDENCY_NOT_FOUND;
}
// Free memory in our string table
s->string_table.n = old_buf_size;
small_vec_u64_free(&needed_buf_offsets);
small_vec_u64_free(&needed);
return exit_code;
}
static int parse_ld_config_file(struct string_table_t *st, char *path);
static int ld_conf_globbing(struct string_table_t *st, char *pattern) {
glob_t result;
memset(&result, 0, sizeof(result));
int status = glob(pattern, 0, NULL, &result);
// Handle errors (no result is not an error...)
switch (status) {
case GLOB_NOSPACE:
case GLOB_ABORTED:
globfree(&result);
return 1;
case GLOB_NOMATCH:
globfree(&result);
return 0;
}
// Otherwise parse the files we've found!
int code = 0;
for (size_t i = 0; i < result.gl_pathc; ++i)
code |= parse_ld_config_file(st, result.gl_pathv[i]);
globfree(&result);
return code;
}
static int parse_ld_config_file(struct string_table_t *st, char *path) {
FILE *fptr = fopen(path, "r");
if (fptr == NULL)
return 1;
int c = 0;
char line[MAX_PATH_LENGTH];
char tmp[MAX_PATH_LENGTH];
while (c != EOF) {
size_t line_len = 0;
while ((c = getc(fptr)) != '\n' && c != EOF) {
if (line_len < MAX_PATH_LENGTH - 1) {
line[line_len++] = c;
}
}
line[line_len] = '\0';
char *begin = line;
char *end = line + line_len;
// Remove leading whitespace
for (; isspace(*begin); ++begin) {
}
// Remove trailing comments
char *comment = strchr(begin, '#');
if (comment != NULL)
*comment = '\0';
// Remove trailing whitespace
while (end != begin)
if (!isspace(*--end))
break;
// Skip empty lines
if (begin == end)
continue;
// Put back the end of the string
end[1] = '\0';
// 'include ': glob whatever follows.
if (strncmp(begin, "include", 7) == 0 && isspace(begin[7])) {
begin += 8;
// Remove more whitespace.
while (isspace(*begin))
++begin;
// Prepend current dir when include dir is relative.
if (*begin != '/') {
char *wd = strrchr(path, '/');
wd = wd == NULL ? strrchr(path, '\0') : wd;
// bytes until /
size_t wd_len = wd - path;
size_t include_len = end - begin + 1;
// just skip then.
if (wd_len + 1 + include_len >= MAX_PATH_LENGTH)
continue;
memcpy(tmp, path, wd_len);
tmp[wd_len] = '/';
memcpy(tmp + wd_len + 1, begin, include_len);
tmp[wd_len + 1 + include_len] = '\0';
begin = tmp;
}
ld_conf_globbing(st, begin);
} else {
// Copy over and replace trailing \0 with :.
string_table_store(st, begin);
st->arr[st->n - 1] = ':';
}
}
fclose(fptr);
return 0;
}
static void parse_ld_so_conf(struct libtree_state_t *s) {
struct string_table_t *st = &s->string_table;
s->ld_so_conf_offset = st->n;
// Linux / glibc
parse_ld_config_file(st, s->ld_conf_file);
// Replace the last semicolon with a '\0'
// if we have a nonzero number of paths.
if (st->n > s->ld_so_conf_offset) {
st->arr[st->n - 1] = '\0';
} else {
string_table_store(st, "");
}
}
static void parse_ld_library_path(struct libtree_state_t *s) {
s->ld_library_path_offset = SIZE_MAX;
char *val = getenv("LD_LIBRARY_PATH");
// not set, so nothing to do.
if (val == NULL)
return;
s->ld_library_path_offset = s->string_table.n;
// otherwise, we just copy it over and replace ; with :
string_table_store(&s->string_table, val);
// replace ; with :
char *search = s->string_table.arr + s->ld_library_path_offset;
while ((search = strchr(search, ';')) != NULL)
*search++ = ':';
}
static void set_default_paths(struct libtree_state_t *s) {
s->default_paths_offset = s->string_table.n;
// TODO: how to retrieve this list properly at runtime?
string_table_store(&s->string_table, "/lib:/lib64:/usr/lib:/usr/lib64");
}
static void libtree_state_init(struct libtree_state_t *s) {
s->string_table.n = 0;
s->string_table.capacity = 1024;
s->string_table.arr = malloc(s->string_table.capacity * sizeof(char));
s->visited.n = 0;
s->visited.capacity = 256;
s->visited.arr =
malloc(s->visited.capacity * sizeof(struct visited_file_t));
}
static void libtree_state_free(struct libtree_state_t *s) {
free(s->string_table.arr);
free(s->visited.arr);
}
static int print_tree(int pathc, char **pathv, struct libtree_state_t *s) {
// First collect standard paths
libtree_state_init(s);
parse_ld_so_conf(s);
parse_ld_library_path(s);
set_default_paths(s);
int exit_code = 0;
for (int i = 0; i < pathc; ++i) {
int code = recurse(pathv[i], 0, s, (struct compat_t){.any = 1},
(struct found_t){.how = INPUT});
fflush(stdout);
if (code != 0) {
exit_code = code;
fputs("Error [", stderr);
fputs(pathv[i], stderr);
fputs("]: ", stderr);
}
char *msg = NULL;
switch (code) {
case ERR_INVALID_MAGIC:
msg = "Invalid ELF magic bytes\n";
break;
case ERR_INVALID_CLASS:
msg = "Invalid ELF class\n";
break;
case ERR_INVALID_DATA:
msg = "Invalid ELF data\n";
break;
case ERR_INVALID_HEADER:
msg = "Invalid ELF header\n";
break;
case ERR_INVALID_BITS:
msg = "Invalid bits\n";
break;
case ERR_INVALID_ENDIANNESS:
msg = "Invalid endianness\n";
break;
case ERR_NO_EXEC_OR_DYN:
msg = "Not an ET_EXEC or ET_DYN ELF file\n";
break;
case ERR_INVALID_PHOFF:
msg = "Invalid ELF program header offset\n";
break;
case ERR_INVALID_PROG_HEADER:
msg = "Invalid ELF program header\n";
break;
case ERR_CANT_STAT:
msg = "Can't stat file\n";
break;
case ERR_INVALID_DYNAMIC_SECTION:
msg = "Invalid ELF dynamic section\n";
break;
case ERR_INVALID_DYNAMIC_ARRAY_ENTRY:
msg = "Invalid ELF dynamic array entry\n";
break;
case ERR_NO_STRTAB:
msg = "No ELF string table found\n";
break;
case ERR_INVALID_SONAME:
msg = "Can't read DT_SONAME\n";
break;
case ERR_INVALID_RPATH:
msg = "Can't read DT_RPATH\n";
break;
case ERR_INVALID_RUNPATH:
msg = "Can't read DT_RUNPATH\n";
break;
case ERR_INVALID_NEEDED:
msg = "Can't read DT_NEEDED\n";
break;
case ERR_DEPENDENCY_NOT_FOUND:
msg = "Not all dependencies were found\n";
break;
case ERR_NO_PT_LOAD:
msg = "No PT_LOAD found in ELF file\n";
break;
case ERR_VADDRS_NOT_ORDERED:
msg = "Virtual addresses are not ordered\n";
break;
case ERR_COULD_NOT_OPEN_FILE:
msg = "Could not open file\n";
break;
case ERR_INCOMPATIBLE_ISA:
msg = "Incompatible ISA\n";
break;
}
if (msg != NULL)
fputs(msg, stderr);
fflush(stderr);
}
libtree_state_free(s);
return exit_code;
}
int main(int argc, char **argv) {
// Enable or disable colors (no-color.com)
struct libtree_state_t s;
s.color = getenv("NO_COLOR") == NULL && isatty(STDOUT_FILENO);
s.verbosity = 0;
s.path = 0;
s.max_depth = MAX_RECURSION_DEPTH;
// We want to end up with an array of file names
// in argv[1] up to argv[positional-1].
int positional = 1;
struct utsname uname_val;
if (uname(&uname_val) != 0)
return 1;
// Technically this should be AT_PLATFORM, but
// (a) the feature is rarely used
// (b) it's almost always the same
s.PLATFORM = uname_val.machine;
s.OSNAME = uname_val.sysname;
s.OSREL = uname_val.release;
s.ld_conf_file = "/etc/ld.so.conf";
if (strcmp(uname_val.sysname, "FreeBSD") == 0)
s.ld_conf_file = "/etc/ld-elf.so.conf";
// TODO: how to find this value at runtime?
s.LIB = "lib";
int opt_help = 0;
int opt_version = 0;
// After `--` we treat everything as filenames, not flags.
int opt_raw = 0;
for (int i = 1; i < argc; ++i) {
char *arg = argv[i];
// Positional args don't start with - or are `-` literal.
if (opt_raw || *arg != '-' || arg[1] == '\0') {
argv[positional++] = arg;
continue;
}
// Now we're in flag land!
++arg;
// Long flags
if (*arg == '-') {
++arg;
// Literal '--'
if (*arg == '\0') {
opt_raw = 1;
continue;
}
if (strcmp(arg, "version") == 0) {
opt_version = 1;
} else if (strcmp(arg, "path") == 0) {
s.path = 1;
} else if (strcmp(arg, "verbose") == 0) {
++s.verbosity;
} else if (strcmp(arg, "help") == 0) {
opt_help = 1;
} else if (strcmp(arg, "ldconf") == 0) {
// Require a value
if (i + 1 == argc) {
fputs("Expected value after `--ldconf`\n", stderr);
return 1;
}
s.ld_conf_file = argv[++i];
} else if (strcmp(arg, "max-depth") == 0) {
// Require a value
if (i + 1 == argc) {
fputs("Expected value after `--max-depth`\n", stderr);
return 1;
}
// Limit it by MAX_RECURSION_DEPTH.
char *ptr;
s.max_depth = strtoul(argv[++i], &ptr, 10);
if (s.max_depth > MAX_RECURSION_DEPTH)
s.max_depth = MAX_RECURSION_DEPTH;
} else {
fputs("Unrecognized flag `--", stderr);
fputs(arg, stderr);
fputs("`\n", stderr);
return 1;
}
continue;
}
// Short flags
for (; *arg != '\0'; ++arg) {
switch (*arg) {
case 'h':
opt_help = 1;
break;
case 'p':
s.path = 1;
break;
case 'v':
++s.verbosity;
break;
default:
fputs("Unrecognized flag `-", stderr);
fputs(arg, stderr);
fputs("`\n", stderr);
return 1;
}
}
}
++argv;
--positional;
// Print a help message on -h, --help or no positional args.
if (opt_help || (!opt_version && positional == 0)) {
// clang-format off
fputs("Show the dynamic dependency tree of ELF files\n"
"Usage: libtree [OPTION]... [--] FILE [FILES]...\n"
"\n"
" -h, --help Print help info\n"
" --version Print version info\n"
"\n"
"File names starting with '-', for example '-.so', can be specified as follows:\n"
" libtree -- -.so\n"
"\n"
"Locating libs options:\n"
" -p, --path Show the path of libraries instead of the soname\n"
" -v Show libraries skipped by default*\n"
" -vv Show dependencies of libraries skipped by default*\n"
" -vvv Show dependencies of already encountered libraries\n"
" --ldconf <path> Config file for extra search paths [", stdout);
fputs(s.ld_conf_file, stdout);
fputs("]\n"
" --max-depth <n> Limit library traversal to at most n levels of depth\n"
"\n"
"* For brevity, the following libraries are not shown by default:\n"
" ",
stdout);
// clang-format on
// Print a comma separated list of skipped libraries,
// with some new lines every now and then to make it readable.
size_t num_excluded = sizeof(exclude_list) / sizeof(char *);
size_t cursor_x = 3;
for (size_t j = 0; j < num_excluded; ++j) {
cursor_x += strlen(exclude_list[j]);
if (cursor_x > 60) {
cursor_x = 3;
fputs("\n ", stdout);
}
fputs(exclude_list[j], stdout);
if (j + 1 != num_excluded)
fputs(", ", stdout);
}
// rpath substitution values:
fputs(".\n\nThe following rpath/runpath substitutions are used:\n",
stdout);
fputs(" PLATFORM ", stdout);
fputs(s.PLATFORM, stdout);
fputs("\n LIB ", stdout);
fputs(s.LIB, stdout);
fputs("\n OSNAME ", stdout);
fputs(s.OSNAME, stdout);
fputs("\n OSREL ", stdout);
fputs(s.OSREL, stdout);
putchar('\n');
// Return an error status code if no positional args were passed.
return !opt_help;
}
if (opt_version) {
puts(VERSION);
return 0;
}
return print_tree(positional, argv, &s);
}
|