@@ -617,23 +617,44 @@ bool LiftCodeIntoModule(const remill::Arch *arch, const Program &program,
617617
618618 // Lift global variables.
619619 program.ForEachVariable ([&](const anvill::GlobalVarDecl *decl) {
620- const auto addr = decl->address ;
621- const auto name = anvill::CreateVariableName (addr);
622- const auto gvar = decl->DeclareInModule (name, module );
620+ const auto addr{ decl->address } ;
621+ const auto name{ anvill::CreateVariableName (addr)} ;
622+ const auto gvar{ decl->DeclareInModule (name, module )} ;
623623
624+ // Check if we have mapped bytes
625+ if (!program.FindByte (addr)) {
626+ return true ;
627+ }
624628 // Set initializer
625- auto init = CreateConstFromMemory (addr, decl->type , arch, program, module );
629+ auto init{ CreateConstFromMemory (addr, decl->type , arch, program, module )} ;
626630 gvar->setInitializer (init);
627-
628631 return true ;
629632 });
630633
631634 // Lift functions.
632635 program.ForEachFunction ([&](const FunctionDecl *decl) {
633- const auto entry = lifter.LiftFunction (*decl);
634- DefineNativeToLiftedWrapper (arch, *decl, entry);
636+ // Initialize function entry. This will lift machine code
637+ // into `entry.lifted` if instruction bytes for are
638+ // available and declare `entry.lifted_to_native` and
639+ // `entry.lifted_to_native` wrapper functions that
640+ // are needed for further lifting to native functions.
641+ const auto entry{lifter.LiftFunction (*decl)};
642+
643+ // We have `entry.lifted` available. `entry.lifted`
644+ // will be inlined into `entry.native_to_lifted`.
645+ if (!entry.lifted ->isDeclaration ()) {
646+ DefineNativeToLiftedWrapper (arch, *decl, entry);
647+ }
648+ // Wrap native functions in a function that lifted
649+ // functions can call. This will result in the
650+ // lifted functions calling the native ones.
635651 DefineLiftedToNativeWrapper (*decl, entry);
652+
653+ // Optimize and inline. After this we should end up
654+ // with only native functions.
636655 OptimizeFunction (entry.native_to_lifted );
656+
657+ // The ritual is done.
637658 return true ;
638659 });
639660
0 commit comments