2

I'm trying to add a user space program in xv6, but when I try to compile it gives error: "/usr/include/bits/stdio2.h:104: undefined reference to __printf_chk". Any help? My user program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/user.h>

int 
main(int argc, char *argv[])
{
    setbuf(stdout, NULL);
    for (int i = 1; i < argc; i++)
    {
         printf("Print in user space: %s\n", argv[0]);
    }
    exit(0);

    return 0;
}


Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Syed
  • 57
  • 1
  • 6
  • That name (`__printf_chk`) looks like a name that might be implemented in a modern C compiler with a modern C library. My understanding of XV6 is that it is mainly Unix Version 6, which is anything but modern. I wonder if you're using a header that is not for the XV6 environment? – Jonathan Leffler Sep 15 '19 at 00:38
  • I saw the exact same headers working fine with printf statements in the program. – Syed Sep 15 '19 at 00:43
  • Since your program only uses `setbuf()`, `printf()` and `exit()`, it isn't clear how this program that fails differs from the one(s) that work using `printf()`. You're probably not using the compiler correctly, somehow, but I don't have XV6 on my machines so I can't help much more. – Jonathan Leffler Sep 15 '19 at 00:45
  • It isn't clear why you use `exit(0)` making the `return 0;` unreachable. On the whole, I'd omit the `exit()`, and then you only need the `` header (you only need that and `` if you keep the call to `exit()`). The other headers probably aren't breaking anything, but it isn't clear they're helping either. – Jonathan Leffler Sep 15 '19 at 00:47
  • 1
    @JonathanLeffler I think there's some issue in XV6 where `return` from `main` doesn't work but the compiler warns if you don't have the `return` there. – S.S. Anne Sep 15 '19 at 00:50
  • 1
    @JL2210 — judging from the [XV6](https://pdos.csail.mit.edu/6.828/2012/xv6.html) home page, the compiler is GCC 4.5.1, which is more modern than a Unix V6 compiler, but still not quite cutting edge (not now; it may have been when XV6 was first released). As I said, "it isn't clear"; what you describe as mitigating circumstances are not clear to those not intimately familiar with XV6. But that's also why I said "it isn't clear"; that allows for the possibility (apparently, more than just a possibility) that there is a reason. It's more than a little odd that the XV6 environment isn't cleaner. – Jonathan Leffler Sep 15 '19 at 00:55

2 Answers2

4

You've compiled the program with your host headers, which are from glibc, but you're trying to link it with the xv6 target libraries. That's not going to work. You need to be using a proper cross compiler and taking care not to pollute it with -I options that would bring in host headers or -L options that would bring in host libraries.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
0

for future reference i will answer dont include normal C header files you have to use headers provided by the xv6-libraries if u just include normal C headers you will get an error, more specifically delete the C headers and , if you are on RISC-V use #include "user/user.h"