Page 1 of 1

Quick Program Loading Question

Posted: Fri Jan 27, 2017 6:56 pm
by zmz4
I'm working on loading ELF executables into memory, but I'm unclear as to how the user program stack works. In the ELF there is a .bss section which I thought would indicate the size of the stack needed, effectively. Then in crt0.S I set the stack pointer to the end of the .bss section. When looking at the ELF sections, however, I'm seeing that the .bss section requires 0x3c bytes of memory in RAM, which is a tiny stack and it quickly overflows.

Am I thinking about this wrong? Do I need to provide an external stack for the process instead?

Thanks,
Zack

Re: Quick Program Loading Question

Posted: Fri Jan 27, 2017 7:01 pm
by BrightLight
I don't know much about ELF, but bss should contain all uninitialized data. For example, in a program that contains:

Code: Select all

int a, b;
Space for two int's would be reserved in the bss section. I think it's unlikely that the stack belongs in bss, and your loader should allocate a stack for the program, and expand it when it overflows.

Re: Quick Program Loading Question

Posted: Fri Jan 27, 2017 7:09 pm
by alexfru
The ELF format does not specify the stack size or location. It's up to the OS to set it up to something reasonable. The OS may provide a way of changing it. But the stack should not normally be part of any section/segment.

Re: Quick Program Loading Question

Posted: Fri Jan 27, 2017 7:10 pm
by crunch
Bss is NOT the stack. Bss would be, for instance, global variables, like

Code: Select all

int array[0x1000];
Memset the . Bss section with a value of 0. You'll need to set up a separate stack (user accessible if you're using paging)

Re: Quick Program Loading Question

Posted: Fri Jan 27, 2017 7:13 pm
by zmz4
Gotcha. That makes perfect sense. Thanks everyone.

Re: Quick Program Loading Question

Posted: Fri Jan 27, 2017 7:49 pm
by zmz4
Actually, one more question: should each process have its own heap as well?

Re: Quick Program Loading Question

Posted: Fri Jan 27, 2017 8:10 pm
by alexfru
If you don't care about reliability, you can have a share heap (provided it's accessible in all your processes (typically, when there's a shared address space, however, early versions of Windows showed how great it is (not))).