Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define a program section in C code (GCC)

Tags:

gcc

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.

like image 509
richard Avatar asked Aug 18 '10 20:08

richard


People also ask

What is definition section in C program?

Definition section − Here, variables are defined and initialised. Global declaration section − In this section, global variables are defined which can be used throughout the program. Function prototype declaration section − This section gives information like return type, parameters, names used inside the function.

What does GCC do in C?

gcc performs the compilation step to build a program, and then it calls other programs to assemble the program and to link the program's component parts into an executable program that you can run.

What is Section in programming?

When referring to HTML, a section is an area in the web page containing specific programming, text, or images that are separated from other areas using certain HTML tags.

Can G ++ compile C?

gcc is used to compile C program. g++ can compile any . c or . cpp files but they will be treated as C++ files only.


1 Answers

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.

like image 109
Stephen Canon Avatar answered Oct 17 '22 03:10

Stephen Canon