1#!/usr/bin/env python 2 3# 4# genv8constants.py output_file libv8_base.a 5# 6# Emits v8dbg constants stored in libv8_base.a in a format suitable for the V8 7# ustack helper. 8# 9 10from __future__ import print_function 11import re 12import subprocess 13import sys 14import errno 15 16if len(sys.argv) != 3: 17 print("usage: objsym.py outfile libv8_base.a") 18 sys.exit(2) 19 20outfile = open(sys.argv[1], 'w') 21try: 22 pipe = subprocess.Popen([ 'objdump', '-z', '-D', sys.argv[2] ], 23 bufsize=-1, stdout=subprocess.PIPE).stdout 24except OSError as e: 25 if e.errno == errno.ENOENT: 26 print(''' 27 Node.js compile error: could not find objdump 28 29 Check that GNU binutils are installed and included in PATH 30 ''') 31 else: 32 print('problem running objdump: ', e.strerror) 33 34 sys.exit() 35 36pattern = re.compile('([0-9a-fA-F]{8}|[0-9a-fA-F]{16}) <(.*)>:') 37v8dbg = re.compile('^v8dbg.*$') 38numpattern = re.compile('^[0-9a-fA-F]{2} $') 39octets = 4 40 41outfile.write(""" 42/* 43 * File automatically generated by genv8constants. Do not edit. 44 * 45 * The following offsets are dynamically from libv8_base.a. See src/v8ustack.d 46 * for details on how these values are used. 47 */ 48 49#ifndef V8_CONSTANTS_H 50#define V8_CONSTANTS_H 51 52""") 53 54curr_sym = None 55curr_val = 0 56curr_octet = 0 57 58def out_reset(): 59 global curr_sym, curr_val, curr_octet 60 curr_sym = None 61 curr_val = 0 62 curr_octet = 0 63 64def out_define(): 65 global curr_sym, curr_val, curr_octet, outfile, octets 66 if curr_sym != None: 67 wrapped_val = curr_val & 0xffffffff 68 if curr_val & 0x80000000 != 0: 69 wrapped_val = 0x100000000 - wrapped_val 70 outfile.write("#define %s -0x%x\n" % (curr_sym.upper(), wrapped_val)) 71 else: 72 outfile.write("#define %s 0x%x\n" % (curr_sym.upper(), wrapped_val)) 73 out_reset() 74 75for line in pipe: 76 if curr_sym != None: 77 # 78 # This bit of code has nasty knowledge of the objdump text output 79 # format, but this is the most obvious robust approach. We could almost 80 # rely on looking at numbered fields, but some instructions look very 81 # much like hex numbers (e.g., "adc"), and we don't want to risk picking 82 # those up by mistake, so we look at character-based columns instead. 83 # 84 for i in range(0, 3): 85 # 6-character margin, 2-characters + 1 space for each field 86 idx = 6 + i * 3 87 octetstr = line[idx:idx+3] 88 if curr_octet > octets: 89 break 90 91 if not numpattern.match(octetstr): 92 break 93 94 curr_val += int('0x%s' % octetstr, 16) << (curr_octet * 8) 95 curr_octet += 1 96 97 match = pattern.match(line) 98 if match == None: 99 continue 100 101 # Print previous symbol 102 out_define() 103 104 v8match = v8dbg.match(match.group(2)) 105 if v8match != None: 106 out_reset() 107 curr_sym = match.group(2) 108 109# Print last symbol 110out_define() 111 112outfile.write(""" 113 114#endif /* V8_CONSTANTS_H */ 115""") 116