Page 1 of 1

Choosing ELF sections in C

Posted: Sat May 02, 2009 2:27 pm
by NickJohnson
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.

Re: Choosing ELF sections in C

Posted: Sat May 02, 2009 2:45 pm
by frank

Code: Select all

void * foo __attribute__ ((section ("SEC_A")));
Should put a function into a certain section. Also if you are using your own linker script then you could place an object file into a section. Ex:

Code: Select all

.start : 
        {
                startc.o(*)
        }
places everything from the startc.o object file into the start section.

Re: Choosing ELF sections in C

Posted: Sat May 02, 2009 3:28 pm
by NickJohnson
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.

Re: Choosing ELF sections in C

Posted: Sun May 03, 2009 10:03 am
by NickJohnson
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?

Re: Choosing ELF sections in C

Posted: Sun May 03, 2009 10:06 am
by Troy Martin
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

Posted: Sun May 03, 2009 10:25 am
by frank
Are you using a linker script? In that case then you could do:

Code: Select all

.start :
        {
                START_OF_START = .;
                *(.start)

                . = ALIGN( 4096 );
                END_OF_START = .;
        }
then you could use

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;
Otherwise you might have to parse the elf header itself. That I don't really know much about.

Re: Choosing ELF sections in C

Posted: Sun May 03, 2009 10:56 am
by NickJohnson
Now it works great, thanks!