Problem with linking my kernel with libk.

Programming, for all ages and all languages.
Post Reply
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

Problem with linking my kernel with libk.

Post 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.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Problem with linking my kernel with libk.

Post by nexos »

Where is -lk in your link line?
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

Re: Problem with linking my kernel with libk.

Post 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.
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

Re: Problem with linking my kernel with libk.

Post 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)
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

Re: Problem with linking my kernel with libk.

Post by antoni »

Thanks a lot! It works now. It's quite a bit weird, anyway.
Post Reply