public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/4] ld: Add --text-section-ordering-file FILE
@ 2024-01-19 19:45 H.J. Lu
  2024-01-19 19:45 ` [PATCH 1/4] " H.J. Lu
                   ` (3 more replies)
  0 siblings, 4 replies; 19+ messages in thread
From: H.J. Lu @ 2024-01-19 19:45 UTC (permalink / raw)
  To: binutils; +Cc: goldstein.w.n

Add --text-section-ordering-file FILE to specify the text section
ordering file.  Inside the text section ordering file, when seeing an
input file name without section list and the file name starts with '.',
treat the filename as a section name and create a wild card as the file
name.

A text section ordering file which contains text section wildcards:

---
text_section_foo*
text_section_b?r
text_section_name
---

can be used to group input text sections together to:

1. Reduce gaps between text sections.
2. Put hot text sections close to each other.

Text sections in the text section ordering file are placed at the
beginning of the output text section and code text sections are moved
toward the end so that the first section in the text section ordering
file is aligned to the output section alignment.

--text-section-ordering-file must be placed before -T/--script option
so that the text section ordering file can always be included in linker
scripts.  Nested INCLUDE in the text section order file is disallowed.

--text-section-ordering-file FILE is supported only in ELF and PE COFF
linkers which support input .text.* sections.

NB: Gold has the command-line option, --section-ordering-file FILE, to
layout sections in the order specified.  --text-section-ordering-file
supports the same section ordering file format, but it applies only to
text sections.

H.J. Lu (4):
  ld: Add --text-section-ordering-file FILE
  ld: Include the text section order file in all ELF linker scripts
  ld: Include the text section order file in PE COFF linker scripts
  ld: Document --text-section-ordering-file FILE

 ld/NEWS                                 |  3 +
 ld/ld.h                                 |  3 +
 ld/ld.texi                              | 11 ++++
 ld/ldfile.c                             | 74 +++++++++++++++++++++++--
 ld/ldlang.c                             | 39 +++++++++++--
 ld/ldlex.h                              |  3 +
 ld/ldlex.l                              |  7 +++
 ld/lexsup.c                             | 11 ++++
 ld/scripttempl/arclinux.sc              |  5 +-
 ld/scripttempl/avr.sc                   |  1 +
 ld/scripttempl/dlx.sc                   |  1 +
 ld/scripttempl/elf.sc                   |  5 +-
 ld/scripttempl/elf32cr16.sc             |  1 +
 ld/scripttempl/elf32crx.sc              |  1 +
 ld/scripttempl/elf32msp430.sc           |  1 +
 ld/scripttempl/elf64bpf.sc              |  5 +-
 ld/scripttempl/elf64hppa.sc             |  1 +
 ld/scripttempl/elfarc.sc                |  1 +
 ld/scripttempl/elfarcv2.sc              |  1 +
 ld/scripttempl/elfd10v.sc               |  1 +
 ld/scripttempl/elfd30v.sc               |  1 +
 ld/scripttempl/elfm68hc11.sc            |  1 +
 ld/scripttempl/elfm68hc12.sc            |  1 +
 ld/scripttempl/elfm9s12z.sc             |  1 +
 ld/scripttempl/elfmicroblaze.sc         |  1 +
 ld/scripttempl/elfxgate.sc              |  1 +
 ld/scripttempl/elfxtensa.sc             |  1 +
 ld/scripttempl/ft32.sc                  |  1 +
 ld/scripttempl/i386beos.sc              |  1 +
 ld/scripttempl/i386go32.sc              |  1 +
 ld/scripttempl/iq2000.sc                |  1 +
 ld/scripttempl/mcorepe.sc               |  1 +
 ld/scripttempl/mep.sc                   |  1 +
 ld/scripttempl/mmo.sc                   |  1 +
 ld/scripttempl/nds32elf.sc              |  5 +-
 ld/scripttempl/pe.sc                    |  1 +
 ld/scripttempl/pep.sc                   |  1 +
 ld/scripttempl/pru.sc                   |  1 +
 ld/scripttempl/v850.sc                  |  1 +
 ld/scripttempl/v850_rh850.sc            |  1 +
 ld/scripttempl/visium.sc                |  1 +
 ld/scripttempl/xstormy16.sc             |  1 +
 ld/testsuite/ld-scripts/start.s         | 14 +++++
 ld/testsuite/ld-scripts/text-order-1a.d | 17 ++++++
 ld/testsuite/ld-scripts/text-order-1a.s |  8 +++
 ld/testsuite/ld-scripts/text-order-1a.t |  5 ++
 ld/testsuite/ld-scripts/text-order-1b.d | 17 ++++++
 ld/testsuite/ld-scripts/text-order-1b.s |  8 +++
 ld/testsuite/ld-scripts/text-order-1b.t |  4 ++
 ld/testsuite/ld-scripts/text-order-1c.d |  6 ++
 ld/testsuite/ld-scripts/text-order-1c.t |  5 ++
 ld/testsuite/ld-scripts/text-order.exp  | 43 ++++++++++++++
 52 files changed, 311 insertions(+), 17 deletions(-)
 create mode 100644 ld/testsuite/ld-scripts/start.s
 create mode 100644 ld/testsuite/ld-scripts/text-order-1a.d
 create mode 100644 ld/testsuite/ld-scripts/text-order-1a.s
 create mode 100644 ld/testsuite/ld-scripts/text-order-1a.t
 create mode 100644 ld/testsuite/ld-scripts/text-order-1b.d
 create mode 100644 ld/testsuite/ld-scripts/text-order-1b.s
 create mode 100644 ld/testsuite/ld-scripts/text-order-1b.t
 create mode 100644 ld/testsuite/ld-scripts/text-order-1c.d
 create mode 100644 ld/testsuite/ld-scripts/text-order-1c.t
 create mode 100644 ld/testsuite/ld-scripts/text-order.exp

-- 
2.43.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 1/4] ld: Add --text-section-ordering-file FILE
  2024-01-19 19:45 [PATCH 0/4] ld: Add --text-section-ordering-file FILE H.J. Lu
@ 2024-01-19 19:45 ` H.J. Lu
  2024-01-19 21:50   ` Fangrui Song
       [not found]   ` <DS7PR12MB5765A2B98C7E1217F8C374B9CB702@DS7PR12MB5765.namprd12.prod.outlook.com>
  2024-01-19 19:45 ` [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts H.J. Lu
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 19+ messages in thread
From: H.J. Lu @ 2024-01-19 19:45 UTC (permalink / raw)
  To: binutils; +Cc: goldstein.w.n

Add --text-section-ordering-file FILE to specify the text section
ordering file.  Inside the text section ordering file, when seeing an
input file name without section list and the file name starts with '.',
treat the filename as a section name and create a wild card as the file
name.

A text section ordering file which contains text section wildcards:

---
text_section_foo*
text_section_b?r
text_section_name
---

can be used to group input text sections together to:

1. Reduce gaps between text sections.
2. Put hot text sections close to each other.

Text sections in the text section ordering file are placed at the
beginning of the output text section and code text sections are moved
toward the end so that the first section in the text section ordering
file is aligned to the output section alignment.

--text-section-ordering-file must be placed before -T/--script option
so that the text section ordering file can always be included in linker
scripts.  Nested INCLUDE in the text section order file is disallowed.

NB: Gold has the command-line option, --section-ordering-file FILE, to
layout sections in the order specified.  --text-section-ordering-file
supports the same section ordering file format, but it applies only to
text sections.

	* ld.h (ld_config_type): Add text_section_ordering_file.
	* ldfile.c (in_text_section_ordering_file): New.
	(try_open): Add an argument for the original file name.  Dump
	the text section order file for verbose output.
	(ldfile_find_command_file): Pass the original file name to
	try_open.
	(ldfile_open_command_file_1): Don't allow nested INCLUDE in the
	text section order file.  If the text section order file is
	specified, set in_text_section_ordering_file, load the file and
	don't set saved_script_handle.  Clear seen_eof_include_file.
	* ldlang.c (lang_add_wild): When seeing an input file name
	without section list and the file name starts with '.', treat
	the filename as a section name and create a wild card as the
	file name.
	* ldlex.h (option_values): Add OPTION_TEXT_SECTION_ORDERING_FILE.
	(in_text_section_ordering_file): New.
	(seen_eof_include_file): Likewise.
	* ldlex.l (in_text_section_ordering_file): New.
	(seen_eof_include_file): Likewise.
	(<<EOF>>): Set seen_eof_include_file.
	* lexsup.c (ld_options): Document --text-section-ordering-file.
	(parse_args): Handle OPTION_TEXT_SECTION_ORDERING_FILE.  Issue
	an error if --text-section-ordering-file is placed after
	-T/--script.
	* scripttempl/elf.sc: Include the text section order file and
	place code text sections toward the end.
	* testsuite/ld-scripts/start.s: New file.
	* testsuite/ld-scripts/text-order-1a.d: New file.
	* testsuite/ld-scripts/text-order-1a.s: Likewise.
	* testsuite/ld-scripts/text-order-1a.t: Likewise.
	* testsuite/ld-scripts/text-order-1b.d: Likewise.
	* testsuite/ld-scripts/text-order-1b.s: Likewise.
	* testsuite/ld-scripts/text-order-1b.t: Likewise.
	* testsuite/ld-scripts/text-order-1c.d: Likewise.
	* testsuite/ld-scripts/text-order-1d.t: Likewise.
	* testsuite/ld-scripts/text-order.exp: Likewise.
---
 ld/ld.h                                 |  3 +
 ld/ldfile.c                             | 74 +++++++++++++++++++++++--
 ld/ldlang.c                             | 39 +++++++++++--
 ld/ldlex.h                              |  3 +
 ld/ldlex.l                              |  7 +++
 ld/lexsup.c                             | 11 ++++
 ld/scripttempl/elf.sc                   |  5 +-
 ld/testsuite/ld-scripts/start.s         | 12 ++++
 ld/testsuite/ld-scripts/text-order-1a.d | 20 +++++++
 ld/testsuite/ld-scripts/text-order-1a.s |  8 +++
 ld/testsuite/ld-scripts/text-order-1a.t |  5 ++
 ld/testsuite/ld-scripts/text-order-1b.d | 20 +++++++
 ld/testsuite/ld-scripts/text-order-1b.s |  8 +++
 ld/testsuite/ld-scripts/text-order-1b.t |  4 ++
 ld/testsuite/ld-scripts/text-order-1c.d |  9 +++
 ld/testsuite/ld-scripts/text-order-1c.t |  5 ++
 ld/testsuite/ld-scripts/text-order.exp  | 42 ++++++++++++++
 17 files changed, 264 insertions(+), 11 deletions(-)
 create mode 100644 ld/testsuite/ld-scripts/start.s
 create mode 100644 ld/testsuite/ld-scripts/text-order-1a.d
 create mode 100644 ld/testsuite/ld-scripts/text-order-1a.s
 create mode 100644 ld/testsuite/ld-scripts/text-order-1a.t
 create mode 100644 ld/testsuite/ld-scripts/text-order-1b.d
 create mode 100644 ld/testsuite/ld-scripts/text-order-1b.s
 create mode 100644 ld/testsuite/ld-scripts/text-order-1b.t
 create mode 100644 ld/testsuite/ld-scripts/text-order-1c.d
 create mode 100644 ld/testsuite/ld-scripts/text-order-1c.t
 create mode 100644 ld/testsuite/ld-scripts/text-order.exp

diff --git a/ld/ld.h b/ld/ld.h
index 54d9079678c..a80255a73ba 100644
--- a/ld/ld.h
+++ b/ld/ld.h
@@ -316,6 +316,9 @@ typedef struct
 
   /* Compress DWARF debug sections.  */
   enum compressed_debug_section_type compress_debug;
+
+  /* The optional text section ordering file.  */
+  const char *text_section_ordering_file;
 } ld_config_type;
 
 extern ld_config_type config;
diff --git a/ld/ldfile.c b/ld/ldfile.c
index dc9875d8813..6e9fbb7730f 100644
--- a/ld/ldfile.c
+++ b/ld/ldfile.c
@@ -736,7 +736,7 @@ ldfile_open_file (lang_input_statement_type *entry)
 /* Try to open NAME.  */
 
 static FILE *
-try_open (const char *name, bool *sysrooted)
+try_open (const char *name, const char *orig_name, bool *sysrooted)
 {
   FILE *result;
 
@@ -750,7 +750,34 @@ try_open (const char *name, bool *sysrooted)
 
   if (verbose)
     {
-      if (result == NULL)
+      if (config.text_section_ordering_file != NULL
+	  && strcmp (orig_name, config.text_section_ordering_file) == 0)
+	{
+	  if (result == NULL)
+	    info_msg (_("cannot find text section ordering file: %s\n"),
+		      name);
+	  else
+	    {
+	      static const int ld_bufsz = 8193;
+	      size_t n;
+	      char *buf = (char *) xmalloc (ld_bufsz);
+
+	      info_msg (_("opened text section ordering file: %s\n"),
+			name);
+	      info_msg ("==================================================\n");
+
+	      while ((n = fread (buf, 1, ld_bufsz - 1, result)) > 0)
+		{
+		  buf[n] = 0;
+		  info_msg ("%s", buf);
+		}
+	      rewind (result);
+	      free (buf);
+
+	      info_msg ("==================================================\n\n");
+	    }
+	}
+      else if (result == NULL)
 	info_msg (_("cannot find script file %s\n"), name);
       else
 	info_msg (_("opened script file %s\n"), name);
@@ -832,7 +859,7 @@ ldfile_find_command_file (const char *name,
   if (!default_only)
     {
       /* First try raw name.  */
-      result = try_open (name, sysrooted);
+      result = try_open (name, name, sysrooted);
       if (result != NULL)
 	return result;
     }
@@ -859,7 +886,7 @@ ldfile_find_command_file (const char *name,
        search = search->next)
     {
       path = concat (search->name, slash, name, (const char *) NULL);
-      result = try_open (path, sysrooted);
+      result = try_open (path, name, sysrooted);
       free (path);
       if (result)
 	break;
@@ -908,6 +935,38 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
 	}
     }
 
+  /* Don't allow nested INCLUDE in the text section ordering file.  */
+  if (in_text_section_ordering_file)
+    {
+      einfo (_("%F%P: error: nested include '%s' in the text section "
+	       "ordering file: '%s'\n"), name,
+	       config.text_section_ordering_file);
+      return;
+    }
+
+  if (strcmp (name, "config.text_section_ordering_file") == 0)
+    {
+      /* Support
+
+	 INCLUDE config.text_section_ordering_file;
+
+	 in input text sections in linker script.  */
+      if (config.text_section_ordering_file == NULL)
+	{
+	  /* Skip if the text section ordering file isn't specified.  */
+	  lex_push_file (NULL, name, false);
+	  return;
+	}
+
+      /* Load the text section ordering file.  */
+      name = config.text_section_ordering_file;
+
+      /* Set the in the text section ordering file marker.  */
+      in_text_section_ordering_file = 1;
+    }
+  else
+    in_text_section_ordering_file = 0;
+
   /* FIXME: This memory is never freed, but that should not really matter.
      It will be released when the linker exits, and it is unlikely to ever
      be more than a few tens of bytes.  */
@@ -932,7 +991,12 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
 
   lineno = 1;
 
-  saved_script_handle = ldlex_input_stack;
+  /* Clear the end of the include file marker.  */
+  seen_eof_include_file = 0;
+
+  /* The text section ordering file isn't a real linker script file.  */
+  if (!in_text_section_ordering_file)
+    saved_script_handle = ldlex_input_stack;
 }
 
 /* Open command file NAME in the current directory, -L directories,
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 229401c8342..d2b4d43d8ae 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -8512,12 +8512,43 @@ lang_add_wild (struct wildcard_spec *filespec,
 
   if (filespec != NULL && filespec->name != NULL)
     {
-      if (strcmp (filespec->name, "*") == 0)
-	filespec->name = NULL;
-      else if (!wildcardp (filespec->name))
-	lang_has_input_file = true;
+      if (in_text_section_ordering_file != 0
+	  && section_list == NULL
+	  && filespec->name[0] == '.'
+	  && filespec->sorted == none
+	  && filespec->exclude_name_list == NULL
+	  && filespec->section_flag_list == NULL
+	  && !filespec->reversed)
+	{
+	  /* When seeing an input file name without section list in the
+	     text section ordering file and the file name starts with
+	     '.', treat the filename as a section name and create a
+	     wild card as the file name.  */
+	  struct wildcard_list *single_section
+	    = (struct wildcard_list *) xmalloc (sizeof (*single_section));
+	  memset (single_section, 0, sizeof (*single_section));
+	  single_section->spec.name = filespec->name;
+	  single_section->spec.sorted = none;
+	  /* A NULL indicates the wild card file name, "*".  */
+	  filespec->name = NULL;
+	  section_list = single_section;
+	}
+      else
+	{
+	  if (strcmp (filespec->name, "*") == 0)
+	    filespec->name = NULL;
+	  else if (!wildcardp (filespec->name))
+	    lang_has_input_file = true;
+	}
     }
 
+  /* NB: Clear the in the text section ordering file marker after
+     processing the last entry when the end of the text section
+     ordering file is reached.  */
+  if (in_text_section_ordering_file != 0
+      && seen_eof_include_file != 0)
+    in_text_section_ordering_file = 0;
+
   new_stmt = new_stat (lang_wild_statement, stat_ptr);
   new_stmt->filename = NULL;
   new_stmt->filenames_sorted = false;
diff --git a/ld/ldlex.h b/ld/ldlex.h
index e5ac2fa7fca..a2c49656e1a 100644
--- a/ld/ldlex.h
+++ b/ld/ldlex.h
@@ -68,6 +68,7 @@ enum option_values
   OPTION_TASK_LINK,
   OPTION_TBSS,
   OPTION_TDATA,
+  OPTION_TEXT_SECTION_ORDERING_FILE,
   OPTION_TTEXT,
   OPTION_TTEXT_SEGMENT,
   OPTION_TRODATA_SEGMENT,
@@ -484,6 +485,8 @@ extern input_type parser_input;
 
 extern unsigned int lineno;
 extern const char *lex_string;
+extern int in_text_section_ordering_file;
+extern int seen_eof_include_file;
 
 /* In ldlex.l.  */
 extern int yylex (void);
diff --git a/ld/ldlex.l b/ld/ldlex.l
index e113c90812b..b41e1220661 100644
--- a/ld/ldlex.l
+++ b/ld/ldlex.l
@@ -43,6 +43,12 @@ input_type parser_input;
 /* Line number in the current input file.  */
 unsigned int lineno;
 
+/* 1 if the current input file is the text section ordering file.  */
+int in_text_section_ordering_file = 0;
+
+/* 1 if the end of the include file is reached.  */
+int seen_eof_include_file = 0;
+
 /* The string we are currently lexing, or NULL if we are reading a
    file.  */
 const char *lex_string = NULL;
@@ -487,6 +493,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
 
   lineno = lineno_stack[include_stack_ptr];
   input_flags.sysrooted = sysrooted_stack[include_stack_ptr];
+  seen_eof_include_file = 1;
 
   return END;
 }
diff --git a/ld/lexsup.c b/ld/lexsup.c
index 099dff8ecde..21385628020 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -484,6 +484,9 @@ static const struct ld_option ld_options[] =
   { {"sort-section", required_argument, NULL, OPTION_SORT_SECTION},
     '\0', N_("name|alignment"),
     N_("Sort sections by name or maximum alignment"), TWO_DASHES },
+  { {"text-section-ordering-file", required_argument, NULL, OPTION_TEXT_SECTION_ORDERING_FILE},
+    '\0', N_("FILE"),
+    N_("Sort text sections by FILE"), TWO_DASHES },
   { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
     '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"),
     TWO_DASHES },
@@ -670,6 +673,7 @@ parse_args (unsigned argc, char **argv)
     dynamic_list
   } opt_dynamic_list = dynamic_list_unset;
   struct bfd_elf_dynamic_list *export_list = NULL;
+  bool seen_linker_script = false;
 
   shortopts = (char *) xmalloc (OPTION_COUNT * 3 + 2);
   longopts = (struct option *)
@@ -1394,6 +1398,12 @@ parse_args (unsigned argc, char **argv)
 	    einfo (_("%F%P: invalid section sorting option: %s\n"),
 		   optarg);
 	  break;
+	case OPTION_TEXT_SECTION_ORDERING_FILE:
+	  if (seen_linker_script)
+	    einfo (_("%F%P: --text-section-ordering-file must be placed"
+		     " before -T/--script\n"));
+	  config.text_section_ordering_file = optarg;
+	  break;
 	case OPTION_STATS:
 	  config.stats = true;
 	  break;
@@ -1410,6 +1420,7 @@ parse_args (unsigned argc, char **argv)
 	  ++trace_files;
 	  break;
 	case 'T':
+	  seen_linker_script = true;
 	  previous_script_handle = saved_script_handle;
 	  ldfile_open_script_file (optarg);
 	  parser_input = input_script;
diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
index fae7c2ad71c..8014dfbfa27 100644
--- a/ld/scripttempl/elf.sc
+++ b/ld/scripttempl/elf.sc
@@ -553,11 +553,12 @@ cat <<EOF
   .text         ${RELOCATING-0} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
+    ${RELOCATING+*(.text.hot .text.hot.*)}
+    ${RELOCATING+*(SORT(.text.sorted.*))}
     ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
     ${RELOCATING+*(.text.exit .text.exit.*)}
     ${RELOCATING+*(.text.startup .text.startup.*)}
