Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify output sections in C files compiled using GCC?

Tags:

c

gcc

ld

In assembly language I use .section directive to tell the assembler what section to output to e.g

.section init

Is there a way to do the same in C files. I want the code for some files to go into different section so I can load it to different memory address. I know I can create a script for ld and specify sections there but I dont want to do that. Is there some compiler switch or .section directive kind of thing for C files that will do this?

like image 676
binW Avatar asked Jan 21 '11 07:01

binW


People also ask

What is GCC output?

The default executable output of gcc is "a.out", which can be run by typing ./a.out. It is also possible to specify a name for the executable file at the command line by using the syntax -o outputfile , as shown in the following example: gcc filename -o outputfile. Again, you can run your program with "./outputfile".

Which option should be used with GCC to compile source files to the C99 standard?

The option -fno-gnu89-inline explicitly tells GCC to use the C99 semantics for inline when in C99 or gnu99 mode (i.e., it specifies the default behavior).


1 Answers

There is:

__attribute__((section("section_name")))

So, for example:

void foo() __attribute__((section(".text_foo")));

....

void foo() {}

Would place foo in .text_foo

See here for more information.

like image 82
Matthew Iselin Avatar answered Oct 06 '22 21:10

Matthew Iselin