Press "Enter" to skip to content

Ubuntu20 编译 Linux 0.12 内核过程

0.编译前的准备

最近由于课程要求,需要编译Linux0.12内核使用。Linux0.12出生至今已经过了二十多年,环境和语法什么的也发生了些许的变化,难免会遇到各种小问题。这里为了完整地记录编译过程中的问题,特地安装了一台全新的Ubuntu20.04虚拟机,从头编译一遍。

这里使用的代码是来自赵炯老师维护的网站oldlinux.org

如果懒得再造一遍轮子,也可以在这里直接下载我编译好的版本Image

1.开始编译

下面的顺序为编译过程中遇到问题的顺序

1.as86

首先我们会看到报错

make: as86: Command not found.

这里我们需要安装汇编相关的库

sudo apt install bin86

2.gas

报错信息:

make: gas: Command not found.

这是因为gas已经过时,需要将所有的makefile文件中的gas改为as

由于每个Makefile中已经把gas作为全局变量AS,所以只需在每个Makefile开头声明变量处将

AS = gas

改为

AS = as

即可。

3.head.s

错误信息:

boot/head.s: Assembler messages:
boot/head.s:43: 错误: unsupported instruction `mov'
boot/head.s:47: 错误: unsupported instruction `mov'
boot/head.s:59: 错误: unsupported instruction `mov'
boot/head.s:61: 错误: unsupported instruction `mov'
boot/head.s:136: 错误: invalid instruction suffix for `push'
boot/head.s:137: 错误: invalid instruction suffix for `push'
boot/head.s:138: 错误: invalid instruction suffix for `push'
boot/head.s:139: 错误: invalid instruction suffix for `push'
boot/head.s:140: 错误: invalid instruction suffix for `push'
boot/head.s:151: 错误: invalid instruction suffix for `push'
boot/head.s:152: 错误: invalid instruction suffix for `push'
boot/head.s:153: 错误: invalid instruction suffix for `push'
boot/head.s:154: 错误: you can't `push %ds'
boot/head.s:155: 错误: you can't `push %es'
boot/head.s:161: 错误: invalid instruction suffix for `push'
boot/head.s:163: 错误: invalid instruction suffix for `pop'
boot/head.s:165: 错误: you can't `pop %es'
boot/head.s:166: 错误: you can't `pop %ds'
boot/head.s:167: 错误: invalid instruction suffix for `pop'
boot/head.s:168: 错误: invalid instruction suffix for `pop'
boot/head.s:169: 错误: invalid instruction suffix for `pop'
boot/head.s:214: 错误: unsupported instruction `mov'
boot/head.s:215: 错误: unsupported instruction `mov'
boot/head.s:217: 错误: unsupported instruction `mov'
boot/head.s:231: 错误: alignment not a power of 2

这一部分包括两个问题

1.32位汇编

由于曾经Linux0.12内核是编写在32位环境下的,因此我们需要给as指定参数来模拟32位环境。

在每一个Makefile文件的as后面添加参数–32,CFLAGS后面添加参数-m32

2.地址对齐

由于.align语法的变化,我们需要将所有的.align n改为.align [latex]2^n[/latex]

.align 2改为.align 4

.align 3改为.align 8

注:这个错误需要修改所有的.s文件(注意是小写s)

4.gcc

错误信息:

gcc: error: unrecognized command line option ‘-fcombine-regs’
gcc: error: unrecognized command line option ‘-mstring-insns’

随着gcc的版本升级,已经不再支持这两个选项,需要在所有的Makefile文件的CFLAGS中删除这两个选项

5.fork, pause, sync, printf

错误信息:

init/main.c:23:29: error: static declaration of ‘fork’ follows non-static declaration
   23 | static inline _syscall0(int,fork)
      |                             ^~~~
include/unistd.h:151:6: note: in definition of macro ‘_syscall0’
  151 | type name(void) \
      |      ^~~~
include/unistd.h:227:5: note: previous declaration of ‘fork’ was here
  227 | int fork(void);
      |     ^~~~
init/main.c:24:29: error: static declaration of ‘pause’ follows non-static declaration
   24 | static inline _syscall0(int,pause)
      |                             ^~~~~
include/unistd.h:151:6: note: in definition of macro ‘_syscall0’
  151 | type name(void) \
      |      ^~~~
include/unistd.h:241:5: note: previous declaration of ‘pause’ was here
  241 | int pause(void);
      |     ^~~~~
init/main.c:26:29: error: static declaration of ‘sync’ follows non-static declaration
   26 | static inline _syscall0(int,sync)
      |                             ^~~~
include/unistd.h:151:6: note: in definition of macro ‘_syscall0’
  151 | type name(void) \
      |      ^~~~
include/unistd.h:252:5: note: previous declaration of ‘sync’ was here
  252 | int sync(void);
      |     ^~~~
init/main.c:179:12: error: static declaration of ‘printf’ follows non-static declaration
  179 | static int printf(const char *fmt, ...)
      |            ^~~~~~
In file included from include/linux/mm.h:6,
                 from include/linux/sched.h:36,
                 from init/main.c:29:
include/linux/kernel.h:7:5: note: previous declaration of ‘printf’ was here
    7 | int printf(const char * fmt, ...);
      |     ^~~~~~

这是因为这几个函数的定义是static的,而头文件中的声明没有static,我们只需要在头文件中加上static即可。

fork(), pause(), sync() : include/unistd.h

printf() : include/linux/kernel.h

6.asm

错误信息:

