读书人

朝花夕拾-1-程序的通译和启动

发布时间: 2012-11-08 08:48:11 作者: rapoo

朝花夕拾-1-程序的翻译和启动

引言

一般的人,都有怀旧情结,我也有。

做技术就像一场旅行,有时候走的远了,对我们刚开始的东西就模糊了,所以就会产生的怀旧的想法。

突然有人问你,程序是怎么开始运行的啊?大概过程是知道的,但是细节就不见得说的清楚,说着说着就感觉力不从心了。打算弄一些小短文,每一篇只说一个问题,力求短小精悍,给自己提提醒,挺好的。

程序的翻译和启动(Translating and Starting a Program)


1.1 a general picture

朝花夕拾-1-程序的通译和启动

上图是一个大概的过程,下面就一一说一下。


1.2 compiler

关于编译器,我写过一个东西,见博客的《编译原理》,编译完了生成汇编语言文件,其他的就不细说了。


1.3 assembler

汇编器处理完了,会生成目标文件,目标文件一般包含下面6部分:

1》The object file header describes the size and position of the other pieces of the object file.
2》 The text segment contains the machine language code.
3》The static data segment contains data allocated for the life of the program.
(UNIX allows programs to use either static data, which is allocated throughout the program, or dynamic data, which can grow or shrink as needed by the program.)
4》The relocation information identifies instructions and data words that depend on absolute addresses when the program is loaded into memory.
5》The symbol table contains the remaining labels that are not defined, such as external references.
6》The debugging information contains a concise description of how the modules were compiled so that a debugger can associate machine instructions with C source files and make data structures readable.


1.4 linker

汇编器处理完了是很多单独的文件,连接器负责把这些文件进行拼接,生成可执行文件。链接器的工作主要包含3部分:

1》. Place code and data modules symbolically in memory.
2》. Determine the addresses of data and instruction labels.
3》. Patch both the internal and external references.

可执行文件的内容格式和目标文件差不多,只是更详细而已。


1.5 loader

现在文件准备好了,但是还在硬盘上呆着呢,要想运行,就需要加载器了,加载器的工作如下:

1》. Reads the executable file header to determine size of the text and data segments.
2》. Creates an address space large enough for the text and data.
3》. Copies the instructions and data from the executable file into memory.
4》. Copies the parameters (if any) to the main program onto the stack.
5》. Initializes the machine registers and sets the stack pointer to the first free location.
6》. Jumps to a start-up routine that copies the parameters into the argument registers and calls the main routine of the program. When the main routine returns, the start-up routine terminates the program with an exit system call.


1.6 Dynamically Linked Libraries

库怎么弄呢,静态库?,动态库?


1.7 run

cpu根据PC的值,从RAM(准确点是cache)里取指令,执行。程序就开始运行了。


1.8 core和RAM都是AMBA(或者其它的内部总线)的设备,理解一下。

2楼36昨天 14:19
不错。n为啥CSDN不能直接转载呢,麻烦...
1楼aaalllggg昨天 12:17
有意犹未尽的感觉。。。

读书人网 >软件开发

热点推荐