|
12 | 12 |
|
13 | 13 | from ghidra.program.model import data |
14 | 14 |
|
15 | | -# The file 'out.json' which contains function signatures is loaded |
16 | | -# The file is expected to be in the same directory as this script file |
17 | | -filename = os.path.dirname(__file__) + '/out.json' |
18 | | -with open(filename) as fd: |
| 15 | + |
| 16 | + |
| 17 | +GO_MAXVER = 23 |
| 18 | + |
| 19 | +versions = ['go1.%d' % num for num in range(GO_MAXVER+1)[::-1]] |
| 20 | + |
| 21 | +#Find section by name |
| 22 | +def getSection(name): |
| 23 | + block = getMemoryBlock(name) |
| 24 | + if block is None: |
| 25 | + print "No %s section found." % name |
| 26 | + return None |
| 27 | + |
| 28 | + start = block.getStart() |
| 29 | + end = block.getEnd() |
| 30 | + print "%s [start: 0x%x, end: 0x%x]" % (block.getName(), start.getOffset(), end.getOffset()) |
| 31 | + return start, end |
| 32 | + |
| 33 | +#Find version by string search in specific memory block |
| 34 | +def findVersion(name): |
| 35 | + section = getSection(name) |
| 36 | + if section is None: |
| 37 | + return None |
| 38 | + start, end = section |
| 39 | + address_set = ghidra.program.model.address.AddressSet(start, end) |
| 40 | + |
| 41 | + for version in versions: |
| 42 | + if findBytes(address_set, version, 1, 1): |
| 43 | + print "Version found" |
| 44 | + print version |
| 45 | + return version |
| 46 | + |
| 47 | + print('version not found in section') |
| 48 | + return None |
| 49 | + |
| 50 | +def apply_delta(a, delta): |
| 51 | + """ |
| 52 | + Applies the changes specified in the `delta` dictionary to the `a` dictionary. |
| 53 | +
|
| 54 | + Args: |
| 55 | + a (dict): The original dictionary to modify. |
| 56 | + delta (dict): The changes to apply to the original dictionary. This should be in the same |
| 57 | + format as the output of the `get_delta` function. That is, the keys are paths |
| 58 | + (formatted as 'key1->key2->key3') to the values that should be changed, and |
| 59 | + the values are the new values. If the new value is "_DELETED_", the key at |
| 60 | + that path is deleted. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + dict: The dictionary `a` after applying the changes specified in `delta`. |
| 64 | +
|
| 65 | + Note: |
| 66 | + This function modifies the dictionary `a` in-place, but also returns it for convenience. |
| 67 | + Ensure that this side effect is acceptable in your context. |
| 68 | +
|
| 69 | + Examples: |
| 70 | + >> a = {'x': 1, 'y': {'a': 10, 'b': 20}} |
| 71 | + >> delta = {'x': 2, 'y->b': 30, 'y->a': '_DELETED_'} |
| 72 | + >> apply_delta(a, delta) |
| 73 | + {'x': 2, 'y': {'b': 30}} |
| 74 | + """ |
| 75 | + for key_path, value in delta.items(): |
| 76 | + key_list = key_path.split("->") |
| 77 | + current = a |
| 78 | + for key in key_list[:-1]: |
| 79 | + current = current[key] |
| 80 | + |
| 81 | + if value == "_DELETED_": |
| 82 | + del current[key_list[-1]] |
| 83 | + else: |
| 84 | + current[key_list[-1]] = value |
| 85 | + |
| 86 | + return a |
| 87 | + |
| 88 | + |
| 89 | +unix_os = ['aix', 'android', 'darwin', 'dragonfly', 'freebsd', 'hurd', 'illumos', 'ios', 'linux', 'netbsd', 'openbsd', 'solaris'] |
| 90 | + |
| 91 | +def matching_architectures(os, arch, cgo): |
| 92 | + matches = [os, arch, '{}-{}'.format(os, arch), 'all'] |
| 93 | + if os in unix_os: |
| 94 | + matches.append('unix') |
| 95 | + if cgo: |
| 96 | + matches.extend(['cgo', '{}-{}-cgo'.format(os, arch)]) |
| 97 | + return matches |
| 98 | + |
| 99 | +# version = findVersion('.go.buildinfo') |
| 100 | +version = 'go1.14' |
| 101 | + |
| 102 | +dirname = os.path.dirname(__file__) + '/go_deduped/' |
| 103 | +matches = sorted( |
| 104 | + (path for path in glob.glob(dirname + '*.json')), |
| 105 | + key=lambda e: [int(x) for x in e.rsplit('/', 1)[-1][2:].replace('_delta', '').replace('.json', '').split('.')] |
| 106 | +) |
| 107 | + |
| 108 | +indexes = [ |
| 109 | + path.rsplit('/', 1)[-1].replace('_delta', '').replace('.json', '') |
| 110 | + for path in matches |
| 111 | +] |
| 112 | +end_index = indexes.index(version) + 1 |
| 113 | +matches = matches[:end_index] |
| 114 | + |
| 115 | +print(matches) |
| 116 | + |
| 117 | +with open(matches[0]) as fd: |
19 | 118 | definitions = json.load(fd) |
20 | 119 |
|
21 | | -# Architecture-specific code follows |
22 | | -# As of now, only windows-amd64 is supported. Support for other architectures |
23 | | -# can be added in the future. |
24 | | -current_arch = 'windows-amd64' # The current architecture |
| 120 | +for match in matches[1:]: |
| 121 | + with open(match) as fd: |
| 122 | + delta = json.load(fd) |
| 123 | + apply_delta(definitions, delta) |
| 124 | + |
| 125 | +# current architecture, for architectures-specific functions |
| 126 | +# TODO Ghidra can report platform info, get this dynamically |
| 127 | +# once more platforms are supported |
| 128 | +current_arch = 'linux-amd64' |
25 | 129 |
|
26 | | -# Function signatures that apply to all architectures are retrieved, |
27 | | -# along with those that are specific to the current architecture. |
28 | | -# This information is stored in the prog_definitions dictionary. |
| 130 | +# get function signatures applying to all architectures |
| 131 | +# + those specific to the current architecture |
29 | 132 | prog_definitions = definitions['all'] |
30 | | -for tag in set(( |
31 | | - 'cgo', 'amd64', 'windows', 'windows-amd64', |
32 | | - 'windows-cgo', 'windows-amd64-cgo', |
33 | | -)) & set(definitions): |
| 133 | +for tag in set(matching_architectures('linux', 'amd64', True)) & set(definitions): |
34 | 134 | new_definitions = definitions[tag] |
35 | 135 | for key in ('Aliases', 'Funcs', 'Interfaces', 'Structs', 'Types'): |
36 | 136 | prog_definitions[key].update(new_definitions[key]) |
@@ -532,7 +632,7 @@ def get_params(param_types): |
532 | 632 |
|
533 | 633 | """ |
534 | 634 | result_params = [] |
535 | | - stack_offset = 0 |
| 635 | + stack_offset = 8 |
536 | 636 | I = 0 |
537 | 637 | FP = 0 |
538 | 638 | # print(param_types) |
|
0 commit comments