Ubuntu18.04下編譯Linux0.12筆記(編譯+調試學習---升級版)

一、下載源代碼
網站:https://mirrors.edge.kernel.org/pub/linux/kernel/Historic/old-versions/
二、

chenhao@chenhao-VirtualBox:~/chenhao/linux0.12/linux-0.12$ make
cpp -nostdinc -Iinclude -traditional boot/bootsect.S -o boot/bootsect.s
as86 -0 -a -o boot/bootsect.o boot/bootsect.s
ld86 -0 -s -o boot/bootsect boot/bootsect.o
cpp -nostdinc -Iinclude -traditional boot/setup.S -o boot/setup.s
as86 -0 -a -o boot/setup.o boot/setup.s
ld86 -0 -s -o boot/setup boot/setup.o
gas -c -o boot/head.o boot/head.s
make: gas: Command not found
Makefile:35: recipe for target 'boot/head.o' failed
make: *** [boot/head.o] Error 127

gas,gld已經被淘汰,該替換成as和ld(Makefile中)

chenhao@chenhao-VirtualBox:~/chenhao/linux0.12/linux-0.12$ make
as -c -o boot/head.o boot/head.s
boot/head.s: Assembler messages:
boot/head.s:43: Error: unsupported instruction `mov'
boot/head.s:47: Error: unsupported instruction `mov'
boot/head.s:59: Error: unsupported instruction `mov'
boot/head.s:61: Error: unsupported instruction `mov'
boot/head.s:136: Error: invalid instruction suffix for `push'
boot/head.s:137: Error: invalid instruction suffix for `push'
boot/head.s:138: Error: invalid instruction suffix for `push'
boot/head.s:139: Error: invalid instruction suffix for `push'
boot/head.s:140: Error: invalid instruction suffix for `push'
boot/head.s:151: Error: invalid instruction suffix for `push'
boot/head.s:152: Error: invalid instruction suffix for `push'
boot/head.s:153: Error: invalid instruction suffix for `push'
boot/head.s:154: Error: operand type mismatch for `push'
boot/head.s:155: Error: operand type mismatch for `push'
boot/head.s:161: Error: invalid instruction suffix for `push'
boot/head.s:163: Error: invalid instruction suffix for `pop'
boot/head.s:165: Error: operand type mismatch for `pop'
boot/head.s:166: Error: operand type mismatch for `pop'
boot/head.s:167: Error: invalid instruction suffix for `pop'
boot/head.s:168: Error: invalid instruction suffix for `pop'
boot/head.s:169: Error: invalid instruction suffix for `pop'
boot/head.s:214: Error: unsupported instruction `mov'
boot/head.s:215: Error: unsupported instruction `mov'
boot/head.s:217: Error: unsupported instruction `mov'
boot/head.s:231: Error: alignment not a power of 2
Makefile:35: recipe for target 'boot/head.o' failed
make: *** [boot/head.o] Error 1

這是由於在64位機器上編譯的原因,需要告訴編譯器,我們要編譯32位的code,在所有Makefile的AS後面添加 --32,CFLAGS中加-m32

chenhao@chenhao-VirtualBox:~/chenhao/linux0.12/linux-0.12$ make
as --32 -c -o boot/head.o boot/head.s
boot/head.s: Assembler messages:
boot/head.s:231: Error: alignment not a power of 2
Makefile:35: recipe for target 'boot/head.o' failed
make: *** [boot/head.o] Error 1

把align n -> align 2^n

chenhao@chenhao-VirtualBox:~/chenhao/linux0.12/linux-0.12$ make
as --32 -c -o boot/head.o boot/head.s
gcc  -Wall -O -fstrength-reduce -fomit-frame-pointer -fcombine-regs -mstring-insns -m32 \
-nostdinc -Iinclude -c -o init/main.o init/main.c
gcc: error: unrecognized command line option ‘-fcombine-regs’; did you mean ‘-Wcompare-reals’?
gcc: error: unrecognized command line option ‘-mstring-insns’; did you mean ‘-fstrict-enums’?
Makefile:37: recipe for target 'init/main.o' failed
make: *** [init/main.o] Error 1

現在的編譯器不支持-fcombine-regs和-mstring-insns選項,Makefile中刪除這兩個後綴

chenhao@chenhao-VirtualBox:~/chenhao/linux0.12/linux-0.12$ make
gcc  -Wall -O -fstrength-reduce -fomit-frame-pointer -m32 \
-nostdinc -Iinclude -c -o init/main.o init/main.c
In file included from init/main.c:8:0:
init/main.c:23:29: error: static declaration of ‘fork’ follows non-static declaration
 static inline _syscall0(int,fork)
                             ^
include/unistd.h:151:6: note: in definition of macro ‘_syscall0’
 type name(void) \
      ^~~~
include/unistd.h:227:5: note: previous declaration of ‘fork’ was here
 int fork(void);
     ^~~~
init/main.c:24:29: error: static declaration of ‘pause’ follows non-static declaration
 static inline _syscall0(int,pause)
                             ^
include/unistd.h:151:6: note: in definition of macro ‘_syscall0’
 type name(void) \
      ^~~~
include/unistd.h:241:5: note: previous declaration of ‘pause’ was here
 int pause(void);
     ^~~~~
init/main.c:26:29: error: static declaration of ‘sync’ follows non-static declaration
 static inline _syscall0(int,sync)
                             ^
include/unistd.h:151:6: note: in definition of macro ‘_syscall0’
 type name(void) \
      ^~~~
include/unistd.h:252:5: note: previous declaration of ‘sync’ was here
 int sync(void);
     ^~~~
In file included from init/main.c:42:0:
include/string.h:128:22: warning: conflicting types for built-in function ‘strchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strchr(const char * s,char c)
                      ^~~~~~
include/string.h:145:22: warning: conflicting types for built-in function ‘strrchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strrchr(const char * s,char c)
                      ^~~~~~~
include/string.h:379:22: warning: conflicting types for built-in function ‘memchr’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memchr(const void * cs,char c,int count)
                      ^~~~~~
include/string.h:395:22: warning: conflicting types for built-in function ‘memset’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memset(void * s,char c,int count)
                      ^~~~~~
init/main.c:127:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(void)  /* This really IS void, no error here. */
      ^~~~
init/main.c:179:12: error: static declaration of ‘printf’ follows non-static declaration
 static int printf(const char *fmt, ...)
            ^~~~~~
