程序可以读取自己的小精灵部分吗?

问题描述 投票:6回答:3

我想使用ld的--build-id选项将构建信息添加到我的二进制文件中。但是,我不确定如何在程序中使用此信息。假设我想编写一个程序,该程序在每次发生异常时都编写回溯,并编写一个解析此信息的脚本。该脚本读取程序的符号表并搜索在backtrace中打印的地址(由于程序是静态链接的,而backtrace_symbols无法正常工作,我被迫使用此类脚本)。为了使脚本正常工作,我需要将程序的构建版本与创建回溯的程序的构建版本进行匹配。如何从程序本身打印程序的构建版本(位于.note.gnu.build-id elf部分)?

linux elf backtrace
3个回答
5
投票

如何从程序本身打印程序的构建版本(位于.note.gnu.build-id elf节中?

  1. 您需要阅读ElfW(Ehdr)(在文件的开头)以找到二进制文件中的程序头(.e_phoff.e_phnum会告诉您程序头在哪里,以及要读取多少个程序头。 )。

  2. 然后,您阅读程序头,直到找到程序的PT_NOTE段。该段将告诉您偏移到二进制文件中所有音符的开头。

  3. 然后您需要阅读ElfW(Nhdr)并跳过笔记的其余部分(笔记的总大小为sizeof(Nhdr) + .n_namesz + .n_descsz,正确对齐),直到找到带有.n_type == NT_GNU_BUILD_ID的笔记。

  4. 一旦找到NT_GNU_BUILD_ID音符,跳过其.n_namesz,并读取.n_descsz字节以读取实际的build-id。

您可以通过将读取的内容与readelf -n a.out的输出进行比较来验证您正在读取正确的数据。

P.S。

如果您要像上面那样繁琐地解码build-id,并且if不会被剥离,那么最好只解码并打印symbol名称而不是(即复制backtrace_symbols所做的操作)–实际上,这比解码ELF注释更容易,因为符号表包含固定大小的条目。


2
投票

基本上,这是我根据对问题的回答而编写的代码。为了编译代码,我必须进行一些更改,希望它能在尽可能多的平台上运行。但是,仅在一台构建计算机上对其进行了测试。我使用的一种假设是,程序是在运行该程序的计算机上构建的,因此检查程序和计算机之间的字节序兼容性毫无意义。

user@:~/$ uname -s -r -m -o
Linux 3.2.0-45-generic x86_64 GNU/Linux
user@:~/$ g++ test.cpp -o test
user@:~/$ readelf -n test | grep Build
    Build ID: dc5c4682e0282e2bd8bc2d3b61cfe35826aa34fc
user@:~/$ ./test
    Build ID: dc5c4682e0282e2bd8bc2d3b61cfe35826aa34fc
#include <elf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>

#if __x86_64__
#  define ElfW(type) Elf64_##type
#else
#  define ElfW(type) Elf32_##type
#endif

/*
detecting build id of a program from its note section
http://stackoverflow.com/questions/17637745/can-a-program-read-its-own-elf-section
http://www.scs.stanford.edu/histar/src/pkg/uclibc/utils/readelf.c
http://www.sco.com/developers/gabi/2000-07-17/ch5.pheader.html#note_section
*/

int main (int argc, char* argv[])
{
  char *thefilename = argv[0];
  FILE *thefile;
  struct stat statbuf;
  ElfW(Ehdr) *ehdr = 0;
  ElfW(Phdr) *phdr = 0;
  ElfW(Nhdr) *nhdr = 0;
  if (!(thefile = fopen(thefilename, "r"))) {
    perror(thefilename);
    exit(EXIT_FAILURE);
  }
  if (fstat(fileno(thefile), &statbuf) < 0) {
    perror(thefilename);
    exit(EXIT_FAILURE);
  }
  ehdr = (ElfW(Ehdr) *)mmap(0, statbuf.st_size, 
    PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
  phdr = (ElfW(Phdr) *)(ehdr->e_phoff + (size_t)ehdr);
  while (phdr->p_type != PT_NOTE)
  {
    ++phdr;
  }
  nhdr = (ElfW(Nhdr) *)(phdr->p_offset + (size_t)ehdr); 
  while (nhdr->n_type != NT_GNU_BUILD_ID)
  {
    nhdr = (ElfW(Nhdr) *)((size_t)nhdr + sizeof(ElfW(Nhdr)) + nhdr->n_namesz + nhdr->n_descsz);
  }
  unsigned char * build_id = (unsigned char *)malloc(nhdr->n_descsz);
  memcpy(build_id, (void *)((size_t)nhdr + sizeof(ElfW(Nhdr)) + nhdr->n_namesz), nhdr->n_descsz);
  printf("    Build ID: ");
  for (int i = 0 ; i < nhdr->n_descsz ; ++i)
  {
    printf("%02x",build_id[i]);
  }
  free(build_id);
  printf("\n");
  return 0;
}

0
投票

是,程序可以读取自己的.note.gnu.build-id。重要的是dl_iterate_phdr功能。

[我已经在Mesa(OpenGL / Vulkan实现)中使用了此技术,以读取其自己的build-id以与磁盘着色器缓存一起使用。

我已将这些位提取到一个单独的项目[1]中,以方便他人使用。

[1] https://github.com/mattst88/build-id

© www.soinside.com 2019 - 2024. All rights reserved.