I use the following script, seems to work quite well. As the comment explains, we simply find the largest LOAD section end offset in the file. (Note that it accounts for sparse files.)
If I remember correctly, I lifted the technique from GDB's corefile loading code (or some similar standard tool that warns about corefile truncation).
#!/bin/bashtrap 'exit 1' ERR # Abort script on error.if [[ $# != 1 ]] ; then echo "$( basename $0 ) <coreFile>" exit 1ficoreFile=$1# Examine all LOAD sections in the corefile, calculate the file offset of each section's end,# and find the largest offset.expectedSize=$( readelf -l ${coreFile} | grep -A 1 LOAD | while read type offset etc && read fsize etc ; do echo $(( $offset + $fsize )) done | sort -n | tail -n 1 )actualSize=$( du --block-size=1 --apparent-size ${coreFile} | cut -f1 )physicalSize=$( du --block-size=1 ${coreFile} | cut -f1 )if [[ ${actualSize} < ${expectedSize} ]] ; then echo "Physical size ${physicalSize}" echo "Expected logical size ${expectedSize}" echo "Actual logical size ${actualSize}" exit 2fi