[LinkerScript.3] 简单的链接器脚本实例 - Simple Linker Script Example

本文介绍了如何使用链接器脚本定义输出文件的内存布局,包括设置特定地址的代码、初始化和未初始化的数据段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Many linker scripts are fairly simple.

很多链接器脚本都有相应的简单.


The simplest possible linker script has just one command: ‘SECTIONS’. You use the ‘SECTIONS’ command to describe the memory layout of the output file.

最简单的链接脚本可能仅有一个命令: 'SECTION'. 你可以使用'SECTION'命令去描述输出文件的内存分布.


The ‘SECTIONS’ command is a powerful command. Here we will describe a simple use of it. Let's assume your program consists only of code, initialized data, and uninitialized data. These will be in the ‘.text’, ‘.data’, and ‘.bss’ sections, respectively. Let's assume further that these are the only sections which appear in your input files.

'SECTION'命令是一个强大的命令.这儿我们将描述它的一个简单应用.让我假设你的程序仅由代码,初始化数据和未初始化数据所组成.这些将相应地在'.text','.data'和'.bss'的section中.让我们进一步假设在你的输入文件中有且仅有这些sections.


For this example, let's say that the code should be loaded at address 0x10000, and that the data should start at address 0x8000000. Here is a linker script which will do that:

对于这个例子,比如说代码应该被加载到地址为0x10000处,而数据应该开始在地址0x80000000处.给一个链接器脚本应该这样写:


     SECTIONS
     {
       . = 0x10000;
       .text : { *(.text) }
       . = 0x8000000;
       .data : { *(.data) }
       .bss : { *(.bss) }
     }

You write the ‘SECTIONS’ command as the keyword ‘SECTIONS’, followed by a series of symbol assignments and output section descriptions enclosed in curly braces.

你写的 SECTION 命令,就是关键字' SECTION ',随后跟上一系列的符号分配和输出section的描述都被放在大括号里.


The first line inside the ‘SECTIONS’ command of the above example sets the value of the special symbol ‘.’, which is the location counter. If you do not specify the address of an output section in some other way (other ways are described later), the address is set from the current value of the location counter. The location counter is then incremented by the size of the output section. At the start of the ‘SECTIONS’ command, the location counter has the value ‘0’.

上面例子SECTION里的第一行是设置特殊符号'.'的值.  '.'代表位置计数.如果你没有用一些其它的方式(之后会介绍其它的方式)指定一个输出section的地址,那么这个地址被设置为位置计数的当前值


The second line defines an output section, ‘.text’. The colon is required syntax which may be ignored for now. Within the curly braces after the output section name, you list the names of the input sections which should be placed into this output section. The ‘*’ is a wildcard which matches any file name. The expression ‘*(.text)’ means all ‘.text’ input sections in all input files.

第二行定义了一个输出section, '.text'. 冒号是语法要求的,现在可能可以忽略.在输出section名称之后的大括号里,你可以罗列出输入section的名称,这些输入的section会被放置到这个输出section里. '*'是一个表示匹配所有文件名称的通配符. '*(.text)'代表所有输入文件中所有名称为'.text'的输入section.


Since the location counter is ‘0x10000’ when the output section ‘.text’ is defined, the linker will set the address of the ‘.text’ section in the output file to be ‘0x10000’.

既然在输出section '.text'被定义时,该位置计数是0x10000,那么链接器将在输出文件中把'.text' section的地址设置为0x10000.


The remaining lines define the ‘.data’ and ‘.bss’ sections in the output file. The linker will place the ‘.data’ output section at address ‘0x8000000’. After the linker places the ‘.data’ output section, the value of the location counter will be ‘0x8000000’ plus the size of the ‘.data’ output section. The effect is that the linker will place the ‘.bss’ output section immediately after the ‘.data’ output section in memory.

剩下这行定义为输出文件中的'.data'和'.bss' section.  链接器将在地址0x80000000放置'.data'输出section. 在链接器放置'.data'输出section之后,位置计数的值将是0x80000000加上'.data'输出section的大小.  这影响的是链接器将在内存中把'.bss'输出section直接放置在'.data'输出section的后面.


The linker will ensure that each output section has the required alignment, by increasing the location counter if necessary. In this example, the specified addresses for the ‘.text’ and ‘.data’ sections will probably satisfy any alignment constraints, but the linker may have to create a small gap between the ‘.data’ and ‘.bss’ sections.

链接器将保证每个输出section是按要求对齐的, 如果需要对齐,则增加位置计数.在这个例子中,除了链接器可能需要在'.data'和'.bss' sections之间创建一个小的间隙外,'.text'和'.data' sections所指定的地址将可能满足任何对齐的限制.


That's it! That's a simple and complete linker script.

就是这样! 那就是一个简单和完整的链接器脚本.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值