I was wondering if it is possible to select in which ELF section a piece of code or global data will be stored in C. I know you can do it quite easily in assembly. My intent is to put all of the code that has to do with initializing my kernel in a separate ELF section, then free it once the system is booted, because more than half of the code is for initialization (my kernel is very small), and I want to be conservative about memory. I can get a couple of pages back based on the object size, and it's only about half written already.
It seems like this wouldn't be conventionally possible, because of the fact that not everyone uses ELF binaries, but is there some sort of linker hack that would let this work?
Alternatively, because all of the init code is compiled into one object, then linked with objects from other parts of the kernel, which I'm *assuming* means they are contiguously grouped, perhaps there is some way of detecting the upper and lower limits of that object in the section?
Thanks!
BTW, this is using GNU CC/Binutils and a higher half kernel design.
Choosing ELF sections in C
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Choosing ELF sections in C
Code: Select all
void * foo __attribute__ ((section ("SEC_A")));
Code: Select all
.start :
{
startc.o(*)
}
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Choosing ELF sections in C
Awesome, thanks! I'm just applying the attribute tag to all of the functions in the init system individually - some initialization data is still needed, and it might be useful to leave stuff fine-grained so I can let it free code elsewhere as well.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Choosing ELF sections in C
Well, now that I've actually started to try and implement the freeing algorithm, I've run into a problem. I don't know a good way to find the upper and lower limits of the sections I defined (.ttext and .tdata) within the C program. Do I have to make a symbol at each end of the segment somehow, or is there some sort of predefined symbol that I can access?
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: Choosing ELF sections in C
IIRC the symbols are generated for you. They're like SECTION_x_END or something like that. (Or is that in NASM, hmm...)
Re: Choosing ELF sections in C
Are you using a linker script? In that case then you could do:
then you could use
Otherwise you might have to parse the elf header itself. That I don't really know much about.
Code: Select all
.start :
{
START_OF_START = .;
*(.start)
. = ALIGN( 4096 );
END_OF_START = .;
}
Code: Select all
extern unsigned int START_OF_START;
extern unsigned int END_OF_START;
// then use the address of the symbols to find their values ie.
start = &START_OF_START;
end = &END_OF_START;
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Choosing ELF sections in C
Now it works great, thanks!