Can I just turn on the C libraries when programming the operating system (since they are made in C)

I am trying to create an operating system for the Raspberry Pi (nothing special, just for fun), and although I could write all this in an assembly, it would be much more difficult than writing in C. I wonder (and why not, if I can’t), I could just include the C libraries (files) in the Operating System so that I don’t have to rewrite them. Could this work because the libraries themselves are written in C?

+4
source share
1 answer

No, you need to transfer the C library to your operating system, because the library has stubs that connect to the specifics of the operating system. The C standard means that some headers are present offline, which is always available to you. But library functions, such as printf, must be implemented independently or ported by filling in stubs. Take a look at newlib to see what kind of work you should do. At the very least, this requires a working kernel with the syscall interface (for reading, writing, etc.). This will depend on the features available in your operating system (for example, the file system.) Taken from the FAQ:

  1. What steps must be taken to port newlib to the new platform?

  .

     
  •   
  • newlib/libc/machine

         

    setjmp/longjmp. , setjmp/longjmp   . libc/machine/fr30 /    .

      
  • newlib/libc/include/machine/ieeefp.h

         

    ieee . , .   , endianness , , ,    .    .

      
  • newlib/libc/include/machine/setjmp.h

         

    setjmp, setjmp/longjmp.      setjmp buffer. . .

      
  • newlib/libc/include/sys/config.h

         

    . , . , ,    .

      
  • configure.host

         

    , newlib .       machine_dir. newlib   . Sys_dir , .    sys_dir ,    . Syscall_dir - ,    syscall_dir = syscalls.    newlib/libc/include/reent.h .

      
  • libgloss

         

    bsp . , newlib .    ( ). .   mn10300 fr30 . configure.in   rebate configure, .   get libnosys, syscall-.    .   __exit. , , .

      
  • ,

         

    , newlib/libc/machine/Header       ,   newlib/Libc//. , , .

      
     

,    .

     

linux - . ,    . newlib/libc/sys/linux,    (, . io.c).    syscall, .    x86 ,    newlib/libc/sys/linux/machine/i386/syscall.h. ,    linux x86. , syscall.h       , .

newlib syscall, , . , , sbrk, , . , , C.

_exit

. , , (, ).

close

. :

          int close(int file) {
            return -1;
          }

environ

. :

          char *__env[1] = { 0 };
          char **environ = __env;

execve

. ( ):

          #include <errno.h>
          #undef errno
          extern int errno;
          int execve(char *name, char **argv, char **env) {
            errno = ENOMEM;
            return -1;
          }

fork

. ( ):

          #include <errno.h>
          #undef errno
          extern int errno;
          int fork(void) {
            errno = EAGAIN;
            return -1;
          }

fstat

. . sys/stat.h include C.

          #include <sys/stat.h>
          int fstat(int file, struct stat *st) {
            st->st_mode = S_IFCHR;
            return 0;
          }

getpid

; , . :

          int getpid(void) {
            return 1;
          }

isatty

, . , stdout, :

          int isatty(int file) {
            return 1;
          }

kill

. :

          #include <errno.h>
          #undef errno
          extern int errno;
          int kill(int pid, int sig) {
            errno = EINVAL;
            return -1;
          }

link

. :

          #include <errno.h>
          #undef errno
          extern int errno;
          int link(char *old, char *new) {
            errno = EMLINK;
            return -1;
          }

lseek

. :

          int lseek(int file, int ptr, int dir) {
            return 0;
          }

open

. :

          int open(const char *name, int flags, int mode) {
            return -1;
          }

read

. :

          int read(int file, char *ptr, int len) {
            return 0;
          }

sbrk

. malloc , . ; _end GNU.

          caddr_t sbrk(int incr) {
            extern char _end;     /* Defined by the linker */
            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;
          }

stat

( ). :

          int stat(char *file, struct stat *st) {
            st->st_mode = S_IFCHR;
            return 0;
          }

times

. :

          int times(struct tms *buf) {
            return -1;
          }

unlink

. :

          #include <errno.h>
          #undef errno
          extern int errno;
          int unlink(char *name) {
            errno = ENOENT;
            return -1;
          }

wait

. :

          #include <errno.h>
          #undef errno
          extern int errno;
          int wait(int *status) {
            errno = ECHILD;
            return -1;
          }

write

. libc , stdout, , , , , . ; ( , , , ) .

          int write(int file, char *ptr, int len) {
            int todo;

            for (todo = 0; todo < len; todo++) {
              outbyte (*ptr++);
            }
            return len;
          }

, newlib, . osdev.org. -, , C , .

+7

All Articles