svn commit: r358783 - stable/12/usr.bin/elfdump

Ed Maste emaste at FreeBSD.org
Sun Mar 8 21:11:51 UTC 2020


Author: emaste
Date: Sun Mar  8 21:11:49 2020
New Revision: 358783
URL: https://svnweb.freebsd.org/changeset/base/358783

Log:
  MFC r340169 (brooks): 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'

Modified:
  stable/12/usr.bin/elfdump/elfdump.1
  stable/12/usr.bin/elfdump/elfdump.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/usr.bin/elfdump/elfdump.1
==============================================================================
--- stable/12/usr.bin/elfdump/elfdump.1	Sun Mar  8 21:10:16 2020	(r358782)
+++ stable/12/usr.bin/elfdump/elfdump.1	Sun Mar  8 21:11:49 2020	(r358783)
@@ -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: stable/12/usr.bin/elfdump/elfdump.c
==============================================================================
--- stable/12/usr.bin/elfdump/elfdump.c	Sun Mar  8 21:10:16 2020	(r358782)
+++ stable/12/usr.bin/elfdump/elfdump.c	Sun Mar  8 21:11:49 2020	(r358783)
@@ -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
@@ -527,7 +528,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;
@@ -538,6 +539,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;
@@ -575,7 +579,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)
@@ -593,8 +598,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);
@@ -1270,6 +1279,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);
 }


More information about the svn-src-all mailing list