在 MAC 上编译 XV6

Compile XV6 on MAC

在编译 XV6 之前,我已经阅读了 this page 以构建我自己的编译器工具链。结果也差不多。

➜  xv6-public git:(master) ✗ i386-jos-elf-objdump -i
BFD header file version (GNU Binutils) 2.27
elf32-i386
 (header little endian, data little endian)
  i386

➜  xv6-public git:(master) ✗ i386-jos-elf-gcc -v
Using built-in specs.
COLLECT_GCC=i386-jos-elf-gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/i386-jos-elf/6.1.0/lto-wrapper
Target: i386-jos-elf
Configured with: ../configure --prefix=/usr/local --target=i386-jos-elf --disable-werror --disable-libssp --disable-libmudflap --with-newlib 

--without-headers --enable-languages=c CC=/usr/bin/gcc
    Thread model: single
    gcc version 6.1.0 (GCC)

但是当我开始编译XV6时,出现错误:

  ➜  xv6-public git:(master) ✗ make
    i386-jos-elf-gcc -fno-pic -static -fno-builtin -fno-strict-aliasing -fvar-tracking -fvar-tracking-assignments -O0 -g -Wall -MD -gdwarf-2 -m32 -Werror -fno-omit-frame-pointer -fno-stack-protector -fno-pic -O -nostdinc -I. -c bootmain.c
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:5:2: error: unknown directive
            .type   waitdisk, @function
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:11:2: error: instruction requires: Not 64-bit mode
            pushl   %ebp
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:26:1: error: unexpected token at start of statement
    /APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:27:1: error: unexpected token at start of statement
    /  8 "x86.h" 1
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:29:1: error: unexpected token at start of statement
    /  0 "" 2
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:31:1: error: unexpected token at start of statement
    /NO_APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:39:2: error: instruction requires: Not 64-bit mode
            popl    %ebp
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:46:2: error: unknown directive
            .size   waitdisk, .-waitdisk
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:48:2: error: unknown directive
            .type   readsect, @function
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:54:2: error: instruction requires: Not 64-bit mode
            pushl   %ebp
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:61:2: error: instruction requires: Not 64-bit mode
            pushl   %edi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:62:2: error: instruction requires: Not 64-bit mode
            pushl   %ebx
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:74:1: error: unexpected token at start of statement
    /APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:75:1: error: unexpected token at start of statement
    /  24 "x86.h" 1
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:77:1: error: unexpected token at start of statement
    /  0 "" 2
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:79:1: error: unexpected token at start of statement
    /NO_APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:86:1: error: unexpected token at start of statement
    /APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:87:1: error: unexpected token at start of statement
    /  24 "x86.h" 1
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:89:1: error: unexpected token at start of statement
    /  0 "" 2
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:91:1: error: unexpected token at start of statement
    /NO_APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:101:1: error: unexpected token at start of statement
    /APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:102:1: error: unexpected token at start of statement
    /  24 "x86.h" 1
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:104:1: error: unexpected token at start of statement
    /  0 "" 2
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:106:1: error: unexpected token at start of statement
    /NO_APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:116:1: error: unexpected token at start of statement
    /APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:117:1: error: unexpected token at start of statement
    /  24 "x86.h" 1
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:119:1: error: unexpected token at start of statement
    /  0 "" 2
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:121:1: error: unexpected token at start of statement
    /NO_APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:132:1: error: unexpected token at start of statement
    /APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:133:1: error: unexpected token at start of statement
    /  24 "x86.h" 1
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:135:1: error: unexpected token at start of statement
    /  0 "" 2
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:137:1: error: unexpected token at start of statement
    /NO_APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:144:1: error: unexpected token at start of statement
    /APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:145:1: error: unexpected token at start of statement
    /  24 "x86.h" 1
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:147:1: error: unexpected token at start of statement
    /  0 "" 2
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:149:1: error: unexpected token at start of statement
    /NO_APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:161:1: error: unexpected token at start of statement
    /APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:162:1: error: unexpected token at start of statement
    /  15 "x86.h" 1
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:164:1: error: unexpected token at start of statement
    /  0 "" 2
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:166:1: error: unexpected token at start of statement
    /NO_APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:170:2: error: instruction requires: Not 64-bit mode
            popl    %ebx
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:172:2: error: instruction requires: Not 64-bit mode
            popl    %edi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:174:2: error: instruction requires: Not 64-bit mode
            popl    %ebp
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:181:2: error: unknown directive
            .size   readsect, .-readsect
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:183:2: error: unknown directive
            .type   readseg, @function
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:189:2: error: instruction requires: Not 64-bit mode
            pushl   %ebp
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:196:2: error: instruction requires: Not 64-bit mode
            pushl   %edi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:197:2: error: instruction requires: Not 64-bit mode
            pushl   %esi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:198:2: error: instruction requires: Not 64-bit mode
            pushl   %ebx
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:222:2: error: instruction requires: Not 64-bit mode
            pushl   %esi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:223:2: error: instruction requires: Not 64-bit mode
            pushl   %ebx
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:237:2: error: instruction requires: Not 64-bit mode
            popl    %ebx
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:240:2: error: instruction requires: Not 64-bit mode
            popl    %esi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:243:2: error: instruction requires: Not 64-bit mode
            popl    %edi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:246:2: error: instruction requires: Not 64-bit mode
            popl    %ebp
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:253:2: error: unknown directive
            .size   readseg, .-readseg
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:255:2: error: unknown directive
            .type   bootmain, @function
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:260:2: error: instruction requires: Not 64-bit mode
            pushl   %ebp
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:267:2: error: instruction requires: Not 64-bit mode
            pushl   %edi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:268:2: error: instruction requires: Not 64-bit mode
            pushl   %esi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:269:2: error: instruction requires: Not 64-bit mode
            pushl   %ebx
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:276:2: error: instruction requires: Not 64-bit mode
            pushl   [=12=]
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:277:2: error: instruction requires: Not 64-bit mode
            pushl   96
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:278:2: error: instruction requires: Not 64-bit mode
            pushl   536
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:288:2: error: instruction requires: Not 64-bit mode
            popl    %ebx
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:291:2: error: instruction requires: Not 64-bit mode
            popl    %esi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:293:2: error: instruction requires: Not 64-bit mode
            popl    %edi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:295:2: error: instruction requires: Not 64-bit mode
            popl    %ebp
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:333:2: error: instruction requires: Not 64-bit mode
            pushl   4(%ebx)
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:334:2: error: instruction requires: Not 64-bit mode
            pushl   16(%ebx)
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:335:2: error: instruction requires: Not 64-bit mode
            pushl   %edi
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:354:1: error: unexpected token at start of statement
    /APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:355:1: error: unexpected token at start of statement
    /  45 "x86.h" 1
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:357:1: error: unexpected token at start of statement
    /  0 "" 2
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:359:1: error: unexpected token at start of statement
    /NO_APP
    ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:365:2: error: unknown directive
            .size   bootmain, .-bootmain
            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:369:11: error: mach-o section specifier uses an unknown section type
            .section        .debug_info,"",@progbits
                            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:993:11: error: mach-o section specifier uses an unknown section type
            .section        .debug_abbrev,"",@progbits
                            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:1454:11: error: mach-o section specifier uses an unknown section type
            .section        .debug_loc,"",@progbits
                            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:1785:11: error: mach-o section specifier uses an unknown section type
            .section        .debug_aranges,"",@progbits
                            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:1797:11: error: mach-o section specifier uses an unknown section type
            .section        .debug_line,"",@progbits
                            ^
    /var/folders/nc/w_y2cp4j4jx2vz4w_1rjy_kh0000gn/T//cc8pMjCq.s:1799:11: error: mach-o section specifier uses an unknown section type
            .section        .debug_str,"MS",@progbits,1
                            ^
    make: *** [bootblock] Error 1

我查了很多博客,都没有这个问题。 我使用 MAC OS X El Capitan (10.11.3)

您不需要编译自己的交叉编译器(和工具)。它适用于 osx 端口。

sudo port install i386-elf-gcc gdb

我正在使用 Sierra,xv6 与以下软件完美配合:

make TOOLCHAIN=i386-elf- qemu-nox

调试也有效。

附带说明一下,您需要卸载 texinfo 才能安装 i386-elf-binutils(它在 i386-elf-gcc 的依赖链中)。该数据包的文档中存在一些不兼容的代码。

我必须从 here 安装 MacPorts,然后使用两个命令:

sudo port install i386-elf-gcc gdb

make TOOLPREFIX=i386-elf- qemu-nox

macOS Big Sur (11.0+) 更新

如果您使用的是 Homebrew,则以下设置应该有效

brew install qemu x86_64-elf-gcc

export TOOLPREFIX=x86_64-elf- 
export QEMU=qemu-system-i386

make qemu