-    ${RELOCATING+*(.text.hot .text.hot.*)}
-    ${RELOCATING+*(SORT(.text.sorted.*))}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/testsuite/ld-scripts/start.s b/ld/testsuite/ld-scripts/start.s
new file mode 100644
index 00000000000..4d8239479a6
--- /dev/null
+++ b/ld/testsuite/ld-scripts/start.s
@@ -0,0 +1,12 @@
+	.text
+	.global start	/* Used by SH targets.  */
+start:
+	.global _start
+_start:
+	.global __start
+__start:
+	.global main	/* Used by HPPA targets.  */
+main:
+	.globl	_main	/* Used by LynxOS targets.  */
+_main:
+	.dc.a 0
diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
new file mode 100644
index 00000000000..87cee75055f
--- /dev/null
+++ b/ld/testsuite/ld-scripts/text-order-1a.d
@@ -0,0 +1,20 @@
+#source: text-order-1a.s
+#source: text-order-1b.s
+#source: start.s
+#ld: --text-section-ordering-file text-order-1a.t
+#nm: -n
+#target: [is_elf_format]
+#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
+# generic linker targets don't support --text-section-ordering-file
+
+#...
+[0-9a-f]+ T yyy
+#...
+[0-9a-f]+ T bar
+#...
+[0-9a-f]+ T _start
+#...
+[0-9a-f]+ T xxx
+#...
+[0-9a-f]+ T foo
+#pass
diff --git a/ld/testsuite/ld-scripts/text-order-1a.s b/ld/testsuite/ld-scripts/text-order-1a.s
new file mode 100644
index 00000000000..25f59b9e444
--- /dev/null
+++ b/ld/testsuite/ld-scripts/text-order-1a.s
@@ -0,0 +1,8 @@
+	.section .text.foo
+	.globl	foo
+foo:
+	.dc.a 0
+	.section .text.bar
+	.globl	bar
+bar:
+	.dc.a 0
diff --git a/ld/testsuite/ld-scripts/text-order-1a.t b/ld/testsuite/ld-scripts/text-order-1a.t
new file mode 100644
index 00000000000..72ea5f4cf46
--- /dev/null
+++ b/ld/testsuite/ld-scripts/text-order-1a.t
@@ -0,0 +1,5 @@
+*(.text.yyy)
+*(.text.b?r)
+*(.t*t)
+*(.text.xxx)
+*(.text.foo)
diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
new file mode 100644
index 00000000000..3f83813da2b
--- /dev/null
+++ b/ld/testsuite/ld-scripts/text-order-1b.d
@@ -0,0 +1,20 @@
+#source: text-order-1a.s
+#source: text-order-1b.s
+#source: start.s
+#ld: --text-section-ordering-file text-order-1b.t
+#nm: -n
+#target: [is_elf_format]
+#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
+# generic linker targets don't support --text-section-ordering-file
+
+#...
+[0-9a-f]+ T yyy
+#...
+[0-9a-f]+ T bar
+#...
+[0-9a-f]+ T _start
+#...
+[0-9a-f]+ T xxx
+#...
+[0-9a-f]+ T foo
+#pass
diff --git a/ld/testsuite/ld-scripts/text-order-1b.s b/ld/testsuite/ld-scripts/text-order-1b.s
new file mode 100644
index 00000000000..7ae27b9676a
--- /dev/null
+++ b/ld/testsuite/ld-scripts/text-order-1b.s
@@ -0,0 +1,8 @@
+	.section .text.xxx
+	.globl	xxx
+xxx:
+	.dc.a 0
+	.section .text.yyy
+	.globl	yyy
+yyy:
+	.dc.a 0
diff --git a/ld/testsuite/ld-scripts/text-order-1b.t b/ld/testsuite/ld-scripts/text-order-1b.t
new file mode 100644
index 00000000000..c1d092edb7e
--- /dev/null
+++ b/ld/testsuite/ld-scripts/text-order-1b.t
@@ -0,0 +1,4 @@
+.text.yyy
+.text.b?r
+.t*t
+.text.xxx .text.foo
diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
new file mode 100644
index 00000000000..0c484637784
--- /dev/null
+++ b/ld/testsuite/ld-scripts/text-order-1c.d
@@ -0,0 +1,9 @@
+#source: text-order-1a.s
+#source: text-order-1b.s
+#source: start.s
+#ld: --text-section-ordering-file text-order-1c.t
+#nm: -n
+#target: [is_elf_format]
+#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
+# generic linker targets don't support --text-section-ordering-file
+#error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
diff --git a/ld/testsuite/ld-scripts/text-order-1c.t b/ld/testsuite/ld-scripts/text-order-1c.t
new file mode 100644
index 00000000000..9ecbfb42826
--- /dev/null
+++ b/ld/testsuite/ld-scripts/text-order-1c.t
@@ -0,0 +1,5 @@
+.text.yyy
+.text.b?r
+INCLUDE text-order-1b.t
+.t*t
+.text.xxx .text.foo
diff --git a/ld/testsuite/ld-scripts/text-order.exp b/ld/testsuite/ld-scripts/text-order.exp
new file mode 100644
index 00000000000..30cb531137e
--- /dev/null
+++ b/ld/testsuite/ld-scripts/text-order.exp
@@ -0,0 +1,42 @@
+# Test for --text-section-ordering-file FILE.
+# Copyright (C) 2024 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.
+
+if { !([is_elf_format] && ![is_generic]) } {
+    return
+}
+
+set old_ldflags $LDFLAGS
+if { [istarget spu*-*-*] } then {
+    set LDFLAGS "$LDFLAGS --local-store 0:0 --no-overlays"
+} elseif { [is_pecoff_format] } then {
+    set LDFLAGS "$LDFLAGS --image-base 0"
+} elseif { [is_xcoff_format] } then {
+    set LDFLAGS "$LDFLAGS -bnogc"
+}
+
+set test_list [lsort [glob -nocomplain $srcdir/$subdir/text-order*.d]]
+foreach test_file $test_list {
+    set test_name [file rootname $test_file]
+    set map_file "tmpdir/[file tail $test_name].map"
+    verbose $test_name
+    run_dump_test $test_name
+}
+
+set LDFLAGS $old_ldflags
-- 
2.43.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts
  2024-01-19 19:45 [PATCH 0/4] ld: Add --text-section-ordering-file FILE H.J. Lu
  2024-01-19 19:45 ` [PATCH 1/4] " H.J. Lu
@ 2024-01-19 19:45 ` H.J. Lu
  2024-01-19 20:00   ` Noah Goldstein
  2024-01-19 19:45 ` [PATCH 3/4] ld: Include the text section order file in PE COFF " H.J. Lu
  2024-01-19 19:45 ` [PATCH 4/4] ld: Document --text-section-ordering-file FILE H.J. Lu
  3 siblings, 1 reply; 19+ messages in thread
From: H.J. Lu @ 2024-01-19 19:45 UTC (permalink / raw)
  To: binutils; +Cc: goldstein.w.n

Include the text section order file in all ELF linker scripts and enable
--text-section-ordering-file tests for all ELF targets.

	* scripttempl/avr.sc: Include the text section order file.
	* scripttempl/dlx.sc: Likewise.
	* scripttempl/elf32cr16.sc: Likewise.
	* scripttempl/elf32crx.sc: Likewise.
	* scripttempl/elf32msp430.sc: Likewise.
	* scripttempl/elf64bpf.sc: Likewise.
	* scripttempl/elf64hppa.sc: Likewise.
	* scripttempl/elfarc.sc: Likewise.
	* scripttempl/elfarcv2.sc: Likewise.
	* scripttempl/elfd10v.sc: Likewise.
	* scripttempl/elfd30v.sc: Likewise.
	* scripttempl/elfm68hc11.sc: Likewise.
	* scripttempl/elfm68hc12.sc: Likewise.
	* scripttempl/elfm9s12z.sc: Likewise.
	* scripttempl/elfmicroblaze.sc: Likewise.
	* scripttempl/elfxgate.sc: Likewise.
	* scripttempl/elfxtensa.sc: Likewise.
	* scripttempl/ft32.sc: Likewise.
	* scripttempl/iq2000.sc: Likewise.
	* scripttempl/mep.sc: Likewise.
	* scripttempl/mmo.sc: Likewise.
	* scripttempl/pru.sc: Likewise.
	* scripttempl/v850.sc: Likewise.
	* scripttempl/v850_rh850.sc: Likewise.
	* scripttempl/visium.sc: Likewise.
	* scripttempl/xstormy16.sc: Likewise.
	* scripttempl/arclinux.sc: Include the text section order file
	and place code text sections toward the end.
	* scripttempl/elf64bpf.sc: Likewise.
	* scripttempl/nds32elf.sc: Likewise.
	* testsuite/ld-scripts/text-order-1a.d: Remove xfail.
	* testsuite/ld-scripts/text-order-1b.d: Likewise.
	* testsuite/ld-scripts/text-order-1c.d: Likewise.
---
 ld/scripttempl/arclinux.sc              | 5 +++--
 ld/scripttempl/avr.sc                   | 1 +
 ld/scripttempl/dlx.sc                   | 1 +
 ld/scripttempl/elf32cr16.sc             | 1 +
 ld/scripttempl/elf32crx.sc              | 1 +
 ld/scripttempl/elf32msp430.sc           | 1 +
 ld/scripttempl/elf64bpf.sc              | 5 +++--
 ld/scripttempl/elf64hppa.sc             | 1 +
 ld/scripttempl/elfarc.sc                | 1 +
 ld/scripttempl/elfarcv2.sc              | 1 +
 ld/scripttempl/elfd10v.sc               | 1 +
 ld/scripttempl/elfd30v.sc               | 1 +
 ld/scripttempl/elfm68hc11.sc            | 1 +
 ld/scripttempl/elfm68hc12.sc            | 1 +
 ld/scripttempl/elfm9s12z.sc             | 1 +
 ld/scripttempl/elfmicroblaze.sc         | 1 +
 ld/scripttempl/elfxgate.sc              | 1 +
 ld/scripttempl/elfxtensa.sc             | 1 +
 ld/scripttempl/ft32.sc                  | 1 +
 ld/scripttempl/iq2000.sc                | 1 +
 ld/scripttempl/mep.sc                   | 1 +
 ld/scripttempl/mmo.sc                   | 1 +
 ld/scripttempl/nds32elf.sc              | 5 +++--
 ld/scripttempl/pru.sc                   | 1 +
 ld/scripttempl/v850.sc                  | 1 +
 ld/scripttempl/v850_rh850.sc            | 1 +
 ld/scripttempl/visium.sc                | 1 +
 ld/scripttempl/xstormy16.sc             | 1 +
 ld/testsuite/ld-scripts/text-order-1a.d | 2 --
 ld/testsuite/ld-scripts/text-order-1b.d | 2 --
 ld/testsuite/ld-scripts/text-order-1c.d | 2 --
 31 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc
index 36ba5a664d3..646dc08a9f8 100644
--- a/ld/scripttempl/arclinux.sc
+++ b/ld/scripttempl/arclinux.sc
@@ -487,11 +487,12 @@ cat <<EOF
   .text         ${RELOCATING-0} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}}
-    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
-    ${RELOCATING+*(.text.exit .text.exit.*)}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     ${RELOCATING+*(.text.startup .text.startup.*)}
     ${RELOCATING+*(.text.hot .text.hot.*)}
     ${RELOCATING+*(SORT(.text.sorted.*))}
+    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
+    ${RELOCATING+*(.text.exit .text.exit.*)}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/avr.sc b/ld/scripttempl/avr.sc
index 3b18897f33c..558b4e22c75 100644
--- a/ld/scripttempl/avr.sc
+++ b/ld/scripttempl/avr.sc
@@ -244,6 +244,7 @@ SECTIONS
     KEEP (*(.init8))
     *(.init9)  /* Call main().  */
     KEEP (*(.init9))}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+. = ALIGN(2);
     *(.text.*)
diff --git a/ld/scripttempl/dlx.sc b/ld/scripttempl/dlx.sc
index 8dcbf300c56..e66c68f064d 100644
--- a/ld/scripttempl/dlx.sc
+++ b/ld/scripttempl/dlx.sc
@@ -22,6 +22,7 @@ SECTIONS
   .text :
   {
     ${RELOCATING+CREATE_OBJECT_SYMBOLS}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+etext = ${DATA_ALIGNMENT};}
   }
diff --git a/ld/scripttempl/elf32cr16.sc b/ld/scripttempl/elf32cr16.sc
index 3e7d92a4f34..2d6cd971bc2 100644
--- a/ld/scripttempl/elf32cr16.sc
+++ b/ld/scripttempl/elf32cr16.sc
@@ -81,6 +81,7 @@ SECTIONS
   .text :
   {
     __TEXT_START = .;
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text) *(.text.*) *(.gnu.linkonce.t.*)
     __TEXT_END = .;
   }${RELOCATING+ > rom}
diff --git a/ld/scripttempl/elf32crx.sc b/ld/scripttempl/elf32crx.sc
index 1b1316676a7..1eb5e752c3a 100644
--- a/ld/scripttempl/elf32crx.sc
+++ b/ld/scripttempl/elf32crx.sc
@@ -77,6 +77,7 @@ SECTIONS
   .text :
   {
     __TEXT_START = .;
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text) *(.text.*) *(.gnu.linkonce.t.*)
     __TEXT_END = .;
   } > rom
diff --git a/ld/scripttempl/elf32msp430.sc b/ld/scripttempl/elf32msp430.sc
index bed0d673238..1fadaca2e27 100644
--- a/ld/scripttempl/elf32msp430.sc
+++ b/ld/scripttempl/elf32msp430.sc
@@ -163,6 +163,7 @@ SECTIONS
     *(.lower.text.* .lower.text)
 
     . = ALIGN(2);}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+. = ALIGN(2);
     *(.text.*)
diff --git a/ld/scripttempl/elf64bpf.sc b/ld/scripttempl/elf64bpf.sc
index ca62d7c88e0..93d8592a6a6 100644
--- a/ld/scripttempl/elf64bpf.sc
+++ b/ld/scripttempl/elf64bpf.sc
@@ -508,11 +508,12 @@ cat <<EOF
   .text         ${RELOCATING-0} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}}
-    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
-    ${RELOCATING+*(.text.exit .text.exit.*)}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     ${RELOCATING+*(.text.startup .text.startup.*)}
     ${RELOCATING+*(.text.hot .text.hot.*)}
     ${RELOCATING+*(SORT(.text.sorted.*))}
+    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
+    ${RELOCATING+*(.text.exit .text.exit.*)}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/elf64hppa.sc b/ld/scripttempl/elf64hppa.sc
index 6bde304dd27..94b458010c5 100644
--- a/ld/scripttempl/elf64hppa.sc
+++ b/ld/scripttempl/elf64hppa.sc
@@ -412,6 +412,7 @@ cat <<EOF
   .text         ${RELOCATING-0} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/elfarc.sc b/ld/scripttempl/elfarc.sc
index 1ae0248a900..ec16bf5a184 100644
--- a/ld/scripttempl/elfarc.sc
+++ b/ld/scripttempl/elfarc.sc
@@ -288,6 +288,7 @@ cat <<EOF
   .text         ${RELOCATING-0} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/elfarcv2.sc b/ld/scripttempl/elfarcv2.sc
index 3054e4c62f3..30bcbd304ab 100644
--- a/ld/scripttempl/elfarcv2.sc
+++ b/ld/scripttempl/elfarcv2.sc
@@ -192,6 +192,7 @@ SECTIONS
 
     /* Remaining code.  */
     ${RELOCATING+ . = ALIGN(4);}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/elfd10v.sc b/ld/scripttempl/elfd10v.sc
index 1ecf4a19092..e42bf3cfa70 100644
--- a/ld/scripttempl/elfd10v.sc
+++ b/ld/scripttempl/elfd10v.sc
@@ -109,6 +109,7 @@ SECTIONS
     KEEP (*(SORT_NONE(.init.*)))
     KEEP (*(SORT_NONE(.fini)))
     KEEP (*(SORT_NONE(.fini.*)))}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     /* .gnu.warning sections are handled specially by elf.em.  */
diff --git a/ld/scripttempl/elfd30v.sc b/ld/scripttempl/elfd30v.sc
index ed0c412edc8..2dcbb7522cc 100644
--- a/ld/scripttempl/elfd30v.sc
+++ b/ld/scripttempl/elfd30v.sc
@@ -125,6 +125,7 @@ SECTIONS
   /* Internal text space or external memory */
   .text :
   {
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.gnu.linkonce.t*)
     KEEP (*(SORT_NONE(.fini)))
diff --git a/ld/scripttempl/elfm68hc11.sc b/ld/scripttempl/elfm68hc11.sc
index b122da0e32a..f4393a87b68 100644
--- a/ld/scripttempl/elfm68hc11.sc
+++ b/ld/scripttempl/elfm68hc11.sc
@@ -318,6 +318,7 @@ SECTIONS
     /* Put startup code at beginning so that _start keeps same address.  */
     ${RELOCATING+${STARTUP_CODE}}
 
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     /* .gnu.warning sections are handled specially by elf.em.  */
diff --git a/ld/scripttempl/elfm68hc12.sc b/ld/scripttempl/elfm68hc12.sc
index 47410a95f46..24ea7ed0302 100644
--- a/ld/scripttempl/elfm68hc12.sc
+++ b/ld/scripttempl/elfm68hc12.sc
@@ -317,6 +317,7 @@ SECTIONS
     /* Put startup code at beginning so that _start keeps same address.  */
     ${RELOCATING+${STARTUP_CODE}}
 
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     /* .gnu.warning sections are handled specially by elf.em.  */
diff --git a/ld/scripttempl/elfm9s12z.sc b/ld/scripttempl/elfm9s12z.sc
index 271d218835b..2d929e1e3fe 100644
--- a/ld/scripttempl/elfm9s12z.sc
+++ b/ld/scripttempl/elfm9s12z.sc
@@ -303,6 +303,7 @@ SECTIONS
     /* Put startup code at beginning so that _start keeps same address.  */
     ${RELOCATING+${STARTUP_CODE}}
 
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     /* .gnu.warning sections are handled specially by elf.em.  */
diff --git a/ld/scripttempl/elfmicroblaze.sc b/ld/scripttempl/elfmicroblaze.sc
index 88abe4f10ba..e386da51512 100644
--- a/ld/scripttempl/elfmicroblaze.sc
+++ b/ld/scripttempl/elfmicroblaze.sc
@@ -79,6 +79,7 @@ SECTIONS
 
   ${RELOCATING+ _ftext  =  .;}
   .text : {
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     ${RELOCATING+*(.gnu.linkonce.t.*)}
diff --git a/ld/scripttempl/elfxgate.sc b/ld/scripttempl/elfxgate.sc
index 4c25bc161f2..71d4855f47e 100644
--- a/ld/scripttempl/elfxgate.sc
+++ b/ld/scripttempl/elfxgate.sc
@@ -317,6 +317,7 @@ SECTIONS
     /* Put startup code at beginning so that _start keeps same address.  */
     ${RELOCATING+${STARTUP_CODE}}
 
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     /* .gnu.warning sections are handled specially by elf.em.  */
diff --git a/ld/scripttempl/elfxtensa.sc b/ld/scripttempl/elfxtensa.sc
index 231f53b5f5b..abc1d33350e 100644
--- a/ld/scripttempl/elfxtensa.sc
+++ b/ld/scripttempl/elfxtensa.sc
@@ -418,6 +418,7 @@ cat <<EOF
     ${RELOCATING+${INIT_END}}
 
     ${RELOCATING+${TEXT_START_SYMBOLS}}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.literal .text .stub${RELOCATING+ .literal.* .text.* .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/ft32.sc b/ld/scripttempl/ft32.sc
index e52e75cd924..8c966de3bb1 100644
--- a/ld/scripttempl/ft32.sc
+++ b/ld/scripttempl/ft32.sc
@@ -32,6 +32,7 @@ SECTIONS
 {
   .text :
   {
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text${RELOCATING+*})
     ${RELOCATING+*(.strings)
     *(._pm*)
diff --git a/ld/scripttempl/iq2000.sc b/ld/scripttempl/iq2000.sc
index e85aa2303ed..94e834aef15 100644
--- a/ld/scripttempl/iq2000.sc
+++ b/ld/scripttempl/iq2000.sc
@@ -297,6 +297,7 @@ cat <<EOF
   .text    ${RELOCATING-0} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     *(.stub)
diff --git a/ld/scripttempl/mep.sc b/ld/scripttempl/mep.sc
index 39e4d64857c..656b1f64386 100644
--- a/ld/scripttempl/mep.sc
+++ b/ld/scripttempl/mep.sc
@@ -310,6 +310,7 @@ cat <<EOF
   .text         ${RELOCATING-0} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/mmo.sc b/ld/scripttempl/mmo.sc
index e87a758eb90..ac6a57eee2a 100644
--- a/ld/scripttempl/mmo.sc
+++ b/ld/scripttempl/mmo.sc
@@ -30,6 +30,7 @@ SECTIONS
     ${RELOCATING+ KEEP (*(SORT_NONE(.init)))}
     ${RELOCATING+ PROVIDE (_init_end = .);}
 
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     ${RELOCATING+*(.gnu.linkonce.t*)}
diff --git a/ld/scripttempl/nds32elf.sc b/ld/scripttempl/nds32elf.sc
index 0f8366d91ac..2626e773a4e 100644
--- a/ld/scripttempl/nds32elf.sc
+++ b/ld/scripttempl/nds32elf.sc
@@ -434,11 +434,12 @@ cat <<EOF
   .text         ${RELOCATING-0} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}}
-    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
-    ${RELOCATING+*(.text.exit .text.exit.*)}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     ${RELOCATING+*(.text.startup .text.startup.*)}
     ${RELOCATING+*(.text.hot .text.hot.*)}
     ${RELOCATING+*(SORT(.text.sorted.*))}
+    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
+    ${RELOCATING+*(.text.exit .text.exit.*)}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/pru.sc b/ld/scripttempl/pru.sc
index 3ff86bc61c7..d4d79cbcf82 100644
--- a/ld/scripttempl/pru.sc
+++ b/ld/scripttempl/pru.sc
@@ -110,6 +110,7 @@ SECTIONS
     ${RELOCATING+KEEP (*(.init0))}
 
     ${RELOCATING+. = ALIGN(4);}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+. = ALIGN(4);}
     ${RELOCATING+*(.text.*)}
diff --git a/ld/scripttempl/v850.sc b/ld/scripttempl/v850.sc
index 10d1da8b5bb..530b4ee72ca 100644
--- a/ld/scripttempl/v850.sc
+++ b/ld/scripttempl/v850.sc
@@ -76,6 +76,7 @@ SECTIONS
 
   .text		:
   {
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
 
diff --git a/ld/scripttempl/v850_rh850.sc b/ld/scripttempl/v850_rh850.sc
index e63e7db8884..3d2874267a9 100644
--- a/ld/scripttempl/v850_rh850.sc
+++ b/ld/scripttempl/v850_rh850.sc
@@ -80,6 +80,7 @@ SECTIONS
 
   .text		:
   {
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
 
diff --git a/ld/scripttempl/visium.sc b/ld/scripttempl/visium.sc
index 0b90b888136..8d97bbf4a68 100644
--- a/ld/scripttempl/visium.sc
+++ b/ld/scripttempl/visium.sc
@@ -67,6 +67,7 @@ SECTIONS
 
   .text ${RELOCATING-0} : {
     ${RELOCATING+ _ftext  =  .;}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     ${RELOCATING+*(.gnu.linkonce.t.*)}
diff --git a/ld/scripttempl/xstormy16.sc b/ld/scripttempl/xstormy16.sc
index c4117fcde3e..3de1bfa0d18 100644
--- a/ld/scripttempl/xstormy16.sc
+++ b/ld/scripttempl/xstormy16.sc
@@ -179,6 +179,7 @@ SECTIONS
   .text    ${RELOCATING-0} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     *(.stub)
diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
index 87cee75055f..d6f976f42ad 100644
--- a/ld/testsuite/ld-scripts/text-order-1a.d
+++ b/ld/testsuite/ld-scripts/text-order-1a.d
@@ -4,8 +4,6 @@
 #ld: --text-section-ordering-file text-order-1a.t
 #nm: -n
 #target: [is_elf_format]
-#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
-# generic linker targets don't support --text-section-ordering-file
 
 #...
 [0-9a-f]+ T yyy
diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
index 3f83813da2b..3d6f4fd8d66 100644
--- a/ld/testsuite/ld-scripts/text-order-1b.d
+++ b/ld/testsuite/ld-scripts/text-order-1b.d
@@ -4,8 +4,6 @@
 #ld: --text-section-ordering-file text-order-1b.t
 #nm: -n
 #target: [is_elf_format]
-#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
-# generic linker targets don't support --text-section-ordering-file
 
 #...
 [0-9a-f]+ T yyy
diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
index 0c484637784..4166d634606 100644
--- a/ld/testsuite/ld-scripts/text-order-1c.d
+++ b/ld/testsuite/ld-scripts/text-order-1c.d
@@ -4,6 +4,4 @@
 #ld: --text-section-ordering-file text-order-1c.t
 #nm: -n
 #target: [is_elf_format]
-#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
-# generic linker targets don't support --text-section-ordering-file
 #error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
-- 
2.43.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 3/4] ld: Include the text section order file in PE COFF linker scripts
  2024-01-19 19:45 [PATCH 0/4] ld: Add --text-section-ordering-file FILE H.J. Lu
  2024-01-19 19:45 ` [PATCH 1/4] " H.J. Lu
  2024-01-19 19:45 ` [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts H.J. Lu
@ 2024-01-19 19:45 ` H.J. Lu
  2024-01-19 19:45 ` [PATCH 4/4] ld: Document --text-section-ordering-file FILE H.J. Lu
  3 siblings, 0 replies; 19+ messages in thread
From: H.J. Lu @ 2024-01-19 19:45 UTC (permalink / raw)
  To: binutils; +Cc: goldstein.w.n

Include the text section order file in PE COFF linker scripts and enable
--text-section-ordering-file tests for PE COFF targets.

	* scripttempl/i386beos.sc: Include the text section order file.
	* scripttempl/i386go32.sc: Likewise.
	* scripttempl/mcorepe.sc: Likewise.
	* scripttempl/pe.sc: Likewise.
	* scripttempl/pep.sc: Likewise.
	* testsuite/ld-scripts/text-order-1a.d: Remove target.
	* testsuite/ld-scripts/text-order-1b.d: Likewise.
	* testsuite/ld-scripts/text-order-1c.d: Likewise.
	* testsuite/ld-scripts/text-order.exp: Only run for ELF and PE
	COFF targets.
---
 ld/scripttempl/i386beos.sc              | 1 +
 ld/scripttempl/i386go32.sc              | 1 +
 ld/scripttempl/mcorepe.sc               | 1 +
 ld/scripttempl/pe.sc                    | 1 +
 ld/scripttempl/pep.sc                   | 1 +
 ld/testsuite/ld-scripts/start.s         | 2 ++
 ld/testsuite/ld-scripts/text-order-1a.d | 1 -
 ld/testsuite/ld-scripts/text-order-1b.d | 1 -
 ld/testsuite/ld-scripts/text-order-1c.d | 1 -
 ld/testsuite/ld-scripts/text-order.exp  | 3 ++-
 10 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/ld/scripttempl/i386beos.sc b/ld/scripttempl/i386beos.sc
index 6c512e67760..cc3d737b21c 100644
--- a/ld/scripttempl/i386beos.sc
+++ b/ld/scripttempl/i386beos.sc
@@ -60,6 +60,7 @@ SECTIONS
   {
     ${RELOCATING+ __text_start__ = . ;}
     ${RELOCATING+ KEEP (*(SORT_NONE(.init)))}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${R_TEXT}
     *(.glue_7t)
diff --git a/ld/scripttempl/i386go32.sc b/ld/scripttempl/i386go32.sc
index 9b992279e4e..3a229319988 100644
--- a/ld/scripttempl/i386go32.sc
+++ b/ld/scripttempl/i386go32.sc
@@ -34,6 +34,7 @@ ${RELOCATING+ENTRY (${ENTRY})}
 SECTIONS
 {
   .text ${RELOCATING+ ${TARGET_PAGE_SIZE}+SIZEOF_HEADERS} : {
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${RELOCATING+*(.text.*)}
     ${RELOCATING+*(.gnu.linkonce.t*)}
diff --git a/ld/scripttempl/mcorepe.sc b/ld/scripttempl/mcorepe.sc
index 3262102e85e..13dc5b1c89e 100644
--- a/ld/scripttempl/mcorepe.sc
+++ b/ld/scripttempl/mcorepe.sc
@@ -66,6 +66,7 @@ SECTIONS
   .text ${RELOCATING+ __image_base__ + __section_alignment__ } :
   {
     ${RELOCATING+ KEEP (*(SORT_NONE(.init)))}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${R_TEXT}
     ${RELOCATING+ *(.text.*)}
diff --git a/ld/scripttempl/pe.sc b/ld/scripttempl/pe.sc
index 70f5194b02f..b2691e0ef18 100644
--- a/ld/scripttempl/pe.sc
+++ b/ld/scripttempl/pe.sc
@@ -91,6 +91,7 @@ SECTIONS
   .text ${RELOCATING+ __image_base__ + ( __section_alignment__ < ${TARGET_PAGE_SIZE} ? . : __section_alignment__ )} :
   {
     ${RELOCATING+KEEP (*(SORT_NONE(.init)))}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${R_TEXT}
     ${RELOCATING+ *(.text.*)}
diff --git a/ld/scripttempl/pep.sc b/ld/scripttempl/pep.sc
index 63039f11574..f35e50c9430 100644
--- a/ld/scripttempl/pep.sc
+++ b/ld/scripttempl/pep.sc
@@ -92,6 +92,7 @@ SECTIONS
   .text ${RELOCATING+ __image_base__ + ( __section_alignment__ < ${TARGET_PAGE_SIZE} ? . : __section_alignment__ )} :
   {
     ${RELOCATING+KEEP (*(SORT_NONE(.init)))}
+    ${RELOCATING+INCLUDE config.text_section_ordering_file}
     *(.text)
     ${R_TEXT}
     ${RELOCATING+ *(.text.*)}
diff --git a/ld/testsuite/ld-scripts/start.s b/ld/testsuite/ld-scripts/start.s
index 4d8239479a6..3f646267716 100644
--- a/ld/testsuite/ld-scripts/start.s
+++ b/ld/testsuite/ld-scripts/start.s
@@ -5,6 +5,8 @@ start:
 _start:
 	.global __start
 __start:
+	.global _mainCRTStartup	/* Used by PE targets.  */
+_mainCRTStartup:
 	.global main	/* Used by HPPA targets.  */
 main:
 	.globl	_main	/* Used by LynxOS targets.  */
diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
index d6f976f42ad..3aca3d6d5e9 100644
--- a/ld/testsuite/ld-scripts/text-order-1a.d
+++ b/ld/testsuite/ld-scripts/text-order-1a.d
@@ -3,7 +3,6 @@
 #source: start.s
 #ld: --text-section-ordering-file text-order-1a.t
 #nm: -n
-#target: [is_elf_format]
 
 #...
 [0-9a-f]+ T yyy
diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
index 3d6f4fd8d66..c9d83276da6 100644
--- a/ld/testsuite/ld-scripts/text-order-1b.d
+++ b/ld/testsuite/ld-scripts/text-order-1b.d
@@ -3,7 +3,6 @@
 #source: start.s
 #ld: --text-section-ordering-file text-order-1b.t
 #nm: -n
-#target: [is_elf_format]
 
 #...
 [0-9a-f]+ T yyy
diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
index 4166d634606..91f0a6bee4d 100644
--- a/ld/testsuite/ld-scripts/text-order-1c.d
+++ b/ld/testsuite/ld-scripts/text-order-1c.d
@@ -3,5 +3,4 @@
 #source: start.s
 #ld: --text-section-ordering-file text-order-1c.t
 #nm: -n
-#target: [is_elf_format]
 #error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
diff --git a/ld/testsuite/ld-scripts/text-order.exp b/ld/testsuite/ld-scripts/text-order.exp
index 30cb531137e..2e51fd6f62a 100644
--- a/ld/testsuite/ld-scripts/text-order.exp
+++ b/ld/testsuite/ld-scripts/text-order.exp
@@ -18,7 +18,8 @@
 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
 # MA 02110-1301, USA.
 
-if { !([is_elf_format] && ![is_generic]) } {
+# ELF and PE COFF linker targets support input .text.* sections.
+if { !([is_elf_format] || [is_pecoff_format]) } {
     return
 }
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH 4/4] ld: Document --text-section-ordering-file FILE
  2024-01-19 19:45 [PATCH 0/4] ld: Add --text-section-ordering-file FILE H.J. Lu
                   ` (2 preceding siblings ...)
  2024-01-19 19:45 ` [PATCH 3/4] ld: Include the text section order file in PE COFF " H.J. Lu
@ 2024-01-19 19:45 ` H.J. Lu
  3 siblings, 0 replies; 19+ messages in thread
From: H.J. Lu @ 2024-01-19 19:45 UTC (permalink / raw)
  To: binutils; +Cc: goldstein.w.n

--text-section-ordering-file FILE is supported only in ELF and PE COFF
linkers which support input .text.* sections.

	* NEWS: Mention --text-section-ordering-file FILE.
	* ld.texi: Document text-section-ordering-file FILE.
---
 ld/NEWS    |  3 +++
 ld/ld.texi | 11 +++++++++++
 2 files changed, 14 insertions(+)

diff --git a/ld/NEWS b/ld/NEWS
index c3ebe320e5e..c98570f308a 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,8 @@
 -*- text -*-
 
+* Add the linker option, --text-section-ordering-file FILE, for ELF and
+  PE COFF linker to specify the input text section order.
+
 Changes in 2.42:
 
 * Add -z mark-plt/-z nomark-plt options to x86-64 ELF linker to mark PLT
diff --git a/ld/ld.texi b/ld/ld.texi
index 4fda259a552..d1bb59202de 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -2541,6 +2541,17 @@ patterns in the linker script.
 This option will apply @code{SORT_BY_ALIGNMENT} to all wildcard section
 patterns in the linker script.
 
+@kindex text-section-ordering-file=@file{file}
+@item text-section-ordering-file=@file{file}
+This option groups input text sections together in the order specified
+in the file which contains the list of text section wildcards starting
+with @code{.}, separated by whitespace.
+
+If the @option{verbose} option is enabled, the contents of the text
+section ordering file will be reported.
+
+This option is supported only in ELF and PE COFF linkers.
+
 @kindex --spare-dynamic-tags
 @item --spare-dynamic-tags=@var{count}
 This option specifies the number of empty slots to leave in the
-- 
2.43.0


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts
  2024-01-19 19:45 ` [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts H.J. Lu
@ 2024-01-19 20:00   ` Noah Goldstein
  2024-01-19 20:18     ` H.J. Lu
  0 siblings, 1 reply; 19+ messages in thread
From: Noah Goldstein @ 2024-01-19 20:00 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Fri, Jan 19, 2024 at 7:45 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> Include the text section order file in all ELF linker scripts and enable
> --text-section-ordering-file tests for all ELF targets.
>
>         * scripttempl/avr.sc: Include the text section order file.
>         * scripttempl/dlx.sc: Likewise.
>         * scripttempl/elf32cr16.sc: Likewise.
>         * scripttempl/elf32crx.sc: Likewise.
>         * scripttempl/elf32msp430.sc: Likewise.
>         * scripttempl/elf64bpf.sc: Likewise.
>         * scripttempl/elf64hppa.sc: Likewise.
>         * scripttempl/elfarc.sc: Likewise.
>         * scripttempl/elfarcv2.sc: Likewise.
>         * scripttempl/elfd10v.sc: Likewise.
>         * scripttempl/elfd30v.sc: Likewise.
>         * scripttempl/elfm68hc11.sc: Likewise.
>         * scripttempl/elfm68hc12.sc: Likewise.
>         * scripttempl/elfm9s12z.sc: Likewise.
>         * scripttempl/elfmicroblaze.sc: Likewise.
>         * scripttempl/elfxgate.sc: Likewise.
>         * scripttempl/elfxtensa.sc: Likewise.
>         * scripttempl/ft32.sc: Likewise.
>         * scripttempl/iq2000.sc: Likewise.
>         * scripttempl/mep.sc: Likewise.
>         * scripttempl/mmo.sc: Likewise.
>         * scripttempl/pru.sc: Likewise.
>         * scripttempl/v850.sc: Likewise.
>         * scripttempl/v850_rh850.sc: Likewise.
>         * scripttempl/visium.sc: Likewise.
>         * scripttempl/xstormy16.sc: Likewise.
>         * scripttempl/arclinux.sc: Include the text section order file
>         and place code text sections toward the end.
>         * scripttempl/elf64bpf.sc: Likewise.
>         * scripttempl/nds32elf.sc: Likewise.
>         * testsuite/ld-scripts/text-order-1a.d: Remove xfail.
>         * testsuite/ld-scripts/text-order-1b.d: Likewise.
>         * testsuite/ld-scripts/text-order-1c.d: Likewise.
> ---
>  ld/scripttempl/arclinux.sc              | 5 +++--
>  ld/scripttempl/avr.sc                   | 1 +
>  ld/scripttempl/dlx.sc                   | 1 +
>  ld/scripttempl/elf32cr16.sc             | 1 +
>  ld/scripttempl/elf32crx.sc              | 1 +
>  ld/scripttempl/elf32msp430.sc           | 1 +
>  ld/scripttempl/elf64bpf.sc              | 5 +++--
>  ld/scripttempl/elf64hppa.sc             | 1 +
>  ld/scripttempl/elfarc.sc                | 1 +
>  ld/scripttempl/elfarcv2.sc              | 1 +
>  ld/scripttempl/elfd10v.sc               | 1 +
>  ld/scripttempl/elfd30v.sc               | 1 +
>  ld/scripttempl/elfm68hc11.sc            | 1 +
>  ld/scripttempl/elfm68hc12.sc            | 1 +
>  ld/scripttempl/elfm9s12z.sc             | 1 +
>  ld/scripttempl/elfmicroblaze.sc         | 1 +
>  ld/scripttempl/elfxgate.sc              | 1 +
>  ld/scripttempl/elfxtensa.sc             | 1 +
>  ld/scripttempl/ft32.sc                  | 1 +
>  ld/scripttempl/iq2000.sc                | 1 +
>  ld/scripttempl/mep.sc                   | 1 +
>  ld/scripttempl/mmo.sc                   | 1 +
>  ld/scripttempl/nds32elf.sc              | 5 +++--
>  ld/scripttempl/pru.sc                   | 1 +
>  ld/scripttempl/v850.sc                  | 1 +
>  ld/scripttempl/v850_rh850.sc            | 1 +
>  ld/scripttempl/visium.sc                | 1 +
>  ld/scripttempl/xstormy16.sc             | 1 +
>  ld/testsuite/ld-scripts/text-order-1a.d | 2 --
>  ld/testsuite/ld-scripts/text-order-1b.d | 2 --
>  ld/testsuite/ld-scripts/text-order-1c.d | 2 --
>  31 files changed, 34 insertions(+), 12 deletions(-)
>
> diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc
> index 36ba5a664d3..646dc08a9f8 100644
> --- a/ld/scripttempl/arclinux.sc
> +++ b/ld/scripttempl/arclinux.sc
> @@ -487,11 +487,12 @@ cat <<EOF
>    .text         ${RELOCATING-0} :
>    {
>      ${RELOCATING+${TEXT_START_SYMBOLS}}

Why after start symbols? It seems that just decreases the ability
to control alignment.
> -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> -    ${RELOCATING+*(.text.exit .text.exit.*)}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      ${RELOCATING+*(.text.startup .text.startup.*)}
>      ${RELOCATING+*(.text.hot .text.hot.*)}
>      ${RELOCATING+*(SORT(.text.sorted.*))}
> +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> +    ${RELOCATING+*(.text.exit .text.exit.*)}
>      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
>      /* .gnu.warning sections are handled specially by elf.em.  */
>      *(.gnu.warning)
> diff --git a/ld/scripttempl/avr.sc b/ld/scripttempl/avr.sc
> index 3b18897f33c..558b4e22c75 100644
> --- a/ld/scripttempl/avr.sc
> +++ b/ld/scripttempl/avr.sc
> @@ -244,6 +244,7 @@ SECTIONS
>      KEEP (*(.init8))
>      *(.init9)  /* Call main().  */
>      KEEP (*(.init9))}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+. = ALIGN(2);
>      *(.text.*)
> diff --git a/ld/scripttempl/dlx.sc b/ld/scripttempl/dlx.sc
> index 8dcbf300c56..e66c68f064d 100644
> --- a/ld/scripttempl/dlx.sc
> +++ b/ld/scripttempl/dlx.sc
> @@ -22,6 +22,7 @@ SECTIONS
>    .text :
>    {
>      ${RELOCATING+CREATE_OBJECT_SYMBOLS}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+etext = ${DATA_ALIGNMENT};}
>    }
> diff --git a/ld/scripttempl/elf32cr16.sc b/ld/scripttempl/elf32cr16.sc
> index 3e7d92a4f34..2d6cd971bc2 100644
> --- a/ld/scripttempl/elf32cr16.sc
> +++ b/ld/scripttempl/elf32cr16.sc
> @@ -81,6 +81,7 @@ SECTIONS
>    .text :
>    {
>      __TEXT_START = .;
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
>      __TEXT_END = .;
>    }${RELOCATING+ > rom}
> diff --git a/ld/scripttempl/elf32crx.sc b/ld/scripttempl/elf32crx.sc
> index 1b1316676a7..1eb5e752c3a 100644
> --- a/ld/scripttempl/elf32crx.sc
> +++ b/ld/scripttempl/elf32crx.sc
> @@ -77,6 +77,7 @@ SECTIONS
>    .text :
>    {
>      __TEXT_START = .;
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
>      __TEXT_END = .;
>    } > rom
> diff --git a/ld/scripttempl/elf32msp430.sc b/ld/scripttempl/elf32msp430.sc
> index bed0d673238..1fadaca2e27 100644
> --- a/ld/scripttempl/elf32msp430.sc
> +++ b/ld/scripttempl/elf32msp430.sc
> @@ -163,6 +163,7 @@ SECTIONS
>      *(.lower.text.* .lower.text)
>
>      . = ALIGN(2);}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+. = ALIGN(2);
>      *(.text.*)
> diff --git a/ld/scripttempl/elf64bpf.sc b/ld/scripttempl/elf64bpf.sc
> index ca62d7c88e0..93d8592a6a6 100644
> --- a/ld/scripttempl/elf64bpf.sc
> +++ b/ld/scripttempl/elf64bpf.sc
> @@ -508,11 +508,12 @@ cat <<EOF
>    .text         ${RELOCATING-0} :
>    {
>      ${RELOCATING+${TEXT_START_SYMBOLS}}
> -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> -    ${RELOCATING+*(.text.exit .text.exit.*)}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      ${RELOCATING+*(.text.startup .text.startup.*)}
>      ${RELOCATING+*(.text.hot .text.hot.*)}
>      ${RELOCATING+*(SORT(.text.sorted.*))}
> +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> +    ${RELOCATING+*(.text.exit .text.exit.*)}
>      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
>      /* .gnu.warning sections are handled specially by elf.em.  */
>      *(.gnu.warning)
> diff --git a/ld/scripttempl/elf64hppa.sc b/ld/scripttempl/elf64hppa.sc
> index 6bde304dd27..94b458010c5 100644
> --- a/ld/scripttempl/elf64hppa.sc
> +++ b/ld/scripttempl/elf64hppa.sc
> @@ -412,6 +412,7 @@ cat <<EOF
>    .text         ${RELOCATING-0} :
>    {
>      ${RELOCATING+${TEXT_START_SYMBOLS}}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
>      /* .gnu.warning sections are handled specially by elf.em.  */
>      *(.gnu.warning)
> diff --git a/ld/scripttempl/elfarc.sc b/ld/scripttempl/elfarc.sc
> index 1ae0248a900..ec16bf5a184 100644
> --- a/ld/scripttempl/elfarc.sc
> +++ b/ld/scripttempl/elfarc.sc
> @@ -288,6 +288,7 @@ cat <<EOF
>    .text         ${RELOCATING-0} :
>    {
>      ${RELOCATING+${TEXT_START_SYMBOLS}}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
>      /* .gnu.warning sections are handled specially by elf.em.  */
>      *(.gnu.warning)
> diff --git a/ld/scripttempl/elfarcv2.sc b/ld/scripttempl/elfarcv2.sc
> index 3054e4c62f3..30bcbd304ab 100644
> --- a/ld/scripttempl/elfarcv2.sc
> +++ b/ld/scripttempl/elfarcv2.sc
> @@ -192,6 +192,7 @@ SECTIONS
>
>      /* Remaining code.  */
>      ${RELOCATING+ . = ALIGN(4);}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
>      /* .gnu.warning sections are handled specially by elf.em.  */
>      *(.gnu.warning)
> diff --git a/ld/scripttempl/elfd10v.sc b/ld/scripttempl/elfd10v.sc
> index 1ecf4a19092..e42bf3cfa70 100644
> --- a/ld/scripttempl/elfd10v.sc
> +++ b/ld/scripttempl/elfd10v.sc
> @@ -109,6 +109,7 @@ SECTIONS
>      KEEP (*(SORT_NONE(.init.*)))
>      KEEP (*(SORT_NONE(.fini)))
>      KEEP (*(SORT_NONE(.fini.*)))}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      /* .gnu.warning sections are handled specially by elf.em.  */
> diff --git a/ld/scripttempl/elfd30v.sc b/ld/scripttempl/elfd30v.sc
> index ed0c412edc8..2dcbb7522cc 100644
> --- a/ld/scripttempl/elfd30v.sc
> +++ b/ld/scripttempl/elfd30v.sc
> @@ -125,6 +125,7 @@ SECTIONS
>    /* Internal text space or external memory */
>    .text :
>    {
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.gnu.linkonce.t*)
>      KEEP (*(SORT_NONE(.fini)))
> diff --git a/ld/scripttempl/elfm68hc11.sc b/ld/scripttempl/elfm68hc11.sc
> index b122da0e32a..f4393a87b68 100644
> --- a/ld/scripttempl/elfm68hc11.sc
> +++ b/ld/scripttempl/elfm68hc11.sc
> @@ -318,6 +318,7 @@ SECTIONS
>      /* Put startup code at beginning so that _start keeps same address.  */
>      ${RELOCATING+${STARTUP_CODE}}
>
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      /* .gnu.warning sections are handled specially by elf.em.  */
> diff --git a/ld/scripttempl/elfm68hc12.sc b/ld/scripttempl/elfm68hc12.sc
> index 47410a95f46..24ea7ed0302 100644
> --- a/ld/scripttempl/elfm68hc12.sc
> +++ b/ld/scripttempl/elfm68hc12.sc
> @@ -317,6 +317,7 @@ SECTIONS
>      /* Put startup code at beginning so that _start keeps same address.  */
>      ${RELOCATING+${STARTUP_CODE}}
>
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      /* .gnu.warning sections are handled specially by elf.em.  */
> diff --git a/ld/scripttempl/elfm9s12z.sc b/ld/scripttempl/elfm9s12z.sc
> index 271d218835b..2d929e1e3fe 100644
> --- a/ld/scripttempl/elfm9s12z.sc
> +++ b/ld/scripttempl/elfm9s12z.sc
> @@ -303,6 +303,7 @@ SECTIONS
>      /* Put startup code at beginning so that _start keeps same address.  */
>      ${RELOCATING+${STARTUP_CODE}}
>
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      /* .gnu.warning sections are handled specially by elf.em.  */
> diff --git a/ld/scripttempl/elfmicroblaze.sc b/ld/scripttempl/elfmicroblaze.sc
> index 88abe4f10ba..e386da51512 100644
> --- a/ld/scripttempl/elfmicroblaze.sc
> +++ b/ld/scripttempl/elfmicroblaze.sc
> @@ -79,6 +79,7 @@ SECTIONS
>
>    ${RELOCATING+ _ftext  =  .;}
>    .text : {
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      ${RELOCATING+*(.gnu.linkonce.t.*)}
> diff --git a/ld/scripttempl/elfxgate.sc b/ld/scripttempl/elfxgate.sc
> index 4c25bc161f2..71d4855f47e 100644
> --- a/ld/scripttempl/elfxgate.sc
> +++ b/ld/scripttempl/elfxgate.sc
> @@ -317,6 +317,7 @@ SECTIONS
>      /* Put startup code at beginning so that _start keeps same address.  */
>      ${RELOCATING+${STARTUP_CODE}}
>
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      /* .gnu.warning sections are handled specially by elf.em.  */
> diff --git a/ld/scripttempl/elfxtensa.sc b/ld/scripttempl/elfxtensa.sc
> index 231f53b5f5b..abc1d33350e 100644
> --- a/ld/scripttempl/elfxtensa.sc
> +++ b/ld/scripttempl/elfxtensa.sc
> @@ -418,6 +418,7 @@ cat <<EOF
>      ${RELOCATING+${INIT_END}}
>
>      ${RELOCATING+${TEXT_START_SYMBOLS}}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.literal .text .stub${RELOCATING+ .literal.* .text.* .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*})
>      /* .gnu.warning sections are handled specially by elf.em.  */
>      *(.gnu.warning)
> diff --git a/ld/scripttempl/ft32.sc b/ld/scripttempl/ft32.sc
> index e52e75cd924..8c966de3bb1 100644
> --- a/ld/scripttempl/ft32.sc
> +++ b/ld/scripttempl/ft32.sc
> @@ -32,6 +32,7 @@ SECTIONS
>  {
>    .text :
>    {
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text${RELOCATING+*})
>      ${RELOCATING+*(.strings)
>      *(._pm*)
> diff --git a/ld/scripttempl/iq2000.sc b/ld/scripttempl/iq2000.sc
> index e85aa2303ed..94e834aef15 100644
> --- a/ld/scripttempl/iq2000.sc
> +++ b/ld/scripttempl/iq2000.sc
> @@ -297,6 +297,7 @@ cat <<EOF
>    .text    ${RELOCATING-0} :
>    {
>      ${RELOCATING+${TEXT_START_SYMBOLS}}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      *(.stub)
> diff --git a/ld/scripttempl/mep.sc b/ld/scripttempl/mep.sc
> index 39e4d64857c..656b1f64386 100644
> --- a/ld/scripttempl/mep.sc
> +++ b/ld/scripttempl/mep.sc
> @@ -310,6 +310,7 @@ cat <<EOF
>    .text         ${RELOCATING-0} :
>    {
>      ${RELOCATING+${TEXT_START_SYMBOLS}}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
>      /* .gnu.warning sections are handled specially by elf.em.  */
>      *(.gnu.warning)
> diff --git a/ld/scripttempl/mmo.sc b/ld/scripttempl/mmo.sc
> index e87a758eb90..ac6a57eee2a 100644
> --- a/ld/scripttempl/mmo.sc
> +++ b/ld/scripttempl/mmo.sc
> @@ -30,6 +30,7 @@ SECTIONS
>      ${RELOCATING+ KEEP (*(SORT_NONE(.init)))}
>      ${RELOCATING+ PROVIDE (_init_end = .);}
>
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      ${RELOCATING+*(.gnu.linkonce.t*)}
> diff --git a/ld/scripttempl/nds32elf.sc b/ld/scripttempl/nds32elf.sc
> index 0f8366d91ac..2626e773a4e 100644
> --- a/ld/scripttempl/nds32elf.sc
> +++ b/ld/scripttempl/nds32elf.sc
> @@ -434,11 +434,12 @@ cat <<EOF
>    .text         ${RELOCATING-0} :
>    {
>      ${RELOCATING+${TEXT_START_SYMBOLS}}
> -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> -    ${RELOCATING+*(.text.exit .text.exit.*)}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      ${RELOCATING+*(.text.startup .text.startup.*)}
>      ${RELOCATING+*(.text.hot .text.hot.*)}
>      ${RELOCATING+*(SORT(.text.sorted.*))}
> +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> +    ${RELOCATING+*(.text.exit .text.exit.*)}
>      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
>      /* .gnu.warning sections are handled specially by elf.em.  */
>      *(.gnu.warning)
> diff --git a/ld/scripttempl/pru.sc b/ld/scripttempl/pru.sc
> index 3ff86bc61c7..d4d79cbcf82 100644
> --- a/ld/scripttempl/pru.sc
> +++ b/ld/scripttempl/pru.sc
> @@ -110,6 +110,7 @@ SECTIONS
>      ${RELOCATING+KEEP (*(.init0))}
>
>      ${RELOCATING+. = ALIGN(4);}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+. = ALIGN(4);}
>      ${RELOCATING+*(.text.*)}
> diff --git a/ld/scripttempl/v850.sc b/ld/scripttempl/v850.sc
> index 10d1da8b5bb..530b4ee72ca 100644
> --- a/ld/scripttempl/v850.sc
> +++ b/ld/scripttempl/v850.sc
> @@ -76,6 +76,7 @@ SECTIONS
>
>    .text                :
>    {
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>
> diff --git a/ld/scripttempl/v850_rh850.sc b/ld/scripttempl/v850_rh850.sc
> index e63e7db8884..3d2874267a9 100644
> --- a/ld/scripttempl/v850_rh850.sc
> +++ b/ld/scripttempl/v850_rh850.sc
> @@ -80,6 +80,7 @@ SECTIONS
>
>    .text                :
>    {
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>
> diff --git a/ld/scripttempl/visium.sc b/ld/scripttempl/visium.sc
> index 0b90b888136..8d97bbf4a68 100644
> --- a/ld/scripttempl/visium.sc
> +++ b/ld/scripttempl/visium.sc
> @@ -67,6 +67,7 @@ SECTIONS
>
>    .text ${RELOCATING-0} : {
>      ${RELOCATING+ _ftext  =  .;}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      ${RELOCATING+*(.gnu.linkonce.t.*)}
> diff --git a/ld/scripttempl/xstormy16.sc b/ld/scripttempl/xstormy16.sc
> index c4117fcde3e..3de1bfa0d18 100644
> --- a/ld/scripttempl/xstormy16.sc
> +++ b/ld/scripttempl/xstormy16.sc
> @@ -179,6 +179,7 @@ SECTIONS
>    .text    ${RELOCATING-0} :
>    {
>      ${RELOCATING+${TEXT_START_SYMBOLS}}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
>      *(.text)
>      ${RELOCATING+*(.text.*)}
>      *(.stub)
> diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
> index 87cee75055f..d6f976f42ad 100644
> --- a/ld/testsuite/ld-scripts/text-order-1a.d
> +++ b/ld/testsuite/ld-scripts/text-order-1a.d
> @@ -4,8 +4,6 @@
>  #ld: --text-section-ordering-file text-order-1a.t
>  #nm: -n
>  #target: [is_elf_format]
> -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> -# generic linker targets don't support --text-section-ordering-file
>
>  #...
>  [0-9a-f]+ T yyy
> diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
> index 3f83813da2b..3d6f4fd8d66 100644
> --- a/ld/testsuite/ld-scripts/text-order-1b.d
> +++ b/ld/testsuite/ld-scripts/text-order-1b.d
> @@ -4,8 +4,6 @@
>  #ld: --text-section-ordering-file text-order-1b.t
>  #nm: -n
>  #target: [is_elf_format]
> -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> -# generic linker targets don't support --text-section-ordering-file
>
>  #...
>  [0-9a-f]+ T yyy
> diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
> index 0c484637784..4166d634606 100644
> --- a/ld/testsuite/ld-scripts/text-order-1c.d
> +++ b/ld/testsuite/ld-scripts/text-order-1c.d
> @@ -4,6 +4,4 @@
>  #ld: --text-section-ordering-file text-order-1c.t
>  #nm: -n
>  #target: [is_elf_format]
> -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> -# generic linker targets don't support --text-section-ordering-file
>  #error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts
  2024-01-19 20:00   ` Noah Goldstein
