ldd.c 21 KB

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