1 /* vi: set sw=4 ts=4: */
3 * A small little ldd implementation for uClibc
5 * Copyright (C) 2000-2004 Erik Andersen <andersee@debian.org>
7 * Several functions in this file (specifically, elf_find_section_type(),
8 * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
9 * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
10 * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include <sys/types.h>
39 #include <sys/types.h>
54 #define MATCH_MACHINE(x) (x == EM_ARM)
55 #define ELFCLASSM ELFCLASS32
59 #define MATCH_MACHINE(x) (x == EM_S390)
60 #define ELFCLASSM ELFCLASS32
65 #define MATCH_MACHINE(x) (x == EM_386)
67 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
69 #define ELFCLASSM ELFCLASS32
72 #if defined(__mc68000__)
73 #define MATCH_MACHINE(x) (x == EM_68K)
74 #define ELFCLASSM ELFCLASS32
78 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
79 #define ELFCLASSM ELFCLASS32
82 #if defined(__powerpc__)
83 #define MATCH_MACHINE(x) (x == EM_PPC)
84 #define ELFCLASSM ELFCLASS32
88 #define MATCH_MACHINE(x) (x == EM_SH)
89 #define ELFCLASSM ELFCLASS32
92 #if defined (__v850e__)
93 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
94 #define ELFCLASSM ELFCLASS32
97 #if defined (__sparc__)
98 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
99 #define ELFCLASSM ELFCLASS32
102 #if defined(__cris__)
103 #define MATCH_MACHINE(x) (x == EM_CRIS)
104 #define ELFCLASSM ELFCLASS32
107 #ifndef MATCH_MACHINE
108 #warning "You really should add a MATCH_MACHINE() macro for your architecture"
111 #if __BYTE_ORDER == __LITTLE_ENDIAN
112 #define ELFDATAM ELFDATA2LSB
113 #elif __BYTE_ORDER == __BIG_ENDIAN
114 #define ELFDATAM ELFDATA2MSB
121 struct library *next;
123 struct library *lib_list = NULL;
124 char not_found[] = "not found";
126 char *interp_dir = NULL;
128 static int interpreter_already_found=0;
130 inline uint32_t byteswap32_to_host(uint32_t value)
133 return(bswap_32(value));
139 Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr)
143 shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr);
144 for (j = ehdr->e_shnum; --j>=0; ++shdr) {
145 if (key==(int)byteswap32_to_host(shdr->sh_type)) {
152 Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr)
155 Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr);
156 for (j = ehdr->e_phnum; --j>=0; ++phdr) {
157 if (type==(int)byteswap32_to_host(phdr->p_type)) {
164 /* Returns value if return_val==1, ptr otherwise */
165 void * elf_find_dynamic(int const key, Elf32_Dyn *dynp,
166 Elf32_Ehdr *ehdr, int return_val)
168 Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
169 unsigned tx_reloc = byteswap32_to_host(pt_text->p_vaddr) - byteswap32_to_host(pt_text->p_offset);
170 for (; DT_NULL!=byteswap32_to_host(dynp->d_tag); ++dynp) {
171 if (key == (int)byteswap32_to_host(dynp->d_tag)) {
173 return (void *)(intptr_t)byteswap32_to_host(dynp->d_un.d_val);
175 return (void *)(byteswap32_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr );
181 static char * elf_find_rpath(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic)
185 for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
186 if (DT_RPATH == byteswap32_to_host(dyns->d_tag)) {
188 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
189 return ((char*)strtab + byteswap32_to_host(dyns->d_un.d_val));
195 int check_elf_header(Elf32_Ehdr *const ehdr)
197 if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||
198 ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
199 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
204 /* Check if the target endianness matches the host's endianness */
206 #if __BYTE_ORDER == __LITTLE_ENDIAN
207 if (ehdr->e_ident[5] == ELFDATA2MSB) {
208 /* Ick -- we will have to byte-swap everything */
211 #elif __BYTE_ORDER == __BIG_ENDIAN
212 if (ehdr->e_ident[5] == ELFDATA2LSB) {
213 /* Ick -- we will have to byte-swap everything */
217 #error Unknown host byte order!
220 /* Be vary lazy, and only byteswap the stuff we use */
222 ehdr->e_type=bswap_16(ehdr->e_type);
223 ehdr->e_phoff=bswap_32(ehdr->e_phoff);
224 ehdr->e_shoff=bswap_32(ehdr->e_shoff);
225 ehdr->e_phnum=bswap_16(ehdr->e_phnum);
226 ehdr->e_shnum=bswap_16(ehdr->e_shnum);
232 /* This function's behavior must exactly match that
233 * in uClibc/ldso/ldso/dl-elf.c */
234 static void search_for_named_library(char *name, char *result, const char *path_list)
238 struct stat filestat;
240 /* We need a writable copy of this string */
241 path = strdup(path_list);
243 fprintf(stderr, "Out of memory!\n");
246 /* Eliminate all double //s */
248 while((path_n=strstr(path_n, "//"))) {
250 memmove(path_n, path_n+1, i-1);
251 *(path_n + i - 1)='\0';
254 /* Replace colons with zeros in path_list and count them */
255 for(i=strlen(path); i > 0; i--) {
262 for (i = 0; i < count; i++) {
263 strcpy(result, path_n);
265 strcat(result, name);
266 if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
270 path_n += (strlen(path_n) + 1);
276 void locate_library_file(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, int is_suid, struct library *lib)
280 struct stat filestat;
282 /* If this is a fully resolved name, our job is easy */
283 if (stat (lib->name, &filestat) == 0) {
284 lib->path = strdup(lib->name);
288 /* We need some elbow room here. Make some room...*/
291 fprintf(stderr, "Out of memory!\n");
295 /* This function must match the behavior of _dl_load_shared_library
296 * in readelflib1.c or things won't work out as expected... */
298 /* The ABI specifies that RPATH is searched first, so do that now. */
299 path = elf_find_rpath(ehdr, dynamic);
301 search_for_named_library(lib->name, buf, path);
308 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
309 * Since this app doesn't actually run an executable I will skip
310 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
314 path = getenv("LD_LIBRARY_PATH");
316 search_for_named_library(lib->name, buf, path);
324 /* FIXME -- add code to check the Cache here */
328 /* Next look for libraries wherever the shared library
329 * loader was installed -- this is usually where we
330 * should find things... */
332 search_for_named_library(lib->name, buf, interp_dir);
339 /* Lastly, search the standard list of paths for the library.
340 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
341 path = UCLIBC_RUNTIME_PREFIX "lib:"
342 UCLIBC_RUNTIME_PREFIX "usr/lib";
343 search_for_named_library(lib->name, buf, path);
348 lib->path = not_found;
352 static int add_library(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, int is_setuid, char *s)
354 char *tmp, *tmp1, *tmp2;
355 struct library *cur, *newlib=lib_list;
357 if (!s || !strlen(s))
367 /* We add ldso elsewhere */
368 if (interpreter_already_found && (tmp=strrchr(interp, '/')) != NULL)
370 int len = strlen(interp_dir);
371 if (strcmp(s, interp+1+len)==0)
375 for (cur = lib_list; cur; cur=cur->next) {
376 /* Check if this library is already in the list */
377 tmp1 = tmp2 = cur->name;
383 if(strcmp(tmp2, s)==0) {
384 //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
389 /* Ok, this lib needs to be added to the list */
390 newlib = malloc(sizeof(struct library));
393 newlib->name = malloc(strlen(s)+1);
394 strcpy(newlib->name, s);
395 newlib->resolved = 0;
399 /* Now try and locate where this library might be living... */
400 locate_library_file(ehdr, dynamic, is_setuid, newlib);
402 //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
406 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
412 static void find_needed_libraries(Elf32_Ehdr* ehdr,
413 Elf32_Dyn* dynamic, int is_setuid)
417 for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
418 if (DT_NEEDED == byteswap32_to_host(dyns->d_tag)) {
420 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
421 add_library(ehdr, dynamic, is_setuid,
422 (char*)strtab + byteswap32_to_host(dyns->d_un.d_val));
427 static struct library * find_elf_interpreter(Elf32_Ehdr* ehdr)
431 if (interpreter_already_found==1)
433 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
435 struct library *cur, *newlib=NULL;
436 char *s = (char*)ehdr + byteswap32_to_host(phdr->p_offset);
440 interp_dir = strdup(s);
441 tmp = strrchr(interp_dir, '/');
454 for (cur = lib_list; cur; cur=cur->next) {
455 /* Check if this library is already in the list */
456 if(strcmp(cur->name, tmp1)==0) {
457 //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
460 if (newlib->path != not_found) {
469 newlib = malloc(sizeof(struct library));
472 newlib->name = malloc(strlen(s)+1);
473 strcpy(newlib->name, s);
474 newlib->path = strdup(newlib->name);
475 newlib->resolved = 1;
479 //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
483 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
487 interpreter_already_found=1;
493 /* map the .so, and locate interesting pieces */
494 int find_dependancies(char* filename)
499 Elf32_Ehdr *ehdr = NULL;
500 Elf32_Shdr *dynsec = NULL;
501 Elf32_Dyn *dynamic = NULL;
502 struct library *interp;
504 if (filename == not_found)
508 fprintf(stderr, "No filename specified.\n");
511 if (!(thefile = fopen(filename, "r"))) {
515 if (fstat(fileno(thefile), &statbuf) < 0) {
521 if ((size_t)statbuf.st_size < sizeof(Elf32_Ehdr))
524 if (!S_ISREG(statbuf.st_mode))
527 /* mmap the file to make reading stuff from it effortless */
528 ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size,
529 PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
530 if (ehdr == MAP_FAILED) {
532 fprintf(stderr, "Out of memory!\n");
539 /* Check if this looks like a legit ELF file */
540 if (check_elf_header(ehdr)) {
541 fprintf(stderr, "%s: not an ELF file.\n", filename);
544 /* Check if this is the right kind of ELF file */
545 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
546 fprintf(stderr, "%s: not a dynamic executable\n", filename);
549 if (ehdr->e_type == ET_EXEC || ehdr->e_type != ET_DYN) {
550 if (statbuf.st_mode & S_ISUID)
552 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
556 fprintf(stderr, "%s: is setuid\n", filename);
559 interpreter_already_found=0;
560 interp = find_elf_interpreter(ehdr);
562 #ifdef __LDSO_LDD_SUPPORT
563 if (interp && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) && ehdr->e_ident[EI_CLASS] == ELFCLASSM &&
564 ehdr->e_ident[EI_DATA] == ELFDATAM
565 && ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine))
568 if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
571 static const char * const environment[] = {
572 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
574 "LD_TRACE_LOADED_OBJECTS=1",
578 if ((pid = fork()) == 0) {
579 /* Cool, it looks like we should be able to actually
580 * run this puppy. Do so now... */
581 execle(filename, filename, NULL, environment);
585 /* Wait till it returns */
586 waitpid(pid, &status, 0);
587 if (WIFEXITED(status) && WEXITSTATUS(status)==0) {
591 /* If the exec failed, we fall through to trying to find
592 * all the needed libraries ourselves by rummaging about
593 * in the ELF headers... */
598 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
600 dynamic = (Elf32_Dyn*)(byteswap32_to_host(dynsec->sh_offset) + (intptr_t)ehdr);
601 find_needed_libraries(ehdr, dynamic, is_suid);
607 int main( int argc, char** argv)
611 char *filename = NULL;
615 fprintf(stderr, "ldd: missing file arguments\n");
616 fprintf(stderr, "Try `ldd --help' for more information.\n");
626 if(strcmp(*argv, "--")==0) {
631 if(strcmp(*argv, "--help")==0) {
632 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
633 fprintf(stderr, "\t--help\t\tprint this help and exit\n");
639 fprintf(stderr, "No filename specified.\n");
644 printf("%s:\n", *argv);
647 if (find_dependancies(filename)!=0)
652 /* Keep walking the list till everybody is resolved */
653 for (cur = lib_list; cur; cur=cur->next) {
654 if (cur->resolved == 0 && cur->path) {
656 //printf("checking sub-depends for '%s\n", cur->path);
657 find_dependancies(cur->path);
666 for (cur = lib_list; cur; cur=cur->next) {
668 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
670 if (interp && interpreter_already_found==1)
671 printf("\t%s => %s (0x00000000)\n", interp, interp);
673 printf("\tnot a dynamic executable\n");
675 for (cur = lib_list; cur; cur=cur->next) {
678 if (cur->path && cur->path != not_found) {