1 //===-- Internalize.cpp - Mark functions internal -------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass loops over all of the functions and variables in the input module. 10 // If the function or variable does not need to be preserved according to the 11 // client supplied callback, it is marked as internal. 12 // 13 // This transformation would not be legal in a regular compilation, but it gets 14 // extra information from the linker about what is safe. 15 // 16 // For example: Internalizing a function with external linkage. Only if we are 17 // told it is only used from within this module, it is safe to do it. 18 // 19 //===----------------------------------------------------------------------===// 20 21 #include "llvm/Transforms/IPO/Internalize.h" 22 #include "llvm/ADT/SmallPtrSet.h" 23 #include "llvm/ADT/Statistic.h" 24 #include "llvm/ADT/StringSet.h" 25 #include "llvm/ADT/Triple.h" 26 #include "llvm/Analysis/CallGraph.h" 27 #include "llvm/IR/Module.h" 28 #include "llvm/InitializePasses.h" 29 #include "llvm/Pass.h" 30 #include "llvm/Support/CommandLine.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/LineIterator.h" 33 #include "llvm/Support/MemoryBuffer.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include "llvm/Transforms/IPO.h" 36 #include "llvm/Transforms/Utils/GlobalStatus.h" 37 #include "llvm/Transforms/Utils/ModuleUtils.h" 38 using namespace llvm; 39 40 #define DEBUG_TYPE "internalize" 41 42 STATISTIC(NumAliases, "Number of aliases internalized"); 43 STATISTIC(NumFunctions, "Number of functions internalized"); 44 STATISTIC(NumGlobals, "Number of global vars internalized"); 45 46 // APIFile - A file which contains a list of symbols that should not be marked 47 // external. 48 static cl::opt<std::string> 49 APIFile("internalize-public-api-file", cl::value_desc("filename"), 50 cl::desc("A file containing list of symbol names to preserve")); 51 52 // APIList - A list of symbols that should not be marked internal. 53 static cl::list<std::string> 54 APIList("internalize-public-api-list", cl::value_desc("list"), 55 cl::desc("A list of symbol names to preserve"), cl::CommaSeparated); 56 57 namespace { 58 // Helper to load an API list to preserve from file and expose it as a functor 59 // for internalization. 60 class PreserveAPIList { 61 public: 62 PreserveAPIList() { 63 if (!APIFile.empty()) 64 LoadFile(APIFile); 65 ExternalNames.insert(APIList.begin(), APIList.end()); 66 } 67 68 bool operator()(const GlobalValue &GV) { 69 return ExternalNames.count(GV.getName()); 70 } 71 72 private: 73 // Contains the set of symbols loaded from file 74 StringSet<> ExternalNames; 75 76 void LoadFile(StringRef Filename) { 77 // Load the APIFile... 78 ErrorOr<std::unique_ptr<MemoryBuffer>> Buf = 79 MemoryBuffer::getFile(Filename); 80 if (!Buf) { 81 errs() << "WARNING: Internalize couldn't load file '" << Filename 82 << "'! Continuing as if it's empty.\n"; 83 return; // Just continue as if the file were empty 84 } 85 for (line_iterator I(*Buf->get(), true), E; I != E; ++I) 86 ExternalNames.insert(*I); 87 } 88 }; 89 } // end anonymous namespace 90 91 bool InternalizePass::shouldPreserveGV(const GlobalValue &GV) { 92 // Function must be defined here 93 if (GV.isDeclaration()) 94 return true; 95 96 // Available externally is really just a "declaration with a body". 97 if (GV.hasAvailableExternallyLinkage()) 98 return true; 99 100 // Assume that dllexported symbols are referenced elsewhere 101 if (GV.hasDLLExportStorageClass()) 102 return true; 103 104 // Already local, has nothing to do. 105 if (GV.hasLocalLinkage()) 106 return false; 107 108 // Check some special cases 109 if (AlwaysPreserved.count(GV.getName())) 110 return true; 111 112 return MustPreserveGV(GV); 113 } 114 115 bool InternalizePass::maybeInternalize( 116 GlobalValue &GV, DenseMap<const Comdat *, ComdatInfo> &ComdatMap) { 117 SmallString<0> ComdatName; 118 if (Comdat *C = GV.getComdat()) { 119 // For GlobalAlias, C is the aliasee object's comdat which may have been 120 // redirected. So ComdatMap may not contain C. 121 if (ComdatMap.lookup(C).External) 122 return false; 123 124 if (auto *GO = dyn_cast<GlobalObject>(&GV)) { 125 // If a comdat with one member is not externally visible, we can drop it. 126 // Otherwise, the comdat can be used to establish dependencies among the 127 // group of sections. Thus we have to keep the comdat. 128 // 129 // On ELF, GNU ld and gold use the signature name as the comdat 130 // deduplication key. Rename the comdat to suppress deduplication with 131 // other object files. On COFF, non-external selection symbol suppresses 132 // deduplication and thus does not need renaming. 133 ComdatInfo &Info = ComdatMap.find(C)->second; 134 if (Info.Size == 1) { 135 GO->setComdat(nullptr); 136 } else if (IsELF) { 137 if (Info.Dest == nullptr) 138 Info.Dest = GV.getParent()->getOrInsertComdat( 139 (C->getName() + ModuleId).toStringRef(ComdatName)); 140 GO->setComdat(Info.Dest); 141 } 142 } 143 144 if (GV.hasLocalLinkage()) 145 return false; 146 } else { 147 if (GV.hasLocalLinkage()) 148 return false; 149 150 if (shouldPreserveGV(GV)) 151 return false; 152 } 153 154 GV.setVisibility(GlobalValue::DefaultVisibility); 155 GV.setLinkage(GlobalValue::InternalLinkage); 156 return true; 157 } 158 159 // If GV is part of a comdat and is externally visible, update the comdat size 160 // and keep track of its comdat so that we don't internalize any of its members. 161 void InternalizePass::checkComdat( 162 GlobalValue &GV, DenseMap<const Comdat *, ComdatInfo> &ComdatMap) { 163 Comdat *C = GV.getComdat(); 164 if (!C) 165 return; 166 167 ComdatInfo &Info = ComdatMap.try_emplace(C).first->second; 168 ++Info.Size; 169 if (shouldPreserveGV(GV)) 170 Info.External = true; 171 } 172 173 bool InternalizePass::internalizeModule(Module &M, CallGraph *CG) { 174 bool Changed = false; 175 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr; 176 177 SmallVector<GlobalValue *, 4> Used; 178 collectUsedGlobalVariables(M, Used, false); 179 180 // Collect comdat size and visiblity information for the module. 181 DenseMap<const Comdat *, ComdatInfo> ComdatMap; 182 if (!M.getComdatSymbolTable().empty()) { 183 for (Function &F : M) 184 checkComdat(F, ComdatMap); 185 for (GlobalVariable &GV : M.globals()) 186 checkComdat(GV, ComdatMap); 187 for (GlobalAlias &GA : M.aliases()) 188 checkComdat(GA, ComdatMap); 189 } 190 191 // We must assume that globals in llvm.used have a reference that not even 192 // the linker can see, so we don't internalize them. 193 // For llvm.compiler.used the situation is a bit fuzzy. The assembler and 194 // linker can drop those symbols. If this pass is running as part of LTO, 195 // one might think that it could just drop llvm.compiler.used. The problem 196 // is that even in LTO llvm doesn't see every reference. For example, 197 // we don't see references from function local inline assembly. To be 198 // conservative, we internalize symbols in llvm.compiler.used, but we 199 // keep llvm.compiler.used so that the symbol is not deleted by llvm. 200 for (GlobalValue *V : Used) { 201 AlwaysPreserved.insert(V->getName()); 202 } 203 204 // Mark all functions not in the api as internal. 205 ModuleId = getUniqueModuleId(&M); 206 IsELF = Triple(M.getTargetTriple()).isOSBinFormatELF(); 207 for (Function &I : M) { 208 if (!maybeInternalize(I, ComdatMap)) 209 continue; 210 Changed = true; 211 212 if (ExternalNode) 213 // Remove a callgraph edge from the external node to this function. 214 ExternalNode->removeOneAbstractEdgeTo((*CG)[&I]); 215 216 ++NumFunctions; 217 LLVM_DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n"); 218 } 219 220 // Never internalize the llvm.used symbol. It is used to implement 221 // attribute((used)). 222 // FIXME: Shouldn't this just filter on llvm.metadata section?? 223 AlwaysPreserved.insert("llvm.used"); 224 AlwaysPreserved.insert("llvm.compiler.used"); 225 226 // Never internalize anchors used by the machine module info, else the info 227 // won't find them. (see MachineModuleInfo.) 228 AlwaysPreserved.insert("llvm.global_ctors"); 229 AlwaysPreserved.insert("llvm.global_dtors"); 230 AlwaysPreserved.insert("llvm.global.annotations"); 231 232 // Never internalize symbols code-gen inserts. 233 // FIXME: We should probably add this (and the __stack_chk_guard) via some 234 // type of call-back in CodeGen. 235 AlwaysPreserved.insert("__stack_chk_fail"); 236 AlwaysPreserved.insert("__stack_chk_guard"); 237 238 // Mark all global variables with initializers that are not in the api as 239 // internal as well. 240 for (auto &GV : M.globals()) { 241 if (!maybeInternalize(GV, ComdatMap)) 242 continue; 243 Changed = true; 244 245 ++NumGlobals; 246 LLVM_DEBUG(dbgs() << "Internalized gvar " << GV.getName() << "\n"); 247 } 248 249 // Mark all aliases that are not in the api as internal as well. 250 for (auto &GA : M.aliases()) { 251 if (!maybeInternalize(GA, ComdatMap)) 252 continue; 253 Changed = true; 254 255 ++NumAliases; 256 LLVM_DEBUG(dbgs() << "Internalized alias " << GA.getName() << "\n"); 257 } 258 259 return Changed; 260 } 261 262 InternalizePass::InternalizePass() : MustPreserveGV(PreserveAPIList()) {} 263 264 PreservedAnalyses InternalizePass::run(Module &M, ModuleAnalysisManager &AM) { 265 if (!internalizeModule(M, AM.getCachedResult<CallGraphAnalysis>(M))) 266 return PreservedAnalyses::all(); 267 268 PreservedAnalyses PA; 269 PA.preserve<CallGraphAnalysis>(); 270 return PA; 271 } 272 273 namespace { 274 class InternalizeLegacyPass : public ModulePass { 275 // Client supplied callback to control wheter a symbol must be preserved. 276 std::function<bool(const GlobalValue &)> MustPreserveGV; 277 278 public: 279 static char ID; // Pass identification, replacement for typeid 280 281 InternalizeLegacyPass() : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {} 282 283 InternalizeLegacyPass(std::function<bool(const GlobalValue &)> MustPreserveGV) 284 : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) { 285 initializeInternalizeLegacyPassPass(*PassRegistry::getPassRegistry()); 286 } 287 288 bool runOnModule(Module &M) override { 289 if (skipModule(M)) 290 return false; 291 292 CallGraphWrapperPass *CGPass = 293 getAnalysisIfAvailable<CallGraphWrapperPass>(); 294 CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr; 295 return internalizeModule(M, MustPreserveGV, CG); 296 } 297 298 void getAnalysisUsage(AnalysisUsage &AU) const override { 299 AU.setPreservesCFG(); 300 AU.addPreserved<CallGraphWrapperPass>(); 301 } 302 }; 303 } 304 305 char InternalizeLegacyPass::ID = 0; 306 INITIALIZE_PASS(InternalizeLegacyPass, "internalize", 307 "Internalize Global Symbols", false, false) 308 309 ModulePass *llvm::createInternalizePass() { 310 return new InternalizeLegacyPass(); 311 } 312 313 ModulePass *llvm::createInternalizePass( 314 std::function<bool(const GlobalValue &)> MustPreserveGV) { 315 return new InternalizeLegacyPass(std::move(MustPreserveGV)); 316 } 317