From patchwork Fri Nov 25 16:47:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Matz X-Patchwork-Id: 26096 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp4153522wrr; Fri, 25 Nov 2022 08:47:29 -0800 (PST) X-Google-Smtp-Source: AA0mqf7bitT6knq8KZ21aZ+5w4uw/LQ25MCDWHoYb61yHkUNajC4HEh8Bmnh3I+4qrvDCamYirH1 X-Received: by 2002:a17:906:2612:b0:7ad:9c82:6cdb with SMTP id h18-20020a170906261200b007ad9c826cdbmr3689186ejc.233.1669394849273; Fri, 25 Nov 2022 08:47:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669394849; cv=none; d=google.com; s=arc-20160816; b=GXG0lUX17/NbVNlZNRtk5cxsGV/BQ4uA5Lbh1bCrIkA/JH201E6Z8PRbTEoXu/PzfZ xwARLr6hxMVmlRpXehcf1255uayG1mtt6Fz87JCWXOtytOmXqIFdJrvqwaWwTNcXkz6t az5R6mcc3+mCluIK0fdUw+mmWB8jAJyIcCxYRpVHnKEzojkr/Z81dEdDCd+rh4RrGCca iMK/CoR7BjgdrvBWjqqniU6C30PzEUsHbwgpWcZr+Fpd2ADMwx6ZbtZPUYVxHnfbLeJ9 rXKQUjixe1tx/lOcs+9SU5bgtI9JcSUg8EcKKyvE8uX6hlk1iXT7+Rf74MbEuWtqbk5A L5ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:mime-version :user-agent:references:message-id:in-reply-to:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ZUfTPzvJqk9V3tNqjRUuHziRVNc5zoq+eOVVz3g0zMQ=; b=0+kxNYdCR0AUFsx7gBAnPVkURc3YynI0D7YwGe6meDDeiIDbQqPEOeTnrhd81SgnHL i7siGJw7/N8Tj66IzBbJD8GTTHs/ETOFGPuf36WNUH8G0Yavi4SiYEP8x1G5Y0y3x9Yv N2veNAI/kpqEjRp3VaSLHAFdNTKVgfEBOa6gHHPafFrnyYkpqmuzQ28V1NMqWxxuYhyx Xv+DeUdTlGBEhbMdEnHj1MXCkDozBGiTeb4AxR7tpLp2z0kPMGKDU704oliTjXLng6g8 4/kqCPrpfQcoNDwlsj0SoQ4SV/dHi9FbkO0I77Lh08Jppp2/YNw9Ja0QorzAGIHiS/0N bUrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=EEYoz0sP; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ml21-20020a170906cc1500b007ae545e330csi2725057ejb.218.2022.11.25.08.47.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Nov 2022 08:47:29 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=EEYoz0sP; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DEF14384E38F for ; Fri, 25 Nov 2022 16:47:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DEF14384E38F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669394846; bh=ZUfTPzvJqk9V3tNqjRUuHziRVNc5zoq+eOVVz3g0zMQ=; h=Date:To:Subject:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=EEYoz0sPN7A6c8Esel5PopDhIenNPOuZRi7LC4AHaJekm4NbMPJVrK8bttYwQpF/X MVgMRUziDH7MNnZz1Bw92G8a8h8EWoNkjcAx98smzpf3H3Zhw6TkVh0U5ZVwbtyOaj JsKgvDZ324zj6ay0ZNkTdYORSz5hue1MkH3XBDGs= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id DB438384EF7A for ; Fri, 25 Nov 2022 16:47:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DB438384EF7A Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 1923F1FD89 for ; Fri, 25 Nov 2022 16:47:17 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 1450B2C141 for ; Fri, 25 Nov 2022 16:47:17 +0000 (UTC) Received: by wotan.suse.de (Postfix, from userid 10510) id 0F1756586; Fri, 25 Nov 2022 16:47:17 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by wotan.suse.de (Postfix) with ESMTP id 0D6FB6238 for ; Fri, 25 Nov 2022 16:47:17 +0000 (UTC) Date: Fri, 25 Nov 2022 16:47:17 +0000 (UTC) To: binutils@sourceware.org Subject: [PATCH 3/8] section-select: Implement a prefix-tree In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (LSU 67 2015-01-07) MIME-Version: 1.0 X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Michael Matz via Binutils From: Michael Matz Reply-To: Michael Matz Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750487373304482872?= X-GMAIL-MSGID: =?utf-8?q?1750487373304482872?= Now that we have a list of potentially matching sections per wild statement we can actually pre-fill that one by going once over all input sections and match their names against a prefix-tree that points to the potentially matching wild statements. So instead of looking at all sections names for each glob for each wild statement we now look at the sections only once and then only check against those globs that have a possibility of a match at all (usually only one or two). This pushes the whole section selection off the profiles. --- ld/ldlang.c | 362 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 353 insertions(+), 9 deletions(-) diff --git a/ld/ldlang.c b/ld/ldlang.c index 1e4f3a5ee05..06fa541df3a 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -59,6 +59,7 @@ /* Local variables. */ static struct obstack stat_obstack; static struct obstack map_obstack; +static struct obstack pt_obstack; #define obstack_chunk_alloc xmalloc #define obstack_chunk_free free @@ -210,6 +211,9 @@ name_match (const char *pattern, const char *name) return strcmp (pattern, name); } +/* Given an analyzed wildcard_spec SPEC, match it against NAME, + returns zero on a match, non-zero if there's no match. */ + static int spec_match (const struct wildcard_spec *spec, const char *name) { @@ -383,6 +387,63 @@ walk_wild_consider_section (lang_wild_statement_type *ptr, (*callback) (ptr, sec, s, file, data); } +static void +walk_wild_section_match (lang_wild_statement_type *ptr, + lang_input_statement_type *file, + asection *s, + callback_t callback, + void *data) +{ + struct wildcard_list *sec; + const char *file_spec = ptr->filename; + char *p; + + /* Check if filenames match. */ + if (file_spec == NULL) + ; + else if ((p = archive_path (file_spec)) != NULL) + { + if (!input_statement_is_archive_path (file_spec, p, file)) + return; + } + else if (wildcardp (file_spec)) + { + if (fnmatch (file_spec, file->filename, 0) != 0) + return; + } + else + { + lang_input_statement_type *f; + /* Perform the iteration over a single file. */ + f = lookup_name (file_spec); + if (f != file) + return; + } + + /* Check section name against each wildcard spec. If there's no + wildcard all sections match. */ + sec = ptr->section_list; + if (sec == NULL) + (*callback) (ptr, sec, s, file, data); + + while (sec != NULL) + { + bool skip = false; + + if (sec->spec.name != NULL) + { + const char *sname = bfd_section_name (s); + + skip = spec_match (&sec->spec, sname) != 0; + } + + if (!skip) + walk_wild_consider_section (ptr, file, s, sec, callback, data); + + sec = sec->next; + } +} + /* Lowest common denominator routine that can handle everything correctly, but slowly. */ @@ -922,6 +983,159 @@ wild_spec_can_overlap (const char *name1, const char *name2) return memcmp (name1, name2, min_prefix_len) == 0; } + +/* Sections are matched against wildcard statements via a prefix tree. + The prefix tree holds prefixes of all matching patterns (up to the first + wildcard character), and the wild statement from which those patterns + came. When matching a section name against the tree we're walking through + the tree character by character. Each statement we hit is one that + potentially matches. This is checked by actually going through the + (glob) matching routines. + + When the section name turns out to actually match we record that section + in the wild statements list of matching sections. */ + +/* A prefix can be matched by multiple statement, so we need a list of them. */ +struct wild_stmt_list +{ + lang_wild_statement_type *stmt; + struct wild_stmt_list *next; +}; + +/* The prefix tree itself. */ +struct prefixtree +{ + /* The list of all children (linked via .next). */ + struct prefixtree *child; + struct prefixtree *next; + /* This tree node is responsible for the prefix of parent plus 'c'. */ + char c; + /* The statements that potentially can match this prefix. */ + struct wild_stmt_list *stmt; +}; + +/* We always have a root node in the prefix tree. It corresponds to the + empty prefix. E.g. a glob like "*" would sit in this root. */ +static struct prefixtree the_root, *ptroot = &the_root; + +/* Given a prefix tree in *TREE, corresponding to prefix P, find or + INSERT the tree node corresponding to prefix P+C. */ + +static struct prefixtree * +get_prefix_tree (struct prefixtree **tree, char c, bool insert) +{ + struct prefixtree *t; + for (t = *tree; t; t = t->next) + if (t->c == c) + return t; + if (!insert) + return NULL; + t = (struct prefixtree *) obstack_alloc (&pt_obstack, sizeof *t); + t->child = NULL; + t->next = *tree; + t->c = c; + t->stmt = NULL; + *tree = t; + return t; +} + +/* Add STMT to the set of statements that can be matched by the prefix + corresponding to prefix tree T. */ + +static void +pt_add_stmt (struct prefixtree *t, lang_wild_statement_type *stmt) +{ + struct wild_stmt_list *sl, **psl; + sl = (struct wild_stmt_list *) obstack_alloc (&pt_obstack, sizeof *sl); + sl->stmt = stmt; + sl->next = NULL; + psl = &t->stmt; + while (*psl) + psl = &(*psl)->next; + *psl = sl; +} + +/* Insert STMT into the global prefix tree. */ + +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; + + if (!stmt->section_list) + { + /* If we have no section_list (no wildcards in the wild STMT), + then every section name will match, so add this to the root. */ + pt_add_stmt (ptroot, stmt); + return; + } + + for (sec = stmt->section_list; sec; sec = sec->next) + { + const char *name = sec->spec.name ? sec->spec.name : ""; + char c; + pt = &ptroot; + t = ptroot; + for (; (c = *name); name++) + { + if (c == '*' || c == '[' || c == '?') + break; + t = get_prefix_tree (&t->child, c, true); + } + /* If we hit a glob character, the matching prefix is what we saw + until now. If we hit the end of pattern (hence it's no glob) then + we can do better: we only need to record a match when a section name + completely matches, not merely a prefix, so record the trailing 0 + 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; + } +} + +/* Dump T indented by INDENT spaces. */ + +static void +debug_prefix_tree_rec (struct prefixtree *t, int indent) +{ + for (; t; t = t->next) + { + struct wild_stmt_list *sl; + printf ("%*s %c", indent, "", t->c); + for (sl = t->stmt; sl; sl = sl->next) + { + struct wildcard_list *curr; + printf (" %p ", sl->stmt); + for (curr = sl->stmt->section_list; curr; curr = curr->next) + printf ("%s ", curr->spec.name ? curr->spec.name : "*"); + } + printf ("\n"); + debug_prefix_tree_rec (t->child, indent + 2); + } +} + +/* Dump the global prefix tree. */ + +static void +debug_prefix_tree (void) +{ + debug_prefix_tree_rec (ptroot, 2); +} + +/* Like strcspn() but start to look from the end to beginning of + S. Returns the length of the suffix of S consisting entirely + of characters not in REJECT. */ + static size_t rstrcspn (const char *s, const char *reject) { @@ -936,8 +1150,8 @@ rstrcspn (const char *s, const char *reject) return sufflen; } -/* Select specialized code to handle various kinds of wildcard - statements. */ +/* Analyze the wildcards in wild statement PTR to setup various + things for quick matching. */ static void analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) @@ -969,6 +1183,8 @@ analyze_walk_wild_section_handler (lang_wild_statement_type *ptr) sec->spec.namelen = sec->spec.prefixlen = sec->spec.suffixlen = 0; } + insert_prefix_tree (ptr); + /* Count how many wildcard_specs there are, and how many of those actually use wildcards in the name. Also, bail out if any of the wildcard names are NULL. (Can this actually happen? @@ -1077,6 +1293,9 @@ walk_wild_file (lang_wild_statement_type *s, } } +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, @@ -1088,12 +1307,119 @@ add_matching_callback (lang_wild_statement_type *ptr, lang_input_statement_type *file, void *data ATTRIBUTE_UNUSED) { - 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; + 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. */ + +static void +resolve_wild_sections (lang_input_statement_type *file) +{ + asection *s; + + if (file->flags.just_syms) + return; + + 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; + 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); + //printf (" ZZZ maybe place into %p\n", sl->stmt); + } + } + 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); + } + } + } +} + +/* Match all sections from all input files against the global prefix tree. */ + +static void +resolve_wilds (void) +{ + check_resolve = false; + LANG_FOR_EACH_INPUT_STATEMENT (f) + { + //printf("XXX %s\n", f->filename); + /* XXX if (walk_wild_file_in_exclude_list (s->exclude_name_list, f)) + return;*/ + + if (f->the_bfd == NULL + || !bfd_check_format (f->the_bfd, bfd_archive)) + resolve_wild_sections (f); + else + { + bfd *member; + + /* This is an archive file. We must map each member of the + archive separately. */ + member = bfd_openr_next_archived_file (f->the_bfd, NULL); + while (member != NULL) + { + /* When lookup_name is called, it will call the add_symbols + entry point for the archive. For each element of the + archive which is included, BFD will call ldlang_add_file, + which will set the usrdata field of the member to the + lang_input_statement. */ + if (bfd_usrdata (member) != NULL) + resolve_wild_sections (bfd_usrdata (member)); + + member = bfd_openr_next_archived_file (f->the_bfd, member); + } + } + } + old_max_section_id = bfd_get_max_section_id (); + check_resolve = true; } static void @@ -1138,6 +1464,9 @@ walk_wild_resolve (lang_wild_statement_type *s) } } +/* For each input section that matches wild statement S calls + CALLBACK with DATA. */ + static void walk_wild (lang_wild_statement_type *s, callback_t callback, void *data) { @@ -1149,7 +1478,7 @@ 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", file_spec ? file_spec : ""); - walk_wild_resolve (s); + //walk_wild_resolve (s); s->resolved = true; s->max_section_id = bfd_get_max_section_id (); } @@ -1505,6 +1834,7 @@ void lang_init (void) { obstack_begin (&stat_obstack, 1000); + obstack_init (&pt_obstack); stat_ptr = &statement_list; @@ -8273,6 +8603,11 @@ lang_process (void) /* Size up the common data. */ lang_common (); + if (0) + debug_prefix_tree (); + + resolve_wilds (); + /* Remove unreferenced sections if asked to. */ lang_gc_sections (); @@ -8283,6 +8618,8 @@ lang_process (void) ldemul_after_check_relocs (); + resolve_wilds (); + /* Update wild statements in case the user gave --sort-section. Note how the option might have come after the linker script and so couldn't have been set when the wild statements were created. */ @@ -8440,6 +8777,13 @@ lang_add_wild (struct wildcard_spec *filespec, new_stmt->max_section_id = 0; lang_list_init (&new_stmt->matching_sections); analyze_walk_wild_section_handler (new_stmt); + if (0) + { + printf ("wild %s(", new_stmt->filename ? new_stmt->filename : "*"); + for (curr = new_stmt->section_list; curr; curr = curr->next) + printf ("%s ", curr->spec.name ? curr->spec.name : "*"); + printf (")\n"); + } } void