Newlib does not flush stdout at end of execution

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
Haoud
Member
Member
Posts: 31
Joined: Wed Aug 10, 2016 3:07 am
Libera.chat IRC: Haoud

Newlib does not flush stdout at end of execution

Post by Haoud »

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

Code: Select all

#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: Select all

#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!
nullplan
Member
Member
Posts: 1794
Joined: Wed Aug 30, 2017 8:24 am

Re: Newlib does not flush stdout at end of execution

Post by nullplan »

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.
Carpe diem!
Haoud
Member
Member
Posts: 31
Joined: Wed Aug 10, 2016 3:07 am
Libera.chat IRC: Haoud

Re: Newlib does not flush stdout at end of execution

Post by Haoud »

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 :(
Post Reply