objdiff 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. # objdiff - a small script for validating that a commit or series of commits
  3. # didn't change object code.
  4. #
  5. # Copyright 2014, Jason Cooper <jason@lakedaemon.net>
  6. #
  7. # Licensed under the terms of the GNU GPL version 2
  8. # usage example:
  9. #
  10. # $ git checkout COMMIT_A
  11. # $ <your fancy build command here>
  12. # $ ./scripts/objdiff record path/to/*.o
  13. #
  14. # $ git checkout COMMIT_B
  15. # $ <your fancy build command here>
  16. # $ ./scripts/objdiff record path/to/*.o
  17. #
  18. # $ ./scripts/objdiff diff COMMIT_A COMMIT_B
  19. # $
  20. # And to clean up (everything is in .tmp_objdiff/*)
  21. # $ ./scripts/objdiff clean all
  22. #
  23. # Note: 'make mrproper' will also remove .tmp_objdiff
  24. SRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd)
  25. if [ -z "$SRCTREE" ]; then
  26. echo >&2 "ERROR: Not a git repository."
  27. exit 1
  28. fi
  29. TMPD=$SRCTREE/.tmp_objdiff
  30. usage() {
  31. echo >&2 "Usage: $0 <command> <args>"
  32. echo >&2 " record <list of object files or directories>"
  33. echo >&2 " diff <commitA> <commitB>"
  34. echo >&2 " clean all | <commit>"
  35. exit 1
  36. }
  37. get_output_dir() {
  38. dir=${1%/*}
  39. if [ "$dir" = "$1" ]; then
  40. dir=.
  41. fi
  42. dir=$(cd $dir; pwd)
  43. echo $TMPD/$CMT${dir#$SRCTREE}
  44. }
  45. do_objdump() {
  46. dir=$(get_output_dir $1)
  47. base=${1##*/}
  48. dis=$dir/${base%.o}.dis
  49. [ ! -d "$dir" ] && mkdir -p $dir
  50. # remove addresses for a cleaner diff
  51. # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
  52. $OBJDUMP -D $1 | sed "s/^[[:space:]]\+[0-9a-f]\+//" > $dis
  53. }
  54. dorecord() {
  55. [ $# -eq 0 ] && usage
  56. FILES="$*"
  57. CMT="`git rev-parse --short HEAD`"
  58. OBJDUMP="${CROSS_COMPILE}objdump"
  59. for d in $FILES; do
  60. if [ -d "$d" ]; then
  61. for f in $(find $d -name '*.o')
  62. do
  63. do_objdump $f
  64. done
  65. else
  66. do_objdump $d
  67. fi
  68. done
  69. }
  70. dodiff() {
  71. [ $# -ne 2 ] && [ $# -ne 0 ] && usage
  72. if [ $# -eq 0 ]; then
  73. SRC="`git rev-parse --short HEAD^`"
  74. DST="`git rev-parse --short HEAD`"
  75. else
  76. SRC="`git rev-parse --short $1`"
  77. DST="`git rev-parse --short $2`"
  78. fi
  79. DIFF="`which colordiff`"
  80. if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
  81. DIFF="`which diff`"
  82. fi
  83. SRCD="$TMPD/$SRC"
  84. DSTD="$TMPD/$DST"
  85. if [ ! -d "$SRCD" ]; then
  86. echo >&2 "ERROR: $SRCD doesn't exist"
  87. exit 1
  88. fi
  89. if [ ! -d "$DSTD" ]; then
  90. echo >&2 "ERROR: $DSTD doesn't exist"
  91. exit 1
  92. fi
  93. $DIFF -Nurd $SRCD $DSTD
  94. }
  95. doclean() {
  96. [ $# -eq 0 ] && usage
  97. [ $# -gt 1 ] && usage
  98. if [ "x$1" = "xall" ]; then
  99. rm -rf $TMPD/*
  100. else
  101. CMT="`git rev-parse --short $1`"
  102. if [ -d "$TMPD/$CMT" ]; then
  103. rm -rf $TMPD/$CMT
  104. else
  105. echo >&2 "$CMT not found"
  106. fi
  107. fi
  108. }
  109. [ $# -eq 0 ] && usage
  110. case "$1" in
  111. record)
  112. shift
  113. dorecord $*
  114. ;;
  115. diff)
  116. shift
  117. dodiff $*
  118. ;;
  119. clean)
  120. shift
  121. doclean $*
  122. ;;
  123. *)
  124. echo >&2 "Unrecognized command '$1'"
  125. exit 1
  126. ;;
  127. esac