简体   繁体   English

静态链接到 newlib 的程序无法在 xv6 中运行

[英]Program statically linked to newlib failed to run in xv6

I encountered some problems when running newlib-linked programs in xv6.我在 xv6 中运行 newlib 链接的程序时遇到了一些问题。 ( This is the newlib port I have used) 是我用过的newlib端口)

I used this toolchain to compile the newlib.我使用这个工具链来编译 newlib。 There aren't any problems in compiling and I do get libc.a, libm.a and other library files.编译没有任何问题,我确实得到了 libc.a、libm.a 和其他库文件。

Then I wrote a hello world program and linked it statically against newlib.然后我写了一个 hello world 程序并将它静态链接到 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.但是生成的可执行文件对于 xv6 文件系统来说太大了(这是设计的限制),所以我把它去掉了。 After stripping the file size is 53k so it's now ok to put it in fs.剥离后文件大小为 53k,因此现在可以将其放入 fs。

When I run "make qemu" I was able to go into the system, and other programs works fine.当我运行“make qemu”时,我能够进入系统,并且其他程序运行良好。 But when I run my test program, it stuck for a few second and then it says "panic: loaduvm: addr must be page aligned".但是当我运行我的测试程序时,它卡住了几秒钟,然后它说“恐慌:loaduvm:addr 必须页面对齐”。 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?是因为我剥离了我的程序,或者我必须对 xv6 源代码应用补丁或修改,还是其他一些原因?

PS 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: PS 我使用的是来自官方 Github 存储库的最新版本的 xv6,下面是我用来编译测试程序的标志:

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

The problem has been solved.问题已经解决。 I forgot to add a "-N" parameter when linking with GCC.与 GCC 链接时,我忘记添加“-N”参数。

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.根据我的理解,“-N”和“-Ttext 0”标志都使编译的程序在加载到内存中时保持 4k 对齐,这是 xv6 所要求的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM