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

时间:2021-04-10 21:15:27

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. 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. 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或使用普通标准语言库之外的工具。我想把这些数据简单地附加到ELF文件的末尾(用某种哨兵来指示数据的开始,这样读取程序就可以向后寻找哨兵了),但我想确保这样做不会首先违反ELF规范。我对使用这样的附加数据是否能正常工作并不感兴趣;我想知道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.

我无法通过几次搜索确定该规范是否明确允许我想要的属性。

2 个解决方案

#1


7  

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. Only the ELF header has a fixed position in the file.”, which gives sufficient room to embed data one way or another, using a section, or doing without one [this is then unreferenced data!].

规范并没有真正说出任何关于它的内容,因此有人可能会争辩说“拥有尾随数据是未定义的行为”。另一方面,ELF规范对其期望非常清楚:“部分和细分没有指定的顺序。只有ELF标题在文件中有一个固定的位置。“,它提供了足够的空间以某种方式嵌入数据,使用一个部分,或者没有一个[这是未引用的数据!]。

This "data freedom" has been exploited since at least the end of the 1980s; consider "self-extracting archives" where a generic unpacking code stub is let loose on a trailing data portion.

这种“数据*”至少从20世纪80年代末开始被利用;考虑“自解压档案”,其中通用解包代码存根在尾随数据部分上松散。

In fact, you can find such implicit feature even in non-executable data formats, such as RIFF and 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. (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.)

实际上,即使在非可执行数据格式(如RIFF和PNG)中也可以找到这种隐式功能。当然,并非所有格式都允许这样做;特别是那些数据定义为运行到EOF而不是存储在某个标题中的固定长度的数据。 (考虑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.

使用这样的未引用数据只有一个缺点:在读取和保存文件时,您可能会丢失此数据。

#2


0  

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. And don't forget to document very well (if possible, in a freely accessible document) what you are doing.

可以将额外的数据添加到ELF文件中(因为您可以向ELF添加新的段和新的部分),但是您应该(或改进)工具来处理“改进的”ELF,这可能是重要的负担。并且不要忘记记录(如果可能的话,在可*访问的文档中)您正在做什么。

#1


7  

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. Only the ELF header has a fixed position in the file.”, which gives sufficient room to embed data one way or another, using a section, or doing without one [this is then unreferenced data!].

规范并没有真正说出任何关于它的内容,因此有人可能会争辩说“拥有尾随数据是未定义的行为”。另一方面,ELF规范对其期望非常清楚:“部分和细分没有指定的顺序。只有ELF标题在文件中有一个固定的位置。“,它提供了足够的空间以某种方式嵌入数据,使用一个部分,或者没有一个[这是未引用的数据!]。

This "data freedom" has been exploited since at least the end of the 1980s; consider "self-extracting archives" where a generic unpacking code stub is let loose on a trailing data portion.

这种“数据*”至少从20世纪80年代末开始被利用;考虑“自解压档案”,其中通用解包代码存根在尾随数据部分上松散。

In fact, you can find such implicit feature even in non-executable data formats, such as RIFF and 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. (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.)

实际上,即使在非可执行数据格式(如RIFF和PNG)中也可以找到这种隐式功能。当然,并非所有格式都允许这样做;特别是那些数据定义为运行到EOF而不是存储在某个标题中的固定长度的数据。 (考虑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.

使用这样的未引用数据只有一个缺点:在读取和保存文件时,您可能会丢失此数据。

#2


0  

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. And don't forget to document very well (if possible, in a freely accessible document) what you are doing.

可以将额外的数据添加到ELF文件中(因为您可以向ELF添加新的段和新的部分),但是您应该(或改进)工具来处理“改进的”ELF,这可能是重要的负担。并且不要忘记记录(如果可能的话,在可*访问的文档中)您正在做什么。