Kamek icon indicating copy to clipboard operation
Kamek copied to clipboard

Using kmBranch to add a branch from one address to another causes exceptions while linking

Open lilousky opened this issue 3 years ago • 4 comments

When trying to use the kmBranch function to redirect a certain address to another, one will be met with the following exceptions while linking:

   at Kamek.Word.op_GreaterThanOrEqual(Word a, Word b)
   at Kamek.KamekFile.Contains(Word addr)
   at Kamek.Commands.BranchCommand.Apply(KamekFile file)
   at Kamek.KamekFile.ApplyStaticCommands()
   at Kamek.KamekFile.LoadFromLinker(Linker linker)
   at Kamek.Program.Main(String[] args)

In this case, I am trying to redirect a function to a more optimized version found elsewhere, by using kmBranch(0x80005f34, 0x801D17F8);

Maybe I am thinking about it wrong, but is there any reason why the function to add a branch wouldn't be able to insert a branch?

lilousky avatar Sep 23 '22 19:09 lilousky

You should define the destination as an extern function. That way should work and will be auto-ported.

CLF78 avatar Sep 24 '22 08:09 CLF78

After defining the dest as an extern void function, adding the function to the symbol file, and replacing the address with the function name in the kmBranch results in the same error.

lilousky avatar Sep 24 '22 17:09 lilousky

The original intended use of kmBranch was to branch to new functions in your own code:

void example() {
    // ...
}

kmBranch(0x80005f34, example);

So the way in which you're using it was probably never tested. But it does make sense to support it, so I've opened PR #30 to fix it.

RoadrunnerWMC avatar Oct 06 '22 05:10 RoadrunnerWMC

The original intended use of kmBranch was to branch to new functions in your own code

Yeah I figured this was the case

But it does make sense to support it, so I've opened PR #30 to fix it. Thanks for this, it should be able to get the job done!

lilousky avatar Oct 06 '22 21:10 lilousky