objdiff 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. GIT_DIR="`git rev-parse --git-dir`"
  25. if [ -d "$GIT_DIR" ]; then
  26. TMPD="${GIT_DIR%git}tmp_objdiff"
  27. [ -d "$TMPD" ] || mkdir "$TMPD"
  28. else
  29. echo "ERROR: git directory not found."
  30. exit 1
  31. fi
  32. usage() {
  33. echo "Usage: $0 <command> <args>"
  34. echo " record <list of object files>"
  35. echo " diff <commitA> <commitB>"
  36. echo " clean all | <commit>"
  37. exit 1
  38. }
  39. dorecord() {
  40. [ $# -eq 0 ] && usage
  41. FILES="$*"
  42. CMT="`git rev-parse --short HEAD`"
  43. OBJDUMP="${CROSS_COMPILE}objdump"
  44. OBJDIFFD="$TMPD/$CMT"
  45. [ ! -d "$OBJDIFFD" ] && mkdir -p "$OBJDIFFD"
  46. for f in $FILES; do
  47. dn="${f%/*}"
  48. bn="${f##*/}"
  49. [ ! -d "$OBJDIFFD/$dn" ] && mkdir -p "$OBJDIFFD/$dn"
  50. # remove addresses for a more clear diff
  51. # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
  52. $OBJDUMP -D "$f" | sed "s/^[[:space:]]\+[0-9a-f]\+//" \
  53. >"$OBJDIFFD/$dn/$bn"
  54. done
  55. }
  56. dodiff() {
  57. [ $# -ne 2 ] && [ $# -ne 0 ] && usage
  58. if [ $# -eq 0 ]; then
  59. SRC="`git rev-parse --short HEAD^`"
  60. DST="`git rev-parse --short HEAD`"
  61. else
  62. SRC="`git rev-parse --short $1`"
  63. DST="`git rev-parse --short $2`"
  64. fi
  65. DIFF="`which colordiff`"
  66. if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
  67. DIFF="`which diff`"
  68. fi
  69. SRCD="$TMPD/$SRC"
  70. DSTD="$TMPD/$DST"
  71. if [ ! -d "$SRCD" ]; then
  72. echo "ERROR: $SRCD doesn't exist"
  73. exit 1
  74. fi
  75. if [ ! -d "$DSTD" ]; then
  76. echo "ERROR: $DSTD doesn't exist"
  77. exit 1
  78. fi
  79. $DIFF -Nurd $SRCD $DSTD
  80. }
  81. doclean() {
  82. [ $# -eq 0 ] && usage
  83. [ $# -gt 1 ] && usage
  84. if [ "x$1" = "xall" ]; then
  85. rm -rf $TMPD/*
  86. else
  87. CMT="`git rev-parse --short $1`"
  88. if [ -d "$TMPD/$CMT" ]; then
  89. rm -rf $TMPD/$CMT
  90. else
  91. echo "$CMT not found"
  92. fi
  93. fi
  94. }
  95. [ $# -eq 0 ] && usage
  96. case "$1" in
  97. record)
  98. shift
  99. dorecord $*
  100. ;;
  101. diff)
  102. shift
  103. dodiff $*
  104. ;;
  105. clean)
  106. shift
  107. doclean $*
  108. ;;
  109. *)
  110. echo "Unrecognized command '$1'"
  111. exit 1
  112. ;;
  113. esac