|
|
|
|
| <<A guide to the internals of the GNU linker>>摘录 |
|
|
|
[Original]
[Print]
[Top]
|
前几天看的,把其中的重点摘录了一下翻译过来,在移植ld的时候用的上。
附件里是全文。
每个target都若干个emulations,其中包含默认的linker script。
emulation在build过程中由shell脚本genscripts.sh产生。genscripts.sh首先读入emulparams目录的一个文件来,这个文件用来设置在genscripts.sh中使用到的一些shell变量和其他要被调用的shell脚本;然后调用scripttempl目录下的一个shell脚本,由此生成默认的linker scripts(该shell会被调用5、6次,对应生成5、6个linker scripts,链接时由命令行参数选择);之后调用emultempl目录下个shell脚本,该脚本生成emulation的源文件,绝大多数的target都是用generic.em来生成。
ld的命令行中用-m可以指定emulation,用-V可以显示target支持的所有emulations。
emulparams目录下的shell脚本由在configure.tgt中设置的名为targ_emul的shell变量指定。targ_emul=emul ---> 脚本就是emulparams/emul.sh。emul.sh中主要设置脚本变量供genscripts.sh和scripttempl、emultempl目录下的脚本使用。SCRIPT_NAME=script(指定scripttempl目录下的脚本为script.sc)、TEMPLATE_NAME=template(指定emultempl目录下的脚本为template.em,缺省值为generic)、GENERATE_SHLIB_SCRIPT、OUTPUT_FORMAT、ARCH、ENTRY、TEXT_START_ADDR、NONPAGED_TEXT_START_ADDR、SEGMENT_SIZE、TARGET_PAGE_SIZE、ALIGNMENT。
scripttempl/script.sc就是输出一个链接脚本到标准输出,如果emulparams目录下的脚本文件是emul.sh,那么输出的链接脚本就是build目录下的ldscripts/emul.extension。extension由genscripts.sh中的shell变量LD_FLAG指定,genscripts.sh调用scripttempl/script.sc脚本5到8次,每次生成一个不同的链接脚本,对应的后缀extension不同。
-----------------------------------------------------
|LD_FLAG |链接脚本后缀名 | ld调用参数 |
-----------------------------------------------------
|empty | .x |without followings(默认)|
-----------------------------------------------------
|n | .xn | -n |
-----------------------------------------------------
|N | .xbn | -N |
-----------------------------------------------------
|r | .xr | -r |
-----------------------------------------------------
|u | .xu | -u |
-----------------------------------------------------
|shared | .xs | -shared |
-----------------------------------------------------
|c | .xc | -c |
-----------------------------------------------------
|cshared | .xsc | -cshared |
-----------------------------------------------------
除此,在script.sc中还会定义如下几个shell变量:RELOCATING、CONSTRUCTING、DATA_ALIGNMENT、CREATE_SHLIB、COMBRELOC。链接脚本的主要作用是把各个sections按顺序排放在正确的内存地址上。在一些特殊targets中链接脚本还有其他的作用。在MIPS平台上,链接脚本要定义符号_gp来初始化$gp reg。嵌入式系统的链接脚本中要定义符号__stack来初始化栈指针。
emultempl/template.em中的变量和函数都是static的。唯一全局可见的数据对象名为ld_
EMULATION_NAME_emulation,该变量是一个struct ld_emulation_xfer_struct结构类型,定义在ldemul.h中。这个结构中的域主要都是些函数指针,会被linker使用到。在build后,emultempl/下的脚本由template.em生成etemplate.c的源文件,再用ldemul.h和ldemul.c把指定的emulation赋值给变量ld_emulation。
ld的整个流程:
? main() in ‘ldmain.c’
? ‘emultempl/EMULATION.em’ has your code
? ldemul_choose_target (defaults to your target_name)
? ldemul_before_parse
? Parse argv, calls ldemul_parse_args for each
? ldemul_set_symbols
? ldemul_get_script
? parse script
? may call ldemul_hll or ldemul_syslib
? may call ldemul_open_dynamic_archive
? ldemul_after_parse
? lang_process() in ‘ldlang.c’
? create output_bfd
? ldemul_set_output_arch
? ldemul_create_output_section_statements
? read objects, create input bfds - all symbols exist, but have no values
? may call ldemul_unrecognized_file
? will call ldemul_recognized_file
? ldemul_after_open
? map input sections to output sections
? may call ldemul_place_orphan for remaining sections
? ldemul_before_allocation
? gives input sections offsets into output sections, places output sections
? ldemul_after_allocation - section addresses valid
? assigns values to symbols
? ldemul_finish - symbol values valid
? output bfd is written to disk
|
|
|
--
|
|
[Original]
[Print]
[Top]
|
|
|