Bug 22362

Summary: Installed crt1.o, crti,.o and crtn.o files are used with -m32
Product: glibc Reporter: H.J. Lu <hjl.tools>
Component: libcAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: drepper.fsp
Priority: P2 Flags: fweimer: security-
Version: 2.27   
Target Milestone: 2.27   
Host: i686 Target: x86-64
Build: Last reconfirmed:

Description H.J. Lu 2017-10-27 22:44:08 UTC
On x86-64, when building i686 glibc using x86-64 GCC with -m32,
installed crt1.o, crti,.o and crtn.o files are used to create
i686 shared libraries due to:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82754

GCC first looks for crt files under ./32.  It checks ./ only if the installed
crt files don't exist.

Should we make copies of crt1.o, crti,.o and crtn.o under csu/32?
Comment 1 jsm-csl@polyomino.org.uk 2017-10-27 22:49:57 UTC
You'll need to give a more detailed explanation of what the problem is and 
why it's not actually a mistake in the way you are configuring or building 
glibc.  gcc -B should not be used in the glibc build.  I build 32-bit 
glibc with 64-bit compilers routinely and have never seen this problem.
Comment 2 H.J. Lu 2017-10-27 23:03:46 UTC
(In reply to joseph@codesourcery.com from comment #1)
> You'll need to give a more detailed explanation of what the problem is and 
> why it's not actually a mistake in the way you are configuring or building 
> glibc.  gcc -B should not be used in the glibc build.  I build 32-bit 
> glibc with 64-bit compilers routinely and have never seen this problem.

When I built i686 glibc on x86-64 using x86-64 GCC with -m32, for
crypt/libcrypt.so, I got

gcc -m32   -shared -static-libgcc -Wl,-O1  -Wl,-z,defs -Wl,-dynamic-linker=/lib/ld-linux.so.2  -B/export/build/gnu/glibc-32bit/build-i686-linux/csu/  -Wl,--version-script=/export/build/gnu/glibc-32bit/build-i686-linux/libcrypt.map -Wl,-soname=libcrypt.so.1 -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both   -L/export/build/gnu/glibc-32bit/build-i686-linux -L/export/build/gnu/glibc-32bit/build-i686-linux/math -L/export/build/gnu/glibc-32bit/build-i686-linux/elf -L/export/build/gnu/glibc-32bit/build-i686-linux/dlfcn -L/export/build/gnu/glibc-32bit/build-i686-linux/nss -L/export/build/gnu/glibc-32bit/build-i686-linux/nis -L/export/build/gnu/glibc-32bit/build-i686-linux/rt -L/export/build/gnu/glibc-32bit/build-i686-linux/resolv -L/export/build/gnu/glibc-32bit/build-i686-linux/crypt -L/export/build/gnu/glibc-32bit/build-i686-linux/mathvec -L/export/build/gnu/glibc-32bit/build-i686-linux/support -L/export/build/gnu/glibc-32bit/build-i686-linux/nptl -Wl,-rpath-link=/export/build/gnu/glibc-32bit/build-i686-linux:/export/build/gnu/glibc-32bit/build-i686-linux/math:/export/build/gnu/glibc-32bit/build-i686-linux/elf:/export/build/gnu/glibc-32bit/build-i686-linux/dlfcn:/export/build/gnu/glibc-32bit/build-i686-linux/nss:/export/build/gnu/glibc-32bit/build-i686-linux/nis:/export/build/gnu/glibc-32bit/build-i686-linux/rt:/export/build/gnu/glibc-32bit/build-i686-linux/resolv:/export/build/gnu/glibc-32bit/build-i686-linux/crypt:/export/build/gnu/glibc-32bit/build-i686-linux/mathvec:/export/build/gnu/glibc-32bit/build-i686-linux/support:/export/build/gnu/glibc-32bit/build-i686-linux/nptl -o /export/build/gnu/glibc-32bit/build-i686-linux/crypt/libcrypt.so -T /export/build/gnu/glibc-32bit/build-i686-linux/shlib.lds /export/build/gnu/glibc-32bit/build-i686-linux/csu/abi-note.o -Wl,--whole-archive /export/build/gnu/glibc-32bit/build-i686-linux/crypt/libcrypt_pic.a -Wl,--no-whole-archive   -Wl,--start-group /export/build/gnu/glibc-32bit/build-i686-linux/libc.so /export/build/gnu/glibc-32bit/build-i686-linux/libc_nonshared.a -Wl,--as-needed /export/build/gnu/glibc-32bit/build-i686-linux/elf/ld.so -Wl,--no-as-needed -Wl,--end-group

When I added -v, it shows /lib/crt*.o are passed to linker.
Comment 3 H.J. Lu 2017-10-28 23:54:15 UTC
We should move this from nptl/Makefile to csu/Makefile:

ifeq ($(build-shared),yes)

# Set the `multidir' variable by grabbing the variable from the compiler.
# We do it once and save the result in a generated makefile.
-include $(objpfx)multidir.mk
$(objpfx)multidir.mk: $(common-objpfx)config.make
        $(make-target-directory)
        dir=`$(CC) $(CFLAGS) $(CPPFLAGS) -print-multi-directory`; \
        echo "multidir := $$dir" > $@T
        mv -f $@T $@

crti-objs := crti.o
crtn-objs := crtn.o
ifneq (,$(patsubst .,,$(multidir)))
generated-dirs += $(firstword $(subst /, , $(multidir)))
crti-objs += $(multidir)/crti.o
crtn-objs += $(multidir)/crtn.o
$(objpfx)$(multidir):
        mkdir -p $@
endif
extra-objs += $(crti-objs) $(crtn-objs)
extra-objs += pt-crti.o
endif
Comment 4 Sourceware Commits 2017-10-29 02:06:18 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, hjl/pr22362/master has been created
        at  340c6cf9bdf6734013f0c6b4e0d8f3a10fbd4ed0 (commit)

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=340c6cf9bdf6734013f0c6b4e0d8f3a10fbd4ed0

commit 340c6cf9bdf6734013f0c6b4e0d8f3a10fbd4ed0
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat Oct 28 17:41:16 2017 -0700

    Use newly built crt*.o files to build shared objects [BZ #22362]
    
    When multi-lib GCC is used to build glibc, the search order of GCC driver
    for crt*.o is -B*/`gcc -print-multi-directory`, the installed diretory,
    -B*/.  This patch extends multi-lib support from nptl/Makefile to
    csu/Makefile so that -B/glibc-build-directory/csu/ will pick up the newly
    built crt*.o.
    
    Tested on x86-64 for i686 and x32.
    
    	[BZ #22362]
    	* config.make.in (multidir): New.
    	* configure.ac (libc_cv_multidir): New.  AC_SUBST.
    	* configure: Regenerated.
    	* csu/Makefile [$(multidir) != .](multilib-extra-objs): New.
    	[$(multidir) != .](extra-objs): Add $(multilib-extra-objs).
    	[$(multidir) != .]($(addprefix $(objpfx)$(multidir)/, $(install-lib))):
    	New target.
    	* nptl/Makefile: Don't include multidir.mk.
    	($(objpfx)multidir.mk): Removed.

-----------------------------------------------------------------------
Comment 5 Sourceware Commits 2017-10-29 19:34:04 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, hjl/pr22362/symlink has been created
        at  d7ca99d742d11803ab0e2b594ab828886bc069dc (commit)

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=d7ca99d742d11803ab0e2b594ab828886bc069dc

commit d7ca99d742d11803ab0e2b594ab828886bc069dc
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sun Oct 29 12:03:03 2017 -0700

    nptl: Simplify libpthread.so rules
    
    libpthread.so must be created against a special crti.o and when multi-lib
    GCC is used to build glibc, the special crti.o must be placed under the
    `gcc -print-multi-directory` subdirectory so that -B$(common-objpfx)nptl/
    will pick it up.  This patch compiles the special crti.o directly with
    "-include pt-crti.h" and uses the new make-link-multidir from the fix
    for [BZ #22362] to create the symlink for the multi-lib subdirectory.
    It simplifies libpthread.so rules to support -B$(common-objpfx)nptl/
    and -B$(common-objpfx)/ for correct crt*.o files.
    
    Tested on x86-64 for x86-64, i686 and x32.
    
    	* nptl/Makefile (multidir.mk): Don't generate.
    	($(objpfx)multidir.mk): Removed.
    	(crtn-objs): Likewise.
    	(generated-dirs): Don't add $(multidir).
    	(multilib-crti-objs): New.
    	(extra-objs): Use $(multilib-crti-objs).  Don't add pt-crti.o.
    	($(objpfx)libpthread.so): Removed.
    	(CPPFLAGS-crti.o): New.
    	($(objpfx)crti.o): Removed.
    	($(objpfx)$(multidir)/crti.o): Likewise.
    	($(objpfx)$(multidir)/crtn.o): Likewise.
    	($(addprefix $(objpfx)$(multidir)/, $(crti-objs))): New target.
    	(generated): Remove multidir.mk.
    	* nptl/pt-crti.S: Renamed to ...
    	* nptl/pt-crti.h: This.  Don't include <crti.S>.

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=3242db2138de7e0786ee3806f8bd016925d06099

commit 3242db2138de7e0786ee3806f8bd016925d06099
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat Oct 28 17:41:16 2017 -0700

    Use newly built crt*.o files to build shared objects [BZ #22362]
    
    When multi-lib GCC is used to build glibc, the search order of GCC driver
    for crt*.o is -B*/`gcc -print-multi-directory`, the installed diretory,
    -B*/.  This patch adds multi-lib support to csu/Makefile so that
    -B/glibc-build-directory/csu/ will pick up the newly built crt*.o.
    
    Tested on x86-64 for i686 and x32.
    
    	[BZ #22362]
    	* Makerules (make-link-multidir): New.
    	* config.make.in (multidir): New.
    	* configure.ac (libc_cv_multidir): New.  AC_SUBST.
    	* configure: Regenerated.
    	* csu/Makefile [$(multidir) != .](multilib-extra-objs): New.
    	[$(multidir) != .](extra-objs): Add $(multilib-extra-objs).
    	[$(multidir) != .]($(addprefix $(objpfx)$(multidir)/, $(install-lib))):
    	New target.

-----------------------------------------------------------------------
Comment 6 Sourceware Commits 2017-11-06 17:30:27 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".

The branch, master has been updated
       via  abcb584d0eae7270b35e1b3fed1f9661e26b8be0 (commit)
      from  4e2dff67beeb063cb36fe100d9d2b3f2f88d80c6 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=abcb584d0eae7270b35e1b3fed1f9661e26b8be0

commit abcb584d0eae7270b35e1b3fed1f9661e26b8be0
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Mon Nov 6 08:29:48 2017 -0800

    Use newly built crt*.o files to build shared objects [BZ #22362]
    
    When multi-lib GCC is used to build glibc, the search order of GCC driver
    for crt*.o is -B*/`gcc -print-multi-directory`, the installed diretory,
    -B*/.  This patch adds multi-lib support to csu/Makefile so that
    -B/glibc-build-directory/csu/ will pick up the newly built crt*.o.
    
    Tested on x86-64 for i686 and x32.
    
    	[BZ #22362]
    	* Makerules (make-link-multidir): New.
    	* config.make.in (multidir): New.
    	* configure.ac (libc_cv_multidir): New.  AC_SUBST.
    	* configure: Regenerated.
    	* csu/Makefile [$(multidir) != .](multilib-extra-objs): New.
    	[$(multidir) != .](extra-objs): Add $(multilib-extra-objs).
    	[$(multidir) != .]($(addprefix $(objpfx)$(multidir)/, $(install-lib))):
    	New target.

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog      |   12 ++++++++++++
 Makerules      |    9 +++++++++
 config.make.in |    1 +
 configure      |    6 ++++++
 configure.ac   |    5 +++++
 csu/Makefile   |   10 ++++++++++
 6 files changed, 43 insertions(+), 0 deletions(-)
Comment 7 H.J. Lu 2017-11-06 17:35:31 UTC
Fixed.