diff --git a/ld/ldlang.c b/ld/ldlang.c
index 3748bf9bec9..18d10531d51 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -81,6 +81,9 @@ static void exp_init_os (etree_type *);
 static lang_input_statement_type *lookup_name (const char *);
 static void insert_undefined (const char *);
 static bool sort_def_symbol (struct bfd_link_hash_entry *, void *);
+static lang_statement_union_type *new_statement (enum statement_enum type,
+						size_t size,
+						lang_statement_list_type *list);
 static void print_statement (lang_statement_union_type *,
 			     lang_output_section_statement_type *);
 static void print_statement_list (lang_statement_union_type *,
@@ -365,39 +368,43 @@ walk_wild_file_in_exclude_list (struct name_list *exclude_list,
   return false;
 }
 
-/* Try processing a section against a wildcard.  This just calls
-   the callback unless the filename exclusion list is present
-   and excludes the file.  It's hardly ever present so this
-   function is very fast.  */
+static unsigned int old_max_section_id = 0;
+
+/* Add SECTION (from input FILE) to the list of matching sections
+   within PTR (the matching wildcard is SEC).  */
 
 static void
-walk_wild_consider_section (lang_wild_statement_type *ptr,
-			    lang_input_statement_type *file,
-			    asection *s,
-			    struct wildcard_list *sec,
-			    callback_t callback,
-			    void *data)
+add_matching_section (lang_wild_statement_type *ptr,
+		      struct wildcard_list *sec,
+		      asection *section,
+		      lang_input_statement_type *file)
 {
-  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;
-
-  (*callback) (ptr, sec, s, file, data);
+  lang_input_matcher_type *new_section;
+  /* Add a section reference to the list.  */
+  new_section = new_stat (lang_input_matcher, &ptr->matching_sections);
+  new_section->section = section;
+  new_section->pattern = sec;
+  new_section->input_stmt = file;
 }
 
+/* Process section S (from input file FILE) in relation to wildcard
+   statement PTR.  We already know that a prefix of the name of S matches
+   some wildcard in PTR's wildcard list.  Here we check if the filename
+   matches as well (if it's specified) and if any of the wildcards in fact
+   does match.  */
+
 static void
 walk_wild_section_match (lang_wild_statement_type *ptr,
 			 lang_input_statement_type *file,
-			 asection *s,
-			 callback_t callback,
-			 void *data)
+			 asection *s)
 {
   struct wildcard_list *sec;
   const char *file_spec = ptr->filename;
   char *p;
 
+  if (s->id < ptr->max_section_id)
+    return;
+
   /* Check if filenames match.  */
   if (file_spec == NULL)
     ;
@@ -424,23 +431,21 @@ walk_wild_section_match (lang_wild_statement_type *ptr,
      wildcard all sections match.  */
   sec = ptr->section_list;
   if (sec == NULL)
-    (*callback) (ptr, sec, s, file, data);
-
-  while (sec != NULL)
+    add_matching_section (ptr, sec, s, file);
+  else
     {
-      bool skip = false;
-
-      if (sec->spec.name != NULL)
+      const char *sname = bfd_section_name (s);
+      for (; sec != NULL; sec = sec->next)
 	{
-	  const char *sname = bfd_section_name (s);
+	  if (sec->spec.name != NULL
+	      && spec_match (&sec->spec, sname) != 0)
+	    continue;
 
-	  skip = spec_match (&sec->spec, sname) != 0;
+	  /* Don't process sections from files which were excluded.  */
+	  if (!walk_wild_file_in_exclude_list (sec->spec.exclude_name_list,
+					       file))
+	    add_matching_section (ptr, sec, s, file);
 	}
-
-      if (!skip)
-	walk_wild_consider_section (ptr, file, s, sec, callback, data);
-
-      sec = sec->next;
     }
 }
 
@@ -769,8 +774,7 @@ static void
 insert_prefix_tree (lang_wild_statement_type *stmt)
 {
   struct wildcard_list *sec;
-  struct prefixtree **pt = &ptroot, *t;
-  struct wild_stmt_list *sl, **psl;
+  struct prefixtree *t;
 
   if (!stmt->section_list)
     {
@@ -782,9 +786,8 @@ insert_prefix_tree (lang_wild_statement_type *stmt)
 
   for (sec = stmt->section_list; sec; sec = sec->next)
     {
-      const char *name = sec->spec.name ? sec->spec.name : "";
+      const char *name = sec->spec.name ? sec->spec.name : "*";
       char c;
-      pt = &ptroot;
       t = ptroot;
       for (; (c = *name); name++)
 	{
@@ -799,15 +802,7 @@ insert_prefix_tree (lang_wild_statement_type *stmt)
 	 as well.  */
       if (!c)
 	t = get_prefix_tree (&t->child, 0, true);
-      else if (!t)
-	abort();
-      sl = (struct wild_stmt_list *) xmalloc (sizeof *sl);
-      sl->stmt = stmt;
-      sl->next = NULL;
-      psl = &t->stmt;
-      while (*psl)
-	psl = &(*psl)->next;
-      *psl = sl;
+      pt_add_stmt (t, stmt);
     }
 }
 
@@ -885,47 +880,6 @@ analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
   insert_prefix_tree (ptr);
 }
 
-static bool check_resolve = false;
-static unsigned int old_max_section_id = 0;
-
-static lang_statement_union_type *
-new_statement (enum statement_enum type,
-	       size_t size,
-	       lang_statement_list_type *list);
-static void
-add_matching_callback (lang_wild_statement_type *ptr,
-		       struct wildcard_list *sec,
-		       asection *section,
-		       lang_input_statement_type *file,
-		       void *data ATTRIBUTE_UNUSED)
-{
-  if (check_resolve)
-    {
-      if (0)
-	{
-	  lang_statement_union_type *l;
-	  for (l = ptr->matching_sections.head; l; l = l->header.next)
-	    {
-	      if (section == l->input_matcher.section
-		  && sec == l->input_matcher.pattern
-		  && file == l->input_matcher.input_stmt)
-		break;
-	    }
-	  if (!l)
-	    abort();
-	}
-    }
-  else
-    {
-      lang_input_matcher_type *new_section;
-      /* Add a section reference to the list.  */
-      new_section = new_stat (lang_input_matcher, &ptr->matching_sections);
-      new_section->section = section;
-      new_section->pattern = sec;
-      new_section->input_stmt = file;
-    }
-}
-
 /* Match all sections from FILE against the global prefix tree,
    and record them into each wild statement that has a match.  */
 
@@ -940,37 +894,28 @@ resolve_wild_sections (lang_input_statement_type *file)
   for (s = file->the_bfd->sections; s != NULL; s = s->next)
     {
       const char *sname = bfd_section_name (s);
-      char c;
-      struct prefixtree **pt = &ptroot, *t = *pt;
+      char c = 1;
+      struct prefixtree *t = ptroot;
       if (old_max_section_id && s->id < old_max_section_id)
 	continue;
       //printf (" YYY consider %s of %s\n", sname, file->the_bfd->filename);
       do
 	{
-	  if (!t)
-	    break;
 	  if (t->stmt)
 	    {
 	      struct wild_stmt_list *sl;
 	      for (sl = t->stmt; sl; sl = sl->next)
 		{
-		  walk_wild_section_match (sl->stmt, file, s, add_matching_callback, NULL);
+		  walk_wild_section_match (sl->stmt, file, s);
 		  //printf ("   ZZZ maybe place into %p\n", sl->stmt);
 		}
 	    }
+	  if (!c)
+	    break;
 	  c = *sname++;
 	  t = get_prefix_tree (&t->child, c, false);
 	}
-      while (c && t);
-      if (t && t->stmt)
-	{
-	  struct wild_stmt_list *sl;
-	  for (sl = t->stmt; sl; sl = sl->next)
-	    {
-	      walk_wild_section_match (sl->stmt, file, s, add_matching_callback, NULL);
-	      //printf ("   ZZZ maybe place into %p\n", sl->stmt);
-	    }
-	}
+      while (t);
     }
 }
 
@@ -979,7 +924,6 @@ resolve_wild_sections (lang_input_statement_type *file)
 static void
 resolve_wilds (void)
 {
-  check_resolve = false;
   LANG_FOR_EACH_INPUT_STATEMENT (f)
     {
       //printf("XXX   %s\n", f->filename);
@@ -1011,7 +955,6 @@ resolve_wilds (void)
 	}
     }
   old_max_section_id = bfd_get_max_section_id ();
-  check_resolve = true;
 }
 
 /* For each input section that matches wild statement S calls
@@ -1025,13 +968,13 @@ walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
   if (s->max_section_id < bfd_get_max_section_id ())
     {
       //printf("XXX %s\n", s->filename ? s->filename : "<null>");
-      s->resolved = true;
       s->max_section_id = bfd_get_max_section_id ();
     }
 
   for (l = s->matching_sections.head; l; l = l->header.next)
     {
-      (*callback) (s, l->input_matcher.pattern, l->input_matcher.section, l->input_matcher.input_stmt, data);
+      (*callback) (s, l->input_matcher.pattern, l->input_matcher.section,
+		   l->input_matcher.input_stmt, data);
     }
 }
 
@@ -7910,7 +7853,6 @@ reset_one_wild (lang_statement_union_type *statement)
   if (statement->header.type == lang_wild_statement_enum)
     {
       lang_wild_statement_type *stmt = &statement->wild_statement;
-      stmt->resolved = false;
       stmt->max_section_id = 0;
       /* XXX Leaks? */
       lang_list_init (&stmt->matching_sections);
@@ -8298,7 +8240,6 @@ lang_add_wild (struct wildcard_spec *filespec,
   new_stmt->section_list = section_list;
   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 09c43611a22..5d9d2447f1c 100644
--- a/ld/ldlang.h
+++ b/ld/ldlang.h
@@ -399,7 +399,6 @@ struct lang_wild_statement_struct
   lang_statement_list_type children;
   struct name_list *exclude_name_list;
   lang_statement_list_type matching_sections;
-  bool resolved;
   unsigned int max_section_id;
 
   lang_section_bst_type *tree, **rightmost;
