How to identify whether a file is elf file using C language function?

泪湿孤枕 提交于 2019-12-04 03:56:34

问题


In my program , I want to identify whether a file is ELF(Executable and Linkable Format) type. How to identify whether a file is elf file using C language function?


回答1:


If the only thing you want to do is test whether the file is ELF or not, then read the first 16 bytes to check the file identity. The first four bytes will equal {0x7f, 'E', 'L', 'F'}. The remaining bytes can vary, but checking them will help you be even more certain that the file is elf. See the elf(3) man page for more detail.

That man page gives enough info for parsing ELF files in general, but if you want to do more than just check the format, then you should probably use a library. See both the Elf Toolchain and the Binary File Descriptor Library.

Update: Yet another alternative is libmagic(3) which will read the ELF header for you. It is probably overkill if you are only interested in ELF, but libmagic also knows about just about every file format worth knowing about.



来源:https://stackoverflow.com/questions/23002797/how-to-identify-whether-a-file-is-elf-file-using-c-language-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!