ldd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * A small little ldd implementation for uClibc
  4. *
  5. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  6. *
  7. * Several functions in this file (specifically, elf_find_section_type(),
  8. * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
  9. * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
  10. * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC
  11. * (GPL2).
  12. *
  13. * Licensed under GPLv2 or later
  14. */
  15. #include "porting.h"
  16. #if defined(__alpha__)
  17. #define MATCH_MACHINE(x) (x == EM_ALPHA)
  18. #define ELFCLASSM ELFCLASS64
  19. #endif
  20. #if defined(__arc__)
  21. #define MATCH_MACHINE(x) (x == EM_ARCOMPACT)
  22. #define ELFCLASSM ELFCLASS32
  23. #endif
  24. #if defined(__arm__) || defined(__thumb__)
  25. #define MATCH_MACHINE(x) (x == EM_ARM)
  26. #define ELFCLASSM ELFCLASS32
  27. #endif
  28. #if defined(__avr32__)
  29. #define MATCH_MACHINE(x) (x == EM_AVR32)
  30. #define ELFCLASSM ELFCLASS32
  31. #endif
  32. #if defined(__s390__)
  33. #define MATCH_MACHINE(x) (x == EM_S390)
  34. #define ELFCLASSM ELFCLASS32
  35. #endif
  36. #if defined(__hppa__)
  37. #define MATCH_MACHINE(x) (x == EM_PARISC)
  38. #if defined(__LP64__)
  39. #define ELFCLASSM ELFCLASS64
  40. #else
  41. #define ELFCLASSM ELFCLASS32
  42. #endif
  43. #endif
  44. #if defined(__i386__)
  45. #ifndef EM_486
  46. #define MATCH_MACHINE(x) (x == EM_386)
  47. #else
  48. #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
  49. #endif
  50. #define ELFCLASSM ELFCLASS32
  51. #endif
  52. #if defined(__ia64__)
  53. #define MATCH_MACHINE(x) (x == EM_IA_64)
  54. #define ELFCLASSM ELFCLASS64
  55. #endif
  56. #if defined(__mc68000__)
  57. #define MATCH_MACHINE(x) (x == EM_68K)
  58. #define ELFCLASSM ELFCLASS32
  59. #endif
  60. #if defined(__metag__)
  61. #define MATCH_MACHINE(x) (x == EM_METAG)
  62. #define ELFCLASSM ELFCLASS32
  63. #endif
  64. #if defined(__mips__)
  65. #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
  66. #define ELFCLASSM ELFCLASS32
  67. #endif
  68. #if defined(__powerpc64__)
  69. #define MATCH_MACHINE(x) (x == EM_PPC64)
  70. #define ELFCLASSM ELFCLASS64
  71. #elif defined(__powerpc__)
  72. #define MATCH_MACHINE(x) (x == EM_PPC)
  73. #define ELFCLASSM ELFCLASS32
  74. #endif
  75. #if defined(__sh__)
  76. #define MATCH_MACHINE(x) (x == EM_SH)
  77. #define ELFCLASSM ELFCLASS32
  78. #endif
  79. #if defined(__sparc__)
  80. #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
  81. #define ELFCLASSM ELFCLASS32
  82. #endif
  83. #if defined(__cris__)
  84. #define MATCH_MACHINE(x) (x == EM_CRIS)
  85. #define ELFCLASSM ELFCLASS32
  86. #endif
  87. #if defined(__x86_64__)
  88. #define MATCH_MACHINE(x) (x == EM_X86_64)
  89. #define ELFCLASSM ELFCLASS64
  90. #endif
  91. #if defined(__microblaze__)
  92. #define MATCH_MACHINE(x) (x == EM_MICROBLAZE_OLD)
  93. #define ELFCLASSM ELFCLASS32
  94. #endif
  95. #if defined(__xtensa__)
  96. #define MATCH_MACHINE(x) (x == EM_XTENSA)
  97. #define ELFCLASSM ELFCLASS32
  98. #endif
  99. #ifndef MATCH_MACHINE
  100. # ifdef __linux__
  101. # include <asm/elf.h>
  102. # endif
  103. # ifdef ELF_ARCH
  104. # define MATCH_MACHINE(x) (x == ELF_ARCH)
  105. # endif
  106. # ifdef ELF_CLASS
  107. # define ELFCLASSM ELF_CLASS
  108. # endif
  109. #endif
  110. #ifndef MATCH_MACHINE
  111. # warning "You really should add a MATCH_MACHINE() macro for your architecture"
  112. #endif
  113. #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
  114. #define ELFDATAM ELFDATA2LSB
  115. #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
  116. #define ELFDATAM ELFDATA2MSB
  117. #endif
  118. #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
  119. struct library {
  120. char *name;
  121. int resolved;
  122. char *path;
  123. struct library *next;
  124. };
  125. static struct library *lib_list = NULL;
  126. static char not_found[] = "not found";
  127. static char *interp_name = NULL;
  128. static char *interp_dir = NULL;
  129. static int byteswap;
  130. static int interpreter_already_found = 0;
  131. static __inline__ uint32_t byteswap32_to_host(uint32_t value)
  132. {
  133. if (byteswap) {
  134. return (bswap_32(value));
  135. } else {
  136. return (value);
  137. }
  138. }
  139. static __inline__ uint64_t byteswap64_to_host(uint64_t value)
  140. {
  141. if (byteswap) {
  142. return (bswap_64(value));
  143. } else {
  144. return (value);
  145. }
  146. }
  147. #if __WORDSIZE == 64
  148. # define byteswap_to_host(x) byteswap64_to_host(x)
  149. #else
  150. # define byteswap_to_host(x) byteswap32_to_host(x)
  151. #endif
  152. static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
  153. {
  154. int j;
  155. ElfW(Shdr) *shdr;
  156. shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
  157. for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
  158. if (key == byteswap32_to_host(shdr->sh_type)) {
  159. return shdr;
  160. }
  161. }
  162. return NULL;
  163. }
  164. static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
  165. {
  166. int j;
  167. ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
  168. for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
  169. if (type == byteswap32_to_host(phdr->p_type)) {
  170. return phdr;
  171. }
  172. }
  173. return NULL;
  174. }
  175. /* Returns value if return_val==1, ptr otherwise */
  176. static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
  177. ElfW(Ehdr) *ehdr, int return_val)
  178. {
  179. ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
  180. unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
  181. for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
  182. if (key == byteswap_to_host(dynp->d_tag)) {
  183. if (return_val == 1)
  184. return (void *)byteswap_to_host(dynp->d_un.d_val);
  185. else
  186. return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
  187. }
  188. }
  189. return NULL;
  190. }
  191. static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
  192. {
  193. ElfW(Dyn) *dyns;
  194. for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
  195. if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
  196. char *strtab;
  197. strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  198. return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
  199. }
  200. }
  201. return NULL;
  202. }
  203. static int check_elf_header(ElfW(Ehdr) *const ehdr)
  204. {
  205. if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
  206. /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
  207. || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5)
  208. || ehdr->e_ident[EI_VERSION] != EV_CURRENT
  209. ) {
  210. return 1;
  211. }
  212. /* Check if the target endianness matches the host's endianness */
  213. byteswap = !(ehdr->e_ident[5] == ELFDATAM);
  214. /* Be very lazy, and only byteswap the stuff we use */
  215. if (byteswap) {
  216. ehdr->e_type = bswap_16(ehdr->e_type);
  217. ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
  218. ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
  219. ehdr->e_phnum = bswap_16(ehdr->e_phnum);
  220. ehdr->e_shnum = bswap_16(ehdr->e_shnum);
  221. }
  222. return 0;
  223. }
  224. #ifdef __LDSO_CACHE_SUPPORT__
  225. static caddr_t cache_addr = NULL;
  226. static size_t cache_size = 0;
  227. static int map_cache(void)
  228. {
  229. int fd;
  230. struct stat st;
  231. header_t *header;
  232. libentry_t *libent;
  233. int i, strtabsize;
  234. if (cache_addr == (caddr_t) - 1)
  235. return -1;
  236. else if (cache_addr != NULL)
  237. return 0;
  238. if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) {
  239. fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
  240. cache_addr = (caddr_t) - 1; /* so we won't try again */
  241. return -1;
  242. }
  243. cache_size = st.st_size;
  244. cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
  245. close(fd);
  246. if (cache_addr == MAP_FAILED) {
  247. fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
  248. return -1;
  249. }
  250. header = (header_t *) cache_addr;
  251. if (cache_size < sizeof(header_t)
  252. || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
  253. || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
  254. || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
  255. || cache_addr[cache_size - 1] != '\0')
  256. {
  257. fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
  258. goto fail;
  259. }
  260. strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
  261. libent = (libentry_t *) & header[1];
  262. for (i = 0; i < header->nlibs; i++) {
  263. if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
  264. fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
  265. goto fail;
  266. }
  267. }
  268. return 0;
  269. fail:
  270. munmap(cache_addr, cache_size);
  271. cache_addr = (caddr_t) - 1;
  272. return -1;
  273. }
  274. static int unmap_cache(void)
  275. {
  276. if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
  277. return -1;
  278. #if 1
  279. munmap(cache_addr, cache_size);
  280. cache_addr = NULL;
  281. #endif
  282. return 0;
  283. }
  284. #else
  285. static __inline__ void map_cache(void)
  286. {
  287. }
  288. static __inline__ void unmap_cache(void)
  289. {
  290. }
  291. #endif
  292. /* This function's behavior must exactly match that
  293. * in uClibc/ldso/ldso/dl-elf.c */
  294. static void search_for_named_library(char *name, char *result,
  295. const char *path_list)
  296. {
  297. int i, count = 1;
  298. char *path, *path_n;
  299. struct stat filestat;
  300. /* We need a writable copy of this string */
  301. path = strdup(path_list);
  302. if (!path) {
  303. fprintf(stderr, "%s: Out of memory!\n", __func__);
  304. exit(EXIT_FAILURE);
  305. }
  306. /* Eliminate all double //s */
  307. path_n = path;
  308. while ((path_n = strstr(path_n, "//"))) {
  309. i = strlen(path_n);
  310. memmove(path_n, path_n + 1, i - 1);
  311. *(path_n + i - 1) = '\0';
  312. }
  313. /* Replace colons with zeros in path_list and count them */
  314. for (i = strlen(path); i > 0; i--) {
  315. if (path[i] == ':') {
  316. path[i] = 0;
  317. count++;
  318. }
  319. }
  320. path_n = path;
  321. for (i = 0; i < count; i++) {
  322. strcpy(result, path_n);
  323. strcat(result, "/");
  324. strcat(result, name);
  325. if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
  326. free(path);
  327. return;
  328. }
  329. path_n += (strlen(path_n) + 1);
  330. }
  331. free(path);
  332. *result = '\0';
  333. }
  334. static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic,
  335. int is_suid, struct library *lib)
  336. {
  337. char *buf;
  338. char *path;
  339. struct stat filestat;
  340. /* If this is a fully resolved name, our job is easy */
  341. if (stat(lib->name, &filestat) == 0) {
  342. lib->path = strdup(lib->name);
  343. return;
  344. }
  345. /* We need some elbow room here. Make some room... */
  346. buf = malloc(1024);
  347. if (!buf) {
  348. fprintf(stderr, "%s: Out of memory!\n", __func__);
  349. exit(EXIT_FAILURE);
  350. }
  351. /* This function must match the behavior of _dl_load_shared_library
  352. * in readelflib1.c or things won't work out as expected... */
  353. /* The ABI specifies that RPATH is searched first, so do that now. */
  354. path = elf_find_rpath(ehdr, dynamic);
  355. if (path) {
  356. search_for_named_library(lib->name, buf, path);
  357. if (*buf != '\0') {
  358. lib->path = buf;
  359. return;
  360. }
  361. }
  362. /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
  363. * Since this app doesn't actually run an executable I will skip
  364. * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
  365. if (is_suid == 1)
  366. path = NULL;
  367. else
  368. path = getenv("LD_LIBRARY_PATH");
  369. if (path) {
  370. search_for_named_library(lib->name, buf, path);
  371. if (*buf != '\0') {
  372. lib->path = buf;
  373. return;
  374. }
  375. }
  376. #ifdef __LDSO_CACHE_SUPPORT__
  377. if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
  378. int i;
  379. header_t *header = (header_t *) cache_addr;
  380. libentry_t *libent = (libentry_t *) & header[1];
  381. char *strs = (char *)&libent[header->nlibs];
  382. for (i = 0; i < header->nlibs; i++) {
  383. if ((libent[i].flags == LIB_ELF ||
  384. libent[i].flags == LIB_ELF_LIBC0 ||
  385. libent[i].flags == LIB_ELF_LIBC5) &&
  386. strcmp(lib->name, strs + libent[i].sooffset) == 0)
  387. {
  388. lib->path = strdup(strs + libent[i].liboffset);
  389. return;
  390. }
  391. }
  392. }
  393. #endif
  394. /* Next look for libraries wherever the shared library
  395. * loader was installed -- this is usually where we
  396. * should find things... */
  397. if (interp_dir) {
  398. search_for_named_library(lib->name, buf, interp_dir);
  399. if (*buf != '\0') {
  400. lib->path = buf;
  401. return;
  402. }
  403. }
  404. /* Lastly, search the standard list of paths for the library.
  405. This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
  406. path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
  407. #ifndef __LDSO_CACHE_SUPPORT__
  408. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  409. #endif
  410. ;
  411. search_for_named_library(lib->name, buf, path);
  412. if (*buf != '\0') {
  413. lib->path = buf;
  414. } else {
  415. free(buf);
  416. lib->path = not_found;
  417. }
  418. }
  419. static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
  420. {
  421. char *tmp, *tmp1, *tmp2;
  422. struct library *cur, *newlib = lib_list;
  423. if (!s || !strlen(s))
  424. return 1;
  425. tmp = s;
  426. while (*tmp) {
  427. if (*tmp == '/')
  428. s = tmp + 1;
  429. tmp++;
  430. }
  431. /* We add ldso elsewhere */
  432. if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
  433. int len = strlen(interp_dir);
  434. if (strcmp(s, interp_name + 1 + len) == 0)
  435. return 1;
  436. }
  437. for (cur = lib_list; cur; cur = cur->next) {
  438. /* Check if this library is already in the list */
  439. tmp1 = tmp2 = cur->name;
  440. if (!cur->name)
  441. continue;
  442. while (*tmp1) {
  443. if (*tmp1 == '/')
  444. tmp2 = tmp1 + 1;
  445. tmp1++;
  446. }
  447. if (strcmp(tmp2, s) == 0) {
  448. /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
  449. return 0;
  450. }
  451. }
  452. /* Ok, this lib needs to be added to the list */
  453. newlib = malloc(sizeof(struct library));
  454. if (!newlib)
  455. return 1;
  456. newlib->name = malloc(strlen(s) + 1);
  457. strcpy(newlib->name, s);
  458. newlib->resolved = 0;
  459. newlib->path = NULL;
  460. newlib->next = NULL;
  461. /* Now try and locate where this library might be living... */
  462. locate_library_file(ehdr, dynamic, is_setuid, newlib);
  463. /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
  464. if (!lib_list) {
  465. lib_list = newlib;
  466. } else {
  467. for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
  468. cur->next = newlib;
  469. }
  470. return 0;
  471. }
  472. static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
  473. {
  474. ElfW(Dyn) *dyns;
  475. for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
  476. if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
  477. char *strtab;
  478. strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  479. add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
  480. }
  481. }
  482. }
  483. #ifdef __LDSO_LDD_SUPPORT__
  484. static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
  485. {
  486. ElfW(Phdr) *phdr;
  487. if (interpreter_already_found == 1)
  488. return NULL;
  489. phdr = elf_find_phdr_type(PT_INTERP, ehdr);
  490. if (phdr) {
  491. struct library *cur, *newlib = NULL;
  492. char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
  493. char *tmp, *tmp1;
  494. interp_name = strdup(s);
  495. interp_dir = strdup(s);
  496. tmp = strrchr(interp_dir, '/');
  497. if (tmp)
  498. *tmp = '\0';
  499. else {
  500. free(interp_dir);
  501. interp_dir = interp_name;
  502. }
  503. tmp1 = tmp = s;
  504. while (*tmp) {
  505. if (*tmp == '/')
  506. tmp1 = tmp + 1;
  507. tmp++;
  508. }
  509. for (cur = lib_list; cur; cur = cur->next) {
  510. /* Check if this library is already in the list */
  511. if (!tmp1 || !cur->name)
  512. return NULL;
  513. if (strcmp(cur->name, tmp1) == 0) {
  514. /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
  515. newlib = cur;
  516. free(newlib->name);
  517. if (newlib->path != not_found) {
  518. free(newlib->path);
  519. }
  520. newlib->name = NULL;
  521. newlib->path = NULL;
  522. break;
  523. }
  524. }
  525. if (newlib == NULL)
  526. newlib = malloc(sizeof(struct library));
  527. if (!newlib)
  528. return NULL;
  529. newlib->name = malloc(strlen(s) + 1);
  530. strcpy(newlib->name, s);
  531. newlib->path = strdup(newlib->name);
  532. newlib->resolved = 1;
  533. newlib->next = NULL;
  534. #if 0
  535. /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
  536. if (!lib_list) {
  537. lib_list = newlib;
  538. } else {
  539. for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
  540. cur->next = newlib;
  541. }
  542. #endif
  543. interpreter_already_found = 1;
  544. return newlib;
  545. }
  546. return NULL;
  547. }
  548. #endif /* __LDSO_LDD_SUPPORT__ */
  549. /* map the .so, and locate interesting pieces */
  550. /*
  551. #warning "There may be two warnings here about vfork() clobbering, ignore them"
  552. */
  553. static int find_dependencies(char *filename)
  554. {
  555. int is_suid = 0;
  556. FILE *thefile;
  557. struct stat statbuf;
  558. ElfW(Ehdr) *ehdr = NULL;
  559. ElfW(Shdr) *dynsec = NULL;
  560. ElfW(Dyn) *dynamic = NULL;
  561. #ifdef __LDSO_LDD_SUPPORT__
  562. struct library *interp;
  563. #endif
  564. if (filename == not_found)
  565. return 0;
  566. if (!filename) {
  567. fprintf(stderr, "No filename specified.\n");
  568. return -1;
  569. }
  570. if (!(thefile = fopen(filename, "r"))) {
  571. perror(filename);
  572. return -1;
  573. }
  574. if (fstat(fileno(thefile), &statbuf) < 0) {
  575. perror(filename);
  576. fclose(thefile);
  577. return -1;
  578. }
  579. if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
  580. goto foo;
  581. if (!S_ISREG(statbuf.st_mode))
  582. goto foo;
  583. /* mmap the file to make reading stuff from it effortless */
  584. ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
  585. if (ehdr == MAP_FAILED) {
  586. fclose(thefile);
  587. fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno));
  588. return -1;
  589. }
  590. foo:
  591. fclose(thefile);
  592. /* Check if this looks like a legit ELF file */
  593. if (check_elf_header(ehdr)) {
  594. fprintf(stderr, "%s: not an ELF file.\n", filename);
  595. return -1;
  596. }
  597. /* Check if this is the right kind of ELF file */
  598. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
  599. fprintf(stderr, "%s: not a dynamic executable\n", filename);
  600. return -1;
  601. }
  602. if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
  603. if (statbuf.st_mode & S_ISUID)
  604. is_suid = 1;
  605. if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  606. is_suid = 1;
  607. /* FIXME */
  608. if (is_suid)
  609. fprintf(stderr, "%s: is setuid\n", filename);
  610. }
  611. interpreter_already_found = 0;
  612. #ifdef __LDSO_LDD_SUPPORT__
  613. interp = find_elf_interpreter(ehdr);
  614. if (interp
  615. && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
  616. && ehdr->e_ident[EI_CLASS] == ELFCLASSM
  617. && ehdr->e_ident[EI_DATA] == ELFDATAM
  618. && ehdr->e_ident[EI_VERSION] == EV_CURRENT
  619. && MATCH_MACHINE(ehdr->e_machine))
  620. {
  621. struct stat st;
  622. if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) {
  623. pid_t pid;
  624. int status;
  625. static const char *const environment[] = {
  626. "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
  627. "SHELL=/bin/sh",
  628. "LD_TRACE_LOADED_OBJECTS=1",
  629. NULL
  630. };
  631. # ifdef __LDSO_STANDALONE_SUPPORT__
  632. char * lib_path = getenv("LD_LIBRARY_PATH");
  633. /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
  634. static char *ext_environment[ARRAY_SIZE(environment) + 1];
  635. char **envp = (char **) environment;
  636. if (lib_path) {
  637. /*
  638. * If the LD_LIBRARY_PATH is set, it needs to include it
  639. * into the environment for the new process to be spawned
  640. */
  641. char ** eenvp = (char **) ext_environment;
  642. /* Copy the N-1 environment's entries */
  643. while (*envp)
  644. *eenvp++=*envp++;
  645. /* Make room for LD_LIBRARY_PATH */
  646. *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=")
  647. + strlen(lib_path));
  648. strcpy(*eenvp, "LD_LIBRARY_PATH=");
  649. strcat(*eenvp, lib_path);
  650. lib_path = *eenvp;
  651. /* ext_environment[size] is already NULL */
  652. /* Use the extended environment */
  653. envp = ext_environment;
  654. }
  655. if ((pid = vfork()) == 0) {
  656. /*
  657. * Force to use the standard dynamic linker in stand-alone mode.
  658. * It will fails at runtime if support is not actually available
  659. */
  660. execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp);
  661. _exit(0xdead);
  662. }
  663. # else
  664. if ((pid = vfork()) == 0) {
  665. /* Cool, it looks like we should be able to actually
  666. * run this puppy. Do so now... */
  667. execle(filename, filename, NULL, environment);
  668. _exit(0xdead);
  669. }
  670. # endif
  671. /* Wait till it returns */
  672. waitpid(pid, &status, 0);
  673. # ifdef __LDSO_STANDALONE_SUPPORT__
  674. /* Do not leak */
  675. free(lib_path);
  676. # endif
  677. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  678. return 1;
  679. }
  680. /* If the exec failed, we fall through to trying to find
  681. * all the needed libraries ourselves by rummaging about
  682. * in the ELF headers... */
  683. }
  684. }
  685. #endif
  686. dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
  687. if (dynsec) {
  688. dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
  689. find_needed_libraries(ehdr, dynamic, is_suid);
  690. }
  691. return 0;
  692. }
  693. int main(int argc, char **argv)
  694. {
  695. int multi = 0;
  696. int got_em_all = 1;
  697. char *filename = NULL;
  698. struct library *cur;
  699. if (argc < 2) {
  700. fprintf(stderr, "ldd: missing file arguments\n"
  701. "Try `ldd --help' for more information.\n");
  702. exit(EXIT_FAILURE);
  703. }
  704. if (argc > 2)
  705. multi++;
  706. while (--argc > 0) {
  707. ++argv;
  708. if (strcmp(*argv, "--") == 0) {
  709. /* Ignore "--" */
  710. continue;
  711. }
  712. if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
  713. fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
  714. "\t--help\t\tprint this help and exit\n");
  715. exit(EXIT_SUCCESS);
  716. }
  717. filename = *argv;
  718. if (!filename) {
  719. fprintf(stderr, "No filename specified.\n");
  720. exit(EXIT_FAILURE);
  721. }
  722. if (multi) {
  723. printf("%s:\n", *argv);
  724. }
  725. map_cache();
  726. if (find_dependencies(filename) != 0)
  727. continue;
  728. while (got_em_all) {
  729. got_em_all = 0;
  730. /* Keep walking the list till everybody is resolved */
  731. for (cur = lib_list; cur; cur = cur->next) {
  732. if (cur->resolved == 0 && cur->path) {
  733. got_em_all = 1;
  734. printf("checking sub-depends for '%s'\n", cur->path);
  735. find_dependencies(cur->path);
  736. cur->resolved = 1;
  737. }
  738. }
  739. }
  740. unmap_cache();
  741. /* Print the list */
  742. got_em_all = 0;
  743. for (cur = lib_list; cur; cur = cur->next) {
  744. got_em_all = 1;
  745. printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
  746. }
  747. if (interp_name && interpreter_already_found == 1)
  748. printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
  749. else
  750. printf("\tnot a dynamic executable\n");
  751. for (cur = lib_list; cur; cur = cur->next) {
  752. free(cur->name);
  753. cur->name = NULL;
  754. if (cur->path && cur->path != not_found) {
  755. free(cur->path);
  756. cur->path = NULL;
  757. }
  758. }
  759. lib_list = NULL;
  760. }
  761. return 0;
  762. }