e.g. It may become known that only one of the possible derived types will be handled, such that virtual dispatch can be replaced with a specific call
在检测到哪些硬件可用之后,可以对匹配代码进行硬编码
Unnecessary code can be replaced with no-op instructions or a jump over it, or have the next bit of code shifted directly into place (easier if using position-independent opcodes)
为了方便自己的调试而编写的代码可能会在战略位置注入调试器所期望的陷阱/信号/中断指令。
一些基于用户输入的谓词表达式可以由库编译成本机代码
Inlining some simple operations that aren't visible until runtime (e.g. from dynamically loaded library)...
有条件地添加自检测/分析步骤
裂缝可以实现为修改加载它们的代码的库(不是“自我”修改,而是需要相同的技术和权限)。
...
Some OSs' security models mean self-modifying code can't run without root/admin privileges, making it impractical for general-purpose use.
来自维基百科:
在严格 W ^ X 安全的操作系统下运行的应用软件无法在允许写入的页面中执行指令ーー只有操作系统本身可以将指令写入内存,然后执行这些指令。
Generating 专门职能 on the fly has long been common in computer graphics. See e.g. Rob Pike and Bart Locanthi and John Reiser 位图图形的硬件软件折衷(1984) or this 张贴(2006) by Chris Lattner on Apple's use of LLVM for runtime code specialization in their OpenGL stack.
在某些情况下,软件诉诸于一种称为 trampoline的技术,它涉及到在堆栈(或其他位置)上动态创建代码。例如,GCC 的 嵌套函数和一些 Unice 的 signal mechanism。
Sometimes code is translated into code at runtime (this is called 动态二进制翻译):
Emulators like Apple's 罗塞塔 use this technique to speed up emulation. Another example is Transmeta's 编码变形软件编码变形软件.
Some 动态连接器动态连接器 modify code at runtime. This article provides some background on the runtime relocation of Windows DLLs, which is effectively a form of code modification.
什么是程序自修改?将值放入执行流的代码,以便不将其解释为数据而是将其解释为命令。当然,在函数式语言中有一种理论观点认为实际上没有区别。我的意思是在 e 上可以直接用命令式语言和编译器/解释器做到这一点,而不需要假定具有相同的地位。
What I'm referring to is in the practical sense that data can alter program execution paths (in some sense this is extremely obvious). I am thinking of something like a compiler-compiler that creates a table (an array of data) that one traverses through in parsing, moving from state to state (and also modifying other variables), just like how a program moves from command to command, modifying variables in the process.
I run statistical analyses against a continually updated database. My statistical model is written and re-written each time the code is executed to accommodate new data that become available.
当针对不同(微)体系结构有多个版本时,它还可以用于将函数调用更改为最优化的版本。例如,你有相同的函数写在标量,SSE2,AVX,AVX-512... 并取决于当前的 CPU 你会选择最好的一个。这可以很容易地使用函数指针来完成,这些指针在启动时由代码调度程序设置,但是这样就会多出一个间接级别,这对 CPU 不利。有些编译器支持自动编译到不同版本的 函数多版本控制,然后在加载时,链接器会将函数地址固定到所需的地址。但是,如果您不支持编译器和链接器,并且您也不希望使用间接方式,那么该怎么办呢?只需在启动时自己修改调用指令,而不是更改函数指针。现在所有的调用都是静态的,并且可以被 CPU 正确地预测