Skip to content

Commit

Permalink
[IDA] Add IDA 9 support
Browse files Browse the repository at this point in the history
  • Loading branch information
patacca committed Feb 13, 2025
1 parent 695f99e commit b17f0be
Show file tree
Hide file tree
Showing 14 changed files with 1,037 additions and 376 deletions.
47 changes: 6 additions & 41 deletions include/quokka/Comment.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@
#include <vector>

#include "Compatibility.h"

#include <bytes.hpp>
#include <enum.hpp>
#include <funcs.hpp>
#include <ida.hpp>
#include <struct.hpp>

#include "absl/container/flat_hash_map.h"

#include "Localization.h" //Kept for Location
#include "Util.h"
#include "Windows.h"

#if IDA_SDK_VERSION < 900
#include "api_v8/Comment_v8.h"
#else
#include "api_v9/Comment_v9.h"
#endif

namespace quokka {

Expand Down Expand Up @@ -222,44 +226,5 @@ bool GetLineComment(ea_t addr, int index, std::string* output);
void GetFunctionComments(Comments& comments, const func_t* func,
std::shared_ptr<Function> function_p);

/**
* Retrieve the comments associated to the member of an enumeration.
*
* @param member_p Pointer to the `quokka::StructureMember`
* @param member Pointer to the `const_t` (IDA)
*/
void GetEnumMemberComment(std::shared_ptr<StructureMember> member_p,
const_t member);

/**
* Retrieve the comments associated to the enum.
*
* @warning Does not retrieve the comments associated to the enum member
*
* @param structure A quokka::Structure pointer
* @param ida_enum The ida enum
*/
void GetEnumComment(std::shared_ptr<Structure> structure, enum_t ida_enum);

/**
* Retrieve the comments associated to the members of a structure
*
* @param member_p Pointer to the `quokka::StructureMember`
* @param member Pointer to the `tid_t` (IDA)
*/
void GetStructureMemberComment(std::shared_ptr<StructureMember> member_p,
tid_t member);

/**
* Retrieve the comments associated to the structure.
*
* @warning Does not retrieve the comments associated to the struct member
*
* @param structure A quokka::Structure pointer
* @param ida_struct The ida struct
*/
void GetStructureComment(std::shared_ptr<Structure> structure,
tid_t ida_struct);

} // namespace quokka
#endif // QUOKKA_COMMENT_H
118 changes: 41 additions & 77 deletions include/quokka/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
#ifndef QUOKKA_DATA_H
#define QUOKKA_DATA_H

#include <cassert>
#include <cstdint>
#include <memory>
#include <string>

#include "Compatibility.h"

#include <bytes.hpp>
#include <enum.hpp>
#include <kernwin.hpp>
#include <name.hpp>
#include <struct.hpp>
#include <typeinf.hpp>

#include "absl/hash/hash.h"
#include "absl/strings/str_format.h"
Expand Down Expand Up @@ -164,6 +165,14 @@ class Data : public ProtoHelper {
*/
DataType GetDataType(flags_t flags);

/**
* Return the type associated to the data.
*
* @param tinf IDA Type info object
* @return A data type
*/
DataType GetDataType(const tinfo_t& tinf);

class Structure;

/**
Expand All @@ -177,36 +186,42 @@ struct StructureMember : public ProtoHelper {
std::string name; ///< Name of the field
DataType type; ///< Type of the value
asize_t size = 0; ///< Size of the field
int64 value = 0; ///< Value of the field
uint64 value = 0; ///< Value of the field

std::weak_ptr<Structure> parent; ///< Back pointer towards the parent

/**
* Constructor for struct member
*
* @param ida_member A ida structure of the struct member
*/
explicit StructureMember(member_t* ida_member) {
offset = ida_member->soff;
name = ConvertIdaString(get_member_name(ida_member->id));
type = GetDataType(ida_member->flag);
}
StructureMember(const StructureMember& obj)
: offset(obj.offset),
name(obj.name),
type(obj.type),
size(obj.size),
value(obj.value),
parent(obj.parent) {}

StructureMember(StructureMember&& obj)
: offset(obj.offset),
name(std::move(obj.name)),
type(obj.type),
size(obj.size),
value(obj.value),
parent(std::move(obj.parent)) {}

/**
* Constructor for enum member
* Constructor for struct member
*
* @param cid Enum id
* @param member_value Member value
* @param _offset Field offset (IDA internal)
* @param _name Name of the field
* @param _type Type of the value
* @param _size Size of the field
* @param _value Value of the field
*/
explicit StructureMember(const_t cid, uval_t member_value) {
qstring member_name;
get_enum_member_name(&member_name, cid);
offset = ea_t(cid);
name = ConvertIdaString(member_name);
// TODO(dm) IDA reports member value as a *positive* integer
value = int64_t(member_value);
type = TYPE_B;
}
explicit StructureMember(ea_t _offset, const qstring& _name, DataType _type,
asize_t _size = 0, uint64_t _value = 0)
: offset(_offset),
name(ConvertIdaString(_name)),
type(_type),
size(_size),
value(_value) {}
};

/**
Expand Down Expand Up @@ -300,30 +315,6 @@ class Structures {
[[nodiscard]] const_iterator end() const { return structures_.end(); }
};

/**
* Export the struct members
*
* Iterate through the ida-struct member and export each of them.
*
* @param structure A pointer to the `Structure` object
* @param ida_struc A pointer to the IDA struct
*/
void ExportStructMembers(std::shared_ptr<Structure>& structure,
struc_t* ida_struc);

