From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id D32283858C50 for ; Fri, 26 Apr 2024 12:43:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D32283858C50 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D32283858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::436 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714135435; cv=none; b=fbykbcY+BORDbN16yOsFTzSjhWlWMgXt3cnUxpb9xQhCfSYyXmrzb4VXu2MTzOc1udN1Uv525sK4huHQE/BmNj0U4yp90tQDrrCg1SWTKEVR8WHQ3fAptMakkDA8mTlkAxmotN9DGS6viH+6OfV63YnP48EWvlYK87We468ZVoY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714135435; c=relaxed/simple; bh=cbCCErLW9UahZ9gyxbNqNnlbBTfCY2Gp1+yqLz7xFHg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=HqqyFemKY22RxAmZ+PC2W9sJnKy7vVJ7GX2CBslrsL8eqIdmDbvbj5pSz6ywzdETa++zIcB/lCkNTehTCMBQ0LEHB6MzszV3QniQMeDDPQ9XZZEjOEMcLPznHYiJtZHUujocoySbLCqKt4n4u51AKaJcz3foUTZ6dvNAj/ihjv0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6f28bb6d747so1848504b3a.3 for ; Fri, 26 Apr 2024 05:43:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714135432; x=1714740232; darn=sourceware.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=vlNNNo+2FJS1e2WzWGpXx1t0miaTw186QVlO8rsZcy0=; b=SXakvmMs57s9Ig0c3CgOsfwXMRpDY8uuzOVNr8MwR62FR03Y5sAlLJOFmasf6E0LvR k2wu/USt6GmkjEbK0C0jCV55LjKavlhc8R9nri6oQ8WWCju0NZ+2TMedaj42YwnnPAel u9DMB0LX9XNzClMRgpivVKfKrNTNHbjGKcblveUoQN0ezUtkUUyTcnzvOWmdZW/cNoOm s2ToTp6vBOGrHjrWfPGKllaCqE6yVG650jScmuJqoozpE+Hh0wQilUNl50HIUe56P1HD utKDTaZB2qa6EGcNbKiQkJp7sesbRP/DJz7xd1myvlKOK/5j1OSrEa+m6sn/Yt58FdqI scJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714135432; x=1714740232; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=vlNNNo+2FJS1e2WzWGpXx1t0miaTw186QVlO8rsZcy0=; b=ZOVFD/cWkGEzYQi/dQff4psVJvjRofXUHv8lo7z1SYJj54nxIyqf/6XtnSd5rv6G45 jy6NY4oR6XGPxMSBgTq3FqJGgjWh8fMjinr897ZDA+AGrPSuYekjPrGbQ6pWAiMEq6+E ZiUovkWzRrqORs0xnoK+AvCW+aSPfqXZLT1uSf43DB1ulRInsXKRIPWGvNIkis7LfKjd C3WwNoLE4YkBMDLJHeU/6S4YWME/FyFRI3r7mUri+VV8Ch6mpxxmFT+ufKaDlVjJ8GxC wmJ7tNkdVcM3d9zf/prH9OGvaP+w5pKXUehg9BKW1wtHVRcWOQPDStkBEartvmCWgS/H ABTw== X-Forwarded-Encrypted: i=1; AJvYcCVSdhlHzzpfXPKZ7kptq+VN6OAEa0oqJ+dtpCK/UOOaSU9nc1a5EFaAitDc4Jwl3WJtCCiw4Xa5pDC5uAe8NmanJWOe+c1Fdw== X-Gm-Message-State: AOJu0YwjVBdxvPmKNidgJ8LPVD2Flfa54kLssLGvu645PjSyojsPEpB6 pstW4anqLA0z9ap2aEs2QpCSxc1Mx951RxDzvoobPAWwT0WvR7OtTMhLvA== X-Google-Smtp-Source: AGHT+IFO4DjjHZ1xYJ/39WbAi5Dy4ZxlpPkyydZPR7PqKjHLskrGW+4buTQX4BM7mbD81uI5wWzJ1w== X-Received: by 2002:a05:6a21:271e:b0:1ac:4272:5f88 with SMTP id rm30-20020a056a21271e00b001ac42725f88mr2365692pzb.17.1714135431385; Fri, 26 Apr 2024 05:43:51 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:ebac:394:f596:1fd2]) by smtp.gmail.com with ESMTPSA id y8-20020aa78048000000b006f2e10b00d6sm8639458pfm.41.2024.04.26.05.43.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 05:43:51 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id A9A5911402C0; Fri, 26 Apr 2024 22:13:48 +0930 (ACST) Date: Fri, 26 Apr 2024 22:13:48 +0930 From: Alan Modra To: Nick Clifton Cc: hjl.tools@gmail.com, binutils@sourceware.org, siddhesh@redhat.com Subject: Re: RFC: ld: Add --text-section-ordering-file (version 2) Message-ID: References: <87edat7g1e.fsf@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-3033.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,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 List-Id: On Fri, Apr 26, 2024 at 10:59:00AM +0100, Nick Clifton wrote: > It could be done, but I like keeping things simple. Here's a completely untested implementation of the idea I'm proposing, written just to show that the linker modifications are relatively small. (There is a very good chance I've messed up list handling..) What appears inside the --section-ordering-file output sections is spliced in to corresponding output sections in the default script, before the first input wild statement. I like keeping things simple too. This should work without modifying default linker scripts (and user scripts!), and allows all the usual ways of specifying input sections. That means you can use constructs like EXCLUDE_FILE, SORT, KEEP, *(.text .rdata) vs. *(.text) *(.rdata) and so on. diff --git a/ld/ld.h b/ld/ld.h index fcdd9a2c083..15d9dc262b6 100644 --- a/ld/ld.h +++ b/ld/ld.h @@ -196,6 +196,8 @@ typedef struct /* Default linker script. */ char *default_script; + + char *section_ordering_file; } args_type; extern args_type command_line; @@ -325,6 +327,7 @@ extern ld_config_type config; extern FILE * saved_script_handle; extern bool force_make_executable; +extern bool in_section_ordering; extern int yyparse (void); extern void add_cref (const char *, bfd *, asection *, bfd_vma); diff --git a/ld/ldlang.c b/ld/ldlang.c index 54d1af62ebe..f9fb50e99ec 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -1299,6 +1299,7 @@ output_section_statement_newfunc (struct bfd_hash_entry *entry, ret->s.output_section_statement.section_alignment = NULL; ret->s.output_section_statement.block_value = 1; lang_list_init (&ret->s.output_section_statement.children); + lang_list_init (&ret->s.output_section_statement.sort_children); lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next); /* For every output section statement added to the list, except the @@ -7613,13 +7614,19 @@ lang_enter_output_section_statement (const char *output_section_statement_name, lang_output_section_statement_type *os; os = lang_output_section_statement_lookup (output_section_statement_name, - constraint, 2); + constraint, + in_section_ordering ? 1 : 2); current_section = os; + /* Make next things chain into subchain of this. */ + push_stat_ptr (in_section_ordering ? &os->sort_children : &os->children); + + if (in_section_ordering) + return os; + if (os->addr_tree == NULL) - { - os->addr_tree = address_exp; - } + os->addr_tree = address_exp; + os->sectype = sectype; if (sectype == type_section || sectype == typed_readonly_section) os->sectype_value = sectype_value; @@ -7629,9 +7636,6 @@ lang_enter_output_section_statement (const char *output_section_statement_name, os->flags = SEC_NO_FLAGS; os->block_value = 1; - /* Make next things chain into subchain of this. */ - push_stat_ptr (&os->children); - os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT; if (os->align_lma_with_input && align != NULL) einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"), @@ -8127,9 +8131,33 @@ reset_resolved_wilds (void) lang_for_each_statement (reset_one_wild); } +static void +lang_os_merge_sort_children (void) +{ + lang_output_section_statement_type *os; + for (os = (void *) lang_os_list.head; os != NULL; os = os->next) + { + if (os->sort_children.head != NULL) + { + lang_statement_union_type **where; + for (where = &os->children.head; + *where != NULL; + where = &(*where)->header.next) + if ((*where)->header.type == lang_wild_statement_enum) + break; + if (*where == NULL) + where = &os->children.head; + lang_list_insert_after (&os->children, &os->sort_children, where); + lang_list_init (&os->sort_children); + } + } +} + void lang_process (void) { + lang_os_merge_sort_children (); + /* Finalize dynamic list. */ if (link_info.dynamic_list) lang_finalize_version_expr_head (&link_info.dynamic_list->head); @@ -8817,6 +8845,10 @@ lang_leave_output_section_statement (fill_type *fill, const char *memspec, lang_output_section_phdr_list *phdrs, const char *lma_memspec) { + pop_stat_ptr (); + if (in_section_ordering) + return; + lang_get_regions (¤t_section->region, ¤t_section->lma_region, memspec, lma_memspec, @@ -8825,7 +8857,6 @@ lang_leave_output_section_statement (fill_type *fill, const char *memspec, current_section->fill = fill; current_section->phdrs = phdrs; - pop_stat_ptr (); } /* Set the output format type. -oformat overrides scripts. */ diff --git a/ld/ldlang.h b/ld/ldlang.h index ea1c26d00f3..91947c56fda 100644 --- a/ld/ldlang.h +++ b/ld/ldlang.h @@ -162,6 +162,8 @@ typedef struct lang_output_section_statement_struct lang_output_section_phdr_list *phdrs; + lang_statement_list_type sort_children; + /* Used by ELF SHF_LINK_ORDER sorting. */ void *data; diff --git a/ld/ldlex.h b/ld/ldlex.h index d575562a357..5708e6f5e34 100644 --- a/ld/ldlex.h +++ b/ld/ldlex.h @@ -62,6 +62,7 @@ enum option_values OPTION_SONAME, OPTION_SORT_COMMON, OPTION_SORT_SECTION, + OPTION_SECTION_ORDERING_FILE, OPTION_STATS, OPTION_SYMBOLIC, OPTION_SYMBOLIC_FUNCTIONS, diff --git a/ld/ldmain.c b/ld/ldmain.c index fe389681bd3..77b57a9fd32 100644 --- a/ld/ldmain.c +++ b/ld/ldmain.c @@ -90,6 +90,8 @@ bool version_printed; /* TRUE if we should demangle symbol names. */ bool demangling; +bool in_section_ordering; + args_type command_line; ld_config_type config; @@ -446,6 +448,19 @@ main (int argc, char **argv) info_msg ("\n==================================================\n"); } + if (command_line.section_ordering_file) + { + FILE *hold_script_handle; + + hold_script_handle = saved_script_handle; + ldfile_open_command_file (command_line.section_ordering_file); + saved_script_handle = hold_script_handle; + in_section_ordering = true; + parser_input = input_script; + yyparse (); + in_section_ordering = false; + } + if (command_line.force_group_allocation || !bfd_link_relocatable (&link_info)) link_info.resolve_section_groups = true; diff --git a/ld/lexsup.c b/ld/lexsup.c index dad3b6059ed..0c337a4d170 100644 --- a/ld/lexsup.c +++ b/ld/lexsup.c @@ -487,6 +487,9 @@ static const struct ld_option ld_options[] = { {"sort-section", required_argument, NULL, OPTION_SORT_SECTION}, '\0', N_("name|alignment"), N_("Sort sections by name or maximum alignment"), TWO_DASHES }, + { {"section-ordering-file", required_argument, NULL, OPTION_SECTION_ORDERING_FILE}, + '\0', N_("FILE"), + N_("Sort sections by statements in 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 }, @@ -1400,6 +1403,9 @@ parse_args (unsigned argc, char **argv) einfo (_("%F%P: invalid section sorting option: %s\n"), optarg); break; + case OPTION_SECTION_ORDERING_FILE: + command_line.section_ordering_file = optarg; + break; case OPTION_STATS: config.stats = true; break; -- Alan Modra