How does a program know where the bss segment is located

As far as i have understood x86 has dedicated registers for pointers to code-, data- and stack segment, but not bss- and heap segment. How does the computer remember where these segments are? Especially heap, since bss is directly after data, but heap often is placed a different place in memory.


The heap is usually created by the C runtine which is linked with your code (statically or dynamically). It decides an address in the virtual address, calls the operating system provided system calls to map pages and stores the address in some data structure which is used by malloc (and family of functions) as heap. All this code is either executed before calling main or is statically initialized in the binary.

As for the bss section, As you know it is filled with all zeroes. The binary has information on the size of the .bss section and the base address. The loader maps pages to this virtual address and clears them with zeroes (in an efficient way).

You can see the address of the bss segment and its size by running dumpbin /HEADERS binary.exe if on windows. On Linux you can use objdump . I believe the required flag is -x .

About your question that how can they be moved around if the offsets are hard coded in the instructions -

The binary also has a table called the relocation table which has addresses of all instructions that access these values on a particular section. The loader may decide to place the segment somewhere else (usually happens when you load multiple dlls or shared libraries in Linux). In that case it patches all the instructions looking at the relocation table. It actually changes the offsets in the instruction. This is done by the loader before executing the main .

Ofcourse this has an overhead and can be done only if the relocation information is available. Some binaries may choose to omit the relocation table and in that case the binary fails loading if the section cannot be placed at the specified location.

Hope that clears some of the confusion.


Actually, the simple answer is that the "bss segment" is just a number in the executable that tells the loader how much data at a minimum to reserve for the zero-initialized global data. Nothing more.

After having reserved and set to zero this memory, the run-time start-up of the program (be it compiled from Fortran, C or whatever for the particular platform), now reserves more memory and sets up the heap there and next decides where the stack is to go. Following more platform specific initializiation, control is finally transfered to the program entry point as indicated in the executable file, and control is transfered to there. Only now is the program "live".


As far as i have understood x86 has dedicated registers for pointers to code-, data- and stack segment, but not bss- and heap segment. How does the computer remember where these segments are? Especially heap, since bss is directly after data, but heap often is placed a different place in memory.

You are suffering for overlapping terminology. A segment can refer to memory segment under the segmented memory model (as used in pre-64-bit X86). A segment can also refer to an block of memory with common access attributes created by a linker.

Your question appears to refer to the second usage.

You also appear to suffer from an oversimplified view of memory. First of all, there is no heap segment. A heap is one or more blocks of read/write data.

Second, a linker can create multiple demand zero (bss) segments. A linker can also put the segments in any order in memory

Third, the knowledge of segments is only needed at load time. Once the segments are loaded, they are just memory.

链接地址: http://www.djcxy.com/p/82382.html

上一篇: 解决PermGen问题的各种选项

下一篇: 程序如何知道bss段的位置