tags:

views:

158

answers:

3

For a rather obscure use case I'd like to have a (large) statically linked Linux executable made up of a small piece of control code and large pieces of static (read-only) data. Is it possible, to save memory, to get the loader to load only the sections for the control code, and then manually load the sections of RO data as they are needed, and unload them again once the processing is done?

Is this possible?

(I suppose data streams (on the filesystem level) could be used to solve this, but they aren't available to me (EXT3) and distribution would be tricky since data streams easily get lost.)

A: 

No, if it's part of the ELF file, it will be mapped. I'm not sure of the particulars of ELF, but if this was a PE file, you could simply tag on your added data to the end of the PE file, beyond the PE structure. Data that isn't part of the PE structure doesn't get mapped to memory in windows. I suspect the same exists in ELF.

Daniel Goldberg
Hmm, I guess I'm going off in a different direction here, but as you probably have guessed I have limited RAM available. Will the mapping of the entire file result in an equivalent amount of RAM being used, or will only the text segments (the code) actually be loaded into RAM (on demand I guess)?
Magnus
+2  A: 

This is (very probably) already taken care of for you.

The real answer of course will be system-dependent, but in general, modern operating systems (and certainly Linux) use demand paging for executables, so no RAM will be actually allocated for sections of the ELF file you don't reference.

Idelic
Yes, that's what I expected, however, I also need to expunge pages from RAM once I'm done. Unfortunately there is no virtual memory on the system I'm working with, so relying on that mechanism to free up RAM isn't available.
Magnus
The executable pages will be automatically be unmapped when more pages are needed. Linux always implements virtual memory of some sort, and you can think of your executable as a sort of read-only swap file.
karunski
+1  A: 

Instead of linking your blobs into the binary, append them to it. They won't be mapped, but you can read or map them as you need them.

Alexey Feldgendler