/**
* Export an IDA-struct or an union
*
* Completely export a structure, including the references and comments.
*
* @see ExportStructureReference
* @see GetStructureComment
*
* @param ida_struct A pointer to the IDA struct
* @return Created structure
*/
std::shared_ptr<Structure> ExportStructure(struc_t* ida_struct);

/**
* Export all the structures defined in the program
*
Expand All @@ -333,33 +324,6 @@ std::shared_ptr<Structure> ExportStructure(struc_t* ida_struct);
*/
void ExportStructures(Structures& structure_list);

/**
* Export the enum members of enumeration.
*
* @note Use a visitor pattern because that's the IDA way of iterating
* through the enum members ..
*
* @param enumeration A pointer to the quokka::Structure
* @param ida_enum Ida-enum
* @param enum_idx Index of the enumeration (@see ExportEnum)
*/
void ExportEnumMembers(std::shared_ptr<Structure>& enumeration, enum_t ida_enum,
size_t enum_idx);

/**
* Export an enum
*
* Ghost enum don't have an index, so we use the position in the `Structures`
* for all of enumeration as an index. It will be used for attaching comments
* and references.
*
* @param ida_enum Ida-enum
* @param enum_idx Index of the enumeration (position in the Structures
* container)
* @return Created structure
*/
std::shared_ptr<Structure> ExportEnum(enum_t ida_enum, size_t enum_idx);

/**
* Export all the enums defined in the program
*
Expand Down
78 changes: 78 additions & 0 deletions include/quokka/api_v8/Comment_v8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2022-2023 Quarkslab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @file Comment_v9.h
* Management of comments specific for the old IDA API (v8).
*/

#ifndef QUOKKA_COMMENT_V8_H
#define QUOKKA_COMMENT_V8_H

#include <memory>

#include "../Compatibility.h"

#include <enum.hpp>

#include "../Data.h"

#if IDA_SDK_VERSION >= 900
#error "Comment_v8.h can only be used with IDA SDK < 9.0"
#endif

namespace quokka {

/**
* Retrieve the comments associated to the members of a structure
*
* @param member_p Pointer to the `quokka::StructureMember`
* @param member Pointer to the `tid_t` (IDA)
*/
void GetStructureMemberComment_v8(std::shared_ptr<StructureMember> member_p,
tid_t member);

/**
* Retrieve the comments associated to the structure.
*
* @warning Does not retrieve the comments associated to the struct member
*
* @param structure A quokka::Structure pointer
* @param ida_struct The ida struct
*/
void GetStructureComment_v8(std::shared_ptr<Structure> structure,
tid_t ida_struct);

/**
* Retrieve the comments associated to the member of an enumeration.
*
* @param member_p Pointer to the `quokka::StructureMember`
* @param member Pointer to the `const_t` (IDA)
*/
void GetEnumMemberComment_v8(std::shared_ptr<StructureMember> member_p,
const_t member);

/**
* Retrieve the comments associated to the enum.
*
* @warning Does not retrieve the comments associated to the enum member
*
* @param structure A quokka::Structure pointer
* @param ida_enum The ida enum
*/
void GetEnumComment_v8(std::shared_ptr<Structure> structure, enum_t ida_enum);

} // namespace quokka

#endif // QUOKKA_COMMENT_V8_H
79 changes: 79 additions & 0 deletions include/quokka/api_v9/Comment_v9.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2022-2023 Quarkslab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @file Comment_v9.h
* Management of comments specific for the new IDA API (v9).
*/

#ifndef QUOKKA_COMMENT_V9_H
#define QUOKKA_COMMENT_V9_H

#include <memory>

#include "../Compatibility.h"

#include <typeinf.hpp>

#include "../Data.h"

#if IDA_SDK_VERSION < 900
#error "Comment_v9.h can only be used with IDA SDK >= 9.0"
#endif

namespace quokka {

/**
* Retrieve the comments associated to the members of a structure
*
* @param member_p Pointer to the `quokka::StructureMember`
* @param udm IDA user-defined member
*/
void GetStructureMemberComment_v9(std::shared_ptr<StructureMember> member_p,
const udm_t& udm);

/**
* Retrieve the comments associated to the structure.
*
* @warning Does not retrieve the comments associated to the struct member
*
* @param structure A quokka::Structure pointer
* @param struct_tif The IDA struct type info
*/
void GetStructureComment_v9(std::shared_ptr<Structure> structure,
const tinfo_t& struct_tif);

/**
* Retrieve the comments associated to the member of an enumeration.
*
* @param member_p Pointer to the `quokka::StructureMember`
* @param edm Enum member IDA object
*/
void GetEnumMemberComment_v9(std::shared_ptr<StructureMember> member_p,
const edm_t& edm);

/**
* Retrieve the comments associated to the enum.
*
* @warning Does not retrieve the comments associated to the enum member
*
* @param structure A quokka::Structure pointer
* @param enum_tif Ida enum type info
*/
void GetEnumComment_v9(std::shared_ptr<Structure> structure,
const tinfo_t& enum_tif);

} // namespace quokka

#endif // QUOKKA_COMMENT_V9_H
Loading

0 comments on commit b17f0be

Please sign in to comment.