This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Commit: Add --warn-orphan option to the linker


Hi Guys,

  I am applying the attached patch to add a small new feature to the
  linker: a command line option --warn-orphan that generates a warning
  message whenever a orphan section is placed into the output file.
  Whilst this information could be discovered by parsing a map generated
  by the --map option, this new option makes it simpler and easier to
  discover orphan sections, which should help when writing linker
  scripts.

Cheers
  Nick

ld/ChangeLog
2015-04-07  Nick Clifton  <nickc@redhat.com>

	* ld.h (struct ld_config_type): Add new field: warn_orphan.
	* ldlex.h (enum option_values): Add OPTION_WARN_ORPHAN and
	OPTION_NO_WARN_ORPHAN.
	* lexsup.c (ld_options): Add --warn-orphan and --no-warn-orphan.
	(parse_args): Handle the new options.
	* ldemul.c (ldemul_place_orphan): If requested, generate a warning
	message when an orphan section is placed in the output file.
	* ld.texinfo: Document the new option.
	* NEWS: Mention the new feature.

ld/testsuite/ChangeLog
2015-04-07  Nick Clifton  <nickc@redhat.com>

	* ld-elf/orphan-5.l: New test - checks the linker's output with
	--warn-orphan enabled.
	* ld-elf/elf.exp: Run the new test.

diff --git a/ld/NEWS b/ld/NEWS
index 3a592be..9cafe12 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,7 @@
 -*- text -*-
 
+* Add --warn-orphan option to report orphan sections.
+
 * Add support for LLVM plugin.
 
 Changes in 2.25:
diff --git a/ld/ld.h b/ld/ld.h
index e0ca3e8..f804f9c 100644
--- a/ld/ld.h
+++ b/ld/ld.h
@@ -226,6 +226,9 @@ typedef struct {
   /* If TRUE, only warn once about a particular undefined symbol.  */
   bfd_boolean warn_once;
 
+  /* If TRUE, issue warning messages when orphan sections are encountered.  */
+  bfd_boolean warn_orphan;
+
   /* If TRUE, warn if multiple global-pointers are needed (Alpha
      only).  */
   bfd_boolean warn_multiple_gp;
diff --git a/ld/ld.texinfo b/ld/ld.texinfo
index 440fa41..82735e9 100644
--- a/ld/ld.texinfo
+++ b/ld/ld.texinfo
@@ -2064,6 +2064,17 @@ option causes a warning to be issued whenever this case occurs.
 Only warn once for each undefined symbol, rather than once per module
 which refers to it.
 
+@kindex --warn-orphan
+@kindex --no-warn-orphan
+@cindex warnings, on orphan sections
+@cindex orphan sections, warnings on
+@item --warn-orphan
+The @option{--warn-orphan} option tells the linker to generate a
+warning message whenever it has to place an orphan section into the
+output file.  @xref{Orphan Sections}  The @option{--no-warn-orphan}
+option restores the default behaviour of just silently placing these
+sections.
+
 @kindex --warn-section-align
 @cindex warnings, on section alignment
 @cindex section alignment, warnings on
diff --git a/ld/ldemul.c b/ld/ldemul.c
index 8b2cae7..4898892 100644
--- a/ld/ldemul.c
+++ b/ld/ldemul.c
@@ -120,6 +120,10 @@ ldemul_open_dynamic_archive (const char *arch, search_dirs_type *search,
 lang_output_section_statement_type *
 ldemul_place_orphan (asection *s, const char *name, int constraint)
 {
+  if (config.warn_orphan)
+    einfo (_("%P: Warning: input section '%s' from file '%B' is not mentioned in linker script\n"),
+	   name, s->owner);
+
   if (ld_emulation->place_orphan)
     return (*ld_emulation->place_orphan) (s, name, constraint);
   return NULL;
diff --git a/ld/ldlex.h b/ld/ldlex.h
index be7f653..f174c28 100644
--- a/ld/ldlex.h
+++ b/ld/ldlex.h
@@ -85,6 +85,8 @@ enum option_values
   OPTION_NO_WARN_FATAL,
   OPTION_WARN_MULTIPLE_GP,
   OPTION_WARN_ONCE,
+  OPTION_WARN_ORPHAN,
+  OPTION_NO_WARN_ORPHAN,
   OPTION_WARN_SECTION_ALIGN,
   OPTION_SPLIT_BY_RELOC,
   OPTION_SPLIT_BY_FILE ,
diff --git a/ld/lexsup.c b/ld/lexsup.c
index aa6c3cd..a30fafa 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -489,6 +489,10 @@ static const struct ld_option ld_options[] =
     '\0', NULL, N_("Warn if the multiple GP values are used"), TWO_DASHES },
   { {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
     '\0', NULL, N_("Warn only once per undefined symbol"), TWO_DASHES },
+  { {"warn-orphan", no_argument, NULL, OPTION_WARN_ORPHAN},
+    '\0', NULL, N_("Warn if any orphan sections are encountered"), TWO_DASHES },
+  { {"no-warn-orphan", no_argument, NULL, OPTION_NO_WARN_ORPHAN},
+    '\0', NULL, N_("Do not warn if orphan sections are encountered (default)"), TWO_DASHES },
   { {"warn-section-align", no_argument, NULL, OPTION_WARN_SECTION_ALIGN},
     '\0', NULL, N_("Warn if start of section changes due to alignment"),
     TWO_DASHES },
@@ -1350,6 +1354,12 @@ parse_args (unsigned argc, char **argv)
 	case OPTION_WARN_ONCE:
 	  config.warn_once = TRUE;
 	  break;
+	case OPTION_WARN_ORPHAN:
+	  config.warn_orphan = TRUE;
+	  break;
+	case OPTION_NO_WARN_ORPHAN:
+	  config.warn_orphan = FALSE;
+	  break;
 	case OPTION_WARN_SECTION_ALIGN:
 	  config.warn_section_align = TRUE;
 	  break;
diff --git a/ld/testsuite/ld-elf/elf.exp b/ld/testsuite/ld-elf/elf.exp
index f126650..d1a70ea 100644
--- a/ld/testsuite/ld-elf/elf.exp
+++ b/ld/testsuite/ld-elf/elf.exp
@@ -116,6 +116,18 @@ foreach t $test_list {
     run_dump_test [file rootname $t]
 }
 
+# Check that the --warn-orphan option works correctly.
+run_ld_link_tests {
+    {"Report orphan sections"
+	"--script orphan.ld --warn-orphan"
+	""
+	""
+	{orphan.s}
+	{ { ld "orphan-5.l" } }
+	"orphan"
+    }
+}
+
 if { [istarget *-*-linux*]
      || [istarget *-*-nacl*]
      || [istarget *-*-gnu*] } {
--- /dev/null	2015-04-07 15:45:11.503790286 +0100
+++ ld/testsuite/ld-elf/orphan-5.l	2015-04-07 15:10:20.640700637 +0100
@@ -0,0 +1,5 @@
+#...
+.*Warning: input section '.notbad' from file 'tmpdir/orphan.o' is not mentioned in linker script
+#...
+.*Warning: input section '.note.bar' from file 'tmpdir/orphan.o' is not mentioned in linker script
+#...

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]