ldd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * A small little ldd implementation for uClibc
  4. *
  5. * Copyright (C) 2000 by Lineo, inc and Erik Andersen
  6. * Copyright (C) 2000-2002 Erik Andersen <andersee@debian.org>
  7. *
  8. * Several functions in this file (specifically, elf_find_section_type(),
  9. * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
  10. * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
  11. * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC
  12. * (GPL2).
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #define _GNU_SOURCE
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <sys/mman.h>
  36. #include <sys/stat.h>
  37. #include <sys/types.h>
  38. #include "bswap.h"
  39. #if defined (sun)
  40. #include "link.h"
  41. #else
  42. #include "elf.h"
  43. #endif
  44. #ifdef DMALLOC
  45. #include <dmalloc.h>
  46. #endif
  47. struct library {
  48. char *name;
  49. int resolved;
  50. char *path;
  51. struct library *next;
  52. };
  53. struct library *lib_list = NULL;
  54. char not_found[] = "not found";
  55. char *interp = NULL;
  56. char *interp_dir = NULL;
  57. int byteswap;
  58. inline uint32_t byteswap32_to_host(uint32_t value)
  59. {
  60. if (byteswap==1) {
  61. return(bswap_32(value));
  62. } else {
  63. return(value);
  64. }
  65. }
  66. Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr)
  67. {
  68. int j;
  69. Elf32_Shdr *shdr;
  70. shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr);
  71. for (j = ehdr->e_shnum; --j>=0; ++shdr) {
  72. if (key==byteswap32_to_host(shdr->sh_type)) {
  73. return shdr;
  74. }
  75. }
  76. return NULL;
  77. }
  78. Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr)
  79. {
  80. int j;
  81. Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr);
  82. for (j = ehdr->e_phnum; --j>=0; ++phdr) {
  83. if (type==byteswap32_to_host(phdr->p_type)) {
  84. return phdr;
  85. }
  86. }
  87. return NULL;
  88. }
  89. /* Returns value if return_val==1, ptr otherwise */
  90. void * elf_find_dynamic(int const key, Elf32_Dyn *dynp,
  91. Elf32_Ehdr *ehdr, int return_val)
  92. {
  93. Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
  94. unsigned tx_reloc = byteswap32_to_host(pt_text->p_vaddr) - byteswap32_to_host(pt_text->p_offset);
  95. for (; DT_NULL!=byteswap32_to_host(dynp->d_tag); ++dynp) {
  96. if (key == byteswap32_to_host(dynp->d_tag)) {
  97. if (return_val == 1)
  98. return (void *)(intptr_t)byteswap32_to_host(dynp->d_un.d_val);
  99. else
  100. return (void *)(byteswap32_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr );
  101. }
  102. }
  103. return NULL;
  104. }
  105. int check_elf_header(Elf32_Ehdr *const ehdr)
  106. {
  107. if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||
  108. ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
  109. ehdr->e_ident[EI_VERSION] != EV_CURRENT)
  110. {
  111. return 1;
  112. }
  113. /* Check if the target endianness matches the host's endianness */
  114. byteswap = 0;
  115. #if __BYTE_ORDER == __LITTLE_ENDIAN
  116. if (ehdr->e_ident[5] == ELFDATA2MSB) {
  117. /* Ick -- we will have to byte-swap everything */
  118. byteswap = 1;
  119. }
  120. #elif __BYTE_ORDER == __BIG_ENDIAN
  121. if (ehdr->e_ident[5] == ELFDATA2LSB) {
  122. byteswap = 1;
  123. }
  124. #else
  125. #error Unknown host byte order!
  126. #endif
  127. /* Be vary lazy, and only byteswap the stuff we use */
  128. if (byteswap==1) {
  129. ehdr->e_type=bswap_16(ehdr->e_type);
  130. ehdr->e_phoff=bswap_32(ehdr->e_phoff);
  131. ehdr->e_shoff=bswap_32(ehdr->e_shoff);
  132. ehdr->e_phnum=bswap_16(ehdr->e_phnum);
  133. ehdr->e_shnum=bswap_16(ehdr->e_shnum);
  134. }
  135. return 0;
  136. }
  137. /* This function's behavior must exactly match that
  138. * in uClibc/ldso/d-link/readelflib1.c */
  139. static void search_for_named_library(char *name, char *result, const char *path_list)
  140. {
  141. int i, count = 1;
  142. char *path, *path_n;
  143. struct stat filestat;
  144. /* We need a writable copy of this string */
  145. path = strdup(path_list);
  146. if (!path) {
  147. fprintf(stderr, "Out of memory!\n");
  148. exit(EXIT_FAILURE);
  149. }
  150. /* Eliminate all double //s */
  151. path_n=path;
  152. while((path_n=strstr(path_n, "//"))) {
  153. i = strlen(path_n);
  154. memmove(path_n, path_n+1, i-1);
  155. }
  156. /* Replace colons with zeros in path_list and count them */
  157. for(i=strlen(path); i > 0; i--) {
  158. if (path[i]==':') {
  159. path[i]=0;
  160. count++;
  161. }
  162. }
  163. path_n = path;
  164. for (i = 0; i < count; i++) {
  165. strcpy(result, path_n);
  166. strcat(result, "/");
  167. strcat(result, name);
  168. if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
  169. free(path);
  170. return;
  171. }
  172. path_n += (strlen(path_n) + 1);
  173. }
  174. free(path);
  175. *result = '\0';
  176. }
  177. void locate_library_file(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab,
  178. int is_suid, struct library *lib)
  179. {
  180. char *buf;
  181. char *path;
  182. struct stat filestat;
  183. /* If this is a fully resolved name, our job is easy */
  184. if (stat (lib->name, &filestat) == 0) {
  185. lib->path = lib->name;
  186. return;
  187. }
  188. /* We need some elbow room here. Make some room...*/
  189. buf = malloc(1024);
  190. if (!buf) {
  191. fprintf(stderr, "Out of memory!\n");
  192. exit(EXIT_FAILURE);
  193. }
  194. /* This function must match the behavior of _dl_load_shared_library
  195. * in readelflib1.c or things won't work out as expected... */
  196. /* The ABI specifies that RPATH is searched first, so do that now. */
  197. path = (char *)elf_find_dynamic(DT_RPATH, dynamic, ehdr, 0);
  198. if (path) {
  199. search_for_named_library(lib->name, buf, path);
  200. if (*buf != '\0') {
  201. lib->path = buf;
  202. return;
  203. }
  204. }
  205. /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
  206. * Since this app doesn't actually run an executable I will skip
  207. * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
  208. if (is_suid==1)
  209. path = NULL;
  210. else
  211. path = getenv("LD_LIBRARY_PATH");
  212. if (path) {
  213. search_for_named_library(lib->name, buf, path);
  214. if (*buf != '\0') {
  215. lib->path = buf;
  216. return;
  217. }
  218. }
  219. #ifdef USE_CACHE
  220. /* FIXME -- add code to check the Cache here */
  221. #endif
  222. /* Next look for libraries wherever the shared library
  223. * loader was installed -- this is usually where we
  224. * should find things... */
  225. if (interp_dir) {
  226. search_for_named_library(lib->name, buf, interp_dir);
  227. if (*buf != '\0') {
  228. lib->path = buf;
  229. return;
  230. }
  231. }
  232. /* Lastly, search the standard list of paths for the library.
  233. This list must exactly match the list in uClibc/ldso/d-link/readelflib1.c */
  234. path = UCLIBC_TARGET_PREFIX "/usr/lib:"
  235. UCLIBC_TARGET_PREFIX "/lib:"
  236. UCLIBC_DEVEL_PREFIX "/lib:"
  237. UCLIBC_BUILD_DIR "/lib:"
  238. "/usr/lib:"
  239. "/lib";
  240. search_for_named_library(lib->name, buf, path);
  241. if (*buf != '\0') {
  242. lib->path = buf;
  243. } else {
  244. free(buf);
  245. lib->path = not_found;
  246. }
  247. }
  248. static int add_library(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid, char *s)
  249. {
  250. char *tmp, *tmp1, *tmp2;
  251. struct library *cur, *newlib=lib_list;
  252. if (!s || !strlen(s))
  253. return 1;
  254. tmp = s;
  255. while (*tmp) {
  256. if (*tmp == '/')
  257. s = tmp + 1;
  258. tmp++;
  259. }
  260. /* We add libc.so.0 elsewhere */
  261. if ((tmp=strrchr(interp, '/')) != NULL)
  262. {
  263. int len = strlen(interp_dir);
  264. if (strcmp(s, interp+1+len)==0)
  265. return 1;
  266. }
  267. for (cur = lib_list; cur; cur=cur->next) {
  268. /* Check if this library is already in the list */
  269. tmp1 = tmp2 = cur->name;
  270. while (*tmp1) {
  271. if (*tmp1 == '/')
  272. tmp2 = tmp1 + 1;
  273. tmp1++;
  274. }
  275. if(strcmp(tmp2, s)==0) {
  276. //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
  277. return 0;
  278. }
  279. }
  280. /* Ok, this lib needs to be added to the list */
  281. newlib = malloc(sizeof(struct library));
  282. if (!newlib)
  283. return 1;
  284. newlib->name = malloc(strlen(s)+1);
  285. strcpy(newlib->name, s);
  286. newlib->resolved = 0;
  287. newlib->path = NULL;
  288. newlib->next = NULL;
  289. /* Now try and locate where this library might be living... */
  290. locate_library_file(ehdr, dynamic, strtab, is_setuid, newlib);
  291. //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
  292. if (!lib_list) {
  293. lib_list = newlib;
  294. } else {
  295. for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
  296. cur->next = newlib;
  297. }
  298. return 0;
  299. }
  300. static void find_needed_libraries(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid)
  301. {
  302. Elf32_Dyn *dyns;
  303. for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
  304. if (DT_NEEDED == byteswap32_to_host(dyns->d_tag)) {
  305. add_library(ehdr, dynamic, strtab, is_setuid, (char*)strtab +
  306. byteswap32_to_host(dyns->d_un.d_val));
  307. }
  308. }
  309. }
  310. static void find_elf_interpreter(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid)
  311. {
  312. static int been_there_done_that=0;
  313. Elf32_Phdr *phdr;
  314. if (been_there_done_that==1)
  315. return;
  316. been_there_done_that=1;
  317. phdr = elf_find_phdr_type(PT_INTERP, ehdr);
  318. if (phdr) {
  319. struct library *cur, *newlib=NULL;
  320. char *s = (char*)ehdr + byteswap32_to_host(phdr->p_offset);
  321. char *tmp, *tmp1;
  322. interp = strdup(s);
  323. interp_dir = strdup(s);
  324. tmp = strrchr(interp_dir, '/');
  325. if (*tmp)
  326. *tmp = '\0';
  327. else {
  328. free(interp_dir);
  329. interp_dir = interp;
  330. }
  331. tmp1 = tmp = s;
  332. while (*tmp) {
  333. if (*tmp == '/')
  334. tmp1 = tmp + 1;
  335. tmp++;
  336. }
  337. for (cur = lib_list; cur; cur=cur->next) {
  338. /* Check if this library is already in the list */
  339. if(strcmp(cur->name, tmp1)==0) {
  340. //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
  341. newlib = cur;
  342. free(newlib->name);
  343. free(newlib->path);
  344. return;
  345. }
  346. }
  347. if (newlib == NULL)
  348. newlib = malloc(sizeof(struct library));
  349. if (!newlib)
  350. return;
  351. newlib->name = malloc(strlen(s)+1);
  352. strcpy(newlib->name, s);
  353. newlib->path = newlib->name;
  354. newlib->resolved = 1;
  355. newlib->next = NULL;
  356. #if 0
  357. //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
  358. if (!lib_list) {
  359. lib_list = newlib;
  360. } else {
  361. for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
  362. cur->next = newlib;
  363. }
  364. #endif
  365. }
  366. }
  367. /* map the .so, and locate interesting pieces */
  368. int find_dependancies(char* filename)
  369. {
  370. int is_suid = 0;
  371. FILE *thefile;
  372. struct stat statbuf;
  373. char *dynstr=NULL;
  374. Elf32_Ehdr *ehdr = NULL;
  375. Elf32_Shdr *dynsec = NULL;
  376. Elf32_Dyn *dynamic = NULL;
  377. if (filename == not_found)
  378. return 0;
  379. if (!filename) {
  380. fprintf(stderr, "No filename specified.\n");
  381. exit(EXIT_FAILURE);
  382. }
  383. if (!(thefile = fopen(filename, "r"))) {
  384. perror(filename);
  385. exit(EXIT_FAILURE);
  386. }
  387. if (fstat(fileno(thefile), &statbuf) < 0) {
  388. perror(filename);
  389. exit(EXIT_FAILURE);
  390. }
  391. if (statbuf.st_size < sizeof(Elf32_Ehdr))
  392. goto foo;
  393. /* mmap the file to make reading stuff from it effortless */
  394. ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size,
  395. PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
  396. foo:
  397. /* Check if this looks like a legit ELF file */
  398. if (check_elf_header(ehdr)) {
  399. fprintf(stderr, "%s: not an ELF file.\n", filename);
  400. exit(EXIT_FAILURE);
  401. }
  402. /* Check if this is the right kind of ELF file */
  403. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
  404. fprintf(stderr, "%s: not a dynamic executable\n", filename);
  405. exit(EXIT_FAILURE);
  406. }
  407. if (ehdr->e_type == ET_EXEC) {
  408. if (statbuf.st_mode & S_ISUID)
  409. is_suid = 1;
  410. if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  411. is_suid = 1;
  412. /* FIXME */
  413. if (is_suid)
  414. fprintf(stderr, "%s: is setuid\n", filename);
  415. }
  416. dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
  417. find_elf_interpreter(ehdr, dynamic, dynstr, is_suid);
  418. if (dynsec) {
  419. dynamic = (Elf32_Dyn*)(byteswap32_to_host(dynsec->sh_offset) + (intptr_t)ehdr);
  420. dynstr = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  421. find_needed_libraries(ehdr, dynamic, dynstr, is_suid);
  422. }
  423. return 0;
  424. }
  425. int main( int argc, char** argv)
  426. {
  427. int multi=0;
  428. int got_em_all=1;
  429. char *filename = NULL;
  430. struct library *cur;
  431. if (argc < 2) {
  432. fprintf(stderr, "ldd: missing file arguments\n");
  433. fprintf(stderr, "Try `ldd --help' for more information.\n");
  434. exit(EXIT_FAILURE);
  435. }
  436. if (argc > 2) {
  437. multi++;
  438. }
  439. while (--argc > 0) {
  440. ++argv;
  441. if(strcmp(*argv, "--")==0) {
  442. /* Ignore "--" */
  443. continue;
  444. }
  445. if(strcmp(*argv, "--help")==0) {
  446. fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
  447. fprintf(stderr, "\t--help\t\tprint this help and exit\n");
  448. exit(EXIT_FAILURE);
  449. }
  450. filename=*argv;
  451. if (!filename) {
  452. fprintf(stderr, "No filename specified.\n");
  453. exit(EXIT_FAILURE);
  454. }
  455. find_dependancies(filename);
  456. while(got_em_all) {
  457. got_em_all=0;
  458. /* Keep walking the list till everybody is resolved */
  459. for (cur = lib_list; cur; cur=cur->next) {
  460. if (cur->resolved == 0 && cur->path) {
  461. got_em_all=1;
  462. //printf("checking sub-depends for '%s\n", cur->path);
  463. find_dependancies(cur->path);
  464. cur->resolved = 1;
  465. }
  466. }
  467. }
  468. /* Print the list */
  469. got_em_all=0;
  470. if (multi) {
  471. printf("%s:\n", *argv);
  472. }
  473. for (cur = lib_list; cur; cur=cur->next) {
  474. got_em_all=1;
  475. printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
  476. }
  477. if (interp_dir && got_em_all==1)
  478. printf("\t%s => %s (0x00000000)\n", interp, interp);
  479. if (got_em_all==0)
  480. printf("\tnot a dynamic executable\n");
  481. }
  482. return 0;
  483. }