获得文本段的起始和结束地址在可执行 [英] Get the start and end address of text section in an executable

查看:331
本文介绍了获得文本段的起始和结束地址在可执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到一个可执行文件的文本段的起始和结束地址。我怎样才能得到它呢?

我可以得到大约结束地址 _init 符号或 _start 符号,但是起始地址?我应考虑文本的结束地址部分的 .RODATA 部分的开始前的最后一个地址<? / p>

或者我将编辑默认LD脚本,并添加自己的符号表示文本部分的开始和结束,并将它传递给GCC编译时?在这种情况下,我应该在哪里放置新的符号,应我认为init和FIN​​I节?

什么是一个很好的方式来获取文本段的起始和结束地址?


解决方案

对于基于ELF的平台的GNU binutils的默认链接脚本通常定义相当数量的可用于找出各个部分的开始和结束不同的符号。

正文部分的结束通常是由一个选择三种不同的符号引用: ETEXT _etext __ ETEXT ;一开始可以发现 __ executable_start 。 (请注意,这些符号通常导出使用 PROVIDE()的机构,这意味着它们将被覆盖如果别的东西在你的可执行文件的定义的他们,而不是仅仅引用的他们,尤其这意味着 _etext __ ETEXT 有可能会比 ETEXT 更安全的选择。)

例如:

  $猫etext.c
#包括LT&;&stdio.h中GT;焦炭的extern __executable_start;
焦炭的extern __etext;INT主要(无效)
{
  的printf(0X%LX \\ n,(无符号长)及__ executable_start);
  的printf(0X%LX \\ n,(无符号长)及__ ETEXT);
  返回0;
}
$ GCC -Wall -o ETEXT etext.c
$ ./etext
0x8048000
0x80484a0
$

我不认为任何这些符号是由任何标准规定,所以这不应该被认为是可移植的(我不知道是否甚至GNU binutils的提供他们的所有的ELF基于平台,还是提供已经改变了不同的binutils版本),但我想,如果一)你正在做的事情,需要此信息,并且b)你考虑砍死链接脚本作为一个选项的符号集,便携带性是不是太关注!

要看到符号的确切集合在特定平台上建立一个特别的事情,当你得到的,给 - 详细标志 LD (或轮候册, - 详细 GCC )来打印它选择的链接脚本使用(实际上有几种不同的默认链接脚本,根据连接选项,你正在构建的对象的类型而有所不同)。

I need to get the start and end address of an executable's text section. How can I get it?

I can get the starting address from the _init symbol or the _start symbol, but what about the ending address? Shall I consider the ending address of the text section to be the last address before starting of the .rodata section?

Or shall I edit the default ld script and add my own symbols to indicate the start and end of the text section, and pass it to GCC when compiling? In this case, where shall I place the new symbols, shall I consider the init and fini section?

What is a good way to get the start and end address of the text section?

解决方案

The GNU binutils default linker scripts for ELF-based platforms normally define quite a number of different symbols which can be used to find the start and end of various sections.

The end of the text section is usually referenced by a choice of three different symbols: etext, _etext or __etext; the start can be found as __executable_start. (Note that these symbols are usually exported using the PROVIDE() mechanism, which means that they will be overridden if something else in your executable defines them rather than merely referencing them. In particular that means that _etext or __etext are likely to be safer choices than etext.)

Example:

$ cat etext.c
#include <stdio.h>

extern char __executable_start;
extern char __etext;

int main(void)
{
  printf("0x%lx\n", (unsigned long)&__executable_start);
  printf("0x%lx\n", (unsigned long)&__etext);
  return 0;
}
$ gcc -Wall -o etext etext.c
$ ./etext
0x8048000
0x80484a0
$

I don't believe that any of these symbols are specified by any standard, so this shouldn't be assumed to be portable (I have no idea whether even GNU binutils provides them for all ELF-based platforms, or whether the set of symbols provided has changed over different binutils versions), although I guess if a) you are doing something that needs this information, and b) you're considering hacked linker scripts as an option, then portability isn't too much of a concern!

To see the exact set of symbols you get when building a particular thing on a particular platform, give the --verbose flag to ld (or -Wl,--verbose to gcc) to print the linker script it chooses to use (there are really several different default linker scripts, which vary according to linker options and the type of object you're building).

这篇关于获得文本段的起始和结束地址在可执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