objdiff 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. stripped=$dir/${base%.o}.stripped
  49. dis=$dir/${base%.o}.dis
  50. [ ! -d "$dir" ] && mkdir -p $dir
  51. # remove addresses for a cleaner diff
  52. # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
  53. $STRIP -g $1 -R __bug_table -R .note -R .comment -o $stripped
  54. $OBJDUMP -D $stripped | sed -e "s/^[[:space:]]\+[0-9a-f]\+//" -e "s:^$stripped:$1:" > $dis
  55. }
  56. dorecord() {
  57. [ $# -eq 0 ] && usage
  58. FILES="$*"
  59. CMT="`git rev-parse --short HEAD`"
  60. STRIP="${CROSS_COMPILE}strip"
  61. OBJDUMP="${CROSS_COMPILE}objdump"
  62. for d in $FILES; do
  63. if [ -d "$d" ]; then
  64. for f in $(find $d -name '*.o')
  65. do
  66. do_objdump $f
  67. done
  68. else
  69. do_objdump $d
  70. fi
  71. done
  72. }
  73. dodiff() {
  74. [ $# -ne 2 ] && [ $# -ne 0 ] && usage
  75. if [ $# -eq 0 ]; then
  76. SRC="`git rev-parse --short HEAD^`"
  77. DST="`git rev-parse --short HEAD`"
  78. else
  79. SRC="`git rev-parse --short $1`"
  80. DST="`git rev-parse --short $2`"
  81. fi
  82. DIFF="`which colordiff`"
  83. if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
  84. DIFF="`which diff`"
  85. fi
  86. SRCD="$TMPD/$SRC"
  87. DSTD="$TMPD/$DST"
  88. if [ ! -d "$SRCD" ]; then
  89. echo >&2 "ERROR: $SRCD doesn't exist"
  90. exit 1
  91. fi
  92. if [ ! -d "$DSTD" ]; then
  93. echo >&2 "ERROR: $DSTD doesn't exist"
  94. exit 1
  95. fi
  96. $DIFF -Nurd $SRCD $DSTD
  97. }
  98. doclean() {
  99. [ $# -eq 0 ] && usage
  100. [ $# -gt 1 ] && usage
  101. if [ "x$1" = "xall" ]; then
  102. rm -rf $TMPD/*
  103. else
  104. CMT="`git rev-parse --short $1`"
  105. if [ -d "$TMPD/$CMT" ]; then
  106. rm -rf $TMPD/$CMT
  107. else
  108. echo >&2 "$CMT not found"
  109. fi
  110. fi
  111. }
  112. [ $# -eq 0 ] && usage
  113. case "$1" in
  114. record)
  115. shift
  116. dorecord $*
  117. ;;
  118. diff)
  119. shift
  120. dodiff $*
  121. ;;
  122. clean)
  123. shift
  124. doclean $*
  125. ;;
  126. *)
  127. echo >&2 "Unrecognized command '$1'"
  128. exit 1
  129. ;;
  130. esac