What is the use of “#pragma section <XYZ>” in C?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 01:39:44
TOC

The #pragma directive is an implementation specific directive it is a standard way to provide additional information to the compiler. This directive has the following form:

#pragma name

If the preprocessor recognizes the specified "name", it performs whatever action they stand for, or passes information on to the compiler. If "name" is not supported by the c implementation it's ignored.

For example gcc compiler accept the list of pragmas listed here.

For the #pragma section, the documentation of gcc said:

section ("section-name") Normally, the compiler places the code it generates in the text section. Sometimes, however, you need additional sections, or you need certain particular functions to appear in special sections. The section attribute specifies that a function lives in a particular section. For example, the declaration:

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

puts the function foobar in the bar section.

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.

More on that here.

Section creates a section in an .obj file.
Refer to MSDN for more details.

Code and data are generated in sections in an object file, combined by the linker into an executable file, and ultimately located in target memory at specific locations. Default sections are predefined and have certain attributes. The section pragmas may be used to change the default attributes, to define new sections, and to control the assignment of code and variables to particular sections and, along with the linker command file, their locations.

#pragma section defines a section class, and optionally, one or two sections in the class. A section class controls the addressing and accessibility of variables and code placed in an instance of the class.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!