ldd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. #if defined(__arm__)
  48. #define MATCH_MACHINE(x) (x == EM_ARM)
  49. #define ELFCLASSM ELFCLASS32
  50. #endif
  51. #if defined(__s390__)
  52. #define MATCH_MACHINE(x) (x == EM_S390)
  53. #define ELFCLASSM ELFCLASS32
  54. #endif
  55. #if defined(__i386__)
  56. #ifndef EM_486
  57. #define MATCH_MACHINE(x) (x == EM_386)
  58. #else
  59. #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
  60. #endif
  61. #define ELFCLASSM ELFCLASS32
  62. #endif
  63. #if defined(__mc68000__)
  64. #define MATCH_MACHINE(x) (x == EM_68K)
  65. #define ELFCLASSM ELFCLASS32
  66. #endif
  67. #if defined(__mips__)
  68. #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
  69. #define ELFCLASSM ELFCLASS32
  70. #endif
  71. #if 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. #endif
  78. #if defined (__v850e__)
  79. #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
  80. #define ELFCLASSM ELFCLASS32
  81. #endif
  82. #ifndef MATCH_MACHINE
  83. #warning "You really should add a MATCH_MACHINE() macro for your architecture"
  84. #endif
  85. #if __BYTE_ORDER == __LITTLE_ENDIAN
  86. #define ELFDATAM ELFDATA2LSB
  87. #elif __BYTE_ORDER == __BIG_ENDIAN
  88. #define ELFDATAM ELFDATA2MSB
  89. #endif
  90. struct library {
  91. char *name;
  92. int resolved;
  93. char *path;
  94. struct library *next;
  95. };
  96. struct library *lib_list = NULL;
  97. char not_found[] = "not found";
  98. char *interp = NULL;
  99. char *interp_dir = NULL;
  100. int byteswap;
  101. inline uint32_t byteswap32_to_host(uint32_t value)
  102. {
  103. if (byteswap==1) {
  104. return(bswap_32(value));
  105. } else {
  106. return(value);
  107. }
  108. }
  109. Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr)
  110. {
  111. int j;
  112. Elf32_Shdr *shdr;
  113. shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr);
  114. for (j = ehdr->e_shnum; --j>=0; ++shdr) {
  115. if (key==byteswap32_to_host(shdr->sh_type)) {
  116. return shdr;
  117. }
  118. }
  119. return NULL;
  120. }
  121. Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr)
  122. {
  123. int j;
  124. Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr);
  125. for (j = ehdr->e_phnum; --j>=0; ++phdr) {
  126. if (type==byteswap32_to_host(phdr->p_type)) {
  127. return phdr;
  128. }
  129. }
  130. return NULL;
  131. }
  132. /* Returns value if return_val==1, ptr otherwise */
  133. void * elf_find_dynamic(int const key, Elf32_Dyn *dynp,
  134. Elf32_Ehdr *ehdr, int return_val)
  135. {
  136. Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
  137. unsigned tx_reloc = byteswap32_to_host(pt_text->p_vaddr) - byteswap32_to_host(pt_text->p_offset);
  138. for (; DT_NULL!=byteswap32_to_host(dynp->d_tag); ++dynp) {
  139. if (key == byteswap32_to_host(dynp->d_tag)) {
  140. if (return_val == 1)
  141. return (void *)(intptr_t)byteswap32_to_host(dynp->d_un.d_val);
  142. else
  143. return (void *)(byteswap32_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr );
  144. }
  145. }
  146. return NULL;
  147. }
  148. int check_elf_header(Elf32_Ehdr *const ehdr)
  149. {
  150. if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||
  151. ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
  152. ehdr->e_ident[EI_VERSION] != EV_CURRENT)
  153. {
  154. return 1;
  155. }
  156. /* Check if the target endianness matches the host's endianness */
  157. byteswap = 0;
  158. #if __BYTE_ORDER == __LITTLE_ENDIAN
  159. if (ehdr->e_ident[5] == ELFDATA2MSB) {
  160. /* Ick -- we will have to byte-swap everything */
  161. byteswap = 1;
  162. }
  163. #elif __BYTE_ORDER == __BIG_ENDIAN
  164. if (ehdr->e_ident[5] == ELFDATA2LSB) {
  165. /* Ick -- we will have to byte-swap everything */
  166. byteswap = 1;
  167. }
  168. #else
  169. #error Unknown host byte order!
  170. #endif
  171. /* Be vary lazy, and only byteswap the stuff we use */
  172. if (byteswap==1) {
  173. ehdr->e_type=bswap_16(ehdr->e_type);
  174. ehdr->e_phoff=bswap_32(ehdr->e_phoff);
  175. ehdr->e_shoff=bswap_32(ehdr->e_shoff);
  176. ehdr->e_phnum=bswap_16(ehdr->e_phnum);
  177. ehdr->e_shnum=bswap_16(ehdr->e_shnum);
  178. }
  179. return 0;
  180. }
  181. /* This function's behavior must exactly match that
  182. * in uClibc/ldso/d-link/readelflib1.c */
  183. static void search_for_named_library(char *name, char *result, const char *path_list)
  184. {
  185. int i, count = 1;
  186. char *path, *path_n;
  187. struct stat filestat;
  188. /* We need a writable copy of this string */
  189. path = strdup(path_list);
  190. if (!path) {
  191. fprintf(stderr, "Out of memory!\n");
  192. exit(EXIT_FAILURE);
  193. }
  194. /* Eliminate all double //s */
  195. path_n=path;
  196. while((path_n=strstr(path_n, "//"))) {
  197. i = strlen(path_n);
  198. memmove(path_n, path_n+1, i-1);
  199. }
  200. /* Replace colons with zeros in path_list and count them */
  201. for(i=strlen(path); i > 0; i--) {
  202. if (path[i]==':') {
  203. path[i]=0;
  204. count++;
  205. }
  206. }
  207. path_n = path;
  208. for (i = 0; i < count; i++) {
  209. strcpy(result, path_n);
  210. strcat(result, "/");
  211. strcat(result, name);
  212. if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
  213. free(path);
  214. return;
  215. }
  216. path_n += (strlen(path_n) + 1);
  217. }
  218. free(path);
  219. *result = '\0';
  220. }
  221. void locate_library_file(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab,
  222. int is_suid, struct library *lib)
  223. {
  224. char *buf;
  225. char *path;
  226. struct stat filestat;
  227. /* If this is a fully resolved name, our job is easy */
  228. if (stat (lib->name, &filestat) == 0) {
  229. lib->path = lib->name;
  230. return;
  231. }
  232. /* We need some elbow room here. Make some room...*/
  233. buf = malloc(1024);
  234. if (!buf) {
  235. fprintf(stderr, "Out of memory!\n");
  236. exit(EXIT_FAILURE);
  237. }
  238. /* This function must match the behavior of _dl_load_shared_library
  239. * in readelflib1.c or things won't work out as expected... */
  240. /* The ABI specifies that RPATH is searched first, so do that now. */
  241. path = (char *)elf_find_dynamic(DT_RPATH, dynamic, ehdr, 0);
  242. if (path) {
  243. search_for_named_library(lib->name, buf, path);
  244. if (*buf != '\0') {
  245. lib->path = buf;
  246. return;
  247. }
  248. }
  249. /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
  250. * Since this app doesn't actually run an executable I will skip
  251. * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
  252. if (is_suid==1)
  253. path = NULL;
  254. else
  255. path = getenv("LD_LIBRARY_PATH");
  256. if (path) {
  257. search_for_named_library(lib->name, buf, path);
  258. if (*buf != '\0') {
  259. lib->path = buf;
  260. return;
  261. }
  262. }
  263. #ifdef USE_CACHE
  264. /* FIXME -- add code to check the Cache here */
  265. #endif
  266. /* Next look for libraries wherever the shared library
  267. * loader was installed -- this is usually where we
  268. * should find things... */
  269. if (interp_dir) {
  270. search_for_named_library(lib->name, buf, interp_dir);
  271. if (*buf != '\0') {
  272. lib->path = buf;
  273. return;
  274. }
  275. }
  276. /* Lastly, search the standard list of paths for the library.
  277. This list must exactly match the list in uClibc/ldso/d-link/readelflib1.c */
  278. path = UCLIBC_TARGET_PREFIX "/usr/lib:"
  279. UCLIBC_TARGET_PREFIX "/lib:"
  280. UCLIBC_DEVEL_PREFIX "/lib:"
  281. UCLIBC_BUILD_DIR "/lib:"
  282. "/usr/lib:"
  283. "/lib";
  284. search_for_named_library(lib->name, buf, path);
  285. if (*buf != '\0') {
  286. lib->path = buf;
  287. } else {
  288. free(buf);
  289. lib->path = not_found;
  290. }
  291. }
  292. static int add_library(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid, char *s)
  293. {
  294. char *tmp, *tmp1, *tmp2;
  295. struct library *cur, *newlib=lib_list;
  296. if (!s || !strlen(s))
  297. return 1;
  298. tmp = s;
  299. while (*tmp) {
  300. if (*tmp == '/')
  301. s = tmp + 1;
  302. tmp++;
  303. }
  304. /* We add libc.so.0 elsewhere */
  305. if ((tmp=strrchr(interp, '/')) != NULL)
  306. {
  307. int len = strlen(interp_dir);
  308. if (strcmp(s, interp+1+len)==0)
  309. return 1;
  310. }
  311. for (cur = lib_list; cur; cur=cur->next) {
  312. /* Check if this library is already in the list */
  313. tmp1 = tmp2 = cur->name;
  314. while (*tmp1) {
  315. if (*tmp1 == '/')
  316. tmp2 = tmp1 + 1;
  317. tmp1++;
  318. }
  319. if(strcmp(tmp2, s)==0) {
  320. //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
  321. return 0;
  322. }
  323. }
  324. /* Ok, this lib needs to be added to the list */
  325. newlib = malloc(sizeof(struct library));
  326. if (!newlib)
  327. return 1;
  328. newlib->name = malloc(strlen(s)+1);
  329. strcpy(newlib->name, s);
  330. newlib->resolved = 0;
  331. newlib->path = NULL;
  332. newlib->next = NULL;
  333. /* Now try and locate where this library might be living... */
  334. locate_library_file(ehdr, dynamic, strtab, is_setuid, newlib);
  335. //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
  336. if (!lib_list) {
  337. lib_list = newlib;
  338. } else {
  339. for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
  340. cur->next = newlib;
  341. }
  342. return 0;
  343. }
  344. static void find_needed_libraries(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid)
  345. {
  346. Elf32_Dyn *dyns;
  347. for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
  348. if (DT_NEEDED == byteswap32_to_host(dyns->d_tag)) {
  349. add_library(ehdr, dynamic, strtab, is_setuid, (char*)strtab +
  350. byteswap32_to_host(dyns->d_un.d_val));
  351. }
  352. }
  353. }
  354. static struct library *
  355. find_elf_interpreter(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, char *strtab, int is_setuid)
  356. {
  357. static int been_there_done_that=0;
  358. Elf32_Phdr *phdr;
  359. if (been_there_done_that==1)
  360. return NULL;
  361. been_there_done_that=1;
  362. phdr = elf_find_phdr_type(PT_INTERP, ehdr);
  363. if (phdr) {
  364. struct library *cur, *newlib=NULL;
  365. char *s = (char*)ehdr + byteswap32_to_host(phdr->p_offset);
  366. char *tmp, *tmp1;
  367. interp = strdup(s);
  368. interp_dir = strdup(s);
  369. tmp = strrchr(interp_dir, '/');
  370. if (*tmp)
  371. *tmp = '\0';
  372. else {
  373. free(interp_dir);
  374. interp_dir = interp;
  375. }
  376. tmp1 = tmp = s;
  377. while (*tmp) {
  378. if (*tmp == '/')
  379. tmp1 = tmp + 1;
  380. tmp++;
  381. }
  382. for (cur = lib_list; cur; cur=cur->next) {
  383. /* Check if this library is already in the list */
  384. if(strcmp(cur->name, tmp1)==0) {
  385. //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
  386. newlib = cur;
  387. free(newlib->name);
  388. free(newlib->path);
  389. return NULL;
  390. }
  391. }
  392. if (newlib == NULL)
  393. newlib = malloc(sizeof(struct library));
  394. if (!newlib)
  395. return NULL;
  396. newlib->name = malloc(strlen(s)+1);
  397. strcpy(newlib->name, s);
  398. newlib->path = newlib->name;
  399. newlib->resolved = 1;
  400. newlib->next = NULL;
  401. #if 0
  402. //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
  403. if (!lib_list) {
  404. lib_list = newlib;
  405. } else {
  406. for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
  407. cur->next = newlib;
  408. }
  409. #endif
  410. return newlib;
  411. }
  412. return NULL;
  413. }
  414. /* map the .so, and locate interesting pieces */
  415. int find_dependancies(char* filename)
  416. {
  417. int is_suid = 0;
  418. FILE *thefile;
  419. struct stat statbuf;
  420. char *dynstr=NULL;
  421. Elf32_Ehdr *ehdr = NULL;
  422. Elf32_Shdr *dynsec = NULL;
  423. Elf32_Dyn *dynamic = NULL;
  424. struct library *interp;
  425. if (filename == not_found)
  426. return 0;
  427. if (!filename) {
  428. fprintf(stderr, "No filename specified.\n");
  429. exit(EXIT_FAILURE);
  430. }
  431. if (!(thefile = fopen(filename, "r"))) {
  432. perror(filename);
  433. exit(EXIT_FAILURE);
  434. }
  435. if (fstat(fileno(thefile), &statbuf) < 0) {
  436. perror(filename);
  437. exit(EXIT_FAILURE);
  438. }
  439. if (statbuf.st_size < sizeof(Elf32_Ehdr))
  440. goto foo;
  441. /* mmap the file to make reading stuff from it effortless */
  442. ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size,
  443. PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
  444. foo:
  445. /* Check if this looks like a legit ELF file */
  446. if (check_elf_header(ehdr)) {
  447. fprintf(stderr, "%s: not an ELF file.\n", filename);
  448. exit(EXIT_FAILURE);
  449. }
  450. /* Check if this is the right kind of ELF file */
  451. if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
  452. fprintf(stderr, "%s: not a dynamic executable\n", filename);
  453. exit(EXIT_FAILURE);
  454. }
  455. if (ehdr->e_type == ET_EXEC) {
  456. if (statbuf.st_mode & S_ISUID)
  457. is_suid = 1;
  458. if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  459. is_suid = 1;
  460. /* FIXME */
  461. if (is_suid)
  462. fprintf(stderr, "%s: is setuid\n", filename);
  463. }
  464. dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
  465. interp = find_elf_interpreter(ehdr, dynamic, dynstr, is_suid);
  466. #ifdef __LDSO_LDD_SUPPORT
  467. if (interp && ehdr->e_ident[EI_CLASS] == ELFCLASSM && ehdr->e_ident[EI_DATA] == ELFDATAM
  468. && ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine))
  469. {
  470. struct stat statbuf;
  471. if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
  472. static const char * const environment[] = {
  473. "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
  474. "SHELL=/bin/sh",
  475. "LD_TRACE_LOADED_OBJECTS=1",
  476. NULL
  477. };
  478. /* Cool, it looks like we should be able to actually
  479. * run this puppy. Do so now... */
  480. execle(filename, filename, NULL, environment);
  481. /* If the exec failed, we fall through to trying to find
  482. * all the needed libraries ourselves by rummaging about
  483. * in the ELF headers... */
  484. }
  485. }
  486. #endif
  487. if (dynsec) {
  488. dynamic = (Elf32_Dyn*)(byteswap32_to_host(dynsec->sh_offset) + (intptr_t)ehdr);
  489. dynstr = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
  490. find_needed_libraries(ehdr, dynamic, dynstr, is_suid);
  491. }
  492. return 0;
  493. }
  494. int main( int argc, char** argv)
  495. {
  496. int multi=0;
  497. int got_em_all=1;
  498. char *filename = NULL;
  499. struct library *cur;
  500. if (argc < 2) {
  501. fprintf(stderr, "ldd: missing file arguments\n");
  502. fprintf(stderr, "Try `ldd --help' for more information.\n");
  503. exit(EXIT_FAILURE);
  504. }
  505. if (argc > 2) {
  506. multi++;
  507. }
  508. while (--argc > 0) {
  509. ++argv;
  510. if(strcmp(*argv, "--")==0) {
  511. /* Ignore "--" */
  512. continue;
  513. }
  514. if(strcmp(*argv, "--help")==0) {
  515. fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
  516. fprintf(stderr, "\t--help\t\tprint this help and exit\n");
  517. exit(EXIT_FAILURE);
  518. }
  519. filename=*argv;
  520. if (!filename) {
  521. fprintf(stderr, "No filename specified.\n");
  522. exit(EXIT_FAILURE);
  523. }
  524. find_dependancies(filename);
  525. while(got_em_all) {
  526. got_em_all=0;
  527. /* Keep walking the list till everybody is resolved */
  528. for (cur = lib_list; cur; cur=cur->next) {
  529. if (cur->resolved == 0 && cur->path) {
  530. got_em_all=1;
  531. //printf("checking sub-depends for '%s\n", cur->path);
  532. find_dependancies(cur->path);
  533. cur->resolved = 1;
  534. }
  535. }
  536. }
  537. /* Print the list */
  538. got_em_all=0;
  539. if (multi) {
  540. printf("%s:\n", *argv);
  541. }
  542. for (cur = lib_list; cur; cur=cur->next) {
  543. got_em_all=1;
  544. printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
  545. }
  546. if (interp_dir && got_em_all==1)
  547. printf("\t%s => %s (0x00000000)\n", interp, interp);
  548. if (got_em_all==0)
  549. printf("\tnot a dynamic executable\n");
  550. }
  551. return 0;
  552. }