--- glibc.spec
+++ glibc-x32.spec
@@ -1,7 +1,11 @@
 %define glibcsrcdir glibc-2.17-c758a686
 %define glibcversion 2.17
 ### glibc.spec.in follows:
+%if %{with check}
 %define run_glibc_tests 1
+%else
+%define run_glibc_tests 0
+%endif
 %define auxarches athlon alphaev6
 %define xenarches i686 athlon
 %ifarch %{xenarches}
@@ -24,8 +28,26 @@
 # Remove -s to get verbose output.
 %define silentrules PARALLELMFLAGS=-s
 
+%bcond_without build 
+%bcond_without check 
+
+%define __target_cpu %{_target_cpu}
+
+%define x32 1
+%if 0%{?x32}
+%define x32name -x32
+%define _lib libx32
+%define _libdir %{_prefix}/libx32
+%define __target x86_32-redhat-linux
+%define __target_cpu x86_32
+#define _bindir %{_prefix}/bin/x32
+#define auxarches athlon alphaev6 x86_64
+%define debuginfocommonarches 0
+%define nonss 1
+%endif
+
 Summary: The GNU libc libraries
-Name: glibc
+Name: glibc%{?x32name}
 Version: %{glibcversion}
 Release: 3%{?dist}
 # GPLv2+ is used in a bunch of programs, LGPLv2+ is used for libraries.
@@ -61,56 +83,56 @@ Source1: %{glibcsrcdir}-releng.tar.gz
 
 # Configuration twiddle, not sure there's a good case to get upstream to
 # change this.
-Patch0001: %{name}-fedora-nscd.patch
+Patch0001: glibc-fedora-nscd.patch
 
-Patch0002: %{name}-fedora-regcomp-sw11561.patch
-Patch0003: %{name}-fedora-ldd.patch
+Patch0002: glibc-fedora-regcomp-sw11561.patch
+Patch0003: glibc-fedora-ldd.patch
 
-Patch0004: %{name}-fedora-ppc-unwind.patch
+Patch0004: glibc-fedora-ppc-unwind.patch
 
 # Build info files in the source tree, then move to the build
 # tree so that they're identical for multilib builds
-Patch0005: %{name}-rh825061.patch
+Patch0005: glibc-rh825061.patch
 
 # Horrible hack, never to be upstreamed.  Can go away once the world
 # has been rebuilt to use the new ld.so path.
-Patch0006: %{name}-arm-hardfloat-3.patch
+Patch0006: glibc-arm-hardfloat-3.patch
 
 
 # Needs to be sent upstream
 
 # Needs to be sent upstream
-Patch0008: %{name}-fedora-getrlimit-PLT.patch
-Patch0009: %{name}-fedora-include-bits-ldbl.patch
+Patch0008: glibc-fedora-getrlimit-PLT.patch
+Patch0009: glibc-fedora-include-bits-ldbl.patch
 
 # stap, needs to be sent upstream
-Patch0010: %{name}-stap-libm.patch
+Patch0010: glibc-stap-libm.patch
 
 # Needs to be sent upstream
-Patch0029: %{name}-rh841318.patch
+Patch0029: glibc-rh841318.patch
 
 # All these were from the glibc-fedora.patch mega-patch and need another
 # round of reviewing.  Ideally they'll either be submitted upstream or
 # dropped.
