Skip to content

Instantly share code, notes, and snippets.

@zhangysh1995
Created November 2, 2018 13:01
Show Gist options
  • Save zhangysh1995/3e51f794bc1f7bc3ac259dfe33c2b1e8 to your computer and use it in GitHub Desktop.
Save zhangysh1995/3e51f794bc1f7bc3ac259dfe33c2b1e8 to your computer and use it in GitHub Desktop.
llvmlite bacsic usage
import sys
from llvmlite import binding as bd
from llvmlite import ir
# read bc / ir file
bc = sys.argv[1]
with open(bc, 'r', encoding="latin-1") as mybc:
program = mybc.read()
ct = bd.create_context()
moduleRef = bd.parse_bitcode(program, ct)
valueRef = moduleRef.get_function("main")
# creat CFG
graph = bd.get_function_cfg(valueRef, True)
# print(graph)
bd.view_dot_graph(graph, view= True)
'''
Add function to a module
'''
# define a type
int32 = ir.IntType(32)
# create an empty module
mod = ir.Module(name=__file__)
# create a function
ftype = ir.FunctionType(int32, (int32,))
func = ir.Function(mod, ftype, "test")
for f in mod.functions:
print(str(f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment