From owner-freebsd-arch@FreeBSD.ORG Thu Apr 1 12:01:47 2010 Return-Path: Delivered-To: freebsd-arch@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E06171065678; Thu, 1 Apr 2010 12:01:46 +0000 (UTC) (envelope-from olli@lurza.secnetix.de) Received: from lurza.secnetix.de (lurza.secnetix.de [IPv6:2a01:170:102f::2]) by mx1.freebsd.org (Postfix) with ESMTP id 5BB688FC22; Thu, 1 Apr 2010 12:01:46 +0000 (UTC) Received: from lurza.secnetix.de (localhost [127.0.0.1]) by lurza.secnetix.de (8.14.3/8.14.3) with ESMTP id o31C1TZ6045279; Thu, 1 Apr 2010 14:01:44 +0200 (CEST) (envelope-from oliver.fromme@secnetix.de) Received: (from olli@localhost) by lurza.secnetix.de (8.14.3/8.14.3/Submit) id o31C1TRm045278; Thu, 1 Apr 2010 14:01:29 +0200 (CEST) (envelope-from olli) Date: Thu, 1 Apr 2010 14:01:29 +0200 (CEST) Message-Id: <201004011201.o31C1TRm045278@lurza.secnetix.de> From: Oliver Fromme To: freebsd-hackers@FreeBSD.ORG, daniel.rodrick@gmail.com, freebsd-drivers@FreeBSD.ORG, freebsd-arch@FreeBSD.ORG In-Reply-To: X-Newsgroups: list.freebsd-arch User-Agent: tin/1.8.3-20070201 ("Scotasay") (UNIX) (FreeBSD/6.4-PRERELEASE-20080904 (i386)) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-2.1.2 (lurza.secnetix.de [127.0.0.1]); Thu, 01 Apr 2010 14:01:45 +0200 (CEST) Cc: Subject: Re: Newbie question: kernel image a dynamically linked binary? X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: freebsd-hackers@FreeBSD.ORG, daniel.rodrick@gmail.com List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Apr 2010 12:01:47 -0000 Hi, Please don't crosspost to many lists. This topic is probably suitable for hackers@ but not for the other lists. Daniel Rodrick 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