From 69e6732bc65ca2111b7aa64429e9b13fbbdc9567 Mon Sep 17 00:00:00 2001 From: 2over12 Date: Tue, 5 Dec 2023 13:54:44 -0500 Subject: [PATCH] fix typedef --- include/anvill/Type.h | 5 +++++ lib/Protobuf.cpp | 7 ++++++- lib/Type.cpp | 13 ++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/anvill/Type.h b/include/anvill/Type.h index 7e17db8f..ac00c996 100644 --- a/include/anvill/Type.h +++ b/include/anvill/Type.h @@ -8,6 +8,7 @@ #pragma once +#include #include #include @@ -41,6 +42,10 @@ class Arch; } // namespace remill namespace anvill { +llvm::StructType *getOrCreateNamedStruct(llvm::LLVMContext &context, + llvm::StringRef Name); + + struct TypeSpecificationError final { enum class ErrorCode { InvalidSpecFormat, diff --git a/lib/Protobuf.cpp b/lib/Protobuf.cpp index 75bb70ef..9e267f86 100644 --- a/lib/Protobuf.cpp +++ b/lib/Protobuf.cpp @@ -469,6 +469,9 @@ ProtobufTranslator::DecodeType(const ::specification::TypeSpec &obj) const { if (this->type_names.count(obj.alias())) { TypeSpec res = TypeName(type_names.at(obj.alias())); return res; + } else if (this->type_map.count(obj.alias())) { + TypeSpec tspec = this->type_map.at(obj.alias()); + return tspec; } else { LOG(ERROR) << "Unknown alias id " << obj.alias(); return {BaseType::Void}; @@ -811,6 +814,7 @@ anvill::Result ProtobufTranslator::DecodeType( const std::unordered_map &named_types) { if (obj.has_alias()) { auto alias = obj.alias(); + if (named_types.contains(alias)) { TypeSpec tname = TypeName(named_types.at(alias)); return tname; @@ -928,7 +932,8 @@ Result ProtobufTranslator::DecodeTypeMap( std::string name = names.at(k); - llvm::StructType::create(this->context, sty->elements(), name); + auto res = getOrCreateNamedStruct(this->context, name); + res->setBody(sty->elements()); } type_names[k] = names.at(k); } else { diff --git a/lib/Type.cpp b/lib/Type.cpp index f87e05ef..3b79e8b9 100644 --- a/lib/Type.cpp +++ b/lib/Type.cpp @@ -766,7 +766,8 @@ TypeTranslator::DecodeFromSpec(TypeSpec spec) const { if (std::holds_alternative(spec)) { auto nm = std::get(spec); - auto sty = llvm::StructType::getTypeByName(this->impl->context, nm.name); + auto sty = getOrCreateNamedStruct(this->impl->context, nm.name); + CHECK(sty); return sty; } @@ -774,6 +775,16 @@ TypeTranslator::DecodeFromSpec(TypeSpec spec) const { "Unhandled type specification variant"}; } +llvm::StructType *getOrCreateNamedStruct(llvm::LLVMContext &context, + llvm::StringRef Name) { + auto res = llvm::StructType::getTypeByName(context, Name); + if (res) { + return res; + } + + return llvm::StructType::create(context, Name); +} + namespace { template