#!/usr/bin/env bash # Prove that two release binaries run the same code: compare every loadable section byte for byte (STRA-338). # # scripts/dev/elf-sections-equal.sh A B exit 0 when .text, .rodata, .data* , .eh_frame*, .gcc_except_table, .init/.fini*, # .tdata, .got and .plt of A and B are identical; exit 1 and a diff otherwise # # Two builds of the same program can differ in the symbol table only: LLVM suffixes private symbols with `.llvm.` and # the hash follows the module layout, so `cmp` and `sha256sum` of the whole file are the wrong test after a pure source move. # Build both with the same RUSTFLAGS `--remap-path-prefix==/src` so that panic locations carry no machine path. # Needs GNU objcopy (binutils); run it in Linux or WSL. set -euo pipefail SECTIONS=(.text .rodata .data .data.rel.ro .eh_frame .eh_frame_hdr .gcc_except_table .init_array .fini_array .tdata .got .plt .plt.got .init .fini) sums() { # $1 = ELF file: one "section sha256 size" line per non-empty section local file=$1 dir section dir=$(mktemp -d) for section in "${SECTIONS[@]}"; do if objcopy -O binary --only-section="$section" "$file" "$dir/s" 2>/dev/null && [[ -s "$dir/s" ]]; then echo "$section $(sha256sum <"$dir/s" | cut -c1-64) $(stat -c %s "$dir/s")" fi done rm -rf "$dir" } main() { [[ $# -eq 2 ]] || { sed -n '2,11p' "$0"; exit 2; } if diff <(sums "$1") <(sums "$2"); then echo "sections identical: $1 == $2" else echo "sections DIFFER" >&2 exit 1 fi } main "$@"