[2/8] section-select: Deal with sections added late

Message ID alpine.LSU.2.20.2211251645000.24878@wotan.suse.de
State Accepted
Headers
Series ld: Speed up section selection |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

Michael Matz Nov. 25, 2022, 4:46 p.m. UTC
  at least the check_relocs callback can add input sections (for
creating an dynamic output relocation section).  For those
we need to go through the walk_wild handlers again.  For quick
detection of such new sections we export a new bfd function:
bfd_get_max_section_id and remember that one when resolving a wild
statement.  Any section whose ->id member is higher than that is new.
---

I keep this patch separate (not quashed into the one before) because it 
adds an interface to libbfd, which might be superseded depending on an 
answer to patch 4/8.  And keeping it )and the reversion in 7/8) separate 
makes it easier to throw it out or include it.

 bfd/bfd-in2.h |  2 ++
 bfd/section.c | 19 +++++++++++++++++++
 ld/ldlang.c   | 12 +++++++++++-
 ld/ldlang.h   |  1 +
 4 files changed, 33 insertions(+), 1 deletion(-)
  

Patch

diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 0b071dda1e5..5350ae42b03 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -1330,6 +1330,8 @@  discarded_section (const asection *sec)
   { 0, NAME, 0, BSF_SECTION_SYM, SECTION }
 #endif
 
+unsigned int bfd_get_max_section_id (void);
+
 void bfd_section_list_clear (bfd *);
 
 asection *bfd_get_section_by_name (bfd *abfd, const char *name);
diff --git a/bfd/section.c b/bfd/section.c
index f73e0345e15..d691bf265ab 100644
--- a/bfd/section.c
+++ b/bfd/section.c
@@ -849,6 +849,25 @@  SUBSECTION
 These are the functions exported by the section handling part of BFD.
 */
 
+/*
+FUNCTION
+	bfd_get_max_section_id
+
+SYNOPSIS
+	unsigned int bfd_get_max_section_id (void);
+
+DESCRIPTION
+	Returns an internal number representing the maximum value of
+	any SECTION->id member.  Whenever a new section is created that
+	value increases.  It never decreases.
+*/
+
+unsigned int
+bfd_get_max_section_id (void)
+{
+  return _bfd_section_id;
+}
+
 /*
 FUNCTION
 	bfd_section_list_clear
diff --git a/ld/ldlang.c b/ld/ldlang.c
index c92ebd472f4..1e4f3a5ee05 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -374,6 +374,8 @@  walk_wild_consider_section (lang_wild_statement_type *ptr,
 			    callback_t callback,
 			    void *data)
 {
+  if (s->id < ptr->max_section_id)
+    return;
   /* Don't process sections from files which were excluded.  */
   if (walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, file))
     return;
@@ -395,6 +397,9 @@  walk_wild_section_general (lang_wild_statement_type *ptr,
 
   for (s = file->the_bfd->sections; s != NULL; s = s->next)
     {
+      if (s->id < ptr->max_section_id)
+	continue;
+      //printf ("YYY checking %s:%s\n", s->owner->filename, s->name);
       sec = ptr->section_list;
       if (sec == NULL)
 	(*callback) (ptr, sec, s, file, data);
@@ -1139,11 +1144,14 @@  walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
   const char *file_spec = s->filename;
   //char *p;
 
-  if (!s->resolved)
+#if 1
+  //if (!s->resolved)
+  if (s->max_section_id < bfd_get_max_section_id ())
     {
       //printf("XXX %s\n", file_spec ? file_spec : "<null>");
       walk_wild_resolve (s);
       s->resolved = true;
+      s->max_section_id = bfd_get_max_section_id ();
     }
 
     {
@@ -1154,6 +1162,7 @@  walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
 	}
       return;
     }
+#endif
 
 #if 0
   if (file_spec == NULL)
@@ -8428,6 +8437,7 @@  lang_add_wild (struct wildcard_spec *filespec,
   new_stmt->keep_sections = keep_sections;
   lang_list_init (&new_stmt->children);
   new_stmt->resolved = false;
+  new_stmt->max_section_id = 0;
   lang_list_init (&new_stmt->matching_sections);
   analyze_walk_wild_section_handler (new_stmt);
 }
diff --git a/ld/ldlang.h b/ld/ldlang.h
index 50ad64ce057..8566e022a57 100644
--- a/ld/ldlang.h
+++ b/ld/ldlang.h
@@ -400,6 +400,7 @@  struct lang_wild_statement_struct
   struct name_list *exclude_name_list;
   lang_statement_list_type matching_sections;
   bool resolved;
+  unsigned int max_section_id;
 
   walk_wild_section_handler_t walk_wild_section_handler;
   struct wildcard_list *handler_data[4];