微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linux ELF文件:对于静态和dynamicELF程序,哪个字节会有所不同?

我正在与Linux精灵文件

我想检测,如果给定的精灵程序是静态链接(全静态链接, ldd说“ not a dynamic executable ”)或dynamic链接。 ELF适用于embedded式Linux,因此我不能只运行它或使用ldd实用程序。

我想完全在我的程序中,通过读取和检查一些字节。 我不想依靠file实用程序或libelf,binutils等

哪些字节会不同?

在ELF可执行文件中检测未parsing的符号

在ELF文件中有效添加新的部分

将任意数据附加到ELF文件是否违反ELF规范?

如何find一个精灵文件的部分头string表的偏移量?

我可以通过在可执行文件中硬编码库path来避免导出LD_LIBRARY_PATH吗?

有没有检查ELF文件的emacs模式

是否有可能检查ELF可执行文件的“GNU_HASH”部分中的散列?

枚举当前加载的所有共享对象的所有ELF部分

你如何只提取ELF部分的内容

强制GNU链接生成32位ELF可执行文件

如何使用μClibc的 ldd.c ? 如果需要,应该很容易去除任何不需要的依赖关系/检查。 我认为这是一个更聪明的方法,而不是试图从阅读man 5 elf来看待所有的情况,尽管FWIW看起来只是检查PT_INTERP程序头,正如你在评论中所怀疑的那样。

更新:还有几个检查。 我试图提取相关的部分,但我不能确定,如果我错过了什么,所以检查自己。 该代码检查32位和64位x86 ELF文件。 它假设一个小端的架构。

#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <ctype.h> #include <inttypes.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/mman.h> #include <fcntl.h> #include <unistd.h> #include <elf.h> int main(int argc,char* argv[]) { const char* fname = argv[0]; if (argc >= 2) fname = argv[1]; int fd; struct stat st; void *mapping; if ((fd = open(fname,O_RDONLY)) == -1) { perror(fname); return 1; } if (fstat(fd,&st)) { perror("fstat"); close(fd); return 1; } if ((mapping = mmap(NULL,st.st_size,PROT_READ,MAP_SHARED,fd,0)) == MAP_Failed) { perror("mmap"); close(fd); return 1; } const Elf32_Ehdr* eh = mapping; if (st.st_size < (off_t)sizeof(Elf32_Ehdr) || eh->e_ident[EI_MAG0] != ELFMAG0 || eh->e_ident[EI_MAG1] != ELFMAG1 || eh->e_ident[EI_MAG2] != ELFMAG2 || eh->e_ident[EI_MAG3] != ELFMAG3 || eh->e_ident[EI_VERSION] != EV_CURRENT) { printf("Not a valid ELF filen"); return 0; } if (eh->e_type != ET_EXEC && eh->e_type != ET_DYN) { printf("Not executable or shared objectn"); return 0; } int is_dynamic = 0; // change as appropriate,but remember that byteswapping might be needed in some cases if (eh->e_ident[EI_CLASS] == ELFCLASS32 && eh->e_ident[EI_DATA] == ELFDATA2LSB && eh->e_machine == EM_386) { uint16_t ph_cnt; for (ph_cnt = 0; ph_cnt < eh->e_phnum; ph_cnt++) { const Elf32_Phdr* ph = (const Elf32_Phdr*)((const uint8_t*)mapping + eh->e_phoff + ph_cnt * eh->e_phentsize); if (ph->p_type == PT_DYNAMIC || ph->p_type == PT_INTERP) { is_dynamic = 1; } } } else if (eh->e_ident[EI_CLASS] == ELFCLASS64 && eh->e_ident[EI_DATA] == ELFDATA2LSB && eh->e_machine == EM_X86_64) { const Elf64_Ehdr* eh = mapping; uint16_t ph_cnt; for (ph_cnt = 0; ph_cnt < eh->e_phnum; ph_cnt++) { const Elf64_Phdr* ph = (const Elf64_Phdr*)((const uint8_t*)mapping + eh->e_phoff + ph_cnt * eh->e_phentsize); if (ph->p_type == PT_DYNAMIC || ph->p_type == PT_INTERP) { is_dynamic = 1; } } } else { printf("Unsupported architecturen"); return 0; } munmap(mapping,st.st_size); close(fd); printf("%s: %sdynamicn",fname,is_dynamic?"":"not "); return 0; }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