Debian Codegraph @

scripts-build/checkinsns

raw
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5 import subprocess
6 import traceback
7
8 objdump = os.getenv('CROSS','')+'objdump'
9
10 host = sys.argv[1]
11
12 def warn(w):
13 sys.stderr.write('warning: %s\n' % w)
14 fresult.write('%s\n' % w)
15
16 # ===== setting up capstone
17
18 def capstone_disassembler():
19 try:
20 import capstone
21 except:
22 warn('this script does not know how to check instruction-set extensions without python3-capstone')
23 sys.exit(0)
24
25 # XXX: for arm32 should trace from entry points to see thumb vs. arm
26 capstoneopts = {
27 'amd64': ('CS_ARCH_X86','CS_MODE_64','CS_MODE_LITTLE_ENDIAN'),
28 'arm32': ('CS_ARCH_ARM','CS_MODE_THUMB','CS_MODE_LITTLE_ENDIAN'),
29 'arm64': ('CS_ARCH_ARM64','CS_MODE_LITTLE_ENDIAN'),
30 # 'm68k': ('CS_ARCH_M68K','CS_MODE_M68K_040','CS_MODE_BIG_ENDIAN'),
31 'mips32big': ('CS_ARCH_MIPS','CS_MODE_MIPS32','CS_MODE_BIG_ENDIAN'),
32 'mips32little': ('CS_ARCH_MIPS','CS_MODE_MIPS32','CS_MODE_LITTLE_ENDIAN'),
33 'mips64big': ('CS_ARCH_MIPS','CS_MODE_MIPS64','CS_MODE_BIG_ENDIAN'),
34 'mips64little': ('CS_ARCH_MIPS','CS_MODE_MIPS64','CS_MODE_LITTLE_ENDIAN'),
35 'mipsn32big': ('CS_ARCH_MIPS','CS_MODE_MIPS32','CS_MODE_BIG_ENDIAN'),
36 'mipsn32little':('CS_ARCH_MIPS','CS_MODE_MIPS32','CS_MODE_LITTLE_ENDIAN'),
37 'ppc32big': ('CS_ARCH_PPC','CS_MODE_32','CS_MODE_BIG_ENDIAN'),
38 'ppc32little': ('CS_ARCH_PPC','CS_MODE_32','CS_MODE_LITTLE_ENDIAN'),
39 'ppc64big': ('CS_ARCH_PPC','CS_MODE_64','CS_MODE_BIG_ENDIAN'),
40 'ppc64little': ('CS_ARCH_PPC','CS_MODE_64','CS_MODE_LITTLE_ENDIAN'),
41 'riscv32': ('CS_ARCH_RISCV','CS_MODE_RISCV64','CS_MODE_RISCVC'),
42 'riscv64': ('CS_ARCH_RISCV','CS_MODE_RISCV32','CS_MODE_RISCVC'),
43 's390': ('CS_ARCH_SYSZ','CS_MODE_BIG_ENDIAN'),
44 's390x': ('CS_ARCH_SYSZ','CS_MODE_BIG_ENDIAN'),
45 'sparc32': ('CS_ARCH_SPARC','CS_MODE_BIG_ENDIAN'),
46 'sparc64': ('CS_ARCH_SPARC','CS_MODE_V9','CS_MODE_BIG_ENDIAN'),
47 'x86': ('CS_ARCH_X86','CS_MODE_32','CS_MODE_LITTLE_ENDIAN'),
48 }
49
50 if host not in capstoneopts:
51 warn(f'this script does not know how to check instruction-set extensions for {host}')
52 sys.exit(0)
53
54 try:
55 bigendian = False
56 capstonearch = capstone.__getattribute__(capstoneopts[host][0])
57 capstonemode = 0
58 for opt in capstoneopts[host][1:]:
59 if opt == 'CS_MODE_BIG_ENDIAN': bigendian = True
60 capstonemode |= capstone.__getattribute__(opt)
61 disassembler = capstone.Cs(capstonearch,capstonemode)
62 disassembler.detail = True
63 except Exception as e:
64 warn(f'failed to initialize python3-capstone: {e}')
65 sys.exit(0)
66
67 # script checks these overrides against objdump mnemonics
68 # and against capstone mnemonics
69 capstoneoverrides = {
70 'amd64':{
71 'xgetbv':{'xsave'},
72 'vpmadd52huq':{'avx512vl','avx512ifma'},
73 'vpmadd52luq':{'avx512vl','avx512ifma'},
74 },
75 'arm64':{
76 'aese':{'aes'},
77 'aesd':{'aes'},
78 'aesmc':{'aes'},
79 'aesimc':{'aes'},
80 'sha1c':{'sha1'},
81 'sha1p':{'sha1'},
82 'sha1m':{'sha1'},
83 'sha1h':{'sha1'},
84 'sha1u0':{'sha1'},
85 'sha1u1':{'sha1'},
86 'sha256h':{'sha256'},
87 'sha256h2':{'sha256'},
88 'sha256u0':{'sha256'},
89 'sha256u1':{'sha256'},
90 'sha512h':{'sha512'},
91 'sha512h2':{'sha512'},
92 'sha512u0':{'sha512'},
93 'sha512u1':{'sha512'},
94 },
95 }
96 capstonealiases = {
97 'amd64':{
98 'int':'interrupt',
99 'sse1':'sse',
100 'novlx':'avx',
101 'vlx':'avx512vl',
102 'dqi':'avx512dq',
103 'bmi':'bmi1',
104 'avx512':'avx512f',
105 }
106 }
107 # base: instruction sets that can be assumed
108 # (see also cross-arch list below)
109 # script checks base after applying aliases
110 capstonebase = {
111 'amd64':{'fpu','cmov','sse','sse2','mode64'},
112 'arm32':{'int','v5t','v6','v6m','prev8','thumb','thumb1only','thumb2','T2EXTRACTPACK','THUMB2DSP','mulops'},
113 'arm64':{'neon','fparmv8','pointer authentication'},
114 'mips64big':{'stdenc','mips3','mips32','mips4_32','mips32r2','mips64r2','notmips32r6','notmips64r6','notinmicromips'},
115 'mips64little':{'stdenc','mips3','mips32','mips4_32','mips32r2','mips64r2','notmips32r6','notmips64r6','notinmicromips'},
116 'ppc64big':{'mode32','vsx','altivec'},
117 'ppc64little':{'mode32','vsx','altivec','p8altivec','p8vector'},
118 's390':{'distinctops','highword'},
119 's390x':{'distinctops','highword','loadstoreoncond'},
120 'sparc64':{'64bit','v9'},
121 'x86':{'mode32','fpu','cmov','not64bitmode'},
122 }
123
124 overrides = capstoneoverrides.get(host,{})
125 aliases = capstonealiases.get(host,{})
126
127 base = capstonebase.get(host,set())
128 for ext in 'call','ret','return','jump','branch_relative','privilege','interrupt':
129 base.add(ext)
130
131 extrules = aliases,base
132 return disassembler,bigendian,overrides,extrules
133
134 # ===== main work
135
136 carch = {}
137 for c in os.listdir('compilerarch'):
138 with open('compilerarch/%s' % c) as f:
139 carch[c] = f.read().strip()
140 if carch[c] == 'default': carch[c] = host
141 assert carch[c].split('+')[0] == host
142
143 def checkext(obj,archlist,ext,extrules,insnstr):
144 aliases,base = extrules
145 if ext in aliases: ext = aliases[ext]
146 extset.add(ext)
147 if ext in base: return
148 for arch in archlist:
149 if ext not in arch.split('+')[1:]:
150 warn(f'{obj}: {arch} instruction set does not allow {ext} for {insnstr}')
151
152 def dir2objs(d):
153 objs = [d+'/'+fn for fn in os.listdir(d) if fn.endswith('.o')]
154 if os.path.exists(d+'/dependencies'):
155 with open(d+'/dependencies') as f:
156 for line in f:
157 objs += dir2objs(line.strip())
158 return objs
159
160 def doit(d):
161 disassembler,bigendian,overrides,extrules = capstone_disassembler()
162
163 objs = dir2objs(d)
164 if len(objs) == 0: return
165
166 if len(d.split('/')) < 4:
167 archlist = [host] # no instruction-set extensions allowed
168 else:
169 compiler = d.split('/')[3]
170 if compiler in carch:
171 archlist = [carch[compiler]]
172 else:
173 archlist = []
174 with open(d+'/compilerdirs') as f:
175 for arch in f:
176 archlist += [carch[arch.strip()]]
177
178 try:
179 # XXX: think about how to safely exclude glink section on ppc64
180 p = subprocess.Popen([objdump,'-d']+objs,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,universal_newlines=True)
181 out,err = p.communicate()
182 except Exception as e:
183 warn('objdump exception: %s' % e)
184 return
185 if err:
186 warn('objdump error: %s' % err)
187 return
188 if p.returncode:
189 warn('objdump failure: %s' % p.returncode)
190 return
191
192 obj = 'unknown-object-file'
193 todo = b''
194 # on x86 and amd64, objdump can split insns across lines
195 # and todo accumulates instruction bytes not yet processed
196
197 for line in out.splitlines():
198 if len(objs) > 0 and line.startswith('%s:'%objs[0]):
199 obj,objs = objs[0],objs[1:]
200 if '\t' in line:
201 line = line[line.index('\t')+1:]
202 if '#' in line:
203 line = line[:line.index('#')]
204 if '<' in line:
205 line = line[:line.index('<')]
206
207 for x in line.split():
208 try:
209 x = bytes.fromhex(x)
210 except:
211 if x in overrides:
212 for ext in overrides[x]:
213 checkext(obj,archlist,ext,extrules,f'{x} (from objdump)')
214 break
215 if not bigendian:
216 x = bytes(reversed(bytearray(x)))
217 todo += x
218
219 for insn in disassembler.disasm(todo,0x1000):
220 assert todo.startswith(insn.bytes)
221 todo = todo[len(insn.bytes):]
222 if insn.mnemonic in overrides:
223 for ext in overrides[insn.mnemonic]:
224 checkext(obj,archlist,ext,extrules,f'{insn.mnemonic} {insn.op_str} (from capstone)')
225 else:
226 for g in insn.groups:
227 checkext(obj,archlist,insn.group_name(g),extrules,f'{insn.mnemonic} {insn.op_str} (from capstone)')
228
229 if host not in ('x86','amd64'):
230 if todo != b'':
231 warn(f'capstone disassembly failed, could be capstone bug: {line}')
232 todo = b''
233 if len(todo) > 32:
234 warn(f'capstone disassembly has built up more than 32 bytes, resetting')
235 todo = b''
236
237 for d in sys.argv[2:]:
238 d = d.strip()
239 extset = set()
240 with open('%s/result-insns'%d,'w') as fresult:
241 try:
242 doit(d)
243 except SystemExit as e:
244 sys.exit(0)
245 except:
246 warn(f'checking instructions failed:\n{traceback.format_exc()}')
247 with open('%s/info-insns'%d,'w') as finfo:
248 for ext in sorted(extset):
249 finfo.write(ext+'\n')