ld: Add '--require-defined' command line option.
authorAndrew Burgess <andrew.burgess@embecosm.com>
Wed, 15 Jul 2015 17:37:30 +0000 (18:37 +0100)
committerAndrew Burgess <andrew.burgess@embecosm.com>
Tue, 4 Aug 2015 09:00:05 +0000 (10:00 +0100)
Add a new command line option '--require-defined' to the linker.  This
option operates identically to the '--undefined' option, except that if
the symbol is not defined in the final output file then the linker will
exit with an error.

When making use of --gc-section, or just when trying to pull in parts of
a library, it is not uncommon for a user to use the '--undefined'
command line option to specify a symbol that the user then expects to be
defined by one of the object files supplied to the link.

However, if for any reason the symbol is not satisfied by an object
provided to the link the user will be left with an undefined symbol in
the output file, instead of a defined symbol.

In some cases the above behaviour is what the user wants, in other cases
though we can do better.  The '--require-defined' option tries to fill
this gap.  The symbol passed to the '--require-defined' option is
treated exactly as if the symbol was passed to '--undefined', however,
before the linker exits a check is made that all symbols passed to
'--require-defined' are actually defined, if any are not then the link
will fail with an error.

ld/ChangeLog:

* ld.texinfo (Options): Document --require-defined option.
* ldlang.c (struct require_defined_symbol): New structure.
(require_defined_symbol_list): New variable.
(ldlang_add_require_defined): New function.
(ldlang_check_require_defined_symbols): New function.
(lang_process): Check required symbols are defined.
* ldlang.h (ldlang_add_require_defined): Declare.
* ldlex.h (enum option_values): Add OPTION_REQUIRE_DEFINED_SYMBOL.
* lexsup.c (ld_options): Add '--require-defined' entry.
(parse_args): Handle '--require-defined' entry.
* NEWS: Mention new '--require-defined' option.

ld/testsuite/ChangeLog:

* ld-undefined/require-defined-1.d: New file.
* ld-undefined/require-defined-2.d: New file.
* ld-undefined/require-defined-3.d: New file.
* ld-undefined/require-defined-4.d: New file.
* ld-undefined/require-defined-5.d: New file.
* ld-undefined/require-defined.exp: New file.
* ld-undefined/require-defined.s: New file.

15 files changed:
ld/ChangeLog
ld/NEWS
ld/ld.texinfo
ld/ldlang.c
ld/ldlang.h
ld/ldlex.h
ld/lexsup.c
ld/testsuite/ChangeLog
ld/testsuite/ld-undefined/require-defined-1.d [new file with mode: 0644]
ld/testsuite/ld-undefined/require-defined-2.d [new file with mode: 0644]
ld/testsuite/ld-undefined/require-defined-3.d [new file with mode: 0644]
ld/testsuite/ld-undefined/require-defined-4.d [new file with mode: 0644]
ld/testsuite/ld-undefined/require-defined-5.d [new file with mode: 0644]
ld/testsuite/ld-undefined/require-defined.exp [new file with mode: 0644]
ld/testsuite/ld-undefined/require-defined.s [new file with mode: 0644]

index e0403295b47f2a3a65c1e3fe8aebe0c6744d577e..23b12c8617543614e8644c7d0aff77bf65014e31 100644 (file)
@@ -1,3 +1,17 @@
+2015-08-04  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * ld.texinfo (Options): Document --require-defined option.
+       * ldlang.c (struct require_defined_symbol): New structure.
+       (require_defined_symbol_list): New variable.
+       (ldlang_add_require_defined): New function.
+       (ldlang_check_require_defined_symbols): New function.
+       (lang_process): Check required symbols are defined.
+       * ldlang.h (ldlang_add_require_defined): Declare.
+       * ldlex.h (enum option_values): Add OPTION_REQUIRE_DEFINED_SYMBOL.
+       * lexsup.c (ld_options): Add '--require-defined' entry.
+       (parse_args): Handle '--requre-defined' entry.
+       * NEWS: Mention new '--require-defined' option.
+
 2015-08-03  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * ld.texinfo (MEMORY): Explain that multiple MEMORY commands are
diff --git a/ld/NEWS b/ld/NEWS
index e3de4328095c53f9463060da535835430ee931d3..f9ef88dac55c04c3502a5fdf052ecda3969aaf3e 100644 (file)
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -12,6 +12,9 @@
 
 * Add --print-memory-usage option to report memory blocks usage.
 
+* Add --require-defined option, it's like --undefined except the new symbol
+  must be defined by the end of the link.
+
 Changes in 2.25:
 
 * PE binaries now once again contain real timestamps by default.  To disable
index 8a7a08f981ed8da848deca29731c9bd0b235df8d..bddf926b3c77cb868bb96841b724d8a15c76d523 100644 (file)
@@ -973,6 +973,22 @@ modules from standard libraries.  @samp{-u} may be repeated with
 different option arguments to enter additional undefined symbols.  This
 option is equivalent to the @code{EXTERN} linker script command.
 
