Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2319854rbb; Tue, 27 Feb 2024 19:36:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXuEufWPxzmhFYzYX4tU8mPFEPtEWA3yJimFOZHZBNHb/XBQySTj/2uupaOOHClsYx+tSriUcetvFE0OdYSDWSgnU4LW7d01xQ9F3RAjg== X-Google-Smtp-Source: AGHT+IGPHMNVdbW0xerHsOWozrkIt68Yf8o3AjLdIfQqyIF8C1bhMs0CUCGrLqTBhiPjtM8jvMRG X-Received: by 2002:a1f:ca47:0:b0:4c9:b8a8:78d4 with SMTP id a68-20020a1fca47000000b004c9b8a878d4mr9280933vkg.3.1709091396159; Tue, 27 Feb 2024 19:36:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709091396; cv=pass; d=google.com; s=arc-20160816; b=yYplYynZOkICDpLpOgKn2/MpJ5sICVPxFFyputR/EWp8GvBc3HZ945DVmtmnvlgA4X rnEDs7Zs/hTNJWpfoswLLu9YuIx2sbGt2fabRPNYGL1j2rhNBxAyEAS4qnFfXJUmMS8I KCTDnzmbKSiWS1N20LfNvZxsFtDcb+KTR8cwKuE7MyKomoH8Q6Czq0nIqxi1ow7wujZz 3iKDfr10S+ML12I1XqN5ukW/IUzud8r726nQAeyJElPbSkQRTt42Wb0W2DevTGW4Uecr SjGX+ay4SXmIaZ529nZ4SXm0p+g4JNyU2VG1yzAcuc4GBklyQBcAPtFhNLsyUq1Bwinz fECQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=4Xvb2DA8xzihP7Mzi/v0EMpx+00b5U5crlo4mSn4zeU=; fh=++EP4UzpCCTuz0pme5H+y/p01vuzSp1bpUVVKjwiKc4=; b=L/g38s8ebbc4lCwmFyQLjK9RgjjMI3zpyKyOTQmk3Ce1CXg3EgmaLE9SNsiepr/ytj AIcBv066fARPd6oHET0e8I9tFYlfe4RALtBuilyPNFRMtJmMEDVXrgj3qBLYH7IP7H6b CjP9kkMhCWLtzsE0N0N5GJX9h4USH/fZ98FSCxcb3RC/c9UgFNyL8Th58Q5N1uiIRXIu GHgFnRj6BVzDdT5YsD1eXxEd6VvoZWhBHWaO/jcHPyoqnl3He+bKfUocRUFJTeUKh0dF USmVmW8xf1kjlnNZMHE0El0yMUlXTBRqf3e0ESF4t9ka978xwFRMTyVSE710cMqa9dzG ve+A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=Zab5itUG; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-84445-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84445-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id z2-20020a170902708200b001dbe5c1f2eesi2421415plk.223.2024.02.27.19.36.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 19:36:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84445-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=Zab5itUG; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-84445-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84445-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id D1757B27455 for ; Wed, 28 Feb 2024 02:47:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F32DF1DFE8; Wed, 28 Feb 2024 02:46:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="Zab5itUG" Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (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 35589200C3 for ; Wed, 28 Feb 2024 02:46:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709088374; cv=none; b=vBa6uxEWbWpIEz2dTBoA+XljzM5S8isytL5q7rTHF1ib4pA6gqN709O5L1bECGWpSRTSlSxLkjezXtuFEAKwm4h7z24/Z+XLun6N9fF26cIQkY+1YkiBNstBlybxImWAXOyn8hPmFjmtemyezF2WcJOLFjwCdUWToCFybePOTNc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709088374; c=relaxed/simple; bh=abhzLrMcAo73itQJFRf9PngJ2NfPzbHIHmMJXMFZsec=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X+sOvR/Oj+pt0B4FnURDCeFWSQN915GwRFIgW6Aii2sZeXUasyF9sl2oUV+H4lceKCRkRX2C0N4+PEnsRNDk2BLj/xVUtfRo2MvRygobCgyaIofDcrncTBAJDQvIsCNN7CIpIqKuJt2WiJhHt4uFusz6+lJqIyuPF9CiqsH64j8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=Zab5itUG; arc=none smtp.client-ip=209.85.210.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-6e49812ba77so1652232a34.2 for ; Tue, 27 Feb 2024 18:46:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1709088372; x=1709693172; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4Xvb2DA8xzihP7Mzi/v0EMpx+00b5U5crlo4mSn4zeU=; b=Zab5itUGCPvAlQrjJy0TWTfijq60dXxVpV1rCh4oGw61rTSZcwrh1z1mPTzUufbYPm 5UDndPTkQQAfhPl6inPDKm/vRPHrnvtLACoHsImxuSodljQI6D+eP3k8hVDL0L+7XBUw 9RT3QZUkYWq7u+toLo46SAwvw6xCbAMM8Z9uVnKIVkcyYQMkUQQp2KIbkkDIQ/zML0Y6 EaZQEWCj5jCTMmFxu5p7SiBtuEhdZBzj8IMCeGukyHI3moHuLui5Mmc/REB5u4KRWIaD gMeiZFK63t6qKTXA2TBVazoLlzR+xGt3hdFGODjzNzW0IxghNspagDAZEIec50Sra+Vf mRBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709088372; x=1709693172; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4Xvb2DA8xzihP7Mzi/v0EMpx+00b5U5crlo4mSn4zeU=; b=oafZSLaSoDtUSDeMhi5js79BCgHSKg2lTp5mo7OPE6o7rrStT5Gi0mwV+Wn6ycKcIk 3qG+8BNXoyzq3VQF4eSZkbEkm9yCh6VrTQpmq0ccP/Z9NLUPuaA6Yz5oGQC4wBGwvnfI UGnBMG90fct+MzfyDKPWSbmlv5F5gTFigYyxjnE4n83WF3jA1X2lCHGxxX5LVbZrL+Uj txqb6IssBbgRPdnEtdzxb6ZemMrbfAF3nZO8HisODuRcU1Z45uUHAxPeenbCJkanjb7N eypMLLosVDTca8XBq6RGqn1kxQiPiyfBiCzykKK1XNtu/HqCsCFkSIpCMW3Q/g2cLZbs Sk7w== X-Forwarded-Encrypted: i=1; AJvYcCW7N2WAHwxAbzOMQCE1ut4OzoiSu/QC8MIisJuAMCj0j8NlYu7V980p0B/QtZhna1NGNGGpFG7MN+vpL18VjtFpEt/GyeKKsCbgmga0 X-Gm-Message-State: AOJu0YxLt0rcXBMSi9Lg4gd4wU1vF+c2ioJ73x6V8eFKGPIGTD5l1rrv Usr7SRNG2nk3KzWEYrD+28sBZmOZxjO8Awc7EzXpoZFFxgNCb9iuKoK/X1NOFqY= X-Received: by 2002:a05:6830:120c:b0:6e4:7be4:393a with SMTP id r12-20020a056830120c00b006e47be4393amr14448289otp.5.1709088372243; Tue, 27 Feb 2024 18:46:12 -0800 (PST) Received: from C02CV19DML87.bytedance.net ([240e:6b1:c0:120::1:d]) by smtp.gmail.com with ESMTPSA id 9-20020a631249000000b005dcbb699abfsm6489072pgs.34.2024.02.27.18.46.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 18:46:11 -0800 (PST) From: Rui Qi To: bp@alien8.de, mingo@redhat.com, tglx@linutronix.de, hpa@zytor.com, jpoimboe@redhat.com, peterz@infradead.org, mbenes@suse.cz, gregkh@linuxfoundation.org, stable@vger.kernel.org, alexandre.chartre@oracle.com Cc: x86@kernel.org, linux-kernel@vger.kernel.org, yuanzhu@bytedance.com, Rui Qi Subject: [PATCH v2 2/3] objtool: Add support for intra-function calls Date: Wed, 28 Feb 2024 10:45:34 +0800 Message-Id: <20240228024535.79980-3-qirui.001@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20240228024535.79980-1-qirui.001@bytedance.com> References: <20240228024535.79980-1-qirui.001@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Alexandre Chartre commit 8aa8eb2a8f5b3305a95f39957dd2b715fa668e21 upstream. Change objtool to support intra-function calls. On x86, an intra-function call is represented in objtool as a push onto the stack (of the return address), and a jump to the destination address. That way the stack information is correctly updated and the call flow is still accurate. Signed-off-by: Alexandre Chartre Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Miroslav Benes Acked-by: Josh Poimboeuf Link: https://lkml.kernel.org/r/20200414103618.12657-4-alexandre.chartre@oracle.com Signed-off-by: Rui Qi --- include/linux/frame.h | 11 ++++ .../Documentation/stack-validation.txt | 8 +++ tools/objtool/arch/x86/decode.c | 6 ++ tools/objtool/check.c | 62 +++++++++++++++++-- 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/include/linux/frame.h b/include/linux/frame.h index 02d3ca2d9598..303cda600e56 100644 --- a/include/linux/frame.h +++ b/include/linux/frame.h @@ -15,9 +15,20 @@ static void __used __section(.discard.func_stack_frame_non_standard) \ *__func_stack_frame_non_standard_##func = func +/* + * This macro indicates that the following intra-function call is valid. + * Any non-annotated intra-function call will cause objtool to issue a warning. + */ +#define ANNOTATE_INTRA_FUNCTION_CALL \ + 999: \ + .pushsection .discard.intra_function_calls; \ + .long 999b; \ + .popsection; + #else /* !CONFIG_STACK_VALIDATION */ #define STACK_FRAME_NON_STANDARD(func) +#define ANNOTATE_INTRA_FUNCTION_CALL #endif /* CONFIG_STACK_VALIDATION */ diff --git a/tools/objtool/Documentation/stack-validation.txt b/tools/objtool/Documentation/stack-validation.txt index de094670050b..ee26bb382b70 100644 --- a/tools/objtool/Documentation/stack-validation.txt +++ b/tools/objtool/Documentation/stack-validation.txt @@ -290,6 +290,14 @@ they mean, and suggestions for how to fix them. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70646 +11. file.o: warning: unannotated intra-function call + + This warning means that a direct call is done to a destination which + is not at the beginning of a function. If this is a legit call, you + can remove this warning by putting the ANNOTATE_INTRA_FUNCTION_CALL + directive right before the call. + + If the error doesn't seem to make sense, it could be a bug in objtool. Feel free to ask the objtool maintainer for help. diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c index a62e032863a8..c3ff62c085c8 100644 --- a/tools/objtool/arch/x86/decode.c +++ b/tools/objtool/arch/x86/decode.c @@ -437,6 +437,12 @@ int arch_decode_instruction(struct elf *elf, struct section *sec, case 0xe8: *type = INSN_CALL; + /* + * For the impact on the stack, a CALL behaves like + * a PUSH of an immediate value (the return address). + */ + op->src.type = OP_SRC_CONST; + op->dest.type = OP_DEST_PUSH; break; case 0xfc: diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 71a24fd46dbd..0fa414869f45 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -645,6 +645,7 @@ static int add_jump_destinations(struct objtool_file *file) return 0; } + /* * Find the destination instructions for all calls. */ @@ -666,10 +667,7 @@ static int add_call_destinations(struct objtool_file *file) dest_off); if (!insn->call_dest && !insn->ignore) { - WARN_FUNC("unsupported intra-function call", - insn->sec, insn->offset); - if (retpoline) - WARN("If this is a retpoline, please patch it in with alternatives and annotate it with ANNOTATE_NOSPEC_ALTERNATIVE."); + WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset); return -1; } @@ -1291,6 +1289,58 @@ static int read_retpoline_hints(struct objtool_file *file) return 0; } + +static int read_intra_function_calls(struct objtool_file *file) +{ + struct instruction *insn; + struct section *sec; + struct rela *rela; + + sec = find_section_by_name(file->elf, ".rela.discard.intra_function_calls"); + if (!sec) + return 0; + + list_for_each_entry(rela, &sec->rela_list, list) { + unsigned long dest_off; + + if (rela->sym->type != STT_SECTION) { + WARN("unexpected relocation symbol type in %s", + sec->name); + return -1; + } + + insn = find_insn(file, rela->sym->sec, rela->addend); + if (!insn) { + WARN("bad .discard.intra_function_call entry"); + return -1; + } + + if (insn->type != INSN_CALL) { + WARN_FUNC("intra_function_call not a direct call", + insn->sec, insn->offset); + return -1; + } + + /* + * Treat intra-function CALLs as JMPs, but with a stack_op. + * See add_call_destinations(), which strips stack_ops from + * normal CALLs. + */ + insn->type = INSN_JUMP_UNCONDITIONAL; + + dest_off = insn->offset + insn->len + insn->immediate; + insn->jump_dest = find_insn(file, insn->sec, dest_off); + if (!insn->jump_dest) { + WARN_FUNC("can't find call dest at %s+0x%lx", + insn->sec, insn->offset, + insn->sec->name, dest_off); + return -1; + } + } + + return 0; +} + static void mark_rodata(struct objtool_file *file) { struct section *sec; @@ -1346,6 +1396,10 @@ static int decode_sections(struct objtool_file *file) if (ret) return ret; + ret = read_intra_function_calls(file); + if (ret) + return ret; + ret = add_call_destinations(file); if (ret) return ret; -- 2.39.2 (Apple Git-143)