11

In assembly language, it's easy to define a section like:

.section foo

How can this be done in C code? I want to put a piece of C code in a special section rather than .text, so I will be able to put that section in a special location in the linker script.

I'm using GCC.

2
  • 1
    I'm pretty sure this is going to be compiler specific -- perhaps you can tell us which one you're using... Commented Aug 18, 2010 at 20:31
  • 2
    look at pragma's for your compiler Commented Aug 18, 2010 at 20:35

1 Answer 1

22

The C standard doesn't say anything about "sections" in the sense that you mean, so you'll need to use extensions specific to your compiler.

With GCC, you will want to use the section attribute:

extern void foobar(void) __attribute__((section("bar")));

There is some limited documentation here, including a warning:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks men/women. extern void foobar(void) __attribute__((section("bar"))); works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.