There are several questions regarding some aspects of this problem, but does not seem to answer all of this. The whole problem can be summarized as follows:
- You have an already compiled executable (obviously awaiting use of this technique).
- You want to add binary data of arbitrary size to it (not necessarily by itself, which would be another unpleasant problem to solve).
- You want an already compiled executable to be able to access this added binary data.
My specific use case would be an interpreter in which I would like the user to be able to create one executable file from the interpreter binary code and the code that it supplies (binary translator is an executable file that needs to be fixed with the user entered code in the form of binary data) .
A similar case is self-extracting archives, where a program (archiving utility such as zip) is able to build such an executable file that contains a pre-built decompressor (an already compiled executable file) and data provided by the user (archive contents). Obviously, the compiler or linker is not involved in this process (thanks, Mathias for the note and specifying 7-zip ).
Using existing questions, a specific solution path is shown in the following examples:
Adding data to exe - This refers to the aspect of adding arbitrary data to arbitrary exes, not covering how to actually access it (basically a simple append usually works, also true in Unix ELF format).
Search for the current path of the executable without / proc / self / exe - In combination with the foregoing, this will allow you to get the file name for opening exe to access additional data. There are many more such questions, however, they do not focus primarily on the problem of obtaining a path suitable for actually getting a binary file open as a file (for which purpose it would be (easier) to fulfill (really) don't even need a path, only binary is open for reading).
Other, possibly more elegant, ways to solve this problem may arise than filling out a binary file and opening the file to read it. For example, an executable file can be executed, so that it becomes quite simple to fix it later with an arbitrary data size, so is it βinsideβ in some correct data segment? (I could not find anything on this, for data of a fixed size this should be trivial, although if the executable has some hash)
Can this be done quite well with a minimal deviation from standard C? More or less cross-platform? (At least in terms of service). Please note that it would be preferable that the program performing the addition of binary data does not rely on the compiler tools for this (which the user may have), but solutions requiring their use may also be useful.
Pay attention to the already compiled executable criterion (the first item in the list above), which requires a completely different approach than the solutions described in issues such as C / C ++ with GCC: statically add resource files to the executable file / library or SDL embed the image inside the program executable file , in which the implementation of compilation time is requested.
Additional notes:
The problems with the obvious approach outlined above and suggested in some comments that simply add to the binary and use them are as follows:
- Opening the binary executable file of the program does not seem trivial (opening the executable file for reading is, but not looking for a way to submit the file to an open call, at least not based on cross-platform).
- The method of obtaining the path can provide an attack surface that probably would not exist otherwise. This means that a potential attacker can trick the program into seeing various binary data (provided to them), which, in fact, have an executable file, exposing any vulnerability that may be in the data parser.