From 2a67c9ee39c3a2dd298a9a0aec96a1eff6349344 Mon Sep 17 00:00:00 2001 From: Javed Absar Date: Mon, 5 Jun 2017 10:11:57 +0000 Subject: Add support for #pragma clang section This patch provides a means to specify section-names for global variables, functions and static variables, using #pragma directives. This feature is only defined to work sensibly for ELF targets. One can specify section names as: #pragma clang section bss="myBSS" data="myData" rodata="myRodata" text="myText" One can "unspecify" a section name with empty string e.g. #pragma clang section bss="" data="" text="" rodata="" Reviewers: Roger Ferrer, Jonathan Roelofs, Reid Kleckner Differential Revision: https://reviews.llvm.org/D33412 llvm-svn: 304705 --- clang/lib/CodeGen/CodeGenModule.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index dde8f2e36920..77adf7b441a2 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1026,9 +1026,25 @@ void CodeGenModule::setNonAliasAttributes(const Decl *D, llvm::GlobalObject *GO) { SetCommonAttributes(D, GO); - if (D) + if (D) { + if (auto *GV = dyn_cast(GO)) { + if (auto *SA = D->getAttr()) + GV->addAttribute("bss-section", SA->getName()); + if (auto *SA = D->getAttr()) + GV->addAttribute("data-section", SA->getName()); + if (auto *SA = D->getAttr()) + GV->addAttribute("rodata-section", SA->getName()); + } + + if (auto *F = dyn_cast(GO)) { + if (auto *SA = D->getAttr()) + if (!D->getAttr()) + F->addFnAttr("implicit-section-name", SA->getName()); + } + if (const SectionAttr *SA = D->getAttr()) GO->setSection(SA->getName()); + } getTargetCodeGenInfo().setTargetAttributes(D, GO, *this); } @@ -1127,6 +1143,10 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, setLinkageAndVisibilityForGV(F, FD); + if (FD->getAttr()) { + F->addFnAttr("implicit-section-name"); + } + if (const SectionAttr *SA = FD->getAttr()) F->setSection(SA->getName()); @@ -2828,6 +2848,14 @@ static bool isVarDeclStrongDefinition(const ASTContext &Context, if (D->hasAttr()) return true; + // A variable cannot be both common and exist in a section. + // We dont try to determine which is the right section in the front-end. + // If no specialized section name is applicable, it will resort to default. + if (D->hasAttr() || + D->hasAttr() || + D->hasAttr()) + return true; + // Thread local vars aren't considered common linkage. if (D->getTLSKind()) return true; -- cgit v1.2.3