天天看点

读书笔记,关于pe file的section及pragma命令

The following table shows some of the more common section names and explains each section's purpose.

Section Name Purpose
.bss Uninitialized data
.CRT Read-only C run-time data
.data Initialized data
.debug Debugging information
.didata Delay imported names table
.edata Exported names table
.idata Imported names table
.rdata Read-only run-time data
.reloc Relocation table information
.rsrc Resources
.text .exe's or DLL's code
.tls Thread-local storage
.xdata Exception handling table

In addition to the standard sections created by the compiler and the linker, you can create your own sections when you compile using the following directive:

#pragma data_seg("sectionname")
      

So, for example, I can create a section called "Shared" that contains a single LONG value, as follows:

#pragma data_seg("Shared")
LONG g_lInstanceCount = 0;
#pragma data_seg()      

When the compiler compiles this code, it creates a new section called Shared and places all the initialized data variables that it sees after the pragma in this new section. In the example above, the variable is placed in the Shared section. Following the variable, the #pragma dataseg() line tells the compiler to stop putting initialized variables in the Shared section and to start putting them back in the default data section. It is extremely important to remember that the compiler will store only initialized variables in the new section. For example, if I had removed the initialization from the previous code fragment (as shown in the following code), the compiler would have put this variable in a section other than the Shared section:

#pragma data_seg("Shared")
LONG g_lInstanceCount;
#pragma data_seg()
      
The Microsoft Visual C++ 6.0 compiler offers an allocate declaration specifier, however, that does allow you to place uninitialized data in any section you desire. Take a look at the following code:



           
***************************************************************************      
// Create Shared section & have compiler place initialized data in it.
#pragma data_seg("Shared")

// Initialized, in Shared section
int a = 0; 

// Uninitialized, not in Shared section
int b; 

// Have compiler stop placing initialized data in Shared section.
#pragma data_seg()

// Initialized, in Shared section
_ _declspec(allocate("Shared")) int c = 0; 

// Uninitialized, in Shared section 
_ _declspec(allocate("Shared")) int d; 

// Initialized, not in Shared section
int e = 0; 

// Uninitialized, not in Shared section
int f;        
************************************************************************************************************

--摘自<programming application for Microsoft Windows>

#pragma directives -- 摘自msdn library

Each implementation of C and C++ supports some features unique to its host machine or operating system. Some programs, for instance, need to exercise precise control over the memory areas where data is placed or to control the way certain functions receive parameters. The

#pragma

directives offer a way for each compiler to offer machine- and operating system-specific features while retaining overall compatibility with the C and C++ languages. Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler.

Pragmas can be used in conditional statements, to provide new preprocessor functionality, or to provide implementation-defined information to the compiler. The Microsoft C and C++ compilers recognize the following pragmas:

alloc_text auto_inline bss_seg check_stack
code_seg comment component conform1
const_seg data_seg deprecated function
hdrstop include_alias init_seg1 inline_depth
inline_recursion intrinsic managed message
once optimize pack pointers_to_members1
pop_macro push_macro runtime_checks section
setlocale unmanaged vtordisp1 warning

1. Supported only by the C++ compiler.

 #pragma                 token-string      

The token-string is a series of characters that gives a specific compiler instruction and arguments, if any. The number sign (

#

) must be the first non-white-space character on the line containing the pragma; white-space characters can separate the number sign and the word

pragma

. Following

#pragma

, write any text that the translator can parse as preprocessing tokens. The argument to

#pragma

is subject to macro expansion.

If the compiler finds a pragma it does not recognize, it issues a warning, but compilation continues.

Some pragmas provide the same functionality as compiler options. When a pragma is encountered in source code, it overrides the behavior specified by the compiler option. For example, if you specified tabindex="0" keywords="_core_.2f.Zp">/Zp8, you can override this compiler setting for specific portions of the code with pack:

cl /Zp8 ...

<file> - packing is 8
// ...
#pragma pack(push, 1) - packing is now 1
// ...
#pragma pack(pop) - packing is 8
</file>