views:

77

answers:

1

I have a lot of code in an ELF shared library that is only used during library initialization (it's called from static initializers). If I put this code in its own section (or perhaps it can go in the .init section), which I can do using __attribute__((section(".mysection"))), is there a way to force this section to be paged out after the library has loaded?

This question is related, but the conclusion there was that the kernel will page out unused pages when it's short of memory, so there's no need to do so explicitly. However, I am working in an embedded environment where memory is at a premium and the cost of paging in code from disk (a slow USB flash drive) is high. Therefore I'd rather explicitly flush this code, which I know is never going to be used again, rather than have the kernel maybe decide to flush some other code which might eventually need to be paged back in.

I'm sure I remember reading about a syscall to ask the kernel to page in or out certain regions of memory, though I can't find any reference to this anywhere, so maybe I imagined it. Does such a thing exist?

+1  A: 

Look for documentation on elf overlays. Arrange your code so you have an overlay for initialization, and another for processing. You may also want to look at an overlay for shutdown. Code in overlays should be replaced, when the next overlay is called.

BillThor
Amusingly, this page is now the top Google search result for "elf overlays".
jchl
I can't find much information about ELF overlays. The best I could find was http://sourceware.org/binutils/docs-2.20/ld/Overlay-Description.html, which isn't particularly helpful out of context. Could you elaborate, or provide some links to relevant documentation, in particular relating to the use of overlays with shared libraries? Besides, I'm not sure overlays would work for me, since the initialization code needs access to all the other code in the library.
jchl
At worst could you have a micro overlay for the for the first post-init code?
Douglas Leeder
I found indications that elf support overlays at http://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/gdb/overlay-sample-program.htmlIt has been decades since I had to use overlays. They are mainly a solution to limited memory space. Multiple overlays all map to the same address space, and the load swaps code as needed. New code would be readin over the code you no longer want. In your case you put the init code in a section which would be overlaid with code running after intialization. You really need to initalize early to do this.
BillThor