Page 1 of 1

Kernel source not linking against sysroot libs

Posted: Wed Oct 08, 2014 12:49 pm
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?

Re: Kernel source not linking against sysroot libs

Posted: Wed Oct 08, 2014 1:51 pm
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.

Re: Kernel source not linking against sysroot libs

Posted: Wed Oct 08, 2014 2:05 pm
by cormacobrien
This does indeed work. I was under the impression that gcc would automatically link the libc. Thanks for your help!

Re: Kernel source not linking against sysroot libs

Posted: Wed Oct 08, 2014 2:35 pm
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.

Re: Kernel source not linking against sysroot libs

Posted: Wed Oct 08, 2014 2:38 pm
by iocoder
sortie wrote:Do not link libg or libc into your kernel. Just libk.
Right! Sorry for this mistake