%global run_testsuite 1

Name:           mingw-binutils
Version:        2.32
Release:        7%{?dist}
Summary:        Cross-compiled version of binutils for Win32 and Win64 environments

License:        GPLv2+ and LGPLv2+ and GPLv3+ and LGPLv3+

URL:            http://www.gnu.org/software/binutils/
Source0:        http://ftp.gnu.org/gnu/binutils/binutils-%{version}.tar.bz2

### Patches from native package

# Unneeded, mingw does not have lib64
# Patch01: binutils-2.20.51.0.2-libtool-lib64.patch

# Purpose:  Appends a RHEL or Fedora release string to the generic binutils
#           version string.
# Lifetime: Permanent.  This is a RHEL/Fedora specific patch.
Patch02: binutils-2.25-version.patch

# Purpose:  Exports the demangle.h header file (associated with the libiberty
#           sources) with the binutils-devel rpm.
# Lifetime: Permanent.  This is a RHEL/Fedora specific patch.
Patch03: binutils-export-demangle.h.patch

# Purpose:  Disables the check in the BFD library's bfd.h header file that
#           config.h has been included before the bfd.h header.  See BZ
#           #845084 for more details.
# Lifetime: Permanent - but it should not be.  The bfd.h header defines
#           various types that are dependent upon configuration options, so
#           the order of inclusion is important.
# FIXME:    It would be better if the packages using the bfd.h header were
#           fixed so that they do include the header files in the correct
#           order.
Patch04: binutils-2.22.52.0.4-no-config-h-check.patch

# Purpose:  Include the filename concerned in readelf error messages.  This
#           makes readelf's output more helpful when it is run on multiple
#           input files.
# Lifetime: Permanent.  This patch changes the format of readelf's output,
#           making it better (IMHO) but also potentially breaking tools that
#           depend upon readelf's current format.  Hence it remains a local
#           patch.
Patch05: binutils-2.29-filename-in-error-messages.patch

# Unneeded, affects ELF only
# Patch06: binutils-2.29-revert-PLT-elision.patch

# Purpose:  Changes readelf so that when it displays extra information about
#           a symbol, this information is placed at the end of the line.
# Lifetime: Permanent.
# FIXME:    The proper fix would be to update the scripts that are expecting
#           a fixed output from readelf.  But it seems that some of them are
#           no longer being maintained.
Patch07: binutils-readelf-other-sym-info.patch

# Unneeded, affects ELF only
# Patch08: binutils-2.27-aarch64-ifunc.patch

# Purpose:  Stop the binutils from statically linking with libstdc++.
# Lifetime: Permanent.
Patch09: binutils-do-not-link-with-static-libstdc++.patch

# Unneeded, affects ELF only
# Patch10: binutils-attach-to-group.patch

# Purpose:  Stop gold from complaining about relocs in the .gnu.build.attribute
#           section that reference symbols in discarded sections.
# Lifetime: Fixed in 2.33 (maybe)
Patch11: binutils-gold-ignore-discarded-note-relocs.patch

# Purpose:  Allow OS specific sections in section groups.
# Lifetime: Might be fixed in 2.33
Patch12: binutils-special-sections-in-groups.patch

# Purpose:  Fix linker testsuite failures.
# Lifetime: Fixed in 2.33 (possibly)
Patch13: binutils-fix-testsuite-failures.patch

# Purpose:  Improve objdump's handling of corrupt input files.
# Lifetime: Fixed in 2.33
Patch14: binutils-CVE-2019-9073.patch

# Purpose:  Stop illegal memory access parsing corrupt PE files.
# Lifetime: Fixed in 2.33
Patch15: binutils-CVE-2019-9074.patch

# Purpose:  Stop illegal memory access parsing corrupt archives.
# Lifetime: Fixed in 2.33
Patch16: binutils-CVE-2019-9075.patch

# Purpose:  Stop illegal memory access parsing a corrupt MIPS binary.
# Lifetime: Fixed in 2.33
Patch17: binutils-CVE-2019-9077.patch

# Purpose:  Stop a seg-fault when disassembling an EFI binary.
# Lifetime: Fixed in 2.33
Patch18: binutils-disassembling-efi-files.patch

# Purpose:  Fix a stack exhaustion problem in libiberty's name demangling code.
# Lifetime: Fixed in 2.33
Patch19: binutils-CVE-2019-9071.patch

