编译、链接、加载
编译、链接、加载是基础,十几年前从《深入理解计算机系统》等相关书籍中获得了比较全面的理解,现在已经变得有些模糊了。当时没有做总结的习惯,现在零零散散的记一些吧,有时间还要重温书本。
Build time
编译器将输入的源文件编译为汇编文件。
汇编器将输入的汇编文件翻译为机器码,将结果存在目标文件中;并为每个目标文件和指令分配内存地址,这个地址可能是真实的物理地址也可能是虚拟地址,虚拟地址是相对第一条指令的偏移量。
连接器解决所有目标文件中的内存引用,然后将所有的目标文件合并为一个可执行文件;具体分为两步:第一步找到所有引用的模块,方法和变量,第二步确定它们加载时的绝对物理地址。
加载器将可执行程序加载到内存,并初始化应用程序和数据栈,初始化寄存器,然后交给CPU执行。
Java
In Java, dynamic linking is the default behavior.
The Java Virtual Machine (JVM) resolves references to external classes and methods by loading the required libraries from the classpath when they are needed at runtime.However, Java also supports static linking through the use of tools like Ahead-of-Time (AOT) compilation.
Feature | Static Linking | Dynamic Linking |
---|---|---|
Linking time | Compile time | Runtime |
Executable size | Larger | Smaller |
Execution speed | Faster | Slower |
Flexibility | Less | More |
Default in Java | No | Yes |
Runtime
Processes are operating system tasks spawned from executables.
下面的图展示了进程的内存分布:
build system以及runtime/process
C and Java
Shared library
参考资料:
- https://www.baeldung.com/cs/compiler-linker-assembler-loader
- https://www.baeldung.com/cs/process-control-block