📄 ld.html
字号:
<PRE>_as_decimal = 57005;</PRE><P><A NAME="IDX144"></A><A NAME="IDX145"></A>A hexadecimal integer is <SAMP>`0x'</SAMP> or <SAMP>`0X'</SAMP> followed by one ormore hexadecimal digits chosen from <SAMP>`0123456789abcdefABCDEF'</SAMP>.<PRE>_as_hex = 0xdead;</PRE><P><A NAME="IDX146"></A>To write a negative integer, usethe prefix operator <SAMP>`-'</SAMP>; see section <A HREF="ld.html#SEC11">Operators</A>.<PRE>_as_neg = -57005;</PRE><P><A NAME="IDX147"></A><A NAME="IDX148"></A><A NAME="IDX149"></A><A NAME="IDX150"></A><A NAME="IDX151"></A>Additionally the suffixes <CODE>K</CODE> and <CODE>M</CODE> may be used to scale aconstant by respectively. For example, the following all refer to the same quantity:</P><PRE> _fourk_1 = 4K; _fourk_2 = 4096; _fourk_3 = 0x1000;</PRE><H3><A NAME="SEC9" HREF="ld_toc.html#TOC9">Symbol Names</A></H3><P><A NAME="IDX152"></A><A NAME="IDX153"></A><A NAME="IDX154"></A><A NAME="IDX155"></A>Unless quoted, symbol names start with a letter, underscore, or pointand may include any letters, underscores, digits, points,and hyphens. Unquoted symbol names must not conflict with anykeywords. You can specify a symbol which contains odd characters or hasthe same name as a keyword, by surrounding the symbol name in double quotes:<PRE> "SECTION" = 9; "with a space" = "also with a space" + 10;</PRE><P>Since symbols can contain many non-alphabetic characters, it is safestto delimit symbols with spaces. For example, <SAMP>`A-B'</SAMP> is one symbol,whereas <SAMP>`A - B'</SAMP> is an expression involving subtraction.</P><H3><A NAME="SEC10" HREF="ld_toc.html#TOC10">The Location Counter</A></H3><P><A NAME="IDX156"></A><A NAME="IDX157"></A><A NAME="IDX158"></A><A NAME="IDX159"></A>The special linker variable <EM>dot</EM> <SAMP>`.'</SAMP> always contains thecurrent output location counter. Since the <CODE>.</CODE> always refers toa location in an output section, it must always appear in anexpression within a <CODE>SECTIONS</CODE> command. The <CODE>.</CODE> symbolmay appear anywhere that an ordinary symbol is allowed in anexpression, but its assignments have a side effect. Assigning a valueto the <CODE>.</CODE> symbol will cause the location counter to be moved.<A NAME="IDX160"></A>This may be used to create holes in the output section. The locationcounter may never be moved backwards.<PRE>SECTIONS{ output : { file1(.text) . = . + 1000; file2(.text) . += 1000; file3(.text) } = 0x1234;}</PRE><P>In the previous example, <CODE>file1</CODE> is located at the beginning of theoutput section, then there is a 1000 byte gap. Then <CODE>file2</CODE>appears, also with a 1000 byte gap following before <CODE>file3</CODE> isloaded. The notation <SAMP>`= 0x1234'</SAMP> specifies what data to write inthe gaps (see section <A HREF="ld.html#SEC20">Optional Section Attributes</A>).</P><P></P><H3><A NAME="SEC11" HREF="ld_toc.html#TOC11">Operators</A></H3><P><A NAME="IDX161"></A><A NAME="IDX162"></A><A NAME="IDX163"></A>The linker recognizes the standard C set of arithmetic operators, withthe standard bindings and precedence levels:<table><tr><td colspan=4><hr></td></tr><tr><th><b>Precedence</b></th><th><b>Associativity</b></th><th><b>Operators</b><th><b>Notes</b></th></tr><tr><td colspan=4><hr></td></tr><tr><td>(highest)</td></tr><tr><td>1</td><td>left</td><td>! - ~</td><td>(1)</td></tr><tr><td>2</td><td>left</td><td>* / %</td></tr><tr><td>3</td><td>left</td><td>+ -</td></tr><tr><td>4</td><td>left</td><td>>> &l;<</td></tr><tr><td>5</td><td>left</td><td>== != > < <= >=</td></tr><tr><td>6</td><td>left</td><td>&</td></tr><tr><td>7</td><td>left</td><td>|</td></tr><tr><td>8</td><td>left</td><td>&&</td></tr><tr><td>9</td><td>left</td><td>||</td></tr><tr><td>10</td><td>right</td><td>? :</td></tr><tr><td>11</td><td>right</td><td>&= += -= *= /=</td><td> (2)</td></tr><tr><td>(lowest)</td></tr><tr><td colspan=4><hr></td></tr><tr><td>(1)</td><td colspan=3>Prefix operators.</td></tr><tr><td>(2)</td><td colspan=3>See section <A HREF="ld.html#SEC13">Assignment: Defining Symbols</A>.</td></tr><tr><td colspan=4><hr></td></tr></table></P><H3><A NAME="SEC12" HREF="ld_toc.html#TOC12">Evaluation</A></H3><P><A NAME="IDX164"></A><A NAME="IDX165"></A>The linker uses "lazy evaluation" for expressions; it only calculatesan expression when absolutely necessary. The linker needs the value ofthe start address, and the lengths of memory regions, in order to do anylinking at all; these values are computed as soon as possible when thelinker reads in the command file. However, other values (such as symbolvalues) are not known or needed until after storage allocation. Suchvalues are evaluated later, when other information (such as the sizes ofoutput sections) is available for use in the symbol assignmentexpression.</P><H3><A NAME="SEC13" HREF="ld_toc.html#TOC13">Assignment: Defining Symbols</A></H3><P><A NAME="IDX166"></A><A NAME="IDX167"></A><A NAME="IDX168"></A>You may create global symbols, and assign values (addresses) to globalsymbols, using any of the C assignment operators:</P><DL COMPACT><DT><CODE><VAR>symbol</VAR> = <VAR>expression</VAR> ;</CODE><DD><DT><CODE><VAR>symbol</VAR> &= <VAR>expression</VAR> ;</CODE><DD><DT><CODE><VAR>symbol</VAR> += <VAR>expression</VAR> ;</CODE><DD><DT><CODE><VAR>symbol</VAR> -= <VAR>expression</VAR> ;</CODE><DD><DT><CODE><VAR>symbol</VAR> *= <VAR>expression</VAR> ;</CODE><DD><DT><CODE><VAR>symbol</VAR> /= <VAR>expression</VAR> ;</CODE><DD></DL><P>Two things distinguish assignment from other operators in <CODE>ld</CODE>expressions.<UL><LI>Assignment may only be used at the root of an expression;<SAMP>`a=b+3;'</SAMP> is allowed, but <SAMP>`a+b=3;'</SAMP> is an error.<A NAME="IDX169"></A><A NAME="IDX170"></A><LI>You must place a trailing semicolon ("<KBD>;</KBD>") at the end of anassignment statement.</UL><P>Assignment statements may appear:<UL><LI>as commands in their own right in an <CODE>ld</CODE> script; or<LI>as independent statements within a <CODE>SECTIONS</CODE> command; or<LI>as part of the contents of a section definition in a<CODE>SECTIONS</CODE> command.</UL><P>The first two cases are equivalent in effect--both define a symbol withan absolute address. The last case defines a symbol whose address isrelative to a particular section (see section <A HREF="ld.html#SEC16">Specifying Output Sections</A>).</P><P><A NAME="IDX171"></A><A NAME="IDX172"></A><A NAME="IDX173"></A>When a linker expression is evaluated and assigned to a variable, it isgiven either an absolute or a relocatable type. An absolute expressiontype is one in which the symbol contains the value that it will have inthe output file; a relocatable expression type is one in which thevalue is expressed as a fixed offset from the base of a section.</P><P>The type of the expression is controlled by its position in the scriptfile. A symbol assigned within a section definition is created relativeto the base of the section; a symbol assigned in any other place iscreated as an absolute symbol. Since a symbol created within asection definition is relative to the base of the section, itwill remain relocatable if relocatable output is requested. A symbolmay be created with an absolute value even when assigned to within asection definition by using the absolute assignment function<CODE>ABSOLUTE</CODE>. For example, to create an absolute symbol whose addressis the last byte of an output section named <CODE>.data</CODE>:<PRE>SECTIONS{ ... .data : { *(.data) _edata = ABSOLUTE(.) ; } ... }</PRE><P>The linker tries to put off the evaluation of an assignment until allthe terms in the source expression are known (see section <A HREF="ld.html#SEC12">Evaluation</A>). Forinstance, the sizes of sections cannot be known until after allocation,so assignments dependent upon these are not performed until afterallocation. Some expressions, such as those depending upon the locationcounter <EM>dot</EM>, <SAMP>`.'</SAMP> must be evaluated during allocation. If theresult of an expression is required, but the value is not available,then an error results. For example, a script like the following<PRE>SECTIONS { ... text 9+this_isnt_constant : { ... }... }</PRE><P><A NAME="IDX174"></A>will cause the error message "<CODE>Non constant expression for initialaddress</CODE>".</P><P><A NAME="IDX175"></A>In some cases, it is desirable for a linker script to define a symbolonly if it is referenced, and only if it is not defined by any objectincluded in the link. For example, traditional linkers defined thesymbol <SAMP>`etext'</SAMP>. However, ANSI C requires that the user be able touse <SAMP>`etext'</SAMP> as a function name without encountering an error.The <CODE>PROVIDE</CODE> keyword may be used to define a symbol, such as<SAMP>`etext'</SAMP>, only if it is referenced but not defined. The syntax is<CODE>PROVIDE(<VAR>symbol</VAR> = <VAR>expression</VAR>)</CODE>.</P><H3><A NAME="SEC14" HREF="ld_toc.html#TOC14">Arithmetic Functions</A></H3><P><A NAME="IDX176"></A>The command language includes a number of built-infunctions for use in link script expressions.<DL COMPACT><DT><CODE>ABSOLUTE(<VAR>exp</VAR>)</CODE><DD><A NAME="IDX177"></A> <A NAME="IDX178"></A> Return the absolute (non-relocatable, as opposed to non-negative) valueof the expression <VAR>exp</VAR>. Primarily useful to assign an absolutevalue to a symbol within a section definition, where symbol values arenormally section-relative.<A NAME="IDX179"></A><A NAME="IDX180"></A><DT><CODE>ADDR(<VAR>section</VAR>)</CODE><DD>Return the absolute address of the named <VAR>section</VAR>. Your script mustpreviously have defined the location of that section. In the followingexample, <CODE>symbol_1</CODE> and <CODE>symbol_2</CODE> are assigned identicalvalues:<PRE>SECTIONS{ ... .output1 : { start_of_output_1 = ABSOLUTE(.); ... } .output : { symbol_1 = ADDR(.output1); symbol_2 = start_of_output_1; }... }</PRE><A NAME="IDX181"></A><A NAME="IDX182"></A><DT><CODE>ALIGN(<VAR>exp</VAR>)</CODE><DD>Return the result of the current location counter (<CODE>.</CODE>) aligned tothe next <VAR>exp</VAR> boundary. <VAR>exp</VAR> must be an expression whosevalue is a power of two. This is equivalent to <PRE>(. + <VAR>exp</VAR> - 1) & ~(<VAR>exp</VAR> - 1)</PRE><CODE>ALIGN</CODE> doesn't change the value of the location counter--it justdoes arithmetic on it. As an example, to align the output <CODE>.data</CODE>section to the next <CODE>0x2000</CODE> byte boundary after the precedingsection and to set a variable within the section to the next<CODE>0x8000</CODE> boundary after the input sections:<PRE>SECTIONS{ ... .data ALIGN(0x2000): { *(.data) variable = ALIGN(0x8000); }... }</PRE>The first use of <CODE>ALIGN</CODE> in this example specifies the location ofa section because it is used as the optional <VAR>start</VAR> attribute of asection definition (see section <A HREF="ld.html#SEC20">Optional Section Attributes</A>). The second use simplydefines the value of a variable.The built-in <CODE>NEXT</CODE> is closely related to <CODE>ALIGN</CODE>.<A NAME="IDX183"></A><A NAME="IDX184"></A><DT><CODE>DEFINED(<VAR>symbol</VAR>)</CODE><DD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -