From 4b3b7c0eda80d00663b057e5003be229eb0b43cd Mon Sep 17 00:00:00 2001 From: Nick Clifton Date: Mon, 4 Jan 2021 14:24:14 +0000 Subject: [PATCH] Fix linking mixed SHF_LINK_ORDER and non-SHF_LINK_ORDER sections. (#1907945) --- binutils-SHF_LINK_ORDER.patch | 223 ++++++++++++++++++++++++++++++++++ binutils.spec | 17 +-- spec.binutils.cross | 63 +++++----- 3 files changed, 261 insertions(+), 42 deletions(-) create mode 100644 binutils-SHF_LINK_ORDER.patch diff --git a/binutils-SHF_LINK_ORDER.patch b/binutils-SHF_LINK_ORDER.patch new file mode 100644 index 0000000..98a8a62 --- /dev/null +++ b/binutils-SHF_LINK_ORDER.patch @@ -0,0 +1,223 @@ +diff -rup binutils.orig/bfd/bfd-in2.h binutils-2.35.1/bfd/bfd-in2.h +--- binutils.orig/bfd/bfd-in2.h 2021-01-04 13:18:10.234368481 +0000 ++++ binutils-2.35.1/bfd/bfd-in2.h 2021-01-04 13:18:20.596301287 +0000 +@@ -1177,6 +1177,9 @@ typedef struct bfd_section + struct bfd_symbol *symbol; + struct bfd_symbol **symbol_ptr_ptr; + ++ /* The matching section name pattern in linker script. */ ++ const char *pattern; ++ + /* Early in the link process, map_head and map_tail are used to build + a list of input sections attached to an output section. Later, + output sections use these fields for a list of bfd_link_order +@@ -1370,8 +1373,8 @@ discarded_section (const asection *sec) + /* target_index, used_by_bfd, constructor_chain, owner, */ \ + 0, NULL, NULL, NULL, \ + \ +- /* symbol, symbol_ptr_ptr, */ \ +- (struct bfd_symbol *) SYM, &SEC.symbol, \ ++ /* symbol, symbol_ptr_ptr, pattern, */ \ ++ (struct bfd_symbol *) SYM, &SEC.symbol, NULL, \ + \ + /* map_head, map_tail, already_assigned */ \ + { NULL }, { NULL }, NULL \ +diff -rup binutils.orig/bfd/elflink.c binutils-2.35.1/bfd/elflink.c +--- binutils.orig/bfd/elflink.c 2021-01-04 13:18:10.223368552 +0000 ++++ binutils-2.35.1/bfd/elflink.c 2021-01-04 13:18:20.599301268 +0000 +@@ -11662,8 +11662,21 @@ compare_link_order (const void *a, const + const struct bfd_link_order *blo = *(const struct bfd_link_order **) b; + asection *asec = elf_linked_to_section (alo->u.indirect.section); + asection *bsec = elf_linked_to_section (blo->u.indirect.section); +- bfd_vma apos = asec->output_section->lma + asec->output_offset; +- bfd_vma bpos = bsec->output_section->lma + bsec->output_offset; ++ bfd_vma apos, bpos; ++ ++ /* Check if any sections are unordered. */ ++ if (asec == NULL || bsec == NULL) ++ { ++ /* Place ordered sections before unordered sections. */ ++ if (bsec != NULL) ++ return 1; ++ else if (asec != NULL) ++ return -1; ++ return 0; ++ } ++ ++ apos = asec->output_section->lma + asec->output_offset; ++ bpos = bsec->output_section->lma + bsec->output_offset; + + if (apos < bpos) + return -1; +@@ -11698,14 +11711,14 @@ compare_link_order (const void *a, const + sections. Ideally we'd do this in the linker proper. */ + + static bfd_boolean +-elf_fixup_link_order (bfd *abfd, asection *o) ++elf_fixup_link_order (struct bfd_link_info *info, bfd *abfd, asection *o) + { + size_t seen_linkorder; + size_t seen_other; + size_t n; + struct bfd_link_order *p; + bfd *sub; +- struct bfd_link_order **sections; ++ struct bfd_link_order **sections, **indirect_sections; + asection *other_sec, *linkorder_sec; + bfd_vma offset; /* Octets. */ + +@@ -11736,7 +11749,9 @@ elf_fixup_link_order (bfd *abfd, asectio + else + seen_other++; + +- if (seen_other && seen_linkorder) ++ /* Allow mixed ordered and unordered input sections for ++ non-relocatable link. */ ++ if (bfd_link_relocatable (info) && seen_other && seen_linkorder) + { + if (other_sec && linkorder_sec) + _bfd_error_handler +@@ -11756,6 +11771,10 @@ elf_fixup_link_order (bfd *abfd, asectio + if (!seen_linkorder) + return TRUE; + ++ /* Non-relocatable output can have both ordered and unordered input ++ sections. */ ++ seen_linkorder += seen_other; ++ + sections = bfd_malloc (seen_linkorder * sizeof (*sections)); + if (sections == NULL) + return FALSE; +@@ -11764,22 +11783,51 @@ elf_fixup_link_order (bfd *abfd, asectio + for (p = o->map_head.link_order; p != NULL; p = p->next) + sections[seen_linkorder++] = p; + +- /* Sort the input sections in the order of their linked section. */ +- qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order); ++ for (indirect_sections = sections, n = 0; n < seen_linkorder;) ++ { ++ /* Find the first bfd_indirect_link_order section. */ ++ if (indirect_sections[0]->type == bfd_indirect_link_order) ++ { ++ /* Count the consecutive bfd_indirect_link_order sections ++ with the same pattern. */ ++ size_t i, n_indirect; ++ const char *pattern ++ = indirect_sections[0]->u.indirect.section->pattern; ++ for (i = n + 1; i < seen_linkorder; i++) ++ if (sections[i]->type != bfd_indirect_link_order ++ || sections[i]->u.indirect.section->pattern != pattern) ++ break; ++ n_indirect = i - n; ++ /* Sort the bfd_indirect_link_order sections in the order of ++ their linked section. */ ++ qsort (indirect_sections, n_indirect, sizeof (*sections), ++ compare_link_order); ++ indirect_sections += n_indirect; ++ n += n_indirect; ++ } ++ else ++ { ++ indirect_sections++; ++ n++; ++ } ++ } + +- /* Change the offsets of the sections. */ ++ /* Change the offsets of the bfd_indirect_link_order sections. */ + offset = 0; + for (n = 0; n < seen_linkorder; n++) +- { +- bfd_vma mask; +- asection *s = sections[n]->u.indirect.section; +- unsigned int opb = bfd_octets_per_byte (abfd, s); +- +- mask = ~(bfd_vma) 0 << s->alignment_power * opb; +- offset = (offset + ~mask) & mask; +- sections[n]->offset = s->output_offset = offset / opb; +- offset += sections[n]->size; +- } ++ if (sections[n]->type == bfd_indirect_link_order) ++ { ++ bfd_vma mask; ++ asection *s = sections[n]->u.indirect.section; ++ unsigned int opb = bfd_octets_per_byte (abfd, s); ++ ++ mask = ~(bfd_vma) 0 << s->alignment_power * opb; ++ offset = (offset + ~mask) & mask; ++ sections[n]->offset = s->output_offset = offset / opb; ++ offset += sections[n]->size; ++ } ++ else ++ offset = sections[n]->offset + sections[n]->size; + + free (sections); + return TRUE; +@@ -12408,7 +12456,7 @@ bfd_elf_final_link (bfd *abfd, struct bf + /* Reorder SHF_LINK_ORDER sections. */ + for (o = abfd->sections; o != NULL; o = o->next) + { +- if (!elf_fixup_link_order (abfd, o)) ++ if (!elf_fixup_link_order (info, abfd, o)) + return FALSE; + } + +diff -rup binutils.orig/bfd/section.c binutils-2.35.1/bfd/section.c +--- binutils.orig/bfd/section.c 2021-01-04 13:18:10.233368487 +0000 ++++ binutils-2.35.1/bfd/section.c 2021-01-04 13:18:20.599301268 +0000 +@@ -541,6 +541,9 @@ CODE_FRAGMENT + . struct bfd_symbol *symbol; + . struct bfd_symbol **symbol_ptr_ptr; + . ++. {* The matching section name pattern in linker script. *} ++. const char *pattern; ++. + . {* Early in the link process, map_head and map_tail are used to build + . a list of input sections attached to an output section. Later, + . output sections use these fields for a list of bfd_link_order +@@ -734,8 +737,8 @@ CODE_FRAGMENT + . {* target_index, used_by_bfd, constructor_chain, owner, *} \ + . 0, NULL, NULL, NULL, \ + . \ +-. {* symbol, symbol_ptr_ptr, *} \ +-. (struct bfd_symbol *) SYM, &SEC.symbol, \ ++. {* symbol, symbol_ptr_ptr, pattern, *} \ ++. (struct bfd_symbol *) SYM, &SEC.symbol, NULL, \ + . \ + . {* map_head, map_tail, already_assigned *} \ + . { NULL }, { NULL }, NULL \ +diff -rup binutils.orig/gas/config/obj-elf.c binutils-2.35.1/gas/config/obj-elf.c +--- binutils.orig/gas/config/obj-elf.c 2021-01-04 13:18:09.942370375 +0000 ++++ binutils-2.35.1/gas/config/obj-elf.c 2021-01-04 13:18:20.599301268 +0000 +@@ -659,7 +659,9 @@ obj_elf_change_section (const char *name + } + } + +- if (old_sec == NULL && ((attr & ~(SHF_MASKOS | SHF_MASKPROC)) ++ if (old_sec == NULL && ((attr & ~(SHF_LINK_ORDER ++ | SHF_MASKOS ++ | SHF_MASKPROC)) + & ~ssect->attr) != 0) + { + /* As a GNU extension, we permit a .note section to be +diff -rup binutils.orig/ld/ldlang.c binutils-2.35.1/ld/ldlang.c +--- binutils.orig/ld/ldlang.c 2021-01-04 13:18:09.691372002 +0000 ++++ binutils-2.35.1/ld/ldlang.c 2021-01-04 13:18:20.600301261 +0000 +@@ -7421,7 +7421,7 @@ lang_reset_memory_regions (void) + + static void + gc_section_callback (lang_wild_statement_type *ptr, +- struct wildcard_list *sec ATTRIBUTE_UNUSED, ++ struct wildcard_list *sec, + asection *section, + struct flag_info *sflag_info ATTRIBUTE_UNUSED, + lang_input_statement_type *file ATTRIBUTE_UNUSED, +@@ -7431,6 +7431,8 @@ gc_section_callback (lang_wild_statement + should be as well. */ + if (ptr->keep_sections) + section->flags |= SEC_KEEP; ++ if (sec) ++ section->pattern = sec->spec.name; + } + + /* Iterate over sections marking them against GC. */ diff --git a/binutils.spec b/binutils.spec index ebf6386..f82e5e9 100644 --- a/binutils.spec +++ b/binutils.spec @@ -39,7 +39,7 @@ Summary: A GNU collection of binary utilities Name: binutils%{?name_cross}%{?_with_debug:-debug} Version: 2.35.1 -Release: 16%{?dist} +Release: 17%{?dist} License: GPLv3+ URL: https://sourceware.org/binutils @@ -48,6 +48,8 @@ URL: https://sourceware.org/binutils # Binutils SPEC file. Can be invoked with the following parameters to change # the default behaviour: +# --define "binutils_target arm-linux-gnu" to create arm-linux-gnu-binutils. +# # --with bootstrap Build with minimal dependencies. # --with debug Build without optimizations and without splitting # the debuginfo into a separate file. @@ -138,14 +140,13 @@ URL: https://sourceware.org/binutils # too many controversial patches so we stick with the official FSF version # instead. -Source: https://ftp.gnu.org/gnu/binutils/binutils-%%{version}.tar.xz +Source: https://ftp.gnu.org/gnu/binutils/binutils-%{version}.tar.xz Source2: binutils-2.19.50.0.1-output-format.sed #---------------------------------------------------------------------------- Patch00: binutils-2.35.1-update.patch - # Purpose: Use /lib64 and /usr/lib64 instead of /lib and /usr/lib in the # default library search path of 64-bit targets. # Lifetime: Permanent, but it should not be. This is a bug in the libtool @@ -286,12 +287,9 @@ Patch26: binutils-plugin-as-needed.patch # Lifetime: Fixed in 2.36 Patch27: binutils-recursive-debuglink-following.patch -# Purpose: Fix the DWARF parser to skip DW_FORM_ref_addr types -# when attempting to determine a type's signedness. +# Purpose: Correctly handle mixed SHF_LINK_ORDER and non-SHF_LINK_ORDER sections. # Lifetime: Fixed in 2.36 -# Patch28: binutils-dwarf-type-sign.patch -# Patch29: binutils-dwarf-type-sign-2.patch -# Patch30: binutils-dwarf-DW_FORM_ref8.patch +Patch28: binutils-SHF_LINK_ORDER.patch #---------------------------------------------------------------------------- @@ -872,6 +870,9 @@ exit 0 #---------------------------------------------------------------------------- %changelog +* Mon Jan 04 2021 Nick Clifton - 2.35.1-17 +- Fix linking mixed SHF_LINK_ORDER and non-SHF_LINK_ORDER sections. (#1907945) + * Thu Nov 26 2020 Florian Weimer - 2.35.1-16 - NVR bump for toolchain rebuild diff --git a/spec.binutils.cross b/spec.binutils.cross index 1b6f1e4..13a41d5 100644 --- a/spec.binutils.cross +++ b/spec.binutils.cross @@ -2,70 +2,65 @@ # Note - this is an rpm spec file, but it has been renamed in order to avoid # conflict with the real binutils.spec file. # -# Its only use is to build a set of cross-binutils rpms by installing it -# into an rpmbuild tree where the binutils source rpm has already been placed. -# It should be run as: +# Its only use is to build a set of cross-binutils rpms by putting it into +# the SPECS directory of an rpmbuild tree, installing the binutils source +# rpm into the SOURCES directory and then running: # # rpmbuild -bb spec.binutils.cross Summary: A meta collection of GNU binutils executables for cross builds Name: cross-binutils - -# Note: KEEP THE Version and Release fields IN SYNC with binutils.spec -Version: 2.35.1 -Release: 14%{?dist} - License: GPLv3+ URL: https://sourceware.org/binutils -Source: binutils.spec +#---------------------------------------------------------------------------- +# +# Configurable settings - adjust to match your needs: +# + +Version: 2.30 +Release: 89.el8 + +# The list of cross architectures to build. +# Note: this list assumes that we are building on an x86_64-linux-gnu host. + +%define arch_list aarch64-linux-gnu ppc64le-linux-gnu s390x-linux-gnu + +#---------------------------------------------------------------------------- + +%define binutils_source_rpm binutils-%{version}-%{release}.src.rpm + +Source: %{binutils_source_rpm} # Provides: bundled(libiberty) BuildRequires: autoconf automake BuildRequires: perl, sed, coreutils -BuildRequires: gcc +BuildRequires: gcc, bison BuildRequires: gettext, flex, zlib-devel -BuildRequires: findutils +BuildRequires: findutils, texinfo BuildRequires: dejagnu, zlib-static, glibc-static, sharutils, bc -BuildRequires: elfutils-debuginfod-client-devel +# BuildRequires: elfutils-debuginfod-client-devel BuildRequires: rpm-build -Requires(post): %{_sbindir}/alternatives -Requires(post): coreutils -Requires(preun): %{_sbindir}/alternatives - #---------------------------------------------------------------------------- %description Provides a collection of cross built binutils for the targets -supported by RHEL (aarch64, i686, ppc64le, s390x). - -# Note - this list assumes that we are building on an x86_64-linux-gnu host. -%define arch_list aarch64-linux-gnu i686-linux-gnu ppc64le-linux-gnu s390x-linux-gnu - -#---------------------------------------------------------------------------- - -%prep +supported by RHEL (aarch64, ppc64le, s390x). #---------------------------------------------------------------------------- %build +# Install the binutils sources. +rpm -ivh %{_sourcedir}/%{binutils_source_rpm} + # Build each set of cross binutils individually. - for f in %{arch_list}; do - rpmbuild -bb --define "binutils_target $f" %{_specdir}/binutils.spec --without testsuite + rpmbuild -bb --define "binutils_target $f" %{_specdir}/binutils.spec --without testsuite --without gold done -#---------------------------------------------------------------------------- - -%install - -#---------------------------------------------------------------------------- - -%files - #---------------------------------------------------------------------------- %changelog * Wed Nov 11 2020 Nick Clifton - 2.35.1-14