From nobody Mon Feb 9 14:32:36 2026 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="Zab5itUG" 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-Google-Smtp-Source: AGHT+IE/uRJXmSDsmVcdvvQtha1dgj/hFQ8X8ftxAxzYl4tzNY8SQCryfk2c7wi8S6k4aalqlMt/mQ== 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: quoted-printable Content-Type: text/plain; charset="utf-8" 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@or= acle.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 =3D func =20 +/* + * This macro indicates that the following intra-function call is valid. + * Any non-annotated intra-function call will cause objtool to issue a war= ning. + */ +#define ANNOTATE_INTRA_FUNCTION_CALL \ + 999: \ + .pushsection .discard.intra_function_calls; \ + .long 999b; \ + .popsection; + #else /* !CONFIG_STACK_VALIDATION */ =20 #define STACK_FRAME_NON_STANDARD(func) +#define ANNOTATE_INTRA_FUNCTION_CALL =20 #endif /* CONFIG_STACK_VALIDATION */ =20 diff --git a/tools/objtool/Documentation/stack-validation.txt b/tools/objto= ol/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=3D70646 =20 =20 +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. =20 diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decod= e.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 se= ction *sec, =20 case 0xe8: *type =3D INSN_CALL; + /* + * For the impact on the stack, a CALL behaves like + * a PUSH of an immediate value (the return address). + */ + op->src.type =3D OP_SRC_CONST; + op->dest.type =3D OP_DEST_PUSH; break; =20 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 *f= ile) return 0; } =20 + /* * Find the destination instructions for all calls. */ @@ -666,10 +667,7 @@ static int add_call_destinations(struct objtool_file *= file) dest_off); =20 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 an= d annotate it with ANNOTATE_NOSPEC_ALTERNATIVE."); + WARN_FUNC("unannotated intra-function call", insn->sec, insn->offset); return -1; } =20 @@ -1291,6 +1289,58 @@ static int read_retpoline_hints(struct objtool_file = *file) return 0; } =20 + +static int read_intra_function_calls(struct objtool_file *file) +{ + struct instruction *insn; + struct section *sec; + struct rela *rela; + + sec =3D find_section_by_name(file->elf, ".rela.discard.intra_function_cal= ls"); + if (!sec) + return 0; + + list_for_each_entry(rela, &sec->rela_list, list) { + unsigned long dest_off; + + if (rela->sym->type !=3D STT_SECTION) { + WARN("unexpected relocation symbol type in %s", + sec->name); + return -1; + } + + insn =3D find_insn(file, rela->sym->sec, rela->addend); + if (!insn) { + WARN("bad .discard.intra_function_call entry"); + return -1; + } + + if (insn->type !=3D 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 =3D INSN_JUMP_UNCONDITIONAL; + + dest_off =3D insn->offset + insn->len + insn->immediate; + insn->jump_dest =3D 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; =20 + ret =3D read_intra_function_calls(file); + if (ret) + return ret; + ret =3D add_call_destinations(file); if (ret) return ret; --=20 2.39.2 (Apple Git-143)