Date: Thu, 15 Jul 2010 17:32:35 +0300 From: Kostik Belousov <kostikbel@gmail.com> To: Dmitry Krivenok <krivenok.dmitry@gmail.com> Cc: freebsd-hackers@freebsd.org Subject: Re: Kernel linker and undefined references in KLD Message-ID: <20100715143235.GU2381@deviant.kiev.zoral.com.ua> In-Reply-To: <AANLkTikhrbdbBEFl-I97nBzdvZx6qBaafDUsveRmYyp3@mail.gmail.com> References: <AANLkTikhrbdbBEFl-I97nBzdvZx6qBaafDUsveRmYyp3@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
--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 <sys/param.h> > #include <sys/module.h> > #include <sys/kernel.h> > #include <sys/systm.h> > #include <sys/proc.h> > #include <sys/sched.h> > #include <sys/pcpu.h> >=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--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100715143235.GU2381>