Search code examples
cxv6newlib

Program statically linked to newlib failed to run in xv6


I encountered some problems when running newlib-linked programs in xv6. (This is the newlib port I have used)

I used this toolchain to compile the newlib. There aren't any problems in compiling and I do get libc.a, libm.a and other library files.

Then I wrote a hello world program and linked it statically against newlib. The program is simply like this:

#include <stdio.h>

int main()
{   printf("hello world\n");
    return 0;
}

But the executable generated is too big for the xv6 filesystem (That's a restriction of design), so I stripped it. After stripping the file size is 53k so it's now ok to put it in fs.

When I run "make qemu" I was able to go into the system, and other programs works fine. But when I run my test program, it stuck for a few second and then it says "panic: loaduvm: addr must be page aligned". Is it because I stripped my program, or there are patches or modifications I have to apply to xv6 source code, or some other reasons?

P.S. I'm using up-to-date version of xv6 from it's offical Github repo, and below is the flags I used to compile my test programs:

cc -fno-pic -static -fno-strict-aliasing -fvar-tracking -fvar-tracking-assignments -static-libgcc -nostartfiles -nostdlib -ffreestanding -nodefaultlibs -fno-builtin -m32 -Wall -MD -gdwarf-2 -fno-omit-frame-pointer -fno-stack-protector -I../include/newlib -o build/_test test.c -L../lib/newlib/ -L../lib/libnosys -e main -Ttext 0 -lc -lm -lnosys

Solution

  • The problem has been solved. I forgot to add a "-N" parameter when linking with GCC.

    From my understanding, "-N" and "-Ttext 0" flags both keep the compiled programs aligned in 4k when it is loaded into memory, which is required by xv6.