如何找到带有标题信息的ELF文件/图像的大小? [英] How can I find the size of a ELF file/image with Header information?

查看:107
本文介绍了如何找到带有标题信息的ELF文件/图像的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一个小精灵图像的大小,以便进行一些计算.我已经尝试过在Linux上使用readelf实用程序,该实用程序提供了有关标题和部分的信息.我需要具有elf的确切文件大小(整体上).

I need to find the size of an elf image for some computation. I have tried with the readelf utility on linux which gives the informations about the headers and section. I need to have the exact file size of the elf(on the whole).

如何从标题信息中找到ELF的大小,或者是否有其他方法可以在不读取完整图像的情况下找到小精灵的大小.

How do I find the size of the ELF from the header information or Is there any other means to find the size of an elf without reading the full image.

推荐答案

您可以使用stat函数家族( lstat() fstat() )以获取任何文件的大小(使用stat成员的st_size成员). 您需要更具体的内容吗?

You can use the stat functions family (stat(), lstat(), fstat()) to get the size of any file (using the st_size member of the stat member). Do you need something more specific?

如果您确实要使用ELF结构,请使用包含该结构的elf.h标头:

If you really want to use the ELF structure, use the elf.h header which contains that structure:

typedef struct {
         unsigned char  e_ident[EI_NIDENT];
         uint16_t       e_type;
         uint16_t       e_machine;
         uint32_t       e_version;
         ElfN_Addr      e_entry;
         ElfN_Off       e_phoff;
         ElfN_Off       e_shoff;
         uint32_t       e_flags;
         uint16_t       e_ehsize;
         uint16_t       e_phentsize;
         uint16_t       e_phnum;
         uint16_t       e_shentsize;
         uint16_t       e_shnum;
         uint16_t       e_shstrndx;
 } Elf32_Ehdr;

它是ELF32文件的头(对于64位文件,将32替换为64). e_ehsize是文件大小,以字节为单位.

It's the header of an ELF32 file (replace 32 with 64 for a 64-bit file). e_ehsize is the size of the file in bytes.

我将逐字复制作为编辑建议发布的评论:

I'll copy verbatim the comment that was posted as an edit suggestion:

此答案不正确. e_ehsize只是elf标头的大小,而不是elf文件的大小.

This answer is incorrect. e_ehsize is merely the size of the elf header, not the elf file.

这篇关于如何找到带有标题信息的ELF文件/图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