OSDev.org
https://forum.osdev.org/

Newlib does not flush stdout at end of execution
https://forum.osdev.org/viewtopic.php?f=1&t=36449
Page 1 of 1

Author:  Haoud [ Fri Jan 17, 2020 11:55 am ]
Post subject:  Newlib does not flush stdout at end of execution

Hello, I have a problem with newlib: I'm trying to compile a very simple program for my kernem:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
    struct dirent *dirent = NULL;
    char *directory_path = ".";
    DIR * directory = NULL;

    if(argc >= 2)
        directory_path = argv[1];

    directory = opendir(directory_path);
    if(!directory)
        {printf("Impossible d'ouvrir le rĂ©pertoire %s", directory_path); return -1;}

    while ((dirent = readdir(directory)) != NULL)
       printf("%s\n", dirent->d_name);

    closedir(directory);
    return 0;
}

But this code doesn't display anything on the screen unless I explicitly clear stdout. I debugged a bit my program with gdb and I realized that no destructor was called (there is no one) when exit is called, why ? Normally the buffers are emptied when the program ends, right?

My crt0.c code:
Code:
#include <fcntl.h>
#include <stdlib.h>
#include "syscalls.h"

extern char end;
extern char **environ;
extern void exit(int code);
extern int brk(void * addr);
extern void __libc_init_array();
extern void __libc_fini_array();
extern int main(int argc, char ** argv, char ** envp);

extern void _init(void);

void _start(int args)
{
    int *params = &args - 1;
    int argc = *params;
    char **argv = (char **) (params + 1);

    environ = argv + argc + 1;

    brk(&end);                           // Initialise le heap
    __libc_init_array();            // Appelle les constructeurs
    atexit(__libc_fini_array);
    exit(main(argc, argv, environ));
}


Thank you in advance!

Author:  nullplan [ Fri Jan 17, 2020 10:56 pm ]
Post subject:  Re: Newlib does not flush stdout at end of execution

If I read that right, there is a function called __sinit() that should be called from the stdio code. It will set GLOBAL_REENT->__cleanup to _cleanup_r(). exit(), then, will call that function through the pointer. There are not many ways this can go wrong. Find out if __sinit() is called and what GLOBAL_REENT->__cleanup is when exit is called.

Author:  Haoud [ Sat Jan 18, 2020 4:23 pm ]
Post subject:  Re: Newlib does not flush stdout at end of execution

Thanks a lot, everything works fine, the answer was given in this topic (viewtopic.php?f=1&t=31261) but due to a bug in the implementation of the system calls it was not working.

And sorry for the late response but my IP has been blacklisted by osdev, no idea why :(

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/