From 8fdd7217a7e29f1ffac4abb2bffbf5a6555b85b7 Mon Sep 17 00:00:00 2001
From: Nick Clifton <nickc@redhat.com>
Date: Thu, 7 Oct 2004 14:45:24 +0000
Subject: [PATCH] Add linker option: --warn-shared-textrel to produce warnings
 when adding a DT_TEXTREL to a shared object.

---
 bfd/ChangeLog     |  6 ++++++
 bfd/elflink.c     | 28 ++++++++++++++++------------
 include/ChangeLog |  4 ++++
 include/bfdlink.h |  3 +++
 ld/ChangeLog      |  8 ++++++++
 ld/NEWS           | 14 ++++++++------
 ld/ld.texinfo     |  4 ++++
 ld/ldmain.c       |  1 +
 ld/lexsup.c       | 37 ++++++++++++++++++++++---------------
 9 files changed, 72 insertions(+), 33 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 6a4e7449e5e..0500b9bda0d 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,9 @@
+2004-10-07  Jeff Baker  <jbaker@qnx.com>
+
+	* elflink.c (_bfd_elf_add_dynamic_entry): Add code to warn if
+	adding a DT_TEXTREL to a shared object and --warn-shared-textrel
+	was specified.
+
 2004-10-04  Roland McGrath  <roland@redhat.com>
 
 	* hash.c (bfd_hash_set_default_size): Use const for table.
diff --git a/bfd/elflink.c b/bfd/elflink.c
index e93145d6c24..69fdcc044bf 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -2,21 +2,21 @@
    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
    Free Software Foundation, Inc.
 
-This file is part of BFD, the Binary File Descriptor library.
+   This file is part of BFD, the Binary File Descriptor library.
 
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "bfd.h"
 #include "sysdep.h"
@@ -2701,6 +2701,10 @@ _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
   if (! is_elf_hash_table (hash_table))
     return FALSE;
 
+  if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
+    _bfd_error_handler
+      (_("warning: creating a DT_TEXTREL in a shared object."));
+
   bed = get_elf_backend_data (hash_table->dynobj);
   s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
   BFD_ASSERT (s != NULL);
diff --git a/include/ChangeLog b/include/ChangeLog
index b6c11568fb4..9ddcc72b228 100644
--- a/include/ChangeLog
+++ b/include/ChangeLog
@@ -1,3 +1,7 @@
+2004-10-07  Jeff Baker  <jbaker@qnx.com>
+
+	* bfdlink.h (bfd_link_info): Add bitfield: warn_shared_textrel.
+
 2004-09-17  Paul Brook  <paul@codesourcery.com>
 
 	* elf/arm.h: Remove R_ARM_STKCHK and R_ARM_THM_STKCHK.
diff --git a/include/bfdlink.h b/include/bfdlink.h
index 5aa72d4b4c2..db276b02c99 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -308,6 +308,9 @@ struct bfd_link_info
   /* TRUE if PT_GNU_RELRO segment should be created.  */
   unsigned int relro: 1;
 
