Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context management and safe hooks #26

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

RoadrunnerWMC
Copy link
Collaborator

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.

None yet

1 participant