ld: Parse linker script only once

Message ID CAMe9rOq2yMm=3jBTW7EKkZGfKXbLQMuDNm_9Rc3w0O5O+M6tUA@mail.gmail.com
State New
Headers
Series ld: Parse linker script only once |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

H.J. Lu Jan. 14, 2025, 9:53 p.m. UTC
  Parsing linker script twice caused

FAIL: ld-plugin/lto-3r
FAIL: ld-plugin/lto-5r
FAIL: PR ld/19317 (2)

for x86_64-w64-mingw32 with the linker error:

./ld-new:built in linker script:27 assignment to location counter invalid outsid
e of SECTIONS

ldscripts/i386pep.xr has

 24   .rdata  :
 25   {
 26     *(.rdata)
 27     . = ALIGN(4);
 28     /* .ctors & .dtors */
 29     /* .CRT */
 30     /* ___crt_xl_end__ is defined in the TLS Directory support code */
 31   }

Remove ld_parse_linker_script to parse linker script only once.

* ldlang.c (cmdline_emit_object_only_section): Don't call
ld_parse_linker_script.
* ldmain.c (main): Fold ld_parse_linker_script.
(ld_parse_linker_script): Removed.
  

Patch

From e76d66a53ee9ef0a70fdb44a3150518fcc8086ea Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Tue, 14 Jan 2025 21:21:02 +0800
Subject: [PATCH] ld: Parse linker script only once

Parsing linker script twice caused

FAIL: ld-plugin/lto-3r
FAIL: ld-plugin/lto-5r
FAIL: PR ld/19317 (2)

for x86_64-w64-mingw32 with the linker error:

./ld-new:built in linker script:27 assignment to location counter invalid outside of SECTIONS

ldscripts/i386pep.xr has

 24   .rdata  :
 25   {
 26     *(.rdata)
 27     . = ALIGN(4);
 28     /* .ctors & .dtors */
 29     /* .CRT */
 30     /* ___crt_xl_end__ is defined in the TLS Directory support code */
 31   }

Remove ld_parse_linker_script to parse linker script only once.

	* ldlang.c (cmdline_emit_object_only_section): Don't call
	ld_parse_linker_script.
	* ldmain.c (main): Fold ld_parse_linker_script.
	(ld_parse_linker_script): Removed.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 ld/ldlang.c |  2 --
 ld/ldmain.c | 64 +++++++++++++++++++++++------------------------------
 ld/ldmain.h |  2 --
 3 files changed, 28 insertions(+), 40 deletions(-)

diff --git a/ld/ldlang.c b/ld/ldlang.c
index e1e491732a9..634a6843416 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -10823,8 +10823,6 @@  cmdline_emit_object_only_section (void)
   lang_init (true);
   ldexp_init (true);
 
-  ld_parse_linker_script ();
-
   /* Set up the object-only output. */
   lang_final ();
 
diff --git a/ld/ldmain.c b/ld/ldmain.c
index ffe1d70d15e..f1c5f7035c5 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -410,7 +410,34 @@  main (int argc, char **argv)
 
   ldemul_set_symbols ();
 
-  ld_parse_linker_script ();
+  /* If we have not already opened and parsed a linker script,
+     try the default script from command line first.  */
+  if (saved_script_handle == NULL
+      && command_line.default_script != NULL)
+    {
+      ldfile_open_script_file (command_line.default_script);
+      parser_input = input_script;
+      yyparse ();
+    }
+
+  /* If we have not already opened and parsed a linker script
+     read the emulation's appropriate default script.  */
+  if (saved_script_handle == NULL)
+    {
+      int isfile;
+      char *s = ldemul_get_script (&isfile);
+
+      if (isfile)
+	ldfile_open_default_command_file (s);
+      else
+	{
+	  lex_string = s;
+	  lex_redirect (s, _("built in linker script"), 1);
+	}
+      parser_input = input_script;
+      yyparse ();
+      lex_string = NULL;
+    }
 
   if (verbose)
     {
@@ -1677,38 +1704,3 @@  notice (struct bfd_link_info *info,
 
   return true;
 }
-
-/* Parse the linker script.   */
-
-void
-ld_parse_linker_script (void)
-{
-  /* If we have not already opened and parsed a linker script,
-     try the default script from command line first.  */
-  if (saved_script_handle == NULL
-      && command_line.default_script != NULL)
-    {
-      ldfile_open_script_file (command_line.default_script);
-      parser_input = input_script;
-      yyparse ();
-    }
-
-  /* If we have not already opened and parsed a linker script
-     read the emulation's appropriate default script.  */
-  if (saved_script_handle == NULL)
-    {
-      int isfile;
-      char *s = ldemul_get_script (&isfile);
-
-      if (isfile)
-	ldfile_open_default_command_file (s);
-      else
-	{
-	  lex_string = s;
-	  lex_redirect (s, _("built in linker script"), 1);
-	}
-      parser_input = input_script;
-      yyparse ();
-      lex_string = NULL;
-    }
-}
diff --git a/ld/ldmain.h b/ld/ldmain.h
index 34770762f1c..0baa3c305c3 100644
--- a/ld/ldmain.h
+++ b/ld/ldmain.h
@@ -63,6 +63,4 @@  extern void add_ignoresym (struct bfd_link_info *, const char *);
 extern void add_keepsyms_file (const char *);
 extern void track_dependency_files (const char *);
 
-extern void ld_parse_linker_script (void);
-
 #endif
-- 
2.47.1