In file included from include/linux/mm.h:6:0,
                 from include/linux/sched.h:36,
                 from init/main.c:29:
include/linux/kernel.h:7:5: note: previous declaration of ‘printf’ was here
 int printf(const char * fmt, ...);
     ^~~~~~
Makefile:37: recipe for target 'init/main.o' failed
make: *** [init/main.o] Error 1

23/24/25/26行報錯是說類型不匹配,前面unistd.h中函數是non-static型,這個main.c文件中是static型的,需要把static inline去掉。後面的printf也是一樣。

chenhao@chenhao-VirtualBox:~/chenhao/linux0.12/linux-0.12$ make
gcc  -Wall -O -fstrength-reduce -fomit-frame-pointer -m32 \
-nostdinc -Iinclude -c -o init/main.o init/main.c
In file included from init/main.c:42:0:
include/string.h:128:22: warning: conflicting types for built-in function ‘strchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strchr(const char * s,char c)
                      ^~~~~~
include/string.h:145:22: warning: conflicting types for built-in function ‘strrchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strrchr(const char * s,char c)
                      ^~~~~~~
include/string.h:379:22: warning: conflicting types for built-in function ‘memchr’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memchr(const void * cs,char c,int count)
                      ^~~~~~
include/string.h:395:22: warning: conflicting types for built-in function ‘memset’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memset(void * s,char c,int count)
                      ^~~~~~
init/main.c:127:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(void)  /* This really IS void, no error here. */
      ^~~~
init/main.c: In function ‘init’:
init/main.c:200:21: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
  printf("Free mem: %d bytes\n\r",memory_end-main_memory_start);
                    ~^            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    %ld