# Purpose:  Have the GOLD linker for AArch64 generate PLT entries for MOVW_ABS
#           relocations if necessary.
# Lifetime: Fixed in 2.33
Patch20: binutils-aarch64-gold-PLT-for-MOVW_ABS.patch

# Purpose:  Stop gold from aborting when input sections with the same name
#            have different flags.
# Lifetime: Fixed in 2.33 (probably)
Patch21: binutils-gold-mismatched-section-flags.patch

# Purpose:  Corrcect a memory corruption when generating relocs for build
#            notes in the assembler.
# Lifetime: Fixed in 2.33
Patch22: binutils-gas-build-note-relocs.patch

# Purpose:  Stop the BFD library from issueing warning messages about allocated
#            sections being found outside of loadable segments, if they are
#            found inside debuginfo files.
# Lifetime: Fixed in 2.33
Patch23: binutils-do-not-warn-about-debuginfo-files.patch

# Purpose:  Stops the linker from merging section groups with different exclusion flags.
# Lifetime: Fixed in 2.33
Patch24: binutils-do-not-merge-differing-SHF_EXCLUDE-groups.patch

# Purpose: Fix -Map and property merging
# Lifetime: Fixed in 2.33
Patch25: binutils-rh1736114.patch

# Purpose: Change objcopy/strip so that they do not complain if the
#  first note in a sequence of build notes is not a version note.
# Lifetime: Fixed in 2.33
Patch26: binutils-objcopy-gnu-build-version-notes.patch

# Purpose: Add a check to the GOLD linker for a corrupt input file
#  with a fuzzed section offset.
# Lifetime: Fixed in 2.33
Patch27: binutils-CVE-2019-1010204.patch

# Purpose: Add check to libiberty library in order to prevent an integer overflow in the gold linker.
# Lifetime: Fixed in 2.33
Patch28: binutils-CVE-2019-14250.patch

# Purpose: Add check to readelf in order to prevent an integer overflow.
# Lifetime: Fixed in 2.33
Patch29: binutils-CVE-2019-14444.patch

### MINGW specific patches

# Backport https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=999d6dff80fab12d22c2a8d91923db6bde7fb3e5
# Fixes "too many open files" when linking libLLVM.dll
Patch100:       0001-Plugin-target-handling.patch
# Backport https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=patch;h=2219ae0b0ebe14373850b000c2abaa31dab1d741
# Fixes an LTO issue (#1475237)
Patch101:         binutils_24267.patch

BuildRequires:  gcc
BuildRequires:  flex
BuildRequires:  bison
BuildRequires:  texinfo
BuildRequires:  zlib-devel
BuildRequires:  mingw32-filesystem >= 102
BuildRequires:  mingw64-filesystem >= 102
%if %{run_testsuite}
BuildRequires:  dejagnu
BuildRequires:  sharutils
%endif
Provides:       bundled(libiberty)


%description
Cross compiled binutils (utilities like 'strip', 'as', 'ld') which
understand Windows executables and DLLs.

%package -n mingw-binutils-generic
Summary:        Utilities which are needed for both the Win32 and Win64 toolchains

%description -n mingw-binutils-generic
Utilities (like strip and objdump) which are needed for
both the Win32 and Win64 toolchains

%package -n mingw32-binutils
Summary:        Cross-compiled version of binutils for the Win32 environment
Requires:       mingw-binutils-generic = %{version}-%{release}

# NB: This must be left in.
Requires:       mingw32-filesystem >= 95

%description -n mingw32-binutils
Cross compiled binutils (utilities like 'strip', 'as', 'ld') which
understand Windows executables and DLLs.

%package -n mingw64-binutils
Summary:        Cross-compiled version of binutils for the Win64 environment
Requires:       mingw-binutils-generic = %{version}-%{release}

# NB: This must be left in.
Requires:       mingw64-filesystem >= 95

%description -n mingw64-binutils
Cross compiled binutils (utilities like 'strip', 'as', 'ld') which
understand Windows executables and DLLs.


%prep
%autosetup -p1 -n binutils-%{version}


%build
mkdir build_win32
pushd build_win32
CFLAGS="$RPM_OPT_FLAGS" \
../configure \
  --build=%_build --host=%_host \
  --target=%{mingw32_target} \
  --disable-nls \
  --with-sysroot=%{mingw32_sysroot} \
  --prefix=%{_prefix} \
  --bindir=%{_bindir} \
  --includedir=%{_includedir} \
  --libdir=%{_libdir} \
  --mandir=%{_mandir} \
  --infodir=%{_infodir}

