|
有没有人对Linker脚本感兴趣,这是我翻译的,不全,如有人感兴趣,我可以完善它,如有错误之处,请指出!多谢!
Linker Scripts
Linker脚本
The ld command language is a collection of statements; some are simple keywords setting a particular option, some are used
ld 命令语言是一组语句的集合。有一些是一些简单的关键字设置一个参数。一些是用于
to select and group input files or name output files; and two statement types have a fundamental and pervasive impact on
选择和给输入文件分组或命名输出文件;两种语句类型在接连过程中有一个基本的和普遍的冲突。
the linking process.
The most fundamental command of the ld command language is the SECTIONS command (see section Specifying Output
大多数ld的基本命令语言是一个SECTIONS命令((see section Specifying Output
Sections). Every meaningful command script must have a SECTIONS command: it specifies a "picture" of the output file's
Sections). 每一个有意义的命令脚本必须有一个SECTIONS命令:它指定了一个输出文件的设计"蓝图"。
layout, in varying degrees of detail. No other command is required in all cases.
在所有的情况下,不需要其它命令。
The MEMORY command complements SECTIONS by describing the available memory in the target architecture. This command is
MEMORY命令通过描述在目标结构中的可用存储器来补充SECTIONS。这个命令是一个选项。
optional; if you don't use a MEMORY command, ld assumes sufficient memory is available in a contiguous block for all
如果你不使用MEMORY命令,ld假定对于所有的输出来说,存在连续的块中有足够的存储器可以利用。
output. See section Memory Layout.
See section Memory Layout.
You may include comments in linker scripts just as in C: delimited by `/*' and `*/'. As in C, comments are syntactically
你可以在linker脚本中包含一些注释,就象在C中一样:用/* 和 */界定。正如在C中一样,注释在语法上是等同于空白字符。
equivalent to whitespace.
Expressions
表达式
Many useful commands involve arithmetic expressions. The syntax for expressions in the command language is identical to
许多有用的命令包括算术表达式。在命令语言中,表达式的语法是与C表达式一样的
that of C expressions, with the following features:
,存在以下特点:
All expressions evaluated as integers and are of "long" or "unsigned long" type.
All constants are integers.
All of the C arithmetic operators are provided.
You may reference, define, and create global variables.
You may call special purpose built-in functions.
Integers
An octal integer is `0' followed by zero or more of the octal digits (`01234567').
_as_octal = 0157255;
A decimal integer starts with a non-zero digit followed by zero or more digits (`0123456789').
_as_decimal = 57005;
A hexadecimal integer is `0x' or `0X' followed by one or more hexadecimal digits chosen from `0123456789abcdefABCDEF'.
_as_hex = 0xdead;
To write a negative integer, use the prefix operator `-' (see section Operators).
_as_neg = -57005;
Additionally the suffixes K and M may be used to scale a constant by respectively. For example, the following all
另外,后缀K和M可以用于常数的单位。例如,下面所有语句是同一含义:
refer to the same quantity:
_fourk_1 = 4K;
_fourk_2 = 4096;
_fourk_3 = 0x1000;
Symbol Names
符号名
Unless quoted, symbol names start with a letter, underscore, or point and may include any letters, underscores,
除非引用,符号名起始于一个字母,下划线(_),或点(.)并且可以包含任何字符,下划线(_),数字,点(.)和连字符。
digits, points, and hyphens. Unquoted symbol names must not conflict with any keywords. You can specify a symbol which
非引用符号名必须不与任何关键字冲突。你能够通过在双引号("")中指定环境变量名
contains odd characters or has the same name as a keyword, by surrounding the symbol name in double quotes:
指定一个包含不成对的符号或与关键字有相同的一个名字
"SECTION" = 9;
"with a space" = "also with a space" + 10;
Since symbols can contain many non-alphabetic characters, it is safest to delimit symbols with spaces. For example, `A-B' is one symbol, whereas `A - B' is an expression involving subtraction.
The Location Counter
定位计数器
The special linker variable dot `.' always contains the current output location counter. Since the . always refers
linker的特殊变量点`.'总是包含当前输出位置的计数器。因为.总是
to a location in an output section, it must always appear in an expression within a SECTIONS command. The . symbol may
在输出setction中指示一个位置。它必须始终在一个SETCTIONS命令中以表达式的形式出现。.符号可
appear anywhere that an ordinary symbol is allowed in an expression, but its assignments have a side effect. Assigning a
出现在任何允许普通符号出现的表达式中。但它的存取有一个副作用。分配一个值
value to the . symbol will cause the location counter to be moved. This may be used to create holes in the output
给.符号将导致地址计数器移动。这可以被用于在输出section中生成一个空洞。
section. The location counter may never be moved backwards.
地址计数器永远也不会被向后移动。
SECTIONS
{
output :
{
file1(.text)
. = . + 1000;
file2(.text)
. += 1000;
file3(.text)
} = 0x1234;
}
In the previous example, file1 is located at the beginning of the output section, then there is a 1000 byte gap. Then file2
在前一个例子中,file1被定位在输出section的开始位置。之后,有一个1000字节的空隙。接下来是file2。
appears, also with a 1000 byte gap following before file3 is loaded. The notation `= 0x1234' specifies what data to write in
同样也有1000字节空隙,那是在file3被装载之前。符号`= 0x1234'指定在那个空隙中写入哪些数据。
the gaps (see section Optional Section Attributes).
@vfill
Operators
运算符
The linker recognizes the standard C set of arithmetic operators, with the standard bindings and precedence levels: {
linker可识别标准的C运算符,。。。。
@obeylines@parskip=0pt@parindent=0pt @dag@quad Prefix operators. @ddag@quad See section Assignment: Defining Symbols. }
Evaluation
The linker uses "lazy evaluation" for expressions; it only calculates an expression when absolutely necessary. The linker needs the value of the start address, and the lengths of memory regions, in order to do any linking at all; these values are computed as soon as possible when the linker reads in the command file. However, other values (such as symbol values) are not known or needed until after storage allocation. Such values are evaluated later, when other information (such as the sizes of output sections) is available for use in the symbol assignment expression.
Assignment: Defining Symbols
分配:字义字符
You may create global symbols, and assign values (addresses) to global symbols, using any of the C assignment
你可以生成全局符号,同时使用任何C赋值操作符赋一个值(地址)给这个全局变量,
operators:
symbol = expression ;
symbol &= expression ;
symbol += expression ;
symbol -= expression ;
symbol *= expression ;
symbol /= expression ;
Two things distinguish assignment from other operators in ld expressions.
在ld表达式中,与其它运算符相比有两个不同。
Assignment may only be used at the root of an expression; `a=b+3;' is allowed, but `a+b=3;' is an error.
You must place a trailing semicolon (";") at the end of an assignment statement.
Assignment statements may appear:
as commands in their own right in an ld script; or
as independent statements within a SECTIONS command; or
as part of the contents of a section definition in a SECTIONS command.
The first two cases are equivalent in effect--both define a symbol with an absolute address. The last case defines a symbol whose address is relative to a particular section (see section Specifying Output Sections).
When a linker expression is evaluated and assigned to a variable, it is given either an absolute or a relocatable
当一个linker表达式被求值并赋值给一个变量时,它可获得一个或者绝对的或者是可重定位的类型,
type. An absolute expression type is one in which the symbol contains the value that it will have in the output file; a
一个绝对表达式类型是一个在输出文件中拥有一个值的符号。
relocatable expression type is one in which the value is expressed as a fixed offset from the base of a section.
而一个可重定位的表达式类型是一个以setction为基点的一个固定偏移量的符号。
The type of the expression is controlled by its position in the script file. A symbol assigned within a section definition
表达式的类型是被在脚本文件中它的位置控制的。一个在section中定义的被存取的符号
is created relative to the base of the section; a symbol assigned in any other place is created as an absolute symbol.
相对于section的基点被生成。在任何其它地方被存取的符号作为一个绝对符号生成。
Since a symbol created within a section definition is relative to the base of the section, it will remain relocatable if
因为在一个section定义内,一个被生成的符号是与这个section的基点相关的。如果需要重定位输出,它将保持可重定位。
relocatable output is requested. A symbol may be created with an absolute value even when assigned to within a section
当通过使用绝对存取函数ABSOLUTE来在一个section定义内存取时,一个符号甚至可以以一个绝对
definition by using the absolute assignment function ABSOLUTE. For example, to create an absolute symbol whose address is
的值被生成, 例如,生成一个绝对符号,它的地址是
the last byte of an output section named .data:
被命名为.data的输出section的最后一个字节。
SECTIONS{ ...
.data :
{
*(.data)
_edata = ABSOLUTE(.) ;
}
... }
The linker tries to put off the evaluation of an assignment until all the terms in the source expression are known (see
linker尝试推迟值的分配,直到在源表达式中的所有条件是已知的为止。
section Evaluation). For instance, the sizes of sections cannot be known until after allocation, so assignments dependent
例如,sections的大小直到在分配之后才能知道。因此,直到在分配之后,分配依靠没有被执行。
upon these are not performed until after allocation. Some expressions, such as those depending upon the location counter
一些表达式,例如那些依靠位置计数器--点,
dot, `.' must be evaluated during allocation. If the result of an expression is required, but the value is not available,
`.'必须在分配期间被求值。如果需要表达式的结果,但它的值不可用,
then an error results. For example, a script like the following
那么将产生一个错误。例如,有一个脚本,象下面这样
SECTIONS { ...
text 9+this_isnt_constant :
{ ...
}
... }
will cause the error message "Non constant expression for initial address".
将导致一个错误信息"Non constant expression for initial address".
In some cases, it is desirable for a linker script to define a symbol only if it is referenced, and only if it is not
在有些情况下,。。。。
defined by any object included in the link. For example, traditional linkers defined the symbol `etext'. However, ANSI C requires that the user be able to use `etext' as a function name without encountering an error. The PROVIDE keyword may be used to define a symbol, such as `etext', only if it is referenced but not defined. The syntax is PROVIDE(symbol = expression).
Arithmetic Functions
算术函数
The command language includes a number of built-in functions for use in link script expressions.
在link脚本表达式中,命令语言包含一些内建的函数可用。
ABSOLUTE(exp)
Return the absolute (non-relocatable, as opposed to non-negative) value of the expression exp. Primarily useful to
返回表达式的绝对值(不可重定位,与非负数相反)。主要用于
assign an absolute value to a symbol within a section definition, where symbol values are normally section-relative.
在一个section定义内存取一个符号的绝对值,通常那个符号的位置是与section相关的。
ADDR(section)
Return the absolute address of the named section. Your script must previously have defined the location of that
返回section名字的绝对地址。以前,你的脚本必须存在那个section的位置的定义。
section. In the following example, symbol_1 and symbol_2 are assigned identical values:
在下面的例子中,symbol_1和symbol_2被赋予同样的值:
SECTIONS{ ...
.output1 :
{
start_of_output_1 = ABSOLUTE(.);
...
}
.output :
{
symbol_1 = ADDR(.output1);
symbol_2 = start_of_output_1;
}
... }
LOADADDR(section)
Return the absolute load address of the named section. This is normally the same as ADDR, but it may be different if
返回一个section名字的绝对地址。通常情况下,这与ADDR是相同的。但它可以不同,如果关键字AT被在section定义中,
the AT keyword is used in the section definition (see section Optional Section Attributes).
ALIGN(exp)
Return the result of the current location counter (.) aligned to the next exp boundary. exp must be an expression
返回当前地址计数器的值(.)。这个值对齐于下一个表达式的边界。这个表达式的值必须是2的次冥。
whose value is a power of two. This is equivalent to
它等于
(. + exp - 1) & ~(exp - 1)
ALIGN doesn't change the value of the location counter--it just does arithmetic on it. As an example, to align the
ALIGN不能改变地址计数器的值--它仅用它来做运算。做为一个例子,为了对齐
output .data section to the next 0x2000 byte boundary after the preceding section and to set a variable within the
输出的.data section到下0x2000字节边界,在前述的section之后,并在输入section设置一个变量在那个section到下一个
section to the next 0x8000 boundary after the input sections:
0x8000字节边界。
SECTIONS{ ...
.data ALIGN(0x2000): {
*(.data)
variable = ALIGN(0x8000);
}
... }
The first use of ALIGN in this example specifies the location of a section because it is used as the optional start
在这个例子中ALIGN的第一个应用指定了一个section的地址,因为它被用来做为一个section定义的启动属性。
attribute of a section definition (see section Optional Section Attributes). The second use simply defines the value
第二个应用简单地定义了一个变量的值,
of a variable. The built-in NEXT is closely related to ALIGN.
内建的NEXT是是与ALIGN关系最密的一个。
DEFINED(symbol)
Return 1 if symbol is in the linker global symbol table and is defined, otherwise return 0. You can use this function
如果symbol1是在linker全局变量表之内并且被定义过,则返回1。否则返回0.你能使用这个函数
to provide default values for symbols. For example, the following command-file fragment shows how to set a global
为符号提供一个缺省值。 例如,下面命令文件片断展示了如何设置
symbol begin to the first location in the .text section--but if a symbol called begin already existed, its value is
一个在.text section中起始于第一个位置的全局符号--但如果被调用符号已经存在,它的值将被保护:
preserved:
SECTIONS{ ...
.text : {
begin = DEFINED(begin) ? begin : . ;
...
}
... }
NEXT(exp)
Return the next unallocated address that is a multiple of exp. This function is closely related to ALIGN(exp); unless you use the MEMORY command to define discontinuous memory for the output file, the two functions are equivalent.
SIZEOF(section)
Return the size in bytes of the named section, if that section has been allocated. In the following example, symbol_1 and symbol_2 are assigned identical values:
SECTIONS{ ...
.output {
.start = . ;
...
.end = . ;
}
symbol_1 = .end - .start ;
symbol_2 = SIZEOF(.output);
... }
SIZEOF_HEADERS
sizeof_headers
Return the size in bytes of the output file's headers. You can use this number as the start address of the first section, if you choose, to facilitate paging.
MAX(exp1, exp2)
Returns the maximum of exp1 and exp2.
MIN(exp1, exp2)
Returns the minimum of exp1 and exp2.
Semicolons
分号
Semicolons (";") are required in the following places. In all other places they can appear for aesthetic reasons but are
分号(";")需要用在下面这些地方。从美学角度来讲,它们可出现在所有其它地方,
otherwise ignored.
而不被乎略。
Assignment
分配
Semicolons must appear at the end of assignment expressions. See section Assignment: Defining Symbols
分号必须出现在分配表达式之后。
PHDRS
Semicolons must appear at the end of a PHDRS statement. See section ELF Program Headers
Memory Layout
存储空间规划
The linker's default configuration permits allocation of all available memory. You can override this configuration
linker的缺省配置允许所有变量空间的分配。你可以通过使用存储器命令,乎略这个配置。
by using the MEMORY command. The MEMORY command describes the location and size of blocks of memory in the target. By using
存储器命令描述了在目标中的存储器块的大小和位置。使用这个命令要小心,
it carefully, you can describe which memory regions may be used by the linker, and which memory regions it must avoid. The
你可以描述哪个存储区可以被linker使用,以及哪些存储区它必须避免使用。
linker does not shuffle sections to fit into the available regions, but does move the requested sections into the correct
linker不会把sections弄乱来适应可用区,但可以移动所需的sections到
regions and issue errors when the regions become too full.
正确的存储区并且当存储区变得太满时,发出一个错误。
A command file may contain at most one use of the MEMORY command; however, you can define as many blocks of memory within
一个命令文件可以包含至多一个可用MEMORY命令;然而,你可以按你的需要在命令文件内定义一些内存块。
it as you wish. The syntax is:
语法如下:
MEMORY
{
name (attr) : ORIGIN = origin, LENGTH = len
...
} |
|