Skip to content

Commit 1ea7b64

Browse files
anvill: Add StructType support to CreateConstFromMemory (#97)
1 parent c88f20a commit 1ea7b64

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

lib/Lift.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,32 @@ CreateConstFromMemory(const uint64_t addr, llvm::Type *type,
548548
case llvm::Type::PointerTyID: {
549549
} break;
550550

551+
case llvm::Type::StructTyID: {
552+
// Take apart the structure type, recursing into each element
553+
// so that we can create a constant structure
554+
auto struct_type = llvm::dyn_cast<llvm::StructType>(type);
555+
556+
auto num_elms = struct_type->getNumElements();
557+
auto elm_offset = 0;
558+
559+
std::vector<llvm::Constant *> const_list;
560+
561+
for (std::uint64_t i = 0U; i < num_elms; ++i) {
562+
auto elm_type = struct_type->getElementType(i);
563+
auto elm_size = dl.getTypeSizeInBits(elm_type);
564+
565+
auto const_elm =
566+
CreateConstFromMemory(addr + elm_offset, elm_type, arch,
567+
program, module);
568+
569+
const_list.push_back(const_elm);
570+
elm_offset += elm_size / 8;
571+
}
572+
573+
result = llvm::ConstantStruct::get(struct_type,
574+
llvm::ArrayRef(const_list));
575+
} break;
576+
551577
case llvm::Type::ArrayTyID: {
552578
const auto elm_type = type->getArrayElementType();
553579
const auto elm_size = dl.getTypeSizeInBits(elm_type);
@@ -570,12 +596,13 @@ CreateConstFromMemory(const uint64_t addr, llvm::Type *type,
570596
} break;
571597

572598
default:
573-
LOG(FATAL) << "Unknown LLVM Type: " << remill::LLVMThingToString(type);
599+
LOG(FATAL) << "Unhandled LLVM Type: " << remill::LLVMThingToString(type);
574600
break;
575601
}
576602

577603
return result;
578604
}
605+
579606
} // namespace
580607

581608
bool LiftCodeIntoModule(const remill::Arch *arch, const Program &program,

0 commit comments

Comments
 (0)