ldd.c 12 KB

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