Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading/unloading ELF sections on demand?

Tags:

linux

elf

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.)

like image 598
Magnus Avatar asked Nov 25 '25 01:11

Magnus


2 Answers

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.

like image 146
Idelic Avatar answered Nov 27 '25 15:11

Idelic


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.

like image 33
Alexey Feldgendler Avatar answered Nov 27 '25 15:11

Alexey Feldgendler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!