@ 2024-01-19 20:18     ` H.J. Lu
  2024-01-19 20:38       ` Noah Goldstein
  0 siblings, 1 reply; 19+ messages in thread
From: H.J. Lu @ 2024-01-19 20:18 UTC (permalink / raw)
  To: Noah Goldstein; +Cc: binutils

On Fri, Jan 19, 2024 at 12:01 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
>
> On Fri, Jan 19, 2024 at 7:45 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > Include the text section order file in all ELF linker scripts and enable
> > --text-section-ordering-file tests for all ELF targets.
> >
> >         * scripttempl/avr.sc: Include the text section order file.
> >         * scripttempl/dlx.sc: Likewise.
> >         * scripttempl/elf32cr16.sc: Likewise.
> >         * scripttempl/elf32crx.sc: Likewise.
> >         * scripttempl/elf32msp430.sc: Likewise.
> >         * scripttempl/elf64bpf.sc: Likewise.
> >         * scripttempl/elf64hppa.sc: Likewise.
> >         * scripttempl/elfarc.sc: Likewise.
> >         * scripttempl/elfarcv2.sc: Likewise.
> >         * scripttempl/elfd10v.sc: Likewise.
> >         * scripttempl/elfd30v.sc: Likewise.
> >         * scripttempl/elfm68hc11.sc: Likewise.
> >         * scripttempl/elfm68hc12.sc: Likewise.
> >         * scripttempl/elfm9s12z.sc: Likewise.
> >         * scripttempl/elfmicroblaze.sc: Likewise.
> >         * scripttempl/elfxgate.sc: Likewise.
> >         * scripttempl/elfxtensa.sc: Likewise.
> >         * scripttempl/ft32.sc: Likewise.
> >         * scripttempl/iq2000.sc: Likewise.
> >         * scripttempl/mep.sc: Likewise.
> >         * scripttempl/mmo.sc: Likewise.
> >         * scripttempl/pru.sc: Likewise.
> >         * scripttempl/v850.sc: Likewise.
> >         * scripttempl/v850_rh850.sc: Likewise.
> >         * scripttempl/visium.sc: Likewise.
> >         * scripttempl/xstormy16.sc: Likewise.
> >         * scripttempl/arclinux.sc: Include the text section order file
> >         and place code text sections toward the end.
> >         * scripttempl/elf64bpf.sc: Likewise.
> >         * scripttempl/nds32elf.sc: Likewise.
> >         * testsuite/ld-scripts/text-order-1a.d: Remove xfail.
> >         * testsuite/ld-scripts/text-order-1b.d: Likewise.
> >         * testsuite/ld-scripts/text-order-1c.d: Likewise.
> > ---
> >  ld/scripttempl/arclinux.sc              | 5 +++--
> >  ld/scripttempl/avr.sc                   | 1 +
> >  ld/scripttempl/dlx.sc                   | 1 +
> >  ld/scripttempl/elf32cr16.sc             | 1 +
> >  ld/scripttempl/elf32crx.sc              | 1 +
> >  ld/scripttempl/elf32msp430.sc           | 1 +
> >  ld/scripttempl/elf64bpf.sc              | 5 +++--
> >  ld/scripttempl/elf64hppa.sc             | 1 +
> >  ld/scripttempl/elfarc.sc                | 1 +
> >  ld/scripttempl/elfarcv2.sc              | 1 +
> >  ld/scripttempl/elfd10v.sc               | 1 +
> >  ld/scripttempl/elfd30v.sc               | 1 +
> >  ld/scripttempl/elfm68hc11.sc            | 1 +
> >  ld/scripttempl/elfm68hc12.sc            | 1 +
> >  ld/scripttempl/elfm9s12z.sc             | 1 +
> >  ld/scripttempl/elfmicroblaze.sc         | 1 +
> >  ld/scripttempl/elfxgate.sc              | 1 +
> >  ld/scripttempl/elfxtensa.sc             | 1 +
> >  ld/scripttempl/ft32.sc                  | 1 +
> >  ld/scripttempl/iq2000.sc                | 1 +
> >  ld/scripttempl/mep.sc                   | 1 +
> >  ld/scripttempl/mmo.sc                   | 1 +
> >  ld/scripttempl/nds32elf.sc              | 5 +++--
> >  ld/scripttempl/pru.sc                   | 1 +
> >  ld/scripttempl/v850.sc                  | 1 +
> >  ld/scripttempl/v850_rh850.sc            | 1 +
> >  ld/scripttempl/visium.sc                | 1 +
> >  ld/scripttempl/xstormy16.sc             | 1 +
> >  ld/testsuite/ld-scripts/text-order-1a.d | 2 --
> >  ld/testsuite/ld-scripts/text-order-1b.d | 2 --
> >  ld/testsuite/ld-scripts/text-order-1c.d | 2 --
> >  31 files changed, 34 insertions(+), 12 deletions(-)
> >
> > diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc
> > index 36ba5a664d3..646dc08a9f8 100644
> > --- a/ld/scripttempl/arclinux.sc
> > +++ b/ld/scripttempl/arclinux.sc
> > @@ -487,11 +487,12 @@ cat <<EOF
> >    .text         ${RELOCATING-0} :
> >    {
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
>
> Why after start symbols? It seems that just decreases the ability
> to control alignment.

Why?

> > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      ${RELOCATING+*(.text.startup .text.startup.*)}
> >      ${RELOCATING+*(.text.hot .text.hot.*)}
> >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> >      *(.gnu.warning)
> > diff --git a/ld/scripttempl/avr.sc b/ld/scripttempl/avr.sc
> > index 3b18897f33c..558b4e22c75 100644
> > --- a/ld/scripttempl/avr.sc
> > +++ b/ld/scripttempl/avr.sc
> > @@ -244,6 +244,7 @@ SECTIONS
> >      KEEP (*(.init8))
> >      *(.init9)  /* Call main().  */
> >      KEEP (*(.init9))}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+. = ALIGN(2);
> >      *(.text.*)
> > diff --git a/ld/scripttempl/dlx.sc b/ld/scripttempl/dlx.sc
> > index 8dcbf300c56..e66c68f064d 100644
> > --- a/ld/scripttempl/dlx.sc
> > +++ b/ld/scripttempl/dlx.sc
> > @@ -22,6 +22,7 @@ SECTIONS
> >    .text :
> >    {
> >      ${RELOCATING+CREATE_OBJECT_SYMBOLS}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+etext = ${DATA_ALIGNMENT};}
> >    }
> > diff --git a/ld/scripttempl/elf32cr16.sc b/ld/scripttempl/elf32cr16.sc
> > index 3e7d92a4f34..2d6cd971bc2 100644
> > --- a/ld/scripttempl/elf32cr16.sc
> > +++ b/ld/scripttempl/elf32cr16.sc
> > @@ -81,6 +81,7 @@ SECTIONS
> >    .text :
> >    {
> >      __TEXT_START = .;
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
> >      __TEXT_END = .;
> >    }${RELOCATING+ > rom}
> > diff --git a/ld/scripttempl/elf32crx.sc b/ld/scripttempl/elf32crx.sc
> > index 1b1316676a7..1eb5e752c3a 100644
> > --- a/ld/scripttempl/elf32crx.sc
> > +++ b/ld/scripttempl/elf32crx.sc
> > @@ -77,6 +77,7 @@ SECTIONS
> >    .text :
> >    {
> >      __TEXT_START = .;
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
> >      __TEXT_END = .;
> >    } > rom
> > diff --git a/ld/scripttempl/elf32msp430.sc b/ld/scripttempl/elf32msp430.sc
> > index bed0d673238..1fadaca2e27 100644
> > --- a/ld/scripttempl/elf32msp430.sc
> > +++ b/ld/scripttempl/elf32msp430.sc
> > @@ -163,6 +163,7 @@ SECTIONS
> >      *(.lower.text.* .lower.text)
> >
> >      . = ALIGN(2);}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+. = ALIGN(2);
> >      *(.text.*)
> > diff --git a/ld/scripttempl/elf64bpf.sc b/ld/scripttempl/elf64bpf.sc
> > index ca62d7c88e0..93d8592a6a6 100644
> > --- a/ld/scripttempl/elf64bpf.sc
> > +++ b/ld/scripttempl/elf64bpf.sc
> > @@ -508,11 +508,12 @@ cat <<EOF
> >    .text         ${RELOCATING-0} :
> >    {
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      ${RELOCATING+*(.text.startup .text.startup.*)}
> >      ${RELOCATING+*(.text.hot .text.hot.*)}
> >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> >      *(.gnu.warning)
> > diff --git a/ld/scripttempl/elf64hppa.sc b/ld/scripttempl/elf64hppa.sc
> > index 6bde304dd27..94b458010c5 100644
> > --- a/ld/scripttempl/elf64hppa.sc
> > +++ b/ld/scripttempl/elf64hppa.sc
> > @@ -412,6 +412,7 @@ cat <<EOF
> >    .text         ${RELOCATING-0} :
> >    {
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> >      *(.gnu.warning)
> > diff --git a/ld/scripttempl/elfarc.sc b/ld/scripttempl/elfarc.sc
> > index 1ae0248a900..ec16bf5a184 100644
> > --- a/ld/scripttempl/elfarc.sc
> > +++ b/ld/scripttempl/elfarc.sc
> > @@ -288,6 +288,7 @@ cat <<EOF
> >    .text         ${RELOCATING-0} :
> >    {
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> >      *(.gnu.warning)
> > diff --git a/ld/scripttempl/elfarcv2.sc b/ld/scripttempl/elfarcv2.sc
> > index 3054e4c62f3..30bcbd304ab 100644
> > --- a/ld/scripttempl/elfarcv2.sc
> > +++ b/ld/scripttempl/elfarcv2.sc
> > @@ -192,6 +192,7 @@ SECTIONS
> >
> >      /* Remaining code.  */
> >      ${RELOCATING+ . = ALIGN(4);}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> >      *(.gnu.warning)
> > diff --git a/ld/scripttempl/elfd10v.sc b/ld/scripttempl/elfd10v.sc
> > index 1ecf4a19092..e42bf3cfa70 100644
> > --- a/ld/scripttempl/elfd10v.sc
> > +++ b/ld/scripttempl/elfd10v.sc
> > @@ -109,6 +109,7 @@ SECTIONS
> >      KEEP (*(SORT_NONE(.init.*)))
> >      KEEP (*(SORT_NONE(.fini)))
> >      KEEP (*(SORT_NONE(.fini.*)))}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> > diff --git a/ld/scripttempl/elfd30v.sc b/ld/scripttempl/elfd30v.sc
> > index ed0c412edc8..2dcbb7522cc 100644
> > --- a/ld/scripttempl/elfd30v.sc
> > +++ b/ld/scripttempl/elfd30v.sc
> > @@ -125,6 +125,7 @@ SECTIONS
> >    /* Internal text space or external memory */
> >    .text :
> >    {
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.gnu.linkonce.t*)
> >      KEEP (*(SORT_NONE(.fini)))
> > diff --git a/ld/scripttempl/elfm68hc11.sc b/ld/scripttempl/elfm68hc11.sc
> > index b122da0e32a..f4393a87b68 100644
> > --- a/ld/scripttempl/elfm68hc11.sc
> > +++ b/ld/scripttempl/elfm68hc11.sc
> > @@ -318,6 +318,7 @@ SECTIONS
> >      /* Put startup code at beginning so that _start keeps same address.  */
> >      ${RELOCATING+${STARTUP_CODE}}
> >
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> > diff --git a/ld/scripttempl/elfm68hc12.sc b/ld/scripttempl/elfm68hc12.sc
> > index 47410a95f46..24ea7ed0302 100644
> > --- a/ld/scripttempl/elfm68hc12.sc
> > +++ b/ld/scripttempl/elfm68hc12.sc
> > @@ -317,6 +317,7 @@ SECTIONS
> >      /* Put startup code at beginning so that _start keeps same address.  */
> >      ${RELOCATING+${STARTUP_CODE}}
> >
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> > diff --git a/ld/scripttempl/elfm9s12z.sc b/ld/scripttempl/elfm9s12z.sc
> > index 271d218835b..2d929e1e3fe 100644
> > --- a/ld/scripttempl/elfm9s12z.sc
> > +++ b/ld/scripttempl/elfm9s12z.sc
> > @@ -303,6 +303,7 @@ SECTIONS
> >      /* Put startup code at beginning so that _start keeps same address.  */
> >      ${RELOCATING+${STARTUP_CODE}}
> >
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> > diff --git a/ld/scripttempl/elfmicroblaze.sc b/ld/scripttempl/elfmicroblaze.sc
> > index 88abe4f10ba..e386da51512 100644
> > --- a/ld/scripttempl/elfmicroblaze.sc
> > +++ b/ld/scripttempl/elfmicroblaze.sc
> > @@ -79,6 +79,7 @@ SECTIONS
> >
> >    ${RELOCATING+ _ftext  =  .;}
> >    .text : {
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      ${RELOCATING+*(.gnu.linkonce.t.*)}
> > diff --git a/ld/scripttempl/elfxgate.sc b/ld/scripttempl/elfxgate.sc
> > index 4c25bc161f2..71d4855f47e 100644
> > --- a/ld/scripttempl/elfxgate.sc
> > +++ b/ld/scripttempl/elfxgate.sc
> > @@ -317,6 +317,7 @@ SECTIONS
> >      /* Put startup code at beginning so that _start keeps same address.  */
> >      ${RELOCATING+${STARTUP_CODE}}
> >
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> > diff --git a/ld/scripttempl/elfxtensa.sc b/ld/scripttempl/elfxtensa.sc
> > index 231f53b5f5b..abc1d33350e 100644
> > --- a/ld/scripttempl/elfxtensa.sc
> > +++ b/ld/scripttempl/elfxtensa.sc
> > @@ -418,6 +418,7 @@ cat <<EOF
> >      ${RELOCATING+${INIT_END}}
> >
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.literal .text .stub${RELOCATING+ .literal.* .text.* .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*})
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> >      *(.gnu.warning)
> > diff --git a/ld/scripttempl/ft32.sc b/ld/scripttempl/ft32.sc
> > index e52e75cd924..8c966de3bb1 100644
> > --- a/ld/scripttempl/ft32.sc
> > +++ b/ld/scripttempl/ft32.sc
> > @@ -32,6 +32,7 @@ SECTIONS
> >  {
> >    .text :
> >    {
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text${RELOCATING+*})
> >      ${RELOCATING+*(.strings)
> >      *(._pm*)
> > diff --git a/ld/scripttempl/iq2000.sc b/ld/scripttempl/iq2000.sc
> > index e85aa2303ed..94e834aef15 100644
> > --- a/ld/scripttempl/iq2000.sc
> > +++ b/ld/scripttempl/iq2000.sc
> > @@ -297,6 +297,7 @@ cat <<EOF
> >    .text    ${RELOCATING-0} :
> >    {
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      *(.stub)
> > diff --git a/ld/scripttempl/mep.sc b/ld/scripttempl/mep.sc
> > index 39e4d64857c..656b1f64386 100644
> > --- a/ld/scripttempl/mep.sc
> > +++ b/ld/scripttempl/mep.sc
> > @@ -310,6 +310,7 @@ cat <<EOF
> >    .text         ${RELOCATING-0} :
> >    {
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> >      *(.gnu.warning)
> > diff --git a/ld/scripttempl/mmo.sc b/ld/scripttempl/mmo.sc
> > index e87a758eb90..ac6a57eee2a 100644
> > --- a/ld/scripttempl/mmo.sc
> > +++ b/ld/scripttempl/mmo.sc
> > @@ -30,6 +30,7 @@ SECTIONS
> >      ${RELOCATING+ KEEP (*(SORT_NONE(.init)))}
> >      ${RELOCATING+ PROVIDE (_init_end = .);}
> >
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      ${RELOCATING+*(.gnu.linkonce.t*)}
> > diff --git a/ld/scripttempl/nds32elf.sc b/ld/scripttempl/nds32elf.sc
> > index 0f8366d91ac..2626e773a4e 100644
> > --- a/ld/scripttempl/nds32elf.sc
> > +++ b/ld/scripttempl/nds32elf.sc
> > @@ -434,11 +434,12 @@ cat <<EOF
> >    .text         ${RELOCATING-0} :
> >    {
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      ${RELOCATING+*(.text.startup .text.startup.*)}
> >      ${RELOCATING+*(.text.hot .text.hot.*)}
> >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> >      *(.gnu.warning)
> > diff --git a/ld/scripttempl/pru.sc b/ld/scripttempl/pru.sc
> > index 3ff86bc61c7..d4d79cbcf82 100644
> > --- a/ld/scripttempl/pru.sc
> > +++ b/ld/scripttempl/pru.sc
> > @@ -110,6 +110,7 @@ SECTIONS
> >      ${RELOCATING+KEEP (*(.init0))}
> >
> >      ${RELOCATING+. = ALIGN(4);}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+. = ALIGN(4);}
> >      ${RELOCATING+*(.text.*)}
> > diff --git a/ld/scripttempl/v850.sc b/ld/scripttempl/v850.sc
> > index 10d1da8b5bb..530b4ee72ca 100644
> > --- a/ld/scripttempl/v850.sc
> > +++ b/ld/scripttempl/v850.sc
> > @@ -76,6 +76,7 @@ SECTIONS
> >
> >    .text                :
> >    {
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >
> > diff --git a/ld/scripttempl/v850_rh850.sc b/ld/scripttempl/v850_rh850.sc
> > index e63e7db8884..3d2874267a9 100644
> > --- a/ld/scripttempl/v850_rh850.sc
> > +++ b/ld/scripttempl/v850_rh850.sc
> > @@ -80,6 +80,7 @@ SECTIONS
> >
> >    .text                :
> >    {
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >
> > diff --git a/ld/scripttempl/visium.sc b/ld/scripttempl/visium.sc
> > index 0b90b888136..8d97bbf4a68 100644
> > --- a/ld/scripttempl/visium.sc
> > +++ b/ld/scripttempl/visium.sc
> > @@ -67,6 +67,7 @@ SECTIONS
> >
> >    .text ${RELOCATING-0} : {
> >      ${RELOCATING+ _ftext  =  .;}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      ${RELOCATING+*(.gnu.linkonce.t.*)}
> > diff --git a/ld/scripttempl/xstormy16.sc b/ld/scripttempl/xstormy16.sc
> > index c4117fcde3e..3de1bfa0d18 100644
> > --- a/ld/scripttempl/xstormy16.sc
> > +++ b/ld/scripttempl/xstormy16.sc
> > @@ -179,6 +179,7 @@ SECTIONS
> >    .text    ${RELOCATING-0} :
> >    {
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> >      *(.text)
> >      ${RELOCATING+*(.text.*)}
> >      *(.stub)
> > diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
> > index 87cee75055f..d6f976f42ad 100644
> > --- a/ld/testsuite/ld-scripts/text-order-1a.d
> > +++ b/ld/testsuite/ld-scripts/text-order-1a.d
> > @@ -4,8 +4,6 @@
> >  #ld: --text-section-ordering-file text-order-1a.t
> >  #nm: -n
> >  #target: [is_elf_format]
> > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > -# generic linker targets don't support --text-section-ordering-file
> >
> >  #...
> >  [0-9a-f]+ T yyy
> > diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
> > index 3f83813da2b..3d6f4fd8d66 100644
> > --- a/ld/testsuite/ld-scripts/text-order-1b.d
> > +++ b/ld/testsuite/ld-scripts/text-order-1b.d
> > @@ -4,8 +4,6 @@
> >  #ld: --text-section-ordering-file text-order-1b.t
> >  #nm: -n
> >  #target: [is_elf_format]
> > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > -# generic linker targets don't support --text-section-ordering-file
> >
> >  #...
> >  [0-9a-f]+ T yyy
> > diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
> > index 0c484637784..4166d634606 100644
> > --- a/ld/testsuite/ld-scripts/text-order-1c.d
> > +++ b/ld/testsuite/ld-scripts/text-order-1c.d
> > @@ -4,6 +4,4 @@
> >  #ld: --text-section-ordering-file text-order-1c.t
> >  #nm: -n
> >  #target: [is_elf_format]
> > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > -# generic linker targets don't support --text-section-ordering-file
> >  #error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
> > --
> > 2.43.0
> >



-- 
H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts
  2024-01-19 20:18     ` H.J. Lu
@ 2024-01-19 20:38       ` Noah Goldstein
  2024-01-19 20:40         ` H.J. Lu
  0 siblings, 1 reply; 19+ messages in thread
From: Noah Goldstein @ 2024-01-19 20:38 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Fri, Jan 19, 2024 at 8:18 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Fri, Jan 19, 2024 at 12:01 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
> >
> > On Fri, Jan 19, 2024 at 7:45 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> > >
> > > Include the text section order file in all ELF linker scripts and enable
> > > --text-section-ordering-file tests for all ELF targets.
> > >
> > >         * scripttempl/avr.sc: Include the text section order file.
> > >         * scripttempl/dlx.sc: Likewise.
> > >         * scripttempl/elf32cr16.sc: Likewise.
> > >         * scripttempl/elf32crx.sc: Likewise.
> > >         * scripttempl/elf32msp430.sc: Likewise.
> > >         * scripttempl/elf64bpf.sc: Likewise.
> > >         * scripttempl/elf64hppa.sc: Likewise.
> > >         * scripttempl/elfarc.sc: Likewise.
> > >         * scripttempl/elfarcv2.sc: Likewise.
> > >         * scripttempl/elfd10v.sc: Likewise.
> > >         * scripttempl/elfd30v.sc: Likewise.
> > >         * scripttempl/elfm68hc11.sc: Likewise.
> > >         * scripttempl/elfm68hc12.sc: Likewise.
> > >         * scripttempl/elfm9s12z.sc: Likewise.
> > >         * scripttempl/elfmicroblaze.sc: Likewise.
> > >         * scripttempl/elfxgate.sc: Likewise.
> > >         * scripttempl/elfxtensa.sc: Likewise.
> > >         * scripttempl/ft32.sc: Likewise.
> > >         * scripttempl/iq2000.sc: Likewise.
> > >         * scripttempl/mep.sc: Likewise.
> > >         * scripttempl/mmo.sc: Likewise.
> > >         * scripttempl/pru.sc: Likewise.
> > >         * scripttempl/v850.sc: Likewise.
> > >         * scripttempl/v850_rh850.sc: Likewise.
> > >         * scripttempl/visium.sc: Likewise.
> > >         * scripttempl/xstormy16.sc: Likewise.
> > >         * scripttempl/arclinux.sc: Include the text section order file
> > >         and place code text sections toward the end.
> > >         * scripttempl/elf64bpf.sc: Likewise.
> > >         * scripttempl/nds32elf.sc: Likewise.
> > >         * testsuite/ld-scripts/text-order-1a.d: Remove xfail.
> > >         * testsuite/ld-scripts/text-order-1b.d: Likewise.
> > >         * testsuite/ld-scripts/text-order-1c.d: Likewise.
> > > ---
> > >  ld/scripttempl/arclinux.sc              | 5 +++--
> > >  ld/scripttempl/avr.sc                   | 1 +
> > >  ld/scripttempl/dlx.sc                   | 1 +
> > >  ld/scripttempl/elf32cr16.sc             | 1 +
> > >  ld/scripttempl/elf32crx.sc              | 1 +
> > >  ld/scripttempl/elf32msp430.sc           | 1 +
> > >  ld/scripttempl/elf64bpf.sc              | 5 +++--
> > >  ld/scripttempl/elf64hppa.sc             | 1 +
> > >  ld/scripttempl/elfarc.sc                | 1 +
> > >  ld/scripttempl/elfarcv2.sc              | 1 +
> > >  ld/scripttempl/elfd10v.sc               | 1 +
> > >  ld/scripttempl/elfd30v.sc               | 1 +
> > >  ld/scripttempl/elfm68hc11.sc            | 1 +
> > >  ld/scripttempl/elfm68hc12.sc            | 1 +
> > >  ld/scripttempl/elfm9s12z.sc             | 1 +
> > >  ld/scripttempl/elfmicroblaze.sc         | 1 +
> > >  ld/scripttempl/elfxgate.sc              | 1 +
> > >  ld/scripttempl/elfxtensa.sc             | 1 +
> > >  ld/scripttempl/ft32.sc                  | 1 +
> > >  ld/scripttempl/iq2000.sc                | 1 +
> > >  ld/scripttempl/mep.sc                   | 1 +
> > >  ld/scripttempl/mmo.sc                   | 1 +
> > >  ld/scripttempl/nds32elf.sc              | 5 +++--
> > >  ld/scripttempl/pru.sc                   | 1 +
> > >  ld/scripttempl/v850.sc                  | 1 +
> > >  ld/scripttempl/v850_rh850.sc            | 1 +
> > >  ld/scripttempl/visium.sc                | 1 +
> > >  ld/scripttempl/xstormy16.sc             | 1 +
> > >  ld/testsuite/ld-scripts/text-order-1a.d | 2 --
> > >  ld/testsuite/ld-scripts/text-order-1b.d | 2 --
> > >  ld/testsuite/ld-scripts/text-order-1c.d | 2 --
> > >  31 files changed, 34 insertions(+), 12 deletions(-)
> > >
> > > diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc
> > > index 36ba5a664d3..646dc08a9f8 100644
> > > --- a/ld/scripttempl/arclinux.sc
> > > +++ b/ld/scripttempl/arclinux.sc
> > > @@ -487,11 +487,12 @@ cat <<EOF
> > >    .text         ${RELOCATING-0} :
> > >    {
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> >
> > Why after start symbols? It seems that just decreases the ability
> > to control alignment.
>
> Why?
won't it add an arbitrary amount of padding (depending on number of
start symbols) before the actual ordering.
>
> > > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > >      ${RELOCATING+*(.text.hot .text.hot.*)}
> > >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > >      *(.gnu.warning)
> > > diff --git a/ld/scripttempl/avr.sc b/ld/scripttempl/avr.sc
> > > index 3b18897f33c..558b4e22c75 100644
> > > --- a/ld/scripttempl/avr.sc
> > > +++ b/ld/scripttempl/avr.sc
> > > @@ -244,6 +244,7 @@ SECTIONS
> > >      KEEP (*(.init8))
> > >      *(.init9)  /* Call main().  */
> > >      KEEP (*(.init9))}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+. = ALIGN(2);
> > >      *(.text.*)
> > > diff --git a/ld/scripttempl/dlx.sc b/ld/scripttempl/dlx.sc
> > > index 8dcbf300c56..e66c68f064d 100644
> > > --- a/ld/scripttempl/dlx.sc
> > > +++ b/ld/scripttempl/dlx.sc
> > > @@ -22,6 +22,7 @@ SECTIONS
> > >    .text :
> > >    {
> > >      ${RELOCATING+CREATE_OBJECT_SYMBOLS}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+etext = ${DATA_ALIGNMENT};}
> > >    }
> > > diff --git a/ld/scripttempl/elf32cr16.sc b/ld/scripttempl/elf32cr16.sc
> > > index 3e7d92a4f34..2d6cd971bc2 100644
> > > --- a/ld/scripttempl/elf32cr16.sc
> > > +++ b/ld/scripttempl/elf32cr16.sc
> > > @@ -81,6 +81,7 @@ SECTIONS
> > >    .text :
> > >    {
> > >      __TEXT_START = .;
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
> > >      __TEXT_END = .;
> > >    }${RELOCATING+ > rom}
> > > diff --git a/ld/scripttempl/elf32crx.sc b/ld/scripttempl/elf32crx.sc
> > > index 1b1316676a7..1eb5e752c3a 100644
> > > --- a/ld/scripttempl/elf32crx.sc
> > > +++ b/ld/scripttempl/elf32crx.sc
> > > @@ -77,6 +77,7 @@ SECTIONS
> > >    .text :
> > >    {
> > >      __TEXT_START = .;
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
> > >      __TEXT_END = .;
> > >    } > rom
> > > diff --git a/ld/scripttempl/elf32msp430.sc b/ld/scripttempl/elf32msp430.sc
> > > index bed0d673238..1fadaca2e27 100644
> > > --- a/ld/scripttempl/elf32msp430.sc
> > > +++ b/ld/scripttempl/elf32msp430.sc
> > > @@ -163,6 +163,7 @@ SECTIONS
> > >      *(.lower.text.* .lower.text)
> > >
> > >      . = ALIGN(2);}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+. = ALIGN(2);
> > >      *(.text.*)
> > > diff --git a/ld/scripttempl/elf64bpf.sc b/ld/scripttempl/elf64bpf.sc
> > > index ca62d7c88e0..93d8592a6a6 100644
> > > --- a/ld/scripttempl/elf64bpf.sc
> > > +++ b/ld/scripttempl/elf64bpf.sc
> > > @@ -508,11 +508,12 @@ cat <<EOF
> > >    .text         ${RELOCATING-0} :
> > >    {
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > >      ${RELOCATING+*(.text.hot .text.hot.*)}
> > >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > >      *(.gnu.warning)
> > > diff --git a/ld/scripttempl/elf64hppa.sc b/ld/scripttempl/elf64hppa.sc
> > > index 6bde304dd27..94b458010c5 100644
> > > --- a/ld/scripttempl/elf64hppa.sc
> > > +++ b/ld/scripttempl/elf64hppa.sc
> > > @@ -412,6 +412,7 @@ cat <<EOF
> > >    .text         ${RELOCATING-0} :
> > >    {
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > >      *(.gnu.warning)
> > > diff --git a/ld/scripttempl/elfarc.sc b/ld/scripttempl/elfarc.sc
> > > index 1ae0248a900..ec16bf5a184 100644
> > > --- a/ld/scripttempl/elfarc.sc
> > > +++ b/ld/scripttempl/elfarc.sc
> > > @@ -288,6 +288,7 @@ cat <<EOF
> > >    .text         ${RELOCATING-0} :
> > >    {
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > >      *(.gnu.warning)
> > > diff --git a/ld/scripttempl/elfarcv2.sc b/ld/scripttempl/elfarcv2.sc
> > > index 3054e4c62f3..30bcbd304ab 100644
> > > --- a/ld/scripttempl/elfarcv2.sc
> > > +++ b/ld/scripttempl/elfarcv2.sc
> > > @@ -192,6 +192,7 @@ SECTIONS
> > >
> > >      /* Remaining code.  */
> > >      ${RELOCATING+ . = ALIGN(4);}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > >      *(.gnu.warning)
> > > diff --git a/ld/scripttempl/elfd10v.sc b/ld/scripttempl/elfd10v.sc
> > > index 1ecf4a19092..e42bf3cfa70 100644
> > > --- a/ld/scripttempl/elfd10v.sc
> > > +++ b/ld/scripttempl/elfd10v.sc
> > > @@ -109,6 +109,7 @@ SECTIONS
> > >      KEEP (*(SORT_NONE(.init.*)))
> > >      KEEP (*(SORT_NONE(.fini)))
> > >      KEEP (*(SORT_NONE(.fini.*)))}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > diff --git a/ld/scripttempl/elfd30v.sc b/ld/scripttempl/elfd30v.sc
> > > index ed0c412edc8..2dcbb7522cc 100644
> > > --- a/ld/scripttempl/elfd30v.sc
> > > +++ b/ld/scripttempl/elfd30v.sc
> > > @@ -125,6 +125,7 @@ SECTIONS
> > >    /* Internal text space or external memory */
> > >    .text :
> > >    {
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.gnu.linkonce.t*)
> > >      KEEP (*(SORT_NONE(.fini)))
> > > diff --git a/ld/scripttempl/elfm68hc11.sc b/ld/scripttempl/elfm68hc11.sc
> > > index b122da0e32a..f4393a87b68 100644
> > > --- a/ld/scripttempl/elfm68hc11.sc
> > > +++ b/ld/scripttempl/elfm68hc11.sc
> > > @@ -318,6 +318,7 @@ SECTIONS
> > >      /* Put startup code at beginning so that _start keeps same address.  */
> > >      ${RELOCATING+${STARTUP_CODE}}
> > >
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > diff --git a/ld/scripttempl/elfm68hc12.sc b/ld/scripttempl/elfm68hc12.sc
> > > index 47410a95f46..24ea7ed0302 100644
> > > --- a/ld/scripttempl/elfm68hc12.sc
> > > +++ b/ld/scripttempl/elfm68hc12.sc
> > > @@ -317,6 +317,7 @@ SECTIONS
> > >      /* Put startup code at beginning so that _start keeps same address.  */
> > >      ${RELOCATING+${STARTUP_CODE}}
> > >
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > diff --git a/ld/scripttempl/elfm9s12z.sc b/ld/scripttempl/elfm9s12z.sc
> > > index 271d218835b..2d929e1e3fe 100644
> > > --- a/ld/scripttempl/elfm9s12z.sc
> > > +++ b/ld/scripttempl/elfm9s12z.sc
> > > @@ -303,6 +303,7 @@ SECTIONS
> > >      /* Put startup code at beginning so that _start keeps same address.  */
> > >      ${RELOCATING+${STARTUP_CODE}}
> > >
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > diff --git a/ld/scripttempl/elfmicroblaze.sc b/ld/scripttempl/elfmicroblaze.sc
> > > index 88abe4f10ba..e386da51512 100644
> > > --- a/ld/scripttempl/elfmicroblaze.sc
> > > +++ b/ld/scripttempl/elfmicroblaze.sc
> > > @@ -79,6 +79,7 @@ SECTIONS
> > >
> > >    ${RELOCATING+ _ftext  =  .;}
> > >    .text : {
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      ${RELOCATING+*(.gnu.linkonce.t.*)}
> > > diff --git a/ld/scripttempl/elfxgate.sc b/ld/scripttempl/elfxgate.sc
> > > index 4c25bc161f2..71d4855f47e 100644
> > > --- a/ld/scripttempl/elfxgate.sc
> > > +++ b/ld/scripttempl/elfxgate.sc
> > > @@ -317,6 +317,7 @@ SECTIONS
> > >      /* Put startup code at beginning so that _start keeps same address.  */
> > >      ${RELOCATING+${STARTUP_CODE}}
> > >
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > diff --git a/ld/scripttempl/elfxtensa.sc b/ld/scripttempl/elfxtensa.sc
> > > index 231f53b5f5b..abc1d33350e 100644
> > > --- a/ld/scripttempl/elfxtensa.sc
> > > +++ b/ld/scripttempl/elfxtensa.sc
> > > @@ -418,6 +418,7 @@ cat <<EOF
> > >      ${RELOCATING+${INIT_END}}
> > >
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.literal .text .stub${RELOCATING+ .literal.* .text.* .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*})
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > >      *(.gnu.warning)
> > > diff --git a/ld/scripttempl/ft32.sc b/ld/scripttempl/ft32.sc
> > > index e52e75cd924..8c966de3bb1 100644
> > > --- a/ld/scripttempl/ft32.sc
> > > +++ b/ld/scripttempl/ft32.sc
> > > @@ -32,6 +32,7 @@ SECTIONS
> > >  {
> > >    .text :
> > >    {
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text${RELOCATING+*})
> > >      ${RELOCATING+*(.strings)
> > >      *(._pm*)
> > > diff --git a/ld/scripttempl/iq2000.sc b/ld/scripttempl/iq2000.sc
> > > index e85aa2303ed..94e834aef15 100644
> > > --- a/ld/scripttempl/iq2000.sc
> > > +++ b/ld/scripttempl/iq2000.sc
> > > @@ -297,6 +297,7 @@ cat <<EOF
> > >    .text    ${RELOCATING-0} :
> > >    {
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      *(.stub)
> > > diff --git a/ld/scripttempl/mep.sc b/ld/scripttempl/mep.sc
> > > index 39e4d64857c..656b1f64386 100644
> > > --- a/ld/scripttempl/mep.sc
> > > +++ b/ld/scripttempl/mep.sc
> > > @@ -310,6 +310,7 @@ cat <<EOF
> > >    .text         ${RELOCATING-0} :
> > >    {
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > >      *(.gnu.warning)
> > > diff --git a/ld/scripttempl/mmo.sc b/ld/scripttempl/mmo.sc
> > > index e87a758eb90..ac6a57eee2a 100644
> > > --- a/ld/scripttempl/mmo.sc
> > > +++ b/ld/scripttempl/mmo.sc
> > > @@ -30,6 +30,7 @@ SECTIONS
> > >      ${RELOCATING+ KEEP (*(SORT_NONE(.init)))}
> > >      ${RELOCATING+ PROVIDE (_init_end = .);}
> > >
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      ${RELOCATING+*(.gnu.linkonce.t*)}
> > > diff --git a/ld/scripttempl/nds32elf.sc b/ld/scripttempl/nds32elf.sc
> > > index 0f8366d91ac..2626e773a4e 100644
> > > --- a/ld/scripttempl/nds32elf.sc
> > > +++ b/ld/scripttempl/nds32elf.sc
> > > @@ -434,11 +434,12 @@ cat <<EOF
> > >    .text         ${RELOCATING-0} :
> > >    {
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > >      ${RELOCATING+*(.text.hot .text.hot.*)}
> > >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > >      *(.gnu.warning)
> > > diff --git a/ld/scripttempl/pru.sc b/ld/scripttempl/pru.sc
> > > index 3ff86bc61c7..d4d79cbcf82 100644
> > > --- a/ld/scripttempl/pru.sc
> > > +++ b/ld/scripttempl/pru.sc
> > > @@ -110,6 +110,7 @@ SECTIONS
> > >      ${RELOCATING+KEEP (*(.init0))}
> > >
> > >      ${RELOCATING+. = ALIGN(4);}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+. = ALIGN(4);}
> > >      ${RELOCATING+*(.text.*)}
> > > diff --git a/ld/scripttempl/v850.sc b/ld/scripttempl/v850.sc
> > > index 10d1da8b5bb..530b4ee72ca 100644
> > > --- a/ld/scripttempl/v850.sc
> > > +++ b/ld/scripttempl/v850.sc
> > > @@ -76,6 +76,7 @@ SECTIONS
> > >
> > >    .text                :
> > >    {
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >
> > > diff --git a/ld/scripttempl/v850_rh850.sc b/ld/scripttempl/v850_rh850.sc
> > > index e63e7db8884..3d2874267a9 100644
> > > --- a/ld/scripttempl/v850_rh850.sc
> > > +++ b/ld/scripttempl/v850_rh850.sc
> > > @@ -80,6 +80,7 @@ SECTIONS
> > >
> > >    .text                :
> > >    {
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >
> > > diff --git a/ld/scripttempl/visium.sc b/ld/scripttempl/visium.sc
> > > index 0b90b888136..8d97bbf4a68 100644
> > > --- a/ld/scripttempl/visium.sc
> > > +++ b/ld/scripttempl/visium.sc
> > > @@ -67,6 +67,7 @@ SECTIONS
> > >
> > >    .text ${RELOCATING-0} : {
> > >      ${RELOCATING+ _ftext  =  .;}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      ${RELOCATING+*(.gnu.linkonce.t.*)}
> > > diff --git a/ld/scripttempl/xstormy16.sc b/ld/scripttempl/xstormy16.sc
> > > index c4117fcde3e..3de1bfa0d18 100644
> > > --- a/ld/scripttempl/xstormy16.sc
> > > +++ b/ld/scripttempl/xstormy16.sc
> > > @@ -179,6 +179,7 @@ SECTIONS
> > >    .text    ${RELOCATING-0} :
> > >    {
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > >      *(.text)
> > >      ${RELOCATING+*(.text.*)}
> > >      *(.stub)
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
> > > index 87cee75055f..d6f976f42ad 100644
> > > --- a/ld/testsuite/ld-scripts/text-order-1a.d
> > > +++ b/ld/testsuite/ld-scripts/text-order-1a.d
> > > @@ -4,8 +4,6 @@
> > >  #ld: --text-section-ordering-file text-order-1a.t
> > >  #nm: -n
> > >  #target: [is_elf_format]
> > > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > -# generic linker targets don't support --text-section-ordering-file
> > >
> > >  #...
> > >  [0-9a-f]+ T yyy
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
> > > index 3f83813da2b..3d6f4fd8d66 100644
> > > --- a/ld/testsuite/ld-scripts/text-order-1b.d
> > > +++ b/ld/testsuite/ld-scripts/text-order-1b.d
> > > @@ -4,8 +4,6 @@
> > >  #ld: --text-section-ordering-file text-order-1b.t
> > >  #nm: -n
> > >  #target: [is_elf_format]
> > > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > -# generic linker targets don't support --text-section-ordering-file
> > >
> > >  #...
> > >  [0-9a-f]+ T yyy
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
> > > index 0c484637784..4166d634606 100644
> > > --- a/ld/testsuite/ld-scripts/text-order-1c.d
> > > +++ b/ld/testsuite/ld-scripts/text-order-1c.d
> > > @@ -4,6 +4,4 @@
> > >  #ld: --text-section-ordering-file text-order-1c.t
> > >  #nm: -n
> > >  #target: [is_elf_format]
> > > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > -# generic linker targets don't support --text-section-ordering-file
> > >  #error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
> > > --
> > > 2.43.0
> > >
>
>
>
> --
> H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts
  2024-01-19 20:38       ` Noah Goldstein
@ 2024-01-19 20:40         ` H.J. Lu
  2024-01-19 21:07           ` Noah Goldstein
  0 siblings, 1 reply; 19+ messages in thread
From: H.J. Lu @ 2024-01-19 20:40 UTC (permalink / raw)
  To: Noah Goldstein; +Cc: binutils

On Fri, Jan 19, 2024 at 12:39 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
>
> On Fri, Jan 19, 2024 at 8:18 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > On Fri, Jan 19, 2024 at 12:01 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
> > >
> > > On Fri, Jan 19, 2024 at 7:45 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> > > >
> > > > Include the text section order file in all ELF linker scripts and enable
> > > > --text-section-ordering-file tests for all ELF targets.
> > > >
> > > >         * scripttempl/avr.sc: Include the text section order file.
> > > >         * scripttempl/dlx.sc: Likewise.
> > > >         * scripttempl/elf32cr16.sc: Likewise.
> > > >         * scripttempl/elf32crx.sc: Likewise.
> > > >         * scripttempl/elf32msp430.sc: Likewise.
> > > >         * scripttempl/elf64bpf.sc: Likewise.
> > > >         * scripttempl/elf64hppa.sc: Likewise.
> > > >         * scripttempl/elfarc.sc: Likewise.
> > > >         * scripttempl/elfarcv2.sc: Likewise.
> > > >         * scripttempl/elfd10v.sc: Likewise.
> > > >         * scripttempl/elfd30v.sc: Likewise.
> > > >         * scripttempl/elfm68hc11.sc: Likewise.
> > > >         * scripttempl/elfm68hc12.sc: Likewise.
> > > >         * scripttempl/elfm9s12z.sc: Likewise.
> > > >         * scripttempl/elfmicroblaze.sc: Likewise.
> > > >         * scripttempl/elfxgate.sc: Likewise.
> > > >         * scripttempl/elfxtensa.sc: Likewise.
> > > >         * scripttempl/ft32.sc: Likewise.
> > > >         * scripttempl/iq2000.sc: Likewise.
> > > >         * scripttempl/mep.sc: Likewise.
> > > >         * scripttempl/mmo.sc: Likewise.
> > > >         * scripttempl/pru.sc: Likewise.
> > > >         * scripttempl/v850.sc: Likewise.
> > > >         * scripttempl/v850_rh850.sc: Likewise.
> > > >         * scripttempl/visium.sc: Likewise.
> > > >         * scripttempl/xstormy16.sc: Likewise.
> > > >         * scripttempl/arclinux.sc: Include the text section order file
> > > >         and place code text sections toward the end.
> > > >         * scripttempl/elf64bpf.sc: Likewise.
> > > >         * scripttempl/nds32elf.sc: Likewise.
> > > >         * testsuite/ld-scripts/text-order-1a.d: Remove xfail.
> > > >         * testsuite/ld-scripts/text-order-1b.d: Likewise.
> > > >         * testsuite/ld-scripts/text-order-1c.d: Likewise.
> > > > ---
> > > >  ld/scripttempl/arclinux.sc              | 5 +++--
> > > >  ld/scripttempl/avr.sc                   | 1 +
> > > >  ld/scripttempl/dlx.sc                   | 1 +
> > > >  ld/scripttempl/elf32cr16.sc             | 1 +
> > > >  ld/scripttempl/elf32crx.sc              | 1 +
> > > >  ld/scripttempl/elf32msp430.sc           | 1 +
> > > >  ld/scripttempl/elf64bpf.sc              | 5 +++--
> > > >  ld/scripttempl/elf64hppa.sc             | 1 +
> > > >  ld/scripttempl/elfarc.sc                | 1 +
> > > >  ld/scripttempl/elfarcv2.sc              | 1 +
> > > >  ld/scripttempl/elfd10v.sc               | 1 +
> > > >  ld/scripttempl/elfd30v.sc               | 1 +
> > > >  ld/scripttempl/elfm68hc11.sc            | 1 +
> > > >  ld/scripttempl/elfm68hc12.sc            | 1 +
> > > >  ld/scripttempl/elfm9s12z.sc             | 1 +
> > > >  ld/scripttempl/elfmicroblaze.sc         | 1 +
> > > >  ld/scripttempl/elfxgate.sc              | 1 +
> > > >  ld/scripttempl/elfxtensa.sc             | 1 +
> > > >  ld/scripttempl/ft32.sc                  | 1 +
> > > >  ld/scripttempl/iq2000.sc                | 1 +
> > > >  ld/scripttempl/mep.sc                   | 1 +
> > > >  ld/scripttempl/mmo.sc                   | 1 +
> > > >  ld/scripttempl/nds32elf.sc              | 5 +++--
> > > >  ld/scripttempl/pru.sc                   | 1 +
> > > >  ld/scripttempl/v850.sc                  | 1 +
> > > >  ld/scripttempl/v850_rh850.sc            | 1 +
> > > >  ld/scripttempl/visium.sc                | 1 +
> > > >  ld/scripttempl/xstormy16.sc             | 1 +
> > > >  ld/testsuite/ld-scripts/text-order-1a.d | 2 --
> > > >  ld/testsuite/ld-scripts/text-order-1b.d | 2 --
> > > >  ld/testsuite/ld-scripts/text-order-1c.d | 2 --
> > > >  31 files changed, 34 insertions(+), 12 deletions(-)
> > > >
> > > > diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc
> > > > index 36ba5a664d3..646dc08a9f8 100644
> > > > --- a/ld/scripttempl/arclinux.sc
> > > > +++ b/ld/scripttempl/arclinux.sc
> > > > @@ -487,11 +487,12 @@ cat <<EOF
> > > >    .text         ${RELOCATING-0} :
> > > >    {
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > >
> > > Why after start symbols? It seems that just decreases the ability
> > > to control alignment.
> >
> > Why?
> won't it add an arbitrary amount of padding (depending on number of
> start symbols) before the actual ordering.

Start symbols have no impact on section layout nor memory size.

> >
> > > > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > > >      ${RELOCATING+*(.text.hot .text.hot.*)}
> > > >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > > > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > >      *(.gnu.warning)
> > > > diff --git a/ld/scripttempl/avr.sc b/ld/scripttempl/avr.sc
> > > > index 3b18897f33c..558b4e22c75 100644
> > > > --- a/ld/scripttempl/avr.sc
> > > > +++ b/ld/scripttempl/avr.sc
> > > > @@ -244,6 +244,7 @@ SECTIONS
> > > >      KEEP (*(.init8))
> > > >      *(.init9)  /* Call main().  */
> > > >      KEEP (*(.init9))}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+. = ALIGN(2);
> > > >      *(.text.*)
> > > > diff --git a/ld/scripttempl/dlx.sc b/ld/scripttempl/dlx.sc
> > > > index 8dcbf300c56..e66c68f064d 100644
> > > > --- a/ld/scripttempl/dlx.sc
> > > > +++ b/ld/scripttempl/dlx.sc
> > > > @@ -22,6 +22,7 @@ SECTIONS
> > > >    .text :
> > > >    {
> > > >      ${RELOCATING+CREATE_OBJECT_SYMBOLS}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+etext = ${DATA_ALIGNMENT};}
> > > >    }
> > > > diff --git a/ld/scripttempl/elf32cr16.sc b/ld/scripttempl/elf32cr16.sc
> > > > index 3e7d92a4f34..2d6cd971bc2 100644
> > > > --- a/ld/scripttempl/elf32cr16.sc
> > > > +++ b/ld/scripttempl/elf32cr16.sc
> > > > @@ -81,6 +81,7 @@ SECTIONS
> > > >    .text :
> > > >    {
> > > >      __TEXT_START = .;
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
> > > >      __TEXT_END = .;
> > > >    }${RELOCATING+ > rom}
> > > > diff --git a/ld/scripttempl/elf32crx.sc b/ld/scripttempl/elf32crx.sc
> > > > index 1b1316676a7..1eb5e752c3a 100644
> > > > --- a/ld/scripttempl/elf32crx.sc
> > > > +++ b/ld/scripttempl/elf32crx.sc
> > > > @@ -77,6 +77,7 @@ SECTIONS
> > > >    .text :
> > > >    {
> > > >      __TEXT_START = .;
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
> > > >      __TEXT_END = .;
> > > >    } > rom
> > > > diff --git a/ld/scripttempl/elf32msp430.sc b/ld/scripttempl/elf32msp430.sc
> > > > index bed0d673238..1fadaca2e27 100644
> > > > --- a/ld/scripttempl/elf32msp430.sc
> > > > +++ b/ld/scripttempl/elf32msp430.sc
> > > > @@ -163,6 +163,7 @@ SECTIONS
> > > >      *(.lower.text.* .lower.text)
> > > >
> > > >      . = ALIGN(2);}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+. = ALIGN(2);
> > > >      *(.text.*)
> > > > diff --git a/ld/scripttempl/elf64bpf.sc b/ld/scripttempl/elf64bpf.sc
> > > > index ca62d7c88e0..93d8592a6a6 100644
> > > > --- a/ld/scripttempl/elf64bpf.sc
> > > > +++ b/ld/scripttempl/elf64bpf.sc
> > > > @@ -508,11 +508,12 @@ cat <<EOF
> > > >    .text         ${RELOCATING-0} :
> > > >    {
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > > >      ${RELOCATING+*(.text.hot .text.hot.*)}
> > > >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > > > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > >      *(.gnu.warning)
> > > > diff --git a/ld/scripttempl/elf64hppa.sc b/ld/scripttempl/elf64hppa.sc
> > > > index 6bde304dd27..94b458010c5 100644
> > > > --- a/ld/scripttempl/elf64hppa.sc
> > > > +++ b/ld/scripttempl/elf64hppa.sc
> > > > @@ -412,6 +412,7 @@ cat <<EOF
> > > >    .text         ${RELOCATING-0} :
> > > >    {
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > >      *(.gnu.warning)
> > > > diff --git a/ld/scripttempl/elfarc.sc b/ld/scripttempl/elfarc.sc
> > > > index 1ae0248a900..ec16bf5a184 100644
> > > > --- a/ld/scripttempl/elfarc.sc
> > > > +++ b/ld/scripttempl/elfarc.sc
> > > > @@ -288,6 +288,7 @@ cat <<EOF
> > > >    .text         ${RELOCATING-0} :
> > > >    {
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > >      *(.gnu.warning)
> > > > diff --git a/ld/scripttempl/elfarcv2.sc b/ld/scripttempl/elfarcv2.sc
> > > > index 3054e4c62f3..30bcbd304ab 100644
> > > > --- a/ld/scripttempl/elfarcv2.sc
> > > > +++ b/ld/scripttempl/elfarcv2.sc
> > > > @@ -192,6 +192,7 @@ SECTIONS
> > > >
> > > >      /* Remaining code.  */
> > > >      ${RELOCATING+ . = ALIGN(4);}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > >      *(.gnu.warning)
> > > > diff --git a/ld/scripttempl/elfd10v.sc b/ld/scripttempl/elfd10v.sc
> > > > index 1ecf4a19092..e42bf3cfa70 100644
> > > > --- a/ld/scripttempl/elfd10v.sc
> > > > +++ b/ld/scripttempl/elfd10v.sc
> > > > @@ -109,6 +109,7 @@ SECTIONS
> > > >      KEEP (*(SORT_NONE(.init.*)))
> > > >      KEEP (*(SORT_NONE(.fini)))
> > > >      KEEP (*(SORT_NONE(.fini.*)))}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > diff --git a/ld/scripttempl/elfd30v.sc b/ld/scripttempl/elfd30v.sc
> > > > index ed0c412edc8..2dcbb7522cc 100644
> > > > --- a/ld/scripttempl/elfd30v.sc
> > > > +++ b/ld/scripttempl/elfd30v.sc
> > > > @@ -125,6 +125,7 @@ SECTIONS
> > > >    /* Internal text space or external memory */
> > > >    .text :
> > > >    {
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.gnu.linkonce.t*)
> > > >      KEEP (*(SORT_NONE(.fini)))
> > > > diff --git a/ld/scripttempl/elfm68hc11.sc b/ld/scripttempl/elfm68hc11.sc
> > > > index b122da0e32a..f4393a87b68 100644
> > > > --- a/ld/scripttempl/elfm68hc11.sc
> > > > +++ b/ld/scripttempl/elfm68hc11.sc
> > > > @@ -318,6 +318,7 @@ SECTIONS
> > > >      /* Put startup code at beginning so that _start keeps same address.  */
> > > >      ${RELOCATING+${STARTUP_CODE}}
> > > >
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > diff --git a/ld/scripttempl/elfm68hc12.sc b/ld/scripttempl/elfm68hc12.sc
> > > > index 47410a95f46..24ea7ed0302 100644
> > > > --- a/ld/scripttempl/elfm68hc12.sc
> > > > +++ b/ld/scripttempl/elfm68hc12.sc
> > > > @@ -317,6 +317,7 @@ SECTIONS
> > > >      /* Put startup code at beginning so that _start keeps same address.  */
> > > >      ${RELOCATING+${STARTUP_CODE}}
> > > >
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > diff --git a/ld/scripttempl/elfm9s12z.sc b/ld/scripttempl/elfm9s12z.sc
> > > > index 271d218835b..2d929e1e3fe 100644
> > > > --- a/ld/scripttempl/elfm9s12z.sc
> > > > +++ b/ld/scripttempl/elfm9s12z.sc
> > > > @@ -303,6 +303,7 @@ SECTIONS
> > > >      /* Put startup code at beginning so that _start keeps same address.  */
> > > >      ${RELOCATING+${STARTUP_CODE}}
> > > >
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > diff --git a/ld/scripttempl/elfmicroblaze.sc b/ld/scripttempl/elfmicroblaze.sc
> > > > index 88abe4f10ba..e386da51512 100644
> > > > --- a/ld/scripttempl/elfmicroblaze.sc
> > > > +++ b/ld/scripttempl/elfmicroblaze.sc
> > > > @@ -79,6 +79,7 @@ SECTIONS
> > > >
> > > >    ${RELOCATING+ _ftext  =  .;}
> > > >    .text : {
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      ${RELOCATING+*(.gnu.linkonce.t.*)}
> > > > diff --git a/ld/scripttempl/elfxgate.sc b/ld/scripttempl/elfxgate.sc
> > > > index 4c25bc161f2..71d4855f47e 100644
> > > > --- a/ld/scripttempl/elfxgate.sc
> > > > +++ b/ld/scripttempl/elfxgate.sc
> > > > @@ -317,6 +317,7 @@ SECTIONS
> > > >      /* Put startup code at beginning so that _start keeps same address.  */
> > > >      ${RELOCATING+${STARTUP_CODE}}
> > > >
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > diff --git a/ld/scripttempl/elfxtensa.sc b/ld/scripttempl/elfxtensa.sc
> > > > index 231f53b5f5b..abc1d33350e 100644
> > > > --- a/ld/scripttempl/elfxtensa.sc
> > > > +++ b/ld/scripttempl/elfxtensa.sc
> > > > @@ -418,6 +418,7 @@ cat <<EOF
> > > >      ${RELOCATING+${INIT_END}}
> > > >
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.literal .text .stub${RELOCATING+ .literal.* .text.* .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*})
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > >      *(.gnu.warning)
> > > > diff --git a/ld/scripttempl/ft32.sc b/ld/scripttempl/ft32.sc
> > > > index e52e75cd924..8c966de3bb1 100644
> > > > --- a/ld/scripttempl/ft32.sc
> > > > +++ b/ld/scripttempl/ft32.sc
> > > > @@ -32,6 +32,7 @@ SECTIONS
> > > >  {
> > > >    .text :
> > > >    {
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text${RELOCATING+*})
> > > >      ${RELOCATING+*(.strings)
> > > >      *(._pm*)
> > > > diff --git a/ld/scripttempl/iq2000.sc b/ld/scripttempl/iq2000.sc
> > > > index e85aa2303ed..94e834aef15 100644
> > > > --- a/ld/scripttempl/iq2000.sc
> > > > +++ b/ld/scripttempl/iq2000.sc
> > > > @@ -297,6 +297,7 @@ cat <<EOF
> > > >    .text    ${RELOCATING-0} :
> > > >    {
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      *(.stub)
> > > > diff --git a/ld/scripttempl/mep.sc b/ld/scripttempl/mep.sc
> > > > index 39e4d64857c..656b1f64386 100644
> > > > --- a/ld/scripttempl/mep.sc
> > > > +++ b/ld/scripttempl/mep.sc
> > > > @@ -310,6 +310,7 @@ cat <<EOF
> > > >    .text         ${RELOCATING-0} :
> > > >    {
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > >      *(.gnu.warning)
> > > > diff --git a/ld/scripttempl/mmo.sc b/ld/scripttempl/mmo.sc
> > > > index e87a758eb90..ac6a57eee2a 100644
> > > > --- a/ld/scripttempl/mmo.sc
> > > > +++ b/ld/scripttempl/mmo.sc
> > > > @@ -30,6 +30,7 @@ SECTIONS
> > > >      ${RELOCATING+ KEEP (*(SORT_NONE(.init)))}
> > > >      ${RELOCATING+ PROVIDE (_init_end = .);}
> > > >
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      ${RELOCATING+*(.gnu.linkonce.t*)}
> > > > diff --git a/ld/scripttempl/nds32elf.sc b/ld/scripttempl/nds32elf.sc
> > > > index 0f8366d91ac..2626e773a4e 100644
> > > > --- a/ld/scripttempl/nds32elf.sc
> > > > +++ b/ld/scripttempl/nds32elf.sc
> > > > @@ -434,11 +434,12 @@ cat <<EOF
> > > >    .text         ${RELOCATING-0} :
> > > >    {
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > > >      ${RELOCATING+*(.text.hot .text.hot.*)}
> > > >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > > > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > >      *(.gnu.warning)
> > > > diff --git a/ld/scripttempl/pru.sc b/ld/scripttempl/pru.sc
> > > > index 3ff86bc61c7..d4d79cbcf82 100644
> > > > --- a/ld/scripttempl/pru.sc
> > > > +++ b/ld/scripttempl/pru.sc
> > > > @@ -110,6 +110,7 @@ SECTIONS
> > > >      ${RELOCATING+KEEP (*(.init0))}
> > > >
> > > >      ${RELOCATING+. = ALIGN(4);}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+. = ALIGN(4);}
> > > >      ${RELOCATING+*(.text.*)}
> > > > diff --git a/ld/scripttempl/v850.sc b/ld/scripttempl/v850.sc
> > > > index 10d1da8b5bb..530b4ee72ca 100644
> > > > --- a/ld/scripttempl/v850.sc
> > > > +++ b/ld/scripttempl/v850.sc
> > > > @@ -76,6 +76,7 @@ SECTIONS
> > > >
> > > >    .text                :
> > > >    {
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >
> > > > diff --git a/ld/scripttempl/v850_rh850.sc b/ld/scripttempl/v850_rh850.sc
> > > > index e63e7db8884..3d2874267a9 100644
> > > > --- a/ld/scripttempl/v850_rh850.sc
> > > > +++ b/ld/scripttempl/v850_rh850.sc
> > > > @@ -80,6 +80,7 @@ SECTIONS
> > > >
> > > >    .text                :
> > > >    {
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >
> > > > diff --git a/ld/scripttempl/visium.sc b/ld/scripttempl/visium.sc
> > > > index 0b90b888136..8d97bbf4a68 100644
> > > > --- a/ld/scripttempl/visium.sc
> > > > +++ b/ld/scripttempl/visium.sc
> > > > @@ -67,6 +67,7 @@ SECTIONS
> > > >
> > > >    .text ${RELOCATING-0} : {
> > > >      ${RELOCATING+ _ftext  =  .;}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      ${RELOCATING+*(.gnu.linkonce.t.*)}
> > > > diff --git a/ld/scripttempl/xstormy16.sc b/ld/scripttempl/xstormy16.sc
> > > > index c4117fcde3e..3de1bfa0d18 100644
> > > > --- a/ld/scripttempl/xstormy16.sc
> > > > +++ b/ld/scripttempl/xstormy16.sc
> > > > @@ -179,6 +179,7 @@ SECTIONS
> > > >    .text    ${RELOCATING-0} :
> > > >    {
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > >      *(.text)
> > > >      ${RELOCATING+*(.text.*)}
> > > >      *(.stub)
> > > > diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
> > > > index 87cee75055f..d6f976f42ad 100644
> > > > --- a/ld/testsuite/ld-scripts/text-order-1a.d
> > > > +++ b/ld/testsuite/ld-scripts/text-order-1a.d
> > > > @@ -4,8 +4,6 @@
> > > >  #ld: --text-section-ordering-file text-order-1a.t
> > > >  #nm: -n
> > > >  #target: [is_elf_format]
> > > > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > > -# generic linker targets don't support --text-section-ordering-file
> > > >
> > > >  #...
> > > >  [0-9a-f]+ T yyy
> > > > diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
> > > > index 3f83813da2b..3d6f4fd8d66 100644
> > > > --- a/ld/testsuite/ld-scripts/text-order-1b.d
> > > > +++ b/ld/testsuite/ld-scripts/text-order-1b.d
> > > > @@ -4,8 +4,6 @@
> > > >  #ld: --text-section-ordering-file text-order-1b.t
> > > >  #nm: -n
> > > >  #target: [is_elf_format]
> > > > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > > -# generic linker targets don't support --text-section-ordering-file
> > > >
> > > >  #...
> > > >  [0-9a-f]+ T yyy
> > > > diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
> > > > index 0c484637784..4166d634606 100644
> > > > --- a/ld/testsuite/ld-scripts/text-order-1c.d
> > > > +++ b/ld/testsuite/ld-scripts/text-order-1c.d
> > > > @@ -4,6 +4,4 @@
> > > >  #ld: --text-section-ordering-file text-order-1c.t
> > > >  #nm: -n
> > > >  #target: [is_elf_format]
> > > > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > > -# generic linker targets don't support --text-section-ordering-file
> > > >  #error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
> > > > --
> > > > 2.43.0
> > > >
> >
> >
> >
> > --
> > H.J.



-- 
H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts
  2024-01-19 20:40         ` H.J. Lu
@ 2024-01-19 21:07           ` Noah Goldstein
  0 siblings, 0 replies; 19+ messages in thread
From: Noah Goldstein @ 2024-01-19 21:07 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Fri, Jan 19, 2024 at 8:41 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Fri, Jan 19, 2024 at 12:39 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
> >
> > On Fri, Jan 19, 2024 at 8:18 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> > >
> > > On Fri, Jan 19, 2024 at 12:01 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
> > > >
> > > > On Fri, Jan 19, 2024 at 7:45 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> > > > >
> > > > > Include the text section order file in all ELF linker scripts and enable
> > > > > --text-section-ordering-file tests for all ELF targets.
> > > > >
> > > > >         * scripttempl/avr.sc: Include the text section order file.
> > > > >         * scripttempl/dlx.sc: Likewise.
> > > > >         * scripttempl/elf32cr16.sc: Likewise.
> > > > >         * scripttempl/elf32crx.sc: Likewise.
> > > > >         * scripttempl/elf32msp430.sc: Likewise.
> > > > >         * scripttempl/elf64bpf.sc: Likewise.
> > > > >         * scripttempl/elf64hppa.sc: Likewise.
> > > > >         * scripttempl/elfarc.sc: Likewise.
> > > > >         * scripttempl/elfarcv2.sc: Likewise.
> > > > >         * scripttempl/elfd10v.sc: Likewise.
> > > > >         * scripttempl/elfd30v.sc: Likewise.
> > > > >         * scripttempl/elfm68hc11.sc: Likewise.
> > > > >         * scripttempl/elfm68hc12.sc: Likewise.
> > > > >         * scripttempl/elfm9s12z.sc: Likewise.
> > > > >         * scripttempl/elfmicroblaze.sc: Likewise.
> > > > >         * scripttempl/elfxgate.sc: Likewise.
> > > > >         * scripttempl/elfxtensa.sc: Likewise.
> > > > >         * scripttempl/ft32.sc: Likewise.
> > > > >         * scripttempl/iq2000.sc: Likewise.
> > > > >         * scripttempl/mep.sc: Likewise.
> > > > >         * scripttempl/mmo.sc: Likewise.
> > > > >         * scripttempl/pru.sc: Likewise.
> > > > >         * scripttempl/v850.sc: Likewise.
> > > > >         * scripttempl/v850_rh850.sc: Likewise.
> > > > >         * scripttempl/visium.sc: Likewise.
> > > > >         * scripttempl/xstormy16.sc: Likewise.
> > > > >         * scripttempl/arclinux.sc: Include the text section order file
> > > > >         and place code text sections toward the end.
> > > > >         * scripttempl/elf64bpf.sc: Likewise.
> > > > >         * scripttempl/nds32elf.sc: Likewise.
> > > > >         * testsuite/ld-scripts/text-order-1a.d: Remove xfail.
> > > > >         * testsuite/ld-scripts/text-order-1b.d: Likewise.
> > > > >         * testsuite/ld-scripts/text-order-1c.d: Likewise.
> > > > > ---
> > > > >  ld/scripttempl/arclinux.sc              | 5 +++--
> > > > >  ld/scripttempl/avr.sc                   | 1 +
> > > > >  ld/scripttempl/dlx.sc                   | 1 +
> > > > >  ld/scripttempl/elf32cr16.sc             | 1 +
> > > > >  ld/scripttempl/elf32crx.sc              | 1 +
> > > > >  ld/scripttempl/elf32msp430.sc           | 1 +
> > > > >  ld/scripttempl/elf64bpf.sc              | 5 +++--
> > > > >  ld/scripttempl/elf64hppa.sc             | 1 +
> > > > >  ld/scripttempl/elfarc.sc                | 1 +
> > > > >  ld/scripttempl/elfarcv2.sc              | 1 +
> > > > >  ld/scripttempl/elfd10v.sc               | 1 +
> > > > >  ld/scripttempl/elfd30v.sc               | 1 +
> > > > >  ld/scripttempl/elfm68hc11.sc            | 1 +
> > > > >  ld/scripttempl/elfm68hc12.sc            | 1 +
> > > > >  ld/scripttempl/elfm9s12z.sc             | 1 +
> > > > >  ld/scripttempl/elfmicroblaze.sc         | 1 +
> > > > >  ld/scripttempl/elfxgate.sc              | 1 +
> > > > >  ld/scripttempl/elfxtensa.sc             | 1 +
> > > > >  ld/scripttempl/ft32.sc                  | 1 +
> > > > >  ld/scripttempl/iq2000.sc                | 1 +
> > > > >  ld/scripttempl/mep.sc                   | 1 +
> > > > >  ld/scripttempl/mmo.sc                   | 1 +
> > > > >  ld/scripttempl/nds32elf.sc              | 5 +++--
> > > > >  ld/scripttempl/pru.sc                   | 1 +
> > > > >  ld/scripttempl/v850.sc                  | 1 +
> > > > >  ld/scripttempl/v850_rh850.sc            | 1 +
> > > > >  ld/scripttempl/visium.sc                | 1 +
> > > > >  ld/scripttempl/xstormy16.sc             | 1 +
> > > > >  ld/testsuite/ld-scripts/text-order-1a.d | 2 --
> > > > >  ld/testsuite/ld-scripts/text-order-1b.d | 2 --
> > > > >  ld/testsuite/ld-scripts/text-order-1c.d | 2 --
> > > > >  31 files changed, 34 insertions(+), 12 deletions(-)
> > > > >
> > > > > diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc
> > > > > index 36ba5a664d3..646dc08a9f8 100644
> > > > > --- a/ld/scripttempl/arclinux.sc
> > > > > +++ b/ld/scripttempl/arclinux.sc
> > > > > @@ -487,11 +487,12 @@ cat <<EOF
> > > > >    .text         ${RELOCATING-0} :
> > > > >    {
> > > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > >
> > > > Why after start symbols? It seems that just decreases the ability
> > > > to control alignment.
> > >
> > > Why?
> > won't it add an arbitrary amount of padding (depending on number of
> > start symbols) before the actual ordering.
>
> Start symbols have no impact on section layout nor memory size.

ah okay.
>
> > >
> > > > > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > > > >      ${RELOCATING+*(.text.hot .text.hot.*)}
> > > > >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > > > > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > >      *(.gnu.warning)
> > > > > diff --git a/ld/scripttempl/avr.sc b/ld/scripttempl/avr.sc
> > > > > index 3b18897f33c..558b4e22c75 100644
> > > > > --- a/ld/scripttempl/avr.sc
> > > > > +++ b/ld/scripttempl/avr.sc
> > > > > @@ -244,6 +244,7 @@ SECTIONS
> > > > >      KEEP (*(.init8))
> > > > >      *(.init9)  /* Call main().  */
> > > > >      KEEP (*(.init9))}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+. = ALIGN(2);
> > > > >      *(.text.*)
> > > > > diff --git a/ld/scripttempl/dlx.sc b/ld/scripttempl/dlx.sc
> > > > > index 8dcbf300c56..e66c68f064d 100644
> > > > > --- a/ld/scripttempl/dlx.sc
> > > > > +++ b/ld/scripttempl/dlx.sc
> > > > > @@ -22,6 +22,7 @@ SECTIONS
> > > > >    .text :
> > > > >    {
> > > > >      ${RELOCATING+CREATE_OBJECT_SYMBOLS}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+etext = ${DATA_ALIGNMENT};}
> > > > >    }
> > > > > diff --git a/ld/scripttempl/elf32cr16.sc b/ld/scripttempl/elf32cr16.sc
> > > > > index 3e7d92a4f34..2d6cd971bc2 100644
> > > > > --- a/ld/scripttempl/elf32cr16.sc
> > > > > +++ b/ld/scripttempl/elf32cr16.sc
> > > > > @@ -81,6 +81,7 @@ SECTIONS
> > > > >    .text :
> > > > >    {
> > > > >      __TEXT_START = .;
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
> > > > >      __TEXT_END = .;
> > > > >    }${RELOCATING+ > rom}
> > > > > diff --git a/ld/scripttempl/elf32crx.sc b/ld/scripttempl/elf32crx.sc
> > > > > index 1b1316676a7..1eb5e752c3a 100644
> > > > > --- a/ld/scripttempl/elf32crx.sc
> > > > > +++ b/ld/scripttempl/elf32crx.sc
> > > > > @@ -77,6 +77,7 @@ SECTIONS
> > > > >    .text :
> > > > >    {
> > > > >      __TEXT_START = .;
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text) *(.text.*) *(.gnu.linkonce.t.*)
> > > > >      __TEXT_END = .;
> > > > >    } > rom
> > > > > diff --git a/ld/scripttempl/elf32msp430.sc b/ld/scripttempl/elf32msp430.sc
> > > > > index bed0d673238..1fadaca2e27 100644
> > > > > --- a/ld/scripttempl/elf32msp430.sc
> > > > > +++ b/ld/scripttempl/elf32msp430.sc
> > > > > @@ -163,6 +163,7 @@ SECTIONS
> > > > >      *(.lower.text.* .lower.text)
> > > > >
> > > > >      . = ALIGN(2);}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+. = ALIGN(2);
> > > > >      *(.text.*)
> > > > > diff --git a/ld/scripttempl/elf64bpf.sc b/ld/scripttempl/elf64bpf.sc
> > > > > index ca62d7c88e0..93d8592a6a6 100644
> > > > > --- a/ld/scripttempl/elf64bpf.sc
> > > > > +++ b/ld/scripttempl/elf64bpf.sc
> > > > > @@ -508,11 +508,12 @@ cat <<EOF
> > > > >    .text         ${RELOCATING-0} :
> > > > >    {
> > > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > > > >      ${RELOCATING+*(.text.hot .text.hot.*)}
> > > > >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > > > > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > >      *(.gnu.warning)
> > > > > diff --git a/ld/scripttempl/elf64hppa.sc b/ld/scripttempl/elf64hppa.sc
> > > > > index 6bde304dd27..94b458010c5 100644
> > > > > --- a/ld/scripttempl/elf64hppa.sc
> > > > > +++ b/ld/scripttempl/elf64hppa.sc
> > > > > @@ -412,6 +412,7 @@ cat <<EOF
> > > > >    .text         ${RELOCATING-0} :
> > > > >    {
> > > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > >      *(.gnu.warning)
> > > > > diff --git a/ld/scripttempl/elfarc.sc b/ld/scripttempl/elfarc.sc
> > > > > index 1ae0248a900..ec16bf5a184 100644
> > > > > --- a/ld/scripttempl/elfarc.sc
> > > > > +++ b/ld/scripttempl/elfarc.sc
> > > > > @@ -288,6 +288,7 @@ cat <<EOF
> > > > >    .text         ${RELOCATING-0} :
> > > > >    {
> > > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > >      *(.gnu.warning)
> > > > > diff --git a/ld/scripttempl/elfarcv2.sc b/ld/scripttempl/elfarcv2.sc
> > > > > index 3054e4c62f3..30bcbd304ab 100644
> > > > > --- a/ld/scripttempl/elfarcv2.sc
> > > > > +++ b/ld/scripttempl/elfarcv2.sc
> > > > > @@ -192,6 +192,7 @@ SECTIONS
> > > > >
> > > > >      /* Remaining code.  */
> > > > >      ${RELOCATING+ . = ALIGN(4);}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > >      *(.gnu.warning)
> > > > > diff --git a/ld/scripttempl/elfd10v.sc b/ld/scripttempl/elfd10v.sc
> > > > > index 1ecf4a19092..e42bf3cfa70 100644
> > > > > --- a/ld/scripttempl/elfd10v.sc
> > > > > +++ b/ld/scripttempl/elfd10v.sc
> > > > > @@ -109,6 +109,7 @@ SECTIONS
> > > > >      KEEP (*(SORT_NONE(.init.*)))
> > > > >      KEEP (*(SORT_NONE(.fini)))
> > > > >      KEEP (*(SORT_NONE(.fini.*)))}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > > diff --git a/ld/scripttempl/elfd30v.sc b/ld/scripttempl/elfd30v.sc
> > > > > index ed0c412edc8..2dcbb7522cc 100644
> > > > > --- a/ld/scripttempl/elfd30v.sc
> > > > > +++ b/ld/scripttempl/elfd30v.sc
> > > > > @@ -125,6 +125,7 @@ SECTIONS
> > > > >    /* Internal text space or external memory */
> > > > >    .text :
> > > > >    {
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.gnu.linkonce.t*)
> > > > >      KEEP (*(SORT_NONE(.fini)))
> > > > > diff --git a/ld/scripttempl/elfm68hc11.sc b/ld/scripttempl/elfm68hc11.sc
> > > > > index b122da0e32a..f4393a87b68 100644
> > > > > --- a/ld/scripttempl/elfm68hc11.sc
> > > > > +++ b/ld/scripttempl/elfm68hc11.sc
> > > > > @@ -318,6 +318,7 @@ SECTIONS
> > > > >      /* Put startup code at beginning so that _start keeps same address.  */
> > > > >      ${RELOCATING+${STARTUP_CODE}}
> > > > >
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > > diff --git a/ld/scripttempl/elfm68hc12.sc b/ld/scripttempl/elfm68hc12.sc
> > > > > index 47410a95f46..24ea7ed0302 100644
> > > > > --- a/ld/scripttempl/elfm68hc12.sc
> > > > > +++ b/ld/scripttempl/elfm68hc12.sc
> > > > > @@ -317,6 +317,7 @@ SECTIONS
> > > > >      /* Put startup code at beginning so that _start keeps same address.  */
> > > > >      ${RELOCATING+${STARTUP_CODE}}
> > > > >
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > > diff --git a/ld/scripttempl/elfm9s12z.sc b/ld/scripttempl/elfm9s12z.sc
> > > > > index 271d218835b..2d929e1e3fe 100644
> > > > > --- a/ld/scripttempl/elfm9s12z.sc
> > > > > +++ b/ld/scripttempl/elfm9s12z.sc
> > > > > @@ -303,6 +303,7 @@ SECTIONS
> > > > >      /* Put startup code at beginning so that _start keeps same address.  */
> > > > >      ${RELOCATING+${STARTUP_CODE}}
> > > > >
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > > diff --git a/ld/scripttempl/elfmicroblaze.sc b/ld/scripttempl/elfmicroblaze.sc
> > > > > index 88abe4f10ba..e386da51512 100644
> > > > > --- a/ld/scripttempl/elfmicroblaze.sc
> > > > > +++ b/ld/scripttempl/elfmicroblaze.sc
> > > > > @@ -79,6 +79,7 @@ SECTIONS
> > > > >
> > > > >    ${RELOCATING+ _ftext  =  .;}
> > > > >    .text : {
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      ${RELOCATING+*(.gnu.linkonce.t.*)}
> > > > > diff --git a/ld/scripttempl/elfxgate.sc b/ld/scripttempl/elfxgate.sc
> > > > > index 4c25bc161f2..71d4855f47e 100644
> > > > > --- a/ld/scripttempl/elfxgate.sc
> > > > > +++ b/ld/scripttempl/elfxgate.sc
> > > > > @@ -317,6 +317,7 @@ SECTIONS
> > > > >      /* Put startup code at beginning so that _start keeps same address.  */
> > > > >      ${RELOCATING+${STARTUP_CODE}}
> > > > >
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > > diff --git a/ld/scripttempl/elfxtensa.sc b/ld/scripttempl/elfxtensa.sc
> > > > > index 231f53b5f5b..abc1d33350e 100644
> > > > > --- a/ld/scripttempl/elfxtensa.sc
> > > > > +++ b/ld/scripttempl/elfxtensa.sc
> > > > > @@ -418,6 +418,7 @@ cat <<EOF
> > > > >      ${RELOCATING+${INIT_END}}
> > > > >
> > > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.literal .text .stub${RELOCATING+ .literal.* .text.* .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*})
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > >      *(.gnu.warning)
> > > > > diff --git a/ld/scripttempl/ft32.sc b/ld/scripttempl/ft32.sc
> > > > > index e52e75cd924..8c966de3bb1 100644
> > > > > --- a/ld/scripttempl/ft32.sc
> > > > > +++ b/ld/scripttempl/ft32.sc
> > > > > @@ -32,6 +32,7 @@ SECTIONS
> > > > >  {
> > > > >    .text :
> > > > >    {
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text${RELOCATING+*})
> > > > >      ${RELOCATING+*(.strings)
> > > > >      *(._pm*)
> > > > > diff --git a/ld/scripttempl/iq2000.sc b/ld/scripttempl/iq2000.sc
> > > > > index e85aa2303ed..94e834aef15 100644
> > > > > --- a/ld/scripttempl/iq2000.sc
> > > > > +++ b/ld/scripttempl/iq2000.sc
> > > > > @@ -297,6 +297,7 @@ cat <<EOF
> > > > >    .text    ${RELOCATING-0} :
> > > > >    {
> > > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      *(.stub)
> > > > > diff --git a/ld/scripttempl/mep.sc b/ld/scripttempl/mep.sc
> > > > > index 39e4d64857c..656b1f64386 100644
> > > > > --- a/ld/scripttempl/mep.sc
> > > > > +++ b/ld/scripttempl/mep.sc
> > > > > @@ -310,6 +310,7 @@ cat <<EOF
> > > > >    .text         ${RELOCATING-0} :
> > > > >    {
> > > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > >      *(.gnu.warning)
> > > > > diff --git a/ld/scripttempl/mmo.sc b/ld/scripttempl/mmo.sc
> > > > > index e87a758eb90..ac6a57eee2a 100644
> > > > > --- a/ld/scripttempl/mmo.sc
> > > > > +++ b/ld/scripttempl/mmo.sc
> > > > > @@ -30,6 +30,7 @@ SECTIONS
> > > > >      ${RELOCATING+ KEEP (*(SORT_NONE(.init)))}
> > > > >      ${RELOCATING+ PROVIDE (_init_end = .);}
> > > > >
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      ${RELOCATING+*(.gnu.linkonce.t*)}
> > > > > diff --git a/ld/scripttempl/nds32elf.sc b/ld/scripttempl/nds32elf.sc
> > > > > index 0f8366d91ac..2626e773a4e 100644
> > > > > --- a/ld/scripttempl/nds32elf.sc
> > > > > +++ b/ld/scripttempl/nds32elf.sc
> > > > > @@ -434,11 +434,12 @@ cat <<EOF
> > > > >    .text         ${RELOCATING-0} :
> > > > >    {
> > > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > > -    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > > -    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > > > >      ${RELOCATING+*(.text.hot .text.hot.*)}
> > > > >      ${RELOCATING+*(SORT(.text.sorted.*))}
> > > > > +    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > > > +    ${RELOCATING+*(.text.exit .text.exit.*)}
> > > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > > >      *(.gnu.warning)
> > > > > diff --git a/ld/scripttempl/pru.sc b/ld/scripttempl/pru.sc
> > > > > index 3ff86bc61c7..d4d79cbcf82 100644
> > > > > --- a/ld/scripttempl/pru.sc
> > > > > +++ b/ld/scripttempl/pru.sc
> > > > > @@ -110,6 +110,7 @@ SECTIONS
> > > > >      ${RELOCATING+KEEP (*(.init0))}
> > > > >
> > > > >      ${RELOCATING+. = ALIGN(4);}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+. = ALIGN(4);}
> > > > >      ${RELOCATING+*(.text.*)}
> > > > > diff --git a/ld/scripttempl/v850.sc b/ld/scripttempl/v850.sc
> > > > > index 10d1da8b5bb..530b4ee72ca 100644
> > > > > --- a/ld/scripttempl/v850.sc
> > > > > +++ b/ld/scripttempl/v850.sc
> > > > > @@ -76,6 +76,7 @@ SECTIONS
> > > > >
> > > > >    .text                :
> > > > >    {
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >
> > > > > diff --git a/ld/scripttempl/v850_rh850.sc b/ld/scripttempl/v850_rh850.sc
> > > > > index e63e7db8884..3d2874267a9 100644
> > > > > --- a/ld/scripttempl/v850_rh850.sc
> > > > > +++ b/ld/scripttempl/v850_rh850.sc
> > > > > @@ -80,6 +80,7 @@ SECTIONS
> > > > >
> > > > >    .text                :
> > > > >    {
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >
> > > > > diff --git a/ld/scripttempl/visium.sc b/ld/scripttempl/visium.sc
> > > > > index 0b90b888136..8d97bbf4a68 100644
> > > > > --- a/ld/scripttempl/visium.sc
> > > > > +++ b/ld/scripttempl/visium.sc
> > > > > @@ -67,6 +67,7 @@ SECTIONS
> > > > >
> > > > >    .text ${RELOCATING-0} : {
> > > > >      ${RELOCATING+ _ftext  =  .;}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      ${RELOCATING+*(.gnu.linkonce.t.*)}
> > > > > diff --git a/ld/scripttempl/xstormy16.sc b/ld/scripttempl/xstormy16.sc
> > > > > index c4117fcde3e..3de1bfa0d18 100644
> > > > > --- a/ld/scripttempl/xstormy16.sc
> > > > > +++ b/ld/scripttempl/xstormy16.sc
> > > > > @@ -179,6 +179,7 @@ SECTIONS
> > > > >    .text    ${RELOCATING-0} :
> > > > >    {
> > > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > >      *(.text)
> > > > >      ${RELOCATING+*(.text.*)}
> > > > >      *(.stub)
> > > > > diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
> > > > > index 87cee75055f..d6f976f42ad 100644
> > > > > --- a/ld/testsuite/ld-scripts/text-order-1a.d
> > > > > +++ b/ld/testsuite/ld-scripts/text-order-1a.d
> > > > > @@ -4,8 +4,6 @@
> > > > >  #ld: --text-section-ordering-file text-order-1a.t
> > > > >  #nm: -n
> > > > >  #target: [is_elf_format]
> > > > > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > > > -# generic linker targets don't support --text-section-ordering-file
> > > > >
> > > > >  #...
> > > > >  [0-9a-f]+ T yyy
> > > > > diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
> > > > > index 3f83813da2b..3d6f4fd8d66 100644
> > > > > --- a/ld/testsuite/ld-scripts/text-order-1b.d
> > > > > +++ b/ld/testsuite/ld-scripts/text-order-1b.d
> > > > > @@ -4,8 +4,6 @@
> > > > >  #ld: --text-section-ordering-file text-order-1b.t
> > > > >  #nm: -n
> > > > >  #target: [is_elf_format]
> > > > > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > > > -# generic linker targets don't support --text-section-ordering-file
> > > > >
> > > > >  #...
> > > > >  [0-9a-f]+ T yyy
> > > > > diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
> > > > > index 0c484637784..4166d634606 100644
> > > > > --- a/ld/testsuite/ld-scripts/text-order-1c.d
> > > > > +++ b/ld/testsuite/ld-scripts/text-order-1c.d
> > > > > @@ -4,6 +4,4 @@
> > > > >  #ld: --text-section-ordering-file text-order-1c.t
> > > > >  #nm: -n
> > > > >  #target: [is_elf_format]
> > > > > -#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > > > -# generic linker targets don't support --text-section-ordering-file
> > > > >  #error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
> > > > > --
> > > > > 2.43.0
> > > > >
> > >
> > >
> > >
> > > --
> > > H.J.
>
>
>
> --
> H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE
  2024-01-19 19:45 ` [PATCH 1/4] " H.J. Lu
@ 2024-01-19 21:50   ` Fangrui Song
       [not found]   ` <DS7PR12MB5765A2B98C7E1217F8C374B9CB702@DS7PR12MB5765.namprd12.prod.outlook.com>
  1 sibling, 0 replies; 19+ messages in thread
From: Fangrui Song @ 2024-01-19 21:50 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils, goldstein.w.n

On Fri, Jan 19, 2024 at 11:45 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> Add --text-section-ordering-file FILE to specify the text section
> ordering file.  Inside the text section ordering file, when seeing an
> input file name without section list and the file name starts with '.',
> treat the filename as a section name and create a wild card as the file
> name.
>
> A text section ordering file which contains text section wildcards:
>
> ---
> text_section_foo*
> text_section_b?r
> text_section_name
> ---
>
> can be used to group input text sections together to:
>
> 1. Reduce gaps between text sections.
> 2. Put hot text sections close to each other.
>
> Text sections in the text section ordering file are placed at the
> beginning of the output text section and code text sections are moved
> toward the end so that the first section in the text section ordering
> file is aligned to the output section alignment.
>
> --text-section-ordering-file must be placed before -T/--script option
> so that the text section ordering file can always be included in linker
> scripts.  Nested INCLUDE in the text section order file is disallowed.
>
> NB: Gold has the command-line option, --section-ordering-file FILE, to
> layout sections in the order specified.  --text-section-ordering-file
> supports the same section ordering file format, but it applies only to
> text sections.

Thanks for adding the feature to ld! However, I think a
symbol-oriented option likely works better than a section-oriented
option.

https://maskray.me/blog/2020-11-15-explain-gnu-linker-options#symbol-ordering-filefile

This option (--symbol-ordering-file=) is unique to ld.lld. gold has a
--section-ordering-file, sorted by section name. In practice, text and
data sections mostly have different names. However, clang
-fno-unique-section-names (GCC feature request
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95095) can create
sections of the same that defeat --section-ordering-file.

>
>         * ld.h (ld_config_type): Add text_section_ordering_file.
>         * ldfile.c (in_text_section_ordering_file): New.
>         (try_open): Add an argument for the original file name.  Dump
>         the text section order file for verbose output.
>         (ldfile_find_command_file): Pass the original file name to
>         try_open.
>         (ldfile_open_command_file_1): Don't allow nested INCLUDE in the
>         text section order file.  If the text section order file is
>         specified, set in_text_section_ordering_file, load the file and
>         don't set saved_script_handle.  Clear seen_eof_include_file.
>         * ldlang.c (lang_add_wild): When seeing an input file name
>         without section list and the file name starts with '.', treat
>         the filename as a section name and create a wild card as the
>         file name.
>         * ldlex.h (option_values): Add OPTION_TEXT_SECTION_ORDERING_FILE.
>         (in_text_section_ordering_file): New.
>         (seen_eof_include_file): Likewise.
>         * ldlex.l (in_text_section_ordering_file): New.
>         (seen_eof_include_file): Likewise.
>         (<<EOF>>): Set seen_eof_include_file.
>         * lexsup.c (ld_options): Document --text-section-ordering-file.
>         (parse_args): Handle OPTION_TEXT_SECTION_ORDERING_FILE.  Issue
>         an error if --text-section-ordering-file is placed after
>         -T/--script.
>         * scripttempl/elf.sc: Include the text section order file and
>         place code text sections toward the end.
>         * testsuite/ld-scripts/start.s: New file.
>         * testsuite/ld-scripts/text-order-1a.d: New file.
>         * testsuite/ld-scripts/text-order-1a.s: Likewise.
>         * testsuite/ld-scripts/text-order-1a.t: Likewise.
>         * testsuite/ld-scripts/text-order-1b.d: Likewise.
>         * testsuite/ld-scripts/text-order-1b.s: Likewise.
>         * testsuite/ld-scripts/text-order-1b.t: Likewise.
>         * testsuite/ld-scripts/text-order-1c.d: Likewise.
>         * testsuite/ld-scripts/text-order-1d.t: Likewise.
>         * testsuite/ld-scripts/text-order.exp: Likewise.
> ---
>  ld/ld.h                                 |  3 +
>  ld/ldfile.c                             | 74 +++++++++++++++++++++++--
>  ld/ldlang.c                             | 39 +++++++++++--
>  ld/ldlex.h                              |  3 +
>  ld/ldlex.l                              |  7 +++
>  ld/lexsup.c                             | 11 ++++
>  ld/scripttempl/elf.sc                   |  5 +-
>  ld/testsuite/ld-scripts/start.s         | 12 ++++
>  ld/testsuite/ld-scripts/text-order-1a.d | 20 +++++++
>  ld/testsuite/ld-scripts/text-order-1a.s |  8 +++
>  ld/testsuite/ld-scripts/text-order-1a.t |  5 ++
>  ld/testsuite/ld-scripts/text-order-1b.d | 20 +++++++
>  ld/testsuite/ld-scripts/text-order-1b.s |  8 +++
>  ld/testsuite/ld-scripts/text-order-1b.t |  4 ++
>  ld/testsuite/ld-scripts/text-order-1c.d |  9 +++
>  ld/testsuite/ld-scripts/text-order-1c.t |  5 ++
>  ld/testsuite/ld-scripts/text-order.exp  | 42 ++++++++++++++
>  17 files changed, 264 insertions(+), 11 deletions(-)
>  create mode 100644 ld/testsuite/ld-scripts/start.s
>  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.d
>  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.s
>  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.t
>  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.d
>  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.s
>  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.t
>  create mode 100644 ld/testsuite/ld-scripts/text-order-1c.d
>  create mode 100644 ld/testsuite/ld-scripts/text-order-1c.t
>  create mode 100644 ld/testsuite/ld-scripts/text-order.exp
>
> diff --git a/ld/ld.h b/ld/ld.h
> index 54d9079678c..a80255a73ba 100644
> --- a/ld/ld.h
> +++ b/ld/ld.h
> @@ -316,6 +316,9 @@ typedef struct
>
>    /* Compress DWARF debug sections.  */
>    enum compressed_debug_section_type compress_debug;
> +
> +  /* The optional text section ordering file.  */
> +  const char *text_section_ordering_file;
>  } ld_config_type;
>
>  extern ld_config_type config;
> diff --git a/ld/ldfile.c b/ld/ldfile.c
> index dc9875d8813..6e9fbb7730f 100644
> --- a/ld/ldfile.c
> +++ b/ld/ldfile.c
> @@ -736,7 +736,7 @@ ldfile_open_file (lang_input_statement_type *entry)
>  /* Try to open NAME.  */
>
>  static FILE *
> -try_open (const char *name, bool *sysrooted)
> +try_open (const char *name, const char *orig_name, bool *sysrooted)
>  {
>    FILE *result;
>
> @@ -750,7 +750,34 @@ try_open (const char *name, bool *sysrooted)
>
>    if (verbose)
>      {
> -      if (result == NULL)
> +      if (config.text_section_ordering_file != NULL
> +         && strcmp (orig_name, config.text_section_ordering_file) == 0)
> +       {
> +         if (result == NULL)
> +           info_msg (_("cannot find text section ordering file: %s\n"),
> +                     name);
> +         else
> +           {
> +             static const int ld_bufsz = 8193;
> +             size_t n;
> +             char *buf = (char *) xmalloc (ld_bufsz);
> +
> +             info_msg (_("opened text section ordering file: %s\n"),
> +                       name);
> +             info_msg ("==================================================\n");
> +
> +             while ((n = fread (buf, 1, ld_bufsz - 1, result)) > 0)
> +               {
> +                 buf[n] = 0;
> +                 info_msg ("%s", buf);
> +               }
> +             rewind (result);
> +             free (buf);
> +
> +             info_msg ("==================================================\n\n");
> +           }
> +       }
> +      else if (result == NULL)
>         info_msg (_("cannot find script file %s\n"), name);
>        else
>         info_msg (_("opened script file %s\n"), name);
> @@ -832,7 +859,7 @@ ldfile_find_command_file (const char *name,
>    if (!default_only)
>      {
>        /* First try raw name.  */
> -      result = try_open (name, sysrooted);
> +      result = try_open (name, name, sysrooted);
>        if (result != NULL)
>         return result;
>      }
> @@ -859,7 +886,7 @@ ldfile_find_command_file (const char *name,
>         search = search->next)
>      {
>        path = concat (search->name, slash, name, (const char *) NULL);
> -      result = try_open (path, sysrooted);
> +      result = try_open (path, name, sysrooted);
>        free (path);
>        if (result)
>         break;
> @@ -908,6 +935,38 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
>         }
>      }
>
> +  /* Don't allow nested INCLUDE in the text section ordering file.  */
> +  if (in_text_section_ordering_file)
> +    {
> +      einfo (_("%F%P: error: nested include '%s' in the text section "
> +              "ordering file: '%s'\n"), name,
> +              config.text_section_ordering_file);
> +      return;
> +    }
> +
> +  if (strcmp (name, "config.text_section_ordering_file") == 0)
> +    {
> +      /* Support
> +
> +        INCLUDE config.text_section_ordering_file;
> +
> +        in input text sections in linker script.  */
> +      if (config.text_section_ordering_file == NULL)
> +       {
> +         /* Skip if the text section ordering file isn't specified.  */
> +         lex_push_file (NULL, name, false);
> +         return;
> +       }
> +
> +      /* Load the text section ordering file.  */
> +      name = config.text_section_ordering_file;
> +
> +      /* Set the in the text section ordering file marker.  */
> +      in_text_section_ordering_file = 1;
> +    }
> +  else
> +    in_text_section_ordering_file = 0;
> +
>    /* FIXME: This memory is never freed, but that should not really matter.
>       It will be released when the linker exits, and it is unlikely to ever
>       be more than a few tens of bytes.  */
> @@ -932,7 +991,12 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
>
>    lineno = 1;
>
> -  saved_script_handle = ldlex_input_stack;
> +  /* Clear the end of the include file marker.  */
> +  seen_eof_include_file = 0;
> +
> +  /* The text section ordering file isn't a real linker script file.  */
> +  if (!in_text_section_ordering_file)
> +    saved_script_handle = ldlex_input_stack;
>  }
>
>  /* Open command file NAME in the current directory, -L directories,
> diff --git a/ld/ldlang.c b/ld/ldlang.c
> index 229401c8342..d2b4d43d8ae 100644
> --- a/ld/ldlang.c
> +++ b/ld/ldlang.c
> @@ -8512,12 +8512,43 @@ lang_add_wild (struct wildcard_spec *filespec,
>
>    if (filespec != NULL && filespec->name != NULL)
>      {
> -      if (strcmp (filespec->name, "*") == 0)
> -       filespec->name = NULL;
> -      else if (!wildcardp (filespec->name))
> -       lang_has_input_file = true;
> +      if (in_text_section_ordering_file != 0
> +         && section_list == NULL
> +         && filespec->name[0] == '.'
> +         && filespec->sorted == none
> +         && filespec->exclude_name_list == NULL
> +         && filespec->section_flag_list == NULL
> +         && !filespec->reversed)
> +       {
> +         /* When seeing an input file name without section list in the
> +            text section ordering file and the file name starts with
> +            '.', treat the filename as a section name and create a
> +            wild card as the file name.  */
> +         struct wildcard_list *single_section
> +           = (struct wildcard_list *) xmalloc (sizeof (*single_section));
> +         memset (single_section, 0, sizeof (*single_section));
> +         single_section->spec.name = filespec->name;
> +         single_section->spec.sorted = none;
> +         /* A NULL indicates the wild card file name, "*".  */
> +         filespec->name = NULL;
> +         section_list = single_section;
> +       }
> +      else
> +       {
> +         if (strcmp (filespec->name, "*") == 0)
> +           filespec->name = NULL;
> +         else if (!wildcardp (filespec->name))
> +           lang_has_input_file = true;
> +       }
>      }
>
> +  /* NB: Clear the in the text section ordering file marker after
> +     processing the last entry when the end of the text section
> +     ordering file is reached.  */
> +  if (in_text_section_ordering_file != 0
> +      && seen_eof_include_file != 0)
> +    in_text_section_ordering_file = 0;
> +
>    new_stmt = new_stat (lang_wild_statement, stat_ptr);
>    new_stmt->filename = NULL;
>    new_stmt->filenames_sorted = false;
> diff --git a/ld/ldlex.h b/ld/ldlex.h
> index e5ac2fa7fca..a2c49656e1a 100644
> --- a/ld/ldlex.h
> +++ b/ld/ldlex.h
> @@ -68,6 +68,7 @@ enum option_values
>    OPTION_TASK_LINK,
>    OPTION_TBSS,
>    OPTION_TDATA,
> +  OPTION_TEXT_SECTION_ORDERING_FILE,
>    OPTION_TTEXT,
>    OPTION_TTEXT_SEGMENT,
>    OPTION_TRODATA_SEGMENT,
> @@ -484,6 +485,8 @@ extern input_type parser_input;
>
>  extern unsigned int lineno;
>  extern const char *lex_string;
> +extern int in_text_section_ordering_file;
> +extern int seen_eof_include_file;
>
>  /* In ldlex.l.  */
>  extern int yylex (void);
> diff --git a/ld/ldlex.l b/ld/ldlex.l
> index e113c90812b..b41e1220661 100644
> --- a/ld/ldlex.l
> +++ b/ld/ldlex.l
> @@ -43,6 +43,12 @@ input_type parser_input;
>  /* Line number in the current input file.  */
>  unsigned int lineno;
>
> +/* 1 if the current input file is the text section ordering file.  */
> +int in_text_section_ordering_file = 0;
> +
> +/* 1 if the end of the include file is reached.  */
> +int seen_eof_include_file = 0;
> +
>  /* The string we are currently lexing, or NULL if we are reading a
>     file.  */
>  const char *lex_string = NULL;
> @@ -487,6 +493,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
>
>    lineno = lineno_stack[include_stack_ptr];
>    input_flags.sysrooted = sysrooted_stack[include_stack_ptr];
> +  seen_eof_include_file = 1;
>
>    return END;
>  }
> diff --git a/ld/lexsup.c b/ld/lexsup.c
> index 099dff8ecde..21385628020 100644
> --- a/ld/lexsup.c
> +++ b/ld/lexsup.c
> @@ -484,6 +484,9 @@ static const struct ld_option ld_options[] =
>    { {"sort-section", required_argument, NULL, OPTION_SORT_SECTION},
>      '\0', N_("name|alignment"),
>      N_("Sort sections by name or maximum alignment"), TWO_DASHES },
> +  { {"text-section-ordering-file", required_argument, NULL, OPTION_TEXT_SECTION_ORDERING_FILE},
> +    '\0', N_("FILE"),
> +    N_("Sort text sections by FILE"), TWO_DASHES },
>    { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
>      '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"),
>      TWO_DASHES },
> @@ -670,6 +673,7 @@ parse_args (unsigned argc, char **argv)
>      dynamic_list
>    } opt_dynamic_list = dynamic_list_unset;
>    struct bfd_elf_dynamic_list *export_list = NULL;
> +  bool seen_linker_script = false;
>
>    shortopts = (char *) xmalloc (OPTION_COUNT * 3 + 2);
>    longopts = (struct option *)
> @@ -1394,6 +1398,12 @@ parse_args (unsigned argc, char **argv)
>             einfo (_("%F%P: invalid section sorting option: %s\n"),
>                    optarg);
>           break;
> +       case OPTION_TEXT_SECTION_ORDERING_FILE:
> +         if (seen_linker_script)
> +           einfo (_("%F%P: --text-section-ordering-file must be placed"
> +                    " before -T/--script\n"));
> +         config.text_section_ordering_file = optarg;
> +         break;
>         case OPTION_STATS:
>           config.stats = true;
>           break;
> @@ -1410,6 +1420,7 @@ parse_args (unsigned argc, char **argv)
>           ++trace_files;
>           break;
>         case 'T':
> +         seen_linker_script = true;
>           previous_script_handle = saved_script_handle;
>           ldfile_open_script_file (optarg);
>           parser_input = input_script;
> diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
> index fae7c2ad71c..8014dfbfa27 100644
> --- a/ld/scripttempl/elf.sc
> +++ b/ld/scripttempl/elf.sc
> @@ -553,11 +553,12 @@ cat <<EOF
>    .text         ${RELOCATING-0} :
>    {
>      ${RELOCATING+${TEXT_START_SYMBOLS}}
> +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> +    ${RELOCATING+*(.text.hot .text.hot.*)}
> +    ${RELOCATING+*(SORT(.text.sorted.*))}
>      ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
>      ${RELOCATING+*(.text.exit .text.exit.*)}
>      ${RELOCATING+*(.text.startup .text.startup.*)}
> -    ${RELOCATING+*(.text.hot .text.hot.*)}
> -    ${RELOCATING+*(SORT(.text.sorted.*))}
>      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
>      /* .gnu.warning sections are handled specially by elf.em.  */
>      *(.gnu.warning)
> diff --git a/ld/testsuite/ld-scripts/start.s b/ld/testsuite/ld-scripts/start.s
> new file mode 100644
> index 00000000000..4d8239479a6
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/start.s
> @@ -0,0 +1,12 @@
> +       .text
> +       .global start   /* Used by SH targets.  */
> +start:
> +       .global _start
> +_start:
> +       .global __start
> +__start:
> +       .global main    /* Used by HPPA targets.  */
> +main:
> +       .globl  _main   /* Used by LynxOS targets.  */
> +_main:
> +       .dc.a 0
> diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
> new file mode 100644
> index 00000000000..87cee75055f
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/text-order-1a.d
> @@ -0,0 +1,20 @@
> +#source: text-order-1a.s
> +#source: text-order-1b.s
> +#source: start.s
> +#ld: --text-section-ordering-file text-order-1a.t
> +#nm: -n
> +#target: [is_elf_format]
> +#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> +# generic linker targets don't support --text-section-ordering-file
> +
> +#...
> +[0-9a-f]+ T yyy
> +#...
> +[0-9a-f]+ T bar
> +#...
> +[0-9a-f]+ T _start
> +#...
> +[0-9a-f]+ T xxx
> +#...
> +[0-9a-f]+ T foo
> +#pass
> diff --git a/ld/testsuite/ld-scripts/text-order-1a.s b/ld/testsuite/ld-scripts/text-order-1a.s
> new file mode 100644
> index 00000000000..25f59b9e444
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/text-order-1a.s
> @@ -0,0 +1,8 @@
> +       .section .text.foo
> +       .globl  foo
> +foo:
> +       .dc.a 0
> +       .section .text.bar
> +       .globl  bar
> +bar:
> +       .dc.a 0
> diff --git a/ld/testsuite/ld-scripts/text-order-1a.t b/ld/testsuite/ld-scripts/text-order-1a.t
> new file mode 100644
> index 00000000000..72ea5f4cf46
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/text-order-1a.t
> @@ -0,0 +1,5 @@
> +*(.text.yyy)
> +*(.text.b?r)
> +*(.t*t)
> +*(.text.xxx)
> +*(.text.foo)
> diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
> new file mode 100644
> index 00000000000..3f83813da2b
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/text-order-1b.d
> @@ -0,0 +1,20 @@
> +#source: text-order-1a.s
> +#source: text-order-1b.s
> +#source: start.s
> +#ld: --text-section-ordering-file text-order-1b.t
> +#nm: -n
> +#target: [is_elf_format]
> +#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> +# generic linker targets don't support --text-section-ordering-file
> +
> +#...
> +[0-9a-f]+ T yyy
> +#...
> +[0-9a-f]+ T bar
> +#...
> +[0-9a-f]+ T _start
> +#...
> +[0-9a-f]+ T xxx
> +#...
> +[0-9a-f]+ T foo
> +#pass
> diff --git a/ld/testsuite/ld-scripts/text-order-1b.s b/ld/testsuite/ld-scripts/text-order-1b.s
> new file mode 100644
> index 00000000000..7ae27b9676a
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/text-order-1b.s
> @@ -0,0 +1,8 @@
> +       .section .text.xxx
> +       .globl  xxx
> +xxx:
> +       .dc.a 0
> +       .section .text.yyy
> +       .globl  yyy
> +yyy:
> +       .dc.a 0
> diff --git a/ld/testsuite/ld-scripts/text-order-1b.t b/ld/testsuite/ld-scripts/text-order-1b.t
> new file mode 100644
> index 00000000000..c1d092edb7e
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/text-order-1b.t
> @@ -0,0 +1,4 @@
> +.text.yyy
> +.text.b?r
> +.t*t
> +.text.xxx .text.foo
> diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
> new file mode 100644
> index 00000000000..0c484637784
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/text-order-1c.d
> @@ -0,0 +1,9 @@
> +#source: text-order-1a.s
> +#source: text-order-1b.s
> +#source: start.s
> +#ld: --text-section-ordering-file text-order-1c.t
> +#nm: -n
> +#target: [is_elf_format]
> +#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> +# generic linker targets don't support --text-section-ordering-file
> +#error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
> diff --git a/ld/testsuite/ld-scripts/text-order-1c.t b/ld/testsuite/ld-scripts/text-order-1c.t
> new file mode 100644
> index 00000000000..9ecbfb42826
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/text-order-1c.t
> @@ -0,0 +1,5 @@
> +.text.yyy
> +.text.b?r
> +INCLUDE text-order-1b.t
> +.t*t
> +.text.xxx .text.foo
> diff --git a/ld/testsuite/ld-scripts/text-order.exp b/ld/testsuite/ld-scripts/text-order.exp
> new file mode 100644
> index 00000000000..30cb531137e
> --- /dev/null
> +++ b/ld/testsuite/ld-scripts/text-order.exp
> @@ -0,0 +1,42 @@
> +# Test for --text-section-ordering-file FILE.
> +# Copyright (C) 2024 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.
> +
> +if { !([is_elf_format] && ![is_generic]) } {
> +    return
> +}
> +
> +set old_ldflags $LDFLAGS
> +if { [istarget spu*-*-*] } then {
> +    set LDFLAGS "$LDFLAGS --local-store 0:0 --no-overlays"
> +} elseif { [is_pecoff_format] } then {
> +    set LDFLAGS "$LDFLAGS --image-base 0"
> +} elseif { [is_xcoff_format] } then {
> +    set LDFLAGS "$LDFLAGS -bnogc"
> +}
> +
> +set test_list [lsort [glob -nocomplain $srcdir/$subdir/text-order*.d]]
> +foreach test_file $test_list {
> +    set test_name [file rootname $test_file]
> +    set map_file "tmpdir/[file tail $test_name].map"
> +    verbose $test_name
> +    run_dump_test $test_name
> +}
> +
> +set LDFLAGS $old_ldflags
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE
       [not found]   ` <DS7PR12MB5765A2B98C7E1217F8C374B9CB702@DS7PR12MB5765.namprd12.prod.outlook.com>
@ 2024-01-19 22:13     ` H.J. Lu
  2024-01-19 23:15       ` Fangrui Song
       [not found]       ` <DS7PR12MB5765059CCA55EC755F8132ACCB702@DS7PR12MB5765.namprd12.prod.outlook.com>
  0 siblings, 2 replies; 19+ messages in thread
From: H.J. Lu @ 2024-01-19 22:13 UTC (permalink / raw)
  To: Fangrui Song; +Cc: binutils, goldstein.w.n

On Fri, Jan 19, 2024 at 1:57 PM Fangrui Song <i@maskray.me> wrote:
>
> On Fri, Jan 19, 2024 at 11:45 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > Add --text-section-ordering-file FILE to specify the text section
> > ordering file.  Inside the text section ordering file, when seeing an
> > input file name without section list and the file name starts with '.',
> > treat the filename as a section name and create a wild card as the file
> > name.
> >
> > A text section ordering file which contains text section wildcards:
> >
> > ---
> > text_section_foo*
> > text_section_b?r
> > text_section_name
> > ---
> >
> > can be used to group input text sections together to:
> >
> > 1. Reduce gaps between text sections.
> > 2. Put hot text sections close to each other.
> >
> > Text sections in the text section ordering file are placed at the
> > beginning of the output text section and code text sections are moved
> > toward the end so that the first section in the text section ordering
> > file is aligned to the output section alignment.
> >
> > --text-section-ordering-file must be placed before -T/--script option
> > so that the text section ordering file can always be included in linker
> > scripts.  Nested INCLUDE in the text section order file is disallowed.
> >
> > NB: Gold has the command-line option, --section-ordering-file FILE, to
> > layout sections in the order specified.  --text-section-ordering-file
> > supports the same section ordering file format, but it applies only to
> > text sections.
>
> Thanks for adding the feature to ld! However, I think a
> symbol-oriented option likely works better than a section-oriented
> option.
>
> https://maskray.me/blog/2020-11-15-explain-gnu-linker-options#symbol-ordering-filefile
>
> This option (--symbol-ordering-file=) is unique to ld.lld. gold has a
> --section-ordering-file, sorted by section name. In practice, text and
> data sections mostly have different names. However, clang
> -fno-unique-section-names (GCC feature request
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95095) can create
> sections of the same that defeat --section-ordering-file.

--text-section-ordering-file is implemented on the existing linker framework.
A symbol-oriented option will require major changes in ld.

> >
> >         * ld.h (ld_config_type): Add text_section_ordering_file.
> >         * ldfile.c (in_text_section_ordering_file): New.
> >         (try_open): Add an argument for the original file name.  Dump
> >         the text section order file for verbose output.
> >         (ldfile_find_command_file): Pass the original file name to
> >         try_open.
> >         (ldfile_open_command_file_1): Don't allow nested INCLUDE in the
> >         text section order file.  If the text section order file is
> >         specified, set in_text_section_ordering_file, load the file and
> >         don't set saved_script_handle.  Clear seen_eof_include_file.
> >         * ldlang.c (lang_add_wild): When seeing an input file name
> >         without section list and the file name starts with '.', treat
> >         the filename as a section name and create a wild card as the
> >         file name.
> >         * ldlex.h (option_values): Add OPTION_TEXT_SECTION_ORDERING_FILE.
> >         (in_text_section_ordering_file): New.
> >         (seen_eof_include_file): Likewise.
> >         * ldlex.l (in_text_section_ordering_file): New.
> >         (seen_eof_include_file): Likewise.
> >         (<<EOF>>): Set seen_eof_include_file.
> >         * lexsup.c (ld_options): Document --text-section-ordering-file.
> >         (parse_args): Handle OPTION_TEXT_SECTION_ORDERING_FILE.  Issue
> >         an error if --text-section-ordering-file is placed after
> >         -T/--script.
> >         * scripttempl/elf.sc: Include the text section order file and
> >         place code text sections toward the end.
> >         * testsuite/ld-scripts/start.s: New file.
> >         * testsuite/ld-scripts/text-order-1a.d: New file.
> >         * testsuite/ld-scripts/text-order-1a.s: Likewise.
> >         * testsuite/ld-scripts/text-order-1a.t: Likewise.
> >         * testsuite/ld-scripts/text-order-1b.d: Likewise.
> >         * testsuite/ld-scripts/text-order-1b.s: Likewise.
> >         * testsuite/ld-scripts/text-order-1b.t: Likewise.
> >         * testsuite/ld-scripts/text-order-1c.d: Likewise.
> >         * testsuite/ld-scripts/text-order-1d.t: Likewise.
> >         * testsuite/ld-scripts/text-order.exp: Likewise.
> > ---
> >  ld/ld.h                                 |  3 +
> >  ld/ldfile.c                             | 74 +++++++++++++++++++++++--
> >  ld/ldlang.c                             | 39 +++++++++++--
> >  ld/ldlex.h                              |  3 +
> >  ld/ldlex.l                              |  7 +++
> >  ld/lexsup.c                             | 11 ++++
> >  ld/scripttempl/elf.sc                   |  5 +-
> >  ld/testsuite/ld-scripts/start.s         | 12 ++++
> >  ld/testsuite/ld-scripts/text-order-1a.d | 20 +++++++
> >  ld/testsuite/ld-scripts/text-order-1a.s |  8 +++
> >  ld/testsuite/ld-scripts/text-order-1a.t |  5 ++
> >  ld/testsuite/ld-scripts/text-order-1b.d | 20 +++++++
> >  ld/testsuite/ld-scripts/text-order-1b.s |  8 +++
> >  ld/testsuite/ld-scripts/text-order-1b.t |  4 ++
> >  ld/testsuite/ld-scripts/text-order-1c.d |  9 +++
> >  ld/testsuite/ld-scripts/text-order-1c.t |  5 ++
> >  ld/testsuite/ld-scripts/text-order.exp  | 42 ++++++++++++++
> >  17 files changed, 264 insertions(+), 11 deletions(-)
> >  create mode 100644 ld/testsuite/ld-scripts/start.s
> >  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.d
> >  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.s
> >  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.t
> >  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.d
> >  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.s
> >  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.t
> >  create mode 100644 ld/testsuite/ld-scripts/text-order-1c.d
> >  create mode 100644 ld/testsuite/ld-scripts/text-order-1c.t
> >  create mode 100644 ld/testsuite/ld-scripts/text-order.exp
> >
> > diff --git a/ld/ld.h b/ld/ld.h
> > index 54d9079678c..a80255a73ba 100644
> > --- a/ld/ld.h
> > +++ b/ld/ld.h
> > @@ -316,6 +316,9 @@ typedef struct
> >
> >    /* Compress DWARF debug sections.  */
> >    enum compressed_debug_section_type compress_debug;
> > +
> > +  /* The optional text section ordering file.  */
> > +  const char *text_section_ordering_file;
> >  } ld_config_type;
> >
> >  extern ld_config_type config;
> > diff --git a/ld/ldfile.c b/ld/ldfile.c
> > index dc9875d8813..6e9fbb7730f 100644
> > --- a/ld/ldfile.c
> > +++ b/ld/ldfile.c
> > @@ -736,7 +736,7 @@ ldfile_open_file (lang_input_statement_type *entry)
> >  /* Try to open NAME.  */
> >
> >  static FILE *
> > -try_open (const char *name, bool *sysrooted)
> > +try_open (const char *name, const char *orig_name, bool *sysrooted)
> >  {
> >    FILE *result;
> >
> > @@ -750,7 +750,34 @@ try_open (const char *name, bool *sysrooted)
> >
> >    if (verbose)
> >      {
> > -      if (result == NULL)
> > +      if (config.text_section_ordering_file != NULL
> > +         && strcmp (orig_name, config.text_section_ordering_file) == 0)
> > +       {
> > +         if (result == NULL)
> > +           info_msg (_("cannot find text section ordering file: %s\n"),
> > +                     name);
> > +         else
> > +           {
> > +             static const int ld_bufsz = 8193;
> > +             size_t n;
> > +             char *buf = (char *) xmalloc (ld_bufsz);
> > +
> > +             info_msg (_("opened text section ordering file: %s\n"),
> > +                       name);
> > +             info_msg ("==================================================\n");
> > +
> > +             while ((n = fread (buf, 1, ld_bufsz - 1, result)) > 0)
> > +               {
> > +                 buf[n] = 0;
> > +                 info_msg ("%s", buf);
> > +               }
> > +             rewind (result);
> > +             free (buf);
> > +
> > +             info_msg ("==================================================\n\n");
> > +           }
> > +       }
> > +      else if (result == NULL)
> >         info_msg (_("cannot find script file %s\n"), name);
> >        else
> >         info_msg (_("opened script file %s\n"), name);
> > @@ -832,7 +859,7 @@ ldfile_find_command_file (const char *name,
> >    if (!default_only)
> >      {
> >        /* First try raw name.  */
> > -      result = try_open (name, sysrooted);
> > +      result = try_open (name, name, sysrooted);
> >        if (result != NULL)
> >         return result;
> >      }
> > @@ -859,7 +886,7 @@ ldfile_find_command_file (const char *name,
> >         search = search->next)
> >      {
> >        path = concat (search->name, slash, name, (const char *) NULL);
> > -      result = try_open (path, sysrooted);
> > +      result = try_open (path, name, sysrooted);
> >        free (path);
> >        if (result)
> >         break;
> > @@ -908,6 +935,38 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
> >         }
> >      }
> >
> > +  /* Don't allow nested INCLUDE in the text section ordering file.  */
> > +  if (in_text_section_ordering_file)
> > +    {
> > +      einfo (_("%F%P: error: nested include '%s' in the text section "
> > +              "ordering file: '%s'\n"), name,
> > +              config.text_section_ordering_file);
> > +      return;
> > +    }
> > +
> > +  if (strcmp (name, "config.text_section_ordering_file") == 0)
> > +    {
> > +      /* Support
> > +
> > +        INCLUDE config.text_section_ordering_file;
> > +
> > +        in input text sections in linker script.  */
> > +      if (config.text_section_ordering_file == NULL)
> > +       {
> > +         /* Skip if the text section ordering file isn't specified.  */
> > +         lex_push_file (NULL, name, false);
> > +         return;
> > +       }
> > +
> > +      /* Load the text section ordering file.  */
> > +      name = config.text_section_ordering_file;
> > +
> > +      /* Set the in the text section ordering file marker.  */
> > +      in_text_section_ordering_file = 1;
> > +    }
> > +  else
> > +    in_text_section_ordering_file = 0;
> > +
> >    /* FIXME: This memory is never freed, but that should not really matter.
> >       It will be released when the linker exits, and it is unlikely to ever
> >       be more than a few tens of bytes.  */
> > @@ -932,7 +991,12 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
> >
> >    lineno = 1;
> >
> > -  saved_script_handle = ldlex_input_stack;
> > +  /* Clear the end of the include file marker.  */
> > +  seen_eof_include_file = 0;
> > +
> > +  /* The text section ordering file isn't a real linker script file.  */
> > +  if (!in_text_section_ordering_file)
> > +    saved_script_handle = ldlex_input_stack;
> >  }
> >
> >  /* Open command file NAME in the current directory, -L directories,
> > diff --git a/ld/ldlang.c b/ld/ldlang.c
> > index 229401c8342..d2b4d43d8ae 100644
> > --- a/ld/ldlang.c
> > +++ b/ld/ldlang.c
> > @@ -8512,12 +8512,43 @@ lang_add_wild (struct wildcard_spec *filespec,
> >
> >    if (filespec != NULL && filespec->name != NULL)
> >      {
> > -      if (strcmp (filespec->name, "*") == 0)
> > -       filespec->name = NULL;
> > -      else if (!wildcardp (filespec->name))
> > -       lang_has_input_file = true;
> > +      if (in_text_section_ordering_file != 0
> > +         && section_list == NULL
> > +         && filespec->name[0] == '.'
> > +         && filespec->sorted == none
> > +         && filespec->exclude_name_list == NULL
> > +         && filespec->section_flag_list == NULL
> > +         && !filespec->reversed)
> > +       {
> > +         /* When seeing an input file name without section list in the
> > +            text section ordering file and the file name starts with
> > +            '.', treat the filename as a section name and create a
> > +            wild card as the file name.  */
> > +         struct wildcard_list *single_section
> > +           = (struct wildcard_list *) xmalloc (sizeof (*single_section));
> > +         memset (single_section, 0, sizeof (*single_section));
> > +         single_section->spec.name = filespec->name;
> > +         single_section->spec.sorted = none;
> > +         /* A NULL indicates the wild card file name, "*".  */
> > +         filespec->name = NULL;
> > +         section_list = single_section;
> > +       }
> > +      else
> > +       {
> > +         if (strcmp (filespec->name, "*") == 0)
> > +           filespec->name = NULL;
> > +         else if (!wildcardp (filespec->name))
> > +           lang_has_input_file = true;
> > +       }
> >      }
> >
> > +  /* NB: Clear the in the text section ordering file marker after
> > +     processing the last entry when the end of the text section
> > +     ordering file is reached.  */
> > +  if (in_text_section_ordering_file != 0
> > +      && seen_eof_include_file != 0)
> > +    in_text_section_ordering_file = 0;
> > +
> >    new_stmt = new_stat (lang_wild_statement, stat_ptr);
> >    new_stmt->filename = NULL;
> >    new_stmt->filenames_sorted = false;
> > diff --git a/ld/ldlex.h b/ld/ldlex.h
> > index e5ac2fa7fca..a2c49656e1a 100644
> > --- a/ld/ldlex.h
> > +++ b/ld/ldlex.h
> > @@ -68,6 +68,7 @@ enum option_values
> >    OPTION_TASK_LINK,
> >    OPTION_TBSS,
> >    OPTION_TDATA,
> > +  OPTION_TEXT_SECTION_ORDERING_FILE,
> >    OPTION_TTEXT,
> >    OPTION_TTEXT_SEGMENT,
> >    OPTION_TRODATA_SEGMENT,
> > @@ -484,6 +485,8 @@ extern input_type parser_input;
> >
> >  extern unsigned int lineno;
> >  extern const char *lex_string;
> > +extern int in_text_section_ordering_file;
> > +extern int seen_eof_include_file;
> >
> >  /* In ldlex.l.  */
> >  extern int yylex (void);
> > diff --git a/ld/ldlex.l b/ld/ldlex.l
> > index e113c90812b..b41e1220661 100644
> > --- a/ld/ldlex.l
> > +++ b/ld/ldlex.l
> > @@ -43,6 +43,12 @@ input_type parser_input;
> >  /* Line number in the current input file.  */
> >  unsigned int lineno;
> >
> > +/* 1 if the current input file is the text section ordering file.  */
> > +int in_text_section_ordering_file = 0;
> > +
> > +/* 1 if the end of the include file is reached.  */
> > +int seen_eof_include_file = 0;
> > +
> >  /* The string we are currently lexing, or NULL if we are reading a
> >     file.  */
> >  const char *lex_string = NULL;
> > @@ -487,6 +493,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
> >
> >    lineno = lineno_stack[include_stack_ptr];
> >    input_flags.sysrooted = sysrooted_stack[include_stack_ptr];
> > +  seen_eof_include_file = 1;
> >
> >    return END;
> >  }
> > diff --git a/ld/lexsup.c b/ld/lexsup.c
> > index 099dff8ecde..21385628020 100644
> > --- a/ld/lexsup.c
> > +++ b/ld/lexsup.c
> > @@ -484,6 +484,9 @@ static const struct ld_option ld_options[] =
> >    { {"sort-section", required_argument, NULL, OPTION_SORT_SECTION},
> >      '\0', N_("name|alignment"),
> >      N_("Sort sections by name or maximum alignment"), TWO_DASHES },
> > +  { {"text-section-ordering-file", required_argument, NULL, OPTION_TEXT_SECTION_ORDERING_FILE},
> > +    '\0', N_("FILE"),
> > +    N_("Sort text sections by FILE"), TWO_DASHES },
> >    { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
> >      '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"),
> >      TWO_DASHES },
> > @@ -670,6 +673,7 @@ parse_args (unsigned argc, char **argv)
> >      dynamic_list
> >    } opt_dynamic_list = dynamic_list_unset;
> >    struct bfd_elf_dynamic_list *export_list = NULL;
> > +  bool seen_linker_script = false;
> >
> >    shortopts = (char *) xmalloc (OPTION_COUNT * 3 + 2);
> >    longopts = (struct option *)
> > @@ -1394,6 +1398,12 @@ parse_args (unsigned argc, char **argv)
> >             einfo (_("%F%P: invalid section sorting option: %s\n"),
> >                    optarg);
> >           break;
> > +       case OPTION_TEXT_SECTION_ORDERING_FILE:
> > +         if (seen_linker_script)
> > +           einfo (_("%F%P: --text-section-ordering-file must be placed"
> > +                    " before -T/--script\n"));
> > +         config.text_section_ordering_file = optarg;
> > +         break;
> >         case OPTION_STATS:
> >           config.stats = true;
> >           break;
> > @@ -1410,6 +1420,7 @@ parse_args (unsigned argc, char **argv)
> >           ++trace_files;
> >           break;
> >         case 'T':
> > +         seen_linker_script = true;
> >           previous_script_handle = saved_script_handle;
> >           ldfile_open_script_file (optarg);
> >           parser_input = input_script;
> > diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
> > index fae7c2ad71c..8014dfbfa27 100644
> > --- a/ld/scripttempl/elf.sc
> > +++ b/ld/scripttempl/elf.sc
> > @@ -553,11 +553,12 @@ cat <<EOF
> >    .text         ${RELOCATING-0} :
> >    {
> >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > +    ${RELOCATING+*(.text.hot .text.hot.*)}
> > +    ${RELOCATING+*(SORT(.text.sorted.*))}
> >      ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> >      ${RELOCATING+*(.text.exit .text.exit.*)}
> >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > -    ${RELOCATING+*(.text.hot .text.hot.*)}
> > -    ${RELOCATING+*(SORT(.text.sorted.*))}
> >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> >      /* .gnu.warning sections are handled specially by elf.em.  */
> >      *(.gnu.warning)
> > diff --git a/ld/testsuite/ld-scripts/start.s b/ld/testsuite/ld-scripts/start.s
> > new file mode 100644
> > index 00000000000..4d8239479a6
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/start.s
> > @@ -0,0 +1,12 @@
> > +       .text
> > +       .global start   /* Used by SH targets.  */
> > +start:
> > +       .global _start
> > +_start:
> > +       .global __start
> > +__start:
> > +       .global main    /* Used by HPPA targets.  */
> > +main:
> > +       .globl  _main   /* Used by LynxOS targets.  */
> > +_main:
> > +       .dc.a 0
> > diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
> > new file mode 100644
> > index 00000000000..87cee75055f
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/text-order-1a.d
> > @@ -0,0 +1,20 @@
> > +#source: text-order-1a.s
> > +#source: text-order-1b.s
> > +#source: start.s
> > +#ld: --text-section-ordering-file text-order-1a.t
> > +#nm: -n
> > +#target: [is_elf_format]
> > +#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > +# generic linker targets don't support --text-section-ordering-file
> > +
> > +#...
> > +[0-9a-f]+ T yyy
> > +#...
> > +[0-9a-f]+ T bar
> > +#...
> > +[0-9a-f]+ T _start
> > +#...
> > +[0-9a-f]+ T xxx
> > +#...
> > +[0-9a-f]+ T foo
> > +#pass
> > diff --git a/ld/testsuite/ld-scripts/text-order-1a.s b/ld/testsuite/ld-scripts/text-order-1a.s
> > new file mode 100644
> > index 00000000000..25f59b9e444
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/text-order-1a.s
> > @@ -0,0 +1,8 @@
> > +       .section .text.foo
> > +       .globl  foo
> > +foo:
> > +       .dc.a 0
> > +       .section .text.bar
> > +       .globl  bar
> > +bar:
> > +       .dc.a 0
> > diff --git a/ld/testsuite/ld-scripts/text-order-1a.t b/ld/testsuite/ld-scripts/text-order-1a.t
> > new file mode 100644
> > index 00000000000..72ea5f4cf46
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/text-order-1a.t
> > @@ -0,0 +1,5 @@
> > +*(.text.yyy)
> > +*(.text.b?r)
> > +*(.t*t)
> > +*(.text.xxx)
> > +*(.text.foo)
> > diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
> > new file mode 100644
> > index 00000000000..3f83813da2b
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/text-order-1b.d
> > @@ -0,0 +1,20 @@
> > +#source: text-order-1a.s
> > +#source: text-order-1b.s
> > +#source: start.s
> > +#ld: --text-section-ordering-file text-order-1b.t
> > +#nm: -n
> > +#target: [is_elf_format]
> > +#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > +# generic linker targets don't support --text-section-ordering-file
> > +
> > +#...
> > +[0-9a-f]+ T yyy
> > +#...
> > +[0-9a-f]+ T bar
> > +#...
> > +[0-9a-f]+ T _start
> > +#...
> > +[0-9a-f]+ T xxx
> > +#...
> > +[0-9a-f]+ T foo
> > +#pass
> > diff --git a/ld/testsuite/ld-scripts/text-order-1b.s b/ld/testsuite/ld-scripts/text-order-1b.s
> > new file mode 100644
> > index 00000000000..7ae27b9676a
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/text-order-1b.s
> > @@ -0,0 +1,8 @@
> > +       .section .text.xxx
> > +       .globl  xxx
> > +xxx:
> > +       .dc.a 0
> > +       .section .text.yyy
> > +       .globl  yyy
> > +yyy:
> > +       .dc.a 0
> > diff --git a/ld/testsuite/ld-scripts/text-order-1b.t b/ld/testsuite/ld-scripts/text-order-1b.t
> > new file mode 100644
> > index 00000000000..c1d092edb7e
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/text-order-1b.t
> > @@ -0,0 +1,4 @@
> > +.text.yyy
> > +.text.b?r
> > +.t*t
> > +.text.xxx .text.foo
> > diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
> > new file mode 100644
> > index 00000000000..0c484637784
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/text-order-1c.d
> > @@ -0,0 +1,9 @@
> > +#source: text-order-1a.s
> > +#source: text-order-1b.s
> > +#source: start.s
> > +#ld: --text-section-ordering-file text-order-1c.t
> > +#nm: -n
> > +#target: [is_elf_format]
> > +#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > +# generic linker targets don't support --text-section-ordering-file
> > +#error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
> > diff --git a/ld/testsuite/ld-scripts/text-order-1c.t b/ld/testsuite/ld-scripts/text-order-1c.t
> > new file mode 100644
> > index 00000000000..9ecbfb42826
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/text-order-1c.t
> > @@ -0,0 +1,5 @@
> > +.text.yyy
> > +.text.b?r
> > +INCLUDE text-order-1b.t
> > +.t*t
> > +.text.xxx .text.foo
> > diff --git a/ld/testsuite/ld-scripts/text-order.exp b/ld/testsuite/ld-scripts/text-order.exp
> > new file mode 100644
> > index 00000000000..30cb531137e
> > --- /dev/null
> > +++ b/ld/testsuite/ld-scripts/text-order.exp
> > @@ -0,0 +1,42 @@
> > +# Test for --text-section-ordering-file FILE.
> > +# Copyright (C) 2024 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.
> > +
> > +if { !([is_elf_format] && ![is_generic]) } {
> > +    return
> > +}
> > +
> > +set old_ldflags $LDFLAGS
> > +if { [istarget spu*-*-*] } then {
> > +    set LDFLAGS "$LDFLAGS --local-store 0:0 --no-overlays"
> > +} elseif { [is_pecoff_format] } then {
> > +    set LDFLAGS "$LDFLAGS --image-base 0"
> > +} elseif { [is_xcoff_format] } then {
> > +    set LDFLAGS "$LDFLAGS -bnogc"
> > +}
> > +
> > +set test_list [lsort [glob -nocomplain $srcdir/$subdir/text-order*.d]]
> > +foreach test_file $test_list {
> > +    set test_name [file rootname $test_file]
> > +    set map_file "tmpdir/[file tail $test_name].map"
> > +    verbose $test_name
> > +    run_dump_test $test_name
> > +}
> > +
> > +set LDFLAGS $old_ldflags
> > --
> > 2.43.0
> >



-- 
H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE
  2024-01-19 22:13     ` H.J. Lu
@ 2024-01-19 23:15       ` Fangrui Song
  2024-01-22  7:37         ` Jan Beulich
       [not found]       ` <DS7PR12MB5765059CCA55EC755F8132ACCB702@DS7PR12MB5765.namprd12.prod.outlook.com>
  1 sibling, 1 reply; 19+ messages in thread
From: Fangrui Song @ 2024-01-19 23:15 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils, goldstein.w.n

On Fri, Jan 19, 2024 at 2:13 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Fri, Jan 19, 2024 at 1:57 PM Fangrui Song <i@maskray.me> wrote:
> >
> > On Fri, Jan 19, 2024 at 11:45 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> > >
> > > Add --text-section-ordering-file FILE to specify the text section
> > > ordering file.  Inside the text section ordering file, when seeing an
> > > input file name without section list and the file name starts with '.',
> > > treat the filename as a section name and create a wild card as the file
> > > name.
> > >
> > > A text section ordering file which contains text section wildcards:
> > >
> > > ---
> > > text_section_foo*
> > > text_section_b?r
> > > text_section_name
> > > ---
> > >
> > > can be used to group input text sections together to:
> > >
> > > 1. Reduce gaps between text sections.
> > > 2. Put hot text sections close to each other.
> > >
> > > Text sections in the text section ordering file are placed at the
> > > beginning of the output text section and code text sections are moved
> > > toward the end so that the first section in the text section ordering
> > > file is aligned to the output section alignment.
> > >
> > > --text-section-ordering-file must be placed before -T/--script option
> > > so that the text section ordering file can always be included in linker
> > > scripts.  Nested INCLUDE in the text section order file is disallowed.
> > >
> > > NB: Gold has the command-line option, --section-ordering-file FILE, to
> > > layout sections in the order specified.  --text-section-ordering-file
> > > supports the same section ordering file format, but it applies only to
> > > text sections.
> >
> > Thanks for adding the feature to ld! However, I think a
> > symbol-oriented option likely works better than a section-oriented
> > option.
> >
> > https://maskray.me/blog/2020-11-15-explain-gnu-linker-options#symbol-ordering-filefile
> >
> > This option (--symbol-ordering-file=) is unique to ld.lld. gold has a
> > --section-ordering-file, sorted by section name. In practice, text and
> > data sections mostly have different names. However, clang
> > -fno-unique-section-names (GCC feature request
> > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95095) can create
> > sections of the same that defeat --section-ordering-file.
>
> --text-section-ordering-file is implemented on the existing linker framework.
> A symbol-oriented option will require major changes in ld.



> > >
> > >         * ld.h (ld_config_type): Add text_section_ordering_file.
> > >         * ldfile.c (in_text_section_ordering_file): New.
> > >         (try_open): Add an argument for the original file name.  Dump
> > >         the text section order file for verbose output.
> > >         (ldfile_find_command_file): Pass the original file name to
> > >         try_open.
> > >         (ldfile_open_command_file_1): Don't allow nested INCLUDE in the
> > >         text section order file.  If the text section order file is
> > >         specified, set in_text_section_ordering_file, load the file and
> > >         don't set saved_script_handle.  Clear seen_eof_include_file.
> > >         * ldlang.c (lang_add_wild): When seeing an input file name
> > >         without section list and the file name starts with '.', treat
> > >         the filename as a section name and create a wild card as the
> > >         file name.
> > >         * ldlex.h (option_values): Add OPTION_TEXT_SECTION_ORDERING_FILE.
> > >         (in_text_section_ordering_file): New.
> > >         (seen_eof_include_file): Likewise.
> > >         * ldlex.l (in_text_section_ordering_file): New.
> > >         (seen_eof_include_file): Likewise.
> > >         (<<EOF>>): Set seen_eof_include_file.
> > >         * lexsup.c (ld_options): Document --text-section-ordering-file.
> > >         (parse_args): Handle OPTION_TEXT_SECTION_ORDERING_FILE.  Issue
> > >         an error if --text-section-ordering-file is placed after
> > >         -T/--script.
> > >         * scripttempl/elf.sc: Include the text section order file and
> > >         place code text sections toward the end.
> > >         * testsuite/ld-scripts/start.s: New file.
> > >         * testsuite/ld-scripts/text-order-1a.d: New file.
> > >         * testsuite/ld-scripts/text-order-1a.s: Likewise.
> > >         * testsuite/ld-scripts/text-order-1a.t: Likewise.
> > >         * testsuite/ld-scripts/text-order-1b.d: Likewise.
> > >         * testsuite/ld-scripts/text-order-1b.s: Likewise.
> > >         * testsuite/ld-scripts/text-order-1b.t: Likewise.
> > >         * testsuite/ld-scripts/text-order-1c.d: Likewise.
> > >         * testsuite/ld-scripts/text-order-1d.t: Likewise.
> > >         * testsuite/ld-scripts/text-order.exp: Likewise.
> > > ---
> > >  ld/ld.h                                 |  3 +
> > >  ld/ldfile.c                             | 74 +++++++++++++++++++++++--
> > >  ld/ldlang.c                             | 39 +++++++++++--
> > >  ld/ldlex.h                              |  3 +
> > >  ld/ldlex.l                              |  7 +++
> > >  ld/lexsup.c                             | 11 ++++
> > >  ld/scripttempl/elf.sc                   |  5 +-
> > >  ld/testsuite/ld-scripts/start.s         | 12 ++++
> > >  ld/testsuite/ld-scripts/text-order-1a.d | 20 +++++++
> > >  ld/testsuite/ld-scripts/text-order-1a.s |  8 +++
> > >  ld/testsuite/ld-scripts/text-order-1a.t |  5 ++
> > >  ld/testsuite/ld-scripts/text-order-1b.d | 20 +++++++
> > >  ld/testsuite/ld-scripts/text-order-1b.s |  8 +++
> > >  ld/testsuite/ld-scripts/text-order-1b.t |  4 ++
> > >  ld/testsuite/ld-scripts/text-order-1c.d |  9 +++
> > >  ld/testsuite/ld-scripts/text-order-1c.t |  5 ++
> > >  ld/testsuite/ld-scripts/text-order.exp  | 42 ++++++++++++++
> > >  17 files changed, 264 insertions(+), 11 deletions(-)
> > >  create mode 100644 ld/testsuite/ld-scripts/start.s
> > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.d
> > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.s
> > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.t
> > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.d
> > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.s
> > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.t
> > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1c.d
> > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1c.t
> > >  create mode 100644 ld/testsuite/ld-scripts/text-order.exp
> > >
> > > diff --git a/ld/ld.h b/ld/ld.h
> > > index 54d9079678c..a80255a73ba 100644
> > > --- a/ld/ld.h
> > > +++ b/ld/ld.h
> > > @@ -316,6 +316,9 @@ typedef struct
> > >
> > >    /* Compress DWARF debug sections.  */
> > >    enum compressed_debug_section_type compress_debug;
> > > +
> > > +  /* The optional text section ordering file.  */
> > > +  const char *text_section_ordering_file;
> > >  } ld_config_type;
> > >
> > >  extern ld_config_type config;
> > > diff --git a/ld/ldfile.c b/ld/ldfile.c
> > > index dc9875d8813..6e9fbb7730f 100644
> > > --- a/ld/ldfile.c
> > > +++ b/ld/ldfile.c
> > > @@ -736,7 +736,7 @@ ldfile_open_file (lang_input_statement_type *entry)
> > >  /* Try to open NAME.  */
> > >
> > >  static FILE *
> > > -try_open (const char *name, bool *sysrooted)
> > > +try_open (const char *name, const char *orig_name, bool *sysrooted)
> > >  {
> > >    FILE *result;
> > >
> > > @@ -750,7 +750,34 @@ try_open (const char *name, bool *sysrooted)
> > >
> > >    if (verbose)
> > >      {
> > > -      if (result == NULL)
> > > +      if (config.text_section_ordering_file != NULL
> > > +         && strcmp (orig_name, config.text_section_ordering_file) == 0)
> > > +       {
> > > +         if (result == NULL)
> > > +           info_msg (_("cannot find text section ordering file: %s\n"),
> > > +                     name);
> > > +         else
> > > +           {
> > > +             static const int ld_bufsz = 8193;
> > > +             size_t n;
> > > +             char *buf = (char *) xmalloc (ld_bufsz);
> > > +
> > > +             info_msg (_("opened text section ordering file: %s\n"),
> > > +                       name);
> > > +             info_msg ("==================================================\n");
> > > +
> > > +             while ((n = fread (buf, 1, ld_bufsz - 1, result)) > 0)
> > > +               {
> > > +                 buf[n] = 0;
> > > +                 info_msg ("%s", buf);
> > > +               }
> > > +             rewind (result);
> > > +             free (buf);
> > > +
> > > +             info_msg ("==================================================\n\n");
> > > +           }
> > > +       }
> > > +      else if (result == NULL)
> > >         info_msg (_("cannot find script file %s\n"), name);
> > >        else
> > >         info_msg (_("opened script file %s\n"), name);
> > > @@ -832,7 +859,7 @@ ldfile_find_command_file (const char *name,
> > >    if (!default_only)
> > >      {
> > >        /* First try raw name.  */
> > > -      result = try_open (name, sysrooted);
> > > +      result = try_open (name, name, sysrooted);
> > >        if (result != NULL)
> > >         return result;
> > >      }
> > > @@ -859,7 +886,7 @@ ldfile_find_command_file (const char *name,
> > >         search = search->next)
> > >      {
> > >        path = concat (search->name, slash, name, (const char *) NULL);
> > > -      result = try_open (path, sysrooted);
> > > +      result = try_open (path, name, sysrooted);
> > >        free (path);
> > >        if (result)
> > >         break;
> > > @@ -908,6 +935,38 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
> > >         }
> > >      }
> > >
> > > +  /* Don't allow nested INCLUDE in the text section ordering file.  */
> > > +  if (in_text_section_ordering_file)
> > > +    {
> > > +      einfo (_("%F%P: error: nested include '%s' in the text section "
> > > +              "ordering file: '%s'\n"), name,
> > > +              config.text_section_ordering_file);
> > > +      return;
> > > +    }
> > > +
> > > +  if (strcmp (name, "config.text_section_ordering_file") == 0)
> > > +    {
> > > +      /* Support
> > > +
> > > +        INCLUDE config.text_section_ordering_file;
> > > +
> > > +        in input text sections in linker script.  */
> > > +      if (config.text_section_ordering_file == NULL)
> > > +       {
> > > +         /* Skip if the text section ordering file isn't specified.  */
> > > +         lex_push_file (NULL, name, false);
> > > +         return;
> > > +       }
> > > +
> > > +      /* Load the text section ordering file.  */
> > > +      name = config.text_section_ordering_file;
> > > +
> > > +      /* Set the in the text section ordering file marker.  */
> > > +      in_text_section_ordering_file = 1;
> > > +    }
> > > +  else
> > > +    in_text_section_ordering_file = 0;
> > > +
> > >    /* FIXME: This memory is never freed, but that should not really matter.
> > >       It will be released when the linker exits, and it is unlikely to ever
> > >       be more than a few tens of bytes.  */
> > > @@ -932,7 +991,12 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
> > >
> > >    lineno = 1;
> > >
> > > -  saved_script_handle = ldlex_input_stack;
> > > +  /* Clear the end of the include file marker.  */
> > > +  seen_eof_include_file = 0;
> > > +
> > > +  /* The text section ordering file isn't a real linker script file.  */
> > > +  if (!in_text_section_ordering_file)
> > > +    saved_script_handle = ldlex_input_stack;
> > >  }
> > >
> > >  /* Open command file NAME in the current directory, -L directories,
> > > diff --git a/ld/ldlang.c b/ld/ldlang.c
> > > index 229401c8342..d2b4d43d8ae 100644
> > > --- a/ld/ldlang.c
> > > +++ b/ld/ldlang.c
> > > @@ -8512,12 +8512,43 @@ lang_add_wild (struct wildcard_spec *filespec,
> > >
> > >    if (filespec != NULL && filespec->name != NULL)
> > >      {
> > > -      if (strcmp (filespec->name, "*") == 0)
> > > -       filespec->name = NULL;
> > > -      else if (!wildcardp (filespec->name))
> > > -       lang_has_input_file = true;
> > > +      if (in_text_section_ordering_file != 0
> > > +         && section_list == NULL
> > > +         && filespec->name[0] == '.'
> > > +         && filespec->sorted == none
> > > +         && filespec->exclude_name_list == NULL
> > > +         && filespec->section_flag_list == NULL
> > > +         && !filespec->reversed)
> > > +       {
> > > +         /* When seeing an input file name without section list in the
> > > +            text section ordering file and the file name starts with
> > > +            '.', treat the filename as a section name and create a
> > > +            wild card as the file name.  */
> > > +         struct wildcard_list *single_section
> > > +           = (struct wildcard_list *) xmalloc (sizeof (*single_section));
> > > +         memset (single_section, 0, sizeof (*single_section));
> > > +         single_section->spec.name = filespec->name;
> > > +         single_section->spec.sorted = none;
> > > +         /* A NULL indicates the wild card file name, "*".  */
> > > +         filespec->name = NULL;
> > > +         section_list = single_section;
> > > +       }
> > > +      else
> > > +       {
> > > +         if (strcmp (filespec->name, "*") == 0)
> > > +           filespec->name = NULL;
> > > +         else if (!wildcardp (filespec->name))
> > > +           lang_has_input_file = true;
> > > +       }
> > >      }
> > >
> > > +  /* NB: Clear the in the text section ordering file marker after
> > > +     processing the last entry when the end of the text section
> > > +     ordering file is reached.  */
> > > +  if (in_text_section_ordering_file != 0
> > > +      && seen_eof_include_file != 0)
> > > +    in_text_section_ordering_file = 0;
> > > +
> > >    new_stmt = new_stat (lang_wild_statement, stat_ptr);
> > >    new_stmt->filename = NULL;
> > >    new_stmt->filenames_sorted = false;
> > > diff --git a/ld/ldlex.h b/ld/ldlex.h
> > > index e5ac2fa7fca..a2c49656e1a 100644
> > > --- a/ld/ldlex.h
> > > +++ b/ld/ldlex.h
> > > @@ -68,6 +68,7 @@ enum option_values
> > >    OPTION_TASK_LINK,
> > >    OPTION_TBSS,
> > >    OPTION_TDATA,
> > > +  OPTION_TEXT_SECTION_ORDERING_FILE,
> > >    OPTION_TTEXT,
> > >    OPTION_TTEXT_SEGMENT,
> > >    OPTION_TRODATA_SEGMENT,
> > > @@ -484,6 +485,8 @@ extern input_type parser_input;
> > >
> > >  extern unsigned int lineno;
> > >  extern const char *lex_string;
> > > +extern int in_text_section_ordering_file;
> > > +extern int seen_eof_include_file;
> > >
> > >  /* In ldlex.l.  */
> > >  extern int yylex (void);
> > > diff --git a/ld/ldlex.l b/ld/ldlex.l
> > > index e113c90812b..b41e1220661 100644
> > > --- a/ld/ldlex.l
> > > +++ b/ld/ldlex.l
> > > @@ -43,6 +43,12 @@ input_type parser_input;
> > >  /* Line number in the current input file.  */
> > >  unsigned int lineno;
> > >
> > > +/* 1 if the current input file is the text section ordering file.  */
> > > +int in_text_section_ordering_file = 0;
> > > +
> > > +/* 1 if the end of the include file is reached.  */
> > > +int seen_eof_include_file = 0;
> > > +
> > >  /* The string we are currently lexing, or NULL if we are reading a
> > >     file.  */
> > >  const char *lex_string = NULL;
> > > @@ -487,6 +493,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
> > >
> > >    lineno = lineno_stack[include_stack_ptr];
> > >    input_flags.sysrooted = sysrooted_stack[include_stack_ptr];
> > > +  seen_eof_include_file = 1;
> > >
> > >    return END;
> > >  }
> > > diff --git a/ld/lexsup.c b/ld/lexsup.c
> > > index 099dff8ecde..21385628020 100644
> > > --- a/ld/lexsup.c
> > > +++ b/ld/lexsup.c
> > > @@ -484,6 +484,9 @@ static const struct ld_option ld_options[] =
> > >    { {"sort-section", required_argument, NULL, OPTION_SORT_SECTION},
> > >      '\0', N_("name|alignment"),
> > >      N_("Sort sections by name or maximum alignment"), TWO_DASHES },
> > > +  { {"text-section-ordering-file", required_argument, NULL, OPTION_TEXT_SECTION_ORDERING_FILE},
> > > +    '\0', N_("FILE"),
> > > +    N_("Sort text sections by FILE"), TWO_DASHES },
> > >    { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
> > >      '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"),
> > >      TWO_DASHES },
> > > @@ -670,6 +673,7 @@ parse_args (unsigned argc, char **argv)
> > >      dynamic_list
> > >    } opt_dynamic_list = dynamic_list_unset;
> > >    struct bfd_elf_dynamic_list *export_list = NULL;
> > > +  bool seen_linker_script = false;
> > >
> > >    shortopts = (char *) xmalloc (OPTION_COUNT * 3 + 2);
> > >    longopts = (struct option *)
> > > @@ -1394,6 +1398,12 @@ parse_args (unsigned argc, char **argv)
> > >             einfo (_("%F%P: invalid section sorting option: %s\n"),
> > >                    optarg);
> > >           break;
> > > +       case OPTION_TEXT_SECTION_ORDERING_FILE:
> > > +         if (seen_linker_script)
> > > +           einfo (_("%F%P: --text-section-ordering-file must be placed"
> > > +                    " before -T/--script\n"));
> > > +         config.text_section_ordering_file = optarg;
> > > +         break;
> > >         case OPTION_STATS:
> > >           config.stats = true;
> > >           break;
> > > @@ -1410,6 +1420,7 @@ parse_args (unsigned argc, char **argv)
> > >           ++trace_files;
> > >           break;
> > >         case 'T':
> > > +         seen_linker_script = true;
> > >           previous_script_handle = saved_script_handle;
> > >           ldfile_open_script_file (optarg);
> > >           parser_input = input_script;
> > > diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
> > > index fae7c2ad71c..8014dfbfa27 100644
> > > --- a/ld/scripttempl/elf.sc
> > > +++ b/ld/scripttempl/elf.sc
> > > @@ -553,11 +553,12 @@ cat <<EOF
> > >    .text         ${RELOCATING-0} :
> > >    {
> > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > +    ${RELOCATING+*(.text.hot .text.hot.*)}
> > > +    ${RELOCATING+*(SORT(.text.sorted.*))}
> > >      ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > >      ${RELOCATING+*(.text.exit .text.exit.*)}
> > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > > -    ${RELOCATING+*(.text.hot .text.hot.*)}
> > > -    ${RELOCATING+*(SORT(.text.sorted.*))}
> > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > >      *(.gnu.warning)

I see the inflexibility of the current framework.
--text-section-ordering-file seems less general as I'd expect, and
placing ${RELOCATING+INCLUDE config.text_section_ordering_file} beside
the existing text input section descriptions does not make things more
flexible.
It is a shame that the new feature only applies to .text . Data
sections and other code sections should be reorderable as well.

I believe tuning the semantics can make it more useful.
lld's -z keep-text-section-prefix and --symbol-ordering-file= work the
following way:

* When -z keep-text-section-prefix is specified, .text.hot.* sections
are placed into the output section .text.hot. .text.unlikely,
.text.startup, etc are similar. Otherwise, all .text.* go to .text.
* There is no fixed order among the text output sections. The first
seen one wins. https://github.com/llvm/llvm-project/blob/main/lld/test/ELF/text-section-prefix.s
  If the user add `asm(".section .text.hot,\"axR\",%progbits");
asm(".section .text.unlikely,\"axR\",%progbits");` to the crtbegin
source file, the output section order can be fixed in a desired way,
regardless of whether .text.startup/.text.hot appears first in
application code.
* Normally a wildcard pattern in an input section description (e.g.
*(.text .text.*)) uses the input order. However, if
--symbol-ordering-file= is specified, it overrides the order.
https://github.com/llvm/llvm-project/blob/main/lld/test/ELF/symbol-ordering-file.s

Back to GNU ld, I wonder whether we can say, when
config.text_section_ordering_file is present, drop all the existing
rules (e.g. ${RELOCATING+*(.text.exit .text.exit.*)}), and keep just
*(.text .text.*).
Make the ordering file decide how the wildcard is ordered.

> > > diff --git a/ld/testsuite/ld-scripts/start.s b/ld/testsuite/ld-scripts/start.s
> > > new file mode 100644
> > > index 00000000000..4d8239479a6
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/start.s
> > > @@ -0,0 +1,12 @@
> > > +       .text
> > > +       .global start   /* Used by SH targets.  */
> > > +start:
> > > +       .global _start
> > > +_start:
> > > +       .global __start
> > > +__start:
> > > +       .global main    /* Used by HPPA targets.  */
> > > +main:
> > > +       .globl  _main   /* Used by LynxOS targets.  */
> > > +_main:
> > > +       .dc.a 0
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1a.d b/ld/testsuite/ld-scripts/text-order-1a.d
> > > new file mode 100644
> > > index 00000000000..87cee75055f
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/text-order-1a.d
> > > @@ -0,0 +1,20 @@
> > > +#source: text-order-1a.s
> > > +#source: text-order-1b.s
> > > +#source: start.s
> > > +#ld: --text-section-ordering-file text-order-1a.t
> > > +#nm: -n
> > > +#target: [is_elf_format]
> > > +#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > +# generic linker targets don't support --text-section-ordering-file
> > > +
> > > +#...
> > > +[0-9a-f]+ T yyy
> > > +#...
> > > +[0-9a-f]+ T bar
> > > +#...
> > > +[0-9a-f]+ T _start
> > > +#...
> > > +[0-9a-f]+ T xxx
> > > +#...
> > > +[0-9a-f]+ T foo
> > > +#pass
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1a.s b/ld/testsuite/ld-scripts/text-order-1a.s
> > > new file mode 100644
> > > index 00000000000..25f59b9e444
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/text-order-1a.s
> > > @@ -0,0 +1,8 @@
> > > +       .section .text.foo
> > > +       .globl  foo
> > > +foo:
> > > +       .dc.a 0
> > > +       .section .text.bar
> > > +       .globl  bar
> > > +bar:
> > > +       .dc.a 0
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1a.t b/ld/testsuite/ld-scripts/text-order-1a.t
> > > new file mode 100644
> > > index 00000000000..72ea5f4cf46
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/text-order-1a.t
> > > @@ -0,0 +1,5 @@
> > > +*(.text.yyy)
> > > +*(.text.b?r)
> > > +*(.t*t)
> > > +*(.text.xxx)
> > > +*(.text.foo)
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1b.d b/ld/testsuite/ld-scripts/text-order-1b.d
> > > new file mode 100644
> > > index 00000000000..3f83813da2b
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/text-order-1b.d
> > > @@ -0,0 +1,20 @@
> > > +#source: text-order-1a.s
> > > +#source: text-order-1b.s
> > > +#source: start.s
> > > +#ld: --text-section-ordering-file text-order-1b.t
> > > +#nm: -n
> > > +#target: [is_elf_format]
> > > +#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > +# generic linker targets don't support --text-section-ordering-file
> > > +
> > > +#...
> > > +[0-9a-f]+ T yyy
> > > +#...
> > > +[0-9a-f]+ T bar
> > > +#...
> > > +[0-9a-f]+ T _start
> > > +#...
> > > +[0-9a-f]+ T xxx
> > > +#...
> > > +[0-9a-f]+ T foo
> > > +#pass
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1b.s b/ld/testsuite/ld-scripts/text-order-1b.s
> > > new file mode 100644
> > > index 00000000000..7ae27b9676a
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/text-order-1b.s
> > > @@ -0,0 +1,8 @@
> > > +       .section .text.xxx
> > > +       .globl  xxx
> > > +xxx:
> > > +       .dc.a 0
> > > +       .section .text.yyy
> > > +       .globl  yyy
> > > +yyy:
> > > +       .dc.a 0
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1b.t b/ld/testsuite/ld-scripts/text-order-1b.t
> > > new file mode 100644
> > > index 00000000000..c1d092edb7e
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/text-order-1b.t
> > > @@ -0,0 +1,4 @@
> > > +.text.yyy
> > > +.text.b?r
> > > +.t*t
> > > +.text.xxx .text.foo
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1c.d b/ld/testsuite/ld-scripts/text-order-1c.d
> > > new file mode 100644
> > > index 00000000000..0c484637784
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/text-order-1c.d
> > > @@ -0,0 +1,9 @@
> > > +#source: text-order-1a.s
> > > +#source: text-order-1b.s
> > > +#source: start.s
> > > +#ld: --text-section-ordering-file text-order-1c.t
> > > +#nm: -n
> > > +#target: [is_elf_format]
> > > +#xfail: [is_generic] hppa64-*-* mep-*-* mn10200-*-*
> > > +# generic linker targets don't support --text-section-ordering-file
> > > +#error: .*: nested include 'text-order-1b.t' in the text section ordering file: 'text-order-1c.t'
> > > diff --git a/ld/testsuite/ld-scripts/text-order-1c.t b/ld/testsuite/ld-scripts/text-order-1c.t
> > > new file mode 100644
> > > index 00000000000..9ecbfb42826
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/text-order-1c.t
> > > @@ -0,0 +1,5 @@
> > > +.text.yyy
> > > +.text.b?r
> > > +INCLUDE text-order-1b.t
> > > +.t*t
> > > +.text.xxx .text.foo
> > > diff --git a/ld/testsuite/ld-scripts/text-order.exp b/ld/testsuite/ld-scripts/text-order.exp
> > > new file mode 100644
> > > index 00000000000..30cb531137e
> > > --- /dev/null
> > > +++ b/ld/testsuite/ld-scripts/text-order.exp
> > > @@ -0,0 +1,42 @@
> > > +# Test for --text-section-ordering-file FILE.
> > > +# Copyright (C) 2024 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.
> > > +
> > > +if { !([is_elf_format] && ![is_generic]) } {
> > > +    return
> > > +}
> > > +
> > > +set old_ldflags $LDFLAGS
> > > +if { [istarget spu*-*-*] } then {
> > > +    set LDFLAGS "$LDFLAGS --local-store 0:0 --no-overlays"
> > > +} elseif { [is_pecoff_format] } then {
> > > +    set LDFLAGS "$LDFLAGS --image-base 0"
> > > +} elseif { [is_xcoff_format] } then {
> > > +    set LDFLAGS "$LDFLAGS -bnogc"
> > > +}
> > > +
> > > +set test_list [lsort [glob -nocomplain $srcdir/$subdir/text-order*.d]]
> > > +foreach test_file $test_list {
> > > +    set test_name [file rootname $test_file]
> > > +    set map_file "tmpdir/[file tail $test_name].map"
> > > +    verbose $test_name
> > > +    run_dump_test $test_name
> > > +}
> > > +
> > > +set LDFLAGS $old_ldflags
> > > --
> > > 2.43.0
> > >
>
>
>
> --
> H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE
       [not found]       ` <DS7PR12MB5765059CCA55EC755F8132ACCB702@DS7PR12MB5765.namprd12.prod.outlook.com>
@ 2024-01-20 14:12         ` H.J. Lu
  0 siblings, 0 replies; 19+ messages in thread
From: H.J. Lu @ 2024-01-20 14:12 UTC (permalink / raw)
  To: Fangrui Song; +Cc: binutils, goldstein.w.n

On Fri, Jan 19, 2024 at 3:15 PM Fangrui Song <i@maskray.me> wrote:
>
> On Fri, Jan 19, 2024 at 2:13 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > On Fri, Jan 19, 2024 at 1:57 PM Fangrui Song <i@maskray.me> wrote:
> > >
> > > On Fri, Jan 19, 2024 at 11:45 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> > > >
> > > > Add --text-section-ordering-file FILE to specify the text section
> > > > ordering file.  Inside the text section ordering file, when seeing an
> > > > input file name without section list and the file name starts with '.',
> > > > treat the filename as a section name and create a wild card as the file
> > > > name.
> > > >
> > > > A text section ordering file which contains text section wildcards:
> > > >
> > > > ---
> > > > text_section_foo*
> > > > text_section_b?r
> > > > text_section_name
> > > > ---
> > > >
> > > > can be used to group input text sections together to:
> > > >
> > > > 1. Reduce gaps between text sections.
> > > > 2. Put hot text sections close to each other.
> > > >
> > > > Text sections in the text section ordering file are placed at the
> > > > beginning of the output text section and code text sections are moved
> > > > toward the end so that the first section in the text section ordering
> > > > file is aligned to the output section alignment.
> > > >
> > > > --text-section-ordering-file must be placed before -T/--script option
> > > > so that the text section ordering file can always be included in linker
> > > > scripts.  Nested INCLUDE in the text section order file is disallowed.
> > > >
> > > > NB: Gold has the command-line option, --section-ordering-file FILE, to
> > > > layout sections in the order specified.  --text-section-ordering-file
> > > > supports the same section ordering file format, but it applies only to
> > > > text sections.
> > >
> > > Thanks for adding the feature to ld! However, I think a
> > > symbol-oriented option likely works better than a section-oriented
> > > option.
> > >
> > > https://maskray.me/blog/2020-11-15-explain-gnu-linker-options#symbol-ordering-filefile
> > >
> > > This option (--symbol-ordering-file=) is unique to ld.lld. gold has a
> > > --section-ordering-file, sorted by section name. In practice, text and
> > > data sections mostly have different names. However, clang
> > > -fno-unique-section-names (GCC feature request
> > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95095) can create
> > > sections of the same that defeat --section-ordering-file.
> >
> > --text-section-ordering-file is implemented on the existing linker framework.
> > A symbol-oriented option will require major changes in ld.
>
>
>
> > > >
> > > >         * ld.h (ld_config_type): Add text_section_ordering_file.
> > > >         * ldfile.c (in_text_section_ordering_file): New.
> > > >         (try_open): Add an argument for the original file name.  Dump
> > > >         the text section order file for verbose output.
> > > >         (ldfile_find_command_file): Pass the original file name to
> > > >         try_open.
> > > >         (ldfile_open_command_file_1): Don't allow nested INCLUDE in the
> > > >         text section order file.  If the text section order file is
> > > >         specified, set in_text_section_ordering_file, load the file and
> > > >         don't set saved_script_handle.  Clear seen_eof_include_file.
> > > >         * ldlang.c (lang_add_wild): When seeing an input file name
> > > >         without section list and the file name starts with '.', treat
> > > >         the filename as a section name and create a wild card as the
> > > >         file name.
> > > >         * ldlex.h (option_values): Add OPTION_TEXT_SECTION_ORDERING_FILE.
> > > >         (in_text_section_ordering_file): New.
> > > >         (seen_eof_include_file): Likewise.
> > > >         * ldlex.l (in_text_section_ordering_file): New.
> > > >         (seen_eof_include_file): Likewise.
> > > >         (<<EOF>>): Set seen_eof_include_file.
> > > >         * lexsup.c (ld_options): Document --text-section-ordering-file.
> > > >         (parse_args): Handle OPTION_TEXT_SECTION_ORDERING_FILE.  Issue
> > > >         an error if --text-section-ordering-file is placed after
> > > >         -T/--script.
> > > >         * scripttempl/elf.sc: Include the text section order file and
> > > >         place code text sections toward the end.
> > > >         * testsuite/ld-scripts/start.s: New file.
> > > >         * testsuite/ld-scripts/text-order-1a.d: New file.
> > > >         * testsuite/ld-scripts/text-order-1a.s: Likewise.
> > > >         * testsuite/ld-scripts/text-order-1a.t: Likewise.
> > > >         * testsuite/ld-scripts/text-order-1b.d: Likewise.
> > > >         * testsuite/ld-scripts/text-order-1b.s: Likewise.
> > > >         * testsuite/ld-scripts/text-order-1b.t: Likewise.
> > > >         * testsuite/ld-scripts/text-order-1c.d: Likewise.
> > > >         * testsuite/ld-scripts/text-order-1d.t: Likewise.
> > > >         * testsuite/ld-scripts/text-order.exp: Likewise.
> > > > ---
> > > >  ld/ld.h                                 |  3 +
> > > >  ld/ldfile.c                             | 74 +++++++++++++++++++++++--
> > > >  ld/ldlang.c                             | 39 +++++++++++--
> > > >  ld/ldlex.h                              |  3 +
> > > >  ld/ldlex.l                              |  7 +++
> > > >  ld/lexsup.c                             | 11 ++++
> > > >  ld/scripttempl/elf.sc                   |  5 +-
> > > >  ld/testsuite/ld-scripts/start.s         | 12 ++++
> > > >  ld/testsuite/ld-scripts/text-order-1a.d | 20 +++++++
> > > >  ld/testsuite/ld-scripts/text-order-1a.s |  8 +++
> > > >  ld/testsuite/ld-scripts/text-order-1a.t |  5 ++
> > > >  ld/testsuite/ld-scripts/text-order-1b.d | 20 +++++++
> > > >  ld/testsuite/ld-scripts/text-order-1b.s |  8 +++
> > > >  ld/testsuite/ld-scripts/text-order-1b.t |  4 ++
> > > >  ld/testsuite/ld-scripts/text-order-1c.d |  9 +++
> > > >  ld/testsuite/ld-scripts/text-order-1c.t |  5 ++
> > > >  ld/testsuite/ld-scripts/text-order.exp  | 42 ++++++++++++++
> > > >  17 files changed, 264 insertions(+), 11 deletions(-)
> > > >  create mode 100644 ld/testsuite/ld-scripts/start.s
> > > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.d
> > > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.s
> > > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1a.t
> > > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.d
> > > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.s
> > > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1b.t
> > > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1c.d
> > > >  create mode 100644 ld/testsuite/ld-scripts/text-order-1c.t
> > > >  create mode 100644 ld/testsuite/ld-scripts/text-order.exp
> > > >
> > > > diff --git a/ld/ld.h b/ld/ld.h
> > > > index 54d9079678c..a80255a73ba 100644
> > > > --- a/ld/ld.h
> > > > +++ b/ld/ld.h
> > > > @@ -316,6 +316,9 @@ typedef struct
> > > >
> > > >    /* Compress DWARF debug sections.  */
> > > >    enum compressed_debug_section_type compress_debug;
> > > > +
> > > > +  /* The optional text section ordering file.  */
> > > > +  const char *text_section_ordering_file;
> > > >  } ld_config_type;
> > > >
> > > >  extern ld_config_type config;
> > > > diff --git a/ld/ldfile.c b/ld/ldfile.c
> > > > index dc9875d8813..6e9fbb7730f 100644
> > > > --- a/ld/ldfile.c
> > > > +++ b/ld/ldfile.c
> > > > @@ -736,7 +736,7 @@ ldfile_open_file (lang_input_statement_type *entry)
> > > >  /* Try to open NAME.  */
> > > >
> > > >  static FILE *
> > > > -try_open (const char *name, bool *sysrooted)
> > > > +try_open (const char *name, const char *orig_name, bool *sysrooted)
> > > >  {
> > > >    FILE *result;
> > > >
> > > > @@ -750,7 +750,34 @@ try_open (const char *name, bool *sysrooted)
> > > >
> > > >    if (verbose)
> > > >      {
> > > > -      if (result == NULL)
> > > > +      if (config.text_section_ordering_file != NULL
> > > > +         && strcmp (orig_name, config.text_section_ordering_file) == 0)
> > > > +       {
> > > > +         if (result == NULL)
> > > > +           info_msg (_("cannot find text section ordering file: %s\n"),
> > > > +                     name);
> > > > +         else
> > > > +           {
> > > > +             static const int ld_bufsz = 8193;
> > > > +             size_t n;
> > > > +             char *buf = (char *) xmalloc (ld_bufsz);
> > > > +
> > > > +             info_msg (_("opened text section ordering file: %s\n"),
> > > > +                       name);
> > > > +             info_msg ("==================================================\n");
> > > > +
> > > > +             while ((n = fread (buf, 1, ld_bufsz - 1, result)) > 0)
> > > > +               {
> > > > +                 buf[n] = 0;
> > > > +                 info_msg ("%s", buf);
> > > > +               }
> > > > +             rewind (result);
> > > > +             free (buf);
> > > > +
> > > > +             info_msg ("==================================================\n\n");
> > > > +           }
> > > > +       }
> > > > +      else if (result == NULL)
> > > >         info_msg (_("cannot find script file %s\n"), name);
> > > >        else
> > > >         info_msg (_("opened script file %s\n"), name);
> > > > @@ -832,7 +859,7 @@ ldfile_find_command_file (const char *name,
> > > >    if (!default_only)
> > > >      {
> > > >        /* First try raw name.  */
> > > > -      result = try_open (name, sysrooted);
> > > > +      result = try_open (name, name, sysrooted);
> > > >        if (result != NULL)
> > > >         return result;
> > > >      }
> > > > @@ -859,7 +886,7 @@ ldfile_find_command_file (const char *name,
> > > >         search = search->next)
> > > >      {
> > > >        path = concat (search->name, slash, name, (const char *) NULL);
> > > > -      result = try_open (path, sysrooted);
> > > > +      result = try_open (path, name, sysrooted);
> > > >        free (path);
> > > >        if (result)
> > > >         break;
> > > > @@ -908,6 +935,38 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
> > > >         }
> > > >      }
> > > >
> > > > +  /* Don't allow nested INCLUDE in the text section ordering file.  */
> > > > +  if (in_text_section_ordering_file)
> > > > +    {
> > > > +      einfo (_("%F%P: error: nested include '%s' in the text section "
> > > > +              "ordering file: '%s'\n"), name,
> > > > +              config.text_section_ordering_file);
> > > > +      return;
> > > > +    }
> > > > +
> > > > +  if (strcmp (name, "config.text_section_ordering_file") == 0)
> > > > +    {
> > > > +      /* Support
> > > > +
> > > > +        INCLUDE config.text_section_ordering_file;
> > > > +
> > > > +        in input text sections in linker script.  */
> > > > +      if (config.text_section_ordering_file == NULL)
> > > > +       {
> > > > +         /* Skip if the text section ordering file isn't specified.  */
> > > > +         lex_push_file (NULL, name, false);
> > > > +         return;
> > > > +       }
> > > > +
> > > > +      /* Load the text section ordering file.  */
> > > > +      name = config.text_section_ordering_file;
> > > > +
> > > > +      /* Set the in the text section ordering file marker.  */
> > > > +      in_text_section_ordering_file = 1;
> > > > +    }
> > > > +  else
> > > > +    in_text_section_ordering_file = 0;
> > > > +
> > > >    /* FIXME: This memory is never freed, but that should not really matter.
> > > >       It will be released when the linker exits, and it is unlikely to ever
> > > >       be more than a few tens of bytes.  */
> > > > @@ -932,7 +991,12 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
> > > >
> > > >    lineno = 1;
> > > >
> > > > -  saved_script_handle = ldlex_input_stack;
> > > > +  /* Clear the end of the include file marker.  */
> > > > +  seen_eof_include_file = 0;
> > > > +
> > > > +  /* The text section ordering file isn't a real linker script file.  */
> > > > +  if (!in_text_section_ordering_file)
> > > > +    saved_script_handle = ldlex_input_stack;
> > > >  }
> > > >
> > > >  /* Open command file NAME in the current directory, -L directories,
> > > > diff --git a/ld/ldlang.c b/ld/ldlang.c
> > > > index 229401c8342..d2b4d43d8ae 100644
> > > > --- a/ld/ldlang.c
> > > > +++ b/ld/ldlang.c
> > > > @@ -8512,12 +8512,43 @@ lang_add_wild (struct wildcard_spec *filespec,
> > > >
> > > >    if (filespec != NULL && filespec->name != NULL)
> > > >      {
> > > > -      if (strcmp (filespec->name, "*") == 0)
> > > > -       filespec->name = NULL;
> > > > -      else if (!wildcardp (filespec->name))
> > > > -       lang_has_input_file = true;
> > > > +      if (in_text_section_ordering_file != 0
> > > > +         && section_list == NULL
> > > > +         && filespec->name[0] == '.'
> > > > +         && filespec->sorted == none
> > > > +         && filespec->exclude_name_list == NULL
> > > > +         && filespec->section_flag_list == NULL
> > > > +         && !filespec->reversed)
> > > > +       {
> > > > +         /* When seeing an input file name without section list in the
> > > > +            text section ordering file and the file name starts with
> > > > +            '.', treat the filename as a section name and create a
> > > > +            wild card as the file name.  */
> > > > +         struct wildcard_list *single_section
> > > > +           = (struct wildcard_list *) xmalloc (sizeof (*single_section));
> > > > +         memset (single_section, 0, sizeof (*single_section));
> > > > +         single_section->spec.name = filespec->name;
> > > > +         single_section->spec.sorted = none;
> > > > +         /* A NULL indicates the wild card file name, "*".  */
> > > > +         filespec->name = NULL;
> > > > +         section_list = single_section;
> > > > +       }
> > > > +      else
> > > > +       {
> > > > +         if (strcmp (filespec->name, "*") == 0)
> > > > +           filespec->name = NULL;
> > > > +         else if (!wildcardp (filespec->name))
> > > > +           lang_has_input_file = true;
> > > > +       }
> > > >      }
> > > >
> > > > +  /* NB: Clear the in the text section ordering file marker after
> > > > +     processing the last entry when the end of the text section
> > > > +     ordering file is reached.  */
> > > > +  if (in_text_section_ordering_file != 0
> > > > +      && seen_eof_include_file != 0)
> > > > +    in_text_section_ordering_file = 0;
> > > > +
> > > >    new_stmt = new_stat (lang_wild_statement, stat_ptr);
> > > >    new_stmt->filename = NULL;
> > > >    new_stmt->filenames_sorted = false;
> > > > diff --git a/ld/ldlex.h b/ld/ldlex.h
> > > > index e5ac2fa7fca..a2c49656e1a 100644
> > > > --- a/ld/ldlex.h
> > > > +++ b/ld/ldlex.h
> > > > @@ -68,6 +68,7 @@ enum option_values
> > > >    OPTION_TASK_LINK,
> > > >    OPTION_TBSS,
> > > >    OPTION_TDATA,
> > > > +  OPTION_TEXT_SECTION_ORDERING_FILE,
> > > >    OPTION_TTEXT,
> > > >    OPTION_TTEXT_SEGMENT,
> > > >    OPTION_TRODATA_SEGMENT,
> > > > @@ -484,6 +485,8 @@ extern input_type parser_input;
> > > >
> > > >  extern unsigned int lineno;
> > > >  extern const char *lex_string;
> > > > +extern int in_text_section_ordering_file;
> > > > +extern int seen_eof_include_file;
> > > >
> > > >  /* In ldlex.l.  */
> > > >  extern int yylex (void);
> > > > diff --git a/ld/ldlex.l b/ld/ldlex.l
> > > > index e113c90812b..b41e1220661 100644
> > > > --- a/ld/ldlex.l
> > > > +++ b/ld/ldlex.l
> > > > @@ -43,6 +43,12 @@ input_type parser_input;
> > > >  /* Line number in the current input file.  */
> > > >  unsigned int lineno;
> > > >
> > > > +/* 1 if the current input file is the text section ordering file.  */
> > > > +int in_text_section_ordering_file = 0;
> > > > +
> > > > +/* 1 if the end of the include file is reached.  */
> > > > +int seen_eof_include_file = 0;
> > > > +
> > > >  /* The string we are currently lexing, or NULL if we are reading a
> > > >     file.  */
> > > >  const char *lex_string = NULL;
> > > > @@ -487,6 +493,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
> > > >
> > > >    lineno = lineno_stack[include_stack_ptr];
> > > >    input_flags.sysrooted = sysrooted_stack[include_stack_ptr];
> > > > +  seen_eof_include_file = 1;
> > > >
> > > >    return END;
> > > >  }
> > > > diff --git a/ld/lexsup.c b/ld/lexsup.c
> > > > index 099dff8ecde..21385628020 100644
> > > > --- a/ld/lexsup.c
> > > > +++ b/ld/lexsup.c
> > > > @@ -484,6 +484,9 @@ static const struct ld_option ld_options[] =
> > > >    { {"sort-section", required_argument, NULL, OPTION_SORT_SECTION},
> > > >      '\0', N_("name|alignment"),
> > > >      N_("Sort sections by name or maximum alignment"), TWO_DASHES },
> > > > +  { {"text-section-ordering-file", required_argument, NULL, OPTION_TEXT_SECTION_ORDERING_FILE},
> > > > +    '\0', N_("FILE"),
> > > > +    N_("Sort text sections by FILE"), TWO_DASHES },
> > > >    { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
> > > >      '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"),
> > > >      TWO_DASHES },
> > > > @@ -670,6 +673,7 @@ parse_args (unsigned argc, char **argv)
> > > >      dynamic_list
> > > >    } opt_dynamic_list = dynamic_list_unset;
> > > >    struct bfd_elf_dynamic_list *export_list = NULL;
> > > > +  bool seen_linker_script = false;
> > > >
> > > >    shortopts = (char *) xmalloc (OPTION_COUNT * 3 + 2);
> > > >    longopts = (struct option *)
> > > > @@ -1394,6 +1398,12 @@ parse_args (unsigned argc, char **argv)
> > > >             einfo (_("%F%P: invalid section sorting option: %s\n"),
> > > >                    optarg);
> > > >           break;
> > > > +       case OPTION_TEXT_SECTION_ORDERING_FILE:
> > > > +         if (seen_linker_script)
> > > > +           einfo (_("%F%P: --text-section-ordering-file must be placed"
> > > > +                    " before -T/--script\n"));
> > > > +         config.text_section_ordering_file = optarg;
> > > > +         break;
> > > >         case OPTION_STATS:
> > > >           config.stats = true;
> > > >           break;
> > > > @@ -1410,6 +1420,7 @@ parse_args (unsigned argc, char **argv)
> > > >           ++trace_files;
> > > >           break;
> > > >         case 'T':
> > > > +         seen_linker_script = true;
> > > >           previous_script_handle = saved_script_handle;
> > > >           ldfile_open_script_file (optarg);
> > > >           parser_input = input_script;
> > > > diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
> > > > index fae7c2ad71c..8014dfbfa27 100644
> > > > --- a/ld/scripttempl/elf.sc
> > > > +++ b/ld/scripttempl/elf.sc
> > > > @@ -553,11 +553,12 @@ cat <<EOF
> > > >    .text         ${RELOCATING-0} :
> > > >    {
> > > >      ${RELOCATING+${TEXT_START_SYMBOLS}}
> > > > +    ${RELOCATING+INCLUDE config.text_section_ordering_file}
> > > > +    ${RELOCATING+*(.text.hot .text.hot.*)}
> > > > +    ${RELOCATING+*(SORT(.text.sorted.*))}
> > > >      ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
> > > >      ${RELOCATING+*(.text.exit .text.exit.*)}
> > > >      ${RELOCATING+*(.text.startup .text.startup.*)}
> > > > -    ${RELOCATING+*(.text.hot .text.hot.*)}
> > > > -    ${RELOCATING+*(SORT(.text.sorted.*))}
> > > >      *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
> > > >      /* .gnu.warning sections are handled specially by elf.em.  */
> > > >      *(.gnu.warning)
>
> I see the inflexibility of the current framework.
> --text-section-ordering-file seems less general as I'd expect, and
> placing ${RELOCATING+INCLUDE config.text_section_ordering_file} beside
> the existing text input section descriptions does not make things more
> flexible.
> It is a shame that the new feature only applies to .text . Data
> sections and other code sections should be reorderable as well.
>
> I believe tuning the semantics can make it more useful.
> lld's -z keep-text-section-prefix and --symbol-ordering-file= work the
> following way:
>
> * When -z keep-text-section-prefix is specified, .text.hot.* sections
> are placed into the output section .text.hot. .text.unlikely,
> .text.startup, etc are similar. Otherwise, all .text.* go to .text.
> * There is no fixed order among the text output sections. The first
> seen one wins. https://github.com/llvm/llvm-project/blob/main/lld/test/ELF/text-section-prefix.s
>   If the user add `asm(".section .text.hot,\"axR\",%progbits");
> asm(".section .text.unlikely,\"axR\",%progbits");` to the crtbegin
> source file, the output section order can be fixed in a desired way,
> regardless of whether .text.startup/.text.hot appears first in
> application code.
> * Normally a wildcard pattern in an input section description (e.g.
> *(.text .text.*)) uses the input order. However, if
> --symbol-ordering-file= is specified, it overrides the order.
> https://github.com/llvm/llvm-project/blob/main/lld/test/ELF/symbol-ordering-file.s
>
> Back to GNU ld, I wonder whether we can say, when
> config.text_section_ordering_file is present, drop all the existing
> rules (e.g. ${RELOCATING+*(.text.exit .text.exit.*)}), and keep just
> *(.text .text.*).
> Make the ordering file decide how the wildcard is ordered.
>

Since the section order priorities in ld are

1. The exact section name match.
2. The first section name match.

matches in --text-section-ordering-file will override other
matches after INCLUDE.   I updated my tests to show it:

https://gitlab.com/x86-binutils/binutils-gdb/-/commit/cd5b222a2fed565e49dbc4b4d7b61de2380919b7


-- 
H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE
  2024-01-19 23:15       ` Fangrui Song
@ 2024-01-22  7:37         ` Jan Beulich
  2024-01-22 12:51           ` H.J. Lu
  0 siblings, 1 reply; 19+ messages in thread
From: Jan Beulich @ 2024-01-22  7:37 UTC (permalink / raw)
  To: Fangrui Song, H.J. Lu; +Cc: binutils, goldstein.w.n

On 20.01.2024 00:15, Fangrui Song wrote:
> It is a shame that the new feature only applies to .text . Data
> sections and other code sections should be reorderable as well.

I second this remark. In fact I wonder why it is that text needs
treating specially here. Can't the option just be
--section-ordering-file and then permit covering all kinds of
sections?

Jan

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE
  2024-01-22  7:37         ` Jan Beulich
@ 2024-01-22 12:51           ` H.J. Lu
  2024-01-22 18:22             ` Noah Goldstein
  0 siblings, 1 reply; 19+ messages in thread
From: H.J. Lu @ 2024-01-22 12:51 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Fangrui Song, binutils, goldstein.w.n

On Sun, Jan 21, 2024 at 11:37 PM Jan Beulich <jbeulich@suse.com> wrote:
>
> On 20.01.2024 00:15, Fangrui Song wrote:
> > It is a shame that the new feature only applies to .text . Data
> > sections and other code sections should be reorderable as well.
>
> I second this remark. In fact I wonder why it is that text needs

The perf data usually provides guidance for text section layout.

> treating specially here. Can't the option just be
> --section-ordering-file and then permit covering all kinds of
> sections?

It requires much bigger changes to ld.


-- 
H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE
  2024-01-22 12:51           ` H.J. Lu
@ 2024-01-22 18:22             ` Noah Goldstein
  2024-01-22 18:45               ` H.J. Lu
  0 siblings, 1 reply; 19+ messages in thread
From: Noah Goldstein @ 2024-01-22 18:22 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Jan Beulich, Fangrui Song, binutils

On Mon, Jan 22, 2024 at 12:52 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Sun, Jan 21, 2024 at 11:37 PM Jan Beulich <jbeulich@suse.com> wrote:
> >
> > On 20.01.2024 00:15, Fangrui Song wrote:
> > > It is a shame that the new feature only applies to .text . Data
> > > sections and other code sections should be reorderable as well.
> >
> > I second this remark. In fact I wonder why it is that text needs
>
> The perf data usually provides guidance for text section layout.
>
> > treating specially here. Can't the option just be
> > --section-ordering-file and then permit covering all kinds of
> > sections?

Another issue with this approach is that it won't work for custom
linker scripts (because the `INCLUDE` of the ordering file has
been manually inserted in the defaults).

A different approach for this patch that may alleviate both concerns
(work for custom linker scripts + for arbitrary sections) would be to
update the logic of `section: NAME` in `ldgram.y`.

We could insert the script contents upon entering any new section.

This might blowup link time (if a link script is inserted at each section),
so we could add another auxiliary option to specify which sections
the config script should apply for and make it default to something like
`.data` and `.text`.
>
> It requires much bigger changes to ld.
>
>
> --
> H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE
  2024-01-22 18:22             ` Noah Goldstein
@ 2024-01-22 18:45               ` H.J. Lu
  2024-01-24 14:41                 ` H.J. Lu
  0 siblings, 1 reply; 19+ messages in thread
From: H.J. Lu @ 2024-01-22 18:45 UTC (permalink / raw)
  To: Noah Goldstein; +Cc: Jan Beulich, Fangrui Song, binutils

On Mon, Jan 22, 2024 at 10:22 AM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
>
> On Mon, Jan 22, 2024 at 12:52 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > On Sun, Jan 21, 2024 at 11:37 PM Jan Beulich <jbeulich@suse.com> wrote:
> > >
> > > On 20.01.2024 00:15, Fangrui Song wrote:
> > > > It is a shame that the new feature only applies to .text . Data
> > > > sections and other code sections should be reorderable as well.
> > >
> > > I second this remark. In fact I wonder why it is that text needs
> >
> > The perf data usually provides guidance for text section layout.
> >
> > > treating specially here. Can't the option just be
> > > --section-ordering-file and then permit covering all kinds of
> > > sections?
>
> Another issue with this approach is that it won't work for custom
> linker scripts (because the `INCLUDE` of the ordering file has
> been manually inserted in the defaults).

Good point.  I updated the documentation with

This option is supported only for default linker scripts in ELF and PE
COFF linkers.

> A different approach for this patch that may alleviate both concerns
> (work for custom linker scripts + for arbitrary sections) would be to
> update the logic of `section: NAME` in `ldgram.y`.

We shouldn't do it for linker generated sections, like .plt and .got.

> We could insert the script contents upon entering any new section.
>
> This might blowup link time (if a link script is inserted at each section),
> so we could add another auxiliary option to specify which sections
> the config script should apply for and make it default to something like
> `.data` and `.text`.

Sounds like a good improvement.

> >
> > It requires much bigger changes to ld.
> >
> >
> > --
> > H.J.



-- 
H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE
  2024-01-22 18:45               ` H.J. Lu
@ 2024-01-24 14:41                 ` H.J. Lu
  0 siblings, 0 replies; 19+ messages in thread
From: H.J. Lu @ 2024-01-24 14:41 UTC (permalink / raw)
  To: Noah Goldstein; +Cc: Jan Beulich, Fangrui Song, binutils

On Mon, Jan 22, 2024 at 10:45 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Mon, Jan 22, 2024 at 10:22 AM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
> >
> > On Mon, Jan 22, 2024 at 12:52 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> > >
> > > On Sun, Jan 21, 2024 at 11:37 PM Jan Beulich <jbeulich@suse.com> wrote:
> > > >
> > > > On 20.01.2024 00:15, Fangrui Song wrote:
> > > > > It is a shame that the new feature only applies to .text . Data
> > > > > sections and other code sections should be reorderable as well.
> > > >
> > > > I second this remark. In fact I wonder why it is that text needs
> > >
> > > The perf data usually provides guidance for text section layout.
> > >
> > > > treating specially here. Can't the option just be
> > > > --section-ordering-file and then permit covering all kinds of
> > > > sections?
> >
> > Another issue with this approach is that it won't work for custom
> > linker scripts (because the `INCLUDE` of the ordering file has
> > been manually inserted in the defaults).
>
> Good point.  I updated the documentation with
>
> This option is supported only for default linker scripts in ELF and PE
> COFF linkers.
>
> > A different approach for this patch that may alleviate both concerns
> > (work for custom linker scripts + for arbitrary sections) would be to
> > update the logic of `section: NAME` in `ldgram.y`.
>
> We shouldn't do it for linker generated sections, like .plt and .got.
>
> > We could insert the script contents upon entering any new section.
> >
> > This might blowup link time (if a link script is inserted at each section),
> > so we could add another auxiliary option to specify which sections
> > the config script should apply for and make it default to something like
> > `.data` and `.text`.
>
> Sounds like a good improvement.

This approach may not be easy to implement in ld.  We can't add
the same INCLUDE file to more than 1 output section.   The first
INCLUDE will always win.

-- 
H.J.

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2024-01-24 14:42 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-19 19:45 [PATCH 0/4] ld: Add --text-section-ordering-file FILE H.J. Lu
2024-01-19 19:45 ` [PATCH 1/4] " H.J. Lu
2024-01-19 21:50   ` Fangrui Song
     [not found]   ` <DS7PR12MB5765A2B98C7E1217F8C374B9CB702@DS7PR12MB5765.namprd12.prod.outlook.com>
2024-01-19 22:13     ` H.J. Lu
2024-01-19 23:15       ` Fangrui Song
2024-01-22  7:37         ` Jan Beulich
2024-01-22 12:51           ` H.J. Lu
2024-01-22 18:22             ` Noah Goldstein
2024-01-22 18:45               ` H.J. Lu
2024-01-24 14:41                 ` H.J. Lu
     [not found]       ` <DS7PR12MB5765059CCA55EC755F8132ACCB702@DS7PR12MB5765.namprd12.prod.outlook.com>
2024-01-20 14:12         ` H.J. Lu
2024-01-19 19:45 ` [PATCH 2/4] ld: Include the text section order file in all ELF linker scripts H.J. Lu
2024-01-19 20:00   ` Noah Goldstein
2024-01-19 20:18     ` H.J. Lu
2024-01-19 20:38       ` Noah Goldstein
2024-01-19 20:40         ` H.J. Lu
2024-01-19 21:07           ` Noah Goldstein
2024-01-19 19:45 ` [PATCH 3/4] ld: Include the text section order file in PE COFF " H.J. Lu
2024-01-19 19:45 ` [PATCH 4/4] ld: Document --text-section-ordering-file FILE H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).