+  /* TRUE if we should warn when adding a DT_TEXTREL to a shared object.  */
+  unsigned int warn_shared_textrel: 1;
+
   /* What to do with unresolved symbols in an object file.
      When producing executables the default is GENERATE_ERROR.
      When producing shared libraries the default is IGNORE.  The
diff --git a/ld/ChangeLog b/ld/ChangeLog
index d4dc6cccc3d..21095bf42b3 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,11 @@
+2004-10-07  Jeff Baker  <jbaker@qnx.com>
+	
+	* lexsup.c: Handle --warn-shared-textrel option.
+	(ld_options): Restore alpha sorting of options.
+	* ldmain.c (main): Initialise warn_shared_info field to FALSE.
+	* ld.texinfo: Add documentation for --warn-shared-textrel.
+	* NEWS: Added mention of --warn-shared-textrel option.
+
 2004-10-05  Tomer Levi  <Tomer.Levi@nsc.com>
  
 	* emultempl/crxelf.em (disable_relaxation): Add.
diff --git a/ld/NEWS b/ld/NEWS
index c264ae9752c..403b8237a0a 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,8 @@
 -*- text -*-
 
+* ELF: --warn-shared-textrel option to warn if adding a DT_TEXTREL to a shared
+  object.
+
 * Added SORT_BY_NAME and SORT_BY_ALIGNMENT to the linker script
   language to permit sorting sections by section name or section
   maximum alignment.
@@ -7,9 +10,8 @@
 * Added a new linker command line switch, --sort-section name|alignment,
   to sort sections by section name or maximum alignment.
 
-* New ELF --add-needed/--no-add-needed options to control if a
-DT_NEEDED tag should be added when a shared library comes from
-DT_NEEDED tags.
+* ELF: --add-needed/--no-add-needed options to control if a DT_NEEDED tag
+  should be added when a shared library comes from DT_NEEDED tags.
 
 * Support for the crx-elf target added.
 
@@ -29,10 +31,10 @@ DT_NEEDED tags.
 
 Changes in 2.15:
 
-* New ELF --as-needed/--no-as-needed options to control if a DT_NEEDED
-tag should be added only when a shared library is referenced.
+* ELF: --as-needed/--no-as-needed options to control if a DT_NEEDED tag should
+  be added only when a shared library is referenced.
   
-* New PE --large-address-aware option to indicate executables support virtual
+* PE: --large-address-aware option to indicate executables support virtual
   addresses greater than 2 gigabytes.
 
 * DWARF 2 support for i386pe added.
diff --git a/ld/ld.texinfo b/ld/ld.texinfo
index d6f9c254587..420065b6c9f 100644
--- a/ld/ld.texinfo
+++ b/ld/ld.texinfo
@@ -1690,6 +1690,10 @@ The address will only be changed if it not explicitly specified; that
 is, if the @code{SECTIONS} command does not specify a start address for
 the section (@pxref{SECTIONS}).
 
+@kindex --warn-shared-textrel
+@item --warn-shared-textrel
+Warn if the linker adds a DT_TEXTREL to a shared object.  
+
 @kindex --warn-unresolved-symbols
 @item --warn-unresolved-symbols
 If the linker is going to report an unresolved symbol (see the option
diff --git a/ld/ldmain.c b/ld/ldmain.c
index 8274d36dcd1..dc2efa22302 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -336,6 +336,7 @@ main (int argc, char **argv)
   link_info.flags = 0;
   link_info.flags_1 = 0;
   link_info.need_relax_finalize = FALSE;
+  link_info.warn_shared_textrel = FALSE;
 
   ldfile_add_arch ("");
 
diff --git a/ld/lexsup.c b/ld/lexsup.c
index 2340006223d..e40cf6226f7 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -147,6 +147,7 @@ enum option_values
   OPTION_UNRESOLVED_SYMBOLS,
   OPTION_WARN_UNRESOLVED_SYMBOLS,
   OPTION_ERROR_UNRESOLVED_SYMBOLS,
+  OPTION_WARN_SHARED_TEXTREL,
   OPTION_REDUCE_MEMORY_OVERHEADS
 };
 
@@ -304,6 +305,18 @@ static const struct ld_option ld_options[] =
      OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH},
     '\0', NULL, N_("Reject input files whose architecture is unknown"),
     TWO_DASHES },
+  { {"add-needed", no_argument, NULL, OPTION_ADD_NEEDED},
+    '\0', NULL, N_("Set DT_NEEDED tags for DT_NEEDED entries in\n"
+		   "\t\t\t\tfollowing dynamic libs"), TWO_DASHES },
+  { {"no-add-needed", no_argument, NULL, OPTION_NO_ADD_NEEDED},
+    '\0', NULL, N_("Do not set DT_NEEDED tags for DT_NEEDED entries\n"
+		   "\t\t\t\tin following dynamic libs"), TWO_DASHES },
+  { {"as-needed", no_argument, NULL, OPTION_AS_NEEDED},
+    '\0', NULL, N_("Only set DT_NEEDED for following dynamic libs if used"),
+    TWO_DASHES },
+  { {"no-as-needed", no_argument, NULL, OPTION_NO_AS_NEEDED},
+    '\0', NULL, N_("Always set DT_NEEDED for following dynamic libs"),
+    TWO_DASHES },
   { {"assert", required_argument, NULL, OPTION_ASSERT},
     '\0', N_("KEYWORD"), N_("Ignored for SunOS compatibility"), ONE_DASH },
   { {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
@@ -337,6 +350,9 @@ static const struct ld_option ld_options[] =
     TWO_DASHES },
   { {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
     '\0', NULL, N_("Generate embedded relocs"), TWO_DASHES},
+  { {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL},
+    '\0', NULL, N_("Treat warnings as errors"),
+    TWO_DASHES },
   { {"fini", required_argument, NULL, OPTION_FINI},
     '\0', N_("SYMBOL"), N_("Call SYMBOL at unload-time"), ONE_DASH },
   { {"force-exe-suffix", no_argument, NULL, OPTION_FORCE_EXE_SUFFIX},
@@ -476,30 +492,18 @@ static const struct ld_option ld_options[] =
   { {"warn-section-align", no_argument, NULL, OPTION_WARN_SECTION_ALIGN},
     '\0', NULL, N_("Warn if start of section changes due to alignment"),
     TWO_DASHES },
+  { {"warn-shared-textrel", no_argument, NULL, OPTION_WARN_SHARED_TEXTREL},
+    '\0', NULL, N_("Warn if shared object has DT_TEXTREL"),
+    TWO_DASHES },
   { {"warn-unresolved-symbols", no_argument, NULL,
      OPTION_WARN_UNRESOLVED_SYMBOLS},
     '\0', NULL, N_("Report unresolved symbols as warnings"), TWO_DASHES },
   { {"error-unresolved-symbols", no_argument, NULL,
      OPTION_ERROR_UNRESOLVED_SYMBOLS},
     '\0', NULL, N_("Report unresolved symbols as errors"), TWO_DASHES },
-  { {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL},
-    '\0', NULL, N_("Treat warnings as errors"),
-    TWO_DASHES },
   { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
     '\0', NULL, N_("Include all objects from following archives"),
     TWO_DASHES },
-  { {"add-needed", no_argument, NULL, OPTION_ADD_NEEDED},
-    '\0', NULL, N_("Set DT_NEEDED tags for DT_NEEDED entries in\n"
-		   "\t\t\t\tfollowing dynamic libs"), TWO_DASHES },
-  { {"no-add-needed", no_argument, NULL, OPTION_NO_ADD_NEEDED},
-    '\0', NULL, N_("Do not set DT_NEEDED tags for DT_NEEDED entries\n"
-		   "\t\t\t\tin following dynamic libs"), TWO_DASHES },
-  { {"as-needed", no_argument, NULL, OPTION_AS_NEEDED},
-    '\0', NULL, N_("Only set DT_NEEDED for following dynamic libs if used"),
-    TWO_DASHES },
-  { {"no-as-needed", no_argument, NULL, OPTION_NO_AS_NEEDED},
-    '\0', NULL, N_("Always set DT_NEEDED for following dynamic libs"),
-    TWO_DASHES },
   { {"wrap", required_argument, NULL, OPTION_WRAP},
     '\0', N_("SYMBOL"), N_("Use wrapper functions for SYMBOL"), TWO_DASHES },
 };
@@ -1214,6 +1218,9 @@ parse_args (unsigned argc, char **argv)
 	case OPTION_WARN_SECTION_ALIGN:
 	  config.warn_section_align = TRUE;
 	  break;
+	case OPTION_WARN_SHARED_TEXTREL:
+	  link_info.warn_shared_textrel = TRUE;
+	  break;
 	case OPTION_WHOLE_ARCHIVE:
 	  whole_archive = TRUE;
 	  break;