ldd.c 21 KB

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