From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 082EE25C83E; Thu, 26 Jun 2025 23:56:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750982179; cv=none; b=ejlmx4pxgHLoxdlqXg/BVgG/RIzSHvkRWfUwmaLzLgC13F32uvrrTrUGisSguVx7adoHrSa+3Ttdx3wjQnK8ZRmxBgZDKPDk5ki0FeReV7y8ZFxjREWLQsU+UHSdFS6DavGHEdj+UATP1xQts34fbp4GEGLZBnwyg2zWpzasCLg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750982179; c=relaxed/simple; bh=6Idgt0dRrvx6JcsNx2dFuGmAHsQkiymEL/xdABXxvA0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DziA52TMHFYJc7u303ZEh9BkqHzoGs2DOsRMbPwjnEfCkFOHwj+4wBZmk6JWt1sRhA+q1aqRcwucaMoG4jrQ8LLgQTP2CvRx1tg4AJdSc8YgYbSc9Bo2vua6ezDOaTseujz8hIV4yKGFnjq1Fbgx+fHxBv024pn+xGWXVKkpq5A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=c3KsHb8o; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="c3KsHb8o" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14E27C4CEF5; Thu, 26 Jun 2025 23:56:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750982178; bh=6Idgt0dRrvx6JcsNx2dFuGmAHsQkiymEL/xdABXxvA0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c3KsHb8oKuK750AgCtnHQMYTmSd6Qhqgzj8H17A4uttj8ZVFolDTayB+s6zWU1kJa 9BtY/u+eZw1Jap1fRUaZzQ3V6Kx/oERIwmJ2VOAJzFtQHys1mANKnYN5dsR8WDer5J Q9AGfNlfrUot1sNk9O/MZN2l6hzRh+NcWyXvJGfc+QeEADVreQBbW0tKJxu0bADtqB yEIyhDU5Ma/GoTuJmcTDfzxuREC9GkmC1FoLBRPhAOeQ2XPGhkfeZRNrhPpl6+Q5p8 SmYrwFh+IlM0o53z4HXkHdj7ntF6bEW49tVP+vHp2BAZBMiHIylPPjyu7dXuyfsXu/ DuR6GvNvhiwNQ== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Petr Mladek , Miroslav Benes , Joe Lawrence , live-patching@vger.kernel.org, Song Liu , laokz , Jiri Kosina , Marcos Paulo de Souza , Weinan Liu , Fazla Mehrab , Chen Zhongjin , Puranjay Mohan , Dylan Hatch Subject: [PATCH v3 11/64] objtool: Make find_symbol_containing() less arbitrary Date: Thu, 26 Jun 2025 16:54:58 -0700 Message-ID: <4afefca1066b8600d82f7b2dd9f6e3c2922884bd.1750980517.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In the rare case of overlapping symbols, find_symbol_containing() just returns the first one it finds. Make it slightly less arbitrary by returning the smallest symbol with size > 0. Signed-off-by: Josh Poimboeuf --- tools/objtool/elf.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index ca5d77db692a..1c1bb2cb960d 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -193,14 +193,29 @@ struct symbol *find_func_by_offset(struct section *sec, unsigned long offset) struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset) { struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree; - struct symbol *iter; + struct symbol *sym = NULL, *tmp; - __sym_for_each(iter, tree, offset, offset) { - if (iter->type != STT_SECTION) - return iter; + __sym_for_each(tmp, tree, offset, offset) { + if (tmp->len) { + if (!sym) { + sym = tmp; + continue; + } + + if (sym->offset != tmp->offset || sym->len != tmp->len) { + /* + * In the rare case of overlapping symbols, + * pick the smaller one. + * + * TODO: outlaw overlapping symbols + */ + if (tmp->len < sym->len) + sym = tmp; + } + } } - return NULL; + return sym; } /* -- 2.49.0