Skip to content

Commit 2dcbe2e

Browse files
committed
stuff
1 parent 29b516a commit 2dcbe2e

3 files changed

Lines changed: 126 additions & 26 deletions

File tree

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/monoidic/go-api-parser
33
go 1.20
44

55
require (
6-
golang.org/x/mod v0.10.0
7-
golang.org/x/tools v0.8.0
6+
golang.org/x/mod v0.12.0
7+
golang.org/x/tools v0.12.0
88
)
99

10-
require golang.org/x/sys v0.8.0 // indirect
10+
require golang.org/x/sys v0.11.0 // indirect

go.sum

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
2-
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
3-
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
4-
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
5-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6-
golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y=
7-
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
1+
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
2+
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
3+
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
4+
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
5+
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6+
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
7+
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=

params.py

Lines changed: 116 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,125 @@
1212

1313
from ghidra.program.model import data
1414

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:
19118
definitions = json.load(fd)
20119

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'
25129

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
29132
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):
34134
new_definitions = definitions[tag]
35135
for key in ('Aliases', 'Funcs', 'Interfaces', 'Structs', 'Types'):
36136
prog_definitions[key].update(new_definitions[key])
@@ -532,7 +632,7 @@ def get_params(param_types):
532632
533633
"""
534634
result_params = []
535-
stack_offset = 0
635+
stack_offset = 8
536636
I = 0
537637
FP = 0
538638
# print(param_types)

0 commit comments

Comments
 (0)