From owner-svn-src-all@freebsd.org Mon Nov 5 22:53:52 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E31A0110E730; Mon, 5 Nov 2018 22:53:51 +0000 (UTC) (envelope-from brooks@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 6D4B078D78; Mon, 5 Nov 2018 22:53:51 +0000 (UTC) (envelope-from brooks@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4E0D61DF55; Mon, 5 Nov 2018 22:53:51 +0000 (UTC) (envelope-from brooks@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id wA5MrpCX089227; Mon, 5 Nov 2018 22:53:51 GMT (envelope-from brooks@FreeBSD.org) Received: (from brooks@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id wA5MroSf089225; Mon, 5 Nov 2018 22:53:50 GMT (envelope-from brooks@FreeBSD.org) Message-Id: <201811052253.wA5MroSf089225@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: brooks set sender to brooks@FreeBSD.org using -f From: Brooks Davis Date: Mon, 5 Nov 2018 22:53:50 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r340169 - head/usr.bin/elfdump X-SVN-Group: head X-SVN-Commit-Author: brooks X-SVN-Commit-Paths: head/usr.bin/elfdump X-SVN-Commit-Revision: 340169 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 6D4B078D78 X-Spamd-Result: default: False [-102.79 / 200.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.70)[-0.695,0]; ALLOW_DOMAIN_WHITELIST(-100.00)[FreeBSD.org]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-0.99)[-0.991,0]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; HAS_XAW(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all]; DMARC_NA(0.00)[FreeBSD.org]; RCVD_COUNT_THREE(0.00)[4]; MX_GOOD(-0.01)[cached: mx1.FreeBSD.org]; NEURAL_HAM_SHORT(-1.00)[-0.998,0]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; RCVD_TLS_LAST(0.00)[] X-Rspamd-Server: mx1.freebsd.org X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 05 Nov 2018 22:53:52 -0000 Author: brooks Date: Mon Nov 5 22:53:50 2018 New Revision: 340169 URL: https://svnweb.freebsd.org/changeset/base/340169 Log: elfdump: Add -E to test if a file is an ELF binary. This is intended to replace potentially unreliable checks like: file -b $1 | grep -q '^ELF ..-bit .SB executable' Reviewed by: emaste Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D15971 Modified: head/usr.bin/elfdump/elfdump.1 head/usr.bin/elfdump/elfdump.c Modified: head/usr.bin/elfdump/elfdump.1 ============================================================================== --- head/usr.bin/elfdump/elfdump.1 Mon Nov 5 22:51:45 2018 (r340168) +++ head/usr.bin/elfdump/elfdump.1 Mon Nov 5 22:53:50 2018 (r340169) @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd January 15, 2003 +.Dd November 5, 2018 .Dt ELFDUMP 1 .Os .Sh NAME @@ -34,7 +34,7 @@ files .Sh SYNOPSIS .Nm -.Fl a | cdeGhinprs +.Fl a | E | cdeGhinprs .Op Fl w Ar file .Ar file .Sh DESCRIPTION @@ -55,6 +55,11 @@ Dump section headers. Dump dynamic symbols. .It Fl e Dump ELF header. +.It Fl E +Return success if +.Ar file +is an ELF file and failure if it is not. +This option is exclusive with other options. .It Fl G Dump the GOT. .It Fl h Modified: head/usr.bin/elfdump/elfdump.c ============================================================================== --- head/usr.bin/elfdump/elfdump.c Mon Nov 5 22:51:45 2018 (r340168) +++ head/usr.bin/elfdump/elfdump.c Mon Nov 5 22:53:50 2018 (r340169) @@ -60,6 +60,7 @@ __FBSDID("$FreeBSD$"); #define ED_SHDR (1<<8) #define ED_SYMTAB (1<<9) #define ED_ALL ((1<<10)-1) +#define ED_IS_ELF (1<<10) /* Exclusive with other flags */ #define elf_get_addr elf_get_quad #define elf_get_off elf_get_quad @@ -518,7 +519,7 @@ main(int ac, char **av) out = stdout; flags = 0; - while ((ch = getopt(ac, av, "acdeiGhnprsw:")) != -1) + while ((ch = getopt(ac, av, "acdEeiGhnprsw:")) != -1) switch (ch) { case 'a': flags = ED_ALL; @@ -529,6 +530,9 @@ main(int ac, char **av) case 'd': flags |= ED_DYN; break; + case 'E': + flags = ED_IS_ELF; + break; case 'e': flags |= ED_EHDR; break; @@ -566,7 +570,8 @@ main(int ac, char **av) } ac -= optind; av += optind; - if (ac == 0 || flags == 0) + if (ac == 0 || flags == 0 || ((flags & ED_IS_ELF) && + (ac != 1 || (flags & ~ED_IS_ELF) || out != stdout))) usage(); if ((fd = open(*av, O_RDONLY)) < 0 || fstat(fd, &sb) < 0) @@ -584,8 +589,12 @@ main(int ac, char **av) e = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); if (e == MAP_FAILED) err(1, NULL); - if (!IS_ELF(*(Elf32_Ehdr *)e)) + if (!IS_ELF(*(Elf32_Ehdr *)e)) { + if (flags & ED_IS_ELF) + exit(1); errx(1, "not an elf file"); + } else if (flags & ED_IS_ELF) + exit (0); phoff = elf_get_off(e, e, E_PHOFF); shoff = elf_get_off(e, e, E_SHOFF); phentsize = elf_get_quarter(e, e, E_PHENTSIZE); @@ -1254,6 +1263,7 @@ elf_get_quad(Elf32_Ehdr *e, void *base, elf_member_t m static void usage(void) { - fprintf(stderr, "usage: elfdump -a | -cdeGhinprs [-w file] file\n"); + fprintf(stderr, + "usage: elfdump -a | -E | -cdeGhinprs [-w file] file\n"); exit(1); }