简体   繁体   English

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

[英]Does appending arbitrary data to an ELF file violate the ELF spec?

I would like to add some information to an ELF file, but it ideally needs to be done in a way that a program can easily read this information without understanding ELF or using tools outside a normal standard language library. 我想在ELF文件中添加一些信息,但理想情况下,它需要以一种方式完成,即程序可以在不了解ELF或使用普通标准语言库之外的工具的情况下轻松读取此信息。 I was thinking of simply appending this data to the end of the ELF file (with some sort of sentinel to indicate the start of the data so the reading program can just seek backward to the sentinel), but I wanted to make sure this doesn't violate the ELF spec first. 我想把这些数据简单地附加到ELF文件的末尾(用某种哨兵来指示数据的开始,这样读取程序就可以向后寻找哨兵了),但我想确保这样做不会首先违反ELF规范。 I'm not interested in whether a particular loader works fine with such appended data; 我对使用这样的附加数据是否能够正常运行特定加载器感兴趣; I want to know if the ELF spec itself guarantees anything so that I can know different ELF-compliant loaders will be happy with it. 我想知道ELF规范本身是否保证任何东西,以便我可以知道不同的ELF兼容的加载器会很满意。

I see that questions like this have been asked before, but either assuming that this appending is ok or with no direct responses: 我之前已经问过这样的问题,但要么假设这个附加是好的,要么没有直接反应:

As far as I can tell, the ELF spec is here: 据我所知,ELF规范在这里:

I couldn't determine with a few searches whether the property I want is unambiguously allowed by that spec. 我无法通过几次搜索确定该规范是否明确允许我想要的属性。

The specification does not really say anything about it, so one could argue for "it's undefined behavior to have trailing data". 规范并没有真正说出任何关于它的内容,因此有人可能会争论“使用尾随数据这是未定义的行为”。 On the other hand, the ELF specification is rather clear about its expectations: “ sections and segments have no specified order. 另一方面,ELF规范对其期望非常清楚:“ 部分和细分没有指定的顺序。 Only the ELF header has a fixed position in the file. 只有ELF标题在文件中具有固定位置。 ”, which gives sufficient room to embed data one way or another, using a section, or doing without one [this is then unreferenced data!]. “,它提供了足够的空间以某种方式嵌入数据,使用一个部分,或者没有一个[这是未引用的数据!]。

This "data freedom" has been exploited since at least the end of the 1980s; 这种“数据自由”至少从20世纪80年代末开始被利用; consider "self-extracting archives" where a generic unpacking code stub is let loose on a trailing data portion. 考虑“自解压档案”,其中通用解包代码存根在尾随数据部分上松散。

In fact, you can find such implicit feature even in non-executable data formats, such as RIFF and PNG. 实际上,即使在非可执行数据格式(如RIFF和PNG)中也可以找到这种隐式功能。 Not all formats allow this of course; 当然,并非所有格式都允许这样做; in particular those where data is defined to runs until EOF rather than for a fixed length stored in some header. 特别是那些数据定义为运行到EOF而不是存储在某个标题中的固定长度的数据。 (Consider ZIP: appending data is not possible, but prepending is, which is what leads to EXE-ZIPs being readable by both (unmodified) unzip programs and operating systems.) (考虑ZIP:附加数据是不可能的,但是前置是,这导致两个 (未修改的)解压缩程序操作系统可以读取EXE-ZIP。)

There is just one drawback to using unreferenced data like this: when reading and saving a file, you can lose this data. 使用这样的未引用数据只有一个缺点:在读取和保存文件时,您可能会丢失此数据。

It might be ok to add extra data into ELF files (since you can add new segments and new sections to ELF), but you should have (or improve) the tools to work on your "improved" ELFs, and that may be a significant burden. 可以将额外的数据添加到ELF文件中(因为您可以向ELF添加新的段和新的部分),但是您应该(或改进)工具来处理“改进的”ELF,这可能是重要的负担。 And don't forget to document very well (if possible, in a freely accessible document) what you are doing. 并且不要忘记记录您正在做的事情(如果可能的话,在可自由访问的文档中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM