From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 F17A119F43B for ; Wed, 2 Oct 2024 23:34:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727912064; cv=none; b=SDeRZr1tKS3LZco3gxwP63eaWGk1gt9O31fSYPkiFoc/LS1ihqqNzE/cVus0gKnGmhOfd60qWSlm3Qoi9iQBPGtrSHquDbLhiQH5ejMq7u2Zpu0yIRK86qE327Fzy5rUKdzTgIsc2teCZbqlCXSUOtvUxhBeNGVvW1A23z3ZbNo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727912064; c=relaxed/simple; bh=Lie6O9sJl63FI1OpJaBUB78suhFUkbeqvo9RmUOmzmM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=GAcB822DSHwcYguJ6yrc6sxUVB3FZXMmYHBzFl/1fqNbYwqTLvefHUV5IsLYlvCigemuhjMMkJZz6zdgxZE4451j6Zf3vGFDjlkGPqwATMVhG8Nl82W2oALvyolznykI/AJgg0BpZZEKrvvOueLGl4TcbBYjI9NssyPmJmQSDko= 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=jRik37aP; arc=none smtp.client-ip=209.85.219.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="jRik37aP" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e260d4dfc8aso520541276.1 for ; Wed, 02 Oct 2024 16:34:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727912061; x=1728516861; darn=lists.linux.dev; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=14e13Hoyr5VMrj1jISTD53jFiFeUhPa+WN666QY0L8k=; b=jRik37aPzhrSGwhOL6FBVeHyQH1cv5K4a1ivZmZOY5MG4RXHN1ngTYRbDGZunYmjGz cirPlE0cQ+Mzc5xfhoW3g3an95ISBmrlJTxSBK5tnfns21YUfKLroCqkgpzeCKqi0rWf kIKkyDSEz2GfAcNREn8ESW3a79xdONo2FfsE7oVz4OHtnAttiBgwq8jQFYIgvGWEJdO6 o2UfdUGBv6utpH7K7+GFPBHQFtqqtKW0nDK590z8X2iYtdzqQYqx4zHb+A1dRyN+gitD /j/1xTg1Kojr1b0QYjIKCvAv732Ltumpgc3RiAy7uM/W/U/Vfm1qGh8Hj+KIDiCpofPm ygYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727912061; x=1728516861; h=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=14e13Hoyr5VMrj1jISTD53jFiFeUhPa+WN666QY0L8k=; b=a+VFLgrV4dGlLRu/7vTPLgb8ox9JTb7l93Z6qlDC77DF2S/8Hu862Fy0WNmiPvncU8 AmviIX5GnC/Eg38g6ZdO7NW/81+zLQWJAHnL1J9SijnYOE4EelVn+PCXRBbBtyst38MC nEqiP1rOQUG3wzcy349JGzGWFcKctCr78SxpiNem9zVKXEDdfNK1CitnFTDWDEwg4hZX D92/KZtOoBRAte3x+PUV/xWeEGRL8YW3gARaHlImveV3g3LGrkJmQIaDtLdTrlzQedUd XyTiGSdDn/VTVA2w7IfEd8VItpLkhO+hVr/phlcE64grYb2fsJkGqZT0ctJk+yH1zBC4 s64Q== X-Forwarded-Encrypted: i=1; AJvYcCXBwyzDAiu7KB9+6jPMyR0GkVRZa9v1vrMIqYEAZP8hKYkER4raVtXUbsfPa06VkN2E4tFP@lists.linux.dev X-Gm-Message-State: AOJu0Yxv9TsJzF+2XF2JR/dBFD509O66XlH+pnk1zUfzxlsdCL3sXMhv YxaX8rAGWlD5TygGo1yFG1CJ2KsgH/jDn6NYAvOmD2McdS0MsalVsmeHHoZjc1RVCQ== X-Google-Smtp-Source: AGHT+IFpsbObUJH+vI8s73i/WdqY27wJNDCJFPLhXWJvzrW9aW/Q0PSjEgLWBEVAaWsrJztzlATyLsU= X-Received: from xur.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2330]) (user=xur job=sendgmr) by 2002:a25:b314:0:b0:e25:fcb:3205 with SMTP id 3f1490d57ef6-e263842f473mr13083276.8.1727912060817; Wed, 02 Oct 2024 16:34:20 -0700 (PDT) Date: Wed, 2 Oct 2024 16:34:01 -0700 In-Reply-To: <20241002233409.2857999-1-xur@google.com> Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241002233409.2857999-1-xur@google.com> X-Mailer: git-send-email 2.46.1.824.gd892dcdcdd-goog Message-ID: <20241002233409.2857999-3-xur@google.com> Subject: [PATCH v2 2/6] objtool: Fix unreachable instruction warnings for weak funcitons From: Rong Xu To: Rong Xu , Han Shen , Sriraman Tallam , David Li , Krzysztof Pszeniczny , Alice Ryhl , Andrew Morton , Arnd Bergmann , Bill Wendling , Borislav Petkov , Breno Leitao , Brian Gerst , Dave Hansen , Heiko Carstens , "H. Peter Anvin" , Ingo Molnar , Jann Horn , Jonathan Corbet , Josh Poimboeuf , Juergen Gross , Justin Stitt , Kees Cook , linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Masahiro Yamada , "Mike Rapoport (IBM)" , Nathan Chancellor , Nick Desaulniers , Nicolas Schier , "Paul E. McKenney" , Peter Zijlstra , Samuel Holland , Thomas Gleixner , Wei Yang , workflows@vger.kernel.org, x86@kernel.org, "Xin Li (Intel)" 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 */ - /* 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.46.1.824.gd892dcdcdd-goog