From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11olkn2010.outbound.protection.outlook.com [40.92.20.10]) by sourceware.org (Postfix) with ESMTPS id 839843858422 for ; Fri, 19 Jan 2024 21:50:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 839843858422 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=maskray.me Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=maskray.me ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 839843858422 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=40.92.20.10 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1705701038; cv=pass; b=gkFdgiM1ndu0fwYO/89Ypjme0tn/3KDLswt+Ivl95LdemfRQ9cF2VjI7MIguVJ/QwfVj3XLKp+KkFCpXMShMfME/XrFzryeA2+IBnu/BMFx/zpeKaxTQvYOc31LRP5TwgUFoCrp9aeNRCyg6xkr0RQIE/vuCLUA7SIBPO9XTC68= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1705701038; c=relaxed/simple; bh=5bwzOOFUHHkafGQ8vhET2v8mNsTIBkqYEB6xTC6IBKA=; h=From:Date:Message-ID:Subject:To:MIME-Version; b=wxeYK9JrCQiLYWcXM+ebEn+9GiasscfiuuBtriwavtuSFOPWvAF7V8aj5WeLh3F3JAqkb/XLZKbqD5nLzuykn9OjEY3A9/3ML9JfTfR9UFKxJY/DCxgcnEuai+ptsxYIQgl5WHkLgoF5qmfFP6rlfLRtjM5NAaphbynQRUouIS4= ARC-Authentication-Results: i=2; server2.sourceware.org ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OsvKywqnoqyibnpEKY3JKqn9jEtxJ0s86IbY2xRJ06dlrHALeCvzkvr/rQ5KOaPCzGJTBt351NUbq/A1vayuW+Sbh1nn/eK9tqGatrSVrvcGdchtWf65cgAMAm5HRPnX5TbaMyOJ7VHUQhbOhK1d7dQayoI15MuCSYR5GQ27eIYlz0cbCQ5uABA5/vV6DEvA+4Z0SwT0KTHIGwn4PHGq3Fj5i83tML1PQ3M2frB2/3IXTDZKwt+hCibXyICjavXg2xxOEe2gftlUO3L6eSS1Exi9KIfCDrWFPbmNzRBHccNsXDy59RCeS84jvmF4L8G+/pO+2Fw+qoCGcXmo4CqSig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=AfkCY3S/2R9kEp8voXhCOE1uMFm7HOEvDZ0OKSrQTbA=; b=luCsNfJ2aTLC6ItGm5kHw+YQMfk5JN5+R1+WxSn/9C1SSfgM94JSIZuwfg+jHVw03Oa4elMKFQ5/2rG9kchhxdZMIxRzJK2q4zRTjVXa1U6zdd1rxqOtHLhEaQJagxfI45NplicVNO7oMDZs/6ZdeMdOLGAbpT/d7DbqV6Dt2/h3ocyliq7dmH0igXsF1htEIH5dZhZJD1hPtn+QGfiTzmkYvGAxS1NU3CaaL4Dgh5fyHjjPGjENRBTdfNR628KfF9nGWJ/6Ath5uE7plsaYroLpGrEvLFJleP7PaBczLDvdXb9xa645tMvhKB3ippBu+laJ2T3QzyO9SJ+VShwrOw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from DS7PR12MB5765.namprd12.prod.outlook.com (2603:10b6:8:74::19) by CH0PR12MB5251.namprd12.prod.outlook.com (2603:10b6:610:d2::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.24; Fri, 19 Jan 2024 21:50:32 +0000 Received: from DS7PR12MB5765.namprd12.prod.outlook.com ([fe80::fa25:6d58:7ae9:9c0b]) by DS7PR12MB5765.namprd12.prod.outlook.com ([fe80::fa25:6d58:7ae9:9c0b%5]) with mapi id 15.20.7202.024; Fri, 19 Jan 2024 21:50:32 +0000 X-Gm-Message-State: AOJu0Yz5haKxzP+VC9xoWXACyNsmE9X1KPwukNjvl8VHh9NYflYOM/Vm AP5e7M3EaYOjJjQ8IpYNM4JEYt1uFHu2utU6wtapmkQ5wGDPgOBvnDpaLM+yz7C5S13Ugs2g+Zl OZvZa9P6P25loW48yqLmyqdVcTyg= X-Google-Smtp-Source: AGHT+IHBOtggak+FV4Q9/wJWcbgdT0U14uIS7Lmfov/kZVVniH0tjTpR2C10sHuZyugDPDl7lCT7eb2sLRiGz6cwcGA= X-Received: by 2002:a1f:7c0c:0:b0:4b6:be99:4acb with SMTP id x12-20020a1f7c0c000000b004b6be994acbmr412168vkc.16.1705701029963; Fri, 19 Jan 2024 13:50:29 -0800 (PST) References: <20240119194552.1255481-1-hjl.tools@gmail.com> <20240119194552.1255481-2-hjl.tools@gmail.com> In-Reply-To: <20240119194552.1255481-2-hjl.tools@gmail.com> From: Fangrui Song Date: Fri, 19 Jan 2024 13:50:18 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 1/4] ld: Add --text-section-ordering-file FILE To: "H.J. Lu" Cc: binutils@sourceware.org, goldstein.w.n@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-TMN: [7h0FEWbw7b6TKzjBBuqvzuGOYzZg8nXQ] X-ClientProxiedBy: BN0PR04CA0050.namprd04.prod.outlook.com (2603:10b6:408:e8::25) To DS7PR12MB5765.namprd12.prod.outlook.com (2603:10b6:8:74::19) X-Microsoft-Original-Message-ID: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR12MB5765:EE_|CH0PR12MB5251:EE_ X-MS-Office365-Filtering-Correlation-Id: 3411b3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hjdWhscGNPTk1YbE1iaDVq?= =?utf-8?B?TXdUZTdJOFZGaFdLUlFjY2UwdE9JcnNKdDJnZGt2cG1FU2dpVnJmY1FJVlVB?= =?utf-8?B?bE11Z3NveWRIL2t4S0pURjFnenNUN1diZDhqandwbmRPNG1OYWZJZS8zY1JE?= =?utf-8?B?RGdLdGZWc3owSG0wZGdNRGtxQWJNTFBYRVdrekhhTncybHplM2tvT0l6SXJv?= =?utf-8?B?QTl3RS9laDRjSEU1MGVPbzBObkwySkJQdFhuMm9DcFdGRUcxME5yY1B2T0lO?= =?utf-8?Q?Lm8diosluD+7TH7ROlSZTuYeNnnqMci6IaUxzTiBoRANYS?= X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-5183d.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 3411b3ac-aee0-46df-8d95-08dc1938a871 X-MS-Exchange-CrossTenant-AuthSource: DS7PR12MB5765.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jan 2024 21:50:31.8130 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5251 X-Spam-Status: No, score=-6.3 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_ASCII_DIVIDERS,KAM_DMARC_STATUS,KAM_INFOUSMEBIZ,KAM_SHORT,RCVD_IN_BL_SPAMCOP_NET,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Jan 19, 2024 at 11:45=E2=80=AFAM H.J. Lu wrot= e: > > 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-orderi= ng-filefile This option (--symbol-ordering-file=3D) 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=3D95095) 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. > (<>): 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 =3D=3D NULL) > + if (config.text_section_ordering_file !=3D NULL > + && strcmp (orig_name, config.text_section_ordering_file) =3D=3D= 0) > + { > + if (result =3D=3D NULL) > + info_msg (_("cannot find text section ordering file: %s\n"), > + name); > + else > + { > + static const int ld_bufsz =3D 8193; > + size_t n; > + char *buf =3D (char *) xmalloc (ld_bufsz); > + > + info_msg (_("opened text section ordering file: %s\n"), > + name); > + info_msg ("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); > + > + while ((n =3D fread (buf, 1, ld_bufsz - 1, result)) > 0) > + { > + buf[n] =3D 0; > + info_msg ("%s", buf); > + } > + rewind (result); > + free (buf); > + > + info_msg ("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D\n\n"); > + } > + } > + else if (result =3D=3D 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 =3D try_open (name, sysrooted); > + result =3D try_open (name, name, sysrooted); > if (result !=3D NULL) > return result; > } > @@ -859,7 +886,7 @@ ldfile_find_command_file (const char *name, > search =3D search->next) > { > path =3D concat (search->name, slash, name, (const char *) NULL); > - result =3D try_open (path, sysrooted); > + result =3D try_open (path, name, sysrooted); > free (path); > if (result) > break; > @@ -908,6 +935,38 @@ ldfile_open_command_file_1 (const char *name, enum s= cript_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") =3D=3D 0) > + { > + /* Support > + > + INCLUDE config.text_section_ordering_file; > + > + in input text sections in linker script. */ > + if (config.text_section_ordering_file =3D=3D 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 =3D config.text_section_ordering_file; > + > + /* Set the in the text section ordering file marker. */ > + in_text_section_ordering_file =3D 1; > + } > + else > + in_text_section_ordering_file =3D 0; > + > /* FIXME: This memory is never freed, but that should not really matte= r. > It will be released when the linker exits, and it is unlikely to ev= er > be more than a few tens of bytes. */ > @@ -932,7 +991,12 @@ ldfile_open_command_file_1 (const char *name, enum s= cript_open_style open_how) > > lineno =3D 1; > > - saved_script_handle =3D ldlex_input_stack; > + /* Clear the end of the include file marker. */ > + seen_eof_include_file =3D 0; > + > + /* The text section ordering file isn't a real linker script file. */ > + if (!in_text_section_ordering_file) > + saved_script_handle =3D 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 !=3D NULL && filespec->name !=3D NULL) > { > - if (strcmp (filespec->name, "*") =3D=3D 0) > - filespec->name =3D NULL; > - else if (!wildcardp (filespec->name)) > - lang_has_input_file =3D true; > + if (in_text_section_ordering_file !=3D 0 > + && section_list =3D=3D NULL > + && filespec->name[0] =3D=3D '.' > + && filespec->sorted =3D=3D none > + && filespec->exclude_name_list =3D=3D NULL > + && filespec->section_flag_list =3D=3D 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 > + =3D (struct wildcard_list *) xmalloc (sizeof (*single_section= )); > + memset (single_section, 0, sizeof (*single_section)); > + single_section->spec.name =3D filespec->name; > + single_section->spec.sorted =3D none; > + /* A NULL indicates the wild card file name, "*". */ > + filespec->name =3D NULL; > + section_list =3D single_section; > + } > + else > + { > + if (strcmp (filespec->name, "*") =3D=3D 0) > + filespec->name =3D NULL; > + else if (!wildcardp (filespec->name)) > + lang_has_input_file =3D 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 !=3D 0 > + && seen_eof_include_file !=3D 0) > + in_text_section_ordering_file =3D 0; > + > new_stmt =3D new_stat (lang_wild_statement, stat_ptr); > new_stmt->filename =3D NULL; > new_stmt->filenames_sorted =3D 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 =3D 0; > + > +/* 1 if the end of the include file is reached. */ > +int seen_eof_include_file =3D 0; > + > /* The string we are currently lexing, or NULL if we are reading a > file. */ > const char *lex_string =3D NULL; > @@ -487,6 +493,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-= 9\[\]\-\!\^\\]|::)* > > lineno =3D lineno_stack[include_stack_ptr]; > input_flags.sysrooted =3D sysrooted_stack[include_stack_ptr]; > + seen_eof_include_file =3D 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[] =3D > { {"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 =3D dynamic_list_unset; > struct bfd_elf_dynamic_list *export_list =3D NULL; > + bool seen_linker_script =3D false; > > shortopts =3D (char *) xmalloc (OPTION_COUNT * 3 + 2); > longopts =3D (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 =3D optarg; > + break; > case OPTION_STATS: > config.stats =3D true; > break; > @@ -1410,6 +1420,7 @@ parse_args (unsigned argc, char **argv) > ++trace_files; > break; > case 'T': > + seen_linker_script =3D true; > previous_script_handle =3D saved_script_handle; > ldfile_open_script_file (optarg); > parser_input =3D 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 < .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/st= art.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-sc= ripts/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-sc= ripts/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-sc= ripts/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-sc= ripts/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-sc= ripts/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-sc= ripts/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-sc= ripts/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 orderin= g file: 'text-order-1c.t' > diff --git a/ld/testsuite/ld-scripts/text-order-1c.t b/ld/testsuite/ld-sc= ripts/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-scr= ipts/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 >