-Patch0012: %{name}-fedora-linux-tcsetattr.patch
-Patch0014: %{name}-fedora-nptl-linklibc.patch
-Patch0015: %{name}-fedora-localedef.patch
-Patch0016: %{name}-fedora-i386-tls-direct-seg-refs.patch
-Patch0017: %{name}-fedora-gai-canonical.patch
-Patch0018: %{name}-fedora-pt_chown.patch
-Patch0019: %{name}-fedora-nis-rh188246.patch
-Patch0020: %{name}-fedora-manual-dircategory.patch
-Patch0024: %{name}-fedora-locarchive.patch
-Patch0025: %{name}-fedora-streams-rh436349.patch
-Patch0028: %{name}-fedora-localedata-rh61908.patch
-Patch0030: %{name}-fedora-uname-getrlimit.patch
-Patch0031: %{name}-fedora-__libc_multiple_libcs.patch
-Patch0032: %{name}-fedora-elf-rh737223.patch
-Patch0033: %{name}-fedora-elf-ORIGIN.patch
-Patch0034: %{name}-fedora-elf-init-hidden_undef.patch
+Patch0012: glibc-fedora-linux-tcsetattr.patch
+Patch0014: glibc-fedora-nptl-linklibc.patch
+Patch0015: glibc-fedora-localedef.patch
+Patch0016: glibc-fedora-i386-tls-direct-seg-refs.patch
+Patch0017: glibc-fedora-gai-canonical.patch
+Patch0018: glibc-fedora-pt_chown.patch
+Patch0019: glibc-fedora-nis-rh188246.patch
+Patch0020: glibc-fedora-manual-dircategory.patch
+Patch0024: glibc-fedora-locarchive.patch
+Patch0025: glibc-fedora-streams-rh436349.patch
+Patch0028: glibc-fedora-localedata-rh61908.patch
+Patch0030: glibc-fedora-uname-getrlimit.patch
+Patch0031: glibc-fedora-__libc_multiple_libcs.patch
+Patch0032: glibc-fedora-elf-rh737223.patch
+Patch0033: glibc-fedora-elf-ORIGIN.patch
+Patch0034: glibc-fedora-elf-init-hidden_undef.patch
 
 # Needs to be sent upstream
-Patch0035: %{name}-rh911307.patch
+Patch0035: glibc-rh911307.patch
 
 #
 # Patches from upstream
@@ -123,31 +145,31 @@ Patch0035: %{name}-rh911307.patch
 # Obviously we're not there right now, but that's the goal
 #
 # http://sourceware.org/ml/libc-alpha/2012-12/msg00103.html
-Patch2007: %{name}-rh697421.patch
+Patch2007: glibc-rh697421.patch
 
-Patch2011: %{name}-rh757881.patch
+Patch2011: glibc-rh757881.patch
 
-Patch2013: %{name}-rh741105.patch
+Patch2013: glibc-rh741105.patch
 
 # Upstream BZ 9954
-Patch2021: %{name}-rh739743.patch
+Patch2021: glibc-rh739743.patch
 
 # Upstream BZ 13818
-Patch2022: %{name}-rh800224.patch
+Patch2022: glibc-rh800224.patch
 
 # Upstream BZ 14247
-Patch2023: %{name}-rh827510.patch
+Patch2023: glibc-rh827510.patch
 
 # Upstream BZ 13028
-Patch2026: %{name}-rh841787.patch
+Patch2026: glibc-rh841787.patch
 
 # Upstream BZ 14185
-Patch2027: %{name}-rh819430.patch
+Patch2027: glibc-rh819430.patch
 
 # Upstream BZ 15006
-Patch2028: %{name}-rh905184.patch
+Patch2028: glibc-rh905184.patch
 
-Buildroot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
+Buildroot: %{_tmppath}/glibc-%{version}-%{release}-root-%(%{__id_u} -n)
 Obsoletes: glibc-profile < 2.4
 Obsoletes: nss_db
 Provides: ldconfig
@@ -237,9 +259,9 @@ Install glibc-xen if you might run your
 Summary: Object files for development using standard C libraries.
 Group: Development/Libraries
 Requires(pre): /sbin/install-info
-Requires(pre): %{name}-headers
-Requires: %{name}-headers = %{version}-%{release}
-Requires: %{name} = %{version}-%{release}
+Requires(pre): glibc-headers
+Requires: glibc-headers = %{version}-%{release}
+Requires: glibc = %{version}-%{release}
 
 %description devel
 The glibc-devel package contains the object files necessary
@@ -255,7 +277,7 @@ use the standard C libraries.
 %package static
 Summary: C library static libraries for -static linking.
 Group: Development/Libraries
-Requires: %{name}-devel = %{version}-%{release}
+Requires: glibc-devel = %{version}-%{release}
 
 %description static
 The glibc-static package contains the C library static libraries
