binW
binW

Reputation: 13672

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

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?

Upvotes: 13

Views: 17673

Answers (1)

Matthew Iselin
Matthew Iselin

Reputation: 10660

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.

Upvotes: 18

Related Questions