📄 ld.html
字号:
<HTML><HEAD><!-- This HTML file has been created by texi2html 1.52 from ld.texinfo on 22 March 1999 --><TITLE>Using LD, the GNU linker</TITLE><!-- Changed by: Bill Lawson, 23-Mar-1999 --></HEAD><BODY><H1>Using ld</H1><H2>The GNU linker</H2><H2><CODE>ld</CODE> version 2</H2><H2>January 1994</H2><ADDRESS>Steve Chamberlain</ADDRESS><ADDRESS>Cygnus Support</ADDRESS><P><P><HR><P><P><P>Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.</P><P>Permission is granted to make and distribute verbatim copies ofthis manual provided the copyright notice and this permission noticeare preserved on all copies.</P><P>Permission is granted to copy and distribute modified versions of thismanual under the conditions for verbatim copying, provided also thatthe entire resulting derived work is distributed under the terms of apermission notice identical to this one.</P><P>Permission is granted to copy and distribute translations of this manualinto another language, under the above conditions for modified versions.</P><H1><A NAME="SEC1" HREF="ld_toc.html#TOC1">Overview</A></H1><P><A NAME="IDX1"></A><A NAME="IDX2"></A><CODE>ld</CODE> combines a number of object and archive files, relocatestheir data and ties up symbol references. Usually the last step incompiling a program is to run <CODE>ld</CODE>.</P><P><CODE>ld</CODE> accepts Linker Command Language files written ina superset of AT&T's Link Editor Command Language syntax,to provide explicit and total control over the linking process.</P><P>This version of <CODE>ld</CODE> uses the general purpose BFD librariesto operate on object files. This allows <CODE>ld</CODE> to read, combine, andwrite object files in many different formats--for example, COFF or<CODE>a.out</CODE>. Different formats may be linked together to produce anyavailable kind of object file. See section <A HREF="ld.html#SEC26">BFD</A>, for more information.</P><P>Aside from its flexibility, the GNU linker is more helpful than otherlinkers in providing diagnostic information. Many linkers abandonexecution immediately upon encountering an error; whenever possible,<CODE>ld</CODE> continues executing, allowing you to identify other errors(or, in some cases, to get an output file in spite of the error).</P><H1><A NAME="SEC2" HREF="ld_toc.html#TOC2">Invocation</A></H1><P>The GNU linker <CODE>ld</CODE> is meant to cover a broad range of situations,and to be as compatible as possible with other linkers. As a result,you have many choices to control its behavior.</P><H2><A NAME="SEC3" HREF="ld_toc.html#TOC3">Command Line Options</A></H2><P><A NAME="IDX3"></A><A NAME="IDX4"></A>Here is a summary of the options you can use on the <CODE>ld</CODE> commandline:</P><PRE>ld [ -o <VAR>output</VAR> ] <VAR>objfile</VAR>... [ -A<VAR>architecture</VAR> ] [ -b <VAR>input-format</VAR> ] [ -Bstatic ] [ -Bdynamic ] [ -Bsymbolic ] [ -c <VAR>MRI-commandfile</VAR> ] [ -d | -dc | -dp ] [ -defsym <VAR>symbol</VAR>=<VAR>expression</VAR> ] [ -dynamic-linker <VAR>file</VAR> ] [ -embedded-relocs ] [ -export-dynamic ] [ -e <VAR>entry</VAR> ] [ -F ] [ -F <VAR>format</VAR> ] [ -format <VAR>input-format</VAR> ] [ -g ] [ -G <VAR>size</VAR> ] [ -help ] [ -i ] [ -l<VAR>archive</VAR> ] [ -L<VAR>searchdir</VAR> ] [ -M ] [ -Map <VAR>mapfile</VAR> ] [ -m <VAR>emulation</VAR> ] [ -N | -n ] [ -noinhibit-exec ] [ -no-keep-memory ] [ -oformat <VAR>output-format</VAR> ] [ -R <VAR>filename</VAR> ] [ -relax ] [ -retain-symbols-file <VAR>filename</VAR> ] [ -r | -Ur ] [ -rpath <VAR>dir</VAR> ] [-rpath-link <VAR>dir</VAR> ] [ -S ] [ -s ] [ -soname <VAR>name</VAR> ] [ -shared ] [ -sort-common ] [ -stats ] [ -T <VAR>commandfile</VAR> ] [ -Ttext <VAR>org</VAR> ] [ -Tdata <VAR>org</VAR> ] [ -Tbss <VAR>org</VAR> ] [ -t ] [ -traditional-format ] [ -u <VAR>symbol</VAR>] [-V] [-v] [ -verbose] [ -version ] [ -warn-common ] [ -warn-constructors] [ -warn-once ] [ -y <VAR>symbol</VAR> ] [ -X ] [-x ] [ -( [ archives ] -) ] [ --start-group [ archives ] --end-group ] [ -split-by-reloc <VAR>count</VAR> ] [ -split-by-file ] [ --whole-archive ]</PRE><P>This plethora of command-line options may seem intimidating, but inactual practice few of them are used in any particular context.<A NAME="IDX5"></A>For instance, a frequent use of <CODE>ld</CODE> is to link standard Unixobject files on a standard, supported Unix system. On such a system, tolink a file <CODE>hello.o</CODE>:</P><PRE>ld -o <VAR>output</VAR> /lib/crt0.o hello.o -lc</PRE><P>This tells <CODE>ld</CODE> to produce a file called <VAR>output</VAR> as theresult of linking the file <CODE>/lib/crt0.o</CODE> with <CODE>hello.o</CODE> andthe library <CODE>libc.a</CODE>, which will come from the standard searchdirectories. (See the discussion of the <SAMP>`-l'</SAMP> option below.)</P><P>The command-line options to <CODE>ld</CODE> may be specified in any order, andmay be repeated at will. Repeating most options with adifferent argument will either have no further effect, or override prioroccurrences (those further to the left on the command line) of thatoption. </P><P>The exceptions--which may meaningfully be used more than once--are<SAMP>`-A'</SAMP>, <SAMP>`-b'</SAMP> (or its synonym <SAMP>`-format'</SAMP>), <SAMP>`-defsym'</SAMP>,<SAMP>`-L'</SAMP>, <SAMP>`-l'</SAMP>, <SAMP>`-R'</SAMP>, <SAMP>`-u'</SAMP>, and <SAMP>`-('</SAMP> (or itssynonym <SAMP>`--start-group'</SAMP>).</P><P><A NAME="IDX6"></A>The list of object files to be linked together, shown as <VAR>objfile</VAR>...,may follow, precede, or be mixed in with command-line options, except thatan <VAR>objfile</VAR> argument may not be placed between an option andits argument. Alternatively a list of files may be specified using the syntax <CODE>@listfile</CODE>. Here <CODE>listfile</CODE> is the name of a file containing a list of file names, one per line.</P><P>Usually the linker is invoked with at least one object file, but you canspecify other forms of binary input files using <SAMP>`-l'</SAMP>, <SAMP>`-R'</SAMP>,and the script command language. If <EM>no</EM> binary input files at allare specified, the linker does not produce any output, and issues themessage <SAMP>`No input files'</SAMP>.</P><P>If the linker can not recognize the format of an object file, it willassume that it is a linker script. A script specified in this wayaugments the main linker script used for the link (either the defaultlinker script or the one specified by using <SAMP>`-T'</SAMP>). This featurepermits the linker to link against a file which appears to be an objector an archive, but actually merely defines some symbol values, or uses<CODE>INPUT</CODE> or <CODE>GROUP</CODE> to load other objects. See section <A HREF="ld.html#SEC5">Command Language</A>.</P><P>For options whose names are a single letter,option arguments must either follow the option letter without interveningwhitespace, or be given as separate arguments immediately following theoption that requires them.</P><P>For options whose names are multiple letters, either one dash or two canprecede the option name; for example, <SAMP>`--oformat'</SAMP> and<SAMP>`-oformat'</SAMP> are equivalent. Arguments to multiple-letter optionsmust either be separated from the option name by an equals sign, or begiven as separate arguments immediately following the option thatrequires them. For example, <SAMP>`--oformat srec'</SAMP> and<SAMP>`--oformat=srec'</SAMP> are equivalent. Unique abbreviations of the namesof multiple-letter options are accepted.</P><DL COMPACT><DT><CODE>-A<VAR>architecture</VAR></CODE><DD><A NAME="IDX7"></A> <A NAME="IDX8"></A> In the current release of <CODE>ld</CODE>, this option is useful only for theIntel 960 family of architectures. In that <CODE>ld</CODE> configuration, the<VAR>architecture</VAR> argument identifies the particular architecture inthe 960 family, enabling some safeguards and modifying thearchive-library search path. See section <A HREF="ld.html#SEC25"><CODE>ld</CODE> and the Intel 960 family</A>, for details.Future releases of <CODE>ld</CODE> may support similar functionality forother architecture families.<A NAME="IDX9"></A><A NAME="IDX10"></A><A NAME="IDX11"></A><A NAME="IDX12"></A><DT><CODE>-b <VAR>input-format</VAR></CODE><DD><CODE>ld</CODE> may be configured to support more than one kind of objectfile. If your <CODE>ld</CODE> is configured this way, you can use the<SAMP>`-b'</SAMP> option to specify the binary format for input object filesthat follow this option on the command line. Even when <CODE>ld</CODE> isconfigured to support alternative object formats, you don't usually needto specify this, as <CODE>ld</CODE> should be configured to expect as adefault input format the most usual format on each machine.<VAR>input-format</VAR> is a text string, the name of a particular formatsupported by the BFD libraries. (You can list the available binaryformats with <SAMP>`objdump -i'</SAMP>.) <SAMP>`-format <VAR>input-format</VAR>'</SAMP>has the same effect, as does the script command <CODE>TARGET</CODE>.See section <A HREF="ld.html#SEC26">BFD</A>.You may want to use this option if you are linking files with an unusualbinary format. You can also use <SAMP>`-b'</SAMP> to switch formats explicitly (whenlinking object files of different formats), by including<SAMP>`-b <VAR>input-format</VAR>'</SAMP> before each group of object files in aparticular format. The default format is taken from the environment variable<CODE>GNUTARGET</CODE>.See section <A HREF="ld.html#SEC4">Environment Variables</A>.You can also define the inputformat from a script, using the command <CODE>TARGET</CODE>; see section <A HREF="ld.html#SEC23">Option Commands</A>.<A NAME="IDX13"></A><DT><CODE>-Bstatic</CODE><DD>Do not link against shared libraries. This is only meaningful onplatforms for which shared libraries are supported.<A NAME="IDX14"></A><DT><CODE>-Bdynamic</CODE><DD>Link against dynamic libraries. This is only meaningful on platformsfor which shared libraries are supported. This option is normally thedefault on such platforms.<A NAME="IDX15"></A><DT><CODE>-Bsymbolic</CODE><DD>When creating a shared library, bind references to global symbols to thedefinition within the shared library, if any. Normally, it is possiblefor a program linked against a shared library to override the definitionwithin the shared library. This option is only meaningful on ELFplatforms which support shared libraries.<A NAME="IDX16"></A><A NAME="IDX17"></A><DT><CODE>-c <VAR>MRI-commandfile</VAR></CODE><DD>For compatibility with linkers produced by MRI, <CODE>ld</CODE> accepts scriptfiles written in an alternate, restricted command language, described insection <A HREF="ld.html#SEC30">MRI Compatible Script Files</A>. Introduce MRI script files withthe option <SAMP>`-c'</SAMP>; use the <SAMP>`-T'</SAMP> option to run linkerscripts written in the general-purpose <CODE>ld</CODE> scripting language.If <VAR>MRI-cmdfile</VAR> does not exist, <CODE>ld</CODE> looks for it in the directoriesspecified by any <SAMP>`-L'</SAMP> options.<A NAME="IDX18"></A><A NAME="IDX19"></A><A NAME="IDX20"></A><A NAME="IDX21"></A><DT><CODE>-d</CODE><DD><DT><CODE>-dc</CODE><DD><DT><CODE>-dp</CODE><DD>These three options are equivalent; multiple forms are supported forcompatibility with other linkers. Theyassign space to common symbols even if a relocatable output file isspecified (with <SAMP>`-r'</SAMP>). The script command<CODE>FORCE_COMMON_ALLOCATION</CODE> has the same effect. See section <A HREF="ld.html#SEC23">Option Commands</A>.<A NAME="IDX22"></A><A NAME="IDX23"></A><DT><CODE>-defsym <VAR>symbol</VAR>=<VAR>expression</VAR></CODE><DD>Create a global symbol in the output file, containing the absoluteaddress given by <VAR>expression</VAR>. You may use this option as manytimes as necessary to define multiple symbols in the command line. Alimited form of arithmetic is supported for the <VAR>expression</VAR> in thiscontext: you may give a hexadecimal constant or the name of an existingsymbol, or use <CODE>+</CODE> and <CODE>-</CODE> to add or subtract hexadecimalconstants or symbols. If you need more elaborate expressions, considerusing the linker command language from a script (see section <A HREF="ld.html#SEC13">Assignment: Defining Symbols</A>). <EM>Note:</EM> there should be nowhite space between <VAR>symbol</VAR>, the equals sign ("<KBD>=</KBD>"), and<VAR>expression</VAR>.<A NAME="IDX24"></A><A NAME="IDX25"></A><DT><CODE>-dynamic-linker <VAR>file</VAR></CODE><DD>Set the name of the dynamic linker. This is only meaningful whengenerating dynamically linked ELF executables. The default dynamiclinker is normally correct; don't use this unless you know what you aredoing.<A NAME="IDX26"></A><A NAME="IDX27"></A><DT><CODE>-embedded-relocs</CODE><DD>This option is only meaningful when linking MIPS embedded PIC code,generated by the -membedded-pic option to the GNU compiler andassembler. It causes the linker to create a table which may be used atruntime to relocate any data which was statically initialized to pointervalues. See the code in testsuite/ld-empic for details.<A NAME="IDX28"></A><A NAME="IDX29"></A><DT><CODE>-e <VAR>entry</VAR></CODE><DD>Use <VAR>entry</VAR> as the explicit symbol for beginning execution of yourprogram, rather than the default entry point. See section <A HREF="ld.html#SEC22">The Entry Point</A>, for adiscussion of defaults and other ways of specifying theentry point.<A NAME="IDX30"></A><A NAME="IDX31"></A><DT><CODE>-export-dynamic</CODE><DD>When creating an ELF file, add all symbols to the dynamic symbol table.Normally, the dynamic symbol table contains only symbols which are usedby a dynamic object. This option is needed for some uses of<CODE>dlopen</CODE>.<A NAME="IDX32"></A><DT><CODE>-F</CODE><DD><DT><CODE>-F<VAR>format</VAR></CODE><DD>Ignored. Some older linkers used this option throughout a compilationtoolchain for specifying object-file format for both input and outputobject files. The mechanisms <CODE>ld</CODE> uses for this purpose (the<SAMP>`-b'</SAMP> or <SAMP>`-format'</SAMP> options for input files, <SAMP>`-oformat'</SAMP>option or the <CODE>TARGET</CODE> command in linker scripts for output files,the <CODE>GNUTARGET</CODE> environment variable) are more flexible, but<CODE>ld</CODE> accepts the <SAMP>`-F'</SAMP> option for compatibility with scriptswritten to call the old linker.<A NAME="IDX33"></A><DT><CODE>-format <VAR>input-format</VAR></CODE><DD>Synonym for <SAMP>`-b <VAR>input-format</VAR>'</SAMP>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -