diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -623,6 +623,9 @@ HelpText<"Emit Clang AST files for source inputs">; def emit_llvm : Flag<["-"], "emit-llvm">, Flags<[CC1Option]>, Group, HelpText<"Use the LLVM representation for assembler and object files">; +def emit_ifso : Flag<["-"], "emit-interface-stubs">, Flags<[CC1Option]>, Group, + HelpText<"Generate Inteface Stub Files.">; +def ifso_version_EQ : JoinedOrSeparate<["-"], "interface-stubs-version=">, Flags<[CC1Option]>; def exported__symbols__list : Separate<["-"], "exported_symbols_list">; def e : JoinedOrSeparate<["-"], "e">, Group; def fPIC : Flag<["-"], "fPIC">, Group; diff --git a/clang/include/clang/Driver/Types.def b/clang/include/clang/Driver/Types.def --- a/clang/include/clang/Driver/Types.def +++ b/clang/include/clang/Driver/Types.def @@ -88,6 +88,7 @@ // Misc. TYPE("ast", AST, INVALID, "ast", "u") +TYPE("ifs", IFS, INVALID, "ifs", "u") TYPE("pcm", ModuleFile, INVALID, "pcm", "u") TYPE("plist", Plist, INVALID, "plist", "") TYPE("rewritten-objc", RewrittenObjC,INVALID, "cpp", "") diff --git a/clang/include/clang/Frontend/FrontendActions.h b/clang/include/clang/Frontend/FrontendActions.h --- a/clang/include/clang/Frontend/FrontendActions.h +++ b/clang/include/clang/Frontend/FrontendActions.h @@ -119,6 +119,26 @@ bool hasASTFileSupport() const override { return false; } }; +class GenerateIFSOAction : public ASTFrontendAction { +protected: + TranslationUnitKind getTranslationUnitKind() override { return TU_Module; } + + bool hasASTFileSupport() const override { return false; } +}; + +// Support different interface stub formats this way: +class GenerateInterfaceYAMLExpV1Action : public GenerateIFSOAction { +protected: + std::unique_ptr CreateASTConsumer(CompilerInstance &CI, + StringRef InFile) override; +}; + +class GenerateInterfaceTBEExpV1Action : public GenerateIFSOAction { +protected: + std::unique_ptr CreateASTConsumer(CompilerInstance &CI, + StringRef InFile) override; +}; + class GenerateModuleFromModuleMapAction : public GenerateModuleAction { private: bool BeginSourceFileAction(CompilerInstance &CI) override; diff --git a/clang/include/clang/Frontend/FrontendOptions.h b/clang/include/clang/Frontend/FrontendOptions.h --- a/clang/include/clang/Frontend/FrontendOptions.h +++ b/clang/include/clang/Frontend/FrontendOptions.h @@ -87,6 +87,11 @@ /// Generate pre-compiled header. GeneratePCH, + /// Generate Interface Stub Files. + GenerateInterfaceYAMLExpV1, + GenerateInterfaceTBEExpV1, + GenerateInterfaceInvalid, + /// Only execute frontend initialization. InitOnly, diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -285,6 +285,7 @@ (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) || (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) || (PhaseArg = DAL.getLastArg(options::OPT__migrate)) || + (PhaseArg = DAL.getLastArg(options::OPT_emit_ifso)) || (PhaseArg = DAL.getLastArg(options::OPT__analyze, options::OPT__analyze_auto)) || (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { @@ -3445,6 +3446,8 @@ return C.MakeAction(Input, types::TY_ModuleFile); if (Args.hasArg(options::OPT_verify_pch)) return C.MakeAction(Input, types::TY_Nothing); + if (Args.hasArg(options::OPT_emit_ifso)) + return C.MakeAction(Input, types::TY_IFS); return C.MakeAction(Input, types::TY_LLVM_BC); } case phases::Backend: { diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -3600,6 +3600,25 @@ } else if (JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC) { CmdArgs.push_back("-emit-llvm-bc"); + } else if (JA.getType() == types::TY_IFS) { + StringRef StubFormat = + llvm::StringSwitch( + Args.hasArg(options::OPT_ifso_version_EQ) + ? Args.getLastArgValue(options::OPT_ifso_version_EQ) + : "") + .Case("experimental-yaml-elf-v1", "experimental-yaml-elf-v1") + .Case("tapi-tbe", "tapi-tbe") + .Default(""); + + if (StubFormat.empty()) + D.Diag(diag::err_drv_invalid_value) + << "Must specify a valid interface stub format type using " + << "-interface-stubs-version="; + + CmdArgs.push_back("-emit-interface-stubs"); + CmdArgs.push_back( + Args.MakeArgString(Twine("-interface-stubs-version=") + StubFormat)); } else if (JA.getType() == types::TY_PP_Asm) { CmdArgs.push_back("-S"); } else if (JA.getType() == types::TY_AST) { diff --git a/clang/lib/Frontend/CMakeLists.txt b/clang/lib/Frontend/CMakeLists.txt --- a/clang/lib/Frontend/CMakeLists.txt +++ b/clang/lib/Frontend/CMakeLists.txt @@ -45,6 +45,7 @@ TextDiagnosticBuffer.cpp TextDiagnosticPrinter.cpp VerifyDiagnosticConsumer.cpp + InterfaceStubFunctionsConsumer.cpp DEPENDS ClangDriverOptions @@ -54,6 +55,7 @@ clangAST clangBasic clangDriver + clangIndex clangEdit clangLex clangParse diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1645,6 +1645,20 @@ Opts.ProgramAction = frontend::GenerateHeaderModule; break; case OPT_emit_pch: Opts.ProgramAction = frontend::GeneratePCH; break; + case OPT_emit_ifso: { + Opts.ProgramAction = + llvm::StringSwitch( + Args.hasArg(OPT_ifso_version_EQ) + ? Args.getLastArgValue(OPT_ifso_version_EQ) + : "") + .Case("experimental-yaml-elf-v1", + frontend::GenerateInterfaceYAMLExpV1) + .Case("tapi-tbe", frontend::GenerateInterfaceTBEExpV1) + .Default(frontend::GenerateInterfaceInvalid); + if (Opts.ProgramAction == frontend::GenerateInterfaceInvalid) + llvm_unreachable("Must specify a valid interface stub format."); + break; + } case OPT_init_only: Opts.ProgramAction = frontend::InitOnly; break; case OPT_fsyntax_only: @@ -3066,6 +3080,9 @@ case frontend::GenerateModuleInterface: case frontend::GenerateHeaderModule: case frontend::GeneratePCH: + case frontend::GenerateInterfaceYAMLExpV1: + case frontend::GenerateInterfaceTBEExpV1: + case frontend::GenerateInterfaceInvalid: case frontend::ParseSyntaxOnly: case frontend::ModuleFileInfo: case frontend::VerifyPCH: diff --git a/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp b/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp new file mode 100644 --- /dev/null +++ b/clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp @@ -0,0 +1,280 @@ +//===--- InterfaceStubFunctionsConsumer.cpp -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/RecursiveASTVisitor.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendActions.h" +#include "clang/Index/CodegenNameGenerator.h" +#include "clang/Sema/TemplateInstCallback.h" +#include "llvm/BinaryFormat/ELF.h" + +using namespace clang; + +class IFSOFunctionsConsumer : public ASTConsumer { + CompilerInstance &Instance; + StringRef InFile = ""; + StringRef Format = ""; + std::set ParsedTemplates; + + enum RootDeclOrigin { TopLevel = 0, FromTU = 1, IsLate = 2 }; + struct MangledSymbol { + std::string Name = ""; + uint8_t Type; + uint8_t Binding; + MangledSymbol() = delete; + MangledSymbol(const std::string &Name, uint8_t Type, uint8_t Binding) + : Name(Name), Type(Type), Binding(Binding) {} + }; + using MangledSymbols = std::map; + + template + bool WriteNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) { + if (!isa(ND)) + return false; + if (Symbols.find(ND) != Symbols.end()) + return true; + if (isa(ND)) + return true; + + // Here we filter out anything that's not set to DefaultVisibility. + // DefaultVisibility is set on a decl when -fvisibility is not specified on + // the command line (or specified as default) and the decl does not have + // __attribute__((visibility("hidden"))) set or when the command line + // argument is set to hidden but the decl explicitly has + // __attribute__((visibility ("default"))) set. We do this so that the user + // can have fine grain control of what they want to expose in the stub. + if (ND->getVisibility() != DefaultVisibility) + return true; + + // If this is a FunctionDecl that is dependent on a template parameter, then + // don't get the symbol because we can only export specializations. + bool IsRDOLate = (RDO & IsLate); + if (const auto *FD = dyn_cast(ND)) + if (FD->isDependentContext() && !IsRDOLate) + return true; + + index::CodegenNameGenerator CGNameGen(ND->getASTContext()); + std::string MangledName = CGNameGen.getName(ND); + const bool IsWeak = (ND->hasAttr() || + ND->hasAttr() || ND->isWeakImported()); + uint8_t Type = + isa(ND) ? llvm::ELF::STT_OBJECT : llvm::ELF::STT_FUNC; + uint8_t Binding = IsWeak ? llvm::ELF::STB_WEAK : llvm::ELF::STB_GLOBAL; + Symbols.insert( + std::make_pair(ND, MangledSymbol(MangledName, Type, Binding))); + + if (IsRDOLate) + llvm_unreachable("Generating Interface Stubs is not supported with " + "delayed template parsing."); + return true; + } + + template + bool HandleSomeDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) { + if (!isa(ND)) + return false; + for (const auto *D : cast(ND)->decls()) + HandleNamedDecl(dyn_cast(D), Symbols, RDO); + return true; + } + + template + bool HandleSomeDeclSpec(const NamedDecl *ND, MangledSymbols &Symbols, + int RDO) { + if (!isa(ND)) + return false; + for (const auto *D : cast(ND)->specializations()) + HandleNamedDecl(dyn_cast(D), Symbols, RDO); + return true; + } + + bool HandleNamedDecl(const NamedDecl *ND, MangledSymbols &Symbols, int RDO) { + if (!ND) + return false; + // Handle NamespaceDecls first so that we fully recurse to every decl. + if (HandleSomeDecl(ND, Symbols, RDO) || + HandleSomeDecl(ND, Symbols, RDO) || + HandleSomeDeclSpec(ND, Symbols, RDO) || + HandleSomeDeclSpec(ND, Symbols, RDO)) + return true; + if (!(RDO & FromTU)) + return true; + if (const auto *TTPD = dyn_cast(ND)) + return true; + if (WriteNamedDecl(ND, Symbols, RDO) || + WriteNamedDecl(ND, Symbols, RDO) || + WriteNamedDecl(ND, Symbols, RDO)) + return true; + // While IFSOs are in the development stage, it's probably best to catch + // anything that's not a VarDecl or Template/FunctionDecl. + ND->dump(); + llvm_unreachable("ifso: Expected a function or function template decl."); + return false; + } + +public: + IFSOFunctionsConsumer(CompilerInstance &Instance, StringRef InFile, + StringRef Format) + : Instance(Instance), InFile(InFile), Format(Format) {} + + void HandleTranslationUnit(ASTContext &context) override { + struct Visitor : public RecursiveASTVisitor { + bool VisitNamedDecl(NamedDecl *ND) { + if (auto *FD = dyn_cast(ND)) + (FD->isLateTemplateParsed() ? LateParsedDecls : NamedDecls) + .insert(FD); + else if (auto *VD = dyn_cast(ND)) + ValueDecls.insert(VD); + else + NamedDecls.insert(ND); + return true; + } + + std::set LateParsedDecls; + std::set NamedDecls; + std::set ValueDecls; + } v; + + v.TraverseDecl(context.getTranslationUnitDecl()); + + MangledSymbols Symbols; + auto OS = Instance.createDefaultOutputFile(/*Binary=*/false, InFile, "ifo"); + if (!OS) + return; + + if (Instance.getLangOpts().DelayedTemplateParsing) { + clang::Sema &S = Instance.getSema(); + for (const auto *FD : v.LateParsedDecls) { + clang::LateParsedTemplate &LPT = + *S.LateParsedTemplateMap.find(cast(FD))->second; + S.LateTemplateParser(S.OpaqueParser, LPT); + HandleNamedDecl(FD, Symbols, (FromTU | IsLate)); + } + } + + for (const NamedDecl *ND : v.ValueDecls) + HandleNamedDecl(ND, Symbols, FromTU); + for (const NamedDecl *ND : v.NamedDecls) + HandleNamedDecl(ND, Symbols, FromTU); + + auto writeIfoYaml = [](const llvm::Triple &T, const MangledSymbols &Symbols, + const ASTContext &context, StringRef Format, + raw_ostream &OS) -> void { + OS << "--- !" << Format << "\n"; + OS << "FileHeader:\n"; + OS << " Class: ELFCLASS"; + OS << (T.isArch64Bit() ? "64" : "32"); + OS << "\n"; + OS << " Data: ELFDATA2"; + OS << (T.isLittleEndian() ? "LSB" : "MSB"); + OS << "\n"; + OS << " Type: ET_REL\n"; + OS << " Machine: " + << llvm::StringSwitch(T.getArchName()) + .Case("x86_64", "EM_X86_64") + .Case("i386", "EM_386") + .Case("i686", "EM_386") + .Case("aarch64", "EM_AARCH64") + .Case("amdgcn", "EM_AMDGPU") + .Case("r600", "EM_AMDGPU") + .Case("arm", "EM_ARM") + .Case("thumb", "EM_ARM") + .Case("avr", "EM_AVR") + .Case("mips", "EM_MIPS") + .Case("mipsel", "EM_MIPS") + .Case("mips64", "EM_MIPS") + .Case("mips64el", "EM_MIPS") + .Case("msp430", "EM_MSP430") + .Case("ppc", "EM_PPC") + .Case("ppc64", "EM_PPC64") + .Case("ppc64le", "EM_PPC64") + .Case("x86", T.isOSIAMCU() ? "EM_IAMCU" : "EM_386") + .Case("x86_64", "EM_X86_64") + .Default("EM_NONE") + << "\nSymbols:\n"; + for (auto E : Symbols) { + const MangledSymbol &Symbol = E.second; + OS << " - Name: " << Symbol.Name << "\n" + << " Type: STT_"; + switch (Symbol.Type) { + default: + case llvm::ELF::STT_NOTYPE: + OS << "NOTYPE"; + break; + case llvm::ELF::STT_OBJECT: + OS << "OBJECT"; + break; + case llvm::ELF::STT_FUNC: + OS << "FUNC"; + break; + } + OS << "\n Binding: STB_" + << ((Symbol.Binding == llvm::ELF::STB_WEAK) ? "WEAK" : "GLOBAL") + << "\n"; + } + OS << "...\n"; + OS.flush(); + }; + + auto writeIfoElfAbiYaml = [](const llvm::Triple &T, + const MangledSymbols &Symbols, + const ASTContext &context, StringRef Format, + raw_ostream &OS) -> void { + OS << "--- !" << Format << "\n"; + OS << "TbeVersion: 1.0\n"; + OS << "Arch: " << T.getArchName() << "\n"; + OS << "Endian: " << (T.isLittleEndian() ? "little" : "big") << "\n"; + OS << "Symbols:\n"; + for (auto E : Symbols) { + const MangledSymbol &Symbol = E.second; + OS << " " << Symbol.Name << ": { Type: "; + switch (Symbol.Type) { + default: + llvm_unreachable("clang -emit-ifso: Unexpected symbol type."); + case llvm::ELF::STT_NOTYPE: + OS << "NoType"; + break; + case llvm::ELF::STT_OBJECT: { + auto VD = cast(E.first)->getType(); + OS << "Object, Size: " + << context.getTypeSizeInChars(VD).getQuantity(); + break; + } + case llvm::ELF::STT_FUNC: + OS << "Func"; + break; + } + if (Symbol.Binding == llvm::ELF::STB_WEAK) + OS << ", Weak: true"; + OS << " }\n"; + } + OS << "...\n"; + OS.flush(); + }; + + if (Format == "experimental-yaml-elf-v1") + writeIfoYaml(Instance.getTarget().getTriple(), Symbols, context, Format, + *OS); + else + writeIfoElfAbiYaml(Instance.getTarget().getTriple(), Symbols, context, + Format, *OS); + } +}; + +std::unique_ptr +GenerateInterfaceYAMLExpV1Action::CreateASTConsumer(CompilerInstance &CI, + StringRef InFile) { + return llvm::make_unique(CI, InFile, + "experimental-yaml-elf-v1"); +} + +std::unique_ptr +GenerateInterfaceTBEExpV1Action::CreateASTConsumer(CompilerInstance &CI, + StringRef InFile) { + return llvm::make_unique(CI, InFile, "tapi-tbe"); +} diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -64,6 +64,10 @@ case GenerateHeaderModule: return llvm::make_unique(); case GeneratePCH: return llvm::make_unique(); + case GenerateInterfaceYAMLExpV1: + return llvm::make_unique(); + case GenerateInterfaceTBEExpV1: + return llvm::make_unique(); case InitOnly: return llvm::make_unique(); case ParseSyntaxOnly: return llvm::make_unique(); case ModuleFileInfo: return llvm::make_unique(); diff --git a/clang/test/IFSO/foo-inline.h b/clang/test/IFSO/foo-inline.h new file mode 100644 --- /dev/null +++ b/clang/test/IFSO/foo-inline.h @@ -0,0 +1,6 @@ + +inline int fvih() { +static int fortytwo = 42; + return fortytwo; +} + diff --git a/clang/test/IFSO/foo.cpp b/clang/test/IFSO/foo.cpp new file mode 100644 --- /dev/null +++ b/clang/test/IFSO/foo.cpp @@ -0,0 +1,57 @@ +// Using %clang instead of %clang_cc1 because of -fvisibility +// RUN: %clang -target x86_64-linux-gnu -emit-interface-stubs -interface-stubs-version=tapi-tbe -fvisibility=hidden %s -o - | FileCheck --check-prefix=CHECK-HIDDEN %s +// RUN: %clang -target x86_64-linux-gnu -emit-interface-stubs -interface-stubs-version=tapi-tbe %s -o - | FileCheck %s + +// CHECK-HIDDEN-NOT: _Z4fbarff +// CHECK: _Z4fbarff + + + + +// CHECK-HIDDEN-NOT: _Z3fooii +// CHECK-NOT: _Z3fooii + + + +#include "foo-inline.h" + +__attribute__ ((visibility ("hidden"))) int foo(int a, int b) { return a + b; } +__attribute__ ((visibility ("default"))) int foo_default_visi(int a, int b) { return a + b; } + + +__attribute__ ((visibility ("default"))) int fvih_1(int a, int b) { return a + fvih(); } + + +__attribute__((weak)) int someWeakFunc() { return 42; } + +int dataA = 34; + +namespace baz { + template + T add(T a, T b) { + return a + b; + } +} + +namespace n { + template + struct __attribute__((__visibility__("default"))) S { + S() = default; + ~S() = default; + int __attribute__((__visibility__(("default")))) func() const { return 32; } + int __attribute__((__visibility__(("hidden")))) operator()() const { return 53; } + }; +} + +template T neverUsed(T t) { return t + 2; } + +template<> int neverUsed(int t); + +void g() { n::S()(); } + +namespace qux { +int bar(int a, int b) { return baz::add(a, b); } +} + +float fbar(float a, float b) { return baz::add(a, b); } + diff --git a/llvm/include/llvm/TextAPI/ELF/ELFStub.h b/llvm/include/llvm/TextAPI/ELF/ELFStub.h --- a/llvm/include/llvm/TextAPI/ELF/ELFStub.h +++ b/llvm/include/llvm/TextAPI/ELF/ELFStub.h @@ -55,6 +55,7 @@ VersionTuple TbeVersion; Optional SoName; ELFArch Arch; + Optional Endian; std::vector NeededLibs; std::set Symbols; diff --git a/llvm/lib/TextAPI/ELF/TBEHandler.cpp b/llvm/lib/TextAPI/ELF/TBEHandler.cpp --- a/llvm/lib/TextAPI/ELF/TBEHandler.cpp +++ b/llvm/lib/TextAPI/ELF/TBEHandler.cpp @@ -134,6 +134,7 @@ IO.mapRequired("TbeVersion", Stub.TbeVersion); IO.mapOptional("SoName", Stub.SoName); IO.mapRequired("Arch", (ELFArchMapper &)Stub.Arch); + IO.mapOptional("Endian", Stub.Endian); IO.mapOptional("NeededLibs", Stub.NeededLibs); IO.mapRequired("Symbols", Stub.Symbols); }