没有合适的资源?快使用搜索试试~ 我知道了~
首页掌握GNU ld链接器:Linux内核开发基础
GNU ld 是一个强大的链接器,是GNU工具链中的关键组件,用于将目标程序中的对象文件和库连接在一起形成可执行文件。在Linux内核开发和源代码理解中,掌握 GNU ld 的使用至关重要。本指南由 Steve Chamberlain、Ian Lance Taylor 和 Red Hat Inc. 编写,旨在帮助开发者更好地理解和运用这个链接器。
**1. 使用 GNU ld**
GNU ld 是GNU项目的一部分,它不仅负责连接二进制模块,还能够处理复杂的链接过程,如处理链接脚本(ld scripts)。这些脚本允许程序员自定义链接器的行为,包括设定入口点、管理库依赖、指定输出文件结构等。
**2. 链接器命令行选项**
ld 的命令行选项非常丰富,可以根据不同的架构(如 i386 或 PowerPC)进行特定配置。对于 i386 目标平台,选项可能包括控制符号解析、链接库的指定和版本选择等。此外,环境变量也会影响链接过程,如`LD_LIBRARY_PATH`可以指定搜索共享库的路径。
**3. 链接器脚本**
链接器脚本是通过 `.lds` 文件编写的,它扩展了链接器的功能,允许用户精细地控制链接过程。基本概念包括:
- **基本链接器脚本概念**:脚本包含一系列指令,它们告诉链接器如何组织和定位目标程序中的各个部分。
- **脚本格式**:脚本采用一种特定的语法,通常包括标签、地址分配、段定义和链接器命令等。
- **简单示例**:初学者可以通过编写简单的脚本来了解基本用法,比如设置入口点(`.entry`指令)、指定输入文件(`.input`指令)和输出文件(`.output`指令)。
- **命令集**:
- 设置入口点:`.global` 和 `.entry` 用于指定程序的主函数入口点。
- 文件处理:`.text`, `.data`, `.bss` 等用于定义不同类型的内存区域。
- 对象文件格式处理:`.align` 用于对齐内存,`.reloc` 指定重定位信息。
- 其他命令:还有许多高级功能,如处理符号链接、链接库、解决重定位问题等。
GNU ld 的理解和掌握对于深入学习和优化Linux内核以及其他复杂软件项目至关重要。熟练使用链接器脚本不仅可以提高链接效率,还能确保最终产物满足性能和空间要求。要成为一个Linux内核开发者,务必花时间学习并实践使用 GNU ld,这将有助于你更好地探索和修改底层系统。
12 Using LD, the GNU linker
-Bstatic
-dn
-non_shared
-static Do not link against shared libraries. This is only meaningful on platforms for
which shared libraries are supported. The different variants of this option are
for compatibility with various systems. You may use this option multiple times
on the command line: it affects library searching for ‘-l’ options which follow
it.
-Bsymbolic
When creating a shared library, bind references to global symbols to the defi-
nition within the shared library, if any. Normally, it is possible for a program
linked against a shared library to override the definition within the shared li-
brary. This option is only meaningful on ELF platforms which support shared
libraries.
--check-sections
--no-check-sections
Asks the linker not to check section addresse s after they have been assigned to
see if there any overlaps. Normally the linker will perform this check, and if it
finds any overlaps it will produce suitable error messages. The linker does know
about, and does make allowances for sections in overlays. The default b e haviour
can be restored by using the command line switch ‘--check-sections’.
--cref Output a cross reference table. If a linker map file is being generated, the
cross reference table is printed to the map file. Otherwise, it is printed on the
standard output.
The format of the table is intentionally simple, so that it may be easily processed
by a script if necessary. The symbols are printed out, sorted by name. For each
symbol, a list of file names is given. If the symbol is defined, the first file listed
is the location of the definition. The remaining files contain references to the
symbol.
--no-define-common
This option inhibits the assignment of addresses to common symbols. The script
command INHIBIT_COMMON_ALLOCATION has the same effect. See Section 3.4.4
[Miscellaneous Commands], page 33.
The ‘--no-define-common’ option allows decoupling the decision to assign ad-
dresses to Common symbols from the choice of the output file type; otherwise
a non-Relocatable output type forces assigning addresses to Common symbols.
Using ‘--no-define-common’ allows Common symbols that are referenced from
a shared library to be assigned addresses only in the main program. This elim-
inates the unused duplicate space in the shared library, and also prevents any
possible confusion over resolving to the wrong duplicate when there are many
dynamic modules with specialized search paths for runtime symbol resolution.
--defsym symbol =expression
Create a global symbol in the output file, containing the absolute address given
by expression. You may use this option as many times as necessary to define
Chapter 2: Invocation 13
multiple symbols in the command line. A limited form of arithmetic is sup-
ported for the expression in this context: you may give a hexadecimal constant
or the name of an existing symbol, or use + and - to add or subtract hexadec-
imal constants or symbols. If you need more elaborate expressions, consider
using the linker com mand language from a script (see Section 3.5 [Assignment:
Symbol Definitions], page 34). Note: there should be no white space between
symbol, the equals sign (“
h
=
i
”), and expression.
--demangle[=style ]
--no-demangle
These options control whether to demangle symbol names in error messages and
other output. When the linker is told to demangle, it tries to present symbol
names in a readable fashion: it strips leading underscores if they are used by
the object file format, and converts C++ mangled symbol names into user read-
able names. Different compilers have different mangling styles. The optional
demangling style argument can be used to choose an appropriate demangling
style for your compiler. The linker will demangle by default unless the envi-
ronment variable ‘COLLECT_NO_DEMANGLE’ is set. These options may be used to
override the default.
--dynamic-linker file
Set the name of the dynamic linker. This is only meaningful when generating
dynamically linked ELF executables. The default dynamic linker is normally
correct; don’t use this unless you know what you are doing.
--embedded-relocs
This option is only meaningful when linking MIPS embedded PIC code, gen-
erated by the -membedded-pic option to the gnu compiler and assembler. It
causes the linker to create a table which may be used at runtime to relocate
any data which was statically initialized to pointer values. See the code in
testsuite/ld-empic for details.
--fatal-warnings
Treat all warnings as errors.
--force-exe-suffix
Make sure that an output file has a .exe suffix.
If a successfully built fully linked output file does not have a .exe or .dll suffix,
this option forces the linker to copy the output file to one of the same name
with a .exe suffix. This option is useful when using unmodified Unix makefiles
on a Microsoft Windows host, since some versions of Windows won’t run an
image unless it ends in a .exe suffix.
--no-gc-sections
--gc-sections
Enable garbage collection of unused input sections. It is ignored on targets that
do not support this option. This option is not compatible with ‘-r’, nor should
it be used with dynamic linking. The default behaviour (of not performing this
garbage collection) can be restored by specifying ‘--no-gc-sections’ on the
command line.
14 Using LD, the GNU linker
--help Print a summary of the command-line options on the standard output and exit.
--target-help
Print a summary of all target specific options on the standard output and exit.
-Map mapfile
Print a link map to the file mapfile. See the description of the ‘-M’ option,
above.
--no-keep-memory
ld normally optimizes for speed over memory usage by caching the symbol
tables of input files in memory. This option tells ld to instead optimize for
memory usage, by rereading the symbol tables as necessary. This may be
required if ld runs out of memory space while linking a large executable.
--no-undefined
-z defs Normally when creating a non-symbolic shared library, undefined symbols are
allowed and le ft to be resolved by the runtime loader. This option disallows
such undefined symbols if they come from regular object files. The switch
‘--no-allow-shlib-undefined’ c ontrols the behaviour for shared objects be-
ing linked into the shared library.
--allow-multiple-definition
-z muldefs
Normally when a symbol is defined multiple times, the linker will report a fatal
error. These options allow multiple definitions and the first definition will be
used.
--allow-shlib-undefined
--no-allow-shlib-undefined
Allow (the default) or disallow undefined symbols in shared objects. The setting
of this switch overrides ‘--no-undefined’ where shared objects are concerned.
Thus if ‘--no-undefined’ is set but ‘--no-allow-shlib-undefined’ is not,
the net result will be that undefined symbols in regular objec t files will trigger
an error, but undefined symbols in shared objects will be ignored.
The reason that ‘--allow-shlib-undefined’ is the default is that the shared
object being specified at link time may not be the same one that is available
at load time, so the symbols might actually be resolvable at load time. Plus
there are some systems, (eg BeOS) where undefined symbols in shared libraries
is normal since the kernel patches them at load time to select which function
is most appropriate for the current architecture. eg. to dynamically select an
appropriate memset function. Apparently it is also normal for HPPA shared
libraries to have undefined symbols.
--no-undefined-version
Normally when a symbol has an undefined version, the linker will ignore it.
This option disallows symbols with undefined version and a fatal error will be
issued instead.
--no-warn-mismatch
Normally ld will give an error if you try to link together input files that are
mismatched for some reason, perhaps because they have been compiled for
Chapter 2: Invocation 15
different processors or for different endiannesses. This option tells ld that it
should silently permit such possible errors. This option should only be used
with care, in cases when you have taken some special action that ensures that
the linker errors are inappropriate.
--no-whole-archive
Turn off the effect of the ‘--whole-archive’ option for subsequent archive files.
--noinhibit-exec
Retain the executable output file w henever it is still usable. Normally, the linker
will not produce an output file if it encounters errors during the link process;
it exits without writing an output file when it issues any error whatsoever.
-nostdlib
Only search library directories explicitly specified on the command line. Library
directories specified in linker scripts (including linker scripts specified on the
command line) are ignored.
--oformat output-format
ld may be configured to support more than one kind of object file. If your
ld is configured this way, you can use the ‘--oformat’ option to specify the
binary format for the output object file. Even whe n ld is configured to support
alternative object formats, you don’t usually need to specify this, as ld should
be configured to produce as a default output format the most usual format on
each machine. output-format is a text string, the name of a particular format
supported by the BFD libraries. (You can list the available binary formats
with ‘objdump -i’.) The script command OUTPUT_FORMAT can also specify the
output format, but this option overrides it. See Chapter 5 [BFD], page 69.
-qmagic This option is ignored for Linux compatibility.
-Qy This option is ignored for SVR4 compatibility.
--relax An option with machine dependent effects. This option is only s upported on a
few targets. See Section 4.12 [ld and the H8/300], page 59. See Section 4.13 [ld
and the Intel 960 family], page 59. See Section 4.20 [ld and Xtensa Processors],
page 66.
On some platforms, the ‘--relax’ option performs global optimizations that
become possible when the linker resolves addressing in the program, such as
relaxing address modes and synthesizing new instructions in the output object
file.
On some platforms these link time global optimizations may make symbolic
debugging of the resulting executable impossible. This is known to be the case
for the Matsushita MN10200 and MN10300 family of processors.
On platforms where this is not supported, ‘--relax’ is accepted, but ignored.
--retain-symbols-file filename
Retain only the symbols listed in the file filename, discarding all others. file-
name is simply a flat file, with one symbol name per line. This option is esp e -
cially useful in environments (such as VxWorks) where a large global symbol
table is accumulated gradually, to conserve run-time memory.
16 Using LD, the GNU linker
‘--retain-symbols-file’ does not discard undefined symbols, or symbols
needed for relocations.
You may only specify ‘--retain-symbols-file’ once in the command line. It
overrides ‘-s’ and ‘-S’.
-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 w hich
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 filesystems.
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.
-rpath-link DIR
When using ELF or SunOS, one shared library may require another. This
happens when an ld -shared link includes a shared library as one of the input
files.
When the linker encounters such a dependency when doing a non-shared, non-
relocatable link, it will automatically try to locate the required shared library
and include it in the link, if it is not included explicitly. In s uch a case,
the ‘-rpath-link’ option specifies the first set of directories to search. The
‘-rpath-link’ option may specify a sequence of directory names either by spec-
ifying a list of names separated by colons, or by appearing multiple times.
This option should be used with caution as it overrides the search path that
may have been hard compiled into a shared library. In such a case it is possible
to use unintentionally a different search path than the runtime linker would do.
The linker uses the following search paths to locate required shared libraries.
1. Any directories specified by ‘-rpath-link’ options.
2. Any directories specified by ‘-rpath’ options. The difference between
‘-rpath’ and ‘-rpath-link’ is that directories specified by ‘-rpath’ op-
tions are included in the executable and used at runtime, whereas the
‘-rpath-link’ option is only effective at link time. It is for the native
linker only.
3. On an ELF system, if the ‘-rpath’ and rpath-link options were not used,
search the contents of the e nvironment variable LD_RUN_PATH. It is for the
native linker only.
剩余95页未读,继续阅读
wzj3514
- 粉丝: 3
- 资源: 65
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- portfolio2021
- VB在桌面上显示圆形时钟
- torch_sparse-0.6.4-cp37-cp37m-linux_x86_64whl.zip
- HmSetup.zip
- lombok.jar压缩包
- 带动画效果的二级下拉导航菜单
- FoodOrderingApp-Backend
- 投资组合网站
- CoopCPS:出版物来源
- 取GDI图像信息.rar
- torch_cluster-1.5.5-cp37-cp37m-win_amd64whl.zip
- 青少年的消费行为及消费心理DOC
- keIpie.github.io
- 纯css一款非常时髦的菜单
- 风景
- warehouse-location-management:湖畔培训项目
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功