2
votes

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

1
Specification of your question could help increasing the change of a good answer.moffeltje
Specification as in you want some more details about my question? or you want me to reduce the details i have given?Dhruvify
Second, be more specificmoffeltje
I have edited my question and reduced some details. Does it looks clean now? thanks for your feedback.Dhruvify
I took a fast look to the tutorial link, it seems low level stuff, good to learn and know about but not really used on every day C programming, if you are a C beginner may be better you look for some easier tutorial first.Alex

1 Answers

2
votes

The _sbrk function in your question is trying to increase the extern symbol _end allowing the application to malloc more memory. (It doesn't directly increment _end but return the incremented value, heap_end declared as static preserve the value through multiple _sbrk calls)

(Personally I've some doubt this _sbrk implementation have ever been tested, _end declared as char not pointer (* is missing) is suspect and heap_end assumed to be initialized to 0)

About main and kernel_main, almost every C code need an entry point, for common libc application is main (is a libc convention), but as the quote say in this case a bootloader is used and it expect (looks for) a kernel_main symbol to jump into (a bootloader usually doesn't use the libc). The real entry point for (at least gcc) compilers is _start but libc hide it and use it to prepare the environment to launch the application, may be this help you understand how it work.