Skip to content

Instantly share code, notes, and snippets.

@rcoup
Created April 24, 2023 18:51
Show Gist options
  • Select an option

  • Save rcoup/2e742a8792907c41c39938ceb9a4f036 to your computer and use it in GitHub Desktop.

Select an option

Save rcoup/2e742a8792907c41c39938ceb9a4f036 to your computer and use it in GitHub Desktop.

Revisions

  1. rcoup created this gist Apr 24, 2023.
    28 changes: 28 additions & 0 deletions elf-validate.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/bin/bash
    set -eu

    command -v eu-elflint >/dev/null || (echo "eu-elflint not found, install elfutils"; exit 1)
    command -v scanelf >/dev/null || (echo "scanelf not found, install pax-utils"; exit 1)

    BINARIES=$(scanelf -EET_EXEC -RBF %F "${1-.}")

    ERRS=0
    for BINARY in $BINARIES; do
    echo -e "\n$BINARY"

    if [[ "$(scanelf -k '.go.buildinfo' "$BINARY" -q)" == .go.buildinfo* ]]; then
    # go binary
    echo "... is a Golang binary. elflint will return errors"
    fi

    if ! eu-elflint --gnu-ld "$BINARY"; then
    ERRS=$((ERRS+1))
    fi
    done

    if [ "$ERRS" -gt 0 ]; then
    echo -e "\n$ERRS errors encountered during ELF binary validation"
    exit 1
    else
    echo -e "\nELF binaries validated ok"
    fi