include/string.h: In function ‘strcpy’:
include/string.h:29:1: error: ‘asm’ operand has impossible constraints
   29 | __asm__("cld\n"
      | ^~~~~~~

由于GCC的更新,输入或输出寄存器不能出现在可能被修改的寄存器中。在命令行执行:

find -type f -exec sed -i 's/:\"\w\{2\}\"\(,\"\w\{2\}\"\)*)/:) /g' {} \;

即可。

7.gld

错误信息:

make: gld: Command not found.

同问题2的gas,gld也过时了,我们需要将所有Makefile中

LD = gld

改为

LD = ld

8.elf32-i386

错误信息:

ld: relocatable linking with relocations from format elf32-i386(sched.o)
 to format elf64-x86-64 (kernel.o) is not supported

由于之前汇编和编译出来的目标文件(.o)都是模拟32位环境的,我们同样要让链接程序也模拟32位环境。

在所有Makefile文件的ld后面添加-m elf_i386,即开头定义LD处:

LD =ld -m elf_1386

注:从这一条开始,后面每次编译已经可以生成目标文件(.o)了,所以重新生成之前一定要先make clean,将之前有问题的目标文件删除。

9.multiple definition

错误信息:

ld: traps.o: in function `oom':
traps.c:(.text+0x4e5): multiple definition of `oom'; sched.o:sched.c:(.text+0x131): first defined here
ld: traps.o: in function `get_fs_byte':
traps.c:(.text+0x515): multiple definition of `get_fs_byte'; sched.o:sched.c:(.text+0x161): first defined here
ld: traps.o: in function `get_fs_word':
traps.c:(.text+0x521): multiple definition of `get_fs_word'; sched.o:sched.c:(.text+0x16d): first defined here
ld: traps.o: in function `get_fs_long':
traps.c:(.text+0x52e): multiple definition of `get_fs_long'; sched.o:sched.c:(.text+0x17a): first defined here
ld: traps.o: in function `put_fs_byte':
traps.c:(.text+0x53a): multiple definition of `put_fs_byte'; sched.o:sched.c:(.text+0x186): first defined here
ld: traps.o: in function `put_fs_word':
traps.c:(.text+0x54a): multiple definition of `put_fs_word'; sched.o:sched.c:(.text+0x196): first defined here
ld: traps.o: in function `put_fs_long':
traps.c:(.text+0x55b): multiple definition of `put_fs_long'; sched.o:sched.c:(.text+0x1a7): first defined here
ld: traps.o: in function `get_fs':
traps.c:(.text+0x56b): multiple definition of `get_fs'; sched.o:sched.c:(.text+0x1b7): first defined here
ld: traps.o: in function `get_ds':
traps.c:(.text+0x576): multiple definition of `get_ds'; sched.o:sched.c:(.text+0x1c2): first defined here
ld: traps.o: in function `set_fs':
traps.c:(.text+0x581): multiple definition of `set_fs'; sched.o:sched.c:(.text+0x1cd): first defined here
ld: fork.o: in function `oom':
fork.c:(.text+0x0): multiple definition of `oom'; sched.o:sched.c:(.text+0x131): first defined here
ld: fork.o: in function `get_fs_byte':
fork.c:(.text+0x30): multiple definition of `get_fs_byte'; sched.o:sched.c:(.text+0x161): first defined here
ld: fork.o: in function `get_fs_word':
fork.c:(.text+0x3c): multiple definition of `get_fs_word'; sched.o:sched.c:(.text+0x16d): first defined here
ld: fork.o: in function `get_fs_long':
fork.c:(.text+0x49): multiple definition of `get_fs_long'; sched.o:sched.c:(.text+0x17a): first defined here
ld: fork.o: in function `put_fs_byte':
fork.c:(.text+0x55): multiple definition of `put_fs_byte'; sched.o:sched.c:(.text+0x186): first defined here
ld: fork.o: in function `put_fs_word':
fork.c:(.text+0x65): multiple definition of `put_fs_word'; sched.o:sched.c:(.text+0x196): first defined here
ld: fork.o: in function `put_fs_long':
fork.c:(.text+0x76): multiple definition of `put_fs_long'; sched.o:sched.c:(.text+0x1a7): first defined here
ld: fork.o: in function `get_fs':
fork.c:(.text+0x86): multiple definition of `get_fs'; sched.o:sched.c:(.text+0x1b7): first defined here
ld: fork.o: in function `get_ds':
fork.c:(.text+0x91): multiple definition of `get_ds'; sched.o:sched.c:(.text+0x1c2): first defined here
ld: fork.o: in function `set_fs':
fork.c:(.text+0x9c): multiple definition of `set_fs'; sched.o:sched.c:(.text+0x1cd): first defined here
ld: panic.o: in function `oom':
panic.c:(.text+0x0): multiple definition of `oom'; sched.o:sched.c:(.text+0x131): first defined here
ld: vsprintf.o: in function `strcpy':
vsprintf.c:(.text+0x291): multiple definition of `strcpy'; traps.o:traps.c:(.text+0x182): first defined here
ld: vsprintf.o: in function `strncpy':
vsprintf.c:(.text+0x2ab): multiple definition of `strncpy'; traps.o:traps.c:(.text+0x19c): first defined here
ld: vsprintf.o: in function `strcat':
vsprintf.c:(.text+0x2ce): multiple definition of `strcat'; traps.o:traps.c:(.text+0x1bf): first defined here
ld: vsprintf.o: in function `strncat':
vsprintf.c:(.text+0x2f5): multiple definition of `strncat'; traps.o:traps.c:(.text+0x1e6): first defined here
ld: vsprintf.o: in function `strcmp':
vsprintf.c:(.text+0x326): multiple definition of `strcmp'; traps.o:traps.c:(.text+0x217): first defined here
ld: vsprintf.o: in function `strncmp':
vsprintf.c:(.text+0x34d): multiple definition of `strncmp'; traps.o:traps.c:(.text+0x23e): first defined here
ld: vsprintf.o: in function `strchr':
vsprintf.c:(.text+0x37b): multiple definition of `strchr'; traps.o:traps.c:(.text+0x26c): first defined here
ld: vsprintf.o: in function `strrchr':
vsprintf.c:(.text+0x39e): multiple definition of `strrchr'; traps.o:traps.c:(.text+0x28f): first defined here
ld: vsprintf.o: in function `strspn':
vsprintf.c:(.text+0x3c3): multiple definition of `strspn'; traps.o:traps.c:(.text+0x2b4): first defined here
ld: vsprintf.o: in function `strcspn':
vsprintf.c:(.text+0x3fa): multiple definition of `strcspn'; traps.o:traps.c:(.text+0x2eb): first defined here
ld: vsprintf.o: in function `strpbrk':
vsprintf.c:(.text+0x431): multiple definition of `strpbrk'; traps.o:traps.c:(.text+0x322): first defined here
ld: vsprintf.o: in function `strstr':
vsprintf.c:(.text+0x468): multiple definition of `strstr'; traps.o:traps.c:(.text+0x359): first defined here
ld: vsprintf.o: in function `strlen':
vsprintf.c:(.text+0x49f): multiple definition of `strlen'; traps.o:traps.c:(.text+0x390): first defined here
ld: vsprintf.o: in function `strtok':
vsprintf.c:(.text+0x4bc): multiple definition of `strtok'; traps.o:traps.c:(.text+0x3ad): first defined here
ld: vsprintf.o: in function `memcpy':
vsprintf.c:(.text+0x545): multiple definition of `memcpy'; traps.o:traps.c:(.text+0x436): first defined here
ld: vsprintf.o: in function `memmove':
vsprintf.c:(.text+0x55f): multiple definition of `memmove'; traps.o:traps.c:(.text+0x450): first defined here
ld: vsprintf.o: in function `memcmp':
vsprintf.c:(.text+0x589): multiple definition of `memcmp'; traps.o:traps.c:(.text+0x47a): first defined here
ld: vsprintf.o: in function `memchr':
vsprintf.c:(.text+0x5b1): multiple definition of `memchr'; traps.o:traps.c:(.text+0x4a2): first defined here
ld: vsprintf.o: in function `memset':
vsprintf.c:(.text+0x5dc): multiple definition of `memset'; traps.o:traps.c:(.text+0x4cd): first defined here
ld: sys.o: in function `oom':
sys.c:(.text+0x0): multiple definition of `oom'; sched.o:sched.c:(.text+0x131): first defined here
ld: sys.o: in function `get_fs_byte':
sys.c:(.text+0x30): multiple definition of `get_fs_byte'; sched.o:sched.c:(.text+0x161): first defined here
ld: sys.o: in function `get_fs_word':
sys.c:(.text+0x3c): multiple definition of `get_fs_word'; sched.o:sched.c:(.text+0x16d): first defined here
ld: sys.o: in function `get_fs_long':
sys.c:(.text+0x49): multiple definition of `get_fs_long'; sched.o:sched.c:(.text+0x17a): first defined here
ld: sys.o: in function `put_fs_byte':
sys.c:(.text+0x55): multiple definition of `put_fs_byte'; sched.o:sched.c:(.text+0x186): first defined here
ld: sys.o: in function `put_fs_word':
sys.c:(.text+0x65): multiple definition of `put_fs_word'; sched.o:sched.c:(.text+0x196): first defined here
ld: sys.o: in function `put_fs_long':
sys.c:(.text+0x76): multiple definition of `put_fs_long'; sched.o:sched.c:(.text+0x1a7): first defined here
ld: sys.o: in function `get_fs':
sys.c:(.text+0x86): multiple definition of `get_fs'; sched.o:sched.c:(.text+0x1b7): first defined here
ld: sys.o: in function `get_ds':
sys.c:(.text+0x91): multiple definition of `get_ds'; sched.o:sched.c:(.text+0x1c2): first defined here
ld: sys.o: in function `set_fs':
sys.c:(.text+0x9c): multiple definition of `set_fs'; sched.o:sched.c:(.text+0x1cd): first defined here
ld: sys.o: in function `strcpy':
sys.c:(.text+0xa7): multiple definition of `strcpy'; traps.o:traps.c:(.text+0x182): first defined here
ld: sys.o: in function `strncpy':
sys.c:(.text+0xc1): multiple definition of `strncpy'; traps.o:traps.c:(.text+0x19c): first defined here
ld: sys.o: in function `strcat':
sys.c:(.text+0xe4): multiple definition of `strcat'; traps.o:traps.c:(.text+0x1bf): first defined here
ld: sys.o: in function `strncat':
sys.c:(.text+0x10b): multiple definition of `strncat'; traps.o:traps.c:(.text+0x1e6): first defined here
ld: sys.o: in function `strcmp':
sys.c:(.text+0x13c): multiple definition of `strcmp'; traps.o:traps.c:(.text+0x217): first defined here
ld: sys.o: in function `strncmp':
sys.c:(.text+0x163): multiple definition of `strncmp'; traps.o:traps.c:(.text+0x23e): first defined here
ld: sys.o: in function `strchr':
sys.c:(.text+0x191): multiple definition of `strchr'; traps.o:traps.c:(.text+0x26c): first defined here
ld: sys.o: in function `strrchr':
sys.c:(.text+0x1b4): multiple definition of `strrchr'; traps.o:traps.c:(.text+0x28f): first defined here
ld: sys.o: in function `strspn':
sys.c:(.text+0x1d9): multiple definition of `strspn'; traps.o:traps.c:(.text+0x2b4): first defined here
ld: sys.o: in function `strcspn':
sys.c:(.text+0x210): multiple definition of `strcspn'; traps.o:traps.c:(.text+0x2eb): first defined here
ld: sys.o: in function `strpbrk':
sys.c:(.text+0x247): multiple definition of `strpbrk'; traps.o:traps.c:(.text+0x322): first defined here
ld: sys.o: in function `strstr':
sys.c:(.text+0x27e): multiple definition of `strstr'; traps.o:traps.c:(.text+0x359): first defined here
ld: sys.o: in function `strlen':
sys.c:(.text+0x2b5): multiple definition of `strlen'; traps.o:traps.c:(.text+0x390): first defined here
ld: sys.o: in function `strtok':
sys.c:(.text+0x2d2): multiple definition of `strtok'; traps.o:traps.c:(.text+0x3ad): first defined here
ld: sys.o: in function `memcpy':
sys.c:(.text+0x35b): multiple definition of `memcpy'; traps.o:traps.c:(.text+0x436): first defined here
ld: sys.o: in function `memmove':
sys.c:(.text+0x375): multiple definition of `memmove'; traps.o:traps.c:(.text+0x450): first defined here
ld: sys.o: in function `memcmp':
sys.c:(.text+0x39f): multiple definition of `memcmp'; traps.o:traps.c:(.text+0x47a): first defined here
ld: sys.o: in function `memchr':
sys.c:(.text+0x3c7): multiple definition of `memchr'; traps.o:traps.c:(.text+0x4a2): first defined here
ld: sys.o: in function `memset':
sys.c:(.text+0x3f2): multiple definition of `memset'; traps.o:traps.c:(.text+0x4cd): first defined here
ld: exit.o: in function `get_fs_byte':
exit.c:(.text+0x44): multiple definition of `get_fs_byte'; sched.o:sched.c:(.text+0x161): first defined here
ld: exit.o: in function `get_fs_word':
exit.c:(.text+0x50): multiple definition of `get_fs_word'; sched.o:sched.c:(.text+0x16d): first defined here
ld: exit.o: in function `get_fs_long':
exit.c:(.text+0x5d): multiple definition of `get_fs_long'; sched.o:sched.c:(.text+0x17a): first defined here
ld: exit.o: in function `put_fs_byte':
exit.c:(.text+0x69): multiple definition of `put_fs_byte'; sched.o:sched.c:(.text+0x186): first defined here
ld: exit.o: in function `put_fs_word':
exit.c:(.text+0x79): multiple definition of `put_fs_word'; sched.o:sched.c:(.text+0x196): first defined here
ld: exit.o: in function `put_fs_long':
exit.c:(.text+0x8a): multiple definition of `put_fs_long'; sched.o:sched.c:(.text+0x1a7): first defined here
ld: exit.o: in function `get_fs':
exit.c:(.text+0x9a): multiple definition of `get_fs'; sched.o:sched.c:(.text+0x1b7): first defined here
ld: exit.o: in function `get_ds':
exit.c:(.text+0xa5): multiple definition of `get_ds'; sched.o:sched.c:(.text+0x1c2): first defined here
ld: exit.o: in function `set_fs':
exit.c:(.text+0xb0): multiple definition of `set_fs'; sched.o:sched.c:(.text+0x1cd): first defined here
ld: exit.o: in function `oom':
exit.c:(.text+0xc56): multiple definition of `oom'; sched.o:sched.c:(.text+0x131): first defined here
ld: signal.o: in function `oom':
signal.c:(.text+0x0): multiple definition of `oom'; sched.o:sched.c:(.text+0x131): first defined here
ld: signal.o: in function `get_fs_byte':
signal.c:(.text+0x30): multiple definition of `get_fs_byte'; sched.o:sched.c:(.text+0x161): first defined here
ld: signal.o: in function `get_fs_word':
signal.c:(.text+0x3c): multiple definition of `get_fs_word'; sched.o:sched.c:(.text+0x16d): first defined here
ld: signal.o: in function `get_fs_long':
signal.c:(.text+0x49): multiple definition of `get_fs_long'; sched.o:sched.c:(.text+0x17a): first defined here
ld: signal.o: in function `put_fs_byte':
signal.c:(.text+0x55): multiple definition of `put_fs_byte'; sched.o:sched.c:(.text+0x186): first defined here
ld: signal.o: in function `put_fs_word':
signal.c:(.text+0x65): multiple definition of `put_fs_word'; sched.o:sched.c:(.text+0x196): first defined here
ld: signal.o: in function `put_fs_long':
signal.c:(.text+0x76): multiple definition of `put_fs_long'; sched.o:sched.c:(.text+0x1a7): first defined here
ld: signal.o: in function `get_fs':
signal.c:(.text+0x86): multiple definition of `get_fs'; sched.o:sched.c:(.text+0x1b7): first defined here
ld: signal.o: in function `get_ds':
signal.c:(.text+0x91): multiple definition of `get_ds'; sched.o:sched.c:(.text+0x1c2): first defined here
ld: signal.o: in function `set_fs':
signal.c:(.text+0x9c): multiple definition of `set_fs'; sched.o:sched.c:(.text+0x1cd): first defined here

这里同样是定义和声明不一样的问题,需要到对应函数头文件处将extern inline改为static inline。

include/linux/mm.h: oom()

include/asm/segment.h: 所有函数

include/string.h: 所有extern inline 函数

10.page

错误信息:

exec.c: In function ‘copy_strings’:
exec.c:162:44: error: lvalue required as left operand of assignment
  162 |         !(pag = (char *) page[p/PAGE_SIZE] =
      |                                            ^

这里代码存在问题,将

if (!(pag = (char *) page[p/PAGE_SIZE]) &&
	!(pag = (char *) page[p/PAGE_SIZE] =
	(unsigned long *) get_free_page())) 

改为

if (!pag && !(pag = (unsigned long *) get_free_page()))

11.gar

错误信息:

make[1]: gar: Command not found.

gar已经过时,将所有Makefile中gar改为ar

12.BCD

错误信息:

get_put.c: In function ‘put_BCD’:
get_put.c:240:1: error: unsupported size for integer register
  240 | }
      | ^

这里是编译优化导致的错误,将所有Makefile中CFLAGS的-O选项去除。

13.malloc

错误信息:

malloc.c: In function ‘malloc’:
malloc.c:156:46: error: lvalue required as left operand of assignment
  156 |   bdesc->page = bdesc->freeptr = (void *) cp = get_free_page();
      |                                              ^

这里代码错误,将lib/malloc.c中

bdesc->page = bdesc->freeptr = (void *) cp = get_free_page();

改为

cp = get_free_page();
bdesc->freeptr = cp;
bdesc->page = bdesc->freeptr;

14.multiple definition, startup, undefined reference

错误信息:

ld: kernel/blk_drv/blk_drv.a(hd.o): in function `unlock_buffer':
hd.c:(.text+0x0): multiple definition of `unlock_buffer'; kernel/blk_drv/blk_drv.a(floppy.o):floppy.c:(.text+0x0): first defined here
ld: kernel/blk_drv/blk_drv.a(hd.o): in function `end_request':
hd.c:(.text+0x52): multiple definition of `end_request'; kernel/blk_drv/blk_drv.a(floppy.o):floppy.c:(.text+0x52): first defined here
ld: kernel/blk_drv/blk_drv.a(ramdisk.o): in function `unlock_buffer':
ramdisk.c:(.text+0x0): multiple definition of `unlock_buffer'; kernel/blk_drv/blk_drv.a(floppy.o):floppy.c:(.text+0x0): first defined here
ld: kernel/blk_drv/blk_drv.a(ramdisk.o): in function `end_request':
ramdisk.c:(.text+0x52): multiple definition of `end_request'; kernel/blk_drv/blk_drv.a(floppy.o):floppy.c:(.text+0x52): first defined here
ld: 警告: 无法找到项目符号 _start; 缺省为 0000000008049000
ld: boot/head.o: in function `startup_32':
(.text+0x10): undefined reference to `_stack_start'
ld: (.text+0x2e): undefined reference to `_stack_start'
ld: boot/head.o: in function `after_page_tables':
(.text+0x540c): undefined reference to `_main'
ld: boot/head.o: in function `ignore_int':
(.text+0x5440): undefined reference to `_printk'
ld: init/main.o: in function `time_init':
main.c:(.text+0x357): undefined reference to `__stack_chk_fail_local'
ld: kernel/kernel.o: in function `schedule':
(.text+0x461): undefined reference to `_current'
ld: (.text+0x46e): undefined reference to `_current'
ld: (.text+0x478): undefined reference to `_last_task_used_math'
ld: (.text+0x48f): undefined reference to `__stack_chk_fail_local'
ld: kernel/kernel.o: in function `sched_init':
(.text+0xd46): undefined reference to `gdt'
ld: (.text+0xd54): undefined reference to `gdt'
ld: (.text+0xd62): undefined reference to `gdt'
ld: (.text+0xd72): undefined reference to `gdt'
ld: (.text+0xd80): undefined reference to `gdt'
ld: kernel/kernel.o:(.text+0xd8e): more undefined references to `gdt' follow
ld: kernel/kernel.o: in function `sched_init':
(.text+0xf02): undefined reference to `idt'
ld: (.text+0xf11): undefined reference to `idt'
ld: (.text+0xf20): undefined reference to `timer_interrupt'
ld: (.text+0xf5c): undefined reference to `idt'
ld: (.text+0xf6b): undefined reference to `idt'
ld: (.text+0xf7a): undefined reference to `system_call'
ld: kernel/kernel.o: in function `reschedule':
sys_call.o:(.text+0xfa6): undefined reference to `_schedule'
ld: kernel/kernel.o: in function `_system_call':
(.text+0xfc6): undefined reference to `_NR_syscalls'
ld: (.text+0xfcf): undefined reference to `_sys_call_table'
ld: (.text+0xfd5): undefined reference to `_current'
ld: kernel/kernel.o: in function `ret_from_sys_call':
sys_call.o:(.text+0xfe5): undefined reference to `_current'
ld: sys_call.o:(.text+0xfeb): undefined reference to `_task'
ld: sys_call.o:(.text+0x101c): undefined reference to `_do_signal'
ld: kernel/kernel.o: in function `_coprocessor_error':
(.text+0x1054): undefined reference to `_math_error'
ld: kernel/kernel.o: in function `_device_not_available':
(.text+0x1083): undefined reference to `_math_state_restore'
ld: (.text+0x108d): undefined reference to `_math_emulate'
ld: kernel/kernel.o: in function `_timer_interrupt':
(.text+0x10b4): undefined reference to `_jiffies'
ld: (.text+0x10c5): undefined reference to `_do_timer'
ld: kernel/kernel.o: in function `_sys_execve':
(.text+0x10da): undefined reference to `_do_execve'
ld: kernel/kernel.o: in function `_sys_fork':
(.text+0x10e5): undefined reference to `_find_empty_process'
ld: (.text+0x10f4): undefined reference to `_copy_process'
ld: kernel/kernel.o: in function `_hd_interrupt':
(.text+0x111f): undefined reference to `_hd_timeout'
ld: (.text+0x1125): undefined reference to `_do_hd'
ld: (.text+0x112e): undefined reference to `_unexpected_hd_interrupt'
ld: kernel/kernel.o: in function `_floppy_interrupt':
(.text+0x115d): undefined reference to `_do_floppy'
ld: (.text+0x1166): undefined reference to `_unexpected_floppy_interrupt'
ld: kernel/kernel.o: in function `trap_init':
(.text+0x1791): undefined reference to `idt'
ld: (.text+0x1799): undefined reference to `idt'
ld: (.text+0x17a2): undefined reference to `divide_error'
ld: (.text+0x17ba): undefined reference to `idt'
ld: (.text+0x17c3): undefined reference to `idt'
ld: (.text+0x17cc): undefined reference to `debug'
ld: (.text+0x17e4): undefined reference to `idt'
ld: (.text+0x17ed): undefined reference to `idt'
ld: (.text+0x17f6): undefined reference to `nmi'
ld: (.text+0x180e): undefined reference to `idt'
ld: (.text+0x1817): undefined reference to `idt'
ld: (.text+0x1820): undefined reference to `int3'
ld: (.text+0x1838): undefined reference to `idt'
ld: (.text+0x1841): undefined reference to `idt'
ld: (.text+0x184a): undefined reference to `overflow'
ld: (.text+0x1862): undefined reference to `idt'
ld: (.text+0x186b): undefined reference to `idt'
ld: (.text+0x1874): undefined reference to `bounds'
ld: (.text+0x188c): undefined reference to `idt'
ld: (.text+0x1895): undefined reference to `idt'
ld: (.text+0x189e): undefined reference to `invalid_op'
ld: (.text+0x18b6): undefined reference to `idt'
ld: (.text+0x18bf): undefined reference to `idt'
ld: (.text+0x18c8): undefined reference to `device_not_available'
ld: (.text+0x18e0): undefined reference to `idt'
ld: (.text+0x18e9): undefined reference to `idt'
ld: (.text+0x18f2): undefined reference to `double_fault'
ld: (.text+0x190a): undefined reference to `idt'
ld: (.text+0x1913): undefined reference to `idt'
ld: (.text+0x191c): undefined reference to `coprocessor_segment_overrun'
ld: (.text+0x1934): undefined reference to `idt'
ld: (.text+0x193d): undefined reference to `idt'
ld: (.text+0x1946): undefined reference to `invalid_TSS'
ld: (.text+0x195e): undefined reference to `idt'
ld: (.text+0x1967): undefined reference to `idt'
ld: (.text+0x1970): undefined reference to `segment_not_present'
ld: (.text+0x1988): undefined reference to `idt'
ld: (.text+0x1991): undefined reference to `idt'
ld: (.text+0x199a): undefined reference to `stack_segment'
ld: (.text+0x19b2): undefined reference to `idt'
ld: (.text+0x19bb): undefined reference to `idt'
ld: (.text+0x19c4): undefined reference to `general_protection'
ld: (.text+0x19dc): undefined reference to `idt'
ld: (.text+0x19e5): undefined reference to `idt'
ld: (.text+0x19ee): undefined reference to `page_fault'
ld: (.text+0x1a06): undefined reference to `idt'
ld: (.text+0x1a0f): undefined reference to `idt'
ld: (.text+0x1a18): undefined reference to `reserved'
ld: (.text+0x1a30): undefined reference to `idt'
ld: (.text+0x1a3c): undefined reference to `idt'
ld: (.text+0x1a48): undefined reference to `coprocessor_error'
ld: (.text+0x1a60): undefined reference to `idt'
ld: (.text+0x1a6c): undefined reference to `idt'
ld: (.text+0x1a78): undefined reference to `alignment_check'
ld: (.text+0x1aa5): undefined reference to `idt'
ld: (.text+0x1ab9): undefined reference to `idt'
ld: (.text+0x1ac4): undefined reference to `reserved'
ld: (.text+0x1ae8): undefined reference to `idt'
ld: (.text+0x1af4): undefined reference to `idt'
ld: (.text+0x1b00): undefined reference to `irq13'
ld: (.text+0x1b62): undefined reference to `idt'
ld: (.text+0x1b6e): undefined reference to `idt'
ld: (.text+0x1b7a): undefined reference to `parallel_interrupt'
ld: kernel/kernel.o: in function `_divide_error':
(.text+0x1b98): undefined reference to `_do_divide_error'
ld: kernel/kernel.o: in function `_debug':
(.text+0x1bcd): undefined reference to `_do_int3'
ld: kernel/kernel.o: in function `_nmi':
(.text+0x1bd4): undefined reference to `_do_nmi'
ld: kernel/kernel.o: in function `_int3':
(.text+0x1bdb): undefined reference to `_do_int3'
ld: kernel/kernel.o: in function `_overflow':
(.text+0x1be2): undefined reference to `_do_overflow'
ld: kernel/kernel.o: in function `_bounds':
(.text+0x1be9): undefined reference to `_do_bounds'
ld: kernel/kernel.o: in function `_invalid_op':
(.text+0x1bf0): undefined reference to `_do_invalid_op'
ld: kernel/kernel.o: in function `_coprocessor_segment_overrun':
(.text+0x1bf7): undefined reference to `_do_coprocessor_segment_overrun'
ld: kernel/kernel.o: in function `_reserved':
(.text+0x1bfe): undefined reference to `_do_reserved'
ld: kernel/kernel.o: in function `_double_fault':
(.text+0x1c1a): undefined reference to `_do_double_fault'
ld: kernel/kernel.o: in function `_invalid_TSS':
(.text+0x1c51): undefined reference to `_do_invalid_TSS'
ld: kernel/kernel.o: in function `_segment_not_present':
(.text+0x1c58): undefined reference to `_do_segment_not_present'
ld: kernel/kernel.o: in function `_stack_segment':
(.text+0x1c5f): undefined reference to `_do_stack_segment'
ld: kernel/kernel.o: in function `_general_protection':
(.text+0x1c66): undefined reference to `_do_general_protection'
ld: kernel/kernel.o: in function `_alignment_check':
(.text+0x1c6d): undefined reference to `_do_alignment_check'
ld: kernel/kernel.o: in function `copy_process':
(.text+0x2305): undefined reference to `gdt'
ld: (.text+0x2322): undefined reference to `gdt'
ld: (.text+0x2341): undefined reference to `gdt'
ld: (.text+0x235d): undefined reference to `gdt'
ld: (.text+0x2379): undefined reference to `gdt'
ld: kernel/kernel.o:(.text+0x2395): more undefined references to `gdt' follow
ld: kernel/kernel.o: in function `number':
vsprintf.c:(.text+0x2a07): undefined reference to `__stack_chk_fail_local'
ld: kernel/kernel.o: in function `sys_getrusage':
(.text+0x3eac): undefined reference to `__stack_chk_fail_local'
ld: kernel/kernel.o: in function `sys_sigaction':
(.text+0x5993): undefined reference to `__stack_chk_fail_local'
ld: kernel/kernel.o:(.data.rel+0x8): undefined reference to `sys_fork'
ld: kernel/kernel.o:(.data.rel+0x2c): undefined reference to `sys_execve'
ld: kernel/kernel.o:(.data.rel+0x52c): undefined reference to `pg_dir'
ld: mm/mm.o: in function `do_no_page':
(.text+0xd8f): undefined reference to `__stack_chk_fail_local'
ld: mm/mm.o: in function `show_mem':
(.text+0xf0e): undefined reference to `pg_dir'
ld: (.text+0xf26): undefined reference to `pg_dir'
ld: (.text+0xf3d): undefined reference to `pg_dir'
ld: (.text+0xf66): undefined reference to `pg_dir'
ld: (.text+0xf84): undefined reference to `pg_dir'
ld: mm/mm.o:(.text+0x14d8): more undefined references to `pg_dir' follow
ld: mm/mm.o: in function `_page_fault':
(.text+0x191e): undefined reference to `_do_no_page'
ld: (.text+0x1925): undefined reference to `_do_wp_page'
ld: fs/fs.o: in function `sys_open':
(.text+0x9b4): undefined reference to `__stack_chk_fail_local'
ld: fs/fs.o: in function `check_disk_change':
(.text+0x2169): undefined reference to `invalidate_buffers'
ld: fs/fs.o: in function `bread_page':
(.text+0x28e7): undefined reference to `__stack_chk_fail_local'
ld: fs/fs.o: in function `cp_stat':
stat.c:(.text+0x4266): undefined reference to `__stack_chk_fail_local'
ld: fs/fs.o: in function `do_execve':
(.text+0x55c3): undefined reference to `__stack_chk_fail_local'
ld: fs/fs.o: in function `sys_pipe':
(.text+0x5bd2): undefined reference to `__stack_chk_fail_local'
ld: fs/fs.o: in function `get_dir':
namei.c:(.text+0x660b): undefined reference to `__stack_chk_fail_local'
ld: fs/fs.o:namei.c:(.text+0x682c): more undefined references to `__stack_chk_fail_local' follow
ld: kernel/blk_drv/blk_drv.a(floppy.o): in function `setup_DMA':
floppy.c:(.text+0x291): undefined reference to `tmp_floppy_area'
ld: floppy.c:(.text+0x2b4): undefined reference to `tmp_floppy_area'
ld: kernel/blk_drv/blk_drv.a(floppy.o): in function `rw_interrupt':
floppy.c:(.text+0x60e): undefined reference to `tmp_floppy_area'
ld: kernel/blk_drv/blk_drv.a(floppy.o): in function `seek_interrupt':
floppy.c:(.text+0x6c4): undefined reference to `setup_rw_floppy'
ld: kernel/blk_drv/blk_drv.a(floppy.o): in function `transfer':
floppy.c:(.text+0x792): undefined reference to `setup_rw_floppy'
ld: kernel/blk_drv/blk_drv.a(floppy.o): in function `floppy_init':
floppy.c:(.text+0xdc3): undefined reference to `idt'
ld: floppy.c:(.text+0xdcf): undefined reference to `idt'
ld: floppy.c:(.text+0xddb): undefined reference to `floppy_interrupt'
ld: kernel/blk_drv/blk_drv.a(hd.o): in function `hd_init':
hd.c:(.text+0xfa6): undefined reference to `idt'
ld: hd.c:(.text+0xfb2): undefined reference to `idt'
ld: hd.c:(.text+0xfbe): undefined reference to `hd_interrupt'
ld: kernel/blk_drv/blk_drv.a(ramdisk.o): in function `rd_load':
ramdisk.c:(.text+0x5ce): undefined reference to `__stack_chk_fail_local'
ld: kernel/chr_drv/chr_drv.a(tty_io.o): in function `tty_init':
tty_io.c:(.text+0x1964): undefined reference to `__stack_chk_fail_local'
ld: kernel/chr_drv/chr_drv.a(console.o): in function `scrup':
console.c:(.text+0x41f): undefined reference to `_video_num_columns'
ld: console.c:(.text+0x698): undefined reference to `_video_num_columns'
ld: console.c:(.text+0x79b): undefined reference to `_video_num_columns'
ld: kernel/chr_drv/chr_drv.a(console.o): in function `scrdown':
console.c:(.text+0x919): undefined reference to `_video_num_columns'
ld: console.c:(.text+0xa23): undefined reference to `_video_num_columns'
ld: kernel/chr_drv/chr_drv.a(console.o): in function `.L129':
console.c:(.text+0x35a9): undefined reference to `__stack_chk_fail_local'
ld: kernel/chr_drv/chr_drv.a(console.o): in function `con_init':
console.c:(.text+0x3b85): undefined reference to `idt'
ld: console.c:(.text+0x3b91): undefined reference to `idt'
ld: console.c:(.text+0x3b9d): undefined reference to `keyboard_interrupt'
ld: kernel/chr_drv/chr_drv.a(serial.o): in function `rs_init':
serial.c:(.text+0x9a): undefined reference to `idt'
ld: serial.c:(.text+0xa6): undefined reference to `idt'
ld: serial.c:(.text+0xb2): undefined reference to `rs1_interrupt'
ld: serial.c:(.text+0xca): undefined reference to `idt'
ld: serial.c:(.text+0xd6): undefined reference to `idt'
ld: serial.c:(.text+0xe2): undefined reference to `rs2_interrupt'
ld: kernel/chr_drv/chr_drv.a(tty_ioctl.o): in function `get_termio':
tty_ioctl.c:(.text+0x378): undefined reference to `__stack_chk_fail_local'
ld: kernel/chr_drv/chr_drv.a(tty_ioctl.o): in function `set_termio':
tty_ioctl.c:(.text+0x4e5): undefined reference to `__stack_chk_fail_local'

这里有三个问题:

1.multiple definition

和之前的一样,将kernel/blk_drv/blk.h中unlock_buffer()和end_request()前面的extern inline改为static inline

2.无法找到项目符号 _start

在boot/head.s的.text段添加

.globl statup_32

并在./Makefile的ld后面添加-e startup_32来指定程序入口

3.undefined reference

这一步较为繁琐,需要将所有.s文件中以下划线_开头的变量名和函数名的开头下划线_去掉。

注意函数过程中调用的函数名和使用的变量也要做此操作!

15.__stack_chk_fail_local

错误信息:

ld: kernel/chr_drv/chr_drv.a(tty_ioctl.o): in function `get_termio':
tty_ioctl.c:(.text+0x378): undefined reference to `__stack_chk_fail_local'
ld: kernel/chr_drv/chr_drv.a(tty_ioctl.o): in function `set_termio':
tty_ioctl.c:(.text+0x4e5): undefined reference to `__stack_chk_fail_local'
ld: kernel/math/math.a(math_emulate.o): in function `.L132':
math_emulate.c:(.text+0x1dbc): undefined reference to `__stack_chk_fail_local'
ld: kernel/math/math.a(get_put.o): in function `get_short_real':
get_put.c:(.text+0x145): undefined reference to `__stack_chk_fail_local'
ld: kernel/math/math.a(get_put.o): in function `get_long_real':
get_put.c:(.text+0x1ee): undefined reference to `__stack_chk_fail_local'

在所有Makefile中CFLAGS后添加-fno-stack-protector

16.undefined reference

错误信息:

ld: kernel/kernel.o: in function `schedule':
(.text+0x454): undefined reference to `_current'
ld: (.text+0x461): undefined reference to `_current'
ld: (.text+0x46b): undefined reference to `_last_task_used_math'
ld: fs/fs.o: in function `check_disk_change':
(.text+0x2139): undefined reference to `invalidate_buffers'
ld: kernel/blk_drv/blk_drv.a(floppy.o): in function `seek_interrupt':
floppy.c:(.text+0x6bc): undefined reference to `setup_rw_floppy'
ld: kernel/blk_drv/blk_drv.a(floppy.o): in function `transfer':
floppy.c:(.text+0x78a): undefined reference to `setup_rw_floppy'
ld: kernel/chr_drv/chr_drv.a(console.o): in function `scrup':
console.c:(.text+0x41f): undefined reference to `_video_num_columns'
ld: console.c:(.text+0x698): undefined reference to `_video_num_columns'
ld: console.c:(.text+0x79b): undefined reference to `_video_num_columns'
ld: kernel/chr_drv/chr_drv.a(console.o): in function `scrdown':
console.c:(.text+0x919): undefined reference to `_video_num_columns'
ld: console.c:(.text+0xa23): undefined reference to `_video_num_columns'

一共有五处需要修改:

1._current

include/linux/sched.h: _current 前面的下划线去掉

2._last_task_used_math

include/linux/sched.h: _last_task_used_math 前面的下划线去掉

3.invalidate_buffers

fs/buffer.c: 去掉invalidate_buffers()前面的inline

4.setup_rw_floopy

kernel/blk_drv/floppy.c: 去掉setup_rw_floppy()前面的inline

5._video_num_columns

kernel/chr_drv/console.c: _video_num_columns 前面的下划线去掉

17.bits/libc-header-start.h

错误信息:

/usr/include/stdio.h:27:10: fatal error: bits/libc-header-start.h: 没有那个文件或目录
   27 | #include 
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~

缺少库文件,运行下面的命令来安装

sudo apt install gcc-multilib

18.MAJOR, MINOR

错误信息:

build.c:(.text+0x119): undefined reference to `MAJOR'
/usr/bin/ld: build.c:(.text+0x135): undefined reference to `MINOR'
/usr/bin/ld: build.c:(.text+0x1d2): undefined reference to `MAJOR'
/usr/bin/ld: build.c:(.text+0x1ee): undefined reference to `MINOR'

从include/linux/fs.h中把这两个宏复制到tools/build.c中即可

19.hd

错误信息:

tools/build boot/bootsect boot/setup tools/system /dev/hd6 \
	/dev/hd2 > Image
/dev/hd6: No such file or directory

将./Makefile中

ROOT_DEV=/dev/hd6
SWAP_DEV=/dev/hd2

改为

ROOT_DEV=FLOPPY
SWAP_DEV=

20.NON-GCC

错误信息:

Non-GCC header of 'system'

将tools/build.c的以下代码注释掉:

if (((long *) buf)[5] != 0)
     die("Non-GCC header of 'system'");

if (i > SYS_SIZE*16)
	die("System is too big");

至此,已经完全编译结束。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注