Skip to content

Commit 4b6ab77

Browse files
committed
Support alternative sections for patchable function entries
With -fpatchable-function-entry (or the patchable_function_entry function attribute), we emit records of patchable entry locations to the __patchable_function_entries section. Add an additional parameter to the command line option that allows one to specify a different default section name for the records, and an identical parameter to the function attribute that allows one to override the section used. The main use case for this change is the Linux kernel using prefix NOPs for ftrace, and thus depending on__patchable_function_entries to locate traceable functions. Functions that are not traceable currently disable entry NOPs using the function attribute, but this creates a compatibility issue with -fsanitize=kcfi, which expects all indirectly callable functions to have a type hash prefix at the same offset from the function entry. Adding a section parameter would allow the kernel to distinguish between traceable and non-traceable functions by adding entry records to separate sections while maintaining a stable function prefix layout for all functions. LKML discussion: https://lore.kernel.org/lkml/Y1QEzk%[email protected]/
1 parent 57d87ed commit 4b6ab77

File tree

15 files changed

+146
-18
lines changed

15 files changed

+146
-18
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,8 @@ def PatchableFunctionEntry
932932
"riscv64", "x86", "x86_64", "ppc", "ppc64"]>> {
933933
let Spellings = [GCC<"patchable_function_entry">];
934934
let Subjects = SubjectList<[Function, ObjCMethod]>;
935-
let Args = [UnsignedArgument<"Count">, DefaultIntArgument<"Offset", 0>];
935+
let Args = [UnsignedArgument<"Count">, DefaultIntArgument<"Offset", 0>,
936+
StringArgument<"Section", /* optional */ 1>];
936937
let Documentation = [PatchableFunctionEntryDocs];
937938
}
938939

