views:

291

answers:

2

I'm writing a Linux kernel module in which I would like to have some code and associated data in the same section. I declare the data and the functions with the attribute tags, like:

void * foo __attribute__ ((section ("SEC_A"))) = NULL;
void bar(void)  __attribute__ ((section("SEC_A")));

However when I do this, gcc complains with:

error: foo causes a section type conflict

If I do not declare the function with the specific section name, gcc is fine with it. But I want both the function and the variable to be in the same section.

Is there any way to do that with gcc? My gcc version is gcc (Ubuntu 4.3.2-1ubuntu12) 4.3.2

+1  A: 

From the GCC manual:

Some file formats do not support arbitrary sections so the section attribute is not available on all platforms. If you need to map the entire contents of a module to a particular section, consider using the facilities of the linker instead.

IIRC, linux uses a flat memory model, so you don't gain anything by "forcing" things into a single section, anyway, do you?

Hmmm. I suppose you could make an asm function to reserve the space and then do pointer foo to get it's address. Might want to wrap the ugly in a macro...

Another thought would be to split the problem in half; write a small example case of the closest thing you can and still compile, get the asm code, and tinker with it to see what you can get past the downstream stages. If nothing else, you could write something to mungle the asm code for that module, entomb it in you make file, and call it good.

Yet another thought: try putting the variable definitions in a small asm module (e,g, as db's or whatever with the right section declarations) and let the linker handle it.

MarkusQ
Thanks! Yea, I saw that in the manual. The thing is that I don't want to put the entire module into a separate section, just a few functions and global variables. This is for some experimental work, and we will be doing things to the page tables underneath and hence require this layout.
samgrover
The section attribute is certainly available on Linux (2.6, x86).
Martin Carpenter
A: 

I think you cannot put text (function) and data (BSS) objects into the same section because (some) OSes assume immutability of .TEXT section types for process re-use.

Martin Carpenter
So if the data is const, what OP is trying to do may work. Would you agree with that conclusion?
ephemient
No dice, same error - I tried "const char foo __attribute__ ((section ("SEC_A"))) = 'x';".
Martin Carpenter
const does not imply immutability.
sigjuice
const applies to the variable, not to the memory
Mikeage
sigjuice, mikeage: agreed, simply responding to Ephemient's idea. I thought --omagic might help, but I can't seem to make that work either - the linker complains about missing libgcc_s, which is patently false, although this feels promising...
Martin Carpenter