Skip to content

Conversation

@RoadrunnerWMC
Copy link
Collaborator

@RoadrunnerWMC RoadrunnerWMC commented May 23, 2022

It's common to want to add code to a function without damaging the code you're hooking into. To support that, this PR provides macros for saving and restoring context (registers) to the stack. It also adds new shortcut syntax for the common sub-case of hooks that want to add functionality to a function without overwriting any of its original instructions, which works by automatically building a trampoline with the context save/restore macros and a copy of the overwritten instruction (provided by the user, since Kamek doesn't know at link-time what the game's memory layout will look like).

For example usage, see the commits in the kmcontext_and_safe_hooks branch of my RoadrunnerWMC_Levels repo.

To-do list before this gets merged

  • .cpp files: context management macros (for inline asm)
  • .cpp files: safe hooks (for C++)
  • .cpp files: safe hooks (for inline asm)
  • .S files: context management macros
  • .S files: safe hooks

context.h provides kmSaveContext and kmRestoreContext macros to save and restore context within asm functions. kmSafeBranchDefCpp makes use of that to implement "safe hooks" that avoid damaging the code being hooked into, which I've found to be a common need.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant