GNU链接脚本语法(草稿)

链接工具使用(草稿,完善中)

链接脚本的选择

  • 默认脚本

如果没有指定链接脚本,链接器将会选用默认的链接脚本

You can use the ‘–verbose’ command line option to display the default linker script. Certain command line options,
such as ‘-r’ or ‘-N’, will affect the default linker script.

  • 自定义脚本

You may supply your own linker script by using the ‘-T’ command line option.

3.1 Basic Linker Script Concepts

基本概念与词汇

  • The linker combines input files into a single output file.
    • 虽然输入文件一般是可执行文件,但从目的出发,我们都统称输入、输出文件为object文件;
  • Each section in an object file has a name and a size.
    • loadable,当输出文件运行时,该部分数据应该被加载到内存中;
    • allocatable, 无内容的section,应该在内存中排除并且不加载什么数据到这个部分;
    • 其他,非loadable与allocatable的section一般存放debug信息;
      • Every loadable or allocatable output section has two addresses.
        • VMA(virtual memory address),当输出文件运行时,当前段应在的内存位置;
        • LMA(load memory address),当前sections应被加载到的位置;
        • 通常两者的地址相同;
        • 例外,数据段保存在ROM中(LMA),但后续被拷贝到RAM中(VMA)启动;
    • You can see the sections in an object file by using the objdump program with the ‘-h’ option.
  • Every object file also has a list of symbols, known as the symbol table.
    • Each symbol has a name, and each defined symbol has an address, among other information.
      • defined,已经定义的函数、全局变量戓静态变量;
      • undefined,当前输入文件中引用了的函数戓全局变量;
      • You can see the symbols in an object file by using the nm program, or by using the objdump
        program with the ‘-t’ option.

3.2 Linker Script Format

  • 链接脚本是一个纯文本文件;
  • 每条命令包含一个关键字或是符号的赋值;
  • 使用分号;分隔;
  • 空白字符将会被忽略;
  • 文件名或format名可以使用使用字串的形式写入;
  • 如果文件名中包含,逗号,可以将整个字符串使用双眼号包含;
  • 类似C语言,可以使用/**/的形式增加注释内容;

3.3 Simple Linker Script Example

最简单的链接脚本文件只有一句:SECTIONS.

SECTIONS可以用于描述:输出文件的内存分布;

SECTIONS
{
	. = 0x10000;			// 加载地址
	.text : { *(.text) }
	. = 0x8000000;			// 启动地址
	.data : { *(.data) }
	.bss : { *(.bss) }
}
  • .为地址计数符,SECTIONS命令的起始时,值.为0;

  • .text输出部分,list the names of the input sections which should be placed into this output section.

    • *通配符,*(.text),表示所有输入文件的.text部分;
    • 由于在.text段定义时设置了地址计数符为0x10000,所以链接器将会把输出文件的.text段地址放置在地址0x10000位置;
  • .data输出部分,

    • 链接器将会把输出文件的.data段放置在地址0x8000000位置;
    • 链接器将.data数据段设置在输出文件后,地址计数符将会变为:0x8000000 + 输出文件的.data段大小,效果即为:在输出文件中,.bss段将紧挨.data段末尾放置;

链接器会使用增加地址计数符的方式,确保每个输出部分对齐;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值