Page 1 of 1

Problem with linking my kernel with libk.

Posted: Mon Aug 16, 2021 3:20 pm
by antoni
In libk.a file, there are many functions including printf, memset and itoa. However, when I try to link it with the kernel from BOOTBOOT example (mykernel/c/ in BOOTBOOT repository) which I modified to use printf function from my libk instead of puts defined in this file, I get this error:

(I use same linker script and I also link it with font.psf just like in BOOTBOOT example)

Code: Select all

x86_64-elf-gcc --sysroot=/home/<...>/sysroot -isystem=/usr/include -nostdlib -nostartfiles -T kernel.ld -nostdlib -lk -lgcc kernel/kernel.o rc/font.o utils/tty.o  -o sielawix.kernel
x86_64-elf-strip -s -K mmio -K fb -K bootboot -K environment -K initstack sielawix.kernel
/usr/local/lib/gcc/x86_64-elf/10.0.0/../../../../x86_64-elf/bin/ld: kernel/kernel.o: in function `_start':
kernel.c:(.text+0x1b0): undefined reference to `printf'
/usr/local/lib/gcc/x86_64-elf/10.0.0/../../../../x86_64-elf/bin/ld: utils/tty.o: in function `tty_clear':
tty.c:(.text+0xc0): undefined reference to `memset'
/usr/local/lib/gcc/x86_64-elf/10.0.0/../../../../x86_64-elf/bin/ld: utils/tty.o: in function `tty_puti':
tty.c:(.text+0x56f): undefined reference to `itoa'
/usr/local/lib/gcc/x86_64-elf/10.0.0/../../../../x86_64-elf/bin/ld: utils/tty.o: in function `tty_putl':
tty.c:(.text+0x59f): undefined reference to `itoa64'
collect2: error: ld returned 1 exit status
This is weird. I wanted to know is this a problem with my libk, linker script or command so I wrote such simple program in asm:

Code: Select all

BITS 64

global printf
printf:
        nop
        nop
I'm able to link this code with mentioned kernel without any problems, however, when I want to link my libk with it I get weird error. So this should be the problem with my libk, but I used it many times in my previous OS's and it worked...

"printf" and other required functions ARE in libk.a:

Code: Select all

readelf -s sysroot/usr/lib/libk.a 

File: sysroot/usr/lib/libk.a(printf.libk.o)

Symbol table '.symtab' contains 21 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS printf.c
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
     6: 0000000000000000     0 SECTION LOCAL  DEFAULT    7 
     7: 0000000000000000     0 SECTION LOCAL  DEFAULT    8 
     8: 0000000000000000     0 SECTION LOCAL  DEFAULT    9 
     9: 0000000000000000     0 SECTION LOCAL  DEFAULT   11 
    10: 0000000000000000     0 SECTION LOCAL  DEFAULT   12 
    11: 0000000000000000     0 SECTION LOCAL  DEFAULT   14 
    12: 0000000000000000     0 SECTION LOCAL  DEFAULT   16 
    13: 0000000000000000     0 SECTION LOCAL  DEFAULT   15 
    14: 0000000000000000  1129 FUNC    GLOBAL DEFAULT    1 vprintf
    15: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND strlen
    16: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND putchar
    17: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND itoa
    18: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND tty_puts
    19: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND itoa64
    20: 0000000000000470   148 FUNC    GLOBAL DEFAULT    1 printf

.
.
.
I tried many options. I compiled libk.a as PIC, like normal library, I tried different compiler and linker options but could not get this to work.

Here's my code: https://we.tl/t-o8GDiazQot
In order to compile it just run "make" command in root directory, but you need x86_64-elf cross compiler and toolchain.

Re: Problem with linking my kernel with libk.

Posted: Mon Aug 16, 2021 4:04 pm
by nexos
Where is -lk in your link line?

Re: Problem with linking my kernel with libk.

Posted: Tue Aug 17, 2021 12:24 am
by antoni
Where is -lk in your link line?
x86_64-elf-gcc <...> -nostartfiles -T kernel.ld -nostdlib -lk <...> -o sielawix.kernel

However, I also tried to link it without -lk flag, with this command:

Code: Select all

x86_64-elf-ld <my flags> <my objs> sysroot/usr/lib/libk.a -o sielawix.kernel
It failed too, with this same error message.

Re: Problem with linking my kernel with libk.

Posted: Tue Aug 17, 2021 12:45 am
by klange
Dependencies from archives must be listed after the object files that reference them. You have "-lk" before your kernel objects, move it to the end.

(Alternatively, you could not build an archive for your libk, or you could look into the --whole-archive linker flag)

Re: Problem with linking my kernel with libk.

Posted: Tue Aug 17, 2021 7:04 am
by antoni
Thanks a lot! It works now. It's quite a bit weird, anyway.