From patchwork Thu May 11 21:02:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 92848 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4664224vqo; Thu, 11 May 2023 14:02:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5dc7jF9Bft1KTLUUbygVzaIG1FrEHeqFsogA7FiG+SGvh2gbF/xEUKZ2ibMGLy3SP1Nxzx X-Received: by 2002:a17:907:701:b0:94f:236b:9e3a with SMTP id xb1-20020a170907070100b0094f236b9e3amr17628338ejb.3.1683838970645; Thu, 11 May 2023 14:02:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683838970; cv=none; d=google.com; s=arc-20160816; b=HHlx8dZXrTuoTiT3dc53TIz00Oi+o9Ybs/OVfsKRlCh4ZDg+vt0s6wlQX4ED700Lq4 3GKFz9EmvvaXPh5pSLh8vq0b+QpxEUIhasMLK4CfmR5SbOTP4JNq9g+li1EPNlpmBYH5 93poQ24A/6VfJT5yQgJ90MlL53IYS1wn5f815VecDW7k0VITRuZqfSGshN7Sx+g8gfIU IzxGVoPhPRrv5qPlRlUs7WEAGGDZwI/A2eYPHHJt34C9oiXgmCJpn9ILvAZItdSP1bP4 Gq3VDwP9kuC8uNyGLCGkzUBowZcE/RbkkvE4SUK9qwzchnGhZyoc+rYVhKhEQy5I9fXW s7Fw== 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 :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Z8vdu0CbDi14Knnh7lM8C1eWwUU8jzI5WnR63z+EAoc=; b=Hy1Ubu6J1GSe2Zu2CPC79S2EI6Ncs2fyZBNWDY/tRJFy9A1TVU7mgMaS/tbtZNRO/+ YOo4MvwC+sU24CkRXp9DU28eJp/eYtHzqwaC/nVLkGnOxTSZnHE5jtacZNpTdJ1lCLhP Pa6MTb/XZelxocNv0jFctOWUVyyV4dXDw5JSYfmCeY7BVKHgJ1qf6w2eqgaxRjTC40Ks q3OThMGa0Dsze1mKy4FUoieSWNf0WNlOAe4hGf+CGSpk0vttONv6tt733cPZtQDQMHyD U6ksOH1QB6io2rRqZfpGA2iHwY4uAjN8WJNGSODdLLm1v4svjDVlE9K46CK/0Kf6Xyz6 fWDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Qu8qJUMP; 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 q2-20020a1709060e4200b0096605116544si2983196eji.521.2023.05.11.14.02.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 May 2023 14:02:50 -0700 (PDT) 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=Qu8qJUMP; 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 CDAA0385B53D for ; Thu, 11 May 2023 21:02:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CDAA0385B53D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683838966; bh=Z8vdu0CbDi14Knnh7lM8C1eWwUU8jzI5WnR63z+EAoc=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Qu8qJUMPg8nm3oRt6cXnlAunojoDp2BKfITen570r/jaZULxw7heLXfw9i2+QEKFR P5qN/cCSyxYYPFv/byDuvecRHQZvNkk0a51igAfz7rj79OA6zVItKQ8Y30xWRC1DLt QCbyN/qFYFUM2K/63cWsXiTj2TotYUXVEABo909Y= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-oa1-x2f.google.com (mail-oa1-x2f.google.com [IPv6:2001:4860:4864:20::2f]) by sourceware.org (Postfix) with ESMTPS id 6FFE93858D1E for ; Thu, 11 May 2023 21:02:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6FFE93858D1E Received: by mail-oa1-x2f.google.com with SMTP id 586e51a60fabf-192cb642f5dso4010740fac.1 for ; Thu, 11 May 2023 14:02:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683838955; x=1686430955; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Z8vdu0CbDi14Knnh7lM8C1eWwUU8jzI5WnR63z+EAoc=; b=THgVvrgrWqDrePxoc8cQIj5RcpMidf6dBLCcZhDYR2ZC90ZnoTSfYYBNziCv9NP81Z Kr5VAXeUeDpiBk7DGs499iLGdKa5cAAReEV9qHVvhKwMXzlPlXzS4B2e9u1s1gdNrzEC OLqsdgyrfASGjZ5ztSWR7MimlSIbtISe5VXpxEljHhUKRJFMYRYQWOFatPBYAkIOVZN1 noVW2ecDdJBKH2hDhbaGtPKH6FTB0BZnwEX92Y+l4OLW5mNj1y6TSk5abMAp/aC0+v90 Mmo8xsmwfNRHy4LbAmncheBQto2HHoYZUYlCJRzAaXK8hdGRR4m+MYcDDiP8lygZDut8 yVtA== X-Gm-Message-State: AC+VfDzIqlWXguBjc8ptZQsmq59XmF1WCiEc2WgOoyKNO7sfKb0RGBVs uOcJ7B+TCf6T0nKKsJVLk2B6GQ/q5mE= X-Received: by 2002:a05:6870:9183:b0:192:9df1:8587 with SMTP id b3-20020a056870918300b001929df18587mr8521526oaf.8.1683838954864; Thu, 11 May 2023 14:02:34 -0700 (PDT) Received: from noahgold-desk.cust.blueprintrf.com ([12.11.77.130]) by smtp.gmail.com with ESMTPSA id g5-20020a05687085c500b00187e500e7b3sm8400634oal.49.2023.05.11.14.02.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 May 2023 14:02:34 -0700 (PDT) To: binutils@sourceware.org Cc: goldstein.w.n@gmail.com, hjl.tools@gmail.com, arjan@linux.intel.com Subject: [PATCH v1] gold: Make '--section-ordering-file' also specifiable in env variable Date: Thu, 11 May 2023 16:02:32 -0500 Message-Id: <20230511210232.1491265-1-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.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, 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 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: Noah Goldstein via Binutils From: Noah Goldstein Reply-To: Noah Goldstein 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?1765633132691470786?= X-GMAIL-MSGID: =?utf-8?q?1765633132691470786?= New proposed env variable is 'GLOBAL_SECTION_ORDERING_FILE'. The motivation for this change to help package maintainers use the section ordering feature. For package maintainers, modifying the link flags of all their maintained projects, is time consumining to a degree that it is often considered infeasable. By making the feature enablable simply with an environment variable, removes this roadblock. Similiarly, the rationale behind ignoring errors when using the environment specified file is to make it so that it never create new problems in the build process. The hope by making this feature more accessable it will allow package maintainers to use ordering lists from profiles of their applications to distribute better optimized packages. --- gold/gold.cc | 2 +- gold/layout.cc | 17 +++++++++++----- gold/layout.h | 2 +- gold/main.cc | 7 +++++-- gold/options.h | 4 +++- gold/output.cc | 2 +- gold/parameters.cc | 51 ++++++++++++++++++++++++++++++++++++++++++++++ gold/parameters.h | 39 ++++++++++++++++++++++++++++++++++- 8 files changed, 112 insertions(+), 12 deletions(-) diff --git a/gold/gold.cc b/gold/gold.cc index 9fcb1f272d6..c1a6f448d28 100644 --- a/gold/gold.cc +++ b/gold/gold.cc @@ -568,7 +568,7 @@ queue_middle_tasks(const General_options& options, also specified, do not do anything here. */ if (parameters->options().has_plugins() && layout->is_section_ordering_specified() - && !parameters->options().section_ordering_file ()) + && !parameters->get_section_ordering_file ()) { for (Layout::Section_list::const_iterator p = layout->section_list().begin(); diff --git a/gold/layout.cc b/gold/layout.cc index a50086897bb..248844018dc 100644 --- a/gold/layout.cc +++ b/gold/layout.cc @@ -2918,17 +2918,22 @@ Layout::find_section_order_index(const std::string& section_name) // Read the sequence of input sections from the file specified with // option --section-ordering-file. -void +Exit_status Layout::read_layout_from_file() { - const char* filename = parameters->options().section_ordering_file(); + const char* filename = parameters->get_section_ordering_file(); + bool mayfail = parameters->section_ordering_file_mayfail(); std::ifstream in; std::string line; in.open(filename); - if (!in) - gold_fatal(_("unable to open --section-ordering-file file %s: %s"), - filename, strerror(errno)); + if (!in) { + if(mayfail) + gold_fatal(_("unable to open --section-ordering-file file %s: %s"), + filename, strerror(errno)); + else + return GOLD_ERR; + } File_read::record_file_read(filename); @@ -2953,6 +2958,8 @@ Layout::read_layout_from_file() position++; std::getline(in, line); } + + return GOLD_OK; } // Finalize the layout. When this is called, we have created all the diff --git a/gold/layout.h b/gold/layout.h index 5bb9aff5b04..bcfd2e871c4 100644 --- a/gold/layout.h +++ b/gold/layout.h @@ -617,7 +617,7 @@ class Layout // Read the sequence of input sections from the file specified with // linker option --section-ordering-file. - void + Exit_status read_layout_from_file(); // Layout an input reloc section when doing a relocatable link. The diff --git a/gold/main.cc b/gold/main.cc index c6aa1901a95..404aad2085b 100644 --- a/gold/main.cc +++ b/gold/main.cc @@ -174,6 +174,8 @@ main(int argc, char** argv) // Store some options in the globally accessible parameters. set_parameters_options(&command_line.options()); + set_parameters_section_ordering_file_from_env(); + // Do this as early as possible (since it prints a welcome message). write_debug_script(command_line.options().output_file_name(), program_name, args.c_str()); @@ -232,8 +234,9 @@ main(int argc, char** argv) if (layout.incremental_inputs() != NULL) layout.incremental_inputs()->report_command_line(argc, argv); - if (parameters->options().section_ordering_file()) - layout.read_layout_from_file(); + if (parameters->get_section_ordering_file()) + if (layout.read_layout_from_file() == GOLD_ERR) + set_parameters_section_ordering_file_failure(); // Load plugin libraries. if (command_line.options().has_plugins()) diff --git a/gold/options.h b/gold/options.h index 46f658f23ea..bc9bdc5ed13 100644 --- a/gold/options.h +++ b/gold/options.h @@ -1243,7 +1243,9 @@ class General_options N_("Strip LTO intermediate code sections"), NULL); DEFINE_string(section_ordering_file, options::TWO_DASHES, '\0', NULL, - N_("Layout sections in the order specified"), + N_("Layout sections in the order specified. May also be " + "specified using the environment variable " + "'GLOBAL_SECTION_ORDERING_FILE'"), N_("FILENAME")); DEFINE_special(section_start, options::TWO_DASHES, '\0', diff --git a/gold/output.cc b/gold/output.cc index a1978eb5f32..31be6270680 100644 --- a/gold/output.cc +++ b/gold/output.cc @@ -2532,7 +2532,7 @@ Output_section::add_input_section(Layout* layout, /* If section ordering is requested by specifying a ordering file, using --section-ordering-file, match the section name with a pattern. */ - if (parameters->options().section_ordering_file()) + if (parameters->get_section_ordering_file()) { unsigned int section_order_index = layout->find_section_order_index(std::string(secname)); diff --git a/gold/parameters.cc b/gold/parameters.cc index 90bc4db8d46..60d97e66abf 100644 --- a/gold/parameters.cc +++ b/gold/parameters.cc @@ -231,6 +231,49 @@ Parameters::check_rodata_segment() gold_error(_("-Trodata-segment is meaningless without --rosegment")); } +// Set the section_ordering_file using 'GLOBAL_SECTION_ORDERING_FILE' if the +// user-option '--section-ordering-file' wasn't specified. +void +Parameters::set_section_ordering_file_from_env() +{ + const char *ret; + // If we have user-option or already had a failure, nothing to do. + if (this->options().section_ordering_file() + || this->section_ordering_file_has_failed_) + return; + + // Read filepath from environment. + ret = getenv("GLOBAL_SECTION_ORDERING_FILE"); + + // If not set (or error), indicate we should stop trying to return. + if (ret == NULL) + { + this->section_ordering_file_has_failed_ = true; + return; + } + + // Set env file. + section_ordering_file_from_env_ = ret; +} + +// Return section_ordering_file, first checking user-option then checking env +// variable input. +const char * +Parameters::get_section_ordering_file() const +{ + // If we have user-option return it. + const char *ret = this->options().section_ordering_file(); + if (ret) + return ret; + + // If we had any error using the env file then pretend it never existed. + if (this->section_ordering_file_has_failed_) + return NULL; + + // Finally return env file. This may be NULL if it was never specified. + return this->section_ordering_file_from_env_; +} + // Return the name of the entry symbol. const char* @@ -282,6 +325,14 @@ Parameters::incremental_update() const || this->incremental_mode_ == General_options::INCREMENTAL_AUTO); } +void +set_parameters_section_ordering_file_failure() +{ static_parameters.set_section_ordering_file_failure(); } + +void +set_parameters_section_ordering_file_from_env() +{ static_parameters.set_section_ordering_file_from_env(); } + void set_parameters_errors(Errors* errors) { static_parameters.set_errors(errors); } diff --git a/gold/parameters.h b/gold/parameters.h index 76202b186e4..e81506eaa14 100644 --- a/gold/parameters.h +++ b/gold/parameters.h @@ -176,7 +176,36 @@ class Parameters bool incremental_update() const; - private: + // Get section_ordering_file. This first tries the user-option from + // '--section-ordering-file'. It ifs not present, it tries the file from the + // env variable 'GLOBAL_SECTION_ORDERING_FILE'. + const char *get_section_ordering_file() const; + + // Set that we should throw error if we have IO issues with the + // section_order_file. It we are using the user-provided argument we throw + // error in the event of IO issue. If we are using the env version we ignore + // IO errors and stop using the section_ordering_file. + bool + section_ordering_file_mayfail() const + { + return this->section_ordering_file_from_env_ == NULL; + } + + // Only relevent if we are using the env variable section_ordering_file. It + // indicates we failed doing IO on the file, so we want to pretend it never + // existed. + void + set_section_ordering_file_failure() + { + this->section_ordering_file_has_failed_ = true; + } + + // Read environment variable 'GLOBAL_SECTION_ORDERING_FILE' and, if present, + // use its value for the section_ordering_file. Only relevent of user-option + // '--section-ordering-file' is not present. + void set_section_ordering_file_from_env(); + +private: void set_target_once(Target*); @@ -197,6 +226,8 @@ class Parameters int debug_; int incremental_mode_; Set_parameters_target_once* set_parameters_target_once_; + const char *section_ordering_file_from_env_ = NULL; + bool section_ordering_file_has_failed_ = false; }; // This is a global variable. @@ -205,6 +236,12 @@ extern const Parameters* parameters; // We use free functions for these since they affect a global variable // that is internal to parameters.cc. +extern void +set_parameters_section_ordering_file_failure(); + +extern void +set_parameters_section_ordering_file_from_env(); + extern void set_parameters_errors(Errors* errors);