Quick Program Loading Question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
zmz4
Posts: 4
Joined: Sun Jan 22, 2017 1:21 pm

Quick Program Loading Question

Post 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
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Quick Program Loading Question

Post 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.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Quick Program Loading Question

Post 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.
User avatar
crunch
Member
Member
Posts: 81
Joined: Wed Aug 31, 2016 9:53 pm
Libera.chat IRC: crunch
Location: San Diego, CA

Re: Quick Program Loading Question

Post 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)
zmz4
Posts: 4
Joined: Sun Jan 22, 2017 1:21 pm

Re: Quick Program Loading Question

Post by zmz4 »

Gotcha. That makes perfect sense. Thanks everyone.
zmz4
Posts: 4
Joined: Sun Jan 22, 2017 1:21 pm

Re: Quick Program Loading Question

Post by zmz4 »

Actually, one more question: should each process have its own heap as well?
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Quick Program Loading Question

Post 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))).
Post Reply