Skip to content

Commit

Permalink
Register functions from reloc targets
Browse files Browse the repository at this point in the history
Rizin shows calls to reloc targets as their function names in
disassembly. We do the same in the decompiler.
Addresses #312
  • Loading branch information
thestr4ng3r committed Jan 20, 2023
1 parent 9f8dd11 commit d371317
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/RizinScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ FunctionSymbol *RizinScope::registerFunction(RzAnalysisFunction *fcn) const
return dynamic_cast<FunctionSymbol *>(sym);
}

FunctionSymbol *RizinScope::registerRelocTarget(RzBinReloc *reloc) const
{
RzCoreLock core(arch->getCore());
if(!reloc->import || !reloc->import->name)
return nullptr;
return cache->addFunction(Address(arch->getDefaultCodeSpace(), reloc->target_vaddr), reloc->import->name);
}

Symbol *RizinScope::registerFlag(RzFlagItem *flag) const
{
RzCoreLock core(arch->getCore());
Expand Down Expand Up @@ -516,6 +524,14 @@ Symbol *RizinScope::queryRizinAbsolute(ut64 addr, bool contain) const
if(glob)
return registerGlobalVar(glob);

RzBinReloc *reloc = rz_core_get_reloc_to(core, addr);
if(reloc && reloc->import)
{
auto rsym = registerRelocTarget(reloc);
if(rsym)
return rsym;
}

// TODO: register more things

// TODO: correctly handle contain for flags
Expand Down
2 changes: 2 additions & 0 deletions src/RizinScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RizinArchitecture;
typedef struct rz_analysis_function_t RzAnalysisFunction;
typedef struct rz_flag_item_t RzFlagItem;
typedef struct rz_analysis_var_global_t RzAnalysisVarGlobal;
typedef struct rz_bin_reloc_t RzBinReloc;

class RizinScope : public Scope
{
Expand All @@ -30,6 +31,7 @@ class RizinScope : public Scope
uint8 makeId() const { return (*next_id)++; }

FunctionSymbol *registerFunction(RzAnalysisFunction *fcn) const;
FunctionSymbol *registerRelocTarget(RzBinReloc *reloc) const;
Symbol *registerFlag(RzFlagItem *flag) const;
Symbol *registerGlobalVar(RzAnalysisVarGlobal *glob) const;
Symbol *queryRizinAbsolute(ut64 addr, bool contain) const;
Expand Down
19 changes: 19 additions & 0 deletions test/db/extras/ghidra
Original file line number Diff line number Diff line change
Expand Up @@ -3262,3 +3262,22 @@ undefined8 entry0(int64_t arg1, int64_t arg2)
}
EOF
RUN

NAME=reloc target functions
FILE=rizin-testbins/elf/linux-example-x86-32.ko
CMDS=<<EOF
s sym.ko_example_init
af
pdg
EOF
EXPECT=<<EOF

undefined4 sym.ko_example_init(void)
{
// [04] -r-x section size 22 named .init.text
__fentry__();
printk("Hello, Rizin!\n");
return 0;
}
EOF
RUN

0 comments on commit d371317

Please sign in to comment.