Skip to content

Commit 710e571

Browse files
committed
♻️ refactor
1 parent f54bf0a commit 710e571

File tree

2 files changed

+58
-85
lines changed

2 files changed

+58
-85
lines changed

src/main/java/gotools/GoFunctionAnalyzer.java

+31-59
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package gotools;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
63
import ghidra.app.cmd.function.CreateFunctionCmd;
74
import ghidra.app.services.AnalysisPriority;
85
import ghidra.app.services.AnalyzerType;
@@ -26,8 +23,6 @@
2623

2724
public class GoFunctionAnalyzer extends GoTypesAnalyzer {
2825

29-
private Map<Long, Long> funcmap = new HashMap<>();
30-
3126
public GoFunctionAnalyzer() {
3227
super("Go Function Analyzer", "Recovers function names in go binaries.", AnalyzerType.BYTE_ANALYZER);
3328
setPriority(AnalysisPriority.DATA_TYPE_PROPOGATION.before());
@@ -93,34 +88,8 @@ private void getInformation12(Program p, TaskMonitor m, MessageLog log, MemoryBl
9388
continue;
9489
}
9590
Address funcPointer = p.getAddressFactory().getDefaultAddressSpace().getAddress(funcOffset);
96-
Function f = p.getFunctionManager().getFunctionAt(funcPointer);
9791
String functionName = (String) (d.getValue());
98-
if (functionName.startsWith("type..") || functionName.endsWith(".")) {
99-
// TODO what to do with it?
100-
p.getListing().setComment(funcPointer, CodeUnit.EOL_COMMENT, functionName);
101-
continue;
102-
}
103-
if (gopc.contains(funcPointer)) {
104-
log.appendMsg(String.format("skipped %s because it is in the section", functionName));
105-
continue;
106-
}
107-
if (f == null) {
108-
CreateFunctionCmd cmd = new CreateFunctionCmd(functionName, funcPointer, null, SourceType.ANALYSIS);
109-
if (!cmd.applyTo(p, m)) {
110-
log.appendMsg(
111-
String.format("Unable to create function at %s, (expected %s)\n", d.getAddress(), d.getValue()));
112-
}
113-
continue;
114-
} else if (f.getName().equals(functionName)) {
115-
continue;
116-
}
117-
try {
118-
f.setName(functionName, SourceType.ANALYSIS);
119-
funcmap.put(nameOffset, funcPointer.getOffset());
120-
} catch (DuplicateNameException | InvalidInputException e) {
121-
log.appendException(e);
122-
continue;
123-
}
92+
setFunctionName(p, m, log, gopc, funcPointer, functionName, d);
12493
}
12594
}
12695

