Generating code in a particular segment

The gcc generates assembly code for functions in .text and global variables in either .data or .bss segments. There are 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 controlled.

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

compile this file using gcc and generate 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.


Want to learn (C/C++ internals) directly from the author using video call? Learn more

Do you collaborate using whiteboard? Please try Lekh Board - An Intelligent Collaborate Whiteboard App