Bug 20824

Summary: enable warn-shared-textrel by default
Product: binutils Reporter: ma.jiang
Component: ldAssignee: H.J. Lu <hjl.tools>
Status: RESOLVED FIXED    
Severity: enhancement CC: i, sam
Priority: P2    
Version: 2.35   
Target Milestone: 2.35   
URL: https://sourceware.org/pipermail/binutils/2020-May/111251.html
Host: Target:
Build: Last reconfirmed: 2020-05-25 00:00:00
Bug Depends on: 22909    
Bug Blocks:    
Attachments: enable warn-shared-textrel by default
A patch

Description ma.jiang 2016-11-15 08:21:38 UTC
Created attachment 9636 [details]
enable warn-shared-textrel by default

In gnu-ld, warn-shared-textrel is disabled by default. Why not to enable it by default? 
  One of our customers found that he did not have enough memory to run his application after a recompilation. The root cause turn out to be a silly mistake that he forgot to add "-fPIC" for his shared libraries. Yes yes, the one who make mistakes got to pay the price, it's very reasonable. But there were no warning at all, a normal user(not a expert) probably did not know  what was wrong (and how to fix). This does not seem reasonable...
  Although some arches(like x86-64) force all shared libraries to be PIC, there are some that does not. In my opinion, the linker should be a good place to make the warnings. So, warn-shared-textrel should be enable by default.
  attached patch enable "warn-shared-textrel" and add a new option to close this warning, is that ok?
Comment 1 Mike Frysinger 2016-11-15 15:16:21 UTC
patches should be sent to the binutils@sourceware.org mailing list for discussion.  there are problems with this particular patch, but that can be ironed out after we discuss whether we want to do this in the first place.

you'll also need to update gold.
Comment 2 ma.jiang 2016-11-16 10:04:50 UTC
(In reply to Mike Frysinger from comment #1)
> patches should be sent to the binutils@sourceware.org mailing list for
> discussion.  there are problems with this particular patch, but that can be
> ironed out after we discuss whether we want to do this in the first place.
> 
> you'll also need to update gold.

  Thanks for the reply. I have send a mail to  binutils@sourceware.org. 
  for the things about gold: we did not use the gold linker,so I am not familiar with it. if this behavior change were accepted by the binutils community, I will pay some time for the gold.
Comment 3 Fangrui Song 2020-05-24 22:38:34 UTC
See https://sourceware.org/bugzilla/show_bug.cgi?id=25694#c3

Instead of enabled-by-default --warn-shared-textrel, we can

* make bfd_link_info::error_textrel default to 1
* delete warn_shared_textrel
* make --warn-shared-textrel a no-op

The bi-state approach is used by LLD: either -z notext or -z text. There is no state where DT_TEXTREL/DF_TEXTREL is added on demand. The diagnostic should remind the user that -z notext may be needed.

ld.lld: error: can't create dynamic relocation R_386_32 against symbol: gdt in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in a.o
>>> referenced by a.o:(.text+0x2)
Comment 4 H.J. Lu 2020-05-25 03:37:56 UTC
Created attachment 12567 [details]
A patch
Comment 5 H.J. Lu 2020-05-25 13:57:54 UTC
A patch is posted at

https://sourceware.org/pipermail/binutils/2020-May/111251.html
Comment 6 Sourceware Commits 2020-05-28 11:23:19 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=b32632c49968cd03e952f9b63b32d9e9f1ddaf53

commit b32632c49968cd03e952f9b63b32d9e9f1ddaf53
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu May 28 04:21:04 2020 -0700

    ld: Add --enable-textrel-check=[no|yes|warning|error]
    
    Add a configure option, --enable-textrel-check=[no|yes|warning|error],
    to decide what ELF linker should do by default with DT_TEXTREL in an
    executable or shared library.
    
            PR ld/20824
            * NEWS: Mention --enable-textrel-check=[no|yes|warning|error].
            * configure.ac: Add --enable-textrel-check=[no|yes|warning|error].
            (DEFAULT_LD_TEXTREL_CHECK): New AC_DEFINE_UNQUOTED.
            (DEFAULT_LD_TEXTREL_CHECK_WARNING): Likewise.
            * ldmain.c (main): Initialize link_info.textrel_check to
            DEFAULT_LD_TEXTREL_CHECK.
            * lexsup.c (ld_options): Check DEFAULT_LD_TEXTREL_CHECK_WARNING.
            * config.in: Regenerated.
            * configure: Likewise.
Comment 7 H.J. Lu 2020-05-28 11:33:19 UTC
Fixed for 2.35.