+If this option is being used to force additional modules to be pulled
+into the link, and if it is an error for the symbol to remain
+undefined, then the option @option{--require-defined} should be used
+instead.
+
+@kindex --require-defined=@var{symbol}
+@cindex symbols, require defined
+@cindex defined symbol
+@item --require-defined=@var{symbol}
+Require that @var{symbol} is defined in the output file.  This option
+is the same as option @option{--undefined} except that if @var{symbol}
+is not defined in the output file then the linker will issue an error
+and exit.  The same effect can be achieved in a linker script by using
+@code{EXTERN}, @code{ASSERT} and @code{DEFINED} together.  This option
+can be used multiple times to require additional symbols.
+
 @kindex -Ur
 @cindex constructors
 @item -Ur
index b816d698fc334551095a78a6d19adc595a3ea26f..0d6419d91ddaadc7d5449bd7ddcb77f8fc35d7d3 100644 (file)
@@ -3413,6 +3413,55 @@ lang_place_undefineds (void)
     insert_undefined (ptr->name);
 }
 
+/* Structure used to build the list of symbols that the user has required
+   be defined.  */
+
+struct require_defined_symbol
+{
+  const char *name;
+  struct require_defined_symbol *next;
+};
+
+/* The list of symbols that the user has required be defined.  */
+
+static struct require_defined_symbol *require_defined_symbol_list;
+
+/* Add a new symbol NAME to the list of symbols that are required to be
+   defined.  */
+
+void
+ldlang_add_require_defined (const char * const name)
+{
+  struct require_defined_symbol *ptr;
+
+  ldlang_add_undef (name, TRUE);
+  ptr = (struct require_defined_symbol *) stat_alloc (sizeof (*ptr));
+  ptr->next = require_defined_symbol_list;
+  ptr->name = strdup (name);
+  require_defined_symbol_list = ptr;
+}
+
+/* Check that all symbols the user required to be defined, are defined,
+   raise an error if we find a symbol that is not defined.  */
+
+static void
+ldlang_check_require_defined_symbols (void)
+{
+  struct require_defined_symbol *ptr;
+
+  for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next)
+    {
+      struct bfd_link_hash_entry *h;
+
+      h = bfd_link_hash_lookup (link_info.hash, ptr->name,
+                                FALSE, FALSE, TRUE);
+      if (h == NULL
+          || (h->type != bfd_link_hash_defined
+              && h->type != bfd_link_hash_defweak))
+        einfo(_("%P%X: required symbol `%s' not defined\n"), ptr->name);
+    }
+}
+
 /* Check for all readonly or some readwrite sections.  */
 
 static void
@@ -6805,6 +6854,9 @@ lang_process (void)
   if (command_line.check_section_addresses)
     lang_check_section_addresses ();
 
+  /* Check any required symbols are known.  */
+  ldlang_check_require_defined_symbols ();
+
   lang_end ();
 }
 
index 475cf71c21d827b1601adda59d5178fd974a9112..4b7e65df541fff6aab09eff63f2856c4a91410bf 100644 (file)
@@ -609,6 +609,8 @@ extern lang_output_section_statement_type *next_matching_output_section_statemen
   (lang_output_section_statement_type *, int);
 extern void ldlang_add_undef
   (const char *const, bfd_boolean);
+extern void ldlang_add_require_defined
+  (const char *const);
 extern void lang_add_output_format
   (const char *, const char *, const char *, int);
 extern void lang_list_init
index 7885de0282339c300420e2935c3b7e0ea413473b..59bd14f653595812a08e2f403b4359096caa9ed8 100644 (file)
@@ -143,6 +143,7 @@ enum option_values
   OPTION_PUSH_STATE,
   OPTION_POP_STATE,
   OPTION_PRINT_MEMORY_USAGE,
+  OPTION_REQUIRE_DEFINED_SYMBOL,
 };
 
 /* The initial parser states.  */
index 0aa67f532b97b5bf0b2650167191ab6e2c8962c2..28541341f3c1214ff6b1354f73767ba2c99d0fcb 100644 (file)
@@ -212,6 +212,9 @@ static const struct ld_option ld_options[] =
   { {"undefined", required_argument, NULL, 'u'},
     'u', N_("SYMBOL"), N_("Start with undefined reference to SYMBOL"),
     TWO_DASHES },
+  { {"require-defined", required_argument, NULL, OPTION_REQUIRE_DEFINED_SYMBOL},
+    '\0', N_("SYMBOL"), N_("Require SYMBOL be defined in the final output"),
+    TWO_DASHES },
   { {"unique", optional_argument, NULL, OPTION_UNIQUE},
     '\0', N_("[=SECTION]"),
     N_("Don't merge input [SECTION | orphan] sections"), TWO_DASHES },
@@ -1247,6 +1250,9 @@ parse_args (unsigned argc, char **argv)
        case 'u':
          ldlang_add_undef (optarg, TRUE);
          break;
+        case OPTION_REQUIRE_DEFINED_SYMBOL:
+          ldlang_add_require_defined (optarg);
+         break;
        case OPTION_UNIQUE:
          if (optarg != NULL)
            lang_add_unique (optarg);
