git: 2cd53619a1fc - stable/12 - strip/objcopy: handle empty file as unknown

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Mon, 08 Nov 2021 14:03:41 UTC
The branch stable/12 has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=2cd53619a1fcdb1283ca1b813ecf10ec9bdb5b0b

commit 2cd53619a1fcdb1283ca1b813ecf10ec9bdb5b0b
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2021-10-25 21:25:26 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2021-11-08 14:02:46 +0000

    strip/objcopy: handle empty file as unknown
    
    Previously strip reported a somewhat cryptic error for empty files:
    
        strip: elf_begin() failed: Invalid argument
    
    Add a special case to treat empty files as with an unknown file format.
    This is consistent with llvm-strip.  GNU strip produces no output which
    does not seem like useful behaviour (but it does exit with status 1).
    
    Reported by:    andrew
    Reviewed by:    markj
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D32648
    
    (cherry picked from commit 179219ea046f46927d6478d43431e8b541703539)
---
 contrib/elftoolchain/elfcopy/main.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/contrib/elftoolchain/elfcopy/main.c b/contrib/elftoolchain/elfcopy/main.c
index c02bb296c4a3..ae2d3a2440df 100644
--- a/contrib/elftoolchain/elfcopy/main.c
+++ b/contrib/elftoolchain/elfcopy/main.c
@@ -702,6 +702,8 @@ create_file(struct elfcopy *ecp, const char *src, const char *dst)
 	}
 
 	if ((ecp->ein = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
+		if (fstat(ifd, &sb) == 0 && sb.st_size == 0)
+			errx(EXIT_FAILURE, "file format not recognized");
 		errx(EXIT_FAILURE, "elf_begin() failed: %s",
 		    elf_errmsg(-1));