@@ -265,15 +287,15 @@ which is highly discouraged.
 %package headers
 Summary: Header files for development using standard C libraries.
 Group: Development/Libraries
-Provides: %{name}-headers(%{_target_cpu})
+Provides: glibc-headers(%{_target_cpu})
 %ifarch x86_64
 # If both -m32 and -m64 is to be supported on AMD64, x86_64 glibc-headers
 # have to be installed, not i586 ones.
-Obsoletes: %{name}-headers(i586)
-Obsoletes: %{name}-headers(i686)
+Obsoletes: glibc-headers(i586)
+Obsoletes: glibc-headers(i686)
 %endif
 Requires(pre): kernel-headers
-Requires: kernel-headers >= 2.2.1, %{name} = %{version}-%{release}
+Requires: kernel-headers >= 2.2.1, glibc = %{version}-%{release}
 BuildRequires: kernel-headers >= 2.6.22
 
 %description headers
@@ -289,7 +311,7 @@ use the standard C libraries.
 
 %package common
 Summary: Common binaries and locale data for glibc
-Requires: %{name} = %{version}-%{release}
+Requires: glibc = %{version}-%{release}
 Requires: tzdata >= 2003a
 Group: System Environment/Base
 
@@ -297,24 +319,24 @@ Group: System Environment/Base
 The glibc-common package includes common binaries for the GNU libc
 libraries, as well as national language (locale) support.
 
-%package -n nscd
+%package -n nscd%{?x32name}
 Summary: A Name Service Caching Daemon (nscd).
 Group: System Environment/Daemons
-Requires: %{name} = %{version}-%{release}
+Requires: glibc = %{version}-%{release}
 Requires: libselinux >= 1.17.10-1, audit-libs >= 1.1.3
 Requires(pre): /usr/sbin/useradd, coreutils
 Requires(post): systemd-units
 Requires(preun): systemd-units
 Requires(postun): systemd-units, /usr/sbin/userdel
 
-%description -n nscd
+%description -n nscd%{?x32name}
 Nscd caches name service lookups and can dramatically improve
 performance with NIS+, and may help with DNS as well.
 
 %package utils
 Summary: Development utilities from GNU C library
 Group: Development/Tools
-Requires: %{name} = %{version}-%{release}
+Requires: glibc = %{version}-%{release}
 
 %description utils
 The glibc-utils package contains memusage, a memory usage profiler,
@@ -329,7 +351,7 @@ If unsure if you need this, don't instal
 %global __debug_package 1
 
 %package debuginfo
-Summary: Debug information for package %{name}
+Summary: Debug information for package glibc
 Group: Development/Debug
 AutoReqProv: no
 %ifarch %{debuginfocommonarches}
@@ -341,7 +363,7 @@ Obsoletes: glibc-debuginfo-common
 %endif
 
 %description debuginfo
-This package provides debug information for package %{name}.
+This package provides debug information for package glibc.
 Debug information is useful when developing applications that use this
 package or when debugging this package.
 
