c code in other section

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.
YeXo

c code in other section

Post by YeXo »

Is it possible with to instruct gcc to put a certain c function not in the section .text or .rodata, but in a completely other section, like .kernelinit or something like that?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:c code in other section

Post by Solar »

Not to my knowledge (after searching the GCC manual "option index" for "text" or "sect"). You can of course write up a linker script to do this for you.

May I ask why?
Every good solution is obvious once you've found it.
paulbarker

Re:c code in other section

Post by paulbarker »

Look thru the GCC manual. Find the bit about function and data attributes. Thats what you want.

I'll save you 5 mins:
http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Function-Attributes.html#Function-Attributes
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:c code in other section

Post by Pype.Clicker »

that's something used often in kernels, including Linux: declare that some data or some functions do belong to a specific section that you'll remove once initialization has been completed. IIrc, linux uses "__init" modifers (that of course expands to some attribute((...)) stufff).

Code: Select all

// coming out of linux  .../init.h

/* These are for everybody (although not all archs will actually
   discard it in modules) */
#define __init          __attribute__ ((__section__ (".init.text")))
#define __initdata      __attribute__ ((__section__ (".init.data")))
#define __exitdata      __attribute__ ((__section__(".exit.data")))
#define __exit_call     __attribute_used__ __attribute__ ((__section__ (".exitca
ll.exit")))
YeXo

Re:c code in other section

Post by YeXo »

Thanks for your help! It is indeed meant to discard data and functies that are only needed during startup.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:c code in other section

Post by Solar »

Hehehe... thanks for sparking a nice idea. I haven't thought about that yet... 8)
Every good solution is obvious once you've found it.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:c code in other section

Post by Pype.Clicker »

iirc, that's _THE_ reason why people on lkml doesn't want to hear about COFF or something else than ELF for their kernel/module binary files.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:c code in other section

Post by Solar »

I don't read the LKML, after reading their FAQ I felt like throwing up. ::)
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:c code in other section

Post by Candy »

Solar wrote: I don't read the LKML, after reading their FAQ I felt like throwing up. ::)
Subscribed once, it's pointless. They send way more email than I've been able to gather in spam by publicly displaying my mail address on two websites (this and slashdot). It also contains loads of crud without point, flamewars at times about senseless specifics and very occasionally a degree of truth.

I agree with them on the ELF point, but not on why you wouldn't use COFF then. IIRC PE could do the trick too.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:c code in other section

Post by Solar »

Plus the GPL/ABI issue, plus the C++ issue, plus the release policy, and at some point I realized it sure as hell isn't the kernel (or the attitude of the people supporting it) why I prefer to run Linux. 8)
Every good solution is obvious once you've found it.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:c code in other section

Post by Pype.Clicker »

while you might have other names in sections with COFF file format, the compiler backends usually do not allow you to use "__attribute__((section))" things. At least, when i last tried (which was with DJGPP, i have to admit), i got the compiler frowning and babling a warning rather than doing what it was instructed.

So compiler will be creating .ctors and .dtors sections as it wants to, but it will not allow you to declare a ".textinit" section ...
kernel64

Re:c code in other section

Post by kernel64 »

Candy wrote:
Solar wrote: I don't read the LKML, after reading their FAQ I felt like throwing up. ::)
Subscribed once, it's pointless. They send way more email than I've been able to gather in spam by publicly displaying my mail address on two websites (this and slashdot). It also contains loads of crud without point, flamewars at times about senseless specifics and very occasionally a degree of truth.

I agree with them on the ELF point, but not on why you wouldn't use COFF then. IIRC PE could do the trick too.
Yes, you can do that with PE. I want PE32+ support inside my kernel, although first I'll be supporting ELF64. Of course, ELF is supported nearly everywhere and by all Unix tools, but PE is not... (Linux could "embrace and extend" PE... :D). And PE asks what Windows subsystem is in use, etc. It's very Windows specific, and is meant to be "portable" in the sense of portable between Windows versions. But as it turns out, they designed a very capable EXE/OBJ format that could be used elsewhere quite easily, as long as you break the specs and use the Windows fields for your own OS.

I once subscribed to the LKML, too. What an experience! (Almost as good as the flames on OpenBSD lists). I unsubscribed a few hours later. ;D
Rob

Re:c code in other section

Post by Rob »

If I remember it was "portable" in the sense of platform / architecture
portability. There was a powerpc version of Windows....

You can also extend the PE format without breaking it...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:c code in other section

Post by Solar »

Rob wrote: There was a powerpc version of Windows....
AFAIK the "other" Windows was NT for Alpha, not PowerPC. I don't know if there ever was a "production" release of that, though.
Every good solution is obvious once you've found it.
Rob

Re:c code in other section

Post by Rob »

You are totally correct, it was the alpha. My bad. It definitely
has been used in the real world. I'm very sure of that.
Post Reply