How does the program know where the bss segment is

As I understand it, x86 has dedicated registers for pointers to the code, data and stack segment, but not the bss- and heap segment. How does the computer remember where these segments are? Especially the heap, since bss is right after the data, but the heap is often placed elsewhere in memory.

+3


source to share


3 answers


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

As for the bss section, As you know, it is filled with all zeros. The binary file has information about the size of the .bss section and the base address. The loader maps the pages to this virtual address and clears them with zeros (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 on how to move them if offsets are hardcoded in instructions -



The binary also has a table called a move table that has the addresses of all instructions that access these values ​​in a particular section. The loader might decide to place the segment somewhere else (usually when loading multiple DLLs or shared libraries on Linux). In this case, it corrects all instructions looking at the move table. This actually changes the offsets in the instruction. This is done by the loader before executing main

.

Of course, this has an overhead and can only be done if movement information is available. Some binaries may drop the move table, in which case the binary will fail to boot if the partition cannot be placed in the specified location.

Hope that clears up some of the confusion.

+3


source


In fact, the simple answer is that the "bss segment" is simply a number in the executable that tells the loader how much data to reserve for zero initialized global data. Nothing more.



After you have reserved and zeroed that memory, running the program (be it compiled from Fortran, C, or any other platform-specific) now reserves more memory and sets the heap there and then decides where the stack should go. Following more platform initialization, control is finally transferred to the program entry point as specified in the executable, and control is transferred there. Only now the program is "live".

0


source


As I understand it, x86 has dedicated registers for pointers to the code, data and stack segment, but not the bss- and heap segment. How does the computer remember where these segments are? Especially the heap, since bss is right after the data, but the heap is often placed elsewhere in memory.

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

Your question seems to be about the second use.

Also you suffer from simplified memory representation. First of all, there is no heap segment. A heap is one or more read / write data blocks.

Second, the linker can create multiple zero demand buckets (bss). The linker can also place segments in any order in memory

Third, knowledge of the segments is only necessary at boot time. Once the segments are loaded, it's just memory.

0


source







All Articles