@@ -355,12 +377,12 @@ with -static -L%{_prefix}/lib/debug%{_pr
 %ifarch %{debuginfocommonarches}
 
 %package debuginfo-common
-Summary: Debug information for package %{name}
+Summary: Debug information for package glibc
 Group: Development/Debug
 AutoReqProv: no
 
 %description debuginfo-common
-This package provides debug information for package %{name}.
+This package provides debug information for package glibc.
 Debug information is useful when developing applications that use this
 package or when debugging this package.
 
@@ -368,6 +390,10 @@ package or when debugging this package.
 %endif
 
 %prep
+%if %{without build}
+rm -f common.filelist debuginfo.filelist devel.filelist headers.filelist nosegneg.filelist rpm.filelist static.filelist utils.filelist
+exit 0
+%endif
 %setup -q -n %{glibcsrcdir} -b1
 
 %patch0001 -p1
@@ -419,10 +445,15 @@ touch `find . -name configure`
 touch locale/programs/*-kw.h
 
 %build
+%if %{without build}
+exit 0
+%endif
 GCC=gcc
 GXX=g++
 %ifarch %{ix86}
 BuildFlags="-march=%{_target_cpu} -mtune=generic"
+GCC="gcc -m32"
+GXX="g++ -m32"
 %endif
 %ifarch i686
 BuildFlags="-march=i686 -mtune=generic"
@@ -463,6 +494,12 @@ BuildFlags=""
 GCC="gcc -m64"
 GXX="g++ -m64"
 %endif
+%if 0%{?x32}
+BuildFlags="-mtune=generic"
+GCC="gcc -mx32"
+GXX="g++ -mx32"
+sed -i "s/i[[34567]]86 | x86_64 | powerpc* | s390*)/i[[34567]]86 | x86_64 | x86_32 | powerpc* | s390*)/" configure.in
+%endif
 
 BuildFlags="$BuildFlags -fasynchronous-unwind-tables"
 # Add -DNDEBUG unless using a prerelease
@@ -493,7 +530,12 @@ configure_CFLAGS="$build_CFLAGS -fno-asy
 	--prefix=%{_prefix} \
 	--enable-add-ons=ports,nptl$AddOns \
 	--with-headers=%{_prefix}/include $EnableKernel --enable-bind-now \
+%if 0%{?x32}
+	--host=x86_64-x32-linux BUILD_CC='gcc' CPP=cpp \
+	--build=x86_64-redhat-linux \
+%else
 	--build=%{target} \
+%endif
 %ifarch %{multiarcharches}
 	--enable-multi-arch \
 %endif
@@ -504,7 +546,11 @@ configure_CFLAGS="$build_CFLAGS -fno-asy
 %ifarch ppc64p7
 	--with-cpu=power7 \
 %endif
-	--disable-profile --enable-nss-crypt ||
+	--disable-profile \
+	%if !0%{?nonss}
+	--enable-nss-crypt \
+	%endif
+||
 { cat config.log; false; }
 
 make %{?_smp_mflags} -r CFLAGS="$build_CFLAGS" %{silentrules}
@@ -539,13 +585,15 @@ build power6
 )
 %endif
 
+	%if !0%{?nonss}
 cd build-%{target}
-$GCC -static -L. -Os -g ../releng/glibc_post_upgrade.c -o glibc_post_upgrade.%{_target_cpu} \
+$GCC -static -L. -Os -g ../releng/glibc_post_upgrade.c -o glibc_post_upgrade.%{__target_cpu} \
   '-DLIBTLS="/%{_lib}/tls/"' \
   '-DGCONV_MODULES_DIR="%{_prefix}/%{_lib}/gconv"' \
   '-DLD_SO_CONF="/etc/ld.so.conf"' \
-  '-DICONVCONFIG="%{_sbindir}/iconvconfig.%{_target_cpu}"'
+  '-DICONVCONFIG="%{_sbindir}/iconvconfig.%{__target_cpu}"'
 cd ..
+	%endif
 
 %install
 GCC=`cat Gcc`
@@ -685,9 +733,11 @@ mkdir -p $RPM_BUILD_ROOT/etc/sysconfig
 > $RPM_BUILD_ROOT%{_prefix}/%{_lib}/gconv/gconv-modules.cache
 chmod 644 $RPM_BUILD_ROOT%{_prefix}/%{_lib}/gconv/gconv-modules.cache
 
+	%if !0%{?nonss}
 # Install the upgrade program
-install -m 700 build-%{target}/glibc_post_upgrade.%{_target_cpu} \
-  $RPM_BUILD_ROOT/usr/sbin/glibc_post_upgrade.%{_target_cpu}
+install -m 700 build-%{target}/glibc_post_upgrade.%{__target_cpu} \
+  $RPM_BUILD_ROOT/usr/sbin/glibc_post_upgrade.%{__target_cpu}
+	%endif
 
 strip -g $RPM_BUILD_ROOT%{_prefix}/%{_lib}/*.o
 
@@ -722,7 +772,7 @@ rm -f ${RPM_BUILD_ROOT}/%{_lib}/libnss1-
 rm -f ${RPM_BUILD_ROOT}/%{_lib}/libnss-*.so.1
 
 # Ugly hack for buggy rpm
-ln -f ${RPM_BUILD_ROOT}%{_sbindir}/iconvconfig{,.%{_target_cpu}}
+ln -f ${RPM_BUILD_ROOT}%{_sbindir}/iconvconfig{,.%{__target_cpu}}
 
 # In F7+ this is provided by rpcbind rpm
 rm -f $RPM_BUILD_ROOT%{_sbindir}/rpcinfo
@@ -758,7 +808,18 @@ rm -f $RPM_BUILD_ROOT%{_sbindir}/rpcinfo
 
 mkdir -p $RPM_BUILD_ROOT%{_prefix}/%{_lib}
 mv -f $RPM_BUILD_ROOT/%{_lib}/lib{pcprofile,memusage}.so $RPM_BUILD_ROOT%{_prefix}/%{_lib}
+%define memusage 1
+%if 0%{?x32}
+%define memusage 0
+%endif
+%ifarch %{ix86}
+%define memusage 1
+%endif
+%if %{memusage}
 for i in $RPM_BUILD_ROOT%{_prefix}/bin/{xtrace,memusage}; do
+%else
+for i in $RPM_BUILD_ROOT%{_prefix}/bin/xtrace; do
+%endif
   sed -e 's~=/%{_lib}/libpcprofile.so~=%{_prefix}/%{_lib}/libpcprofile.so~' \
       -e 's~=/%{_lib}/libmemusage.so~=%{_prefix}/%{_lib}/libmemusage.so~' \
       -e 's~='\''/\\\$LIB/libpcprofile.so~='\''%{_prefix}/\\$LIB/libpcprofile.so~' \
@@ -810,16 +871,20 @@ grep '/%{_lib}/%{nosegneg_subdir}' < rpm
 sed -i -e '\|/%{_lib}/%{nosegneg_subdir}|d' rpm.filelist
 %endif
 
+%if !0%{?x32}
 echo '%{_prefix}/sbin/build-locale-archive' >> common.filelist
 echo '%{_prefix}/sbin/nscd' > nscd.filelist
+%endif
 
 cat >> rpm.filelist <<EOF
 %{_prefix}/%{_lib}/libmemusage.so
 %{_prefix}/%{_lib}/libpcprofile.so
 EOF
 cat > utils.filelist <<EOF
+%if %{memusage}
 %{_prefix}/bin/memusage
 %{_prefix}/bin/memusagestat
+%endif
 %{_prefix}/bin/mtrace
 %{_prefix}/bin/pcprofiledump
 %{_prefix}/bin/xtrace
@@ -835,6 +900,7 @@ touch -r sunrpc/etc.rpc $RPM_BUILD_ROOT/
 # referenced at link time here, particularly ld.so, may be different than
 # the one used at runtime.  This is really only needed during the ARM 
 # transition from ld-linux.so.3 to ld-linux-armhf.so.3.
+%if !0%{?x32}
 cd releng 
 $GCC -Os -g -o build-locale-archive build-locale-archive.c \
   ../build-%{target}/locale/locarchive.o \
@@ -845,6 +911,7 @@ $GCC -Os -g -o build-locale-archive buil
   -B../build-%{target}/csu/ -lc -lc_nonshared
 install -m 700 build-locale-archive $RPM_BUILD_ROOT/usr/sbin/build-locale-archive
 cd ..
+%endif
 
 # the last bit: more documentation
 rm -rf documentation
@@ -930,8 +997,8 @@ popd
 
 # The #line directives gperf generates do not give the proper
 # file name relative to the build directory.
-(cd locale; ln -s programs/*.gperf .)
-(cd iconv; ln -s ../locale/programs/charmap-kw.gperf .)
+(cd locale; rm -f *.gperf; ln -s programs/*.gperf .)
+(cd iconv; rm -f *.gperf; ln -s ../locale/programs/charmap-kw.gperf .)
 
 ls -l $RPM_BUILD_ROOT/usr/bin/getconf
 ls -l $RPM_BUILD_ROOT/usr/libexec/getconf
@@ -974,6 +1041,9 @@ cat debuginfocommon.sources >> debuginfo
 %ifarch sparc sparcv9
 %define basearch sparc
 %endif
+%if 0%{?x32}
+%define basearch x86_32
+%endif
 
 # auxarches get only these few source files
 auxarches_debugsources=\
@@ -1026,7 +1096,9 @@ if rpm.vercmp(rel, required) < 0 then
   error("FATAL: kernel too old", 0)
 end
 
-%post -p /usr/sbin/glibc_post_upgrade.%{_target_cpu}
+%if !0%{?x32}
+%post -p /usr/sbin/glibc_post_upgrade.%{__target_cpu}
+%endif
 
 %postun -p /sbin/ldconfig
 
@@ -1070,19 +1142,19 @@ fi
 
 %postun utils -p /sbin/ldconfig
 
-%pre -n nscd
+%pre -n nscd%{?x32name}
 getent group nscd >/dev/null || /usr/sbin/groupadd -g 28 -r nscd
 getent passwd nscd >/dev/null ||
   /usr/sbin/useradd -M -o -r -d / -s /sbin/nologin \
 		    -c "NSCD Daemon" -u 28 -g nscd nscd
 
-%post -n nscd
+%post -n nscd%{?x32name}
 %systemd_post nscd.service
 
-%preun -n nscd
+%preun -n nscd%{?x32name}
 %systemd_preun nscd.service
 
-%postun -n nscd
+%postun -n nscd%{?x32name}
 if test $1 = 0; then
   /usr/sbin/userdel nscd > /dev/null 2>&1 || :
 fi
@@ -1138,6 +1210,15 @@ rm -f *.filelist*
 %doc README NEWS INSTALL BUGS PROJECTS CONFORMANCE elf/rtld-debugger-interface.txt
 %doc COPYING COPYING.LIB LICENSES
 %doc hesiod/README.hesiod
+%if 0%{?x32}
+%exclude /usr/libexec/getconf/POSIX_V6_ILP32_OFF32
+%exclude /usr/libexec/getconf/POSIX_V6_ILP32_OFFBIG
+%exclude /usr/libexec/getconf/POSIX_V7_ILP32_OFF32
+%exclude /usr/libexec/getconf/POSIX_V7_ILP32_OFFBIG
+%exclude /usr/libexec/getconf/XBS5_ILP32_OFF32
+%exclude /usr/libexec/getconf/XBS5_ILP32_OFFBIG
+%endif
+
 
 %if %{xenpackage}
 %files -f nosegneg.filelist xen
@@ -1168,6 +1249,25 @@ rm -f *.filelist*
 %files -f utils.filelist utils
 %defattr(-,root,root)
 
+%if 0%{?x32}
+%files -n nscd%{?x32name}
+   /etc/nscd.conf
+   /etc/sysconfig/nscd
+   /lib/systemd/system/nscd.service
+   /lib/systemd/system/nscd.socket
+   /usr/lib/tmpfiles.d/nscd.conf
+   /usr/sbin/nscd
+   /var/db/nscd/group
+   /var/db/nscd/hosts
+   /var/db/nscd/passwd
+   /var/db/nscd/services
+   /var/run/nscd/group
+   /var/run/nscd/hosts
+   /var/run/nscd/nscd.pid
+   /var/run/nscd/passwd
+   /var/run/nscd/services
+   /var/run/nscd/socket
+%else
 %files -f nscd.filelist -n nscd
 %defattr(-,root,root)
 %config(noreplace) /etc/nscd.conf
@@ -1188,6 +1288,7 @@ rm -f *.filelist*
 %attr(0600,root,root) %verify(not md5 size mtime) %ghost %config(missingok,noreplace) /var/db/nscd/services
 %ghost %config(missingok,noreplace) /etc/sysconfig/nscd
 %endif
+%endif
 
 %if 0%{?_enable_debug_packages}
 %files debuginfo -f debuginfo.filelist
