1

I have a legacy Linux application written for C that relies upon static external files on the filesystem. I'd like to bundle all of them together into a single executable, so that the single-file executable doesn't rely upon anything in the filesystem. Is there a way to do this, without having to make lots of changes to the existing code?

I can link the program statically to avoid any dependencies on dynamic libraries, but the application also relies upon other static resources (other read-only files on the filesystem), which I'd like to embed into the application. I know how to embed an external file into the final executable using objcopy (e.g., as described here or here), but then I need to arrange for the program to use the embedded blob instead of trying to open a file on the filesystem. I've seen some ways to access these ELF sections at runtime (e.g., using linker symbol names or elfdataembed, as described here and here), but they require me to change every place in the program that accesses one of these external files to instead refer to the embedded resource. That sounds tedious and error-prone.

To reduce my workload and reduce bugs, I'd prefer to minimize the amount of changes needed to the application's code. How can I do this, minimizing changes to the application? I'm thinking maybe something that wraps open() to detect attempts to open one of the external files and redirecting them to read from the blob embedded in the executable, for instance, though I'm not sure about some of the details (e.g., how the open() wrapper can create a fake fd; whether I'll need to wrap all of the other filesystem functions as well) or what the pitfalls might be.

How can I achieve this? Or, is there an existing tool I should know about, to avoid re-inventing the wheel?

Community
  • 1
  • 1
D.W.
  • 3,382
  • 7
  • 44
  • 110
  • Linux, I presume? Depending on what problem you are really trying to solve, Linux Containers might fit the bill. Otherwise you are going to have lots of fun wrapping open, stat, fstat, opendir, dup, dup2, fcntl, ... – Nemo Jun 19 '15 at 01:33
  • @Nemo, yes, Linux. Thanks for the pointer. My understanding of Linux containers is super-shallow, so I'll have to look into them. The isolation parts aren't relevant to me, but bundling all relevant files into a single-file filesystem image could be. Appreciate it. – D.W. Jun 19 '15 at 01:52
  • If you're going to wrap something, I'd wrap `fopen`, not `open`. And I'd also investigate one of the user-mode filesystems, like FUSE or GVFS. – Steve Summit Jun 19 '15 at 02:07
  • @Nemo, thanks for the edit. I noticed you removed a few tags -- for my education, can you teach me why [tag:embedded-resource] wasn't a relevant tag? Just curious, so I can learn better for the future. (Is it narrowly scoped to C# or Java or something? I do see other Linux/C-related questions also with that tag, but I don't know what the intended usage is.) – D.W. Jun 19 '15 at 02:17
  • I think my net connection was flaky; I did not see any description at all for `embedded-resource`, but now I see it is relevant (although with only 32 subscribers, probably not too helpful). I added it back. The purpose of tags is to get the attention of "regulars" who subscribe to them. I think `c` and `linux` pretty much cover you, for whatever it's worth... – Nemo Jun 19 '15 at 18:19

0 Answers0