#!/bin/sh /usr/share/dpatch/dpatch-run ## 91-file-elf.dpatch by Alessandro Rubini ## ## DP: Revert ELF file-reading (closes: #345089). @DPATCH@ diff -Naur file-4.20.orig/src/readelf.c file-4.20/src/readelf.c --- file-4.20.orig/src/readelf.c 2007-01-18 05:45:35.000000000 +0000 +++ file-4.20/src/readelf.c 2007-03-27 12:23:14.000000000 +0000 @@ -907,29 +915,30 @@ off_t fsize; int flags = 0; - /* - * If we cannot seek, it must be a pipe, socket or fifo. - */ - if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE)) - fd = file_pipe2file(ms, fd, buf, nbytes); - - if (fstat(fd, &st) == -1) { - file_badread(ms); - return -1; - } - fsize = st.st_size; /* * ELF executables have multiple section headers in arbitrary * file locations and thus file(1) cannot determine it from easily. * Instead we traverse thru all section headers until a symbol table * one is found or else the binary is stripped. + * Return immediately if it's not ELF (so we avoid pipe2file unless needed). */ if (buf[EI_MAG0] != ELFMAG0 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1) || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3) return 0; + /* + * If we cannot seek, it must be a pipe, socket or fifo. + */ + if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE)) + fd = file_pipe2file(ms, fd, buf, nbytes); + + if (fstat(fd, &st) == -1) { + file_badread(ms); + return -1; + } + fsize = st.st_size; class = buf[EI_CLASS];