Choosing ELF sections in C

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Choosing ELF sections in C

Post 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.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Choosing ELF sections in C

Post 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.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Choosing ELF sections in C

Post 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.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Choosing ELF sections in C

Post 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?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Choosing ELF sections in C

Post 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...)
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Choosing ELF sections in C

Post 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.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Choosing ELF sections in C

Post by NickJohnson »

Now it works great, thanks!
Post Reply