简体   繁体   English

将数据附加到可执行文件(Windows,Unix)

[英]Appending data to an executable (Windows, Unix)

I have a program which compiles and runs scripts. 我有一个可以编译和运行脚本的程序。

To create a standalone version of the script, I reserve a large static buffer to hold the compiled script. 为了创建脚本的独立版本,我保留了一个较大的静态缓冲区来保存已编译的脚本。 The compiled script is copied into a copy of the program and it can then be run from that copy. 编译后的脚本将被复制到程序的副本中,然后可以从该副本中运行它。

This works fine. 这很好。 It has some disadvantages however: 但是它有一些缺点:

  • the buffer is static and takes up space if there's no compiled program in it. 缓冲区是静态的,如果其中没有编译的程序,则会占用空间。
  • if the script to be included exceeds the buffer's size, I need to build a new version with a larger buffer. 如果要包含的脚本超出了缓冲区的大小,则需要使用更大的缓冲区来构建新版本。

I'd like to add the compiled script to the end of the program, but naively doing so doesn't work as the exe loader chokes on the new file size. 我想将已编译的脚本添加到程序的末尾,但是由于exe加载程序对新文件大小的限制,天真地这样做不起作用。

Is there a way to manipulate the exe so it would be acceptable for the loaders (mind this is a cross platform program)? 有没有一种方法可以操纵该exe,以便它对于装载程序是可以接受的(请注意,这是一个跨平台程序)?

would be acceptable for the loaders (mind this is a cross platform program)? 装载机是否可以接受(请注意,这是一个跨平台程序)?

I would think that this is unlikely to be possible without being platform specific. 我认为如果没有特定于平台的情况,这将不太可能实现。 Time for a common interface with different implementations (so the code that saves/loads the script is common, but the executable manipulation is specific). 具有不同实现的通用接口的时间(因此保存/加载脚本的代码是通用的,但是可执行操作是特定的)。

On Windows you'll hit the problem that a running executable file is locked against modification. 在Windows上,您将遇到一个问题,即正在运行的可执行文件被锁定,无法进行修改。 By working on copies this can be worked around (but the only way to rename back in a completely deterministic way it is perform the move on boot, but scheduling a job might be acceptable). 通过处理副本,可以解决此问题(但以完全确定性的方式重命名的唯一方法是在启动时执行移动,但安排作业可能是可以接受的)。

On Windows the easiest way to add data to an image (executable or dll) is using resources. 在Windows上,将数据添加到映像(可执行文件或dll)的最简单方法是使用资源。 Define a custom resource type and add into the image ( UpdateResource function) and later retrieve with LoadResource . 定义一个自定义资源类型,并添加到映像中( UpdateResource函数),然后使用LoadResource检索。

You said "script", so I suppose you have a separate file containing the script (a text file?). 您说的是“脚本”,所以我想您有一个包含脚本的单独文件(文本文件?)。 You could write a simple program that reads the script file and convert it in a compilable form (eg a C source containing the initialization of an array of byte). 您可以编写一个简单的程序来读取脚本文件,并以可编译的形式对其进行转换(例如,包含初始化字节数组的C源代码)。 There are also tools you can use to convert an arbitrary file into a linkable object (.o or .obj). 还有一些工具可用于将任意文件转换为可链接对象(.o或.obj)。 In the past I have used the command "objcopy" from GNU bimutils. 过去,我使用过来自GNU bimutils的命令“ objcopy”。 In particular, on linux: 特别是在linux上:

objcopy -I binary -O elf32-i386 mydata mydata.o

This command creates an object and three public symbols you can use to find the start, the end and the size of your data block: 此命令创建一个对象和三个公共符号,可用于查找数据块的开始,结束和大小:

_binary_mydata_start _binary_mydata_end _binary_mydata_size _binary_mydata_start _binary_mydata_end _binary_mydata_size

Something similar may work also on Windows, provided that you install a Windows version of GNU binutils (eg cygwin). 如果您安装了Windows版本的GNU binutils(例如cygwin),则在Windows上也可以使用类似的方法。

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

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