1//===- SymbolRewriter.h - Symbol Rewriting Pass -----------------*- C++ -*-===// 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 file provides the prototypes and definitions related to the Symbol 10// Rewriter pass. 11// 12// The Symbol Rewriter pass takes a set of rewrite descriptors which define 13// transformations for symbol names. These can be either single name to name 14// trnsformation or more broad regular expression based transformations. 15// 16// All the functions are re-written at the IR level. The Symbol Rewriter itself 17// is exposed as a module level pass. All symbols at the module level are 18// iterated. For any matching symbol, the requested transformation is applied, 19// updating references to it as well (a la RAUW). The resulting binary will 20// only contain the rewritten symbols. 21// 22// By performing this operation in the compiler, we are able to catch symbols 23// that would otherwise not be possible to catch (e.g. inlined symbols). 24// 25// This makes it possible to cleanly transform symbols without resorting to 26// overly-complex macro tricks and the pre-processor. An example of where this 27// is useful is the sanitizers where we would like to intercept a well-defined 28// set of functions across the module. 29// 30//===----------------------------------------------------------------------===// 31 32#ifndef LLVM_TRANSFORMS_UTILS_SYMBOLREWRITER_H 33#define LLVM_TRANSFORMS_UTILS_SYMBOLREWRITER_H 34 35#include "llvm/IR/PassManager.h" 36#include <list> 37#include <memory> 38#include <string> 39 40namespace llvm { 41 42class MemoryBuffer; 43class Module; 44 45namespace yaml { 46 47class KeyValueNode; 48class MappingNode; 49class ScalarNode; 50class Stream; 51 52} // end namespace yaml 53 54namespace SymbolRewriter { 55 56/// The basic entity representing a rewrite operation. It serves as the base 57/// class for any rewrite descriptor. It has a certain set of specializations 58/// which describe a particular rewrite. 59/// 60/// The RewriteMapParser can be used to parse a mapping file that provides the 61/// mapping for rewriting the symbols. The descriptors individually describe 62/// whether to rewrite a function, global variable, or global alias. Each of 63/// these can be selected either by explicitly providing a name for the ones to 64/// be rewritten or providing a (posix compatible) regular expression that will 65/// select the symbols to rewrite. This descriptor list is passed to the 66/// SymbolRewriter pass. 67class RewriteDescriptor { 68public: 69 enum class Type { 70 Invalid, /// invalid 71 Function, /// function - descriptor rewrites a function 72 GlobalVariable, /// global variable - descriptor rewrites a global variable 73 NamedAlias, /// named alias - descriptor rewrites a global alias 74 }; 75 76 RewriteDescriptor(const RewriteDescriptor &) = delete; 77 RewriteDescriptor &operator=(const RewriteDescriptor &) = delete; 78 virtual ~RewriteDescriptor() = default; 79 80 Type getType() const { return Kind; } 81 82 virtual bool performOnModule(Module &M) = 0; 83 84protected: 85 explicit RewriteDescriptor(Type T) : Kind(T) {} 86 87private: 88 const Type Kind; 89}; 90 91using RewriteDescriptorList = std::list<std::unique_ptr<RewriteDescriptor>>; 92 93class RewriteMapParser { 94public: 95 bool parse(const std::string &MapFile, RewriteDescriptorList *Descriptors); 96 97private: 98 bool parse(std::unique_ptr<MemoryBuffer> &MapFile, RewriteDescriptorList *DL); 99 bool parseEntry(yaml::Stream &Stream, yaml::KeyValueNode &Entry, 100 RewriteDescriptorList *DL); 101 bool parseRewriteFunctionDescriptor(yaml::Stream &Stream, 102 yaml::ScalarNode *Key, 103 yaml::MappingNode *Value, 104 RewriteDescriptorList *DL); 105 bool parseRewriteGlobalVariableDescriptor(yaml::Stream &Stream, 106 yaml::ScalarNode *Key, 107 yaml::MappingNode *Value, 108 RewriteDescriptorList *DL); 109 bool parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K, 110 yaml::MappingNode *V, 111 RewriteDescriptorList *DL); 112}; 113 114} // end namespace SymbolRewriter 115 116class RewriteSymbolPass : public PassInfoMixin<RewriteSymbolPass> { 117public: 118 RewriteSymbolPass() { loadAndParseMapFiles(); } 119 120 RewriteSymbolPass(SymbolRewriter::RewriteDescriptorList &DL) { 121 Descriptors.splice(Descriptors.begin(), DL); 122 } 123 124 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 125 126 // Glue for old PM 127 bool runImpl(Module &M); 128 129private: 130 void loadAndParseMapFiles(); 131 132 SymbolRewriter::RewriteDescriptorList Descriptors; 133}; 134 135} // end namespace llvm 136 137#endif // LLVM_TRANSFORMS_UTILS_SYMBOLREWRITER_H 138