Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions idalib-sys/src/func_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "pro.h"
#include "funcs.hpp"
#include "gdl.hpp"
#include "name.hpp"

#include <cstdint>
#include <exception>
Expand Down Expand Up @@ -42,3 +43,20 @@ rust::Slice<const int> idalib_qbasic_block_succs(qbasic_block_t const *blk) {
rust::Slice<const int> idalib_qbasic_block_preds(qbasic_block_t const *blk) {
return rust::Slice(std::begin(blk->pred), std::size(blk->pred));
}

bool idalib_func_set_name(const func_t *f, const char *name, int flags) {
if (f == nullptr || name == nullptr) {
return false;
}
return set_name(f->start_ea, name, flags);
}

void idalib_func_set_noret(func_t *f, bool noret) {
if (f != nullptr) {
if (noret) {
f->flags |= FUNC_NORET;
} else {
f->flags &= ~FUNC_NORET;
}
}
}
8 changes: 8 additions & 0 deletions idalib-sys/src/idalib_extras.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#pragma once

#include "idalib.hpp"
#include "name.hpp"

bool idalib_get_library_version(int *major, int *minor, int *build) {
return get_library_version(*major, *minor, *build);
}

bool idalib_set_name(uval_t ea, const char *name, int flags) {
if (name == nullptr) {
return false;
}
return set_name(ea, name, flags);
}
43 changes: 40 additions & 3 deletions idalib-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ include_cpp! {
#include "pro.h"
#include "segment.hpp"
#include "strlist.hpp"
#include "typeinf.hpp"
#include "ua.hpp"
#include "xref.hpp"

Expand Down Expand Up @@ -123,6 +124,7 @@ include_cpp! {
generate!("func_t")
generate!("lock_func")
generate!("get_func")
generate!("get_fchunk")
generate!("get_func_num")
generate!("get_func_qty")
generate!("getn_func")
Expand Down Expand Up @@ -384,6 +386,12 @@ include_cpp! {
generate!("is_in_nlist")
generate!("is_public_name")
generate!("is_weak_name")

// types
generate!("parse_decls")
generate!("get_idati")
generate!("get_ordinal_limit")
generate!("get_numbered_type_name")
}

pub mod hexrays {
Expand Down Expand Up @@ -737,6 +745,7 @@ mod ffix {
include!("segm_extras.h");
include!("search_extras.h");
include!("strings_extras.h");
include!("types_extras.h");

type c_short = autocxx::c_short;
type c_int = autocxx::c_int;
Expand Down Expand Up @@ -783,6 +792,8 @@ mod ffix {

unsafe fn idalib_func_flags(f: *const func_t) -> u64;
unsafe fn idalib_func_name(f: *const func_t) -> Result<String>;
unsafe fn idalib_func_set_name(f: *const func_t, name: *const c_char, flags: c_int) -> bool;
unsafe fn idalib_func_set_noret(f: *mut func_t, noret: bool);

unsafe fn idalib_func_flow_chart(
f: *mut func_t,
Expand Down Expand Up @@ -999,6 +1010,7 @@ mod ffix {
unsafe fn idalib_segm_perm(s: *const segment_t) -> u8;
unsafe fn idalib_segm_bitness(s: *const segment_t) -> u8;
unsafe fn idalib_segm_type(s: *const segment_t) -> u8;
unsafe fn idalib_segm_set_perm(s: *mut segment_t, perm: u8);

unsafe fn idalib_get_cmt(ea: c_ulonglong, rptble: bool) -> String;

Expand Down Expand Up @@ -1038,6 +1050,18 @@ mod ffix {
minor: *mut c_int,
build: *mut c_int,
) -> bool;
unsafe fn idalib_set_name(ea: c_ulonglong, name: *const c_char, flags: c_int) -> bool;

unsafe fn idalib_parse_header_file(filename: *const c_char) -> c_int;
unsafe fn idalib_tinfo_get_name_by_ordinal(ordinal: u32) -> Result<String>;
unsafe fn idalib_is_valid_type_ordinal(ordinal: u32) -> bool;
unsafe fn idalib_get_type_ordinal_limit() -> u32;

// Type assignment functions
unsafe fn idalib_apply_type_by_ordinal(ea: c_ulonglong, ordinal: u32, flags: u32) -> bool;
unsafe fn idalib_apply_type_by_decl(ea: c_ulonglong, decl: *const c_char) -> bool;
unsafe fn idalib_get_type_ordinal_at_address(ea: c_ulonglong) -> u32;
unsafe fn idalib_get_type_string_at_address(ea: c_ulonglong) -> Result<String>;
}
}

Expand Down Expand Up @@ -1112,11 +1136,11 @@ pub mod insn {

pub mod func {
pub use super::ffi::{
calc_thunk_func_target, fc_block_type_t, func_t, gdl_graph_t, get_func, get_func_num,
calc_thunk_func_target, fc_block_type_t, func_t, gdl_graph_t, get_func, get_fchunk, get_func_num,
get_func_qty, getn_func, lock_func, qbasic_block_t, qflow_chart_t,
};
pub use super::ffix::{
idalib_func_flags, idalib_func_flow_chart, idalib_func_name, idalib_qbasic_block_preds,
idalib_func_flags, idalib_func_flow_chart, idalib_func_name, idalib_func_set_name, idalib_func_set_noret, idalib_qbasic_block_preds,
idalib_qbasic_block_succs, idalib_qflow_graph_getn_block,
};

Expand Down Expand Up @@ -1158,7 +1182,7 @@ pub mod segment {

pub use super::ffix::{
idalib_segm_align, idalib_segm_bitness, idalib_segm_bytes, idalib_segm_name,
idalib_segm_perm, idalib_segm_type,
idalib_segm_perm, idalib_segm_set_perm, idalib_segm_type,
};
}

Expand Down Expand Up @@ -1233,6 +1257,7 @@ pub mod name {
get_nlist_ea, get_nlist_idx, get_nlist_name, get_nlist_size, is_in_nlist, is_public_name,
is_weak_name,
};
pub use super::ffix::idalib_set_name;
}

pub mod ida {
Expand Down Expand Up @@ -1416,3 +1441,15 @@ pub mod ida {
}
}
}

pub mod types {
pub use super::ffi::{
get_idati, get_ordinal_limit, get_numbered_type_name,
};
pub use super::ffix::{
idalib_get_type_ordinal_limit, idalib_parse_header_file,
idalib_tinfo_get_name_by_ordinal, idalib_is_valid_type_ordinal,
idalib_apply_type_by_ordinal, idalib_apply_type_by_decl,
idalib_get_type_ordinal_at_address, idalib_get_type_string_at_address,
};
}
6 changes: 6 additions & 0 deletions idalib-sys/src/segm_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ std::uint8_t idalib_segm_perm(const segment_t *s) {
std::uint8_t idalib_segm_type(const segment_t *s) {
return s->type;
}

void idalib_segm_set_perm(segment_t *s, std::uint8_t perm) {
if (s != nullptr) {
s->perm = perm;
}
}
117 changes: 117 additions & 0 deletions idalib-sys/src/types_extras.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#pragma once

#include "cxx.h"
#include "pro.h"
#include "typeinf.hpp"

#include <cstdint>
#include <memory>

// Parse types from a header file
inline int idalib_parse_header_file(const char *filename) {
if (filename == nullptr) {
return -1;
}

til_t *til = get_idati();
if (til == nullptr) {
return -1;
}

// HTI_FIL = input is filename, HTI_MAC = define macros from base tils,
// HTI_NWR = no warnings
int flags = HTI_FIL | HTI_MAC | HTI_NWR;
return parse_decls(til, filename, nullptr, flags);
}

// Get type name from tinfo_t (using void* to avoid direct tinfo_t exposure)
inline rust::String idalib_tinfo_get_name_by_ordinal(std::uint32_t ordinal) {
tinfo_t tif;

if (!tif.get_numbered_type(get_idati(), ordinal)) {
return rust::String();
}

const char *name = tif.dstr();
if (name == nullptr) {
return rust::String();
}

return rust::String(name);
}

// Check if a type ordinal is valid
inline bool idalib_is_valid_type_ordinal(std::uint32_t ordinal) {
tinfo_t tif;
return tif.get_numbered_type(get_idati(), ordinal);
}

// Get the maximum ordinal for type iteration
inline std::uint32_t idalib_get_type_ordinal_limit() {
return get_ordinal_limit(get_idati());
}

// Apply type to an address using ordinal
inline bool idalib_apply_type_by_ordinal(std::uint64_t ea,
std::uint32_t ordinal,
std::uint32_t flags) {
tinfo_t tif;

if (!tif.get_numbered_type(get_idati(), ordinal)) {
return false;
}

return apply_tinfo(ea, tif, flags);
}

// Apply type to an address using C declaration string
inline bool idalib_apply_type_by_decl(std::uint64_t ea, const char *decl) {
if (decl == nullptr) {
return false;
}

til_t *til = get_idati();
if (til == nullptr) {
return false;
}

return apply_cdecl(til, ea, decl);
}

// Get type information at an address (returns ordinal, 0 if no type)
inline std::uint32_t idalib_get_type_ordinal_at_address(std::uint64_t ea) {
tinfo_t tif;

if (!guess_tinfo(&tif, ea)) {
return 0;
}

// Try to find the ordinal for this type
std::uint32_t limit = get_ordinal_limit(get_idati());
for (std::uint32_t i = 1; i < limit; i++) {
tinfo_t check_tif;
if (check_tif.get_numbered_type(get_idati(), i)) {
if (tif.equals_to(check_tif)) {
return i;
}
}
}

return 0; // Type not found in numbered types
}

// Get type declaration string at an address
inline rust::String idalib_get_type_string_at_address(std::uint64_t ea) {
tinfo_t tif;

if (!guess_tinfo(&tif, ea)) {
return rust::String();
}

const char *type_str = tif.dstr();
if (type_str == nullptr) {
return rust::String();
}

return rust::String(type_str);
}
Loading