diff --git a/llvm/include/llvm/IR/GlobalObject.h b/llvm/include/llvm/IR/GlobalObject.h index beb476239dae..2171d6e71cb0 100644 --- a/llvm/include/llvm/IR/GlobalObject.h +++ b/llvm/include/llvm/IR/GlobalObject.h @@ -54,8 +54,8 @@ public: unsigned getGlobalObjectSubClassData() const; void setGlobalObjectSubClassData(unsigned Val); - bool hasSection() const { return !StringRef(getSection()).empty(); } - const char *getSection() const { return Section.c_str(); } + bool hasSection() const { return !getSection().empty(); } + StringRef getSection() const { return Section; } void setSection(StringRef S); bool hasComdat() const { return getComdat() != nullptr; } diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h index 4a148db9c9e5..c6a4b3fdb91c 100644 --- a/llvm/include/llvm/IR/GlobalValue.h +++ b/llvm/include/llvm/IR/GlobalValue.h @@ -198,14 +198,8 @@ public: } void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; } - bool hasSection() const { return !StringRef(getSection()).empty(); } - // It is unfortunate that we have to use "char *" in here since this is - // always non NULL, but: - // * The C API expects a null terminated string, so we cannot use StringRef. - // * The C API expects us to own it, so we cannot use a std:string. - // * For GlobalAliases we can fail to find the section and we have to - // return "", so we cannot use a "const std::string &". - const char *getSection() const; + bool hasSection() const { return !getSection().empty(); } + StringRef getSection() const; /// Global values are always pointers. PointerType *getType() const { return cast(User::getType()); } diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index ca02e8ea9ce3..ed16eea8270b 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1528,7 +1528,7 @@ bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { } // Ignore debug and non-emitted data. This handles llvm.compiler.used. - if (StringRef(GV->getSection()) == "llvm.metadata" || + if (GV->getSection() == "llvm.metadata" || GV->hasAvailableExternallyLinkage()) return true; diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index d69e8d59d012..0c6c6f5157d9 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -1485,7 +1485,9 @@ void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) { } const char *LLVMGetSection(LLVMValueRef Global) { - return unwrap(Global)->getSection(); + // Using .data() is safe because of how GlobalObject::setSection is + // implemented. + return unwrap(Global)->getSection().data(); } void LLVMSetSection(LLVMValueRef Global, const char *Section) { diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp index a9702b7af81e..7e8ef6526fc1 100644 --- a/llvm/lib/IR/Globals.cpp +++ b/llvm/lib/IR/Globals.cpp @@ -128,7 +128,7 @@ std::string GlobalValue::getGlobalIdentifier() const { getParent()->getSourceFileName()); } -const char *GlobalValue::getSection() const { +StringRef GlobalValue::getSection() const { if (auto *GA = dyn_cast(this)) { // In general we cannot compute this at the IR level, but we try. if (const GlobalObject *GO = GA->getBaseObject()) @@ -151,7 +151,12 @@ Comdat *GlobalValue::getComdat() { return cast(this)->getComdat(); } -void GlobalObject::setSection(StringRef S) { Section = S; } +void GlobalObject::setSection(StringRef S) { + Section = S; + + // The C api requires this to be null terminated. + Section.c_str(); +} bool GlobalValue::isDeclaration() const { // Globals are definitions if they have an initializer. diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp index 1100daf71162..0d37ad1c6571 100644 --- a/llvm/lib/Linker/IRMover.cpp +++ b/llvm/lib/Linker/IRMover.cpp @@ -786,7 +786,7 @@ Constant *IRLinker::linkAppendingVarProto(GlobalVariable *DstGV, return nullptr; } - if (StringRef(DstGV->getSection()) != SrcGV->getSection()) { + if (DstGV->getSection() != SrcGV->getSection()) { emitError( "Appending variables with different section name need to be linked!"); return nullptr; diff --git a/llvm/lib/Object/IRObjectFile.cpp b/llvm/lib/Object/IRObjectFile.cpp index 76cb15630ae2..46012b7ed484 100644 --- a/llvm/lib/Object/IRObjectFile.cpp +++ b/llvm/lib/Object/IRObjectFile.cpp @@ -248,7 +248,7 @@ uint32_t IRObjectFile::getSymbolFlags(DataRefImpl Symb) const { if (GV->getName().startswith("llvm.")) Res |= BasicSymbolRef::SF_FormatSpecific; else if (auto *Var = dyn_cast(GV)) { - if (Var->getSection() == StringRef("llvm.metadata")) + if (Var->getSection() == "llvm.metadata") Res |= BasicSymbolRef::SF_FormatSpecific; } diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp index fe816ebb68e9..50232c384dd2 100644 --- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -1041,7 +1041,7 @@ void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar, // Skip meta data if (GVar->hasSection()) { - if (GVar->getSection() == StringRef("llvm.metadata")) + if (GVar->getSection() == "llvm.metadata") return; } diff --git a/llvm/lib/Target/XCore/XCoreISelLowering.cpp b/llvm/lib/Target/XCore/XCoreISelLowering.cpp index 171c3343ad44..c42b2f63e9c6 100644 --- a/llvm/lib/Target/XCore/XCoreISelLowering.cpp +++ b/llvm/lib/Target/XCore/XCoreISelLowering.cpp @@ -258,7 +258,7 @@ SDValue XCoreTargetLowering::getGlobalAddressWrapper(SDValue GA, return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA); const auto *GVar = dyn_cast(GV); - if ((GV->hasSection() && StringRef(GV->getSection()).startswith(".cp.")) || + if ((GV->hasSection() && GV->getSection().startswith(".cp.")) || (GVar && GVar->isConstant() && GV->hasLocalLinkage())) return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, GA); diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp index 858645369fe8..a4a52b7c8795 100644 --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -1242,7 +1242,7 @@ bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) { if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false; if (G->hasSection()) { - StringRef Section(G->getSection()); + StringRef Section = G->getSection(); // Globals from llvm.metadata aren't emitted, do not instrument them. if (Section == "llvm.metadata") return false;