Initialized data segment in C binary files running under Windows

I have been trying for a long time to get an idea of ​​how the program memory is processed under the OS (I use Windows, but I think it will be the same or very close to Linux).

So far, I know (mainly thanks to stackoverflow users) that local variables are stored on the stack. Now I also finally understand why. So, OK.

But I still miss how global variables are stored and processed. And I want to know this in assembly language. I have my idea of ​​how this can be handled, but I can’t be sure, because many things that I still don’t know about can make my idea impossible.

So my idea is that global variables are at the end of the program code. After the last instruction. Why do I think this could be so? Become what you, you do not need to waste extra memory and CPU time. Please note that the variables and their default values ​​will be copied to RAM by OS at execution.

Why can I do this? Becouse, if I'm not mistaken, on modern x86 operating systems, each program gets its own address space, starting from 0. Thus, the compiled user knows the address of the global variable very easily. because he knows the length of the program, so he can calculate his position in his address space.

Why do I think this may be all wrong? Becouse I already thought why the local variables created on the stack look the same. And when you will have some routines in ELF format, you have precompiled routines with only unresolved addresses for variables.

Also, in some articles, I read that allocating memory using malloc extends the heap. And because I am a bunch like space after the program code, there will be an error because it will grow on the stack. Otherwise, the stack should be located at the end of the process address space, but max will be a terrible waste of memory.

I tried to describe my point of view as much as I could, so I hope you will understand where I made some mistakes and help me fill in the knowledge that I lack. Thank.

+3
2

, . , #, C.

, :

. , . (, ?)

, , . Windows ( Portable Executable) . , , , , ( - , )

, x86 , 0

, . , , 0 null. , , , , , , . (, , , , , )

, malloc

, , . Windows DLL , / . , " ", , .


. , . . !


EDIT:

PE exe ,

. PE , , . , , .

Os ""

. , .

,

( , , , - ). , , . , PE . , ; , PE , , (MingGW, , , ).

-, .exe ?

, , , . Stack Overflows Windows, 1 ; * nix 8 .

?

, . ; , , . , , , - , . , ; , . 1 , , . 1 , .

, -, - , , / , PE-, .exe.

PE: http://www.microsoft.com/whdc/system/platform/firmware/pecoff.mspx


, , , . , , - - . , "" Windows , " ". Windows ( ) - , , . . , 0x00005556, 0xFFFF890. , .

+5

:

  • : . 0, - . 0 , .
  • : , .
  • / : , , /.
  • BSS ( ): . , , .
  • : bss, Unix brk()/sbrk().
  • : , , .

( + ), ( + bss + ) .

, , (GOT, PLT,...), ,...

0

Source: https://habr.com/ru/post/1761809/


All Articles