Remove bss section for modules
Remove bss section for modules
Hi,
For a while I've had kernel modules. These are just normal elf object files that comes with my initrd which is basically just a tar archive that grub loads into memory on boot. Because the initrd is already in memory I thought that when I load my modules I really don't need to copy them anywhere because they are already in memory so I just relocate them to where they actually are. I know this is not optimal but it worked. However I noticed I did a big mistake and that was to forget that they might contain .bss data and I forgot that I needed to reserve space for that.
I thought that can easily be fixed without having to move them and that would be to put everything in the bss as part of the program code.
My question is: Which is the preferable way to do this? I've figured I got two options but there might be something simpler like a compiler flag or something. One is to add the __attribute__section((".text"))) to each global variable and the other would be to create a linker script similar to the one I have for the kernel that puts the .bss section inside .text. None of these are particularly appealing. Ideas anyone?
For a while I've had kernel modules. These are just normal elf object files that comes with my initrd which is basically just a tar archive that grub loads into memory on boot. Because the initrd is already in memory I thought that when I load my modules I really don't need to copy them anywhere because they are already in memory so I just relocate them to where they actually are. I know this is not optimal but it worked. However I noticed I did a big mistake and that was to forget that they might contain .bss data and I forgot that I needed to reserve space for that.
I thought that can easily be fixed without having to move them and that would be to put everything in the bss as part of the program code.
My question is: Which is the preferable way to do this? I've figured I got two options but there might be something simpler like a compiler flag or something. One is to add the __attribute__section((".text"))) to each global variable and the other would be to create a linker script similar to the one I have for the kernel that puts the .bss section inside .text. None of these are particularly appealing. Ideas anyone?
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
http://github.com/Jezze/fudge/
-
- Member
- Posts: 141
- Joined: Thu Jun 17, 2010 2:36 am
Re: Remove bss section for modules
1. Create your own bootloader that loads your modules as ELF files (and reserves the memory).
2. Hack GRUB to do the same thing.
3. Put your .bss section into the module's .data section (pretty much the same thing as putting it in the .text though).
4. Don't allow your modules to statically allocate memory, and instead rely on a kernel call to get memory.
5. Convert your modules into flat binaries after you link everything together into an ELF file.
6. Parse the ELF headers once inside your kernel and make sure that you make room for any modules .bss section yourself.
7. Write a script to pad your ELF modules with 0's to account for the length of the .bss section.
2. Hack GRUB to do the same thing.
3. Put your .bss section into the module's .data section (pretty much the same thing as putting it in the .text though).
4. Don't allow your modules to statically allocate memory, and instead rely on a kernel call to get memory.
5. Convert your modules into flat binaries after you link everything together into an ELF file.
6. Parse the ELF headers once inside your kernel and make sure that you make room for any modules .bss section yourself.
7. Write a script to pad your ELF modules with 0's to account for the length of the .bss section.
Re: Remove bss section for modules
yupJezze wrote:... but there might be something simpler like a compiler flag or something.
GCC manual wrote:-fno-zero-initialized-in-bss
If the target supports a BSS section, GCC by default puts variables that are initialized to zero into BSS. This can save space in the resulting code.
This option turns off this behavior because some programs explicitly rely on variables going to the data section. E.g., so that the resulting executable can find the beginning of that section and/or make assumptions based on that.
The default is -fzero-initialized-in-bss.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Remove bss section for modules
That description does not guarantee that uninitialized variables end up in .data as well.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Remove bss section for modules
Just use your linker script to put the sections which normally go in BSS (.bss*, COMMON*, etc) into data and you'll be find.
Otherwise, just use paging in order to remap the binaries so they can run in place but get an actual BSS section.
Finally, as a safety check, you may wish to have your loader error out if you find a NOBITS segment to be loaded of nonzero size
Otherwise, just use paging in order to remap the binaries so they can run in place but get an actual BSS section.
Finally, as a safety check, you may wish to have your loader error out if you find a NOBITS segment to be loaded of nonzero size
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Remove bss section for modules
Even better is to check if section size - stored size > 0, as ELF allows merged .bss and .data sections.Owen wrote:Finally, as a safety check, you may wish to have your loader error out if you find a NOBITS segment to be loaded of nonzero size
Re: Remove bss section for modules
Thanks for all the replies! I'll go with adding a linker script and put it in the .data section (and not the .text section as I said previously) until I make it work properly (by demand paging).
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
http://github.com/Jezze/fudge/
Re: Remove bss section for modules
Erm... aren't all static variables initialized by definition? At least that's how it is with C/C++...Combuster wrote:That description does not guarantee that uninitialized variables end up in .data as well.
Every good solution is obvious once you've found it.
-
- Member
- Posts: 141
- Joined: Thu Jun 17, 2010 2:36 am
Re: Remove bss section for modules
Solar wrote:Erm... aren't all static variables initialized by definition? At least that's how it is with C/C++...Combuster wrote:That description does not guarantee that uninitialized variables end up in .data as well.
Code: Select all
#include <stdio.h>
static int foo;
int main()
{
int bar = foo;
printf("bar %i\n", bar);
return 0;
}
-
- Member
- Posts: 141
- Joined: Thu Jun 17, 2010 2:36 am
Re: Remove bss section for modules
Huh, I never knew the = 0 was implicit. I always thought that anything without an explicit initialization was undefined by the standard. :shrug:
Re: Remove bss section for modules
In ISO C/C++ there is no such thing as uninitialized static data. Automatic, register and dynamically allocated data may be uninitialized, but static data that is not initialized explicitly, must be set to zero before main() is called.Rudster816 wrote:Huh, I never knew the = 0 was implicit. I always thought that anything without an explicit initialization was undefined by the standard.