OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:49 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Problem with linking my kernel with libk.
PostPosted: Mon Aug 16, 2021 3:20 pm 
Offline
Member
Member

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


Top
 Profile  
 
 Post subject: Re: Problem with linking my kernel with libk.
PostPosted: Mon Aug 16, 2021 4:04 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 1071
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


Top
 Profile  
 
 Post subject: Re: Problem with linking my kernel with libk.
PostPosted: Tue Aug 17, 2021 12:24 am 
Offline
Member
Member

Joined: Sun May 24, 2020 9:11 am
Posts: 61
Location: /dev/null
Quote:
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:
x86_64-elf-ld <my flags> <my objs> sysroot/usr/lib/libk.a -o sielawix.kernel


It failed too, with this same error message.


Top
 Profile  
 
 Post subject: Re: Problem with linking my kernel with libk.
PostPosted: Tue Aug 17, 2021 12:45 am 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
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)

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: Problem with linking my kernel with libk.
PostPosted: Tue Aug 17, 2021 7:04 am 
Offline
Member
Member

Joined: Sun May 24, 2020 9:11 am
Posts: 61
Location: /dev/null
Thanks a lot! It works now. It's quite a bit weird, anyway.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group