How to get MSVC to place uninitialized data in .bss?

I create a DLL using a custom build system (outside of Visual Studio), and I cannot get uninitialized data to display in the .bss section; the compiler inserts it into .data . This inflates the final binary size as it is filled with gigantic arrays of zeros.

For example (small arrays of 1 KB in the example, but the actual buffers are much larger):

 int uninitialized[1024]; int initialized[1024] = { 123 }; 

The compiler builds as follows:

 PUBLIC _initialized _DATA SEGMENT COMM _uninitialized:DWORD:0400H _initialized DD 07bH ORG $+4092 _DATA ENDS 

The end ends in the object file as follows:

 SECTION HEADER #3 .data name 0 physical address 0 virtual address 1000 size of raw data 147 file pointer to raw data (00000147 to 00001146) 0 file pointer to relocation table 0 file pointer to line numbers 0 number of relocations 0 number of line numbers C0400040 flags Initialized Data 8 byte align Read Write 

(the .bss section is missing.)

Current compilation flags:

 cl -nologo -c -FAsc -Faobjs\ -W4 -WX -X -J -EHs-c- -GR- -Gy -GS- -O1 -Os -Foobjs\file.o file.cpp 

I looked through the list of options http://msdn.microsoft.com/en-us/library/fwkeyyhe(v=vs.71).aspx , but I did not notice anything obvious.

I am using the compiler from Visual Studio 2008 Service Pack 1 (Microsoft (R) 32-bit optimizer for the C / C ++ compiler version 15.00.30729.01 for 80x86).

+7
source share
3 answers

You want to use __declspec(allocate()) , which you can read here: http://msdn.microsoft.com/en-us/library/5bkb2w6t(v=vs.80).aspx

+2
source

Note that the "size of the raw data" is only 0x1000 or 4kB - exactly the size of only your initialized array. VirtualSize of your .data section will be larger than the size of the actual data stored in the binary image, and your uninitialized array will take up free space. Using the bss_seg pragma will force the linker to put your uninitialized data in a separate section.

+2
source

Yo might try using bss_seg pragma if you are not interested in portability.

+1
source

All Articles