Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
dipusone committed Jan 2, 2023
1 parent 3580fd7 commit 0418078
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions golang_parser.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import binaryninja as bn

from binaryninja import Symbol, SymbolType, redirect_output_to_log
from binaryninja import Symbol, SymbolType

from .binaryninja_types import *
from .types import *

NAME = 'Golang Loader Helper'
GoFixLogger = bn.Logger(0, NAME)


log_debug = GoFixLogger.log_debug
log_debug = GoFixLogger.log_debug
log_info = GoFixLogger.log_info
log_warn = GoFixLogger.log_warn
log_error = GoFixLogger.log_error
Expand Down Expand Up @@ -58,7 +57,7 @@ def init_gopclntab(self):
else:
log_error("Failed to find section .gopclntab")
return

self.gopclntab = GoPclnTab(start_addr,
end_addr,
self.bv[start_addr:end_addr]
Expand Down Expand Up @@ -92,7 +91,7 @@ def init_gopclntab(self):
self.gopclntab.funcnametab = self.gopclntab.range(3, 4) # This contains the names of the functions
self.gopclntab.cutab = self.gopclntab.range(4, 5)
self.gopclntab.filetab = self.gopclntab.range(5, 6)
self.gopclntab.pctab = self.gopclntab.range(6, 7)
self.gopclntab.pctab = self.gopclntab.range(6, 7)
self.gopclntab.funcdata = self.gopclntab.data(7) # This is where the functions info are
self.gopclntab.functab = self.gopclntab.data(7)
self.gopclntab.functabsize = (self.gopclntab.nfunctab * 2 + 1) * functabFieldSize
Expand All @@ -111,7 +110,7 @@ def init_gopclntab(self):
self.gopclntab.nfunctab = self.gopclntab.uintptr(8)
self.gopclntab.funcdata = self.gopclntab.raw
self.gopclntab.funcnametab = self.gopclntab.raw
self.gopclntab.functab = self.gopclntab.data_after_offset(8+self.gopclntab.ptrsize)
self.gopclntab.functab = self.gopclntab.data_after_offset(8 + self.gopclntab.ptrsize)
self.gopclntab.functabsize = (self.gopclntab.nfunctab * 2 + 1) * functabFieldSize
self.gopclntab.functab = self.gopclntab.functab[:self.gopclntab.functabsize]
else:
Expand Down Expand Up @@ -154,15 +153,15 @@ def get_function_around(self, addr):
if not bb:
return None
return bb.function

@property
def ptr_size(self):
return self.gopclntab.ptrsize

def quick_go_version(self) -> GoVersion:
gopclntab = self.get_section_by_name(".gopclntab")
start_addr = gopclntab.start
return GoVersion.from_magic(self.bv[start_addr:start_addr+6])
return GoVersion.from_magic(self.bv[start_addr:start_addr + 6])

def read_varint(self, start_addr: int) -> (int, int):
shift = 0
Expand Down Expand Up @@ -217,7 +216,7 @@ def rename_functions(self):
renamed += 1
else:
log_warn(f"not using function name {name} for function at {hex(function_addr)}")

log_info(f"Created {created} functions")
log_info(f"Renamed {renamed - created} functions")
log_info(f"Total {renamed} functions")
Expand All @@ -237,8 +236,7 @@ class TypeParser(GoHelper):
'runtime.makechan',
'runtime.makemap',
'runtime.mapiterinit',
'runtime.makeslice'
]
'runtime.makeslice']

MAX_TYPE_LENGHT = 40

Expand All @@ -257,7 +255,7 @@ def create_types(self):

for go_type in self.TYPES:
name, type_str = go_type
new_type = self.bv.parse_type_string(type_str)
new_type = self.bv.parse_type_string(type_str)
if len(new_type) == 0:
log_warn(f"Unable to parse type string {name}")
continue
Expand All @@ -268,7 +266,7 @@ def create_types(self):
log_info("Searching for functions accessing type objects")
log_info(f"Will search for {len(self.TYPED)} functions")
created = 0

for typed_function in self.TYPED:
functions = self.bv.get_functions_by_name(typed_function)
if not functions:
Expand All @@ -294,7 +292,7 @@ def create_types(self):
# funny enough `not <void var>` will return `True`
if go_data_type is None:
continue

go_data_type.type = golang_type
# TODO figure out why sometime the type info are not there
# the next portion of code might fail
Expand Down Expand Up @@ -346,4 +344,3 @@ def parse_go_file(bv):
fr.join()
tp = TypeParser(bv)
return tp.start()

0 comments on commit 0418078

Please sign in to comment.