I am trying to understand few linux-related concepts Can anyone help me answer the following questions.
[Question I] I am aware that the compiler gives Virtual Address (VA) for a program's data and code segment. The OS uses these VA's and try to fit them into physical memory. This is what ELF loader (fs/binfmt_elf.c in source code) does in linux. When does the VA for stack and heap gets allocated for a process? I guess its dynamic, but from where does the VA come for those segments? If possible, can anyone point me to source codes.
[Question II] While I know that, physical memory is limited in size. Hence the OS has to swap out pages at times to keep several processes running. When it needs to swap out a page, it pulls the page from memory and writes it into disk. I completely agree that data and code pages can be written into disk (perhaps they get written back in the same area where they came from disk). But can the OS swap out the heap and stack pages as well? If yes, where will it store in disk?
1.) In load_elf_binary
2.) In swap partition / file