19
19
import java .util .*;
20
20
21
21
import ghidra .app .util .bin .BinaryReader ;
22
+ import ghidra .app .util .bin .format .golang .GoConstants ;
22
23
import ghidra .app .util .bin .format .golang .rtti .types .GoMethod .GoMethodInfo ;
23
24
import ghidra .app .util .bin .format .golang .structmapping .*;
25
+ import ghidra .formats .gfilesystem .FSUtilities ;
26
+ import ghidra .framework .store .LockException ;
27
+ import ghidra .program .database .sourcemap .SourceFile ;
24
28
import ghidra .program .model .address .*;
25
29
import ghidra .program .model .data .ArrayDataType ;
26
30
import ghidra .program .model .data .DataType ;
27
31
import ghidra .program .model .listing .Function ;
32
+ import ghidra .program .model .listing .Program ;
33
+ import ghidra .program .model .sourcemap .SourceFileManager ;
34
+ import ghidra .util .Msg ;
28
35
import ghidra .util .NumericUtilities ;
29
36
import ghidra .util .exception .CancelledException ;
30
37
@@ -361,6 +368,61 @@ public GoSourceFileInfo getSourceFileInfo() throws IOException {
361
368
return null ;
362
369
}
363
370
371
+ String fileName = getSourceFilename (fileno );
372
+ return fileName != null ? new GoSourceFileInfo (fileName , lineNum ) : null ;
373
+ }
374
+
375
+ public void markupSourceFileInfo () {
376
+ GoModuledata moduledata = getModuledata ();
377
+ if (moduledata == null ) {
378
+ return ;
379
+ }
380
+ Program program = programContext .getProgram ();
381
+ SourceFileManager sfman = program .getSourceFileManager ();
382
+
383
+ try {
384
+ GoPcValueEvaluator fileEval = new GoPcValueEvaluator (this , pcfile );
385
+ GoPcValueEvaluator lineEval = new GoPcValueEvaluator (this , pcln );
386
+
387
+ long startpc = entry ;
388
+ long prevFilenum = -1 ;
389
+ int lineNum ;
390
+ while ((lineNum = lineEval .evalNext ()) > 0 ) {
391
+ int fileNum = fileEval .eval (startpc );
392
+ if (fileNum < 0 ) {
393
+ break ;
394
+ }
395
+ fileEval .reset ();
396
+
397
+ if (fileNum != prevFilenum ) {
398
+ prevFilenum = fileNum ;
399
+ String fileName = getSourceFilename (fileNum );
400
+ if (!GoConstants .GOLANG_AUTOGENERATED_FILENAME .equals (fileName )) {
401
+ fileName = FSUtilities .normalizeNativePath (fileName );
402
+
403
+ Address startAddr = programContext .getCodeAddress (startpc );
404
+ long len = lineEval .getPC () - startpc ;
405
+
406
+ try {
407
+ SourceFile sourceFile = new SourceFile (fileName );
408
+ sfman .addSourceFile (sourceFile );
409
+ sfman .addSourceMapEntry (sourceFile , lineNum , startAddr , len );
410
+ }
411
+ catch (AddressOverflowException e ) {
412
+ Msg .error (this , "Failed to add source file mapping" , e );
413
+ }
414
+ }
415
+ }
416
+ startpc = lineEval .getPC ();
417
+ }
418
+ }
419
+ catch (LockException | IOException e ) {
420
+ Msg .error (this , "Failed to set source file info" , e );
421
+ }
422
+ }
423
+
424
+ private String getSourceFilename (int fileno ) throws IOException {
425
+ GoModuledata moduledata = getModuledata ();
364
426
long fileoff ;
365
427
GoSlice cutab = moduledata .getCutab ();
366
428
GoSlice filetab = moduledata .getFiletab ();
@@ -376,7 +438,7 @@ public GoSourceFileInfo getSourceFileInfo() throws IOException {
376
438
String fileName = fileoff >= 0 // -1 == no value
377
439
? nameSlice .getElementReader (1 , (int ) fileoff ).readNextUtf8String ()
378
440
: null ;
379
- return fileName != null ? new GoSourceFileInfo ( fileName , lineNum ) : null ;
441
+ return fileName ;
380
442
}
381
443
382
444
/**
0 commit comments