静态链接(binutils的ld)与动态链接(glibc的ld-linux)

链接器(英语:Linker),又譯為鏈結器、連結器,是一个程序,将一个或多个由编译器或汇编器生成的目标文件外加库链接为一个可执行文件。

所谓目标文件: Computer programs typically comprise several parts or modules; all these parts/modules need not be contained within a single object file, and in such case refer to each other by means of symbols. Typically, an object file can contain three kinds of symbols:
  • defined symbols, which allow it to be called by other modules,
  • undefined symbols, which call the other modules where these symbols are defined, and
  • local symbols, used internally within the object file to facilitate relocation.


链接工作(binutils的ld程序): When a program comprises multiple object files, the linker combines these files into a unified executable program, resolving the symbols as it goes along.Linkers can take objects from a collection called a library. Some linkers do not include the whole library in the output; they only include its symbols that are referenced from other object files or libraries. Libraries exist for diverse purposes, and one or more system libraries are usually linked in by default.

涉及重定位:The linker also takes care of arranging the objects in a program's address space. This may involve relocating code that assumes a specific base address to another base. Since a compiler seldom knows where an object will reside, it often assumes a fixed base location (for example, zero). Relocating machine code may involve re-targeting of absolute jumps, loads and stores.

动态链接技术(glibc的ld-linux:ld.so, ld-linux.so*):
Many operating system environments allow dynamic linking, that is the postponing of the resolving of some undefined symbols until a program is run. That means that the executable code still contains undefined symbols, plus a list of objects or libraries that will provide definitions for these. Loading the program will load these objects/libraries as well, and perform a final linking.
The dynamic linker can be run either indirectly by running some dynamically linked program or library (in which case no command-line options to the dynamic linker can be passed and, in the ELF case, the dynamic linker which is stored in the .interp section of the program is executed) or directly by running:
/lib/ld-linux.so.* [OPTIONS] [PROGRAM [ARGUMENTS]]




附:
linux下文件的类型是不依赖于其后缀名的,但一般来讲:

  • .o,是目标文件,相当于windows中的.obj文件
  • .so 为共享库,是shared object,用于动态连接的,和dll差不多
  • .a为静态库,是好多个.o合在一起,用于静态连接
  • .la为libtool自动生成的一些共享库,vi编辑查看,主要记录了一些配置信息。可以用如下命令查看*.la文件的格式 $file *.la
  • *.la: ASCII English text
    所以可以用vi来查看其内容。

猜你喜欢

转载自chong-zh.iteye.com/blog/1606932
今日推荐