index c35d4a79bb74f71ba432f2ea400c959f439a5622..512abc317765d70dccceace0b90a769f69ebba2f 100644 (file)
@@ -1,3 +1,13 @@
+2015-08-04  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * ld-undefined/require-defined-1.d: New file.
+       * ld-undefined/require-defined-2.d: New file.
+       * ld-undefined/require-defined-3.d: New file.
+       * ld-undefined/require-defined-4.d: New file.
+       * ld-undefined/require-defined-5.d: New file.
+       * ld-undefined/require-defined.exp: New file.
+       * ld-undefined/require-defined.s: New file.
+
 2015-07-30  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/18735
diff --git a/ld/testsuite/ld-undefined/require-defined-1.d b/ld/testsuite/ld-undefined/require-defined-1.d
new file mode 100644 (file)
index 0000000..12b0cf5
--- /dev/null
@@ -0,0 +1,4 @@
+#name: Check require-defined with an undefined symbol
+#source: require-defined.s
+#ld: -e _start --gc-sections --require-defined=xxx
+#error: required symbol `xxx' not defined
diff --git a/ld/testsuite/ld-undefined/require-defined-2.d b/ld/testsuite/ld-undefined/require-defined-2.d
new file mode 100644 (file)
index 0000000..e770eea
--- /dev/null
@@ -0,0 +1,8 @@
+#name: Check require-defined can require a symbol from an object
+#source: require-defined.s
+#ld: -e _start --gc-sections --require-defined=bar
+#nm: -n
+
+#...
+[0-9a-f]+ T bar
+#...
diff --git a/ld/testsuite/ld-undefined/require-defined-3.d b/ld/testsuite/ld-undefined/require-defined-3.d
new file mode 100644 (file)
index 0000000..c61ed34
--- /dev/null
@@ -0,0 +1,8 @@
+#name: Check require-defined does no error on a defined symbol
+#source: require-defined.s
+#ld: -e _start --require-defined=bar
+#nm: -n
+
+#...
+[0-9a-f]+ T bar
+#...
diff --git a/ld/testsuite/ld-undefined/require-defined-4.d b/ld/testsuite/ld-undefined/require-defined-4.d
new file mode 100644 (file)
index 0000000..2922c99
--- /dev/null
@@ -0,0 +1,8 @@
+#name: Check require-defined can require a symbol from an archive
+#source: require-defined.s
+#ld: -e _start --require-defined=foo tmpdir/libfoo.a
+#nm: -n
+
+#...
+[0-9a-f]+ T foo
+#...
diff --git a/ld/testsuite/ld-undefined/require-defined-5.d b/ld/testsuite/ld-undefined/require-defined-5.d
new file mode 100644 (file)
index 0000000..ea288a9
--- /dev/null
@@ -0,0 +1,10 @@
+#name: Check require-defined can require two symbols
+#source: require-defined.s
+#ld: -e _start --gc-sections --require-defined=bar --require-defined=foo tmpdir/libfoo.a
+#nm: -n
+
+#...
+[0-9a-f]+ T foo
+#...
+[0-9a-f]+ T bar
+#...
\ No newline at end of file
diff --git a/ld/testsuite/ld-undefined/require-defined.exp b/ld/testsuite/ld-undefined/require-defined.exp
new file mode 100644 (file)
index 0000000..16013dc
--- /dev/null
@@ -0,0 +1,58 @@
+# Expect script for ld --undefined and --require-defined testing.
+#   Copyright (C) 2015 Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# 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 3 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.
+#
+# 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., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+#
+
+# Some of these tests use --gc-sections.  For now we don't attempt any
+# if --gc-sections is unavailable, as most major targets do support
+# this option.
+if ![check_gc_sections_available] {
+    unsupported "require-defined testing"
+    return
+}
+
+# The following targets all fail these tests for reasons that appear
+# to be unrelated to the test themselves (for example some of these
+# targets require more complex linker scripts).  Given the small
+# number of targets, I'm just skipping the tests in these cases.]
+if { [istarget "powerpcle-*-*"] \
+     || [istarget "cris-axis-elf"] \
+     || [istarget "ns32k-*-netbsd"] \
+     || [istarget "*-*-coff"] \
+     || [istarget "*-*-xcoff"] \
+     || [istarget "*-*-*aout*"] \
+     || [istarget "*-*-*aix*"] } {
+    unsupported "require-defined testing"
+    return
+}
+
+set build_tests {
+  {"Build libfoo.a"
+   "" "" ""
+   {entry.s} {} "libfoo.a"}
+}
+
+run_ld_link_tests $build_tests
+
+set test_list [lsort [glob -nocomplain $srcdir/$subdir/require-defined*.d]]
+foreach t $test_list {
+    # We need to strip the ".d", but can leave the dirname.
+    verbose [file rootname $t]
+    run_dump_test [file rootname $t]
+}
diff --git a/ld/testsuite/ld-undefined/require-defined.s b/ld/testsuite/ld-undefined/require-defined.s
new file mode 100644 (file)
index 0000000..2bda640
--- /dev/null
@@ -0,0 +1,9 @@
+        .text
+        .global _start
+_start:
+        .word 0
+
+        .section .text.1, "ax"
+        .global bar
+bar:
+        .word 0