Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 20 Apr 2006 15:48:29 +0300
From:      Kostik Belousov <kostikbel@gmail.com>
To:        Jeremie Le Hen <jeremie@le-hen.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Symbol weirdness with static linking
Message-ID:  <20060420124829.GA1446@deviant.kiev.zoral.com.ua>
In-Reply-To: <20060420114822.GB35261@obiwan.tataz.chchile.org>
References:  <20060420114822.GB35261@obiwan.tataz.chchile.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--XsQoSWH+UP9D9v3l
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Thu, Apr 20, 2006 at 01:48:22PM +0200, Jeremie Le Hen wrote:
> Hi hackers,
>=20
> I created a library (libssp) which is to be linked in the same time as
> libgcc (GCC's LIBGCC_SPEC [1]).  This library is intended to provide the
> required symbols for ProPolice/SSP (Stack-Smashing Protector) which GCC
> references whenever it has to protect a function.
>=20
> This works almost perfectly but in one edge case : some programs
> are so "simple" (IOW have no stack-based buffer) that GCC does not
> feel the need to put any reference to the above symbols.  For instance,
> bin/echo and bin/mkdir are such programs.
>=20
> When libssp is linked dynamically, this is not a problem.  However, if
> I use NO_DYNAMICROOT when building world, ld(1) complains that it does not
> find the "syslog" symbol [2].  It happens that libssp uses syslog(3), but
> since libssp won't be in the resulting executable, I don't understand
> why ld(1) complains.
>=20
> OTOH, programs which do have a reference to ProPolice symbols compile
> without any problem.
>=20
> Even weirder, while there are other calls to libc functions in libssp
> - such as open(2), sigfillset(3) or sigprocmask(2) - if I comment out
> the call to syslog(3), ld(1) does not complain any longer.  What is
> so special with libc's "syslog" symbol ?  I don't understand what is
> the difference here between, the "syslog" symbol and, say, the "sigfilset"
> symbol (which is not used in echo(1) either).
>=20
> For the sake of completeness, I added the output of some objdump(1)
> outputs here :
>=20
> /usr/obj/usr/src/bin/echo/echo.o:
> http://tataz.chchile.org/~tataz/symbol_weirdness/objdump-t_echo.txt.gz
>=20
> /usr/obj/usr/src/tmp/usr/lib/libc.a:
> http://tataz.chchile.org/~tataz/symbol_weirdness/objdump-t_libc.txt.gz
>=20
> /usr/obj/usr/src/tmp/usr/lib/libssp.a:
> http://tataz.chchile.org/~tataz/symbol_weirdness/objdump-t_libssp.txt.gz
>=20
>=20
> Thank you.
> Regards,
>=20
>=20
> [1]
> #define LIBGCC_SPEC "%{shared: -lgcc_pic -lssp_pic} %{!shared: %{!pg: -lg=
cc -lssp} %{pg: -lgcc_p -lssp_p}}"
>=20
>=20
> [2]
> =3D=3D=3D> bin/echo (all)
> cc -O2 -fno-strict-aliasing -pipe -march=3Dpentium-m -fstack-protector -W=
system-headers -Werror -Wall -Wno-format-y2k -W -Wno-unused-parameter -Wstr=
ict-prototypes -Wmissing-prototypes -Wpointer-arith -Wreturn-type -Wcast-qu=
al -Wwrite-strings -Wswitch -Wshadow -Wcast-align -Wunused-parameter -Wchar=
-subscripts -Winline -Wnested-externs -Wredundant-decls -c /usr/src/bin/ech=
o/echo.c
> cc -O2 -fno-strict-aliasing -pipe -march=3Dpentium-m -fstack-protector -W=
system-headers -Werror -Wall -Wno-format-y2k -W -Wno-unused-parameter -Wstr=
ict-prototypes -Wmissing-prototypes -Wpointer-arith -Wreturn-type -Wcast-qu=
al -Wwrite-strings -Wswitch -Wshadow -Wcast-align -Wunused-parameter -Wchar=
-subscripts -Winline -Wnested-externs -Wredundant-decls  -v -static -o echo=
 echo.o
> Using built-in specs.
> Configured with: FreeBSD/i386 system compiler
> Thread model: posix
> gcc version 3.4.4 [FreeBSD] 20050518
>  /usr/obj/usr/src/tmp/usr/bin/ld -V -Bstatic -o echo /usr/obj/usr/src/tmp=
/usr/lib/crt1.o /usr/obj/usr/src/tmp/usr/lib/crti.o /usr/obj/usr/src/tmp/us=
r/lib/crtbegin.o -L/usr/obj/usr/src/tmp/usr/lib echo.o -lgcc -lssp -lc -lgc=
c -lssp /usr/obj/usr/src/tmp/usr/lib/crtend.o /usr/obj/usr/src/tmp/usr/lib/=
crtn.o
> GNU ld version 2.15 [FreeBSD] 2004-05-23
>   Supported emulations:
>    elf_i386_fbsd
> /usr/obj/usr/src/tmp/usr/lib/libssp.a(ssp.o)(.text+0xe8): In function `__=
stack_smash_handler':
> : undefined reference to `syslog'
> *** Error code 1
>=20
> --=20
> Jeremie Le Hen
> < jeremie at le-hen dot org >< ttz at chchile dot org >

It seems that you rebuilt world with CFLAGS -fstack-protector,
since your libc has references to the symbols like __stack_smash_handler.
As result, when linking with sequence -lgcc -lssp -lc -lgcc -lssp,
and no references from the main object,
references from libc causes objects from _second_ instance of -lssp to
be pulled into the link. Since libraries are scanned sequentially,
this object from libssp has no way to get required dependencies
from libc.

What makes syslog(3) special is that corresponding object from libc,
syslog.o, requires __stack_smash_handler, while objects for mentioned
syscalls do not.

Probably, another -lc after -lssp will change the situation. But
I'm not sure would it be enough or not.

--XsQoSWH+UP9D9v3l
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (FreeBSD)

iD8DBQFER4McC3+MBN1Mb4gRAg/VAJ977y4H+8itSlYH7oo3h+I1uS+83wCgjxvA
9EhCe+7c5mMah1+MeQynUAs=
=s1qd
-----END PGP SIGNATURE-----

--XsQoSWH+UP9D9v3l--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060420124829.GA1446>