heap and stack collision

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
kubeos
Member
Member
Posts: 138
Joined: Tue Jan 30, 2007 2:31 pm
Location: Kamloops BC, CANADA
Contact:

heap and stack collision

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Where does the stack get assigned by the kernel? It must be above the "sbrk" breakpoint.
kubeos
Member
Member
Posts: 138
Joined: Tue Jan 30, 2007 2:31 pm
Location: Kamloops BC, CANADA
Contact:

ahh... i see the problem

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