In file included from init/main.c:42:0:
include/string.h: In function ‘strcpy’:
include/string.h:29:1: error: ‘asm’ operand has impossible constraints
 __asm__("cld\n"
 ^~~~~~~
Makefile:37: recipe for target 'init/main.o' failed
make: *** [init/main.o] Error 1

新版的asm最後的寄存器不能出現已使用的。需要全部刪除。

chenhao@chenhao-VirtualBox:~/chenhao/linux0.12/linux-0.12$ make
(cd kernel; make)
make[1]: Entering directory '/home/chenhao/chenhao/linux0.12/linux-0.12/kernel'
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -fcombine-regs -nostdinc -I../include \
-c -o sched.o sched.c
gcc: error: unrecognized command line option ‘-fcombine-regs’; did you mean ‘-Wcompare-reals’?
Makefile:24: recipe for target 'sched.o' failed
make[1]: *** [sched.o] Error 1
make[1]: Leaving directory '/home/chenhao/chenhao/linux0.12/linux-0.12/kernel'
Makefile:75: recipe for target 'kernel/kernel.o' failed
make: *** [kernel/kernel.o] Error 2

選項 -fcombine-regs也不支持了,全部刪除。

chenhao@chenhao-VirtualBox:~/chenhao/linux0.12/linux-0.12$ make
gcc  -Wall -O -fstrength-reduce -fomit-frame-pointer -m32 \
-nostdinc -Iinclude -c -o init/main.o init/main.c
In file included from init/main.c:42:0:
include/string.h:128:22: warning: conflicting types for built-in function ‘strchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strchr(const char * s,char c)
                      ^~~~~~
include/string.h:145:22: warning: conflicting types for built-in function ‘strrchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strrchr(const char * s,char c)
                      ^~~~~~~
include/string.h:379:22: warning: conflicting types for built-in function ‘memchr’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memchr(const void * cs,char c,int count)
                      ^~~~~~
include/string.h:395:22: warning: conflicting types for built-in function ‘memset’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memset(void * s,char c,int count)
                      ^~~~~~
init/main.c:127:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(void)  /* This really IS void, no error here. */
      ^~~~
init/main.c: In function ‘init’:
init/main.c:200:21: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
  printf("Free mem: %d bytes\n\r",memory_end-main_memory_start);
                    ~^            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    %ld
(cd kernel; make)
make[1]: Entering directory '/home/chenhao/chenhao/linux0.12/linux-0.12/kernel'
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o sched.o sched.c
sched.c: In function ‘__sleep_on’:
sched.c:190:6: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  if (*p = tmp)
      ^
sched.c: Assembler messages:
sched.c:165: Warning: indirect ljmp without `*'
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o traps.o traps.c
In file included from traps.c:13:0:
../include/string.h:128:22: warning: conflicting types for built-in function ‘strchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strchr(const char * s,char c)
                      ^~~~~~
../include/string.h:145:22: warning: conflicting types for built-in function ‘strrchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strrchr(const char * s,char c)
                      ^~~~~~~
../include/string.h:379:22: warning: conflicting types for built-in function ‘memchr’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memchr(const void * cs,char c,int count)
                      ^~~~~~
../include/string.h:395:22: warning: conflicting types for built-in function ‘memset’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memset(void * s,char c,int count)
                      ^~~~~~
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o fork.o fork.c
fork.c: In function ‘copy_process’:
fork.c:120:7: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (f=p->filp[i])
       ^
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o panic.o panic.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o printk.o printk.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o vsprintf.o vsprintf.c
In file included from vsprintf.c:13:0:
../include/string.h:128:22: warning: conflicting types for built-in function ‘strchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strchr(const char * s,char c)
                      ^~~~~~
../include/string.h:145:22: warning: conflicting types for built-in function ‘strrchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strrchr(const char * s,char c)
                      ^~~~~~~
../include/string.h:379:22: warning: conflicting types for built-in function ‘memchr’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memchr(const void * cs,char c,int count)
                      ^~~~~~
../include/string.h:395:22: warning: conflicting types for built-in function ‘memset’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memset(void * s,char c,int count)
                      ^~~~~~
vsprintf.c: In function ‘number’:
vsprintf.c:58:5: warning: suggest explicit braces to avoid ambiguous ‘else’ [-Wdangling-else]
  if (type&SPECIAL)
     ^
vsprintf.c:73:5: warning: suggest explicit braces to avoid ambiguous ‘else’ [-Wdangling-else]
  if (type&SPECIAL)
     ^
vsprintf.c: In function ‘vsprintf’:
vsprintf.c:105:6: warning: variable ‘qualifier’ set but not used [-Wunused-but-set-variable]
  int qualifier;  /* 'h', 'l', or 'L' for integer fields */
      ^~~~~~~~~
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o sys.o sys.c
In file included from sys.c:18:0:
../include/string.h:128:22: warning: conflicting types for built-in function ‘strchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strchr(const char * s,char c)
                      ^~~~~~
../include/string.h:145:22: warning: conflicting types for built-in function ‘strrchr’ [-Wbuiltin-declaration-mismatch]
 extern inline char * strrchr(const char * s,char c)
                      ^~~~~~~
../include/string.h:379:22: warning: conflicting types for built-in function ‘memchr’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memchr(const void * cs,char c,int count)
                      ^~~~~~
../include/string.h:395:22: warning: conflicting types for built-in function ‘memset’ [-Wbuiltin-declaration-mismatch]
 extern inline void * memset(void * s,char c,int count)
                      ^~~~~~
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o exit.o exit.c
exit.c: In function ‘sys_kill’:
exit.c:214:8: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    if (err = send_sig(sig,*p,0))
        ^~~
exit.c: In function ‘do_exit’:
exit.c:309:6: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
  if (p = current->p_cptr) {
      ^
exit.c: In function ‘sys_exit’:
exit.c:368:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o signal.o signal.c
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o mktime.o mktime.c
ld -r -o kernel.o sched.o sys_call.o traps.o asm.o fork.o panic.o printk.o vsprintf.o sys.o exit.o signal.o mktime.o
traps.o: In function `oom':
traps.c:(.text+0x49b): multiple definition of `oom'
sched.o:sched.c:(.text+0x115): first defined here
traps.o: In function `get_fs_byte':
traps.c:(.text+0x4c7): multiple definition of `get_fs_byte'
sched.o:sched.c:(.text+0x141): first defined here
traps.o: In function `get_fs_word':
traps.c:(.text+0x4cf): multiple definition of `get_fs_word'
sched.o:sched.c:(.text+0x149): first defined here
traps.o: In function `get_fs_long':
traps.c:(.text+0x4d8): multiple definition of `get_fs_long'
sched.o:sched.c:(.text+0x152): first defined here
traps.o: In function `put_fs_byte':
traps.c:(.text+0x4e0): multiple definition of `put_fs_byte'
sched.o:sched.c:(.text+0x15a): first defined here
traps.o: In function `put_fs_word':
traps.c:(.text+0x4ec): multiple definition of `put_fs_word'
sched.o:sched.c:(.text+0x166): first defined here
traps.o: In function `put_fs_long':
traps.c:(.text+0x4f9): multiple definition of `put_fs_long'
sched.o:sched.c:(.text+0x173): first defined here
traps.o: In function `get_fs':
traps.c:(.text+0x505): multiple definition of `get_fs'
sched.o:sched.c:(.text+0x17f): first defined here
traps.o: In function `get_ds':
traps.c:(.text+0x50c): multiple definition of `get_ds'
sched.o:sched.c:(.text+0x186): first defined here
traps.o: In function `set_fs':
traps.c:(.text+0x513): multiple definition of `set_fs'
sched.o:sched.c:(.text+0x18d): first defined here
fork.o: In function `oom':
fork.c:(.text+0x0): multiple definition of `oom'
sched.o:sched.c:(.text+0x115): first defined here
fork.o: In function `get_fs_byte':
fork.c:(.text+0x2c): multiple definition of `get_fs_byte'
sched.o:sched.c:(.text+0x141): first defined here
fork.o: In function `get_fs_word':
fork.c:(.text+0x34): multiple definition of `get_fs_word'
sched.o:sched.c:(.text+0x149): first defined here
fork.o: In function `get_fs_long':
fork.c:(.text+0x3d): multiple definition of `get_fs_long'
sched.o:sched.c:(.text+0x152): first defined here
fork.o: In function `put_fs_byte':
fork.c:(.text+0x45): multiple definition of `put_fs_byte'
sched.o:sched.c:(.text+0x15a): first defined here
fork.o: In function `put_fs_word':
fork.c:(.text+0x51): multiple definition of `put_fs_word'
sched.o:sched.c:(.text+0x166): first defined here
fork.o: In function `put_fs_long':
fork.c:(.text+0x5e): multiple definition of `put_fs_long'
sched.o:sched.c:(.text+0x173): first defined here
fork.o: In function `get_fs':
fork.c:(.text+0x6a): multiple definition of `get_fs'
sched.o:sched.c:(.text+0x17f): first defined here
fork.o: In function `get_ds':
fork.c:(.text+0x71): multiple definition of `get_ds'
sched.o:sched.c:(.text+0x186): first defined here
fork.o: In function `set_fs':
fork.c:(.text+0x78): multiple definition of `set_fs'
sched.o:sched.c:(.text+0x18d): first defined here
panic.o: In function `oom':
panic.c:(.text+0x0): multiple definition of `oom'
sched.o:sched.c:(.text+0x115): first defined here
vsprintf.o: In function `strcpy':
vsprintf.c:(.text+0x265): multiple definition of `strcpy'
traps.o:traps.c:(.text+0x182): first defined here
vsprintf.o: In function `strncpy':
vsprintf.c:(.text+0x27b): multiple definition of `strncpy'
traps.o:traps.c:(.text+0x198): first defined here
vsprintf.o: In function `strcat':
vsprintf.c:(.text+0x29a): multiple definition of `strcat'
traps.o:traps.c:(.text+0x1b7): first defined here
vsprintf.o: In function `strncat':
vsprintf.c:(.text+0x2bd): multiple definition of `strncat'
traps.o:traps.c:(.text+0x1da): first defined here
vsprintf.o: In function `strcmp':
vsprintf.c:(.text+0x2ea): multiple definition of `strcmp'
traps.o:traps.c:(.text+0x207): first defined here
vsprintf.o: In function `strncmp':
vsprintf.c:(.text+0x30d): multiple definition of `strncmp'
traps.o:traps.c:(.text+0x22a): first defined here
vsprintf.o: In function `strchr':
vsprintf.c:(.text+0x337): multiple definition of `strchr'
traps.o:traps.c:(.text+0x254): first defined here
vsprintf.o: In function `strrchr':
vsprintf.c:(.text+0x356): multiple definition of `strrchr'
traps.o:traps.c:(.text+0x273): first defined here
vsprintf.o: In function `strspn':
vsprintf.c:(.text+0x377): multiple definition of `strspn'
traps.o:traps.c:(.text+0x294): first defined here
vsprintf.o: In function `strcspn':
vsprintf.c:(.text+0x3aa): multiple definition of `strcspn'
traps.o:traps.c:(.text+0x2c7): first defined here
vsprintf.o: In function `strpbrk':
vsprintf.c:(.text+0x3dd): multiple definition of `strpbrk'
traps.o:traps.c:(.text+0x2fa): first defined here
vsprintf.o: In function `strstr':
vsprintf.c:(.text+0x410): multiple definition of `strstr'
traps.o:traps.c:(.text+0x32d): first defined here
vsprintf.o: In function `strlen':
vsprintf.c:(.text+0x443): multiple definition of `strlen'
traps.o:traps.c:(.text+0x360): first defined here
vsprintf.o: In function `strtok':
vsprintf.c:(.text+0x45c): multiple definition of `strtok'
traps.o:traps.c:(.text+0x379): first defined here
vsprintf.o: In function `memcpy':
vsprintf.c:(.text+0x4e3): multiple definition of `memcpy'
traps.o:traps.c:(.text+0x400): first defined here
vsprintf.o: In function `memmove':
vsprintf.c:(.text+0x4f9): multiple definition of `memmove'
traps.o:traps.c:(.text+0x416): first defined here
vsprintf.o: In function `memcmp':
vsprintf.c:(.text+0x51f): multiple definition of `memcmp'
traps.o:traps.c:(.text+0x43c): first defined here
vsprintf.o: In function `memchr':
vsprintf.c:(.text+0x543): multiple definition of `memchr'
traps.o:traps.c:(.text+0x460): first defined here
vsprintf.o: In function `memset':
vsprintf.c:(.text+0x56a): multiple definition of `memset'
traps.o:traps.c:(.text+0x487): first defined here
sys.o: In function `oom':
sys.c:(.text+0x0): multiple definition of `oom'
sched.o:sched.c:(.text+0x115): first defined here
sys.o: In function `get_fs_byte':
sys.c:(.text+0x2c): multiple definition of `get_fs_byte'
sched.o:sched.c:(.text+0x141): first defined here
sys.o: In function `get_fs_word':
sys.c:(.text+0x34): multiple definition of `get_fs_word'
sched.o:sched.c:(.text+0x149): first defined here
sys.o: In function `get_fs_long':
sys.c:(.text+0x3d): multiple definition of `get_fs_long'
sched.o:sched.c:(.text+0x152): first defined here
sys.o: In function `put_fs_byte':
sys.c:(.text+0x45): multiple definition of `put_fs_byte'
sched.o:sched.c:(.text+0x15a): first defined here
sys.o: In function `put_fs_word':
sys.c:(.text+0x51): multiple definition of `put_fs_word'
sched.o:sched.c:(.text+0x166): first defined here
sys.o: In function `put_fs_long':
sys.c:(.text+0x5e): multiple definition of `put_fs_long'
sched.o:sched.c:(.text+0x173): first defined here
sys.o: In function `get_fs':
sys.c:(.text+0x6a): multiple definition of `get_fs'
sched.o:sched.c:(.text+0x17f): first defined here
sys.o: In function `get_ds':
sys.c:(.text+0x71): multiple definition of `get_ds'
sched.o:sched.c:(.text+0x186): first defined here
sys.o: In function `set_fs':
sys.c:(.text+0x78): multiple definition of `set_fs'
sched.o:sched.c:(.text+0x18d): first defined here
sys.o: In function `strcpy':
sys.c:(.text+0x7f): multiple definition of `strcpy'
traps.o:traps.c:(.text+0x182): first defined here
sys.o: In function `strncpy':
sys.c:(.text+0x95): multiple definition of `strncpy'
traps.o:traps.c:(.text+0x198): first defined here
sys.o: In function `strcat':
sys.c:(.text+0xb4): multiple definition of `strcat'
traps.o:traps.c:(.text+0x1b7): first defined here
sys.o: In function `strncat':
sys.c:(.text+0xd7): multiple definition of `strncat'
traps.o:traps.c:(.text+0x1da): first defined here
sys.o: In function `strcmp':
sys.c:(.text+0x104): multiple definition of `strcmp'
traps.o:traps.c:(.text+0x207): first defined here
sys.o: In function `strncmp':
sys.c:(.text+0x127): multiple definition of `strncmp'
traps.o:traps.c:(.text+0x22a): first defined here
sys.o: In function `strchr':
sys.c:(.text+0x151): multiple definition of `strchr'
traps.o:traps.c:(.text+0x254): first defined here
sys.o: In function `strrchr':
sys.c:(.text+0x170): multiple definition of `strrchr'
traps.o:traps.c:(.text+0x273): first defined here
sys.o: In function `strspn':
sys.c:(.text+0x191): multiple definition of `strspn'
traps.o:traps.c:(.text+0x294): first defined here
sys.o: In function `strcspn':
sys.c:(.text+0x1c4): multiple definition of `strcspn'
traps.o:traps.c:(.text+0x2c7): first defined here
sys.o: In function `strpbrk':
sys.c:(.text+0x1f7): multiple definition of `strpbrk'
traps.o:traps.c:(.text+0x2fa): first defined here
sys.o: In function `strstr':
sys.c:(.text+0x22a): multiple definition of `strstr'
traps.o:traps.c:(.text+0x32d): first defined here
sys.o: In function `strlen':
sys.c:(.text+0x25d): multiple definition of `strlen'
traps.o:traps.c:(.text+0x360): first defined here
sys.o: In function `strtok':
sys.c:(.text+0x276): multiple definition of `strtok'
traps.o:traps.c:(.text+0x379): first defined here
sys.o: In function `memcpy':
sys.c:(.text+0x2fd): multiple definition of `memcpy'
traps.o:traps.c:(.text+0x400): first defined here
sys.o: In function `memmove':
sys.c:(.text+0x313): multiple definition of `memmove'
traps.o:traps.c:(.text+0x416): first defined here
sys.o: In function `memcmp':
sys.c:(.text+0x339): multiple definition of `memcmp'
traps.o:traps.c:(.text+0x43c): first defined here
sys.o: In function `memchr':
sys.c:(.text+0x35d): multiple definition of `memchr'
traps.o:traps.c:(.text+0x460): first defined here
sys.o: In function `memset':
sys.c:(.text+0x384): multiple definition of `memset'
traps.o:traps.c:(.text+0x487): first defined here
exit.o: In function `get_fs_byte':
exit.c:(.text+0x44): multiple definition of `get_fs_byte'
sched.o:sched.c:(.text+0x141): first defined here
exit.o: In function `get_fs_word':
exit.c:(.text+0x4c): multiple definition of `get_fs_word'
sched.o:sched.c:(.text+0x149): first defined here
exit.o: In function `get_fs_long':
exit.c:(.text+0x55): multiple definition of `get_fs_long'
sched.o:sched.c:(.text+0x152): first defined here
exit.o: In function `put_fs_byte':
exit.c:(.text+0x5d): multiple definition of `put_fs_byte'
sched.o:sched.c:(.text+0x15a): first defined here
exit.o: In function `put_fs_word':
exit.c:(.text+0x69): multiple definition of `put_fs_word'
sched.o:sched.c:(.text+0x166): first defined here
exit.o: In function `put_fs_long':
exit.c:(.text+0x76): multiple definition of `put_fs_long'
sched.o:sched.c:(.text+0x173): first defined here
exit.o: In function `get_fs':
exit.c:(.text+0x82): multiple definition of `get_fs'
sched.o:sched.c:(.text+0x17f): first defined here
exit.o: In function `get_ds':
exit.c:(.text+0x89): multiple definition of `get_ds'
sched.o:sched.c:(.text+0x186): first defined here
exit.o: In function `set_fs':
exit.c:(.text+0x90): multiple definition of `set_fs'
sched.o:sched.c:(.text+0x18d): first defined here
exit.o: In function `oom':
exit.c:(.text+0xc6d): multiple definition of `oom'
sched.o:sched.c:(.text+0x115): first defined here
signal.o: In function `oom':
signal.c:(.text+0x0): multiple definition of `oom'
sched.o:sched.c:(.text+0x115): first defined here
signal.o: In function `get_fs_byte':
signal.c:(.text+0x2c): multiple definition of `get_fs_byte'
sched.o:sched.c:(.text+0x141): first defined here
signal.o: In function `get_fs_word':
signal.c:(.text+0x34): multiple definition of `get_fs_word'
sched.o:sched.c:(.text+0x149): first defined here
signal.o: In function `get_fs_long':
signal.c:(.text+0x3d): multiple definition of `get_fs_long'
sched.o:sched.c:(.text+0x152): first defined here
signal.o: In function `put_fs_byte':
signal.c:(.text+0x45): multiple definition of `put_fs_byte'
sched.o:sched.c:(.text+0x15a): first defined here
signal.o: In function `put_fs_word':
signal.c:(.text+0x51): multiple definition of `put_fs_word'
sched.o:sched.c:(.text+0x166): first defined here
signal.o: In function `put_fs_long':
signal.c:(.text+0x5e): multiple definition of `put_fs_long'
sched.o:sched.c:(.text+0x173): first defined here
signal.o: In function `get_fs':
signal.c:(.text+0x6a): multiple definition of `get_fs'
sched.o:sched.c:(.text+0x17f): first defined here
signal.o: In function `get_ds':
signal.c:(.text+0x71): multiple definition of `get_ds'
sched.o:sched.c:(.text+0x186): first defined here
signal.o: In function `set_fs':
signal.c:(.text+0x78): multiple definition of `set_fs'
sched.o:sched.c:(.text+0x18d): first defined here
ld: Relocatable linking with relocations from format elf32-i386 (sched.o) to format elf64-x86-64 (kernel.o) is not supported
Makefile:32: recipe for target 'kernel.o' failed
make[1]: *** [kernel.o] Error 1
make[1]: Leaving directory '/home/chenhao/chenhao/linux0.12/linux-0.12/kernel'
Makefile:75: recipe for target 'kernel/kernel.o' failed
make: *** [kernel/kernel.o] Error 2

函數重定義錯誤,全局搜索對應錯誤函數,
extern 改爲 static

gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o signal.o signal.c
ld -r -o kernel.o sched.o sys_call.o traps.o asm.o fork.o panic.o printk.o vsprintf.o sys.o exit.o signal.o mktime.o
ld: Relocatable linking with relocations from format elf32-i386 (sched.o) to format elf64-x86-64 (kernel.o) is not supported
Makefile:32: recipe for target 'kernel.o' failed
make[1]: *** [kernel.o] Error 1
make[1]: Leaving directory '/home/chenhao/chenhao/linux0.12/linux-0.12/kernel'
Makefile:75: recipe for target 'kernel/kernel.o' failed
make: *** [kernel/kernel.o] Error 2
  1. 鏈接錯誤
    ld -r -o kernel.o sched.o system_call.o traps.o asm.o fork.o panic.o printk.o vsprintf.o sys.o exit.o signal.o mktime.o
    ld: Relocatable linking with relocations from format elf32-i386 (sched.o) to format elf64-x86-64 (kernel.o) is not supported
    make[1]: *** [kernel.o] Error 1
    解決辦法,kernel目錄下,在x86-64上鍊接出x86 文件,添加 -m elf_i386 選項
    ld -m elf_i386 -r -o kernel.o sched.o system_call.o traps.o asm.o fork.o panic.o printk.o vsprintf.o sys.o exit.o signal.o mktime.o
file_dev.c:46:1: error: unsupported size for integer register
 }
 ^
Makefile:13: recipe for target 'file_dev.o' failed
make[1]: *** [file_dev.o] Error 1
make[1]: Leaving directory '/home/chenhao/chenhao/linux0.12/linux-0.12/fs'
Makefile:81: recipe for target 'fs/fs.o' failed
make: *** [fs/fs.o] Error

原因未知,暫時屏蔽掉這個函數的內容。put_BCD;get_BCD;tty_write

exec.c:162:44: error: lvalue required as left operand of assignment
         !(pag = (char *) page[p/PAGE_SIZE] =
                                            ^
exec.c: In function ‘do_execve’:
exec.c:264:7: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (cp = strchr(buf, '\n')) {
       ^~
In file included from exec.c:22:0:
exec.c: At top level:
../include/string.h:13:15: warning: ‘strerror’ declared ‘static’ but never defined [-Wunused-function]
 static char * strerror(int errno);
               ^~~~~~~~
Makefile:13: recipe for target 'exec.o' failed
make[1]: *** [exec.o] Error 1
make[1]: Leaving directory '/home/chenhao/chenhao/linux0.12/linux-0.12/fs'
Makefile:81: recipe for target 'fs/fs.o' failed
make: *** [fs/fs.o] Error 2

代碼有問題,

				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()))   
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o malloc.o malloc.c
malloc.c: In function ‘malloc’:
malloc.c:156:46: error: lvalue required as left operand of assignment
   bdesc->page = bdesc->freeptr = (void *) cp = get_free_page();
                                              ^
Makefile:24: recipe for target 'malloc.o' failed
make[1]: *** [malloc.o] Error 1
make[1]: Leaving directory '/home/chenhao/chenhao/linux0.12/linux-0.12/lib'
Makefile:84: recipe for target 'lib/lib.a' failed
make: *** [lib/lib.a] Error 2

代碼錯誤,

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

更改成

156                 cp = get_free_page();
157                 bdesc->freeptr = cp;
158                 bdesc->page = bdesc->freeptr;
chenhao@chenhao-VirtualBox:~/chenhao/linux0.12/linux-0.12$ make
(cd lib; make)
make[1]: Entering directory '/home/chenhao/chenhao/linux0.12/linux-0.12/lib'
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -I../include -m32 \
-c -o malloc.o malloc.c
malloc.c: In function ‘malloc’:
malloc.c:156:6: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
   cp = get_free_page();
      ^
malloc.c: In function ‘free_s’:
malloc.c:215:21: warning: ‘prev’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   if ((prev && (prev->next != bdesc)) ||
                 ~~~~^~~~~~
ar rcs lib.a ctype.o _exit.o open.o close.o errno.o write.o dup.o setsid.o execve.o wait.o string.o malloc.o
sync
make[1]: Leaving directory '/home/chenhao/chenhao/linux0.12/linux-0.12/lib'
ld -s -x -M boot/head.o init/main.o \
kernel/kernel.o mm/mm.o fs/fs.o \
kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.a \
kernel/math/math.a \
lib/lib.a \
-o tools/system > System.map
kernel/blk_drv/blk_drv.a(hd.o): In function `unlock_buffer':
hd.c:(.text+0x93f): multiple definition of `unlock_buffer'
kernel/blk_drv/blk_drv.a(floppy.o):floppy.c:(.text+0x757): first defined here
kernel/blk_drv/blk_drv.a(hd.o): In function `end_request':
hd.c:(.text+0x96b): multiple definition of `end_request'
kernel/blk_drv/blk_drv.a(floppy.o):floppy.c:(.text+0x783): first defined here
kernel/blk_drv/blk_drv.a(ramdisk.o): In function `unlock_buffer':
ramdisk.c:(.text+0x2a9): multiple definition of `unlock_buffer'
kernel/blk_drv/blk_drv.a(floppy.o):floppy.c:(.text+0x757): first defined here
kernel/blk_drv/blk_drv.a(ramdisk.o): In function `end_request':
ramdisk.c:(.text+0x2d5): multiple definition of `end_request'
kernel/blk_drv/blk_drv.a(floppy.o):floppy.c:(.text+0x783): first defined here
ld: i386 architecture of input file `boot/head.o' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `init/main.o' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `kernel/kernel.o' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `mm/mm.o' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `fs/fs.o' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `kernel/chr_drv/chr_drv.a(console.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `kernel/chr_drv/chr_drv.a(serial.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `kernel/chr_drv/chr_drv.a(tty_ioctl.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `kernel/chr_drv/chr_drv.a(pty.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(ctype.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(_exit.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(open.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(close.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(errno.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(write.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(dup.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(setsid.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(execve.o)' is incompatible with i386:x86-64 output
ld: i386 architecture of input file `lib/lib.a(wait.o)' is incompatible with i386:x86-64 output
ld: warning: cannot find entry symbol _start; defaulting to 0000000000400120
boot/head.o: In function `startup_32':
(.text+0x10): undefined reference to `_stack_start'
(.text+0x2e): undefined reference to `_stack_start'
boot/head.o: In function `after_page_tables':
(.text+0x540c): undefined reference to `_main'
boot/head.o: In function `ignore_int':
(.text+0x5440): undefined reference to `_printk'
init/main.o: In function `init':
main.c:(.text+0x252): undefined reference to `puts'
kernel/kernel.o: In function `schedule':
(.text+0x3f3): undefined reference to `_current'
(.text+0x400): undefined reference to `_current'
(.text+0x40a): undefined reference to `_last_task_used_math'
(.text+0x428): undefined reference to `__stack_chk_fail_local'
kernel/kernel.o: In function `sched_init':
(.text+0x9d3): undefined reference to `gdt'
(.text+0xa98): undefined reference to `idt'
(.text+0xaa3): undefined reference to `timer_interrupt'
(.text+0xace): undefined reference to `system_call'
kernel/kernel.o: In function `reschedule':
sys_call.o:(.text+0xaf4): undefined reference to `_schedule'
kernel/kernel.o: In function `_system_call':
(.text+0xb12): undefined reference to `_NR_syscalls'
(.text+0xb1b): undefined reference to `_sys_call_table'
(.text+0xb21): undefined reference to `_current'
kernel/kernel.o: In function `ret_from_sys_call':
sys_call.o:(.text+0xb31): undefined reference to `_current'
sys_call.o:(.text+0xb37): undefined reference to `_task'
sys_call.o:(.text+0xb68): undefined reference to `_do_signal'
kernel/kernel.o: In function `_coprocessor_error':
(.text+0xb9e): undefined reference to `_math_error'
kernel/kernel.o: In function `_device_not_available':
(.text+0xbcd): undefined reference to `_math_state_restore'
(.text+0xbd7): undefined reference to `_math_emulate'
kernel/kernel.o: In function `_timer_interrupt':
(.text+0xbfe): undefined reference to `_jiffies'
(.text+0xc0f): undefined reference to `_do_timer'
kernel/kernel.o: In function `_sys_execve':
(.text+0xc22): undefined reference to `_do_execve'
kernel/kernel.o: In function `_sys_fork':
(.text+0xc2b): undefined reference to `_find_empty_process'
(.text+0xc3a): undefined reference to `_copy_process'
kernel/kernel.o: In function `_hd_interrupt':
(.text+0xc65): undefined reference to `_hd_timeout'
(.text+0xc6b): undefined reference to `_do_hd'
(.text+0xc74): undefined reference to `_unexpected_hd_interrupt'
kernel/kernel.o: In function `_floppy_interrupt':
(.text+0xca3): undefined reference to `_do_floppy'
(.text+0xcac): undefined reference to `_unexpected_floppy_interrupt'
kernel/kernel.o: In function `trap_init':
(.text+0x113a): undefined reference to `idt'
(.text+0x1145): undefined reference to `divide_error'
(.text+0x1157): undefined reference to `debug'
(.text+0x116a): undefined reference to `nmi'
(.text+0x117d): undefined reference to `int3'
(.text+0x1190): undefined reference to `overflow'
(.text+0x11a3): undefined reference to `bounds'
(.text+0x11b6): undefined reference to `invalid_op'
(.text+0x11c9): undefined reference to `device_not_available'
(.text+0x11dc): undefined reference to `double_fault'
(.text+0x11ef): undefined reference to `coprocessor_segment_overrun'
(.text+0x1202): undefined reference to `invalid_TSS'
(.text+0x1215): undefined reference to `segment_not_present'
(.text+0x1228): undefined reference to `stack_segment'
(.text+0x123b): undefined reference to `general_protection'
(.text+0x124e): undefined reference to `page_fault'
(.text+0x1261): undefined reference to `reserved'
(.text+0x1274): undefined reference to `coprocessor_error'
(.text+0x128d): undefined reference to `alignment_check'
(.text+0x12b2): undefined reference to `reserved'
(.text+0x12cb): undefined reference to `idt'
(.text+0x12d6): undefined reference to `irq13'
(.text+0x1315): undefined reference to `parallel_interrupt'
kernel/kernel.o: In function `_divide_error':
(.text+0x1332): undefined reference to `_do_divide_error'
kernel/kernel.o: In function `_debug':
(.text+0x1367): undefined reference to `_do_int3'
kernel/kernel.o: In function `_nmi':
(.text+0x136e): undefined reference to `_do_nmi'
kernel/kernel.o: In function `_int3':
(.text+0x1375): undefined reference to `_do_int3'
kernel/kernel.o: In function `_overflow':
(.text+0x137c): undefined reference to `_do_overflow'
kernel/kernel.o: In function `_bounds':
(.text+0x1383): undefined reference to `_do_bounds'
kernel/kernel.o: In function `_invalid_op':
(.text+0x138a): undefined reference to `_do_invalid_op'
kernel/kernel.o: In function `_coprocessor_segment_overrun':
(.text+0x1391): undefined reference to `_do_coprocessor_segment_overrun'
kernel/kernel.o: In function `_reserved':
(.text+0x1398): undefined reference to `_do_reserved'
kernel/kernel.o: In function `_double_fault':
(.text+0x13b4): undefined reference to `_do_double_fault'
kernel/kernel.o: In function `_invalid_TSS':
(.text+0x13eb): undefined reference to `_do_invalid_TSS'
kernel/kernel.o: In function `_segment_not_present':
(.text+0x13f2): undefined reference to `_do_segment_not_present'
kernel/kernel.o: In function `_stack_segment':
(.text+0x13f9): undefined reference to `_do_stack_segment'
kernel/kernel.o: In function `_general_protection':
(.text+0x1400): undefined reference to `_do_general_protection'
kernel/kernel.o: In function `_alignment_check':
(.text+0x1407): undefined reference to `_do_alignment_check'
kernel/kernel.o: In function `copy_process':
(.text+0x17d8): undefined reference to `gdt'
kernel/kernel.o: In function `number':
vsprintf.c:(.text+0x1beb): undefined reference to `__stack_chk_fail_local'
kernel/kernel.o: In function `sys_getrusage':
(.text+0x29b6): undefined reference to `__stack_chk_fail_local'
kernel/kernel.o: In function `sys_sigaction':
(.text+0x3b79): undefined reference to `__stack_chk_fail_local'
mm/mm.o: In function `do_no_page':
(.text+0xa0b): undefined reference to `__stack_chk_fail_local'
mm/mm.o: In function `show_mem':
(.text+0xb0c): undefined reference to `pg_dir'
mm/mm.o: In function `swap_out':
(.text+0xda7): undefined reference to `pg_dir'
(.text+0xdbf): undefined reference to `pg_dir'
(.text+0xdf5): undefined reference to `pg_dir'
mm/mm.o: In function `_page_fault':
(.text+0x1231): undefined reference to `_do_no_page'
(.text+0x1238): undefined reference to `_do_wp_page'
fs/fs.o: In function `sys_open':
(.text+0x5a6): undefined reference to `__stack_chk_fail_local'
fs/fs.o: In function `bread_page':
(.text+0x1c87): undefined reference to `__stack_chk_fail_local'
fs/fs.o: In function `cp_stat':
stat.c:(.text+0x2cc6): undefined reference to `__stack_chk_fail_local'
fs/fs.o: In function `do_execve':
(.text+0x3a49): undefined reference to `__stack_chk_fail_local'
fs/fs.o: In function `sys_pipe':
(.text+0x3e50): undefined reference to `__stack_chk_fail_local'
fs/fs.o:namei.c:(.text+0x42f4): more undefined references to `__stack_chk_fail_local' follow
kernel/blk_drv/blk_drv.a(floppy.o): In function `setup_DMA':
floppy.c:(.text+0x18): undefined reference to `tmp_floppy_area'
kernel/blk_drv/blk_drv.a(floppy.o): In function `rw_interrupt':
floppy.c:(.text+0xccd): undefined reference to `tmp_floppy_area'
kernel/blk_drv/blk_drv.a(floppy.o): In function `floppy_init':
floppy.c:(.text+0xdc8): undefined reference to `floppy_interrupt'
floppy.c:(.text+0xdda): undefined reference to `idt'
floppy.c:(.text+0xde0): undefined reference to `idt'
kernel/blk_drv/blk_drv.a(hd.o): In function `hd_init':
hd.c:(.text+0xe97): undefined reference to `hd_interrupt'
hd.c:(.text+0xea9): undefined reference to `idt'
hd.c:(.text+0xeaf): undefined reference to `idt'
kernel/chr_drv/chr_drv.a(console.o): In function `scrup':
console.c:(.text+0x8f): undefined reference to `_video_num_columns'
console.c:(.text+0xf2): undefined reference to `_video_num_columns'
console.c:(.text+0x174): undefined reference to `_video_num_columns'
kernel/chr_drv/chr_drv.a(console.o): In function `scrdown':
console.c:(.text+0x2b0): undefined reference to `_video_num_columns'
console.c:(.text+0x310): undefined reference to `_video_num_columns'
kernel/chr_drv/chr_drv.a(console.o): In function `con_init':
console.c:(.text+0x1c1e): undefined reference to `idt'
console.c:(.text+0x1c24): undefined reference to `keyboard_interrupt'
kernel/chr_drv/chr_drv.a(serial.o): In function `rs_init':
serial.c:(.text+0x67): undefined reference to `idt'
serial.c:(.text+0x6d): undefined reference to `rs1_interrupt'
serial.c:(.text+0x8b): undefined reference to `rs2_interrupt'
kernel/chr_drv/chr_drv.a(tty_ioctl.o): In function `.L43':
tty_ioctl.c:(.text+0x512): undefined reference to `__stack_chk_fail_local'
kernel/kernel.o:(.data.rel+0x3ec): undefined reference to `pg_dir'
kernel/kernel.o:(.data.rel+0x1028): undefined reference to `sys_fork'
kernel/kernel.o:(.data.rel+0x104c): undefined reference to `sys_execve'
Makefile:58: recipe for target 'tools/system' failed
make: *** [tools/system] Error 1
kernel/chr_drv/chr_drv.a(tty_ioctl.o): In function `.L43':
tty_ioctl.c:(.text+0x512): undefined reference to `__stack_chk_fail_local'
kernel/math/math.a(math_emulate.o): In function `.L117':
math_emulate.c:(.text+0x1758): undefined reference to `__stack_chk_fail_local'
kernel/math/math.a(get_put.o): In function `get_short_real':
get_put.c:(.text+0x5b): undefined reference to `__stack_chk_fail_local'
kernel/math/math.a(get_put.o): In function `get_long_real':
get_put.c:(.text+0xc2): undefined reference to `__stack_chk_fail_local'
kernel/math/math.a(get_put.o): In function `get_short_int':
get_put.c:(.text+0x17f): undefined reference to `__stack_chk_fail_local'
kernel/math/math.a(get_put.o):get_put.c:(.text+0x1fc): more undefined references to `__stack_chk_fail_local' follow
Makefile:58: recipe for target 'tools/system' failed
make: *** [tools/system] Error 1

新版gcc缺少堆棧保護選項,在所有的CFLAGS後面加上-fno-stack-protector選項。

-o tools/system > System.map
ld: warning: cannot find entry symbol _start; defaulting to 00000000080480b8
init/main.o: In function `init':
main.c:(.text+0x246): undefined reference to `puts'
Makefile:58: recipe for target 'tools/system' failed
make: *** [tools/system] Error 1

1.在主目錄下的Makefile的ld後面,添加-e startup_32 -Ttext 0選項。
2.main.c中的printf改個名稱。

gcc  -Wall -O -fstrength-reduce -fomit-frame-pointer -m32 -fno-stack-protector \
-o tools/build tools/build.c
In file included from tools/build.c:25:0:
/usr/include/stdio.h:27:10: fatal error: bits/libc-header-start.h: No such file or directory
 #include <bits/libc-header-start.h>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:51: recipe for target 'tools/build' failed
make: *** [tools/build] Error 1

安裝gcc庫:sudo apt-get install gcc-multilib

/tmp/ccYoJ062.o: In function `main':
build.c:(.text+0xed): undefined reference to `MAJOR'
build.c:(.text+0x107): undefined reference to `MINOR'
build.c:(.text+0x1a5): undefined reference to `MAJOR'
build.c:(.text+0x1bf): undefined reference to `MINOR'
collect2: error: ld returned 1 exit status
Makefile:51: recipe for target 'tools/build' failed
make: *** [tools/build] Error 1

build.c中包含的是標準庫的頭文件 /usr/include/linux/fs.h ,但是這個頭文件裏並沒有實現MAJOR和MINOR宏。解決方法很簡單,從include/linux/fs.h中把這兩個宏複製到build.c中即可.

gcc  -Wall -O -fstrength-reduce -fomit-frame-pointer -m32 -fno-stack-protector \
-o tools/build tools/build.c
tools/build boot/bootsect boot/setup tools/system /dev/hd6 \
	/dev/hd2 > Image
/dev/hd6: No such file or directory
Couldn't stat root device.
Makefile:43: recipe for target 'Image' failed
make: *** [Image] Error 1

這是因爲在源代碼頂層目錄的Makefile中所指定的根設備爲/dev/hd6(代表第二個硬盤的第一個分區), 而本機上並不存在這個設備所致。Linus當年之所以指定根設備爲/dev/hd6, 是因爲他把Linux 0.11安裝在了機子的第二塊硬盤上。我們這裏打算通過在bochs中模擬軟盤來啓動編譯好的系統,故在頂層目錄Makefile中設定根設備爲軟盤:

ROOT_DEV=FLOPPY
SWAP_DEV=

objcopy -O binary -R .note -R .comment tools/system tools/kernel
tools/build boot/bootsect boot/setup tools/kernel FLOPPY > Image
Root device is (0, 0)
Swap device is (0, 0)
Boot sector 512 bytes.
Setup is 1372 bytes.
Non-GCC header of 'system'
Makefile:49: recipe for target 'Image' failed
make: *** [Image] Error 1

將 tools/build.c 文件中的
if (((long *) buf)[5] != 0)
     die("Non-GCC header of 'system'");
這段代碼註釋掉
//if (((long *) buf)[5] != 0)
//  die("Non-GCC header of 'system'");
OUTPUT(tools/system elf32-i386)
nm tools/system | grep -v '\(compiled\)\|\(\.o$\)\|\( [aU] \)\|\(\.\.ng$\)\|\(LASH[RL]DI\)'| sort > System.map
nm: tools/system: no symbols
gcc  -Wall -O -fstrength-reduce -fomit-frame-pointer -m32 -fno-stack-protector \
-o tools/build tools/build.c
objcopy -O binary -R .note -R .comment tools/system tools/kernel
tools/build boot/bootsect boot/setup tools/kernel FLOPPY > Image
Root device is (0, 0)
Swap device is (0, 0)
Boot sector 512 bytes.
Setup is 1372 bytes.
System is 156161 bytes.
rm tools/kernel -f
sync

編譯成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章