From patchwork Mon Mar 10 08:26:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kito Cheng X-Patchwork-Id: 107576 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17B563858C2F for ; Mon, 10 Mar 2025 08:28:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17B563858C2F Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=U3hgDogv X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by sourceware.org (Postfix) with ESMTPS id 591A13858D21 for ; Mon, 10 Mar 2025 08:26:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 591A13858D21 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 591A13858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741595171; cv=none; b=wQSKffroJLgzCbH/ZsR/pyTsEj4q1NGQwrjSvaZIADX6L33PfyADusHVZz9RD1mV1qbc8hhauPZNS+J2+4lJRWsVo7Knor8C/BmWoXsB+Nm+3oDr0mr2G/mWI7/mkyYrwXrOwyGc+i6r59Pa0dfMcoCkkfrvPUvqN3CYqAQOsfg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741595171; c=relaxed/simple; bh=8f54p3tx1yXre9pyO6qntWHjog1RcQTrmXAlSF/+Oss=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=bkF2R09HdAPUVnz9MMt3DMd2GS2UnIjE7JtLyt4ovrVsGG8nGC2B33YvW8e+ysusBrm9YaOQeMcFBfPfPbd0KOwp51pCgQbcIvcJygj63K3GbQIaPkkB5DvQ7z34LBI72NlsHhBCHwpv8cIqiq21tcfgqS61PfIHqrG6tqRbuKY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 591A13858D21 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2255003f4c6so20312775ad.0 for ; Mon, 10 Mar 2025 01:26:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1741595169; x=1742199969; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=aNQPd67ErCcrpO+PKPfPVPAATiD4V4GskRR0dmwZcPc=; b=U3hgDogvRMBcfyCayd1p9xv1yeWmkvUFSBiWj0FrGvtnioxJeKmiMN38e7MFWlFM6z Z365Rcf1P3NyKlQer+tJ/Yyjs6HcJWH0G03a75FqiGku/7htQXgAhnV5GSMT1+Dytudt sQJsyUFGLolZ5HVghKw4Kiz6YAovDDJQm4444DUMx1/QeYnfUiYnKO0VjkBDy4EwGvXN S1u0dcVh2ytvEAHj5Mp7vp15CQ5hU5i47Ql8CB3gGKwtTudq64evf2pS/vCfD93uI+4I P3iHSnP9NL6nfNF/kh8it443H9HmFA+Hh+LqsW20UbN/iPct9g9XBsL3zvuUL7B1dDEN erNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741595169; x=1742199969; 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=aNQPd67ErCcrpO+PKPfPVPAATiD4V4GskRR0dmw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VysIBmls5Mqe8MUw== X-Received: by 2002:a05:6a00:810:b0:730:4c55:4fdf with SMTP id d2e1a72fcca58-736aa9da8ebmr18069409b3a.7.1741595168500; Mon, 10 Mar 2025 01:26:08 -0700 (PDT) Received: from hsinchu18.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736cf475dd8sm2405446b3a.101.2025.03.10.01.26.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 01:26:08 -0700 (PDT) From: Kito Cheng To: gcc-patches@gcc.gnu.org, kito.cheng@gmail.com, palmer@dabbelt.com, jeffreyalaw@gmail.com, jinma@linux.alibaba.com Cc: Kito Cheng Subject: [PATCH v2] driver: Fix multilib_os_dir and multiarch_dir for those target use TARGET_COMPUTE_MULTILIB Date: Mon, 10 Mar 2025 16:26:04 +0800 Message-Id: <20250310082604.1079693-1-kito.cheng@sifive.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org This patch fixes the multilib_os_dir and multiarch_dir for those targets that use TARGET_COMPUTE_MULTILIB, since the TARGET_COMPUTE_MULTILIB hook only update/fix the multilib_dir but not the multilib_os_dir and multiarch_dir, so the multilib_os_dir and multiarch_dir are not set correctly for those targets. Use RISC-V linux target (riscv64-unknown-linux-gnu) as an example: ``` $ riscv64-unknown-linux-gnu-gcc -print-multi-lib .; lib32/ilp32;@march=rv32imac@mabi=ilp32 lib32/ilp32d;@march=rv32imafdc@mabi=ilp32d lib64/lp64;@march=rv64imac@mabi=lp64 lib64/lp64d;@march=rv64imafdc@mabi=lp64d ``` If we use the exactly same -march and -mabi options to compile a source file, the multilib_os_dir and multiarch_dir are set correctly: ``` $ riscv64-unknown-linux-gnu-gcc -print-multi-os-directory -march=rv64imafdc -mabi=lp64d ../lib64/lp64d $ riscv64-unknown-linux-gnu-gcc -print-multi-directory -march=rv64imafdc -mabi=lp64d lib64/lp64d ``` However if we use the -march=rv64imafdcv -mabi=lp64d option to compile a source file, the multilib_os_dir and multiarch_dir are not set correctly: ``` $ riscv64-unknown-linux-gnu-gcc -print-multi-os-directory -march=rv64imafdc -mabi=lp64d lib64/lp64d $ riscv64-unknown-linux-gnu-gcc -print-multi-directory -march=rv64imafdc -mabi=lp64d lib64/lp64d ``` That's because the TARGET_COMPUTE_MULTILIB hook only update/fix the multilib_dir but not the multilib_os_dir, so the multilib_os_dir is blank and will use same value as multilib_dir, but that is not correct. So we introduce second chance to fix the multilib_os_dir if it's not set, we do also try to fix the multiarch_dir, because it may also not set correctly if multilib_os_dir is not set. Changes since v1: - Fix non-multilib build. - Fix fix indentation. gcc/ChangeLog: * gcc.c (find_multilib_os_dir_by_multilib_dir): New. (set_multilib_dir): Fix multilib_os_dir and multiarch_dir if multilib_os_dir is not set. --- gcc/gcc.cc | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/gcc/gcc.cc b/gcc/gcc.cc index 04b3736a5da..89944c33133 100644 --- a/gcc/gcc.cc +++ b/gcc/gcc.cc @@ -9736,6 +9736,103 @@ default_arg (const char *p, int len) return 0; } +/* Use multilib_dir as key to find corresponding multilib_os_dir and + multiarch_dir. */ + +static void +find_multilib_os_dir_by_multilib_dir (const char *multilib_dir, + const char **p_multilib_os_dir, + const char **p_multiarch_dir) +{ + const char *p = multilib_select; + unsigned int this_path_len; + const char *this_path; + int ok = 0; + + while (*p != '\0') + { + /* Ignore newlines. */ + if (*p == '\n') + { + ++p; + continue; + } + + /* Get the initial path. */ + this_path = p; + while (*p != ' ') + { + if (*p == '\0') + { + fatal_error (input_location, "multilib select %qs %qs is invalid", + multilib_select, multilib_reuse); + } + ++p; + } + this_path_len = p - this_path; + + ok = 0; + + /* Skip any arguments, we don't care at this stage. */ + while (*++p != ';'); + + if (this_path_len != 1 + || this_path[0] != '.') + { + char *new_multilib_dir = XNEWVEC (char, this_path_len + 1); + char *q; + + strncpy (new_multilib_dir, this_path, this_path_len); + new_multilib_dir[this_path_len] = '\0'; + q = strchr (new_multilib_dir, ':'); + if (q != NULL) + *q = '\0'; + + if (strcmp (new_multilib_dir, multilib_dir) == 0) + ok = 1; + } + + /* Found matched multilib_dir, update multilib_os_dir and + multiarch_dir. */ + if (ok) + { + const char *q = this_path, *end = this_path + this_path_len; + + while (q < end && *q != ':') + q++; + if (q < end) + { + const char *q2 = q + 1, *ml_end = end; + char *new_multilib_os_dir; + + while (q2 < end && *q2 != ':') + q2++; + if (*q2 == ':') + ml_end = q2; + if (ml_end - q == 1) + *p_multilib_os_dir = xstrdup ("."); + else + { + new_multilib_os_dir = XNEWVEC (char, ml_end - q); + memcpy (new_multilib_os_dir, q + 1, ml_end - q - 1); + new_multilib_os_dir[ml_end - q - 1] = '\0'; + *p_multilib_os_dir = new_multilib_os_dir; + } + + if (q2 < end && *q2 == ':') + { + char *new_multiarch_dir = XNEWVEC (char, end - q2); + memcpy (new_multiarch_dir, q2 + 1, end - q2 - 1); + new_multiarch_dir[end - q2 - 1] = '\0'; + *p_multiarch_dir = new_multiarch_dir; + } + break; + } + } + ++p; + } +} + /* Work out the subdirectory to use based on the options. The format of multilib_select is a list of elements. Each element is a subdirectory name followed by a list of options followed by a semicolon. The format @@ -10014,7 +10111,16 @@ set_multilib_dir (void) multilib_os_dir = NULL; } else if (multilib_dir != NULL && multilib_os_dir == NULL) - multilib_os_dir = multilib_dir; + { + /* Give second chance to search matched multilib_os_dir again by matching + the multilib_dir since some target may use TARGET_COMPUTE_MULTILIB + hook rather than the builtin way. */ + find_multilib_os_dir_by_multilib_dir (multilib_dir, &multilib_os_dir, + &multiarch_dir); + + if (multilib_os_dir == NULL) + multilib_os_dir = multilib_dir; + } } /* Print out the multiple library subdirectory selection