From mboxrd@z Thu Jan 1 00:00:00 1970 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 0DF581791ED for ; Sat, 26 Oct 2024 05:14:18 +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=1729919662; cv=none; b=KlOV7mpXajMYSP0qcljb5plLSUY9WE4ezUrsSKZWFPyT2mNdmpL5nMR+jdH1pVkyO/BMdf8Pslm57otuY4tLw6gVXZmxUfpJIJEYZqdgmf0ro0KtmYh4cTUkSE/2DZJppNHzn5jyGHCCXwzyNWVXVvgElrXNXxTUxNxcfuWJ8bM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729919662; c=relaxed/simple; bh=Q9RzV1qCGVrMb6LAeR7cFFh2RG554NMIU/BK470X3jM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Q+EB9dnmcya+Z+KFovtTxTHZX3m18cQNzE3MuK4XWd74CuO5c6PieWII6rgaQxEFaWnHtGtvB/tE696fj6nhw3tozJwa5iXHxWiHFZs3bxJQ+/pEJIkIXTNlNOBQpkLqbQ0caIP7EhHMhwOCPnPu1ytbnE1eYlR80TQiyMTajRc= 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=w2wUP7R0; 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="w2wUP7R0" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e31d9c8efcso50380947b3.0 for ; Fri, 25 Oct 2024 22:14:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1729919658; x=1730524458; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7kYFsOVpPnUjYIKUaua2wnEWHay18U0XiUGnm1LQcBI=; b=w2wUP7R0EY4KTB7Q55gvz6/rbfznNMFQPZIS0NnaVJPR6ZPxOl643GQO1Fq+h2ZLEc UsRPnD1uNBYpAXpPI+qH7/6Z2jKYC1j3limRhb6Td8GgtAFz+cCjPoHS+P1W+/jV0nhM kdZiQi3SY9Et9+aFNgZt9YTBTxb+bl/DKxzy5TjbgHbKhLp88WMFc1FC/tTfcqHqd3qE 0inxUH039qotMJx8HQz0Nt4zA4dsuD8fyYFP3DWa+cLgfBFK0M6bbUkCliNUPd/Byx6Q /BbX2ofGIs/cY41+jrggeezrKtCz6m58+is3c8g8hpIUN4VcrNmrsQpjjvHF7kM3Mq1k 89pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729919658; x=1730524458; 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=7kYFsOVpPnUjYIKUaua2wnEWHay18U0XiUGnm1LQcBI=; b=IfPgkOZKolgw2f+rauDW3LMH0MbTQLcRwWOYnPZJCJqccAlAvuoiaQZMPQv/+C0BIv IFgy98R/QJoDAeGVk03y1hmHWP+aSWRtR2SjO/ZLGZoRRKRv4yTcKfwJMHzV+zVLgu1k VjThviTe2Pk8RwdOlaf9LY804zsFA1gzN9qyueYfn0wH0PG9WPyYDP9WaNBBEb/Dbh74 KXZds/aIdFGgAfWF7B/5H+MZpMPboRThcsuY5D3IMW2FcjjOpdPHBoS5KFXiMI++Gn4Z PSSqVJsYEc6pdlJyqsmUVPI+5tyNpKcVFdpZJohfvDXBW5XFdJ9jPXROx1lyr0HfkNFE LPHQ== X-Forwarded-Encrypted: i=1; AJvYcCUhxW+OugSNfdgtVZx3xj63ccMcOdUickGNuMChXupScFqBKtqbrCxkQBiUM0LpbpewlAKb@lists.linux.dev X-Gm-Message-State: AOJu0Yzx+7BP0yJ4Hzsoraj2+uUpwzMBEBaGZta9i2yVtaetd9mxXrYV jU//XH/LT9s4tbjIig6DCWC2OYg2ZQEmqoKOv9oKrpfuvqlsECqR/Ql3hJIfg3AHZg== X-Google-Smtp-Source: AGHT+IEk8vUuSuqRki7LGdU/vmd4z0u2mMux+ctpByzh8QxC1iVodNKbzUGif3YLI3VZDHQrTILJeT8= X-Received: from xur.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2330]) (user=xur job=sendgmr) by 2002:a05:690c:6f89:b0:6e7:e3e4:9d83 with SMTP id 00721157ae682-6e9d8b5d877mr449357b3.8.1729919657990; Fri, 25 Oct 2024 22:14:17 -0700 (PDT) Date: Fri, 25 Oct 2024 22:14:04 -0700 In-Reply-To: <20241026051410.2819338-1-xur@google.com> Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241026051410.2819338-1-xur@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241026051410.2819338-3-xur@google.com> Subject: [PATCH v6 2/7] objtool: Fix unreachable instruction warnings for weak functions 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 , "David S. Miller" , Andreas Larsson , Yonghong Song , Yabin Cui , Krzysztof Pszeniczny , Sriraman Tallam , Stephane Eranian Cc: x86@kernel.org, linux-arch@vger.kernel.org, sparclinux@vger.kernel.org, linux-doc@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev 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 Tested-by: Yonghong Song Tested-by: Yabin Cui Change-Id: Ib3a484b6f056db8ba4f8e91e567e3165bbeb51ea --- 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 3d27983dc908d..6f64d611faea9 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 */ - /* didn't find a symbol for which @offset is after it */ - if (!hole.sym) - return 0; /* not a hole */ + /* + * @offset >= 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 = rb_next(&hole.sym->node); + else + n = rb_first_cached(&sec->symbol_tree); - /* @offset >= sym->offset + sym->len, find symbol after it */ - n = rb_next(&hole.sym->node); if (!n) return -1; /* until end of address space */ -- 2.47.0.163.g1226f6d8fa-goog