6
6
import ghidra .app .util .importer .MessageLog ;
7
7
import ghidra .program .model .address .Address ;
8
8
import ghidra .program .model .address .AddressSetView ;
9
+ import ghidra .program .model .data .IntegerDataType ;
9
10
import ghidra .program .model .data .StringDataType ;
10
11
import ghidra .program .model .listing .CodeUnit ;
11
12
import ghidra .program .model .listing .Data ;
22
23
23
24
public class GoFunctionNameAnalyzer extends AnalyzerBase {
24
25
public GoFunctionNameAnalyzer () {
25
- super ("Go Function Name Analyzer" , "Recovers function names in go binaries." ,
26
- AnalyzerType .BYTE_ANALYZER );
26
+ super ("Go Function Name Analyzer" , "Recovers function names in go binaries." , AnalyzerType .BYTE_ANALYZER );
27
27
setPriority (AnalysisPriority .DATA_TYPE_PROPOGATION .before ());
28
28
}
29
29
30
30
@ Override
31
- public boolean added (Program p , AddressSetView set , TaskMonitor monitor , MessageLog log )
32
- throws CancelledException {
31
+ public boolean added (Program p , AddressSetView set , TaskMonitor monitor , MessageLog log ) throws CancelledException {
33
32
MemoryBlock gopcln ;
34
33
try {
35
34
gopcln = getGopclntab (p );
@@ -48,23 +47,28 @@ public boolean added(Program p, AddressSetView set, TaskMonitor monitor, Message
48
47
49
48
private void recoverGoFunctions (Program p , TaskMonitor m , MessageLog log , MemoryBlock gopc )
50
49
throws MemoryAccessException {
50
+ // TODO this only works for 64bit binaries
51
51
long pointerSize = 8 ;
52
52
Address a = gopc .getStart ();
53
53
int goVersionMagic = p .getMemory ().getInt (a );
54
- a .add (8 );
54
+ try {
55
+ createData (p , a , new IntegerDataType ());
56
+ } catch (Exception e ) {
57
+ log .appendException (e );
58
+ }
59
+ a = a .add (pointerSize );
60
+
55
61
// https://github.com/golang/go/blob/release-branch.go1.16/src/debug/gosym/pclntab.go#L169
56
62
if (goVersionMagic == 0xfffffffb ) {
57
- getInformation12 (p , m , log , a , pointerSize );
58
- }
59
- else {
60
- getInformation116 (p , m , log , a , pointerSize );
63
+ getInformation12 (p , m , log , gopc , a , pointerSize );
64
+ } else {
65
+ getInformation116 (p , m , log , gopc , a , pointerSize );
61
66
}
62
67
}
63
- }
64
- // TODO this only works for 64bit binaries
65
- }
66
- private void getInformation12 (Program p , TaskMonitor m , MessageLog log , MemoryBlock gopc , Address a , long pointerSize ){
67
- // skip unimportant header
68
+
69
+ private void getInformation12 (Program p , TaskMonitor m , MessageLog log , MemoryBlock gopc , Address a , long pointerSize )
70
+ throws MemoryAccessException {
71
+ // skip unimportant header
68
72
long size = p .getMemory ().getLong (a );
69
73
a = a .add (pointerSize );
70
74
for (int i = 0 ; i < size ; i ++) {
@@ -95,11 +99,78 @@ private void getInformation12(Program p, TaskMonitor m, MessageLog log, MemoryBl
95
99
continue ;
96
100
}
97
101
if (f == null ) {
98
- CreateFunctionCmd cmd =
99
- new CreateFunctionCmd (functionName , funcPointer , null , SourceType .ANALYSIS );
102
+ CreateFunctionCmd cmd = new CreateFunctionCmd (functionName , funcPointer , null , SourceType .ANALYSIS );
103
+ if (!cmd .applyTo (p , m )) {
104
+ log .appendMsg (
105
+ String .format ("Unable to create function at %s, (expected %s)\n " , d .getAddress (), d .getValue ()));
106
+ }
107
+ continue ;
108
+ } else if (f .getName ().equals (functionName )) {
109
+ continue ;
110
+ }
111
+ try {
112
+ f .setName (functionName , SourceType .ANALYSIS );
113
+ p .getListing ().setComment (funcPointer , CodeUnit .EOL_COMMENT , "from gotool" );
114
+ } catch (DuplicateNameException | InvalidInputException e ) {
115
+ log .appendException (e );
116
+ continue ;
117
+ }
118
+ }
119
+ }
120
+
121
+ private void getInformation116 (Program p , TaskMonitor m , MessageLog log , MemoryBlock gopc , Address a ,
122
+ long pointerSize ) throws MemoryAccessException {
123
+ Address funcDataTable , currentFuncTable ;
124
+ long size = p .getMemory ().getLong (a );
125
+ a = a .add (pointerSize * 2 );
126
+ long funcNameTableOffset = p .getMemory ().getLong (a );
127
+ a = a .add (pointerSize * 4 );
128
+ long funcDataTableOffset = p .getMemory ().getLong (a );
129
+ funcDataTable = gopc .getStart ().add (funcDataTableOffset );
130
+ currentFuncTable = gopc .getStart ().add (funcDataTableOffset );
131
+
132
+ for (int i = 0 ; i < size ; i ++) {
133
+ long funcEntryPoint , funcDataOffset ;
134
+ int funcNameOffset ;
135
+ try {
136
+ funcEntryPoint = p .getMemory ().getLong (currentFuncTable );
137
+ currentFuncTable = currentFuncTable .add (pointerSize );
138
+
139
+ funcDataOffset = p .getMemory ().getLong (currentFuncTable );
140
+ currentFuncTable = currentFuncTable .add (pointerSize );
141
+
142
+ funcNameOffset = p .getMemory ().getInt (funcDataTable .add (funcDataOffset + pointerSize ));
143
+ } catch (Exception e ) {
144
+ log .appendException (e );
145
+ continue ;
146
+ }
147
+
148
+ Address namePointer = gopc .getStart ().add (funcNameTableOffset + funcNameOffset );
149
+ Data d ;
150
+ try {
151
+ d = createData (p , namePointer , new StringDataType ());
152
+ p .getListing ().setComment (namePointer , CodeUnit .EOL_COMMENT , d .getValue ().toString ());
153
+ } catch (Exception e ) {
154
+ log .appendException (e );
155
+ continue ;
156
+ }
157
+ Address funcPointer = p .getAddressFactory ().getDefaultAddressSpace ().getAddress (funcEntryPoint );
158
+ Function f = p .getFunctionManager ().getFunctionAt (funcPointer );
159
+ String functionName = (String ) (d .getValue ());
160
+ if (functionName .startsWith ("type.." ) || functionName .endsWith ("." )) {
161
+ // TODO what to do with it?
162
+ p .getListing ().setComment (funcPointer , CodeUnit .EOL_COMMENT , functionName );
163
+ continue ;
164
+ }
165
+ if (gopc .contains (funcPointer )) {
166
+ log .appendMsg (String .format ("skipped %s because it is in the section" , functionName ));
167
+ continue ;
168
+ }
169
+ if (f == null ) {
170
+ CreateFunctionCmd cmd = new CreateFunctionCmd (functionName , funcPointer , null , SourceType .ANALYSIS );
100
171
if (!cmd .applyTo (p , m )) {
101
- log .appendMsg (String . format (
102
- "Unable to create function at %s, (expected %s)\n " , d .getAddress (), d .getValue ()));
172
+ log .appendMsg (
173
+ String . format ( "Unable to create function at %s, (expected %s)\n " , d .getAddress (), d .getValue ()));
103
174
}
104
175
continue ;
105
176
} else if (f .getName ().equals (functionName )) {
@@ -113,5 +184,4 @@ private void getInformation12(Program p, TaskMonitor m, MessageLog log, MemoryBl
113
184
}
114
185
}
115
186
}
116
- private void getInformation116 (){}
117
187
}
0 commit comments