1 //===-- SymbolRewriter.h - Symbol Rewriting Pass ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the prototypes and definitions related to the Symbol
13 // The Symbol Rewriter pass takes a set of rewrite descriptors which define
14 // transformations for symbol names. These can be either single name to name
15 // trnsformation or more broad regular expression based transformations.
17 // All the functions are re-written at the IR level. The Symbol Rewriter itself
18 // is exposed as a module level pass. All symbols at the module level are
19 // iterated. For any matching symbol, the requested transformation is applied,
20 // updating references to it as well (a la RAUW). The resulting binary will
21 // only contain the rewritten symbols.
23 // By performing this operation in the compiler, we are able to catch symbols
24 // that would otherwise not be possible to catch (e.g. inlined symbols).
26 // This makes it possible to cleanly transform symbols without resorting to
27 // overly-complex macro tricks and the pre-processor. An example of where this
28 // is useful is the sanitizers where we would like to intercept a well-defined
29 // set of functions across the module.
31 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_TRANSFORMS_UTILS_SYMBOLREWRITER_H
34 #define LLVM_TRANSFORMS_UTILS_SYMBOLREWRITER_H
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/PassManager.h"
53 } // end namespace yaml
55 namespace SymbolRewriter {
57 /// The basic entity representing a rewrite operation. It serves as the base
58 /// class for any rewrite descriptor. It has a certain set of specializations
59 /// which describe a particular rewrite.
61 /// The RewriteMapParser can be used to parse a mapping file that provides the
62 /// mapping for rewriting the symbols. The descriptors individually describe
63 /// whether to rewrite a function, global variable, or global alias. Each of
64 /// these can be selected either by explicitly providing a name for the ones to
65 /// be rewritten or providing a (posix compatible) regular expression that will
66 /// select the symbols to rewrite. This descriptor list is passed to the
67 /// SymbolRewriter pass.
68 class RewriteDescriptor {
72 Function, /// function - descriptor rewrites a function
73 GlobalVariable, /// global variable - descriptor rewrites a global variable
74 NamedAlias, /// named alias - descriptor rewrites a global alias
77 RewriteDescriptor(const RewriteDescriptor &) = delete;
78 RewriteDescriptor &operator=(const RewriteDescriptor &) = delete;
79 virtual ~RewriteDescriptor() = default;
81 Type getType() const { return Kind; }
83 virtual bool performOnModule(Module &M) = 0;
86 explicit RewriteDescriptor(Type T) : Kind(T) {}
92 typedef std::list<std::unique_ptr<RewriteDescriptor>> RewriteDescriptorList;
94 class RewriteMapParser {
96 bool parse(const std::string &MapFile, RewriteDescriptorList *Descriptors);
99 bool parse(std::unique_ptr<MemoryBuffer> &MapFile, RewriteDescriptorList *DL);
100 bool parseEntry(yaml::Stream &Stream, yaml::KeyValueNode &Entry,
101 RewriteDescriptorList *DL);
102 bool parseRewriteFunctionDescriptor(yaml::Stream &Stream,
103 yaml::ScalarNode *Key,
104 yaml::MappingNode *Value,
105 RewriteDescriptorList *DL);
106 bool parseRewriteGlobalVariableDescriptor(yaml::Stream &Stream,
107 yaml::ScalarNode *Key,
108 yaml::MappingNode *Value,
109 RewriteDescriptorList *DL);
110 bool parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
111 yaml::MappingNode *V,
112 RewriteDescriptorList *DL);
115 } // end namespace SymbolRewriter
117 ModulePass *createRewriteSymbolsPass();
118 ModulePass *createRewriteSymbolsPass(SymbolRewriter::RewriteDescriptorList &);
120 class RewriteSymbolPass : public PassInfoMixin<RewriteSymbolPass> {
122 RewriteSymbolPass() { loadAndParseMapFiles(); }
123 RewriteSymbolPass(SymbolRewriter::RewriteDescriptorList &DL) {
124 Descriptors.splice(Descriptors.begin(), DL);
127 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
130 bool runImpl(Module &M);
133 void loadAndParseMapFiles();
135 SymbolRewriter::RewriteDescriptorList Descriptors;
138 } // end namespace llvm
140 #endif //LLVM_TRANSFORMS_UTILS_SYMBOLREWRITER_H