summaryrefslogtreecommitdiff
path: root/bin/check-opcodes
blob: 9ed2837c9289847ec6ca271d58a8339a29c6a5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/sh

. "${0%/*}/../conf/default.conf"

usage( ) {
  >&2 cat <<EOF

check_opcodes: [ -a <arch> ] <package>

possible optons:
  -h|--help:         show this help page
  -v|--verbose:      verbose output
  -d|--debug:        enable debug output
  -a|--architecture: architecture family to check against, one of
                     i486, i686, pentium4

EOF
  exit 1
}

VERBOSE=0
log( ) {
  if test $VERBOSE = 1; then
    echo $*
  fi
}

DEBUG=0
debug( ) {
  if test $DEBUG = 1; then
    echo $*
  fi
}

EXIT_CODE=0
err( ) {
  echo "ERROR: $*"
  EXIT_CODE=1
}

tmp_dir=$(mktemp -d "${work_dir}/tmp.check-opcodes.XXXXXX")
trap "rm -rf --one-file-system '${tmp_dir:?}'" EXIT

ARCH=i686

while getopts ":va:d-h-:" opt; do
  case $opt in
    -)
      case "$OPTARG" in
        help)
          usage
          ;;
        verbose)
          VERBOSE=1
          ;;
        debug)
          DEBUG=1
          ;;
        *)
          echo "ERROR: Invalid option: --$OPTARG" >&2
          usage
          ;;
      esac
      ;;
    h)
      usage
      ;;
    v)
      VERBOSE=1
      ;;
    d)
      DEBUG=1
      ;;
    a)
      ARCH=$OPTARG
      ;;
    \?)
      echo "ERROR: Invalid option: -$OPTARG" >&2
      usage
      ;;
  esac
done

shift $((OPTIND-1))

PACKAGE=$1

if test "x$PACKAGE" = "x"; then
  echo "ERROR: Filename of a package required as argument" >&2
  usage
  exit 1
fi

OPCODE_ARGS=""
case $ARCH in
  i486)
    OPCODE_ARGS='-r -a 486 -v'
    ;;
  i686)
    OPCODE_ARGS='-s MMX -s SSE -s SSE2'
    ;;
  pentium4)
    OPCODE_ARGS='-s SSE3 -s SSSE3 -s AVX'
    ;;
  *)
    echo "ERROR: architecture must currently be one of i486 and i686" >&2
    usage
    exit 1
esac

log "Checking package $PACKAGE"
log "Checking for architecture constraint: $ARCH ($OPCODE_ARGS)"
bsdtar --no-fflags -x -C $tmp_dir -f $PACKAGE

for absfile in `find $tmp_dir -type f`; do
  file=`basename $absfile`
  ext=`echo $absfile | rev | cut -f 1 -d . | rev`
  
  case "$file" in
    .PKGINFO|.BUILDINFO|.MTREE)
      debug "Skipping $file (internal Archlinux file)"
      continue
      ;;
    *.hpp|*.h|*.c|*.cpp|*.html|*.mo|*.po|*.svg|*.png|*.pl|*.pod|*.vim|*.js|*.cmake|*.xml|*.txt|*.rst|*.in|*.conf|*.[0123456789])
      debug "Skipping $file (extension is .$ext)"
      continue
      ;;
    *)
      # we cannot really ignore the file by extension
  esac

  mimetype=`file -i $absfile | cut -f 2- -d : | cut -f 1 -d ';' | tr -d ' '`  
  case "$mimetype" in
    application/x-sharedlib)
      debug "Checking library/binary: $file"
      ;;
    *)
      debug "Skipping $file (MIME type is $mimetype)"
      continue
      ;;
  esac
    
  objdump -f $absfile > $tmp_dir/$file.objdump
  arch=`grep ^architecture $tmp_dir/$file.objdump | sed 's/^architecture: //g' | cut -f 1 -d ,`
  case $arch in
    i386:x86-64)
      arch='x86_64'
      ;;
    i386)
      arch='x86'
      ;;
    *)
      arch='unknown'
      ;;
  esac
  debug "  Objdump architecture: $arch"
  
  readelf -a $absfile > $tmp_dir/$file.elf 2> $tmp_dir/$file.elferr
  archelf=`grep '^ \+Class' $tmp_dir/$file.elf | cut -f 2 -d : | tr -d ' '`
  case $archelf in
    ELF64)
      archelf='x86_64'
      ;;
    ELF32)
      archelf='x86'
      ;;
    *)
      archelf='unknown'
      ;;
  esac
  debug "  Readelf architecture: $archelf"

  if test $arch != $archelf; then
    err "$file ambigous architecture information (objdump: $arch, ELF: $archelf)"
  fi
  
  if test $arch = "x86_64"; then
    err "$file is a 64-bit library!"
    continue
  fi

  objdump -M intel -d $absfile > $tmp_dir/$file.asm
  bad_opcodes=`cat $tmp_dir/$file.asm | ${base_dir}/bin/opcode $OPCODE_ARGS -m 1 | wc -l`
  if test $bad_opcodes != 0; then
    case $ARCH in
      i486)
        err "$file is not built for plain i486 opcodes"
        ;;
      i686)
        err "$file contains MMX, SSE or SSE2 opcodes"
        ;;
      pentium4)
        err "$file contains SSE3 or newer opcodes"
        ;;
    esac
  else
    if test $VERBOSE = 1; then
      log "$file fullfills architecture constraint for $ARCH"
    fi
  fi
  
done

exit $EXIT_CODE