From f28fdede6f2d2cb5aafec030913ea5ac7ad6eaed Mon Sep 17 00:00:00 2001 From: dfr Date: Fri, 1 May 1998 08:40:11 +0000 Subject: Add ELF support. --- usr.bin/ldd/ldd.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++-------- usr.bin/ldd/sods.c | 12 +++++++++-- 2 files changed, 62 insertions(+), 11 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/ldd/ldd.c b/usr.bin/ldd/ldd.c index d940261..31a7a67 100644 --- a/usr.bin/ldd/ldd.c +++ b/usr.bin/ldd/ldd.c @@ -27,7 +27,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: ldd.c,v 1.13 1997/02/22 15:46:43 peter Exp $ + * $Id: ldd.c,v 1.14 1997/09/02 21:54:39 jdp Exp $ */ #include @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -118,19 +119,61 @@ char *argv[]; argv++; continue; } - if (read(fd, &hdr, sizeof hdr) != sizeof hdr - || (N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC -#if 1 /* Compatibility */ - || hdr.a_entry < __LDPGSZ -#endif - ) { - - warnx("%s: not a dynamic executable", *argv); + if (read(fd, &hdr, sizeof hdr) != sizeof hdr) { + warnx("%s: can't read program header", *argv); (void)close(fd); rval |= 1; argv++; continue; } + + if (!N_BADMAG(hdr)) { + /* a.out file */ + if ((N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC +#if 1 /* Compatibility */ + || hdr.a_entry < __LDPGSZ +#endif + ) { + warnx("%s: not a dynamic executable", *argv); + (void)close(fd); + rval |= 1; + argv++; + continue; + } + } else if (IS_ELF(*(Elf32_Ehdr*)&hdr)) { + Elf32_Ehdr ehdr; + Elf32_Phdr phdr; + int dynamic = 0, i; + + lseek(fd, 0, SEEK_SET); + if (read(fd, &ehdr, sizeof ehdr) != sizeof ehdr) { + warnx("%s: can't read program header", *argv); + (void)close(fd); + rval |= 1; + argv++; + continue; + } + lseek(fd, 0, ehdr.e_phoff); + for (i = 0; i < ehdr.e_phnum; i++) { + if (read(fd, &phdr, ehdr.e_phentsize) + != sizeof phdr) { + warnx("%s: can't read program header", *argv); + (void)close(fd); + rval |= 1; + argv++; + continue; + } + if (phdr.p_type == PT_DYNAMIC) + dynamic = 1; + } + if (!dynamic) { + warnx("%s: not a dynamic executable", *argv); + (void)close(fd); + rval |= 1; + argv++; + continue; + } + } (void)close(fd); setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1); diff --git a/usr.bin/ldd/sods.c b/usr.bin/ldd/sods.c index 3e28746..801cca0 100644 --- a/usr.bin/ldd/sods.c +++ b/usr.bin/ldd/sods.c @@ -22,7 +22,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: sods.c,v 1.5 1997/09/02 21:54:39 jdp Exp $ + * $Id: sods.c,v 1.6 1997/11/28 19:34:27 jdp Exp $ */ #include @@ -39,6 +39,7 @@ #include #include +#include #include #include @@ -173,6 +174,13 @@ dump_file(const char *fname) file_base = (const char *) objbase; /* Makes address arithmetic easier */ + if (IS_ELF(*(Elf32_Ehdr*) file_base)) { + error("%s: this is an ELF program; use objdump to examine.", fname); + munmap(objbase, sb.st_size); + close(fd); + return; + } + ex = (const struct exec *) file_base; printf("%s: a_midmag = 0x%lx\n", fname, ex->a_midmag); @@ -211,7 +219,7 @@ dump_file(const char *fname) } printf(" Entry = 0x%lx\n", ex->a_entry); - printf(" Text offset = %x, address = %x\n", N_TXTOFF(*ex), + printf(" Text offset = %x, address = %lx\n", N_TXTOFF(*ex), N_TXTADDR(*ex)); printf(" Data offset = %lx, address = %lx\n", N_DATOFF(*ex), N_DATADDR(*ex)); -- cgit v1.1