Skip to content

Commit b5be066

Browse files
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 <[email protected]>
1 parent 7002e0a commit b5be066

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

common/common.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@ parse_metadata_from_mem(struct ulp_metadata *ulp, void *src, size_t size)
260260
struct ulp_dependency *dep, *prev_dep = NULL;
261261
struct ulp_reference *ref, *prev_ref = NULL;
262262

263-
DEBUG("reading live patch metadata from memory");
264-
265263
/* read metadata header information */
266264
ulp->objs = NULL;
267265

@@ -503,6 +501,10 @@ parse_metadata_from_mem(struct ulp_metadata *ulp, void *src, size_t size)
503501
prev_ref = ref;
504502
}
505503

504+
if (ulp->so_filename) {
505+
DEBUG("Patch path: %s", ulp->so_filename);
506+
}
507+
506508
return 0;
507509
}
508510

lib/arch/x86_64/patch.c

+10-18
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,6 @@ static char ulp_prologue_endbr64[ULP_NOPS_LEN_ENDBR64] = {
6161
};
6262
/* clang-format on */
6363

64-
/** @brief Write new function address into data prologue of `old_fentry`.
65-
*
66-
* This function replaces the `<data>` section in prologue `old_fentry`
67-
* with a pointer to the new function given by `manager`, which will
68-
* replace the to be patched function.
69-
*
70-
* @param old_fentry Pointer to prologue of to be replaced function
71-
* @param manager Address of new function.
72-
*/
73-
void
74-
ulp_patch_addr_absolute(void *old_fentry, void *manager)
75-
{
76-
char *dst = (char *)old_fentry + ULP_DATA_OFFSET;
77-
memwrite(dst, &manager, sizeof(void *));
78-
}
79-
8064
/** @brief Copy the ulp proglogue layout into the function to be patched's
8165
* prologue
8266
*
@@ -166,10 +150,18 @@ ulp_patch_addr(void *old_faddr, void *new_faddr, int enable)
166150

167151
/* Actually patch the prologue. */
168152
if (enable) {
169-
ulp_patch_prologue_layout(addr, prologue, ulp_nops_len);
170-
ulp_patch_addr_absolute(addr, new_faddr);
153+
char patched_prologue[ULP_NOPS_LEN_ENDBR64];
154+
memcpy(patched_prologue, prologue, ulp_nops_len);
155+
156+
/* Insert the function redirection jump. */
157+
DEBUG("Patching function 0x%lx to 0x%lx", old_faddr, new_faddr);
158+
memcpy(patched_prologue + ULP_DATA_OFFSET, &new_faddr, sizeof(void *));
159+
160+
/* Replace the prologue. */
161+
ulp_patch_prologue_layout(addr, patched_prologue, ulp_nops_len);
171162
}
172163
else {
164+
DEBUG("Removing patch from 0x%lx", old_faddr);
173165
ulp_skip_prologue(old_faddr);
174166
}
175167

0 commit comments

Comments
 (0)