Skip to content

Commit

Permalink
xzre_code: add find_function
Browse files Browse the repository at this point in the history
  • Loading branch information
smx-smx committed Aug 3, 2024
1 parent e4dca4a commit 6a0619f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions xzre_code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_library(xzre_code
is_endbr64_instruction.c
fake_lzma_alloc.c
fake_lzma_free.c
find_function.c
main_elf_parse.c
run_backdoor_commands.c
secret_data_append_from_address.c
Expand Down
42 changes: 42 additions & 0 deletions xzre_code/find_function.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (C) 2024 Stefano Moioli <[email protected]>
**/
#include "xzre.h"

BOOL find_function(
u8 *code_start,
void **func_start,
void **func_end,
u8 *search_base,
u8 *code_end,
FuncFindType find_mode
){
u8 *res = NULL;
/** should we locate the function prologue? */
if(func_start){
for(u8 *p = code_start;
search_base < p && !find_function_prologue(p, code_end, &res, find_mode);
--p);

if(!res || res == search_base && !find_function_prologue(search_base, code_end, NULL, find_mode)){
return FALSE;
}
*func_start = res;
}
/** should we locate the function epilogue? */
if(func_end){
u8 *search_from = code_start + 1;
u8 *search_to = code_end - 4;
BOOL found;
for(;search_from < search_to &&
(found=find_function_prologue(search_from, code_end, NULL, find_mode)) == FALSE;
++search_from
);
// FIXME: in theory the first check is redundant, as it's covered by the second one
if(found || search_to != search_from || find_function_prologue(search_from, code_end, NULL, find_mode)){
code_end = search_from;
}
*func_end = code_end;
}
return TRUE;
}

0 comments on commit 6a0619f

Please sign in to comment.