Bug 22909 - Expand --warn-shared-textrel support to all executables, not only PIC objects
Summary: Expand --warn-shared-textrel support to all executables, not only PIC objects
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.35
: P2 enhancement
Target Milestone: 2.35
Assignee: H.J. Lu
URL: https://sourceware.org/pipermail/binu...
Keywords:
: 26035 (view as bug list)
Depends on:
Blocks: 20824
  Show dependency treegraph
 
Reported: 2018-03-01 22:13 UTC by Sergei Trofimovich
Modified: 2020-05-27 21:26 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2018-03-12 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergei Trofimovich 2018-03-01 22:13:26 UTC
Gentoo enables --warn-shared-textrel by default for and extends warning to all dynamic executables (PIE or not):
    https://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo/src/patchsets/binutils/2.17/66_all_binutils-2.17.50.0.2-warn-textrel.patch?revision=1.1&view=markup

What do you think of extending this option (or adding new one) to cover executables in upstream binutils?

    Something like --warn-textrel.

Lack of TEXTREL warning came up recently in gcc where gcc was built with TEXTRELs due to a platform-specific gcc bug:
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84553

The warning would be complementary to '-Wl,-z,-text' which converts problems of this kind to errors.

Related bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20824 : "enable warn-shared-textrel by default".
Comment 1 H.J. Lu 2018-03-12 19:58:22 UTC
Do you have a testcase which shows the problem on x86 targets?
Comment 2 Sergei Trofimovich 2018-05-08 22:30:18 UTC
I was not able to force x86 targets to generate TEXTREL executables.

Best I could do is to emit pseudo textrel but it does not cause TEXTREL section creation:

  // a.c
  #include <stdio.h>

  int _start() {
    __asm__ volatile (
      ".Ltext_reloc:"
      ".reloc .Ltext_reloc - _start, R_386_32, printf\n"
      ".long 0x90909090\n"
    );
  }

$ gcc -m32 a.c -O2 -o a -rdynamic -no-pie -nostdlib -lc -Wl,-q

Is there a way to force printf symbol ot be resolved to absolute address instead of .plt?
Comment 3 Sergei Trofimovich 2020-03-19 21:47:47 UTC
Gentoo still has that check enabled. A few days ago it found the problem in riscv64 as bug #25694 (a case of bug #22263).
Comment 4 Fangrui Song 2020-05-25 06:26:27 UTC
Duplicate of PR ld/20824

I think we've had enough text relocation related options. 
With the proposed --enable-textrel-check=error, this might be simple and easy:

* -z notext/-z textoff -> enable text relocations
* -z text -> disallow text relocations (error if unavoidable)
Comment 5 H.J. Lu 2020-05-25 12:30:58 UTC
*** Bug 26035 has been marked as a duplicate of this bug. ***
Comment 6 H.J. Lu 2020-05-25 13:52:59 UTC
A patch is posted at

https://sourceware.org/pipermail/binutils/2020-May/111250.html
Comment 7 Sourceware Commits 2020-05-27 11:57:17 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a6dbf402de65fe66f4ec99b56527dfd00d077cb6

commit a6dbf402de65fe66f4ec99b56527dfd00d077cb6
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed May 27 04:53:54 2020 -0700

    ld: Add --warn-textrel and obsolete --warn-shared-textrel
    
    --warn-shared-textrel and -z text apply to both shared object and PIE.
    Add --warn-textrel and obsolete --warn-shared-textrel.  Consolidate
    --warn-textrel and -z text/notext/textoff implementation.
    
    bfd/
    
            PR ld/22909
            * elflink.c (bfd_elf_final_link): Use bfd_link_textrel_check.
            Check bfd_link_dll when issue a DT_TEXTREL warning.
            * elfxx-x86.c (maybe_set_textrel): Likewise.
            (_bfd_x86_elf_size_dynamic_sections): Likewise.
    
    include/
    
            PR ld/22909
            * bfdlink.h (textrel_check_method): New enum.
            (bfd_link_textrel_check): New.
            (bfd_link_info): Replace warn_shared_textrel and error_textrel
            with textrel_check.
    
    ld/
    
            PR ld/22909
            * NEWS: Mention --warn-textrel.
            * ld.texi: Update -z text/notext/textoff.  Add --warn-textrel.
            Remove --warn-shared-textrel.
            * ldlex.h (option_values): Rename OPTION_WARN_SHARED_TEXTREL to
            OPTION_WARN_TEXTREL.
            * lexsup.c (ld_options): Add --warn-textrel.  Obsolete
            --warn-shared-textrel.
            (parse_args): Updated.
            (elf_shlib_list_options): Check link_info.textrel_check.
            * emultempl/elf.em: Updated.
            * testsuite/ld-elf/pr19539.d: Replace -z notext with
            --warn-textrel.  Expect a warning.
            * testsuite/ld-i386/warn1.d: Update expected warning.
Comment 8 H.J. Lu 2020-05-27 11:58:35 UTC
Fixed for 2.35.
Comment 9 Sourceware Commits 2020-05-27 21:26:26 UTC
The master branch has been updated by Hans-Peter Nilsson <hp@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=2a37823bfe57d43f6c43a48a00237ba169599c3c

commit 2a37823bfe57d43f6c43a48a00237ba169599c3c
Author: Hans-Peter Nilsson <hp@axis.com>
Date:   Wed May 27 23:14:45 2020 +0200

    PR ld/22909 amendment; don't xfail ld-elf/pr19539.d for cris*-*-*.
    
    Noticed by my autotester covering cris-axis-linux-gnu:
    ...
    Running <x>/src/ld/testsuite/ld-elf/elf.exp ...
    XPASS: ld-elf/pr19539
    ...
    
    This was apparently as intended, so I'm just adjusting the xfail.
    
            PR ld/22909
            * testsuite/ld-elf/pr19539.d: Don't xfail for cris*-*-*.