From 953ef133271709d51e7dd46728c94fe091e6b97b Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Fri, 10 May 2024 17:51:46 +0930 Subject: ld --sort-sections-file ldgram.y diff --git a/ld/ldgram.y b/ld/ldgram.y index 0d531fddfa1..07c19ba8692 100644 --- a/ld/ldgram.y +++ b/ld/ldgram.y @@ -157,7 +157,7 @@ static void yyerror (const char *); %token LOG2CEIL FORMAT PUBLIC DEFSYMEND BASE ALIAS TRUNCATE REL %token INPUT_SCRIPT INPUT_MRI_SCRIPT INPUT_DEFSYM CASE EXTERN START %token VERS_TAG VERS_IDENTIFIER -%token GLOBAL LOCAL VERSIONK INPUT_VERSION_SCRIPT +%token GLOBAL LOCAL VERSIONK INPUT_VERSION_SCRIPT INPUT_SECTION_ORDERING_SCRIPT %token KEEP ONLY_IF_RO ONLY_IF_RW SPECIAL INPUT_SECTION_FLAGS ALIGN_WITH_INPUT %token EXCLUDE_FILE %token CONSTANT @@ -172,6 +172,7 @@ file: INPUT_SCRIPT script_file | INPUT_MRI_SCRIPT mri_script_file | INPUT_VERSION_SCRIPT version_script_file + | INPUT_SECTION_ORDERING_SCRIPT section_ordering_script_file | INPUT_DYNAMIC_LIST dynamic_list_file | INPUT_DEFSYM defsym_expr ; @@ -1539,6 +1540,39 @@ opt_semicolon: | ';' ; +section_ordering_script_file: + { + ldlex_script (); + PUSH_ERROR (_("section-ordering-file script")); + } + section_ordering_list + { + ldlex_popstate (); + POP_ERROR (); + } + ; + +section_ordering_list: + section_ordering_list section_order + | section_ordering_list statement_anywhere + | + ; + +section_order: NAME ':' + { + ldlex_wild (); + lang_enter_output_section_statement + ($1, NULL, 0, NULL, NULL, NULL, NULL, 0, 0); + } + '{' + statement_list_opt + '}' + { + ldlex_popstate (); + lang_leave_output_section_statement (NULL, NULL, NULL, NULL); + } + opt_comma + %% static void yyerror (const char *arg) diff --git a/ld/ldlex.h b/ld/ldlex.h index 5708e6f5e34..7a0c3b4be94 100644 --- a/ld/ldlex.h +++ b/ld/ldlex.h @@ -478,6 +478,7 @@ typedef enum input_enum input_script, input_mri_script, input_version_script, + input_section_ordering_script, input_dynamic_list, input_defsym } input_type; diff --git a/ld/ldlex.l b/ld/ldlex.l index e113c90812b..aa613100db0 100644 --- a/ld/ldlex.l +++ b/ld/ldlex.l @@ -120,11 +120,12 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)* parser_input = input_selected; switch (t) { - case input_script: return INPUT_SCRIPT; break; - case input_mri_script: return INPUT_MRI_SCRIPT; break; - case input_version_script: return INPUT_VERSION_SCRIPT; break; - case input_dynamic_list: return INPUT_DYNAMIC_LIST; break; - case input_defsym: return INPUT_DEFSYM; break; + case input_script: return INPUT_SCRIPT; + case input_mri_script: return INPUT_MRI_SCRIPT; + case input_version_script: return INPUT_VERSION_SCRIPT; + case input_section_ordering_script: return INPUT_SECTION_ORDERING_SCRIPT; + case input_dynamic_list: return INPUT_DYNAMIC_LIST; + case input_defsym: return INPUT_DEFSYM; default: abort (); } } diff --git a/ld/ldmain.c b/ld/ldmain.c index 16c8298498d..037099b9d37 100644 --- a/ld/ldmain.c +++ b/ld/ldmain.c @@ -465,7 +465,7 @@ main (int argc, char **argv) display_external_script (); saved_script_handle = hold_script_handle; in_section_ordering = true; - parser_input = input_script; + parser_input = input_section_ordering_script; yyparse (); in_section_ordering = false; diff --git a/ld/testsuite/ld-scripts/section-order-1a.t b/ld/testsuite/ld-scripts/section-order-1a.t index e3786818fea..0eacf3b3927 100644 --- a/ld/testsuite/ld-scripts/section-order-1a.t +++ b/ld/testsuite/ld-scripts/section-order-1a.t @@ -1,15 +1,12 @@ -SECTIONS -{ - .text : { - *(.text.yyy) - *(.text.b?r) - *(.text) - *(.text.xxx .text.foo) - } +.text : { + *(.text.yyy) + *(.text.b?r) + *(.text) + *(.text.xxx .text.foo) +} - .data : { - *(.data.small) - *(.big*) - *(.bar .baz*) - } +.data : { + *(.data.small) + *(.big*) + *(.bar .baz*) } diff --git a/ld/testsuite/ld-scripts/section-order-1b.t b/ld/testsuite/ld-scripts/section-order-1b.t index 896653b2608..6a36250dcbc 100644 --- a/ld/testsuite/ld-scripts/section-order-1b.t +++ b/ld/testsuite/ld-scripts/section-order-1b.t @@ -1,9 +1,7 @@ -SECTIONS { - .text : { - *(.text.yyy) - *(.text.b?r) - *(*t) - *(.text.xxx) - *(.text.foo) - } +.text : { + *(.text.yyy) + *(.text.b?r) + *(*t) + *(.text.xxx) + *(.text.foo) }