GNU ld 详解

GNU ld 命令就是链接器,用于将 *.o 文件链接到一起。执行以下操作:

1、重新分配地址(*.o文件中的地址都是相对地址,只有链接到一起才能确定最终的地址)

2、建立符号关联(例如,调用另外一个*.o文件的函数,是通过符号来查找的,具体不详述)


ld 往往是编译工作的最后一个被调用的程序(一般流程: 编译预处理, 编译, 汇编, 链接, [optional] strip )

ld 可以执行一种脚本,这种脚本用于控制如何链接。这种脚本使用 AT&T's Link Editor Command Language 的语法。


注意(非常重要):

    如果ld是被间接调用的,例如被gcc调用,则在所有传给 ld 的命令行选项之前,都要加一个前缀选项 -Wl, 例如:

    gcc -Wl,--start-group foo.o bar.o -Wl,--end-group

    如果不这样的话,则这些本应该传给链接器的选项就会被自动丢弃


常用选项:

-l 链接一个库

-R

-o

-L


--exclude-libs lib,lib,...
           Specifies a list of archive libraries from which symbols should not
           be automatically exported.  The library names may be delimited by
           commas or colons.  Specifying "--exclude-libs ALL" excludes symbols
           in all archive libraries from automatic export.  This option is
           available only for the i386 PE targeted port of the linker and for
           ELF targeted ports.  For i386 PE, symbols explicitly listed in a
           .def file are still exported, regardless of this option.  For ELF
           targeted ports, symbols affected by this option will be treated as
           hidden.


-EB Link big-endian objects.  This affects the default output format.
-EL Link little-endian objects.  This affects the default output
           format.


-rpath=dir
           Add a directory to the runtime library search path.  This is used
           when linking an ELF executable with shared objects.  All -rpath
           arguments are concatenated and passed to the runtime linker, which
           uses them to locate shared objects at runtime.  The -rpath option
           is also used when locating shared objects which are needed by
           shared objects explicitly included in the link; see the description
           of the -rpath-link option.  If -rpath is not used when linking an
           ELF executable, the contents of the environment variable
           "LD_RUN_PATH" will be used if it is defined.

           The -rpath option may also be used on SunOS.  By default, on SunOS,
           the linker will form a runtime search patch out of all the -L
           options it is given.  If a -rpath option is used, the runtime
           search path will be formed exclusively using the -rpath options,
           ignoring the -L options.  This can be useful when using gcc, which
           adds many -L options which may be on NFS mounted file systems.

           For compatibility with other ELF linkers, if the -R option is
           followed by a directory name, rather than a file name, it is
           treated as the -rpath option.

你可能感兴趣的:(操作系统,Android)