diff options
author | Fangrui Song <maskray@google.com> | 2020-01-03 00:35:47 -0800 |
---|---|---|
committer | Fangrui Song <maskray@google.com> | 2020-01-10 09:55:51 -0800 |
commit | 4d1e23e3b3cd7c72a8b24dc5acb7e13c58a8de37 (patch) | |
tree | dc5bd17c830b737ce47c0d8887049a24b33eb7d5 /llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | |
parent | fd8ded99fe6e9fcae2c98ccad25d6562c5fa8a14 (diff) | |
download | bcm5719-llvm-4d1e23e3b3cd7c72a8b24dc5acb7e13c58a8de37.tar.gz bcm5719-llvm-4d1e23e3b3cd7c72a8b24dc5acb7e13c58a8de37.zip |
[AArch64] Add function attribute "patchable-function-entry" to add NOPs at function entry
The Linux kernel uses -fpatchable-function-entry to implement DYNAMIC_FTRACE_WITH_REGS
for arm64 and parisc. GCC 8 implemented
-fpatchable-function-entry, which can be seen as a generalized form of
-mnop-mcount. The N,M form (function entry points before the Mth NOP) is
currently only used by parisc.
This patch adds N,0 support to AArch64 codegen. N is represented as the
function attribute "patchable-function-entry". We will use a different
function attribute for M, if we decide to implement it.
The patch reuses the existing patchable-function pass, and
TargetOpcode::PATCHABLE_FUNCTION_ENTER which is currently used by XRay.
When the integrated assembler is used, __patchable_function_entries will
be created for each text section with the SHF_LINK_ORDER flag to prevent
--gc-sections (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93197) and
COMDAT (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93195) issues.
Retrospectively, __patchable_function_entries should use a PC-relative
relocation type to avoid the SHF_WRITE flag and dynamic relocations.
"patchable-function-entry"'s interaction with Branch Target
Identification is still unclear (see
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92424 for GCC discussions).
Reviewed By: peter.smith
Differential Revision: https://reviews.llvm.org/D72215
Diffstat (limited to 'llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp')
-rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 9f68b07b76c..1579646352d 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1222,6 +1222,8 @@ void AsmPrinter::EmitFunctionBody() { // Emit section containing stack size metadata. emitStackSizeSection(*MF); + emitPatchableFunctionEntries(); + if (isVerbose()) OutStreamer->GetCommentOS() << "-- End function\n"; @@ -1660,6 +1662,7 @@ MCSymbol *AsmPrinter::getCurExceptionSym() { void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { this->MF = &MF; + const Function &F = MF.getFunction(); // Get the function symbol. if (MAI->needsFunctionDescriptors()) { @@ -1672,7 +1675,6 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { CurrentFnSym = OutContext.getOrCreateSymbol("." + CurrentFnDescSym->getName()); - const Function &F = MF.getFunction(); MCSectionXCOFF *FnEntryPointSec = cast<MCSectionXCOFF>(getObjFileLowering().SectionForGlobal(&F, TM)); // Set the containing csect. @@ -1685,7 +1687,8 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { CurrentFnBegin = nullptr; CurExceptionSym = nullptr; bool NeedsLocalForSize = MAI->needsLocalForSize(); - if (needFuncLabelsForEHOrDebugInfo(MF, MMI) || NeedsLocalForSize || + if (F.hasFnAttribute("patchable-function-entry") || + needFuncLabelsForEHOrDebugInfo(MF, MMI) || NeedsLocalForSize || MF.getTarget().Options.EmitStackSizeSection) { CurrentFnBegin = createTempSymbol("func_begin"); if (NeedsLocalForSize) @@ -3194,6 +3197,38 @@ void AsmPrinter::recordSled(MCSymbol *Sled, const MachineInstr &MI, AlwaysInstrument, &F, Version}); } +void AsmPrinter::emitPatchableFunctionEntries() { + const Function &F = MF->getFunction(); + if (!F.hasFnAttribute("patchable-function-entry")) + return; + const unsigned PointerSize = getPointerSize(); + if (TM.getTargetTriple().isOSBinFormatELF()) { + auto Flags = ELF::SHF_WRITE | ELF::SHF_ALLOC; + std::string GroupName; + if (F.hasComdat()) { + Flags |= ELF::SHF_GROUP; + GroupName = F.getComdat()->getName(); + } + + // As of binutils 2.33, GNU as does not support section flag "o". Use + // SHF_LINK_ORDER if we are using the integrated assembler. + MCSymbolELF *Link = MAI->useIntegratedAssembler() + ? cast<MCSymbolELF>(CurrentFnSym) + : nullptr; + if (Link) + Flags |= ELF::SHF_LINK_ORDER; + + MCSection *Section = getObjFileLowering().SectionForGlobal(&F, TM); + auto R = PatchableFunctionEntryID.try_emplace( + Section, PatchableFunctionEntryID.size()); + OutStreamer->SwitchSection(OutContext.getELFSection( + "__patchable_function_entries", ELF::SHT_PROGBITS, Flags, 0, GroupName, + R.first->second, Link)); + EmitAlignment(Align(PointerSize)); + OutStreamer->EmitSymbolValue(CurrentFnBegin, PointerSize); + } +} + uint16_t AsmPrinter::getDwarfVersion() const { return OutStreamer->getContext().getDwarfVersion(); } |