顺序执行命令需要哪个符号链接_Linux链接脚本学习

一、概论

ld:GNU的链接器.

用来把一定量的目标文件跟档案文件链接在一起,并重新定位它们的数据,链接符号引用.

一般编译一个程序时,最后一步就是运行ld进行链接

每一个链接都被一个链接脚本所控制,这个脚本是用链接命令语言书写的.

二、链接脚本

链接脚本的一个主要目的是描述输入文件中的各个段(数据段,代码段,堆,栈,bss)如何被映射到输出文件中,并控制输出文件的内存排布.

链接器总是使用链接脚本的,如果你不提供,则链接器会使用一个缺省的脚本,这个脚本是被编译进链接器可执行文件的.

可以使用--verbose命令行显示缺省的链接器脚本的内容.

你可以使用-T命令行来提供你自己的链接脚本来替换缺省的链接脚本.

三、简单的链接脚本示例.

许多脚本是相当简单的.

可能最简单的脚本只含有一个命令:'SECTIONS'.

你可以使用'SECTIONS'来描述输出文件的内存布局.

'SECTIONS'是一个功能很强大的命令.

假设你的程序只有代码段,初始化过的数据段,和未初始化过的数据段.这些会存在于'.text','data','bss'段中.

对于这个例子,假设代码应该被载入到地址0x1000处,而数据应该从0x8000000开始,如下是实现这个功能的脚本:

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

具体分析:

关键字'SECTIONS'开始于这个配置.后面跟有一串放在花括号中的符号赋值和输出端描述的内容.

第一行是对一个特殊的符号'.'赋值,这是一个定位标识器.如果你没有以其他的方式制定输出段的地址,那地址值就会被设为定位标识器的现有值,即0x1000.

第二行定义一个输出段,'.text'.冒号':'是语法需要,现在可以被忽略.段后面的花括号中,应该列出所有应该放入这个输出段中的输入端的名字.'*'是通配符,匹配所有文件名.即将所有输入文件中的.text段都保存在此段中.

余下的是.data和.bss段,同理,链接器会把所有.data段从地址0x8000000开始处放置.

最后,定位标识器的值变为0x8000000加上所有.data段的地址.此时链接器把所有.bss放在此处开始的地址.

5da06bc9f4ef83060a914361741df2c1.png

四、简单的链接脚本命令

设置入口点

在运行一个程序时,第一个被执行到的指令成为"入口点".你可以使用"ENTRY"链接脚本命令来设置入口点.参数是一个符号名,如下:

ENTRY(SYMBOL)

有很多不同的方法来设置入口点.链接器会通过按顺序尝试一下方法来设置入口点,如果成功了,就会停止.

1,'-e' 入口命令行选项

2,链接脚本中的ENTRY(SYMBOL)命令

3,如果定义了start,就使用start的值

4,如果存在就使用'.text'段的首地址

5,地址'0'

五、命令行设置链接地址

ld用于将多个obj或者so(库)文件链接成可执行文件.

使用-T选项可以指定数据段,代码段,bss段起始位置.(-T只用于链接bootloader、内核等没有底层软件支持的软件.链接运行于操作系统之上的应用程序时,一般使用默认方式链接).

1,直接指定代码段、数据段、bss段起始地址

如下:

-Ttext startaddr

-Tdata startaddr

-Tbss startaddr

例如:

ld –Ttext 0x00000000 –g led_on.o –o led_on_elf

2,直接使用链接脚本来设置起始地址

ld –Ttimer.lds –o timer_elf a.o b.o

链接脚本timer.lds内容如下:

SECTIONS{.=0x30000000;.text : {*(.text)}.rodata ALIGN(4) : {*(.rodata)}.data ALIGN(4) : {*(.data)}.bss ALIGN(4) : {*.(.bss) *(COMMON)}}

一个SECTIONS命令内部包含一个或多个段,段(section)是连接脚本的基本单元,它表示输入文件中的某部分怎么放置.

六、一个完整的lds文件示例

* GNU linker script for STM32F405*//* Specify the memory areas */MEMORY{ FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x100000 /* entire flash, 1 MiB */ FLASH_ISR (rx) : ORIGIN = 0x08000000, LENGTH = 0x004000 /* sector 0, 16 KiB */ FLASH_TEXT (rx) : ORIGIN = 0x08020000, LENGTH = 0x080000 /* sectors 5,6,7,8, 4*128KiB = 512 KiB (could increase it more) */ CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 0x010000 /* 64 KiB */ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x020000 /* 128 KiB */}/* top end of the stack */_estack = ORIGIN(RAM) + LENGTH(RAM);/* RAM extents for the garbage collector */_ram_end = ORIGIN(RAM) + LENGTH(RAM);_heap_end = 0x2001c000; /* tunable *//* define output sections */SECTIONS{ /* The startup code goes first into FLASH */ .isr_vector : { . = ALIGN(4); KEEP(*(.isr_vector)) /* Startup code */ . = ALIGN(4); } >FLASH_ISR /* The program code and other data goes into FLASH */ .text : { . = ALIGN(4); *(.text) /* .text sections (code) */ *(.text*) /* .text* sections (code) */ *(.rodata) /* .rodata sections (constants, strings, etc.) */ *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ /* *(.glue_7) */ /* glue arm to thumb code */ /* *(.glue_7t) */ /* glue thumb to arm code */ . = ALIGN(4); _etext = .; /* define a global symbol at end of code */ _sidata = _etext; /* This is used by the startup in order to initialize the .data secion */ } >FLASH_TEXT /* .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH .ARM : { __exidx_start = .; *(.ARM.exidx*) __exidx_end = .; } >FLASH */ /* This is the initialized data section The program executes knowing that the data is in the RAM but the loader puts the initial values in the FLASH (inidata). It is one task of the startup to copy the initial values from FLASH to RAM. */ .data : AT ( _sidata ) { . = ALIGN(4); _sdata = .; /* create a global symbol at data start; used by startup code in order to initialise the .data section in RAM */ _ram_start = .; /* create a global symbol at ram start for garbage collector */ *(.data) /* .data sections */ *(.data*) /* .data* sections */ . = ALIGN(4); _edata = .; /* define a global symbol at data end; used by startup code in order to initialise the .data section in RAM */ } >RAM /* Uninitialized data section */ .bss : { . = ALIGN(4); _sbss = .; /* define a global symbol at bss start; used by startup code */ *(.bss) *(.bss*) *(COMMON) . = ALIGN(4); _ebss = .; /* define a global symbol at bss end; used by startup code */ } >RAM /* this is to define the start of the heap, and make sure we have a minimum size */ .heap : { . = ALIGN(4); _heap_start = .; /* define a global symbol at heap start */ } >RAM /* this just checks there is enough RAM for the stack */ .stack : { . = ALIGN(4); } >RAM /* Remove information from the standard libraries */ /* /DISCARD/ : { libc.a ( * ) libm.a ( * ) libgcc.a ( * ) } */ .ARM.attributes 0 : { *(.ARM.attributes) }}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值