From owner-freebsd-hackers@FreeBSD.ORG Thu Jul 15 14:32:43 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1A33A1065670 for ; Thu, 15 Jul 2010 14:32:43 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.zoral.com.ua (mx0.zoral.com.ua [91.193.166.200]) by mx1.freebsd.org (Postfix) with ESMTP id 8FF698FC13 for ; Thu, 15 Jul 2010 14:32:42 +0000 (UTC) Received: from deviant.kiev.zoral.com.ua (root@deviant.kiev.zoral.com.ua [10.1.1.148]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id o6FEWZt0099227 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 15 Jul 2010 17:32:35 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.4/8.14.4) with ESMTP id o6FEWZUi005726; Thu, 15 Jul 2010 17:32:35 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.4/8.14.4/Submit) id o6FEWZPO005725; Thu, 15 Jul 2010 17:32:35 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Thu, 15 Jul 2010 17:32:35 +0300 From: Kostik Belousov To: Dmitry Krivenok Message-ID: <20100715143235.GU2381@deviant.kiev.zoral.com.ua> References: Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="LtN0I0gXzHUftVCi" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: clamav-milter 0.95.2 at skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-2.3 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_50, DNS_FROM_OPENWHOIS autolearn=no version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua Cc: freebsd-hackers@freebsd.org Subject: Re: Kernel linker and undefined references in KLD X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Jul 2010 14:32:43 -0000 --LtN0I0gXzHUftVCi Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Jul 15, 2010 at 06:07:36PM +0400, Dmitry Krivenok wrote: > Hello Hackers, >=20 > I have a question about kernel linker. > Please take a look at an example of simple module: >=20 > #########################################################################= ### > #include > #include > #include > #include > #include > #include > #include >=20 > /* DECLARING A FUNCTION JUST TO AVOID WARNING WHICH IS TREAT AS ERROR > BY DEFAULT */ > void seltdinit(struct thread* t); >=20 > static int event_handler(struct module *module, int event, void *arg) > { > int e =3D 0; > switch (event) > { > case MOD_LOAD: > /* CALLING A FUNCTION DECLARED AS STATIC IN kern/sys_generic.c */ > seltdinit(curthread); > break; > case MOD_QUIESCE: > break; > case MOD_UNLOAD: > break; > case MOD_SHUTDOWN: > break; > default: > e =3D EOPNOTSUPP; > break; > } >=20 > return(e); > }; > #########################################################################= ### >=20 > As you can see, this module calls seltdinit function declared as > _static_ in kern/sys_generic.c file. > I added a declaration of seltdinit function w/o static just to avoid > compilation error since -Werror is used > by default in FreeBSD. >=20 > Then I successfully built the module: >=20 > $ make > Warning: Object directory not changed from original > /usr/home/krived/work/freebsd/trouble > cc -O2 -pipe -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE > -nostdinc -I. -I@ -I@/contrib/altq -finline-limit=3D8000 --param > inline-unit-growth=3D100 --param large-function-growth=3D1000 -fno-common > -fno-omit-frame-pointer -mcmodel=3Dkernel -mno-red-zone -mfpmath=3D387 > -mno-sse -mno-sse2 -mno-sse3 -mno-mmx -mno-3dnow -msoft-float > -fno-asynchronous-unwind-tables -ffreestanding -fstack-protector > -std=3Diso9899:1999 -fstack-protector -Wall -Wredundant-decls > -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes > -Wpointer-arith -Winline -Wcast-qual -Wundef -Wno-pointer-sign > -fformat-extensions -c trouble.c > ld -d -warn-common -r -d -o trouble.ko trouble.o > :> export_syms > awk -f /sys/conf/kmod_syms.awk trouble.ko export_syms | xargs -J% > objcopy % trouble.ko > objcopy --strip-debug trouble.ko > $ >=20 > As expected, seltdinit symbol is undefined in trouble.ko file: > $ nm trouble.ko | grep seltdinit > U seltdinit > $ > and is local in kernel binary file: > $ nm /boot/kernel/kernel | grep seltdinit > ffffffff805fda50 t seltdinit > $ >=20 > I expected to get something like "undefined reference to seltdinit" > error, but the module was loaded w/o any errors: >=20 > $ sudo make load > /sbin/kldload -v /usr/home/krived/work/freebsd/trouble/trouble.ko > Loaded /usr/home/krived/work/freebsd/trouble/trouble.ko, id=3D2 > $ kldstat | grep trouble > 2 1 0xffffffff81212000 126 trouble.ko > $ sudo make unload > /sbin/kldunload -v trouble.ko > Unloading trouble.ko, id=3D2 > $ >=20 > Could you please explain why the linker (or whatever is loading > modules in FreeBSD) doesn't complain? >=20 > For example, ld tool complains for the similar user-space example: >=20 > #########################################################################= ### > $ cat seltdinit.c > static void seltdinit(void* td) > { > return; > } > $ cat main.c > void seltdinit(void* td); >=20 > int main() > { > seltdinit(0); > return 0; > } > $ gcc -Wall -c seltdinit.c > seltdinit.c:2: warning: 'seltdinit' defined but not used > $ gcc -Wall -c main.c > $ gcc seltdinit.o main.o -o sel > main.o: In function `main': > main.c:(.text+0xa): undefined reference to `seltdinit' > collect2: ld returned 1 exit status > $ > #########################################################################= ### >=20 > Thanks in advance! The kernel linker ignores weak attribute of the symbol, as you see. There is more bugs in this department, in regard of the list of exported symbols from the modules. I have a patch that fixes the issues, but I am leery to commit it, since the fix effectively breaks significant set of the modules. --LtN0I0gXzHUftVCi Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (FreeBSD) iEYEARECAAYFAkw/HAMACgkQC3+MBN1Mb4gveQCePvcou0othBN8JqHvC+nZIcsC 29AAoLf+OjpT934Bl0mSXn47IeieDuU9 =tz+7 -----END PGP SIGNATURE----- --LtN0I0gXzHUftVCi--