make all %{?_smp_mflags}
popd

mkdir build_win64
pushd build_win64
CFLAGS="$RPM_OPT_FLAGS" \
../configure \
  --build=%_build --host=%_host \
  --target=%{mingw64_target} \
  --disable-nls \
  --with-sysroot=%{mingw64_sysroot} \
  --prefix=%{_prefix} \
  --bindir=%{_bindir} \
  --includedir=%{_includedir} \
  --libdir=%{_libdir} \
  --mandir=%{_mandir} \
  --infodir=%{_infodir}

make all %{?_smp_mflags}
popd

# Create multilib versions for the tools strip, objdump nm, and objcopy
mkdir build_multilib
pushd build_multilib
CFLAGS="$RPM_OPT_FLAGS" \
../configure \
  --build=%_build --host=%_host \
  --target=%{mingw64_target} \
  --enable-targets=%{mingw64_target},%{mingw32_target} \
  --disable-nls \
  --with-sysroot=%{mingw64_sysroot} \
  --prefix=%{_prefix} \
  --bindir=%{_bindir} \
  --includedir=%{_includedir} \
  --libdir=%{_libdir} \
  --mandir=%{_mandir} \
  --infodir=%{_infodir}

make %{?_smp_mflags}
popd


%check
%if !%{run_testsuite}
echo ====================TESTSUITE DISABLED=========================
%else
pushd build_win32
  make -k check < /dev/null || :
  echo ====================TESTING WIN32 =========================
  cat {gas/testsuite/gas,ld/ld,binutils/binutils}.sum
  echo ====================TESTING WIN32 END=====================
  for file in {gas/testsuite/gas,ld/ld,binutils/binutils}.{sum,log}
  do
    ln $file binutils-%{mingw32_target}-$(basename $file) || :
  done
  tar cjf binutils-%{mingw32_target}.tar.bz2 binutils-%{mingw32_target}-*.{sum,log}
  uuencode binutils-%{mingw32_target}.tar.bz2 binutils-%{mingw32_target}.tar.bz2
  rm -f binutils-%{mingw32_target}.tar.bz2 binutils-%{mingw32_target}-*.{sum,log}
popd

pushd build_win64
  make -k check < /dev/null || :
  echo ====================TESTING WIN64 =========================
  cat {gas/testsuite/gas,ld/ld,binutils/binutils}.sum
  echo ====================TESTING WIN64 END=====================
  for file in {gas/testsuite/gas,ld/ld,binutils/binutils}.{sum,log}
  do
    ln $file binutils-%{mingw64_target}-$(basename $file) || :
  done
  tar cjf binutils-%{mingw64_target}.tar.bz2 binutils-%{mingw64_target}-*.{sum,log}
  uuencode binutils-%{mingw64_target}.tar.bz2 binutils-%{mingw64_target}.tar.bz2
  rm -f binutils-%{mingw64_target}.tar.bz2 binutils-%{mingw64_target}-*.{sum,log}
popd
%endif


%install
%mingw_make_install DESTDIR=$RPM_BUILD_ROOT
make -C build_multilib DESTDIR=$RPM_BUILD_ROOT/multilib install

# These files conflict with ordinary binutils.
rm -rf $RPM_BUILD_ROOT%{_infodir}
rm -f $RPM_BUILD_ROOT%{_libdir}/libiberty*

# Keep the multilib versions of the strip, objdump and objcopy commands
# We need these for the RPM integration as these tools must be able to
# both process win32 and win64 binaries
mv $RPM_BUILD_ROOT/multilib%{_bindir}/%{mingw64_strip} $RPM_BUILD_ROOT%{_bindir}/%{mingw_strip}
mv $RPM_BUILD_ROOT/multilib%{_bindir}/%{mingw64_objdump} $RPM_BUILD_ROOT%{_bindir}/%{mingw_objdump}
mv $RPM_BUILD_ROOT/multilib%{_bindir}/%{mingw64_objcopy} $RPM_BUILD_ROOT%{_bindir}/%{mingw_objcopy}
mv $RPM_BUILD_ROOT/multilib%{_bindir}/%{mingw64_nm} $RPM_BUILD_ROOT%{_bindir}/%{mingw_nm}
rm -rf $RPM_BUILD_ROOT/multilib


