From 1ea7b6420a63e79ad345b626030be50ae8206ba0 Mon Sep 17 00:00:00 2001 From: Alessandro Gario <5714290+alessandrogario@users.noreply.github.com> Date: Thu, 11 Feb 2021 23:46:38 +0100 Subject: [PATCH] anvill: Add StructType support to CreateConstFromMemory (#97) --- lib/Lift.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/Lift.cpp b/lib/Lift.cpp index 0c391b07b..349a5a29c 100644 --- a/lib/Lift.cpp +++ b/lib/Lift.cpp @@ -548,6 +548,32 @@ CreateConstFromMemory(const uint64_t addr, llvm::Type *type, case llvm::Type::PointerTyID: { } break; + case llvm::Type::StructTyID: { + // Take apart the structure type, recursing into each element + // so that we can create a constant structure + auto struct_type = llvm::dyn_cast(type); + + auto num_elms = struct_type->getNumElements(); + auto elm_offset = 0; + + std::vector const_list; + + for (std::uint64_t i = 0U; i < num_elms; ++i) { + auto elm_type = struct_type->getElementType(i); + auto elm_size = dl.getTypeSizeInBits(elm_type); + + auto const_elm = + CreateConstFromMemory(addr + elm_offset, elm_type, arch, + program, module); + + const_list.push_back(const_elm); + elm_offset += elm_size / 8; + } + + result = llvm::ConstantStruct::get(struct_type, + llvm::ArrayRef(const_list)); + } break; + case llvm::Type::ArrayTyID: { const auto elm_type = type->getArrayElementType(); const auto elm_size = dl.getTypeSizeInBits(elm_type); @@ -570,12 +596,13 @@ CreateConstFromMemory(const uint64_t addr, llvm::Type *type, } break; default: - LOG(FATAL) << "Unknown LLVM Type: " << remill::LLVMThingToString(type); + LOG(FATAL) << "Unhandled LLVM Type: " << remill::LLVMThingToString(type); break; } return result; } + } // namespace bool LiftCodeIntoModule(const remill::Arch *arch, const Program &program,