Linux 如何获得二进制文件文本部分的偏移量和大小?

Linux 如何获得二进制文件文本部分的偏移量和大小?,linux,gcc,assembly,executable,Linux,Gcc,Assembly,Executable,我想知道二进制文件文本部分的确切字节位置,以便更好地通过算法检查它。我尝试过使用size,但这似乎只给出了节的大小,而没有给出它们的位置,我尝试过使用readelf,但坦率地说,这给了我太多的信息。来自readelf-S的输出如下所示: There are 21 section headers, starting at offset 0x1fcdc: Section Headers: [Nr] Name Type Addr Off

我想知道二进制文件文本部分的确切字节位置,以便更好地通过算法检查它。我尝试过使用
size
,但这似乎只给出了节的大小,而没有给出它们的位置,我尝试过使用
readelf
,但坦率地说,这给了我太多的信息。

来自
readelf-S
的输出如下所示:

There are 21 section headers, starting at offset 0x1fcdc:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .hash             HASH            000000b4 0000b4 0008e0 04   A  2   0  4
  [ 2] .dynsym           DYNSYM          00000994 000994 0012f0 10   A  3   3  4
  [ 3] .dynstr           STRTAB          00001c84 001c84 0016a4 00   A  0   0  1
  [ 4] .gnu.version      VERSYM          00003328 003328 00025e 02   A  2   0  2
  [ 5] .gnu.version_d    VERDEF          00003588 003588 000118 00   A  3  10  4
  [ 6] .gnu.version_r    VERNEED         000036a0 0036a0 000110 00   A  3   2  4
  [ 7] .rel.dyn          REL             000037b0 0037b0 001428 08   A  2   0  4
  [ 8] .rel.plt          REL             00004bd8 004bd8 0006e0 08   A  2   9  4
  [ 9] .plt              PROGBITS        000052b8 0052b8 000a64 04  AX  0   0  4
  [10] .text             PROGBITS        00005d1c 005d1c 0141d0 00  AX  0   0  4
  [11] .rodata           PROGBITS        00019eec 019eec 004121 00   A  0   0  4
  [12] .data.rel.ro      PROGBITS        0001f010 01e010 00119c 00  WA  0   0  4
  [13] .dynamic          DYNAMIC         000201ac 01f1ac 000110 08  WA  3   0  4
  [14] .got              PROGBITS        000202bc 01f2bc 00045c 04  WA  0   0  4
  [15] .data             PROGBITS        00020718 01f718 0002d0 00  WA  0   0  4
  [16] .bss              NOBITS          000209e8 01f9e8 0005d8 00  WA  0   0  4
  [17] .comment          PROGBITS        00000000 01f9e8 0001f8 00      0   0  1
  [18] .ARM.attributes   ARM_ATTRIBUTES  00000000 01fbe0 00002b 00      0   0  1
  [19] .gnu_debuglink    PROGBITS        00000000 01fc0b 000014 00      0   0  1
  [20] .shstrtab         STRTAB          00000000 01fc1f 0000ba 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
[10]
行是
.text
部分。
Addr
列给出加载节的虚拟地址,
Off
列给出文件中到节的偏移量,
Size
列给出节的大小。所有数字都用十六进制表示

通过编程,您可以使用
elf.h
中定义的结构来解析文件,从而获得有关任何节的信息。下面是一个示例(为了简单起见,省略了错误检查和清理):

int fd=open(…,orduonly);
/*将ELF文件映射到内存中以便于操作*/
结构stat statbuf;
fstat(fd和statbuf);
char*fbase=mmap(NULL,statbuf.st_size,PROT_READ,MAP_SHARED,fd,0);
Elf32_Ehdr*Ehdr=(Elf32_Ehdr*)fbase;
Elf32_-Shdr*部门=(Elf32_-Shdr*)(fbase+ehdr->e_-shoff);
int shsize=ehdr->e_shentsize;
int shnum=ehdr->e_shnum;
int shstrndx=ehdr->e_shstrndx;
/*获取字符串表索引*/
Elf32_Shdr*shstrsect=§s[shstrndx];
char*shstrtab=fbase+shstrsect->sh_偏移量;
int i;

对于(i=0;iIs)我有没有办法找到可执行文件中文本节的位置?特别是我想在不加载可执行文件本身的情况下读取文件并操作其文本节。我不清楚吗?
Off
+
Size
列精确地给出了文本节的位置。
int fd = open(..., O_RDONLY);

/* map ELF file into memory for easier manipulation */
struct stat statbuf;
fstat(fd, &statbuf);
char *fbase = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);

Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fbase;
Elf32_Shdr *sects = (Elf32_Shdr *)(fbase + ehdr->e_shoff);
int shsize = ehdr->e_shentsize;
int shnum = ehdr->e_shnum;
int shstrndx = ehdr->e_shstrndx;

/* get string table index */
Elf32_Shdr *shstrsect = &sects[shstrndx];
char *shstrtab = fbase + shstrsect->sh_offset;

int i;
for(i=0; i<shnum; i++) {
    if(!strcmp(shstrtab+sects[i].sh_name, ".text")) {
        /* found the text section: inspect sects[i].sh_offset, sects[i].sh_size */
    }
}