%files -n mingw-binutils-generic
%doc COPYING
%{_mandir}/man1/*
%{_bindir}/%{mingw_strip}
%{_bindir}/%{mingw_objdump}
%{_bindir}/%{mingw_objcopy}
%{_bindir}/%{mingw_nm}

%files -n mingw32-binutils
%{_bindir}/%{mingw32_target}-addr2line
%{_bindir}/%{mingw32_target}-ar
%{_bindir}/%{mingw32_target}-as
%{_bindir}/%{mingw32_target}-c++filt
%{_bindir}/%{mingw32_target}-dlltool
%{_bindir}/%{mingw32_target}-dllwrap
%{_bindir}/%{mingw32_target}-elfedit
%{_bindir}/%{mingw32_target}-gprof
%{_bindir}/%{mingw32_target}-ld
%{_bindir}/%{mingw32_target}-ld.bfd
%{_bindir}/%{mingw32_target}-nm
%{_bindir}/%{mingw32_target}-objcopy
%{_bindir}/%{mingw32_target}-objdump
%{_bindir}/%{mingw32_target}-ranlib
%{_bindir}/%{mingw32_target}-readelf
%{_bindir}/%{mingw32_target}-size
%{_bindir}/%{mingw32_target}-strings
%{_bindir}/%{mingw32_target}-strip
%{_bindir}/%{mingw32_target}-windmc
%{_bindir}/%{mingw32_target}-windres
%{_prefix}/%{mingw32_target}/bin/ar
%{_prefix}/%{mingw32_target}/bin/as
%{_prefix}/%{mingw32_target}/bin/dlltool
%{_prefix}/%{mingw32_target}/bin/ld
%{_prefix}/%{mingw32_target}/bin/ld.bfd
%{_prefix}/%{mingw32_target}/bin/nm
%{_prefix}/%{mingw32_target}/bin/objcopy
%{_prefix}/%{mingw32_target}/bin/objdump
%{_prefix}/%{mingw32_target}/bin/ranlib
%{_prefix}/%{mingw32_target}/bin/readelf
%{_prefix}/%{mingw32_target}/bin/strip
%{_prefix}/%{mingw32_target}/lib/ldscripts

%files -n mingw64-binutils
%{_bindir}/%{mingw64_target}-addr2line
%{_bindir}/%{mingw64_target}-ar
%{_bindir}/%{mingw64_target}-as
%{_bindir}/%{mingw64_target}-c++filt
%{_bindir}/%{mingw64_target}-dlltool
%{_bindir}/%{mingw64_target}-dllwrap
%{_bindir}/%{mingw64_target}-elfedit
%{_bindir}/%{mingw64_target}-gprof
%{_bindir}/%{mingw64_target}-ld
%{_bindir}/%{mingw64_target}-ld.bfd
%{_bindir}/%{mingw64_target}-nm
%{_bindir}/%{mingw64_target}-objcopy
%{_bindir}/%{mingw64_target}-objdump
%{_bindir}/%{mingw64_target}-ranlib
%{_bindir}/%{mingw64_target}-readelf
%{_bindir}/%{mingw64_target}-size
%{_bindir}/%{mingw64_target}-strings
%{_bindir}/%{mingw64_target}-strip
%{_bindir}/%{mingw64_target}-windmc
%{_bindir}/%{mingw64_target}-windres
%{_prefix}/%{mingw64_target}/bin/ar
%{_prefix}/%{mingw64_target}/bin/as
%{_prefix}/%{mingw64_target}/bin/dlltool
%{_prefix}/%{mingw64_target}/bin/ld
%{_prefix}/%{mingw64_target}/bin/ld.bfd
%{_prefix}/%{mingw64_target}/bin/nm
%{_prefix}/%{mingw64_target}/bin/objcopy
%{_prefix}/%{mingw64_target}/bin/objdump
%{_prefix}/%{mingw64_target}/bin/ranlib
%{_prefix}/%{mingw64_target}/bin/readelf
%{_prefix}/%{mingw64_target}/bin/strip
%{_prefix}/%{mingw64_target}/lib/ldscripts


%changelog
* Thu Oct 08 2020 Zhiyi Weng <zhiyi@iscas.ac.cn> - 2.32-7
- Initial version
