Kernel source not linking against sysroot libs

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
cormacobrien
Posts: 13
Joined: Thu Oct 02, 2014 6:24 pm
Location: Chicago

Kernel source not linking against sysroot libs

Post by cormacobrien »

Git repo is here

I've been loosely following the "Meaty Skeleton" tutorial and this is the first big issue I can't figure out how to solve. My src/kernel/kernel.c relies on strlen(), and since I was carrying over from the Bare Bones tutorial I originally included that function's definition in kernel.c. The project is now reorganized such that src/libc/include/string.h declares strlen(), and src/libc/string/strlen.c defines it. strlen.o gets archived into libk.a with the other standard library functions, so the kernel source should reference that library, if I understand correctly.

kernel.c has

Code: Select all

#include <string.h>
and is referencing it from sysroot/include/string.h, but when I comment out the definition of strlen() in kernel.c I get an undefined reference error. I've looked over my Makefile, and, as far as I can tell, I use the same method for compiling the libc source into libk.a that the tutorial does. Can anyone help me out?
Find me on Github and Bitbucket.
User avatar
iocoder
Member
Member
Posts: 208
Joined: Sun Oct 18, 2009 5:47 pm
Libera.chat IRC: iocoder
Location: Alexandria, Egypt | Ottawa, Canada
Contact:

Re: Kernel source not linking against sysroot libs

Post by iocoder »

Looking at your Makefile, I can't find any line where you link the libraries at sysroot/usr/lib to your kernel.

When I modify the os-0.bin target to this:

Code: Select all

os-0.bin: $(OBJ_LINK_LIST)
	$(CC) -T $(ARCH_DIR)/linker.ld -o $@ $(LDFLAGS) $^ -L sysroot/usr/lib -lk -lg -lc
Things seem to work.
User avatar
cormacobrien
Posts: 13
Joined: Thu Oct 02, 2014 6:24 pm
Location: Chicago

Re: Kernel source not linking against sysroot libs

Post by cormacobrien »

This does indeed work. I was under the impression that gcc would automatically link the libc. Thanks for your help!
Find me on Github and Bitbucket.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Kernel source not linking against sysroot libs

Post by sortie »

Do not link libg or libc into your kernel. Just libk.

Do note how -nostdlib deliberately implies -nostartfiles and -nodefaultlibs. This is intentional. The libc must be compiled specifically as kernel code, at least on particular architectures.
User avatar
iocoder
Member
Member
Posts: 208
Joined: Sun Oct 18, 2009 5:47 pm
Libera.chat IRC: iocoder
Location: Alexandria, Egypt | Ottawa, Canada
Contact:

Re: Kernel source not linking against sysroot libs

Post by iocoder »

sortie wrote:Do not link libg or libc into your kernel. Just libk.
Right! Sorry for this mistake
Post Reply