TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Machine code monkey patching

30 点作者 sheffield大约 14 年前

2 条评论

scott_s大约 14 年前
There's no need to go through these steps if you're compiling the code yourself. If you want to override a library function like malloc, just define a function called malloc. Then make sure that the linker sees your function before the library function. For example, here I override pthread_create: <a href="https://github.com/scotts/streamflow/blob/master/override.c#L66" rel="nofollow">https://github.com/scotts/streamflow/blob/master/override.c#...</a><p>Note that I also figure out, at runtime, how to call the real version of pthread_create using the dlsym interface. I make sure that my pthread_create is the first one in the call chain by putting that object file before -lpthread when I link.<p>The LD_PRELOAD method is for when you <i>are not</i> compiling the code yourself. That is, the function you want to hijack is in a binary, and you can't re-compile that binary. Then you compile a shared library with your function, and use LD_PRELOAD to force the runtime linker to load your library first.
iam大约 14 年前
Monkey patching is pretty common for when you want to override or get on-change notifications for interfaces that don't support it.<p>For example, have header files for functions? Want to know when they're called? Monkey patch it in!<p>Also, from what I've heard monkey patching a function is calling adding a detour, and monkey patching a virtual function is called hooking. "Monkey patching" usually refers to injecting code into dynamic languages, not into compiled languages.
评论 #2550068 未加载