Miscellaneous


Generating code in a particular segment

The gcc generate assembly code for functions in .text and global varibales in either .data or .bss segments. There is predefined rules of which segment the gcc will generate the code of some kind (function, global variables etc). Programmers can override the behavior. The segment can be controller.

Lets see an example.
Here is a c code
void __attribute__ ((section ("MySegment"))) fun()
{
}
int main()
{
    fun();
    return 0;
}

compile this file using gcc and genrate a.out file. Now inspect the a.out file using objdump command as

objdump -h a.out

This command will show various segments inside the a.out. It will show one segment called MySegment.
The __attribute__ can be applied on global variables.

up