[PATCH] ELF: Don't check relocations in non-loaded, non-alloced sections

Fangrui Song i@maskray.me
Wed Jun 3 00:05:48 GMT 2020


I haven't really read the patch, but just wanted to express a point.
It is sometimes useful to check relocations even for a non-SHF_ALLOC
section. For example, a PC relative relocation type does not make
sense from a non-SHF_ALLOC section referencing a SHF_ALLOC section.

Conceptually, even if a non-SHF_ALLOC is loaded as part of the memory
image, the distance between it and a SHF_ALLOC section may not be a
constant, so the linker cannot reasonably resolve the relocation.

On Tue, Jun 2, 2020 at 1:38 PM H.J. Lu via Binutils
<binutils@sourceware.org> wrote:
>
> Don't do anything special with non-loaded, non-alloced sections.
> In particular, any relocs in such sections should not affect GOT
> and PLT reference counting (ie. we don't allow them to create GOT
> or PLT entries), there's no possibility or desire to optimize TLS
> relocs, and there's not much point in propagating relocs to shared
> libs that the dynamic linker won't relocate.
>
> I will check it in if there are no objections.
>
> H.J.
> ---
>         * elf32-i386.c (elf_i386_check_relocs): Remove SEC_ALLOC check.
>         * elf32-lm32.c (lm32_elf_check_relocs): Likewise.
>         * elf32-m32r.c (m32r_elf_check_relocs): Likewise.
>         * elf32-nds32.c (nds32_elf_check_relocs): Likewise.
>         * elf32-or1k.c (or1k_elf_check_relocs): Likewise.
>         * elf32-ppc.c (ppc_elf_check_relocs): Likewise.
>         * elf32-sh.c (sh_elf_check_relocs): Likewise.
>         * elf32-xtensa.c (elf_xtensa_check_relocs): Likewise.
>         * elf64-alpha.c (elf64_alpha_check_relocs): Likewise.
>         * elf64-ppc.c (ppc64_elf_check_relocs): Likewise.
>         * elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
>         * elflink.c (_bfd_elf_link_check_relocs): Skip non-loaded,
>         non-alloced sections.
> ---
>  bfd/elf32-i386.c   |  9 ---------
>  bfd/elf32-lm32.c   |  9 ---------
>  bfd/elf32-m32r.c   |  9 ---------
>  bfd/elf32-nds32.c  |  9 ---------
>  bfd/elf32-or1k.c   |  9 ---------
>  bfd/elf32-ppc.c    |  9 ---------
>  bfd/elf32-sh.c     |  9 ---------
>  bfd/elf32-xtensa.c |  2 +-
>  bfd/elf64-alpha.c  |  9 ---------
>  bfd/elf64-ppc.c    |  9 ---------
>  bfd/elf64-x86-64.c |  9 ---------
>  bfd/elflink.c      | 12 ++++++++++--
>  12 files changed, 11 insertions(+), 93 deletions(-)
>
> diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
> index 544b931552..f6f669957c 100644
> --- a/bfd/elf32-i386.c
> +++ b/bfd/elf32-i386.c
> @@ -1487,15 +1487,6 @@ elf_i386_check_relocs (bfd *abfd,
>    if (bfd_link_relocatable (info))
>      return TRUE;
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>    htab = elf_x86_hash_table (info, I386_ELF_DATA);
>    if (htab == NULL)
>      {
> diff --git a/bfd/elf32-lm32.c b/bfd/elf32-lm32.c
> index 5d09f2d350..acef37af5d 100644
> --- a/bfd/elf32-lm32.c
> +++ b/bfd/elf32-lm32.c
> @@ -1128,15 +1128,6 @@ lm32_elf_check_relocs (bfd *abfd,
>    if (bfd_link_relocatable (info))
>      return TRUE;
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
>    sym_hashes = elf_sym_hashes (abfd);
>    sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
> diff --git a/bfd/elf32-m32r.c b/bfd/elf32-m32r.c
> index afe0ee899c..9b8e5cd124 100644
> --- a/bfd/elf32-m32r.c
> +++ b/bfd/elf32-m32r.c
> @@ -3450,15 +3450,6 @@ m32r_elf_check_relocs (bfd *abfd,
>    if (bfd_link_relocatable (info))
>      return TRUE;
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>    sreloc = NULL;
>    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
>    sym_hashes = elf_sym_hashes (abfd);
> diff --git a/bfd/elf32-nds32.c b/bfd/elf32-nds32.c
> index 4f7ea76469..3e094f6270 100644
> --- a/bfd/elf32-nds32.c
> +++ b/bfd/elf32-nds32.c
> @@ -7081,15 +7081,6 @@ nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>        return TRUE;
>      }
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
>    sym_hashes = elf_sym_hashes (abfd);
>    sym_hashes_end =
> diff --git a/bfd/elf32-or1k.c b/bfd/elf32-or1k.c
> index b141b45886..3ed44ccceb 100644
> --- a/bfd/elf32-or1k.c
> +++ b/bfd/elf32-or1k.c
> @@ -1880,15 +1880,6 @@ or1k_elf_check_relocs (bfd *abfd,
>    if (bfd_link_relocatable (info))
>      return TRUE;
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>    symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
>    sym_hashes = elf_sym_hashes (abfd);
>
> diff --git a/bfd/elf32-ppc.c b/bfd/elf32-ppc.c
> index 588b79781d..62c6270329 100644
> --- a/bfd/elf32-ppc.c
> +++ b/bfd/elf32-ppc.c
> @@ -2888,15 +2888,6 @@ ppc_elf_check_relocs (bfd *abfd,
>    if (bfd_link_relocatable (info))
>      return TRUE;
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>  #ifdef DEBUG
>    _bfd_error_handler ("ppc_elf_check_relocs called for section %pA in %pB",
>                       sec, abfd);
> diff --git a/bfd/elf32-sh.c b/bfd/elf32-sh.c
> index 0428829757..7c9e695981 100644
> --- a/bfd/elf32-sh.c
> +++ b/bfd/elf32-sh.c
> @@ -5376,15 +5376,6 @@ sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
>    if (bfd_link_relocatable (info))
>      return TRUE;
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>    BFD_ASSERT (is_sh_elf (abfd));
>
>    symtab_hdr = &elf_symtab_hdr (abfd);
> diff --git a/bfd/elf32-xtensa.c b/bfd/elf32-xtensa.c
> index 05c4f8430a..9dc815edbb 100644
> --- a/bfd/elf32-xtensa.c
> +++ b/bfd/elf32-xtensa.c
> @@ -1039,7 +1039,7 @@ elf_xtensa_check_relocs (bfd *abfd,
>    const Elf_Internal_Rela *rel;
>    const Elf_Internal_Rela *rel_end;
>
> -  if (bfd_link_relocatable (info) || (sec->flags & SEC_ALLOC) == 0)
> +  if (bfd_link_relocatable (info))
>      return TRUE;
>
>    BFD_ASSERT (is_xtensa_elf (abfd));
> diff --git a/bfd/elf64-alpha.c b/bfd/elf64-alpha.c
> index 4e4efae0b1..0b31d450dc 100644
> --- a/bfd/elf64-alpha.c
> +++ b/bfd/elf64-alpha.c
> @@ -1782,15 +1782,6 @@ elf64_alpha_check_relocs (bfd *abfd, struct bfd_link_info *info,
>    if (bfd_link_relocatable (info))
>      return TRUE;
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>    BFD_ASSERT (is_alpha_elf (abfd));
>
>    dynobj = elf_hash_table (info)->dynobj;
> diff --git a/bfd/elf64-ppc.c b/bfd/elf64-ppc.c
> index 49fda96be7..3941addd57 100644
> --- a/bfd/elf64-ppc.c
> +++ b/bfd/elf64-ppc.c
> @@ -4536,15 +4536,6 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
>    if (bfd_link_relocatable (info))
>      return TRUE;
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>    BFD_ASSERT (is_ppc64_elf (abfd));
>
>    htab = ppc_hash_table (info);
> diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
> index 183c808346..eada0e53ed 100644
> --- a/bfd/elf64-x86-64.c
> +++ b/bfd/elf64-x86-64.c
> @@ -1862,15 +1862,6 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
>    if (bfd_link_relocatable (info))
>      return TRUE;
>
> -  /* Don't do anything special with non-loaded, non-alloced sections.
> -     In particular, any relocs in such sections should not affect GOT
> -     and PLT reference counting (ie. we don't allow them to create GOT
> -     or PLT entries), there's no possibility or desire to optimize TLS
> -     relocs, and there's not much point in propagating relocs to shared
> -     libs that the dynamic linker won't relocate.  */
> -  if ((sec->flags & SEC_ALLOC) == 0)
> -    return TRUE;
> -
>    htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
>    if (htab == NULL)
>      {
> diff --git a/bfd/elflink.c b/bfd/elflink.c
> index a2b40ccb04..7cee0afac0 100644
> --- a/bfd/elflink.c
> +++ b/bfd/elflink.c
> @@ -3956,8 +3956,16 @@ _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
>           Elf_Internal_Rela *internal_relocs;
>           bfd_boolean ok;
>
> -         /* Don't check relocations in excluded sections.  */
> -         if ((o->flags & SEC_RELOC) == 0
> +         /* Don't check relocations in excluded sections.  Don't do
> +            anything special with non-loaded, non-alloced sections.
> +            In particular, any relocs in such sections should not
> +            affect GOT and PLT reference counting (ie.  we don't
> +            allow them to create GOT or PLT entries), there's no
> +            possibility or desire to optimize TLS relocs, and
> +            there's not much point in propagating relocs to shared
> +            libs that the dynamic linker won't relocate.  */
> +         if ((o->flags & SEC_ALLOC) == 0
> +             || (o->flags & SEC_RELOC) == 0
>               || (o->flags & SEC_EXCLUDE) != 0
>               || o->reloc_count == 0
>               || ((info->strip == strip_all || info->strip == strip_debugger)
> --
> 2.26.2
>


More information about the Binutils mailing list