From dd8e0e5b913e7485b60f8fcddd49f0b7e7ca5b75 Mon Sep 17 00:00:00 2001 From: Giuliano Belinassi Date: Tue, 12 Nov 2024 17:04:51 -0300 Subject: [PATCH] Remove one WRITE insn for each function There is no need to insert two WRITEs for each function, as we can craft the final prologue before inserting it. Signed-off-by: Giuliano Belinassi --- common/common.c | 6 ++++-- lib/arch/x86_64/patch.c | 28 ++++++++++------------------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/common/common.c b/common/common.c index 35d81f58..6d5249fe 100644 --- a/common/common.c +++ b/common/common.c @@ -260,8 +260,6 @@ parse_metadata_from_mem(struct ulp_metadata *ulp, void *src, size_t size) struct ulp_dependency *dep, *prev_dep = NULL; struct ulp_reference *ref, *prev_ref = NULL; - DEBUG("reading live patch metadata from memory"); - /* read metadata header information */ ulp->objs = NULL; @@ -503,6 +501,10 @@ parse_metadata_from_mem(struct ulp_metadata *ulp, void *src, size_t size) prev_ref = ref; } + if (ulp->so_filename) { + DEBUG("Patch path: %s", ulp->so_filename); + } + return 0; } diff --git a/lib/arch/x86_64/patch.c b/lib/arch/x86_64/patch.c index b7a7b854..789938e1 100644 --- a/lib/arch/x86_64/patch.c +++ b/lib/arch/x86_64/patch.c @@ -61,22 +61,6 @@ static char ulp_prologue_endbr64[ULP_NOPS_LEN_ENDBR64] = { }; /* clang-format on */ -/** @brief Write new function address into data prologue of `old_fentry`. - * - * This function replaces the `` section in prologue `old_fentry` - * with a pointer to the new function given by `manager`, which will - * replace the to be patched function. - * - * @param old_fentry Pointer to prologue of to be replaced function - * @param manager Address of new function. - */ -void -ulp_patch_addr_absolute(void *old_fentry, void *manager) -{ - char *dst = (char *)old_fentry + ULP_DATA_OFFSET; - memwrite(dst, &manager, sizeof(void *)); -} - /** @brief Copy the ulp proglogue layout into the function to be patched's * prologue * @@ -166,10 +150,18 @@ ulp_patch_addr(void *old_faddr, void *new_faddr, int enable) /* Actually patch the prologue. */ if (enable) { - ulp_patch_prologue_layout(addr, prologue, ulp_nops_len); - ulp_patch_addr_absolute(addr, new_faddr); + char patched_prologue[ULP_NOPS_LEN_ENDBR64]; + memcpy(patched_prologue, prologue, ulp_nops_len); + + /* Insert the function redirection jump. */ + DEBUG("Patching function 0x%lx to 0x%lx", old_faddr, new_faddr); + memcpy(patched_prologue + ULP_DATA_OFFSET, &new_faddr, sizeof(void *)); + + /* Replace the prologue. */ + ulp_patch_prologue_layout(addr, patched_prologue, ulp_nops_len); } else { + DEBUG("Removing patch from 0x%lx", old_faddr); ulp_skip_prologue(old_faddr); }