I am currently working with raspberry pie and following a well written chapter 2 tutorial on Chap 2 of valvers.com. I managed to understand everything but I am little stuck on the last section titled "C-Library stubs". In that I am facing some doubts which i mentioned below:
#include <sys/stat.h>
/* A helper function written in assembler to aid us in allocating memory */
extern caddr_t _get_stack_pointer(void);
/* Increase program data space. As malloc and related functions depend on this,
it is useful to have a working implementation. The following suffices for a
standalone system; it exploits the symbol _end automatically defined by the
GNU linker. */
caddr_t _sbrk( int incr )
{
extern char _end;
static char* heap_end;
char* prev_heap_end;
if( heap_end == 0 )
heap_end = &_end;
prev_heap_end = heap_end;
if( ( heap_end + incr) > _get_stack_pointer() )
{
while(1)
{
/* TRAP HERE! */
}
}
heap_end += incr;
return (caddr_t)prev_heap_end;
}
so far i have only understood that this is the implementation of _sbrk function.
1). I am not able to understand what exactly this code does in reference to the tutorial given in link?
2). The other thing that i am not able to understand is common in armc-008c and 009c i.e. why and how we are changing from main function to kernel_main function? the only text available for this mentions that
NOTE: We've now changed from main to kernel_main, and there's a reason for this - the bootloader is actually expecting a slightly different entry definition compared to the standard C main function. So as we're setting up our own C-Runtime anyway, we can define the correct entry format
I am a beginner in C and these may sound noobish questions but I have did lots of research and spent 3 to 4 days on same tutorial before asking this question here. I am still trying to understand logic behind these programs. Can you guys shed some light over this issue. It will be much more than just help.
thanks in advance