Page 1 of 1

heap and stack collision

Posted: Fri Apr 04, 2008 2:59 pm
by kubeos
Hi,

I just compiled a program for my OS using newlib and a simple libgloss.. but when I try to run my program on my os I get the error:

Heap and stack collision

Now I'm excited that it actually printed something in my os, but what is the problem? Is this something with my linker script? Or maybe my sbrk function? Here's my linker script

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
  }
  end = .;
	_end = .;
}
As you can see I don't provide for a stack but my kernel setups up a stack for every user mode program.. so any ideas?

Thanks in advance.

Posted: Fri Apr 04, 2008 3:00 pm
by JamesM
Where does the stack get assigned by the kernel? It must be above the "sbrk" breakpoint.

ahh... i see the problem

Posted: Fri Apr 04, 2008 3:06 pm
by kubeos
Here's my sbrk.. and I see what's going on now..

Code: Select all

int sbrk(int incr){
      extern char end;		/* Defined by the linker */
	int stack_ptr;
      static char *heap_end;
      char *prev_heap_end;
     
      if (heap_end == 0) {
        heap_end = &end;
      }
      prev_heap_end = heap_end;
      if (heap_end + incr > stack_ptr)
        {
          _write (1, "Heap and stack collision\n", 25);
          abort ();
        }

      heap_end += incr;
      return (caddr_t) prev_heap_end;
}
Guess I'll need to work on this..