@@ -161,35 +130,38 @@ private void getInformation116(Program p, TaskMonitor m, MessageLog log, MemoryB
161130
continue;
162131
}
163132
Address funcPointer = p.getAddressFactory().getDefaultAddressSpace().getAddress(funcEntryPoint);
164-
Function f = p.getFunctionManager().getFunctionAt(funcPointer);
165133
String functionName = (String) (d.getValue());
166-
if (functionName.startsWith("type..") || functionName.endsWith(".")) {
167-
// TODO what to do with it?
168-
p.getListing().setComment(funcPointer, CodeUnit.EOL_COMMENT, functionName);
169-
continue;
170-
}
171-
if (gopc.contains(funcPointer)) {
172-
log.appendMsg(String.format("skipped %s because it is in the section", functionName));
173-
continue;
174-
}
175-
if (f == null) {
176-
CreateFunctionCmd cmd = new CreateFunctionCmd(functionName, funcPointer, null, SourceType.ANALYSIS);
177-
if (!cmd.applyTo(p, m)) {
178-
log.appendMsg(
179-
String.format("Unable to create function at %s, (expected %s)\n", d.getAddress(), d.getValue()));
180-
}
181-
continue;
182-
} else if (f.getName().equals(functionName)) {
183-
funcmap.put((long) funcNameOffset, funcPointer.getOffset());
184-
continue;
185-
}
186-
try {
187-
f.setName(functionName, SourceType.ANALYSIS);
188-
funcmap.put((long) funcNameOffset, funcPointer.getOffset());
189-
} catch (DuplicateNameException | InvalidInputException e) {
190-
log.appendException(e);
191-
continue;
134+
setFunctionName(p, m, log, gopc, funcPointer, functionName, d);
135+
136+
}
137+
}
138+
139+
private void setFunctionName(Program p, TaskMonitor m, MessageLog log, MemoryBlock gopc, Address funcPointer,
140+
String functionName, Data d) {
141+
Function f = p.getFunctionManager().getFunctionAt(funcPointer);
142+
if (functionName.startsWith("type..") || functionName.endsWith(".")) {
143+
// TODO what to do with it?
144+
p.getListing().setComment(funcPointer, CodeUnit.EOL_COMMENT, functionName);
145+
return;
146+
}
147+
if (gopc.contains(funcPointer)) {
148+
log.appendMsg(String.format("skipped %s because it is in the section", functionName));
149+
return;
150+
}
151+
if (f == null) {
152+
CreateFunctionCmd cmd = new CreateFunctionCmd(functionName, funcPointer, null, SourceType.ANALYSIS);
153+
if (!cmd.applyTo(p, m)) {
154+
log.appendMsg(String.format("Unable to create function at %s, (expected %s)\n", d.getAddress(), d.getValue()));
192155
}
156+
return;
157+
} else if (f.getName().equals(functionName)) {
158+
return;
159+
}
160+
try {
161+
f.setName(functionName, SourceType.ANALYSIS);
162+
} catch (DuplicateNameException | InvalidInputException e) {
163+
log.appendException(e);
164+
return;
193165
}
194166
}
195167
}

src/main/java/gotools/GoTypesAnalyzer.java

+27-26
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class GoTypesAnalyzer extends AnalyzerBase {
4242
"Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Uintptr", "Float32", "Float64", "Complex64", "Complex128",
4343
"Array", "Chan", "Func", "Interface", "Map", "Ptr", "Slice", "String", "Struct", "UnsafePointer" };
4444
private final CategoryPath goPath = new CategoryPath("/go");
45+
private final CategoryPath rtypePath = new CategoryPath("/go/rtype");
4546

