ldd.c 15 KB

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