Wil*_*mKF 9 c++ symbols introspection strip dlsym
在我的C++程序中,如何在运行时以编程方式检测符号是否已通过Linux上的"strip"gnu开发工具剥离?
我想要一个函数定义,如果被剥离则返回true,否则返回false.
在"main()"上使用dlsym()可以有效地检测到它吗?
我知道file命令可以区分,所以你可以查看它的来源,看看它使用了什么机制.
从留下的评论中得到另一个答案:
剥离的ELF将缺少一个.symtab条目.该file命令遍历所有ELF节标题,直到找到符号表节.如果找不到,则认为二进制被剥离.
该libelf函数库允许一个程序来操作ELF对象文件,归档文件和归档成员.该精灵(3E)手册页提供与使用的库文件.下面的代码提供了一个示例,通过查找是否存在符号表section(.symtab)来确定是否删除了可执行文件.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
/* Include for ELF processing */
#include <libelf.h>
#include <gelf.h>
int main(int argc, char ** argv)
{
int fd;
const char *file = argv[0];
Elf *elf; /* ELF pointer for libelf */
Elf_Scn *scn; /* section descriptor pointer */
GElf_Shdr shdr; /* section header */
/* Open ELF file to obtain file descriptor */
if((fd = open(file, O_RDONLY)) < 0)
{
fprintf(stderr, "Error opening file %s\n", file);
exit(EXIT_FAILURE);
}
/* Protect program from using an older library */
if(elf_version(EV_CURRENT) == EV_NONE)
{
fprintf(stderr, "WARNING - ELF Library is out of date!\n");
exit(EXIT_FAILURE);
}
/* Initialize elf pointer for examining contents of file */
elf = elf_begin(fd, ELF_C_READ, NULL);
/* Initialize section descriptor pointer so that elf_nextscn()
* returns a pointer to the section descriptor at index 1. */
scn = NULL;
/* Iterate through ELF sections */
while((scn = elf_nextscn(elf, scn)) != NULL)
{
/* Retrieve section header */
gelf_getshdr(scn, &shdr);
/* If a section header holding a symbol table (.symtab)
* is found, this ELF file has not been stripped. */
if(shdr.sh_type == SHT_SYMTAB)
{
printf("NOT STRIPPED\n");
break;
}
}
elf_end(elf);
close(fd);
exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4411 次 |
| 最近记录: |