GNU ld链接时的entry point

来源:互联网 发布:puppy linux 编辑:程序博客网 时间:2024/05/21 22:52

在很多文章中都看到,使用GNU as编译的汇编程序要定义_start作为程序的entry point,至于为什么要这么做,则是因为GNU ld将_start定义为了程序默认的entry point,所谓的entry point也就是程序运行时执行第一条指令的地方,要查看GNU ld指定_start的地方,则比较纠结,因为GNU ld默认使用的链接脚本(linker script)是一起编译到GNU ld这个可执行程序中的,我们可以通过ld --verbose命令查看:

$ ld --verbose
GNU ld (GNU Binutils for Ubuntu) 2.20.51-system.20100908
  Supported emulations:
   elf_x86_64
   elf_i386
   i386linux
   elf_l1om
using internal linker script:
==================================================
/* Script for -z combreloc: combine and sort reloc sections */
OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64",
              "elf64-x86-64")
OUTPUT_ARCH(i386:x86-64)
ENTRY(_start)
SEARCH_DIR("/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");
SECTIONS
{
  /* Read-only sections, merged into text segment: */
  PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS;
  .interp         : { *(.interp) }
  .note.gnu.build-id : { *(.note.gnu.build-id) }  ...

红色部分就是GNU ld在默认的链接脚本中指定默认entry point的地方

当然,GNU ld也给我们提供了机会,让我们不去使用默认的entry pointer,自行指定entry point,通过GNU ld --help命令,我们可以看到:

 -e ADDRESS, --entry ADDRESS Set start address

我们可以在使用GNU ld链接时使用该命令自定义entry point

既然有多种方式可以指定entry point,那么如果多个地方都指定了呢?如何处理?

各种指定entry point的方式有不同的优先级,GNU ld会依次从高优先级到低优先级查看,使用最先找到的指定方式:

the ‘-e’ entry command-line option
theENTRY(symbol)command in a linker script
the value of the symbol start, if defined
the address of the first byte of the ‘.text’ section, if present
the address0




原创粉丝点击