+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
* 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
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
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
if (command_line.check_section_addresses)
lang_check_section_addresses ();
+ /* Check any required symbols are known. */
+ ldlang_check_require_defined_symbols ();
+
lang_end ();
}
(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
OPTION_PUSH_STATE,
OPTION_POP_STATE,
OPTION_PRINT_MEMORY_USAGE,
+ OPTION_REQUIRE_DEFINED_SYMBOL,
};
/* The initial parser states. */
{ {"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 },
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);
+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
--- /dev/null
+#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
--- /dev/null
+#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
+#...
--- /dev/null
+#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
+#...
--- /dev/null
+#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
+#...
--- /dev/null
+#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
--- /dev/null
+# 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]
+}
--- /dev/null
+ .text
+ .global _start
+_start:
+ .word 0
+
+ .section .text.1, "ax"
+ .global bar
+bar:
+ .word 0