clang/include/clang/Basic/AttrDocs.td

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6502,10 +6502,12 @@ only N==1 is supported.
65026502
def PatchableFunctionEntryDocs : Documentation {
65036503
let Category = DocCatFunction;
65046504
let Content = [{
6505-
``__attribute__((patchable_function_entry(N,M)))`` is used to generate M NOPs
6506-
before the function entry and N-M NOPs after the function entry. This attribute
6507-
takes precedence over the command line option ``-fpatchable-function-entry=N,M``.
6508-
``M`` defaults to 0 if omitted.
6505+
``__attribute__((patchable_function_entry(N,M,Section)))`` is used to generate M
6506+
NOPs before the function entry and N-M NOPs after the function entry, with a record of
6507+
the entry stored in section ``Section``. This attribute takes precedence over the
6508+
command line option ``-fpatchable-function-entry=N,M,Section``. ``M`` defaults to 0
6509+
if omitted.``Section`` defaults to the ``-fpatchable-function-entry`` section name if
6510+
set, or to ``__patchable_function_entries`` otherwise.
65096511

65106512
This attribute is only supported on
65116513
aarch64/aarch64-be/loongarch32/loongarch64/riscv32/riscv64/i386/x86-64/ppc/ppc64 targets.

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ class CodeGenOptions : public CodeGenOptionsBase {
281281
/// -fprofile-generate, and -fcs-profile-generate.
282282
std::string InstrProfileOutput;
283283

284+
/// Name of the patchable function entry section with
285+
/// -fpatchable-function-entry.
286+
std::string PatchableFunctionEntrySection;
287+
284288
/// Name of the profile file to use with -fprofile-sample-use.
285289
std::string SampleProfileFile;
286290

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,6 +3598,10 @@ def err_conflicting_codeseg_attribute : Error<
35983598
def warn_duplicate_codeseg_attribute : Warning<
35993599
"duplicate code segment specifiers">, InGroup<Section>;
36003600

3601+
def err_attribute_patchable_function_entry_invalid_section
3602+
: Error<"section argument to 'patchable_function_entry' attribute is not "
3603+
"valid for this target: %0">;
3604+
36013605
def err_anonymous_property: Error<
36023606
"anonymous property is not supported">;
36033607
def err_property_is_variably_modified : Error<

clang/include/clang/Driver/Options.td

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3749,10 +3749,16 @@ defm pascal_strings : BoolFOption<"pascal-strings",
37493749
// Note: This flag has different semantics in the driver and in -cc1. The driver accepts -fpatchable-function-entry=M,N
37503750
// and forwards it to -cc1 as -fpatchable-function-entry=M and -fpatchable-function-entry-offset=N. In -cc1, both flags
37513751
// are treated as a single integer.
3752-
def fpatchable_function_entry_EQ : Joined<["-"], "fpatchable-function-entry=">, Group<f_Group>,
3753-
Visibility<[ClangOption, CC1Option]>,
3754-
MetaVarName<"<N,M>">, HelpText<"Generate M NOPs before function entry and N-M NOPs after function entry">,
3755-
MarshallingInfoInt<CodeGenOpts<"PatchableFunctionEntryCount">>;
3752+
def fpatchable_function_entry_EQ
3753+
: Joined<["-"], "fpatchable-function-entry=">,
3754+
Group<f_Group>,
3755+
Visibility<[ClangOption, CC1Option]>,
3756+
MetaVarName<"<N,M,Section>">,
3757+
HelpText<"Generate M NOPs before function entry and N-M NOPs after "
3758+
"function entry. "
3759+
"If section is specified, use it instead of "
3760+
"__patchable_function_entries.">,
3761+
MarshallingInfoInt<CodeGenOpts<"PatchableFunctionEntryCount">>;
37563762
def fms_hotpatch : Flag<["-"], "fms-hotpatch">, Group<f_Group>,
37573763
Visibility<[ClangOption, CC1Option, CLOption]>,
37583764
HelpText<"Ensure that all functions can be hotpatched at runtime">,
@@ -7577,6 +7583,11 @@ def fpatchable_function_entry_offset_EQ
75777583
: Joined<["-"], "fpatchable-function-entry-offset=">, MetaVarName<"<M>">,
75787584
HelpText<"Generate M NOPs before function entry">,
75797585
MarshallingInfoInt<CodeGenOpts<"PatchableFunctionEntryOffset">>;
7586+
def fpatchable_function_entry_section_EQ
7587+
: Joined<["-"], "fpatchable-function-entry-section=">,
7588+
MetaVarName<"<Section>">,
7589+
HelpText<"Use Section instead of __patchable_function_entries">,
7590+
MarshallingInfoString<CodeGenOpts<"PatchableFunctionEntrySection">>;
75807591
def fprofile_instrument_EQ : Joined<["-"], "fprofile-instrument=">,
75817592
HelpText<"Enable PGO instrumentation">, Values<"none,clang,llvm,csllvm">,
75827593
NormalizedValuesScope<"CodeGenOptions">,

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,18 +959,24 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
959959
}
960960

961961
unsigned Count, Offset;
962+
StringRef Section;
962963
if (const auto *Attr =
963964
D ? D->getAttr<PatchableFunctionEntryAttr>() : nullptr) {
964965
Count = Attr->getCount();
965966
Offset = Attr->getOffset();
967+
Section = Attr->getSection();
966968
} else {
967969
Count = CGM.getCodeGenOpts().PatchableFunctionEntryCount;
968970
Offset = CGM.getCodeGenOpts().PatchableFunctionEntryOffset;
969971
}
972+
if (Section.empty())
973+
Section = CGM.getCodeGenOpts().PatchableFunctionEntrySection;
970974
if (Count && Offset <= Count) {
971975
Fn->addFnAttr("patchable-function-entry", std::to_string(Count - Offset));
972976
if (Offset)
973977
Fn->addFnAttr("patchable-function-prefix", std::to_string(Offset));
978+
if (!Section.empty())
979+
Fn->addFnAttr("patchable-function-entry-section", Section);
974980
}
975981
// Instruct that functions for COFF/CodeView targets should start with a
976982
// patchable instruction, but only on x86/x64. Don't forward this to ARM/ARM64

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6909,8 +6909,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
69096909
D.Diag(diag::err_drv_unsupported_opt_for_target)
69106910
<< A->getAsString(Args) << TripleStr;
69116911
else if (S.consumeInteger(10, Size) ||
6912-
(!S.empty() && (!S.consume_front(",") ||
6913-
S.consumeInteger(10, Offset) || !S.empty())))
6912+
(!S.empty() &&
6913+
(!S.consume_front(",") || S.consumeInteger(10, Offset))) ||
6914+
(!S.empty() && (!S.consume_front(",") || S.empty())))
69146915
D.Diag(diag::err_drv_invalid_argument_to_option)
69156916
<< S0 << A->getOption().getName();
69166917
else if (Size < Offset)
@@ -6919,6 +6920,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
69196920
CmdArgs.push_back(Args.MakeArgString(A->getSpelling() + Twine(Size)));
69206921
CmdArgs.push_back(Args.MakeArgString(
69216922
"-fpatchable-function-entry-offset=" + Twine(Offset)));
6923+
if (!S.empty())
6924+
CmdArgs.push_back(
6925+
Args.MakeArgString("-fpatchable-function-entry-section=" + S));
69226926
}
69236927
}
69246928

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5759,9 +5759,10 @@ static void handlePatchableFunctionEntryAttr(Sema &S, Decl *D,
57595759
return;
57605760
}
57615761
uint32_t Count = 0, Offset = 0;
5762+
StringRef Section;
57625763
if (!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), Count, 0, true))
57635764
return;
5764-
if (AL.getNumArgs() == 2) {
5765+
if (AL.getNumArgs() >= 2) {
57655766
Expr *Arg = AL.getArgAsExpr(1);
57665767
if (!S.checkUInt32Argument(AL, Arg, Offset, 1, true))
57675768
return;
@@ -5771,8 +5772,25 @@ static void handlePatchableFunctionEntryAttr(Sema &S, Decl *D,
57715772
return;
57725773
}
57735774
}
5774-
D->addAttr(::new (S.Context)
5775-
PatchableFunctionEntryAttr(S.Context, AL, Count, Offset));
5775+
if (AL.getNumArgs() == 3) {
5776+
SourceLocation LiteralLoc;
5777+
if (!S.checkStringLiteralArgumentAttr(AL, 2, Section, &LiteralLoc))
5778+
return;
5779+
if (llvm::Error E = S.isValidSectionSpecifier(Section)) {
5780+
S.Diag(LiteralLoc,
5781+
diag::err_attribute_patchable_function_entry_invalid_section)
5782+
<< toString(std::move(E));
5783+
return;
5784+
}
5785+
if (Section.empty()) {
5786+
S.Diag(LiteralLoc,
5787+
diag::err_attribute_patchable_function_entry_invalid_section)
5788+
<< "section must not be empty";
5789+
return;
5790+
}
5791+
}
5792+
D->addAttr(::new (S.Context) PatchableFunctionEntryAttr(S.Context, AL, Count,
5793+
Offset, Section));
57765794
}
57775795

