Newbie question: kernel image a dynamically linked binary?

Oliver Fromme olli at lurza.secnetix.de
Thu Apr 1 12:01:47 UTC 2010


Hi,

Please don't crosspost to many lists.  This topic is probably
suitable for hackers@ but not for the other lists.

Daniel Rodrick <daniel.rodrick at gmail.com> wrote:
 > I'm a newbie and coming from Linux background, and am trying to learn
 > FreeBSD now. The first thing I find a little confusing is that the
 > final FreeBSD kernel image is shown as a DYNAMICALLY LINKED binary:
 > 
 > $
 > $ pwd
 > /boot/kernel
 > $
 > $ file kernel
 > kernel: ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD),
 > dynamically linked (uses shared libs), not stripped
 > $
 > 
 > How can the kernel image use shared libraries? And which ones does it
 > use, if any?
 > 
 > Also, I cannot find out the libraries the image uses using the
 > traditional ldd command:
 > 
 > $ ldd kernel
 > kernel:
 > kernel: signal 6
 > $

ldd works by actually executing the binary with a special
flag for rtld(1).  Compare:

$ ldd /bin/sh
/bin/sh:
        libedit.so.7 => /lib/libedit.so.7 (0x280a8000)
        libncurses.so.8 => /lib/libncurses.so.8 (0x280bd000)
        libc.so.7 => /lib/libc.so.7 (0x280fc000)

$ LD_TRACE_LOADED_OBJECTS=1 /bin/sh
        libedit.so.7 => /lib/libedit.so.7 (0x280a8000)
        libncurses.so.8 => /lib/libncurses.so.8 (0x280bd000)
        libc.so.7 => /lib/libc.so.7 (0x280fc000)

Of course you cannot execute the kernel (only the boot loader
knows how to load and boot the kernel), so ldd fails on the
kernel.

But you can use objdump(1) to list dynamic dependencies.

$ objdump -p /bin/sh | grep NEEDED
  NEEDED      libedit.so.7
  NEEDED      libncurses.so.8
  NEEDED      libc.so.7

$ objdump -p /boot/kernel/kernel | grep NEEDED
  NEEDED      hack.So

As far as I know, the kernel and all kernel modules need to
be dynamic binaries so the kernel linker works, which is
required for dynamically loading kernel modules.

So what is that "hack.So" object?  It's just a dummy that's
required for technical reasons.  You can see the details in
/sys/conf/kern.post.mk which contains this paragraph:

# This is a hack.  BFD "optimizes" away dynamic mode if there are no
# dynamic references.  We could probably do a '-Bforcedynamic' mode like
# in the a.out ld.  For now, this works.
HACK_EXTRA_FLAGS?= -shared
hack.So: Makefile
        :> hack.c
        ${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
        rm -f hack.c

 > Can some please throw some light?

I hope I did.  :-)

Best regards
   Oliver

-- 
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:  http://www.secnetix.de/bsd

"Unix gives you just enough rope to hang yourself --
and then a couple of more feet, just to be sure."
        -- Eric Allman


More information about the freebsd-hackers mailing list