From nobody Wed Nov 27 10:44:48 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C6CB1CF7C3 for ; Thu, 10 Oct 2024 19:24:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728588251; cv=none; b=mVjZF/UYNMcOV0C0PRuNZi7kPisyzUCNLHp0WDv6jeWaosIOqj5+N4IdMOIyYMcEzN5Xpn9i15ye7DusjcybWSkkQWDqftuU7JRSDfMa8EuMibSxKhdOkZ8k9wM0M4PlltXLqMWerJKqLctR+znjH4auaHocCt5YA3e5tb62fzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728588251; c=relaxed/simple; bh=B/fTpCzd8QlJMQ3iEfz2aTZvNZTM1lTyUzt5b6yEoHA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Yy6XZw4KoRqdOFWvhGuFSpS3TNH7T451kTLqFugg9siqAqOr0r8Rm6Y7WFYQnqo3mOMib3F78vE+JGN7U9gWEoKjdSdRD05aiwuSkmD7JhRnjVLGv7pi3LLYFr8VkyCtuCu/pxsLsXMUyz42wf4kgDX2dA9hFRWKlh2B038KKog= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--xur.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qs5hkC9D; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--xur.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qs5hkC9D" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6dbbeee08f0so35650797b3.0 for ; Thu, 10 Oct 2024 12:24:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728588249; x=1729193049; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pwVGeNhEiYELCNV51bxA2EzTaJbA2K5Fr027Br3jO4Q=; b=qs5hkC9DxaPqNC52LcXmgLYggJaqQWSGbKzkQ45ToHblTiJPSYXLIQIzZbZg5jNgTi z/NTFdYp8ufpUA7vRvPtnYnlNEOwNi74wqSF4nDmJsnkmTlHPfG0lh7Ns9a50fyhaFbu jF6xnNLBVPbLQ0r8ueDs+/W6Qf8yYBrxx0YUBK9jCHiyW1UMW6LkSeBnFArdtpcE7U48 zV7TEQq2kpj/DPwKjbMBKG+ZpQ5eoK1htw3FCFqTPAQDb7/WqF5LRuTI5ApS7XGuj35B HIHDRlsa0yIk3XMf8gS4Wbz9HltVsTq6zF0zgDtAouu2sX9G5BQYSF5rsVwXC2bKBKju ZsGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728588249; x=1729193049; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pwVGeNhEiYELCNV51bxA2EzTaJbA2K5Fr027Br3jO4Q=; b=v7l3FAPEgvR6hYYpOP3SXMgCOBT8yhPj4zCUjsDKn1oMZXlEw0UHyD7q77xDvrzPdq DkR5mTICSsW8ufaiH1tN+o0y2LZFAbkssK44QATCyj89m8wou1dy95F3dSau/GJja7WX xMR7UBQH6Kxh6rvOAA9dujPkqixKxTQZbK9YC3ruMprjxhSgq75iqWsLlq+diCYGDLEy YZKubSTo+miwCVp+b3um44oZvtFxdYn/qKka7ammvH+Iye1YKSn0Phzm5At9GKG+5zYs OEtXzHMTGv9A2yV9C6Oesy2o0/lf5+SqvCgWGAOh2bGiey4iAmPzgJ8+Z+qhFUTK8kNS 9/DA== X-Forwarded-Encrypted: i=1; AJvYcCUTDymvTG8DBoLumdRmPh9iMLTLj030C50K5W52k9j7Bpg9tQqVybHL2Qn4mNOEPgMmf1ztC6mc40vRqbs=@vger.kernel.org X-Gm-Message-State: AOJu0YyZ2hmw6UXwzrIiIFcZCjcpTr9lw5GcRA6ExJWcooE/A8hoOCzL R0rbcpDss7veDp2Qbr/MxZ3Vbofy4EmlTyVbRs71DfR/rCfduzWDeFlTMBt33Ua+Gg== X-Google-Smtp-Source: AGHT+IET7ynYxoh2WLaFTqujq1dsN8li9OFmo1T7KNB2xR5JtuPPo+YvvfF+NvnbjuFxvNf7cf4DObk= X-Received: from xur.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2330]) (user=xur job=sendgmr) by 2002:a25:b104:0:b0:e29:a86:fd0d with SMTP id 3f1490d57ef6-e290bb40627mr73646276.5.1728588248551; Thu, 10 Oct 2024 12:24:08 -0700 (PDT) Date: Thu, 10 Oct 2024 12:23:54 -0700 In-Reply-To: <20241010192400.451187-1-xur@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241010192400.451187-1-xur@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241010192400.451187-3-xur@google.com> Subject: [PATCH v3 2/6] objtool: Fix unreachable instruction warnings for weak funcitons From: Rong Xu To: Alice Ryhl , Andrew Morton , Arnd Bergmann , Bill Wendling , Borislav Petkov , Breno Leitao , Brian Gerst , Dave Hansen , David Li , Han Shen , Heiko Carstens , "H. Peter Anvin" , Ingo Molnar , Jann Horn , Jonathan Corbet , Josh Poimboeuf , Juergen Gross , Justin Stitt , Kees Cook , Masahiro Yamada , "Mike Rapoport (IBM)" , Nathan Chancellor , Nick Desaulniers , Nicolas Schier , "Paul E. McKenney" , Peter Zijlstra , Rong Xu , Sami Tolvanen , Thomas Gleixner , Wei Yang , workflows@vger.kernel.org, Miguel Ojeda , Maksim Panchenko Cc: x86@kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Sriraman Tallam , Krzysztof Pszeniczny Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the presence of both weak and strong function definitions, the linker drops the weak symbol in favor of a strong symbol, but leaves the code in place. Code in ignore_unreachable_insn() has some heuristics to suppress the warning, but it does not work when -ffunction-sections is enabled. Suppose function foo has both strong and weak definitions. Case 1: The strong definition has an annotated section name, like .init.text. Only the weak definition will be placed into .text.foo. But since the section has no symbols, there will be no "hole" in the section. Case 2: Both sections are without an annotated section name. Both will be placed into .text.foo section, but there will be only one symbol (the strong one). If the weak code is before the strong code, there is no "hole" as it fails to find the right-most symbol before the offset. The fix is to use the first node to compute the hole if hole.sym is empty. If there is no symbol in the section, the first node will be NULL, in which case, -1 is returned to skip the whole section. Co-developed-by: Han Shen Signed-off-by: Han Shen Signed-off-by: Rong Xu Suggested-by: Sriraman Tallam Suggested-by: Krzysztof Pszeniczny --- tools/objtool/elf.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 3d27983dc908..6f64d611faea 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -224,12 +224,17 @@ int find_symbol_hole_containing(const struct section = *sec, unsigned long offset) if (n) return 0; /* not a hole */ =20 - /* didn't find a symbol for which @offset is after it */ - if (!hole.sym) - return 0; /* not a hole */ + /* + * @offset >=3D sym->offset + sym->len, find symbol after it. + * When hole.sym is empty, use the first node to compute the hole. + * If there is no symbol in the section, the first node will be NULL, + * in which case, -1 is returned to skip the whole section. + */ + if (hole.sym) + n =3D rb_next(&hole.sym->node); + else + n =3D rb_first_cached(&sec->symbol_tree); =20 - /* @offset >=3D sym->offset + sym->len, find symbol after it */ - n =3D rb_next(&hole.sym->node); if (!n) return -1; /* until end of address space */ =20 --=20 2.47.0.rc1.288.g06298d1525-goog