57785796
static void handleBuiltinAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: %clang_cc1 -triple aarch64 -emit-llvm %s -o - | FileCheck --check-prefixes=COMMON,NODEFAULT %s
2+
// RUN: %clang_cc1 -triple x86_64 -emit-llvm %s -fpatchable-function-entry=1 -fpatchable-function-entry-section=__default_section -o - | FileCheck --check-prefixes=COMMON,DEFAULT %s
3+
4+
// COMMON: define{{.*}} void @f0() #0
5+
__attribute__((patchable_function_entry(0))) void f0(void) {}
6+
7+
// COMMON: define{{.*}} void @f00() #0
8+
__attribute__((patchable_function_entry(0, 0, "__unused_section"))) void f00(void) {}
9+
10+
// COMMON: define{{.*}} void @f2() #1
11+
__attribute__((patchable_function_entry(2))) void f2(void) {}
12+
13+
// COMMON: define{{.*}} void @f20() #2
14+
__attribute__((patchable_function_entry(2, 0, "__attr_section"))) void f20(void) {}
15+
16+
// COMMON: define{{.*}} void @f44() #3
17+
__attribute__((patchable_function_entry(4, 4))) void f44(void) {}
18+
19+
// COMMON: define{{.*}} void @f52() #4
20+
__attribute__((patchable_function_entry(5, 2, "__attr_section"))) void f52(void) {}
21+
22+
// OPT: define{{.*}} void @f() #5
23+
void f(void) {}
24+
25+
/// No need to emit "patchable-function-entry" and thus also "patchable-function-entry-section"
26+
// COMMON: attributes #0 = { {{.*}}
27+
// COMMON-NOT: "patchable-function-entry-section"
28+
29+
// NODEFAULT: attributes #1 = { {{.*}} "patchable-function-entry"="2"
30+
// NODEFAULT-NOT: "patchable-function-entry-section"
31+
// DEFAULT: attributes #1 = { {{.*}} "patchable-function-entry"="2" "patchable-function-entry-section"="__default_section"
32+
33+
// COMMON: attributes #2 = { {{.*}} "patchable-function-entry"="2" "patchable-function-entry-section"="__attr_section"
34+
35+
// NODEFAULT: attributes #3 = { {{.*}} "patchable-function-entry"="0" "patchable-function-prefix"="4"
36+
// NODEFAULT-NOT: "patchable-function-entry-section"
37+
// DEFAULT: attributes #3 = { {{.*}} "patchable-function-entry"="0" "patchable-function-entry-section"="__default_section" "patchable-function-prefix"="4"
38+
39+
// COMMON: attributes #4 = { {{.*}} "patchable-function-entry"="3" "patchable-function-entry-section"="__attr_section" "patchable-function-prefix"="2"
40+
41+
// DEFAULT: attributes #5 = { {{.*}} "patchable-function-entry"="1" "patchable-function-entry-section"="__default_section"

clang/test/Driver/fpatchable-function-entry.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
// RUN: %clang --target=aarch64 -fsyntax-only %s -fpatchable-function-entry=2,1 -c -### 2>&1 | FileCheck --check-prefix=21 %s
1616
// 21: "-fpatchable-function-entry=2" "-fpatchable-function-entry-offset=1"
1717

18+
// RUN: %clang --target=aarch64 -fsyntax-only %s -fpatchable-function-entry=1,1,__section_name -c -### 2>&1 | FileCheck --check-prefix=SECTION %s
19+
// SECTION: "-fpatchable-function-entry=1" "-fpatchable-function-entry-offset=1" "-fpatchable-function-entry-section=__section_name"
20+
1821
// RUN: not %clang --target=powerpc64-ibm-aix-xcoff -fsyntax-only %s -fpatchable-function-entry=1 2>&1 | FileCheck --check-prefix=AIX64 %s
1922
// AIX64: error: unsupported option '-fpatchable-function-entry=1' for target 'powerpc64-ibm-aix-xcoff'
2023

0 commit comments

Comments
 (0)