4647
public GoTypesAnalyzer(String name, String description, AnalyzerType type) {
4748
super(name, description, type);
@@ -79,9 +80,9 @@ private void createBaseTypes(Program p) {
7980
}
8081

8182
private void createRTypes(Program p) {
82-
CategoryPath goPath = new CategoryPath("/go");
83+
8384
DataTypeManager dtm = p.getDataTypeManager();
84-
StructureDataType rtype = new StructureDataType(goPath, "Rtype", 0);
85+
StructureDataType rtype = new StructureDataType(rtypePath, "Rtype", 0);
8586
rtype.add(new QWordDataType(), "size", null);
8687
rtype.add(new QWordDataType(), "ptrdata", null);
8788
rtype.add(new QWordDataType(), "hash", null);
@@ -96,11 +97,11 @@ private void createRTypes(Program p) {
9697

9798
dtm.addDataType(rtype, DataTypeConflictHandler.KEEP_HANDLER);
9899

99-
StructureDataType bitvector = new StructureDataType(goPath, "bitvector", 0);
100+
StructureDataType bitvector = new StructureDataType(rtypePath, "Bitvector", 0);
100101
bitvector.add(new QWordDataType(), "n", null);
101102
bitvector.add(new PointerDataType(), "bytedata", null);
102103

103-
StructureDataType moduledata = new StructureDataType(goPath, "moduledata", 0);
104+
StructureDataType moduledata = new StructureDataType(rtypePath, "Moduledata", 0);
104105
moduledata.add(new PointerDataType(), "pcHeader", null);
105106
moduledata.add(dtm.getDataType(goPath, "Slice"), "funcnametab", null);
106107
moduledata.add(dtm.getDataType(goPath, "Slice"), "cutab", null);
@@ -143,38 +144,38 @@ private void createRTypes(Program p) {
143144

144145
dtm.addDataType(moduledata, DataTypeConflictHandler.KEEP_HANDLER);
145146

146-
StructureDataType arr = new StructureDataType("RArray", 0);
147+
StructureDataType arr = new StructureDataType(rtypePath, "Array", 0);
147148
arr.add(new PointerDataType(), 8, "elem", "array element type");
148149
arr.add(new PointerDataType(), 8, "slice", null);
149150
arr.add(new QWordDataType(), "len", null);
150151

151152
dtm.addDataType(arr, DataTypeConflictHandler.KEEP_HANDLER);
152153

153-
StructureDataType chan = new StructureDataType("RChan", 0);
154+
StructureDataType chan = new StructureDataType(rtypePath, "Chan", 0);
154155
chan.add(new PointerDataType(), "elem", "channel element type");
155156
chan.add(new QWordDataType(), "dir", "channel direction");
156157

157158
dtm.addDataType(chan, DataTypeConflictHandler.KEEP_HANDLER);
158159

159-
StructureDataType func = new StructureDataType("RFunc", 0);
160+
StructureDataType func = new StructureDataType(rtypePath, "Func", 0);
160161
func.add(new UnsignedShortDataType(), "in", "function argment number");
161162
func.add(new QWordDataType(), "out", "function return number");
162163

163164
dtm.addDataType(func, DataTypeConflictHandler.KEEP_HANDLER);
164-
// internal type, there is no rtype before this type
165-
StructureDataType imethod = new StructureDataType("RInterfaceMethod", 0);
165+
166+
StructureDataType imethod = new StructureDataType(rtypePath, "InterfaceMethod", 0);
166167
imethod.add(new QWordDataType(), "nameOff", "name of method");
167168
imethod.add(new QWordDataType(), "typeOff", ".(*FuncType) underneath");
168169

169170
dtm.addDataType(imethod, DataTypeConflictHandler.KEEP_HANDLER);
170171

171-
StructureDataType iface = new StructureDataType("RInterface", 0);
172+
StructureDataType iface = new StructureDataType(rtypePath, "Interface", 0);
172173
iface.add(new QWordDataType(), "pkgPath", "import path");
173174
iface.add(dtm.getDataType(goPath, "Slice"), "methods", "interface data");
174175

175176
dtm.addDataType(iface, DataTypeConflictHandler.KEEP_HANDLER);
176177

177-
StructureDataType map = new StructureDataType("RMap", 0);
178+
StructureDataType map = new StructureDataType(rtypePath, "Map", 0);
178179
map.add(new PointerDataType(), "key", "map key type");
179180
map.add(new PointerDataType(), "elem", "map element type");
180181
map.add(new PointerDataType(), "buckets", "hash function");
@@ -185,25 +186,25 @@ private void createRTypes(Program p) {
185186

186187
dtm.addDataType(map, DataTypeConflictHandler.KEEP_HANDLER);
187188

188-
StructureDataType ptr = new StructureDataType("RPtr", 0);
189+
StructureDataType ptr = new StructureDataType(rtypePath, "Ptr", 0);
189190
ptr.add(new PointerDataType(), "elem", "pointer type");
190191

191192
dtm.addDataType(ptr, DataTypeConflictHandler.KEEP_HANDLER);
192193

193-
StructureDataType slice = new StructureDataType("RSlice", 0);
194+
StructureDataType slice = new StructureDataType(rtypePath, "Slice", 0);
194195
slice.add(new PointerDataType(), "data", "slice data");
195196
slice.add(new QWordDataType(), "elem", "slice element type");
196197

197198
dtm.addDataType(slice, DataTypeConflictHandler.KEEP_HANDLER);
198199

199-
StructureDataType structfield = new StructureDataType("GoRStructField", 0);
200+
StructureDataType structfield = new StructureDataType(rtypePath, "StructField", 0);
200201
structfield.add(new PointerDataType(), "name", "name of field");
201202
structfield.add(new PointerDataType(), "typ", "type of field");
202203
structfield.add(new QWordDataType(), "offset", "offset of field");
203204

204205
dtm.addDataType(structfield, DataTypeConflictHandler.KEEP_HANDLER);
205206

206-
StructureDataType struct = new StructureDataType("GoRStruct", 0);
207+
StructureDataType struct = new StructureDataType(rtypePath, "Struct", 0);
207208
struct.add(new PointerDataType(), "name", "name of struct");
208209
struct.add(dtm.getDataType(goPath, "Slice"), "fields", "sorted by offset");
209210

@@ -236,7 +237,7 @@ private void analyzeModuleData(Program p, TaskMonitor m, MessageLog log) {
236237
.getAddress(typeBase.getOffset() + offset);
237238
Map<String, Data> rtype = setRType(p, typeAddress);
238239
int idx = (int) rtype.get("kind").getValue();
239-
typeAddress = typeAddress.add(p.getDataTypeManager().getDataType(goPath, "RType").getLength());
240+
typeAddress = typeAddress.add(p.getDataTypeManager().getDataType(rtypePath, "RType").getLength());
240241
idx = idx & ((1 << 5) - 1); // mask kind
241242
if (idx > 27) {
242243
idx = 0;
@@ -344,7 +345,7 @@ private void analyzeModuleData(Program p, TaskMonitor m, MessageLog log) {
344345
}
345346

346347
private Map<String, Data> setRArrayType(Program p, Address a) throws Exception {
347-
DataType arrayType = p.getDataTypeManager().getDataType(goPath, "RArray");
348+
DataType arrayType = p.getDataTypeManager().getDataType(rtypePath, "Array");
348349
p.getListing().clearCodeUnits(a, a.add(arrayType.getLength()), true);
349350
Map<String, Data> ret = new HashMap<>();
350351
Data d = p.getListing().createData(a, arrayType);
@@ -355,7 +356,7 @@ private Map<String, Data> setRArrayType(Program p, Address a) throws Exception {
355356
}
356357

357358
private Map<String, Data> setRChanType(Program p, Address a) throws Exception {
358-
DataType chanType = p.getDataTypeManager().getDataType(goPath, "RChan");
359+
DataType chanType = p.getDataTypeManager().getDataType(rtypePath, "Chan");
359360
p.getListing().clearCodeUnits(a, a.add(chanType.getLength()), true);
360361
Map<String, Data> ret = new HashMap<>();
361362
Data d = p.getListing().createData(a, chanType);
@@ -365,7 +366,7 @@ private Map<String, Data> setRChanType(Program p, Address a) throws Exception {
365366
}
366367

367368
private Map<String, Data> setRFuncType(Program p, Address a) throws Exception {
368-
DataType funcType = p.getDataTypeManager().getDataType(goPath, "RFunc");
369+
DataType funcType = p.getDataTypeManager().getDataType(rtypePath, "Func");
369370
p.getListing().clearCodeUnits(a, a.add(funcType.getLength()), true);
370371
Map<String, Data> ret = new HashMap<>();
371372
Data d = p.getListing().createData(a, funcType);
@@ -375,7 +376,7 @@ private Map<String, Data> setRFuncType(Program p, Address a) throws Exception {
375376
}
376377

377378
private Map<String, Data> setRInterfaceType(Program p, Address a) throws Exception {
378-
DataType interfaceType = p.getDataTypeManager().getDataType(goPath, "RInterface");
379+
DataType interfaceType = p.getDataTypeManager().getDataType(rtypePath, "Interface");
379380
p.getListing().clearCodeUnits(a, a.add(interfaceType.getLength()), true);
380381
Map<String, Data> ret = new HashMap<>();
381382
Data d = p.getListing().createData(a, interfaceType);
@@ -385,7 +386,7 @@ private Map<String, Data> setRInterfaceType(Program p, Address a) throws Excepti
385386
}
386387

387388
private Map<String, Data> setRMapType(Program p, Address a) throws Exception {
388-
DataType mapType = p.getDataTypeManager().getDataType(goPath, "RMap");
389+
DataType mapType = p.getDataTypeManager().getDataType(rtypePath, "Map");
389390
p.getListing().clearCodeUnits(a, a.add(mapType.getLength()), true);
390391
Map<String, Data> ret = new HashMap<>();
391392
Data d = p.getListing().createData(a, mapType);
@@ -401,7 +402,7 @@ private Map<String, Data> setRMapType(Program p, Address a) throws Exception {
401402
}
402403

403404
private Map<String, Data> setRPtrType(Program p, Address a) throws Exception {
404-
DataType ptrType = p.getDataTypeManager().getDataType(goPath, "RPtr");
405+
DataType ptrType = p.getDataTypeManager().getDataType(rtypePath, "Ptr");
405406
p.getListing().clearCodeUnits(a, a.add(ptrType.getLength()), true);
406407
Map<String, Data> ret = new HashMap<>();
407408
Data d = p.getListing().createData(a, ptrType);
@@ -410,7 +411,7 @@ private Map<String, Data> setRPtrType(Program p, Address a) throws Exception {
410411
}
411412

412413
private Map<String, Data> setRSliceType(Program p, Address a) throws Exception {
413-
DataType sliceType = p.getDataTypeManager().getDataType(goPath, "RSlice");
414+
DataType sliceType = p.getDataTypeManager().getDataType(rtypePath, "Slice");
414415
p.getListing().clearCodeUnits(a, a.add(sliceType.getLength()), true);
415416
Map<String, Data> ret = new HashMap<>();
416417
Data d = p.getListing().createData(a, sliceType);
@@ -419,7 +420,7 @@ private Map<String, Data> setRSliceType(Program p, Address a) throws Exception {
419420
}
420421

421422
private Map<String, Data> setRStructType(Program p, Address a) throws Exception {
422-
DataType structType = p.getDataTypeManager().getDataType(goPath, "RStruct");
423+
DataType structType = p.getDataTypeManager().getDataType(rtypePath, "Struct");
423424
p.getListing().clearCodeUnits(a, a.add(structType.getLength()), true);
424425
Map<String, Data> ret = new HashMap<>();
425426
Data d = p.getListing().createData(a, structType);
@@ -429,7 +430,7 @@ private Map<String, Data> setRStructType(Program p, Address a) throws Exception
429430
}
430431

431432
private Map<String, Data> setRType(Program p, Address a) throws Exception {
432-
DataType rType = p.getDataTypeManager().getDataType(goPath, "RType");
433+
DataType rType = p.getDataTypeManager().getDataType(rtypePath, "Rtype");
433434
p.getListing().clearCodeUnits(a, a.add(rType.getLength()), true);
434435
Map<String, Data> ret = new HashMap<>();
435436
Data d = p.getListing().createData(a, rType);
@@ -448,7 +449,7 @@ private Map<String, Data> setRType(Program p, Address a) throws Exception {
448449
}
449450

450451
private Map<String, Data> setModuleData(Program p, Address a) throws Exception {
451-
DataType moduleData = p.getDataTypeManager().getDataType(goPath, "RModule");
452+
DataType moduleData = p.getDataTypeManager().getDataType(rtypePath, "Moduledata");
452453
p.getListing().clearCodeUnits(a, a.add(moduleData.getLength()), true);
453454
Map<String, Data> ret = new HashMap<>();
454455
Data d = p.getListing().createData(a, moduleData);

0 commit comments

Comments
 (0)